using Content.Server.Atmos.Components; using Content.Shared.Atmos; using Content.Shared.Atmos.Components; using Robust.Shared.GameStates; using Robust.Shared.Map.Components; using Robust.Shared.Utility; namespace Content.Server.Atmos.EntitySystems; public partial class AtmosphereSystem { private void InitializeMap() { SubscribeLocalEvent(OnMapStartup); SubscribeLocalEvent(OnMapRemove); SubscribeLocalEvent(OnMapGetState); SubscribeLocalEvent(OnGridParentChanged); } private void OnMapStartup(EntityUid uid, MapAtmosphereComponent component, ComponentInit args) { component.Mixture.MarkImmutable(); component.Overlay = _gasTileOverlaySystem.GetOverlayData(component.Mixture); } private void OnMapRemove(EntityUid uid, MapAtmosphereComponent component, ComponentRemove args) { if (!TerminatingOrDeleted(uid)) RefreshAllGridMapAtmospheres(uid); } private void OnMapGetState(EntityUid uid, MapAtmosphereComponent component, ref ComponentGetState args) { args.State = new MapAtmosphereComponentState(component.Overlay); } public void SetMapAtmosphere(EntityUid uid, bool space, GasMixture mixture) { DebugTools.Assert(HasComp(uid)); var component = EnsureComp(uid); SetMapGasMixture(uid, mixture, component, false); SetMapSpace(uid, space, component, false); RefreshAllGridMapAtmospheres(uid); } public void SetMapGasMixture(EntityUid uid, GasMixture mixture, MapAtmosphereComponent? component = null, bool updateTiles = true) { if (!Resolve(uid, ref component)) return; if (!mixture.Immutable) { mixture = mixture.Clone(); mixture.MarkImmutable(); } component.Mixture = mixture; component.Overlay = _gasTileOverlaySystem.GetOverlayData(component.Mixture); Dirty(uid, component); if (updateTiles) RefreshAllGridMapAtmospheres(uid); } public void SetMapSpace(EntityUid uid, bool space, MapAtmosphereComponent? component = null, bool updateTiles = true) { if (!Resolve(uid, ref component)) return; if (component.Space == space) return; component.Space = space; if (updateTiles) RefreshAllGridMapAtmospheres(uid); } /// /// Forces a refresh of all MapAtmosphere tiles on every grid on a map. /// public void RefreshAllGridMapAtmospheres(EntityUid map) { DebugTools.Assert(HasComp(map)); var enumerator = AllEntityQuery(); while (enumerator.MoveNext(out var grid, out var atmos, out var xform)) { if (xform.MapUid == map) RefreshMapAtmosphereTiles((grid, atmos)); } } /// /// Forces a refresh of all MapAtmosphere tiles on a given grid. /// private void RefreshMapAtmosphereTiles(Entity grid) { if (!Resolve(grid.Owner, ref grid.Comp)) return; var atmos = grid.Comp; foreach (var tile in atmos.MapTiles) { RemoveMapAtmos(atmos, tile); atmos.InvalidatedCoords.Add(tile.GridIndices); } atmos.MapTiles.Clear(); } /// /// Handles updating map-atmospheres when grids move across maps. /// private void OnGridParentChanged(Entity grid, ref EntParentChangedMessage args) { // Do nothing if detaching to nullspace if (!args.Transform.ParentUid.IsValid()) return; // Avoid doing work if moving from a space-map to another space-map. if (args.OldParent == null || HasComp(args.OldParent) || HasComp(args.Transform.ParentUid)) { RefreshMapAtmosphereTiles((grid, grid)); } } }