Example #1
0
 public override bool ShouldSkip(Pawn pawn, bool forced = false)
 {
     if (forced)
     {
         CELogger.Message("Job is forced. Not skipping.");
         return(false);
     }
     if (pawn.CurJob == null)
     {
         CELogger.Message($"Pawn {pawn.ThingID} has no job. Not skipping.");
         return(false);
     }
     if (pawn.CurJobDef == JobDefOf.ManTurret)
     {
         CELogger.Message($"Pawn {pawn.ThingID}'s current job is {nameof(JobDefOf.ManTurret)}. Not skipping.");
         return(false);
     }
     if (pawn.CurJob.playerForced)
     {
         CELogger.Message($"Pawn {pawn.ThingID}'s current job is forced by the player. Skipping.");
         return(true);
     }
     CELogger.Message($"Pawn {pawn.ThingID}'s current job is {pawn.CurJobDef.reportString}. Skipping");
     return(pawn.CurJobDef == CE_JobDefOf.ReloadTurret || pawn.CurJobDef == CE_JobDefOf.ReloadWeapon);
 }
        public static Job MakeReloadJob(Pawn pawn, Building_Turret turret)
        {
            var compAmmo = turret.GetAmmo();

            if (compAmmo == null)
            {
                CELogger.Error($"{pawn} tried to create a reload job on a thing ({turret}) that's not reloadable.");
                return(null);
            }

            if (!compAmmo.UseAmmo)
            {
                return(MakeReloadJobNoAmmo(turret));
            }

            var ammo = FindBestAmmo(pawn, turret);

            if (ammo == null)
            {
                CELogger.Error($"{pawn} tried to create a reload job without ammo. This should have been checked earlier.");
                return(null);
            }
            CELogger.Message($"Making a reload job for {pawn}, {turret} and {ammo}");

            Job job = JobMaker.MakeJob(CE_JobDefOf.ReloadTurret, turret, ammo);

            job.count = Mathf.Min(ammo.stackCount, compAmmo.MissingToFullMagazine);
            return(job);
        }
Example #3
0
        public static bool CanReload(Pawn pawn, Thing hopefullyTurret, bool forced = false, bool emergency = false)
        {
            if (pawn == null || hopefullyTurret == null)
            {
                CELogger.Error($"{pawn?.ToString() ?? "null pawn"} could not reload {hopefullyTurret?.ToString() ?? "null thing"} one of the two was null.");
                return(false);
            }
            if (!(hopefullyTurret is Building_TurretGunCE))
            {
                CELogger.Error($"{pawn} could not reload {hopefullyTurret} because {hopefullyTurret} is not a Combat Extended Turret. If you are a modder, make sure to use {nameof(CombatExtended)}.{nameof(Building_TurretGunCE)} for your turret's compClass.");
                return(false);
            }
            var turret   = hopefullyTurret as Building_TurretGunCE;
            var compAmmo = turret.CompAmmo;

            if (compAmmo == null)
            {
                CELogger.Error($"{pawn} could not reload {turret} because turret has no {nameof(CompAmmoUser)}.");
                return(false);
            }
            if (turret.isReloading)
            {
                CELogger.Message($"{pawn} could not reload {turret} because turret is already reloading.");
                JobFailReason.Is("CE_TurretAlreadyReloading".Translate());
                return(false);
            }
            if (turret.IsBurning() && !emergency)
            {
                CELogger.Message($"{pawn} could not reload {turret} because turret is on fire.");
                JobFailReason.Is("CE_TurretIsBurning".Translate());
            }
            if (compAmmo.FullMagazine)
            {
                CELogger.Message($"{pawn} could not reload {turret} because it is full of ammo.");
                JobFailReason.Is("CE_TurretFull".Translate());
                return(false);
            }
            if (turret.IsForbidden(pawn) || !pawn.CanReserve(turret, 1, -1, null, forced))
            {
                CELogger.Message($"{pawn} could not reload {turret} because it is forbidden or otherwise busy.");
                return(false);
            }
            if (turret.Faction != pawn.Faction && (turret.Faction != null && pawn.Faction != null && turret.Faction.RelationKindWith(pawn.Faction) != FactionRelationKind.Ally))
            {
                CELogger.Message($"{pawn} could not reload {turret} because the turret is hostile to them.");
                JobFailReason.Is("CE_TurretNonAllied".Translate());
                return(false);
            }
            if ((turret.MannableComp?.ManningPawn != pawn) && !pawn.CanReserveAndReach(turret, PathEndMode.ClosestTouch, forced ? Danger.Deadly : pawn.NormalMaxDanger(), MagicMaxPawns))
            {
                CELogger.Message($"{pawn} could not reload {turret} because turret is manned (or was recently manned) by someone else.");
                return(false);
            }
            if (FindBestAmmo(pawn, turret) == null)
            {
                JobFailReason.Is("CE_NoAmmoAvailable".Translate());
                return(false);
            }
            return(true);
        }
