AtmosphereSystem.Utils.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Runtime.CompilerServices;
  2. using Content.Server.Atmos.Components;
  3. using Content.Server.Maps;
  4. using Content.Shared.Atmos;
  5. using Content.Shared.Atmos.Components;
  6. using Content.Shared.Maps;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Map.Components;
  9. namespace Content.Server.Atmos.EntitySystems;
  10. public partial class AtmosphereSystem
  11. {
  12. /// <summary>
  13. /// Gets the particular price of an air mixture.
  14. /// </summary>
  15. public double GetPrice(GasMixture mixture)
  16. {
  17. float basePrice = 0; // moles of gas * price/mole
  18. float totalMoles = 0; // total number of moles in can
  19. float maxComponent = 0; // moles of the dominant gas
  20. for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
  21. {
  22. basePrice += mixture.Moles[i] * GetGas(i).PricePerMole;
  23. totalMoles += mixture.Moles[i];
  24. maxComponent = Math.Max(maxComponent, mixture.Moles[i]);
  25. }
  26. // Pay more for gas canisters that are more pure
  27. float purity = 1;
  28. if (totalMoles > 0) {
  29. purity = maxComponent / totalMoles;
  30. }
  31. return basePrice * purity;
  32. }
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public void InvalidateVisuals(Entity<GasTileOverlayComponent?> grid, Vector2i tile)
  35. {
  36. _gasTileOverlaySystem.Invalidate(grid, tile);
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. private void InvalidateVisuals(
  40. Entity<GridAtmosphereComponent, GasTileOverlayComponent, MapGridComponent, TransformComponent> ent,
  41. TileAtmosphere tile)
  42. {
  43. _gasTileOverlaySystem.Invalidate((ent.Owner, ent.Comp2), tile.GridIndices);
  44. }
  45. /// <summary>
  46. /// Gets the volume in liters for a number of tiles, on a specific grid.
  47. /// </summary>
  48. /// <param name="mapGrid">The grid in question.</param>
  49. /// <param name="tiles">The amount of tiles.</param>
  50. /// <returns>The volume in liters that the tiles occupy.</returns>
  51. private float GetVolumeForTiles(MapGridComponent mapGrid, int tiles = 1)
  52. {
  53. return Atmospherics.CellVolume * mapGrid.TileSize * tiles;
  54. }
  55. public readonly record struct AirtightData(AtmosDirection BlockedDirections, bool NoAirWhenBlocked,
  56. bool FixVacuum);
  57. private void UpdateAirtightData(EntityUid uid, GridAtmosphereComponent atmos, MapGridComponent grid, TileAtmosphere tile)
  58. {
  59. var oldBlocked = tile.AirtightData.BlockedDirections;
  60. tile.AirtightData = tile.NoGridTile
  61. ? default
  62. : GetAirtightData(uid, grid, tile.GridIndices);
  63. if (tile.AirtightData.BlockedDirections != oldBlocked && tile.ExcitedGroup != null)
  64. ExcitedGroupDispose(atmos, tile.ExcitedGroup);
  65. }
  66. private AirtightData GetAirtightData(EntityUid uid, MapGridComponent grid, Vector2i tile)
  67. {
  68. var blockedDirs = AtmosDirection.Invalid;
  69. var noAirWhenBlocked = false;
  70. var fixVacuum = false;
  71. foreach (var ent in _map.GetAnchoredEntities(uid, grid, tile))
  72. {
  73. if (!_airtightQuery.TryGetComponent(ent, out var airtight))
  74. continue;
  75. fixVacuum |= airtight.FixVacuum;
  76. if(!airtight.AirBlocked)
  77. continue;
  78. blockedDirs |= airtight.AirBlockedDirection;
  79. noAirWhenBlocked |= airtight.NoAirWhenFullyAirBlocked;
  80. if (blockedDirs == AtmosDirection.All && noAirWhenBlocked && fixVacuum)
  81. break;
  82. }
  83. return new AirtightData(blockedDirs, noAirWhenBlocked, fixVacuum);
  84. }
  85. /// <summary>
  86. /// Pries a tile in a grid.
  87. /// </summary>
  88. /// <param name="mapGrid">The grid in question.</param>
  89. /// <param name="tile">The indices of the tile.</param>
  90. private void PryTile(MapGridComponent mapGrid, Vector2i tile)
  91. {
  92. if (!mapGrid.TryGetTileRef(tile, out var tileRef))
  93. return;
  94. _tile.PryTile(tileRef);
  95. }
  96. }