1
0

AtmosphereSystem.ExcitedGroup.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Content.Server.Atmos.Components;
  2. using Content.Shared.Atmos;
  3. using Content.Shared.Atmos.Components;
  4. using Robust.Shared.Map.Components;
  5. using Robust.Shared.Utility;
  6. namespace Content.Server.Atmos.EntitySystems
  7. {
  8. public sealed partial class AtmosphereSystem
  9. {
  10. private void ExcitedGroupAddTile(ExcitedGroup excitedGroup, TileAtmosphere tile)
  11. {
  12. DebugTools.Assert(!excitedGroup.Disposed, "Excited group is disposed!");
  13. DebugTools.Assert(tile.ExcitedGroup == null, "Tried to add a tile to an excited group when it's already in another one!");
  14. excitedGroup.Tiles.Add(tile);
  15. tile.ExcitedGroup = excitedGroup;
  16. ExcitedGroupResetCooldowns(excitedGroup);
  17. }
  18. private void ExcitedGroupRemoveTile(ExcitedGroup excitedGroup, TileAtmosphere tile)
  19. {
  20. DebugTools.Assert(!excitedGroup.Disposed, "Excited group is disposed!");
  21. DebugTools.Assert(tile.ExcitedGroup == excitedGroup, "Tried to remove a tile from an excited group it's not present in!");
  22. tile.ExcitedGroup = null;
  23. excitedGroup.Tiles.Remove(tile);
  24. }
  25. private void ExcitedGroupMerge(GridAtmosphereComponent gridAtmosphere, ExcitedGroup ourGroup, ExcitedGroup otherGroup)
  26. {
  27. DebugTools.Assert(!ourGroup.Disposed, "Excited group is disposed!");
  28. DebugTools.Assert(!otherGroup.Disposed, "Excited group is disposed!");
  29. DebugTools.Assert(gridAtmosphere.ExcitedGroups.Contains(ourGroup), "Grid Atmosphere does not contain Excited Group!");
  30. DebugTools.Assert(gridAtmosphere.ExcitedGroups.Contains(otherGroup), "Grid Atmosphere does not contain Excited Group!");
  31. var ourSize = ourGroup.Tiles.Count;
  32. var otherSize = otherGroup.Tiles.Count;
  33. ExcitedGroup winner;
  34. ExcitedGroup loser;
  35. if (ourSize > otherSize)
  36. {
  37. winner = ourGroup;
  38. loser = otherGroup;
  39. }
  40. else
  41. {
  42. winner = otherGroup;
  43. loser = ourGroup;
  44. }
  45. foreach (var tile in loser.Tiles)
  46. {
  47. tile.ExcitedGroup = winner;
  48. winner.Tiles.Add(tile);
  49. }
  50. loser.Tiles.Clear();
  51. ExcitedGroupDispose(gridAtmosphere, loser);
  52. ExcitedGroupResetCooldowns(winner);
  53. }
  54. private void ExcitedGroupResetCooldowns(ExcitedGroup excitedGroup)
  55. {
  56. DebugTools.Assert(!excitedGroup.Disposed, "Excited group is disposed!");
  57. excitedGroup.BreakdownCooldown = 0;
  58. excitedGroup.DismantleCooldown = 0;
  59. }
  60. private void ExcitedGroupSelfBreakdown(
  61. Entity<GridAtmosphereComponent, GasTileOverlayComponent, MapGridComponent, TransformComponent> ent,
  62. ExcitedGroup excitedGroup)
  63. {
  64. DebugTools.Assert(!excitedGroup.Disposed, "Excited group is disposed!");
  65. DebugTools.Assert(ent.Comp1.ExcitedGroups.Contains(excitedGroup), "Grid Atmosphere does not contain Excited Group!");
  66. var combined = new GasMixture(Atmospherics.CellVolume);
  67. var tileSize = excitedGroup.Tiles.Count;
  68. if (excitedGroup.Disposed)
  69. return;
  70. if (tileSize == 0)
  71. {
  72. ExcitedGroupDispose(ent.Comp1, excitedGroup);
  73. return;
  74. }
  75. foreach (var tile in excitedGroup.Tiles)
  76. {
  77. if (tile?.Air == null)
  78. continue;
  79. Merge(combined, tile.Air);
  80. if (!ExcitedGroupsSpaceIsAllConsuming || !tile.Space)
  81. continue;
  82. combined.Clear();
  83. break;
  84. }
  85. combined.Multiply(1 / (float)tileSize);
  86. foreach (var tile in excitedGroup.Tiles)
  87. {
  88. if (tile?.Air == null)
  89. continue;
  90. tile.Air.CopyFrom(combined);
  91. InvalidateVisuals(ent, tile);
  92. }
  93. excitedGroup.BreakdownCooldown = 0;
  94. }
  95. /// <summary>
  96. /// This de-activates and removes all tiles in an excited group.
  97. /// </summary>
  98. private void DeactivateGroupTiles(GridAtmosphereComponent gridAtmosphere, ExcitedGroup excitedGroup)
  99. {
  100. foreach (var tile in excitedGroup.Tiles)
  101. {
  102. tile.ExcitedGroup = null;
  103. RemoveActiveTile(gridAtmosphere, tile);
  104. }
  105. excitedGroup.Tiles.Clear();
  106. }
  107. /// <summary>
  108. /// This removes an excited group without de-activating its tiles.
  109. /// </summary>
  110. private void ExcitedGroupDispose(GridAtmosphereComponent gridAtmosphere, ExcitedGroup excitedGroup)
  111. {
  112. if (excitedGroup.Disposed)
  113. return;
  114. DebugTools.Assert(gridAtmosphere.ExcitedGroups.Contains(excitedGroup), "Grid Atmosphere does not contain Excited Group!");
  115. excitedGroup.Disposed = true;
  116. gridAtmosphere.ExcitedGroups.Remove(excitedGroup);
  117. foreach (var tile in excitedGroup.Tiles)
  118. {
  119. tile.ExcitedGroup = null;
  120. }
  121. excitedGroup.Tiles.Clear();
  122. }
  123. }
  124. }