Example #4
0
        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            if (!pawn.Reserve(TargetA, job))
            {
                CELogger.Message("Combat Extended: Could not reserve turret for reloading job.");
                return(false);
            }

            var compAmmo = turret?.CompAmmo;

            if (compAmmo == null)
            {
                CELogger.Error($"{TargetA} has no CompAmmo, this should not have been reached.");
                return(false);
            }

            if (!compAmmo.UseAmmo)
            {
                return(true);
            }

            if (ammo == null)
            {
                CELogger.Message("Combat Extended: Ammo is null");
                return(false);
            }
            if (!pawn.Reserve(TargetB, job, Mathf.Max(1, TargetThingB.stackCount - job.count), job.count))
            {
                CELogger.Message("Combat Extended: Could not reserve " + Mathf.Max(1, TargetThingB.stackCount - job.count) + " of ammo.");
                return(false);
            }
            CELogger.Message("Combat Extended: Managed to reserve everything successfully.");
            return(true);
        }
Example #5
0
        public override void SpawnSetup(Map map, bool respawningAfterLoad)      //Add mannableComp, ticksUntilAutoReload, register to GenClosestAmmo
        {
            base.SpawnSetup(map, respawningAfterLoad);
            dormantComp  = GetComp <CompCanBeDormant>();
            powerComp    = GetComp <CompPowerTrader>();
            mannableComp = GetComp <CompMannable>();
            if (!respawningAfterLoad)
            {
                CELogger.Message($"top is {top?.ToString() ?? "null"}");
                top.SetRotationFromOrientation();
                burstCooldownTicksLeft = def.building.turretInitialCooldownTime.SecondsToTicks();

                //Delay auto-reload for a few seconds after spawn, so player can operate the turret right after placing it, before other colonists start reserving it for reload jobs
                if (mannableComp != null)
                {
                    ticksUntilAutoReload = minTicksBeforeAutoReload;
                }
            }

            // if (CompAmmo == null || CompAmmo.Props == null || CompAmmo.Props.ammoSet == null || CompAmmo.Props.ammoSet.ammoTypes.NullOrEmpty())
            //     return;

            // //"Subscribe" turret to GenClosestAmmo
            // foreach (var ammo in CompAmmo.Props.ammoSet.ammoTypes.Select(x => x.ammo))
            // {
            //     if (!GenClosestAmmo.listeners.ContainsKey(ammo))
            //         GenClosestAmmo.listeners.Add(ammo, new List<Building_TurretGunCE>() { this });
            //     else
            //         GenClosestAmmo.listeners[ammo].Add(this);

            //     if (!GenClosestAmmo.latestAmmoUpdate.ContainsKey(ammo))
            //         GenClosestAmmo.latestAmmoUpdate.Add(ammo, 0);
            // }
        }
