diff --git a/1.6/1.6/Assemblies/ArachnaeSwarm.dll b/1.6/1.6/Assemblies/ArachnaeSwarm.dll index c865eab..9ba058d 100644 Binary files a/1.6/1.6/Assemblies/ArachnaeSwarm.dll and b/1.6/1.6/Assemblies/ArachnaeSwarm.dll differ diff --git a/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs b/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs index 332f412..56d2c56 100644 --- a/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs +++ b/Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs @@ -118,8 +118,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..3a3205a 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) { @@ -57,17 +68,34 @@ namespace ArachnaeSwarm private void Arrived() { + // Safety check for invalid destination tile + if (destinationTile < 0) + { + Log.Error("CatastropheMissile cannot arrive at an invalid tile. Removing."); + Find.WorldObjects.Remove(this); + return; + } + 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);