]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix emergency shuttle initialization
authorElectroJr <leonsfriedrich@gmail.com>
Fri, 19 Apr 2024 22:57:12 +0000 (18:57 -0400)
committerElectroJr <leonsfriedrich@gmail.com>
Fri, 19 Apr 2024 22:57:12 +0000 (18:57 -0400)
Content.Server/RoundEnd/RoundEndSystem.cs
Content.Server/Shuttles/Components/StationEmergencyShuttleComponent.cs
Content.Server/Shuttles/Systems/EmergencyShuttleSystem.cs
Content.Server/Station/Events/StationPostInitEvent.cs
Content.Server/Station/Systems/StationSystem.cs

index 3a8331f3f7a68d3522be332224e70da75231d281..758e2e314640a1ea0e07d8aecea5817d3c9f2f16 100644 (file)
@@ -145,11 +145,15 @@ namespace Content.Server.RoundEnd
 
         public void RequestRoundEnd(TimeSpan countdownTime, EntityUid? requester = null, bool checkCooldown = true, string text = "round-end-system-shuttle-called-announcement", string name = "Station")
         {
-            if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
+            if (_gameTicker.RunLevel != GameRunLevel.InRound)
+                return;
 
-            if (checkCooldown && _cooldownTokenSource != null) return;
+            if (checkCooldown && _cooldownTokenSource != null)
+                return;
+
+            if (_countdownTokenSource != null)
+                return;
 
-            if (_countdownTokenSource != null) return;
             _countdownTokenSource = new();
 
             if (requester != null)
index bdfdcb273aa0b2a6d177c086fa77447e969bd937..58d23ee43263cb0a3cd5125e45739d53da2333d6 100644 (file)
@@ -13,7 +13,7 @@ public sealed partial class StationEmergencyShuttleComponent : Component
     /// <summary>
     /// The emergency shuttle assigned to this station.
     /// </summary>
-    [ViewVariables, Access(typeof(ShuttleSystem), typeof(EmergencyShuttleSystem), Friend = AccessPermissions.ReadWrite)]
+    [DataField, Access(typeof(ShuttleSystem), typeof(EmergencyShuttleSystem), Friend = AccessPermissions.ReadWrite)]
     public EntityUid? EmergencyShuttle;
 
     /// <summary>
index 16a5e26b5426ef9a5d18186035d5c89fb1ddc8c5..a63b182fa67376e35abc0444c756e1fc521c725e 100644 (file)
@@ -83,9 +83,9 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
 
         SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
         SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundCleanup);
-        SubscribeLocalEvent<StationEmergencyShuttleComponent, ComponentStartup>(OnStationStartup);
+        SubscribeLocalEvent<StationEmergencyShuttleComponent, StationPostInitEvent>(OnStationStartup);
         SubscribeLocalEvent<StationCentcommComponent, ComponentShutdown>(OnCentcommShutdown);
-        SubscribeLocalEvent<StationCentcommComponent, StationPostInitEvent>(OnStationPostInit);
+        SubscribeLocalEvent<StationCentcommComponent, MapInitEvent>(OnStationInit);
 
         SubscribeLocalEvent<EmergencyShuttleComponent, FTLStartedEvent>(OnEmergencyFTL);
         SubscribeLocalEvent<EmergencyShuttleComponent, FTLCompletedEvent>(OnEmergencyFTLComplete);
@@ -320,8 +320,10 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
         }
     }
 
-    private void OnStationPostInit(EntityUid uid, StationCentcommComponent component, ref StationPostInitEvent args)
+    private void OnStationInit(EntityUid uid, StationCentcommComponent component, MapInitEvent args)
     {
+        // This is handled on map-init, so that centcomm has finished initializing by the time the StationPostInitEvent
+        // gets raised
         if (!_emergencyShuttleEnabled)
             return;
 
@@ -332,12 +334,12 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
             return;
         }
 
-        AddCentcomm(component);
+        AddCentcomm(uid, component);
     }
 
-    private void OnStationStartup(EntityUid uid, StationEmergencyShuttleComponent component, ComponentStartup args)
+    private void OnStationStartup(Entity<StationEmergencyShuttleComponent> ent, ref StationPostInitEvent args)
     {
-        AddEmergencyShuttle(uid, component);
+        AddEmergencyShuttle((ent, ent));
     }
 
     /// <summary>
@@ -374,19 +376,22 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
 
         var centcommQuery = AllEntityQuery<StationCentcommComponent>();
 
-        while (centcommQuery.MoveNext(out var centcomm))
+        while (centcommQuery.MoveNext(out var uid, out var centcomm))
         {
-            AddCentcomm(centcomm);
+            AddCentcomm(uid, centcomm);
         }
 
         var query = AllEntityQuery<StationEmergencyShuttleComponent>();
 
         while (query.MoveNext(out var uid, out var comp))
-            AddEmergencyShuttle(uid, comp);
+        {
+            AddEmergencyShuttle((uid, comp));
+        }
     }
 
-    private void AddCentcomm(StationCentcommComponent component)
+    private void AddCentcomm(EntityUid station, StationCentcommComponent component)
     {
+        DebugTools.Assert(LifeStage(station)>= EntityLifeStage.MapInitialized);
         if (component.MapEntity != null || component.Entity != null)
         {
             Log.Warning("Attempted to re-add an existing centcomm map.");
@@ -402,7 +407,7 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
 
             if (!Exists(otherComp.MapEntity) || !Exists(otherComp.Entity))
             {
-                Log.Error($"Disconvered invalid centcomm component?");
+                Log.Error($"Discovered invalid centcomm component?");
                 ClearCentcomm(otherComp);
                 continue;
             }
@@ -451,6 +456,7 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
         component.MapEntity = map;
         component.Entity = grid;
         _shuttle.TryAddFTLDestination(mapId, false, out _);
+        Log.Info($"Created centcomm grid {ToPrettyString(grid)} on map {ToPrettyString(map)} for station {ToPrettyString(station)}");
     }
 
     public HashSet<EntityUid> GetCentcommMaps()
@@ -467,49 +473,67 @@ public sealed partial class EmergencyShuttleSystem : EntitySystem
         return maps;
     }
 