Example #6
0
        public static Job MakeReloadJobNoAmmo(Building_TurretGunCE turret)
        {
            var compAmmo = turret.TryGetComp <CompAmmoUser>();

            if (compAmmo == null)
            {
                CELogger.Error("Tried to create a reload job on a thing that's not reloadable.");
                return(null);
            }

            return(JobMaker.MakeJob(CE_JobDefOf.ReloadTurret, turret, null));
        }
        private static Job MakeReloadJobNoAmmo(Building_Turret turret)
        {
            var compAmmo = turret.GetAmmo();

            if (compAmmo == null)
            {
                CELogger.Error("Tried to create a reload job on a thing that's not reloadable.");
                return(null);
            }

            return(JobMaker.MakeJob(CE_JobDefOf.ReloadTurret, turret, null));
        }
Example #8
0
        private static bool ImpactSomething(ProjectileCE projectile, Thing launcher)
        {
            var     map           = projectile.Map;
            Vector3 exactPosition = projectile.ExactPosition;

            refreshShields(map);

            var blocked = shields.Any(building => ShieldInterceptsProjectile(building as Building_Shield, projectile, launcher));

            CELogger.Message($"Blocked {projectile}? -- {blocked}");
            return(blocked);
        }
Example #9
0
        //Checks before called (ONLY when in SCANNER):
        // - PawnCanUseWorkGiver(pawn, this)
        //      - nonColonistCanDo || isColonist
        //      - !WorkTagIsDisabled(def.workTags)
        //      - !ShouldSkip(pawn, false)
        //      - MissingRequiredCapacity(pawn) == null
        // - !t.IsForbidden(pawn)
        // - this.PotentialWorkThingRequest.Accepts(t),
        /// <summary>
        /// Called after HasJobOnThing by WorkGiver_Scanner, or by Building_TurretGunCE when turret tryReload with manningPawn
        /// </summary>
        /// <param name="pawn"></param>
        /// <param name="t"></param>
        /// <param name="forced"></param>
        /// <returns></returns>
        public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
        {
            Building_TurretGunCE turret = t as Building_TurretGunCE;

            if (turret == null)
            {
                CELogger.Error($"{pawn} tried to make a reload job on a {t} which isn't a turret. This should never be reached.");
            }

            // NOTE: The removal of the code that used to be here disables reloading turrets directly from one's inventory.
            // The player would need to drop the ammo the pawn is holding first.

            return(JobGiverUtils_Reload.MakeReloadJob(pawn, turret));
        }
 public static Thing GetGun(this Building_Turret turret)
 {
     if (turret is Building_TurretGunCE ceturret)
     {
         return(ceturret.Gun);
     }
     if (enabled)
     {
         GetGunFunction gfunc;
         if (getGun.TryGetValue(turret.GetType(), out gfunc))
         {
             return(gfunc(turret));
         }
     }
     CELogger.Warn("Asked to get gun on an unknown turret type: " + turret);
     return(null);
 }
 public static bool GetReloading(this Building_Turret turret)
 {
     if (turret is Building_TurretGunCE ceturret)
     {
         return(ceturret.isReloading);
     }
     if (enabled)
     {
         GetReloadingFunction func;
         if (getReloading.TryGetValue(turret.GetType(), out func))
         {
             return(func(turret));
         }
     }
     CELogger.Warn("Asked to get reloading on an unknown turret type: " + turret);
     return(false);
 }
 public static void SetReloading(this Building_Turret turret, bool reloading)
 {
     if (turret is Building_TurretGunCE ceturret)
     {
         ceturret.isReloading = reloading;
         return;
     }
     if (enabled)
     {
         SetReloadingFunction func;
         if (setReloading.TryGetValue(turret.GetType(), out func))
         {
             func(turret, reloading);
             return;
         }
     }
     CELogger.Warn("Asked to set reloading on an unknown turret type: " + turret);
 }
Example #13
0
        private float GetThingPriority(Pawn pawn, Thing t, bool forced = false)
        {
            CELogger.Message($"pawn: {pawn}. t: {t}. forced: {forced}");
            Building_TurretGunCE turret = t as Building_TurretGunCE;

            if (!turret.Active)
            {
                return(1f);
            }
            if (turret.EmptyMagazine)
            {
                return(9f);
            }
            if (turret.AutoReloadableMagazine)
            {
                return(5f);
            }
            return(1f);
        }
