diff --git a/Content.Server/AlertLevel/AlertLevelSystem.cs b/Content.Server/AlertLevel/AlertLevelSystem.cs index 6fcafdaf7f..c0c0f6fd20 100644 --- a/Content.Server/AlertLevel/AlertLevelSystem.cs +++ b/Content.Server/AlertLevel/AlertLevelSystem.cs @@ -173,7 +173,7 @@ public sealed class AlertLevelSystem : EntitySystem { if (detail.Sound != null) { - var filter = _stationSystem.GetInStation(station); + var filter = _stationSystem.GetInOwningStation(station); SoundSystem.Play(detail.Sound.GetSound(), filter, detail.Sound.Params); } else diff --git a/Content.Server/Audio/ServerGlobalSoundSystem.cs b/Content.Server/Audio/ServerGlobalSoundSystem.cs index 47f4821e41..706cf8c15d 100644 --- a/Content.Server/Audio/ServerGlobalSoundSystem.cs +++ b/Content.Server/Audio/ServerGlobalSoundSystem.cs @@ -36,7 +36,7 @@ public sealed class ServerGlobalSoundSystem : SharedGlobalSoundSystem private Filter GetStationAndPvs(EntityUid source) { - var stationFilter = _stationSystem.GetInStation(source); + var stationFilter = _stationSystem.GetInOwningStation(source); stationFilter.AddPlayersByPvs(source, entityManager: EntityManager); return stationFilter; } diff --git a/Content.Server/Station/Systems/StationSystem.cs b/Content.Server/Station/Systems/StationSystem.cs index ba3b6e02fc..5a6c6625c0 100644 --- a/Content.Server/Station/Systems/StationSystem.cs +++ b/Content.Server/Station/Systems/StationSystem.cs @@ -1,6 +1,4 @@ using System.Linq; -using Content.Server.Chat; -using Content.Server.Chat.Managers; using Content.Server.Chat.Systems; using Content.Server.GameTicking; using Content.Server.Station.Components; @@ -61,6 +59,8 @@ public sealed class StationSystem : EntitySystem SubscribeLocalEvent(OnStationAdd); SubscribeLocalEvent(OnStationDeleted); SubscribeLocalEvent(OnParentChanged); + SubscribeLocalEvent(OnStationGridDeleted); + SubscribeLocalEvent(OnStationSplitEvent); _configurationManager.OnValueChanged(CCVars.StationOffset, x => _randomStationOffset = x, true); _configurationManager.OnValueChanged(CCVars.MaxStationOffset, x => _maxRandomStationOffset = x, true); @@ -69,6 +69,22 @@ public sealed class StationSystem : EntitySystem _player.PlayerStatusChanged += OnPlayerStatusChanged; } + private void OnStationSplitEvent(EntityUid uid, StationMemberComponent component, ref PostGridSplitEvent args) + { + AddGridToStation(component.Station, args.Grid); // Add the new grid as a member. + } + + private void OnStationGridDeleted(EntityUid uid, StationMemberComponent component, ComponentShutdown args) + { + if (!TryComp(component.Station, out var stationData)) + return; + + stationData.Grids.Remove(uid); + + // TODO: Remove this when we find out what's mysteriously pulling the rug under us maps wise. + _sawmill.Debug($"Station grid is being deleted, trace is: {Environment.StackTrace}"); + } + public override void Shutdown() { base.Shutdown(); @@ -109,6 +125,11 @@ public sealed class StationSystem : EntitySystem Logger.Error($"Station entity {ToPrettyString(uid)} is getting deleted mid-round. Trace: {Environment.StackTrace}"); } + foreach (var grid in component.Grids) + { + RemComp(grid); + } + _stations.Remove(uid); RaiseNetworkEvent(new StationsUpdatedEvent(_stations), Filter.Broadcast()); } @@ -175,14 +196,12 @@ public sealed class StationSystem : EntitySystem if (!dict.Any()) { // Oh jeez, no stations got loaded. - // We'll just take the first grid and setup that, then. - - var grid = ev.Grids[0]; - - AddGrid("Station", grid); + // We'll yell about it, but the thing this used to do with creating a dummy is kinda pointless now. + _sawmill.Error($"There were no station grids for {ev.GameMap.ID}!"); } // Iterate over all PartOfStation + // TODO: Remove this whenever pillar finally gets replaced. It's the sole user. foreach (var grid in ev.Grids) { if (!TryComp(grid, out var partOfStation)) @@ -204,11 +223,12 @@ public sealed class StationSystem : EntitySystem private void OnRoundEnd(GameRunLevelChangedEvent eventArgs) { - if (eventArgs.New != GameRunLevel.PreRoundLobby) return; + if (eventArgs.New != GameRunLevel.PreRoundLobby) + return; foreach (var entity in _stations) { - Del(entity); + DeleteStation(entity); } } @@ -235,7 +255,13 @@ public sealed class StationSystem : EntitySystem return largestGrid; } - public Filter GetInStation(EntityUid source, float range = 32f) + /// + /// Tries to retrieve a filter for everything in the station the source is on. + /// + /// The entity to use to find the station. + /// The range around the station + /// + public Filter GetInOwningStation(EntityUid source, float range = 32f) { var station = GetOwningStation(source); @@ -261,7 +287,8 @@ public sealed class StationSystem : EntitySystem foreach (var gridUid in dataComponent.Grids) { if (!_mapManager.TryGetGrid(gridUid, out var grid) || - !xformQuery.TryGetComponent(gridUid, out var xform)) continue; + !xformQuery.TryGetComponent(gridUid, out var xform)) + continue; var mapId = xform.MapID; var position = _transform.GetWorldPosition(xform, xformQuery); @@ -277,17 +304,20 @@ public sealed class StationSystem : EntitySystem foreach (var session in Filter.GetAllPlayers(_player)) { var entity = session.AttachedEntity; - if (entity == null || !xformQuery.TryGetComponent(entity, out var xform)) continue; + if (entity == null || !xformQuery.TryGetComponent(entity, out var xform)) + continue; var mapId = xform.MapID; - if (!mapIds.Contains(mapId)) continue; + if (!mapIds.Contains(mapId)) + continue; var position = _transform.GetWorldPosition(xform, xformQuery); foreach (var bound in bounds) { - if (!bound.Contains(position)) continue; + if (!bound.Contains(position)) + continue; filter.AddPlayer(session); break; @@ -355,6 +385,7 @@ public sealed class StationSystem : EntitySystem /// Station to attach the grid to. /// Resolve pattern, grid component of mapGrid. /// Resolve pattern, station data component of station. + /// The name to assign to the grid if any. /// Thrown when mapGrid or station are not a grid or station, respectively. public void AddGridToStation(EntityUid station, EntityUid mapGrid, IMapGridComponent? gridComponent = null, StationDataComponent? stationData = null, string? name = null) {