-    private void AddEmergencyShuttle(EntityUid uid, StationEmergencyShuttleComponent component)
+    private void AddEmergencyShuttle(Entity<StationEmergencyShuttleComponent?, StationCentcommComponent?> ent)
     {
-        if (!_emergencyShuttleEnabled
-            || component.EmergencyShuttle != null ||
-            !TryComp<StationCentcommComponent>(uid, out var centcomm)
-            || !TryComp(centcomm.MapEntity, out MapComponent? map))
+        if (!Resolve(ent.Owner, ref ent.Comp1, ref ent.Comp2))
+            return;
+
+        if (!_emergencyShuttleEnabled)
+            return;
+
+        if (ent.Comp1.EmergencyShuttle != null )
         {
+            if (Exists(ent.Comp1.EmergencyShuttle))
+            {
+                Log.Error($"Attempted to add an emergency shuttle to {ToPrettyString(ent)}, despite a shuttle already existing?");
+                return;
+            }
+
+            Log.Error($"Encountered deleted emergency shuttle during initialization of {ToPrettyString(ent)}");
+            ent.Comp1.EmergencyShuttle = null;
+        }
+
+        if (!TryComp(ent.Comp2.MapEntity, out MapComponent? map))
+        {
+            Log.Error($"Failed to add emergency shuttle - centcomm has not been initialized? {ToPrettyString(ent)}");
             return;
         }
 
         // Load escape shuttle
-        var shuttlePath = component.EmergencyShuttlePath;
+        var shuttlePath = ent.Comp1.EmergencyShuttlePath;
         var shuttle = _map.LoadGrid(map.MapId, shuttlePath.ToString(), new MapLoadOptions()
         {
             // Should be far enough... right? I'm too lazy to bounds check CentCom rn.
-            Offset = new Vector2(500f + centcomm.ShuttleIndex, 0f),
+            Offset = new Vector2(500f + ent.Comp2.ShuttleIndex, 0f),
             // fun fact: if you just fucking yeet centcomm into nullspace anytime you try to spawn the shuttle, then any distance is far enough. so lets not do that
             LoadMap = false,
         });
 
         if (shuttle == null)
         {
-            Log.Error($"Unable to spawn emergency shuttle {shuttlePath} for {ToPrettyString(uid)}");
+            Log.Error($"Unable to spawn emergency shuttle {shuttlePath} for {ToPrettyString(ent)}");
             return;
         }
 
-        centcomm.ShuttleIndex += Comp<MapGridComponent>(shuttle.Value).LocalAABB.Width + ShuttleSpawnBuffer;
+        ent.Comp2.ShuttleIndex += Comp<MapGridComponent>(shuttle.Value).LocalAABB.Width + ShuttleSpawnBuffer;
 
         // Update indices for all centcomm comps pointing to same map
         var query = AllEntityQuery<StationCentcommComponent>();
 
         while (query.MoveNext(out var comp))
         {
-            if (comp == centcomm || comp.MapEntity != centcomm.MapEntity)
+            if (comp == ent.Comp2 || comp.MapEntity != ent.Comp2.MapEntity)
                 continue;
 
-            comp.ShuttleIndex = centcomm.ShuttleIndex;
+            comp.ShuttleIndex = ent.Comp2.ShuttleIndex;
         }
 
-        component.EmergencyShuttle = shuttle;
+        ent.Comp1.EmergencyShuttle = shuttle;
         EnsureComp<ProtectedGridComponent>(shuttle.Value);
         EnsureComp<PreventPilotComponent>(shuttle.Value);
         EnsureComp<EmergencyShuttleComponent>(shuttle.Value);
+
+        Log.Info($"Added emergency shuttle {ToPrettyString(shuttle)} for station {ToPrettyString(ent)} and centcomm {ToPrettyString(ent.Comp2.Entity)}");
     }
 
     /// <summary>
index 4f7927cee525f2833d370a5d0fd6323efd87db84..54b8eeeb312739dad8562d58694d0a8ac71148e2 100644 (file)
@@ -4,6 +4,8 @@ namespace Content.Server.Station.Events;
 
 /// <summary>
 /// Raised directed on a station after it has been initialized, as well as broadcast.
+/// This gets raised after the entity has been map-initialized, and the station's centcomm map/entity (if any) has been
+/// set up.
 /// </summary>
 [ByRefEvent]
 public readonly record struct StationPostInitEvent(Entity<StationDataComponent> Station);
index 408b3ebc55512f06be30446df7bab7da8161cca8..2ab33b62a50c98751c50da1838c7f70419f88bda 100644 (file)
@@ -294,8 +294,6 @@ public sealed class StationSystem : EntitySystem
         // Use overrides for setup.
         var station = EntityManager.SpawnEntity(stationConfig.StationPrototype, MapCoordinates.Nullspace, stationConfig.StationComponentOverrides);
 
-
-
         if (name is not null)
             RenameStation(station, name, false);
 
@@ -346,6 +344,9 @@ public sealed class StationSystem : EntitySystem
             }
         }
 
+        if (LifeStage(station) < EntityLifeStage.MapInitialized)
+            throw new Exception($"Station must be man-initialized");
+
         var ev = new StationPostInitEvent((station, data));
         RaiseLocalEvent(station, ref ev, true);