Ejemplo n.º 1
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;
                }
            }
        }