Example #14
0
 public override bool TryMakePreToilReservations(bool errorOnFailed)
 {
     if (!pawn.Reserve(TargetA, job))
     {
         CELogger.Message("Combat Extended: Could not reserve turret for reloading job.");
         return(false);
     }
     if (ammo == null)
     {
         CELogger.Message("Combat Extended: Ammo is null");
         return(false);
     }
     if (!pawn.Reserve(TargetB, job, Mathf.Max(1, TargetThingB.stackCount - job.count), job.count))
     {
         CELogger.Message("Combat Extended: Could not reserve " + Mathf.Max(1, TargetThingB.stackCount - job.count) + " of ammo.");
         return(false);
     }
     CELogger.Message("Combat Extended: Managed to reserve everything successfully.");
     return(true);
 }
Example #15
0
        /// <summary>
        /// Called as a scanner
        /// </summary>
        /// <param name="pawn">Never null</param>
        /// <param name="t">Can be non-turret</param>
        /// <param name="forced">Generally not true</param>
        /// <returns></returns>

        public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
        {
            // Should never happen anymore, as only TurretGunCEs should be returned from PotentialWorkThingsGlobal
            if (!(t is Building_TurretGunCE))
            {
                return(false);
            }

            var priority = GetThingPriority(pawn, t, forced);

            CELogger.Message($"Priority check completed. Got {priority}");

            Building_TurretGunCE turret = t as Building_TurretGunCE;

            CELogger.Message($"Turret uses ammo? {turret.CompAmmo.UseAmmo}");

            CELogger.Message($"Total magazine size: {turret.CompAmmo.Props.magazineSize}. Needed: {turret.CompAmmo.MissingToFullMagazine}");

            return(JobGiverUtils_Reload.CanReload(pawn, turret, forced));
        }
Example #16
0
        private float GetThingPriority(Pawn pawn, Thing t, bool forced = false)
        {
            CELogger.Message($"pawn: {pawn}. t: {t}. forced: {forced}");
            Building_Turret turret = t as Building_Turret;



            if (!((turret as Building_TurretGunCE)?.Active ?? true))
            {
                return(1f);
            }
            if (turret.GetAmmo()?.EmptyMagazine ?? false)
            {
                return(9f);
            }
            if (turret.GetMannable() == null)
            {
                return(5f);
            }
            return(1f);
        }
Example #17
0
        /// <summary>
        /// Called as a scanner
        /// </summary>
        /// <param name="pawn">Never null</param>
        /// <param name="t">Can be non-turret</param>
        /// <param name="forced">Generally not true</param>
        /// <returns></returns>

        public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
        {
            if (!(t is Building_TurretGunCE))
            {
                return(false);
            }
            var priority = GetThingPriority(pawn, t, forced);

            CELogger.Message($"Priority check completed. Got {priority}");

            Building_TurretGunCE turret = t as Building_TurretGunCE;

            CELogger.Message($"Turret uses ammo? {turret.CompAmmo.UseAmmo}");
            if (!turret.CompAmmo.UseAmmo)
            {
                return(true);
            }

            CELogger.Message($"Total magazine size: {turret.CompAmmo.Props.magazineSize}. Needed: {turret.CompAmmo.MissingToFullMagazine}");

            return(JobGiverUtils_Reload.CanReload(pawn, turret, forced));
        }
 public static CompAmmoUser GetAmmo(this Building_Turret turret)
 {
     if (turret is Building_TurretGunCE ceturret)
     {
         return(ceturret.CompAmmo);
     }
     if (enabled)
     {
         GetAmmoFunction func;
         if (getAmmo.TryGetValue(turret.GetType(), out func))
         {
             return(func(turret));
         }
         GetGunFunction gfunc;
         if (getGun.TryGetValue(turret.GetType(), out gfunc))
         {
             return(gfunc(turret)?.TryGetComp <CompAmmoUser>());
         }
     }
     CELogger.Warn("Asked to get ammo on an unknown turret type: " + turret);
     return(null);
 }