/// <summary>
        /// If RimWorld decides the pawn should do this job, this is called to get the pawn working on it.
        /// </summary>
        /// <param name="pawn">Pawn that the job is supposed to take place on.</param>
        /// <returns>Job that the pawn is to be working on.</returns>
        protected override Job TryGiveJob(Pawn pawn)
        {
            ThingWithComps gun;
            AmmoDef        ammo;
            Job            reloadJob = null;

            if (DoReloadCheck(pawn, out gun, out ammo))
            {
                CompAmmoUser  comp          = gun.TryGetComp <CompAmmoUser>();
                CompInventory compInventory = pawn.TryGetComp <CompInventory>();
                // we relied on DoReloadCheck() to do error checking of many variables.

                if (!comp.TryUnload())
                {
                    return(null);                                   // unload the weapon or stop trying if there was a problem.
                }
                // change ammo type if necessary.
                if (comp.UseAmmo && comp.CurrentAmmo != ammo)
                {
                    comp.SelectedAmmo = ammo;
                }

                // Get the reload job from the comp.
                reloadJob = comp.TryMakeReloadJob();
            }
            return(reloadJob);
        }
예제 #2
0
        private FloatMenu MakeAmmoMenu()
        {
            List <ThingDef> ammoList = new List <ThingDef>();      // List of all ammo types the gun can use and the pawn has in his inventory

            if (compAmmo.turret != null)
            {
                // If we have no inventory available (e.g. manned turret), add all possible ammo types to the selection
                foreach (AmmoLink link in compAmmo.Props.ammoSet.ammoTypes)
                {
                    ammoList.Add(link.ammo);
                }
            }
            else
            {
                // Iterate through all suitable ammo types and check if they're in our inventory
                foreach (AmmoLink curLink in compAmmo.Props.ammoSet.ammoTypes)
                {
                    if (compAmmo.CompInventory.ammoList.Any(x => x.def == curLink.ammo))
                    {
                        ammoList.Add(curLink.ammo);
                    }
                }
            }

            // Append float menu options for every available ammo type
            List <FloatMenuOption> floatOptionList = new List <FloatMenuOption>();

            if (ammoList.NullOrEmpty())
            {
                floatOptionList.Add(new FloatMenuOption("CE_OutOfAmmo".Translate(), null));
            }
            else
            {
                // Append all available ammo types
                foreach (ThingDef curDef in ammoList)
                {
                    AmmoDef ammoDef = (AmmoDef)curDef;
                    floatOptionList.Add(new FloatMenuOption(ammoDef.ammoClass.LabelCap, new Action(delegate {
                        bool shouldReload     = Controller.settings.AutoReloadOnChangeAmmo && (compAmmo.SelectedAmmo != ammoDef || compAmmo.CurMagCount < compAmmo.Props.magazineSize) && compAmmo.turret?.MannableComp == null;
                        compAmmo.SelectedAmmo = ammoDef;
                        if (shouldReload)
                        {
                            if (compAmmo.turret != null)
                            {
                                compAmmo.turret.TryOrderReload();
                            }
                            else
                            {
                                compAmmo.TryStartReload();
                            }
                        }
                    })));
                }
            }
            // Append unload command
            var hasOperator = compAmmo.Wielder != null || (compAmmo.turret?.MannableComp?.MannedNow ?? false);

            if (compAmmo.UseAmmo && hasOperator && compAmmo.HasMagazine && compAmmo.CurMagCount > 0)
            {
                floatOptionList.Add(new FloatMenuOption("CE_UnloadLabel".Translate(), new Action(delegate { compAmmo.TryUnload(); })));
            }
            // Append reload command
            if (compAmmo.HasMagazine && hasOperator)
            {
                floatOptionList.Add(new FloatMenuOption("CE_ReloadLabel".Translate(), new Action(action)));
            }
            return(new FloatMenu(floatOptionList));
        }