AtmosphereSystem.API.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System.Linq;
  2. using Content.Server.Atmos.Components;
  3. using Content.Server.Atmos.Piping.Components;
  4. using Content.Server.NodeContainer.NodeGroups;
  5. using Content.Shared.Atmos;
  6. using Content.Shared.Atmos.Components;
  7. using Content.Shared.Atmos.Reactions;
  8. using Robust.Shared.Map.Components;
  9. using Robust.Shared.Utility;
  10. namespace Content.Server.Atmos.EntitySystems;
  11. public partial class AtmosphereSystem
  12. {
  13. public GasMixture? GetContainingMixture(Entity<TransformComponent?> ent, bool ignoreExposed = false, bool excite = false)
  14. {
  15. if (!Resolve(ent, ref ent.Comp))
  16. return null;
  17. return GetContainingMixture(ent, ent.Comp.GridUid, ent.Comp.MapUid, ignoreExposed, excite);
  18. }
  19. public GasMixture? GetContainingMixture(
  20. Entity<TransformComponent?> ent,
  21. Entity<GridAtmosphereComponent?, GasTileOverlayComponent?>? grid,
  22. Entity<MapAtmosphereComponent?>? map,
  23. bool ignoreExposed = false,
  24. bool excite = false)
  25. {
  26. if (!Resolve(ent, ref ent.Comp))
  27. return null;
  28. if (!ignoreExposed && !ent.Comp.Anchored)
  29. {
  30. // Used for things like disposals/cryo to change which air people are exposed to.
  31. var ev = new AtmosExposedGetAirEvent((ent, ent.Comp), excite);
  32. RaiseLocalEvent(ent, ref ev);
  33. if (ev.Handled)
  34. return ev.Gas;
  35. // TODO ATMOS: recursively iterate up through parents
  36. // This really needs recursive InContainer metadata flag for performance
  37. // And ideally some fast way to get the innermost airtight container.
  38. }
  39. var position = _transformSystem.GetGridTilePositionOrDefault((ent, ent.Comp));
  40. return GetTileMixture(grid, map, position, excite);
  41. }
  42. public bool HasAtmosphere(EntityUid gridUid) => _atmosQuery.HasComponent(gridUid);
  43. public bool SetSimulatedGrid(EntityUid gridUid, bool simulated)
  44. {
  45. var ev = new SetSimulatedGridMethodEvent(gridUid, simulated);
  46. RaiseLocalEvent(gridUid, ref ev);
  47. return ev.Handled;
  48. }
  49. public bool IsSimulatedGrid(EntityUid gridUid)
  50. {
  51. var ev = new IsSimulatedGridMethodEvent(gridUid);
  52. RaiseLocalEvent(gridUid, ref ev);
  53. return ev.Simulated;
  54. }
  55. public IEnumerable<GasMixture> GetAllMixtures(EntityUid gridUid, bool excite = false)
  56. {
  57. var ev = new GetAllMixturesMethodEvent(gridUid, excite);
  58. RaiseLocalEvent(gridUid, ref ev);
  59. if(!ev.Handled)
  60. return Enumerable.Empty<GasMixture>();
  61. DebugTools.AssertNotNull(ev.Mixtures);
  62. return ev.Mixtures!;
  63. }
  64. public void InvalidateTile(Entity<GridAtmosphereComponent?> entity, Vector2i tile)
  65. {
  66. if (_atmosQuery.Resolve(entity.Owner, ref entity.Comp, false))
  67. entity.Comp.InvalidatedCoords.Add(tile);
  68. }
  69. public GasMixture?[]? GetTileMixtures(
  70. Entity<GridAtmosphereComponent?, GasTileOverlayComponent?>? grid,
  71. Entity<MapAtmosphereComponent?>? map,
  72. List<Vector2i> tiles,
  73. bool excite = false)
  74. {
  75. GasMixture?[]? mixtures = null;
  76. var handled = false;
  77. // If we've been passed a grid, try to let it handle it.
  78. if (grid is {} gridEnt && Resolve(gridEnt, ref gridEnt.Comp1))
  79. {
  80. if (excite)
  81. Resolve(gridEnt, ref gridEnt.Comp2);
  82. handled = true;
  83. mixtures = new GasMixture?[tiles.Count];
  84. for (var i = 0; i < tiles.Count; i++)
  85. {
  86. var tile = tiles[i];
  87. if (!gridEnt.Comp1.Tiles.TryGetValue(tile, out var atmosTile))
  88. {
  89. // need to get map atmosphere
  90. handled = false;
  91. continue;
  92. }
  93. mixtures[i] = atmosTile.Air;
  94. if (excite)
  95. {
  96. AddActiveTile(gridEnt.Comp1, atmosTile);
  97. InvalidateVisuals((gridEnt.Owner, gridEnt.Comp2), tile);
  98. }
  99. }
  100. }
  101. if (handled)
  102. return mixtures;
  103. // We either don't have a grid, or the event wasn't handled.
  104. // Let the map handle it instead, and also broadcast the event.
  105. if (map is {} mapEnt && _mapAtmosQuery.Resolve(mapEnt, ref mapEnt.Comp))
  106. {
  107. mixtures ??= new GasMixture?[tiles.Count];
  108. for (var i = 0; i < tiles.Count; i++)
  109. {
  110. mixtures[i] ??= mapEnt.Comp.Mixture;
  111. }
  112. return mixtures;
  113. }
  114. // Default to a space mixture... This is a space game, after all!
  115. mixtures ??= new GasMixture?[tiles.Count];
  116. for (var i = 0; i < tiles.Count; i++)
  117. {
  118. mixtures[i] ??= GasMixture.SpaceGas;
  119. }
  120. return mixtures;
  121. }
  122. public GasMixture? GetTileMixture (Entity<TransformComponent?> entity, bool excite = false)
  123. {
  124. if (!Resolve(entity.Owner, ref entity.Comp))
  125. return null;
  126. var indices = _transformSystem.GetGridTilePositionOrDefault(entity);
  127. return GetTileMixture(entity.Comp.GridUid, entity.Comp.MapUid, indices, excite);
  128. }
  129. public GasMixture? GetTileMixture(
  130. Entity<GridAtmosphereComponent?, GasTileOverlayComponent?>? grid,
  131. Entity<MapAtmosphereComponent?>? map,
  132. Vector2i gridTile,
  133. bool excite = false)
  134. {
  135. // If we've been passed a grid, try to let it handle it.
  136. if (grid is {} gridEnt
  137. && Resolve(gridEnt, ref gridEnt.Comp1, false)
  138. && gridEnt.Comp1.Tiles.TryGetValue(gridTile, out var tile))
  139. {
  140. if (excite)
  141. {
  142. AddActiveTile(gridEnt.Comp1, tile);
  143. InvalidateVisuals((grid.Value.Owner, grid.Value.Comp2), gridTile);
  144. }
  145. return tile.Air;
  146. }
  147. if (map is {} mapEnt && _mapAtmosQuery.Resolve(mapEnt, ref mapEnt.Comp, false))
  148. return mapEnt.Comp.Mixture;
  149. // Default to a space mixture... This is a space game, after all!
  150. return GasMixture.SpaceGas;
  151. }
  152. public ReactionResult ReactTile(EntityUid gridId, Vector2i tile)
  153. {
  154. var ev = new ReactTileMethodEvent(gridId, tile);
  155. RaiseLocalEvent(gridId, ref ev);
  156. ev.Handled = true;
  157. return ev.Result;
  158. }
  159. public bool IsTileAirBlocked(EntityUid gridUid, Vector2i tile, AtmosDirection directions = AtmosDirection.All, MapGridComponent? mapGridComp = null)
  160. {
  161. if (!Resolve(gridUid, ref mapGridComp, false))
  162. return false;
  163. var data = GetAirtightData(gridUid, mapGridComp, tile);
  164. return data.BlockedDirections.IsFlagSet(directions);
  165. }
  166. public bool IsTileSpace(Entity<GridAtmosphereComponent?>? grid, Entity<MapAtmosphereComponent?>? map, Vector2i tile)
  167. {
  168. if (grid is {} gridEnt && _atmosQuery.Resolve(gridEnt, ref gridEnt.Comp, false)
  169. && gridEnt.Comp.Tiles.TryGetValue(tile, out var tileAtmos))
  170. {
  171. return tileAtmos.Space;
  172. }
  173. if (map is {} mapEnt && _mapAtmosQuery.Resolve(mapEnt, ref mapEnt.Comp, false))
  174. return mapEnt.Comp.Space;
  175. // If nothing handled the event, it'll default to true.
  176. // Oh well, this is a space game after all, deal with it!
  177. return true;
  178. }
  179. public bool IsTileMixtureProbablySafe(Entity<GridAtmosphereComponent?>? grid, Entity<MapAtmosphereComponent?> map, Vector2i tile)
  180. {
  181. return IsMixtureProbablySafe(GetTileMixture(grid, map, tile));
  182. }
  183. public float GetTileHeatCapacity(Entity<GridAtmosphereComponent?>? grid, Entity<MapAtmosphereComponent?> map, Vector2i tile)
  184. {
  185. return GetHeatCapacity(GetTileMixture(grid, map, tile) ?? GasMixture.SpaceGas);
  186. }
  187. public TileMixtureEnumerator GetAdjacentTileMixtures(Entity<GridAtmosphereComponent?> grid, Vector2i tile, bool includeBlocked = false, bool excite = false)
  188. {
  189. if (!_atmosQuery.Resolve(grid, ref grid.Comp, false))
  190. return TileMixtureEnumerator.Empty;
  191. return !grid.Comp.Tiles.TryGetValue(tile, out var atmosTile)
  192. ? TileMixtureEnumerator.Empty
  193. : new(atmosTile.AdjacentTiles);
  194. }
  195. public void HotspotExpose(Entity<GridAtmosphereComponent?> grid, Vector2i tile, float exposedTemperature, float exposedVolume,
  196. EntityUid? sparkSourceUid = null, bool soh = false)
  197. {
  198. if (!_atmosQuery.Resolve(grid, ref grid.Comp, false))
  199. return;
  200. if (grid.Comp.Tiles.TryGetValue(tile, out var atmosTile))
  201. HotspotExpose(grid.Comp, atmosTile, exposedTemperature, exposedVolume, soh, sparkSourceUid);
  202. }
  203. public void HotspotExpose(TileAtmosphere tile, float exposedTemperature, float exposedVolume,
  204. EntityUid? sparkSourceUid = null, bool soh = false)
  205. {
  206. if (!_atmosQuery.TryGetComponent(tile.GridIndex, out var atmos))
  207. return;
  208. DebugTools.Assert(atmos.Tiles.TryGetValue(tile.GridIndices, out var tmp) && tmp == tile);
  209. HotspotExpose(atmos, tile, exposedTemperature, exposedVolume, soh, sparkSourceUid);
  210. }
  211. public void HotspotExtinguish(EntityUid gridUid, Vector2i tile)
  212. {
  213. var ev = new HotspotExtinguishMethodEvent(gridUid, tile);
  214. RaiseLocalEvent(gridUid, ref ev);
  215. }
  216. public bool IsHotspotActive(EntityUid gridUid, Vector2i tile)
  217. {
  218. var ev = new IsHotspotActiveMethodEvent(gridUid, tile);
  219. RaiseLocalEvent(gridUid, ref ev);
  220. // If not handled, this will be false. Just like in space!
  221. return ev.Result;
  222. }
  223. public bool AddPipeNet(Entity<GridAtmosphereComponent?> grid, PipeNet pipeNet)
  224. {
  225. return _atmosQuery.Resolve(grid, ref grid.Comp, false) && grid.Comp.PipeNets.Add(pipeNet);
  226. }
  227. public bool RemovePipeNet(Entity<GridAtmosphereComponent?> grid, PipeNet pipeNet)
  228. {
  229. return _atmosQuery.Resolve(grid, ref grid.Comp, false) && grid.Comp.PipeNets.Remove(pipeNet);
  230. }
  231. public bool AddAtmosDevice(Entity<GridAtmosphereComponent?> grid, Entity<AtmosDeviceComponent> device)
  232. {
  233. DebugTools.Assert(device.Comp.JoinedGrid == null);
  234. DebugTools.Assert(Transform(device).GridUid == grid);
  235. if (!_atmosQuery.Resolve(grid, ref grid.Comp, false))
  236. return false;
  237. if (!grid.Comp.AtmosDevices.Add(device))
  238. return false;
  239. device.Comp.JoinedGrid = grid;
  240. return true;
  241. }
  242. public bool RemoveAtmosDevice(Entity<GridAtmosphereComponent?> grid, Entity<AtmosDeviceComponent> device)
  243. {
  244. DebugTools.Assert(device.Comp.JoinedGrid == grid);
  245. if (!_atmosQuery.Resolve(grid, ref grid.Comp, false))
  246. return false;
  247. if (!grid.Comp.AtmosDevices.Remove(device))
  248. return false;
  249. device.Comp.JoinedGrid = null;
  250. return true;
  251. }
  252. [ByRefEvent] private record struct SetSimulatedGridMethodEvent
  253. (EntityUid Grid, bool Simulated, bool Handled = false);
  254. [ByRefEvent] private record struct IsSimulatedGridMethodEvent
  255. (EntityUid Grid, bool Simulated = false, bool Handled = false);
  256. [ByRefEvent] private record struct GetAllMixturesMethodEvent
  257. (EntityUid Grid, bool Excite = false, IEnumerable<GasMixture>? Mixtures = null, bool Handled = false);
  258. [ByRefEvent] private record struct ReactTileMethodEvent
  259. (EntityUid GridId, Vector2i Tile, ReactionResult Result = default, bool Handled = false);
  260. [ByRefEvent] private record struct HotspotExtinguishMethodEvent
  261. (EntityUid Grid, Vector2i Tile, bool Handled = false);
  262. [ByRefEvent] private record struct IsHotspotActiveMethodEvent
  263. (EntityUid Grid, Vector2i Tile, bool Result = false, bool Handled = false);
  264. }