// New methods

        public void TryOrderReload()
        {
            /*
             * if (mannableComp == null)
             * {
             *  if (!CompAmmo.useAmmo) CompAmmo.LoadAmmo();
             *  return;
             * }
             */

            if ((!mannableComp?.MannedNow ?? true) || (CompAmmo.CurrentAmmo == CompAmmo.SelectedAmmo && CompAmmo.CurMagCount == CompAmmo.Props.magazineSize))
            {
                return;
            }
            Job reloadJob = null;

            if (CompAmmo.UseAmmo)
            {
                CompInventory inventory = mannableComp.ManningPawn.TryGetComp <CompInventory>();
                if (inventory != null)
                {
                    Thing ammo = inventory.container.FirstOrDefault(x => x.def == CompAmmo.SelectedAmmo);

                    // NPC's switch ammo types
                    if (ammo == null)
                    {
                        ammo = inventory.container.FirstOrDefault(x => CompAmmo.Props.ammoSet.ammoTypes.Any(a => a.ammo == x.def));
                    }
                    if (ammo != null)
                    {
                        if (ammo.def != CompAmmo.SelectedAmmo)
                        {
                            CompAmmo.SelectedAmmo = ammo.def as AmmoDef;
                        }
                        Thing droppedAmmo;
                        int   amount = CompAmmo.Props.magazineSize;
                        if (CompAmmo.CurrentAmmo == CompAmmo.SelectedAmmo)
                        {
                            amount -= CompAmmo.CurMagCount;
                        }
                        if (inventory.container.TryDrop(ammo, this.Position, this.Map, ThingPlaceMode.Direct, Mathf.Min(ammo.stackCount, amount), out droppedAmmo))
                        {
                            reloadJob = new Job(CE_JobDefOf.ReloadTurret, this, droppedAmmo)
                            {
                                count = droppedAmmo.stackCount
                            };
                        }
                    }
                }
            }
            if (reloadJob == null)
            {
                reloadJob = new WorkGiver_ReloadTurret().JobOnThing(mannableComp.ManningPawn, this);
            }
            if (reloadJob != null)
            {
                var pawn = mannableComp.ManningPawn;
                pawn.jobs.StartJob(reloadJob, JobCondition.Ongoing, null, pawn.CurJob?.def != reloadJob.def);
            }
        }
Ejemplo n.º 2
0
        // New methods

        public void OrderReload()
        {
            if (mannableComp == null ||
                !mannableComp.MannedNow ||
                (compAmmo.currentAmmo == compAmmo.selectedAmmo && compAmmo.curMagCount == compAmmo.Props.magazineSize))
            {
                return;
            }
            Job           reloadJob = null;
            CompInventory inventory = mannableComp.ManningPawn.TryGetComp <CompInventory>();

            if (inventory != null)
            {
                Thing ammo = inventory.container.FirstOrDefault(x => x.def == compAmmo.selectedAmmo);
                if (ammo != null)
                {
                    Thing droppedAmmo;
                    int   amount = compAmmo.Props.magazineSize;
                    if (compAmmo.currentAmmo == compAmmo.selectedAmmo)
                    {
                        amount -= compAmmo.curMagCount;
                    }
                    if (inventory.container.TryDrop(ammo, this.Position, this.Map, ThingPlaceMode.Direct, Mathf.Min(ammo.stackCount, amount), out droppedAmmo))
                    {
                        reloadJob = new Job(DefDatabase <JobDef> .GetNamed("ReloadTurret"), this, droppedAmmo)
                        {
                            count = droppedAmmo.stackCount
                        };
                    }
                }
            }
            if (reloadJob == null)
            {
                reloadJob = new WorkGiver_ReloadTurret().JobOnThing(mannableComp.ManningPawn, this);
            }
            if (reloadJob != null)
            {
                mannableComp.ManningPawn.jobs.StartJob(reloadJob, JobCondition.Ongoing, null, true);
            }
        }
