diff --git a/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs b/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs index 332f412..8a626db 100644 --- a/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs +++ b/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs @@ -37,6 +37,13 @@ namespace ArachnaeSwarm // Base tick handles all local targeting, cooldowns, and fuel consumption via XML. base.Tick(); + // --- Target Sanity Checks --- + // If we have a remote target, check if it's still valid. If not, stop targeting. + if (this.longTarget.HasThing && (this.longTarget.ThingDestroyed || !this.longTarget.Thing.Spawned)) + { + this.longTarget = GlobalTargetInfo.Invalid; + } + // If a local target is active, prevent remote targeting. if (this.forcedTarget.IsValid && this.longTarget.IsValid) { @@ -118,8 +125,9 @@ namespace ArachnaeSwarm DefDatabase.GetNamed("CatastropheMissile_Flying") ); missile.Tile = this.Map.Tile; - missile.destinationTile = target.Tile; - missile.destinationCell = target.Cell; + missile.primaryTarget = target; // The primary target (could be a Thing) + missile.destinationTile = target.Tile; // The map tile + missile.destinationCell = target.Cell; // The fallback cell position missile.Projectile = DefDatabase.GetNamed("Projectile_CatastropheMissile"); Find.WorldObjects.Add(missile); diff --git a/Source/ArachnaeSwarm/World/WorldObject_CatastropheMissile.cs b/Source/ArachnaeSwarm/World/WorldObject_CatastropheMissile.cs index c3169da..194a07c 100644 --- a/Source/ArachnaeSwarm/World/WorldObject_CatastropheMissile.cs +++ b/Source/ArachnaeSwarm/World/WorldObject_CatastropheMissile.cs @@ -7,8 +7,9 @@ namespace ArachnaeSwarm { public class WorldObject_CatastropheMissile : WorldObject { - public int destinationTile = -1; - public IntVec3 destinationCell = IntVec3.Invalid; + public GlobalTargetInfo primaryTarget; // The actual target (can be a Thing) + public int destinationTile = -1; // The map tile of the target + public IntVec3 destinationCell = IntVec3.Invalid; // The fallback cell position public ThingDef Projectile; private int initialTile = -1; @@ -18,6 +19,7 @@ namespace ArachnaeSwarm public override void ExposeData() { base.ExposeData(); + Scribe_TargetInfo.Look(ref primaryTarget, "primaryTarget"); Scribe_Values.Look(ref destinationTile, "destinationTile", 0); Scribe_Values.Look(ref destinationCell, "destinationCell"); Scribe_Defs.Look(ref Projectile, "Projectile"); @@ -39,6 +41,15 @@ namespace ArachnaeSwarm protected override void Tick() { base.Tick(); + + // Safety check: if the destination is invalid, log an error and remove the missile. + if (this.destinationTile < 0) + { + Log.Error("CatastropheMissile spawned with an invalid destination tile. Removing."); + Find.WorldObjects.Remove(this); + return; + } + float distance = GenMath.SphericalDistance(StartPos.normalized, EndPos.normalized); if(distance > 0) { @@ -60,14 +71,23 @@ namespace ArachnaeSwarm Map targetMap = Current.Game.FindMap(this.destinationTile); if (targetMap != null) { - // Find a random entry point at the north edge of the target map - IntVec3 entryCell = CellFinder.RandomEdgeCell(Rot4.North, targetMap); - - // Spawn the final projectile (the cruise missile) at the entry point - Projectile_CruiseMissile missile = (Projectile_CruiseMissile)GenSpawn.Spawn(this.Projectile, entryCell, targetMap, WipeMode.Vanish); + // Determine the final target. + LocalTargetInfo finalTarget; + // Check if the primary target (Thing) is still valid and on the correct map. + if (primaryTarget.HasThing && primaryTarget.Thing.Spawned && primaryTarget.Thing.Map == targetMap) + { + // Target is still valid, aim at its current position. + finalTarget = new LocalTargetInfo(primaryTarget.Thing); + } + else + { + // Target is invalid (destroyed, moved maps, etc.), use the fallback position. + finalTarget = new LocalTargetInfo(this.destinationCell); + } - // Launch it from the entry point towards the final destination cell - missile.Launch(null, entryCell.ToVector3Shifted(), new LocalTargetInfo(this.destinationCell), new LocalTargetInfo(this.destinationCell), ProjectileHitFlags.IntendedTarget); + IntVec3 entryCell = CellFinder.RandomEdgeCell(Rot4.North, targetMap); + Projectile_CruiseMissile missile = (Projectile_CruiseMissile)GenSpawn.Spawn(this.Projectile, entryCell, targetMap, WipeMode.Vanish); + missile.Launch(null, entryCell.ToVector3Shifted(), finalTarget, finalTarget, ProjectileHitFlags.IntendedTarget); } Find.WorldObjects.Remove(this);