Ejemplo n.º 3
0
        public void TryOrderReload(bool forced = false)
        {
            //No reload necessary at all --
            if ((CompAmmo.CurrentAmmo == CompAmmo.SelectedAmmo && (!CompAmmo.HasMagazine || CompAmmo.CurMagCount == CompAmmo.Props.magazineSize)))
            {
                return;
            }

            //Non-mannableComp interaction
            if (!mannableComp?.MannedNow ?? true)
            {
                //If auto-turret test for "auto-reload"
                if (!forced && ticksUntilAutoReload > 0)
                {
                    return;
                }

                //Unmanned or autoturret -- cancel job on reserved turret
                if (Map.physicalInteractionReservationManager.IsReserved(new LocalTargetInfo(this)))
                {
                    return;
                }

                //If manningPawn is null, WorkGiver_ReloadTurret might find a Humanlike intelligence pawn to reload the turret -- however, this method won't pick one at random to do so
                //If manningPawn is null, try to assign jobs to a pawn of faction Mechanoids
                if (Faction.def != FactionDefOf.Mechanoid)
                {
                    return;
                }

                List <Pawn> pawns;
                if (this.GetLord() != null && this.GetLord().AnyActivePawn)     //Prevents mechs in ancient danger from joining in
                {
                    pawns = this.GetLord().ownedPawns;
                }
                //else if (Map.mapPawns?.PawnsInFaction(Faction).Any() ?? false)
                //{
                //    pawns = Map.mapPawns.PawnsInFaction(Faction);
                //}
                else
                {
                    return;     //Nothing could reload this
                }
                var giver = new WorkGiver_ReloadTurret();
                var pawn  = giver.BestNonJobUser(pawns.Where(x => x.RaceProps.intelligence == Intelligence.ToolUser
                                                             //|| (x.training?.HasLearned(CE_TrainableDefOf.Haul) ?? false)    //would allow trained pawns to reload as well
                                                             ), this, out bool fromInventory, forced);
                if (pawn == null)
                {
                    return;
                }

                Job reloadJob = null;
                if (!CompAmmo.UseAmmo)
                {
                    reloadJob = new Job(CE_JobDefOf.ReloadTurret, this, null);
                }
                else
                {
                    Thing ammo = fromInventory ? InventoryAmmo(pawn.TryGetComp <CompInventory>()) : nearestViableAmmo;
                    if (ammo != null)
                    {
                        if (ammo.def != CompAmmo.SelectedAmmo)
                        {
                            CompAmmo.SelectedAmmo = ammo.def as AmmoDef;
                        }

                        int amount = CompAmmo.Props.magazineSize;
                        if (CompAmmo.CurrentAmmo == CompAmmo.SelectedAmmo)
                        {
                            amount -= CompAmmo.CurMagCount;
                        }

                        if (fromInventory)
                        {
                            if (!pawn.TryGetComp <CompInventory>().container.TryDrop(ammo, Position, Map, ThingPlaceMode.Direct, Mathf.Min(ammo.stackCount, amount), out ammo))
                            {
                                Log.ErrorOnce("Found optimal ammo (" + ammo.LabelCap + "), but could not drop from " + pawn.LabelCap, 81274728);
                                return;
                            }
                        }

                        reloadJob = new Job(CE_JobDefOf.ReloadTurret, this, ammo)
                        {
                            count = Mathf.Min(ammo.stackCount, amount)
                        };
                    }
                }

                if (reloadJob != null)
                {
                    pawn.jobs.StartJob(reloadJob, JobCondition.Ongoing, null, pawn.CurJob?.def != reloadJob.def && (!pawn.CurJob?.def.isIdle ?? true));
                }
            }
            else    //MannableComp interaction
            {
                //Only have manningPawn reload after a long time of no firing
                if (!forced && Reloadable && (compAmmo.CurMagCount != 0 || ticksUntilAutoReload > 0))
                {
                    return;
                }

                //Already reserved for manning
                Pawn manningPawn = mannableComp.ManningPawn;
                if (manningPawn != null)
                {
                    UpdateNearbyAmmo(forced);
                    var jobOnThing = new WorkGiver_ReloadTurret().JobOnThing(manningPawn, this, forced);

                    if (jobOnThing != null)
                    {
                        manningPawn.jobs.StartJob(jobOnThing, JobCondition.Ongoing, null, manningPawn.CurJob?.def != CE_JobDefOf.ReloadTurret);
                    }

                    return;
                }
            }
        }