1
0

AtmosphereSystem.Superconductivity.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Content.Server.Atmos.Components;
  2. using Content.Shared.Atmos;
  3. using Robust.Shared.Map.Components;
  4. namespace Content.Server.Atmos.EntitySystems
  5. {
  6. public sealed partial class AtmosphereSystem
  7. {
  8. private void Superconduct(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile)
  9. {
  10. var directions = ConductivityDirections(gridAtmosphere, tile);
  11. for(var i = 0; i < Atmospherics.Directions; i++)
  12. {
  13. var direction = (AtmosDirection) (1 << i);
  14. if (!directions.IsFlagSet(direction))
  15. continue;
  16. var adjacent = tile.AdjacentTiles[i];
  17. // TODO ATMOS handle adjacent being null.
  18. if (adjacent == null || adjacent.ThermalConductivity == 0f)
  19. continue;
  20. if(adjacent.ArchivedCycle < gridAtmosphere.UpdateCounter)
  21. Archive(adjacent, gridAtmosphere.UpdateCounter);
  22. NeighborConductWithSource(gridAtmosphere, adjacent, tile);
  23. ConsiderSuperconductivity(gridAtmosphere, adjacent);
  24. }
  25. RadiateToSpace(tile);
  26. FinishSuperconduction(gridAtmosphere, tile);
  27. }
  28. private AtmosDirection ConductivityDirections(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile)
  29. {
  30. if(tile.Air == null)
  31. {
  32. if(tile.ArchivedCycle < gridAtmosphere.UpdateCounter)
  33. Archive(tile, gridAtmosphere.UpdateCounter);
  34. return AtmosDirection.All;
  35. }
  36. // TODO ATMOS check if this is correct
  37. return AtmosDirection.All;
  38. }
  39. public bool ConsiderSuperconductivity(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile)
  40. {
  41. if (tile.ThermalConductivity == 0f || !Superconduction)
  42. return false;
  43. gridAtmosphere.SuperconductivityTiles.Add(tile);
  44. return true;
  45. }
  46. public bool ConsiderSuperconductivity(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile, bool starting)
  47. {
  48. if (!Superconduction)
  49. return false;
  50. if (tile.Air == null || tile.Air.Temperature < (starting
  51. ? Atmospherics.MinimumTemperatureStartSuperConduction
  52. : Atmospherics.MinimumTemperatureForSuperconduction))
  53. return false;
  54. return !(GetHeatCapacity(tile.Air) < Atmospherics.MCellWithRatio)
  55. && ConsiderSuperconductivity(gridAtmosphere, tile);
  56. }
  57. public void FinishSuperconduction(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile)
  58. {
  59. // Conduct with air on my tile if I have it
  60. if (tile.Air != null)
  61. {
  62. tile.Temperature = TemperatureShare(tile, tile.ThermalConductivity, tile.Temperature, tile.HeatCapacity);
  63. }
  64. FinishSuperconduction(gridAtmosphere, tile, tile.Air?.Temperature ?? tile.Temperature);
  65. }
  66. public void FinishSuperconduction(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile, float temperature)
  67. {
  68. // Make sure it's still hot enough to continue conducting.
  69. if (temperature < Atmospherics.MinimumTemperatureForSuperconduction)
  70. {
  71. gridAtmosphere.SuperconductivityTiles.Remove(tile);
  72. }
  73. }
  74. public void NeighborConductWithSource(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile, TileAtmosphere other)
  75. {
  76. if (tile.Air == null)
  77. {
  78. // TODO ATMOS: why does this need to check if a tile exists if it doesn't use the tile?
  79. if (TryComp<MapGridComponent>(other.GridIndex, out var grid)
  80. && _mapSystem.TryGetTileRef(other.GridIndex, grid, other.GridIndices, out var _))
  81. {
  82. TemperatureShareOpenToSolid(other, tile);
  83. }
  84. else
  85. {
  86. TemperatureShareMutualSolid(other, tile, tile.ThermalConductivity);
  87. }
  88. // TODO ATMOS: tile.TemperatureExpose(null, tile.Temperature, gridAtmosphere.GetVolumeForCells(1));
  89. return;
  90. }
  91. if (other.Air != null)
  92. {
  93. TemperatureShare(other, tile, Atmospherics.WindowHeatTransferCoefficient);
  94. }
  95. else
  96. {
  97. TemperatureShareOpenToSolid(tile, other);
  98. }
  99. AddActiveTile(gridAtmosphere, tile);
  100. }
  101. private void TemperatureShareOpenToSolid(TileAtmosphere tile, TileAtmosphere other)
  102. {
  103. if (tile.Air == null)
  104. return;
  105. other.Temperature = TemperatureShare(tile, other.ThermalConductivity, other.Temperature, other.HeatCapacity);
  106. }
  107. private void TemperatureShareMutualSolid(TileAtmosphere tile, TileAtmosphere other, float conductionCoefficient)
  108. {
  109. if (tile.AirArchived == null || other.AirArchived == null)
  110. return;
  111. var deltaTemperature = (tile.AirArchived.Temperature - other.AirArchived.Temperature);
  112. if (MathF.Abs(deltaTemperature) > Atmospherics.MinimumTemperatureDeltaToConsider
  113. && tile.HeatCapacity != 0f && other.HeatCapacity != 0f)
  114. {
  115. var heat = conductionCoefficient * deltaTemperature *
  116. (tile.HeatCapacity * other.HeatCapacity / (tile.HeatCapacity + other.HeatCapacity));
  117. tile.Temperature -= heat / tile.HeatCapacity;
  118. other.Temperature += heat / other.HeatCapacity;
  119. }
  120. }
  121. public void RadiateToSpace(TileAtmosphere tile)
  122. {
  123. if (tile.AirArchived == null)
  124. return;
  125. // Considering 0ºC as the break even point for radiation in and out.
  126. if (tile.Temperature > Atmospherics.T0C)
  127. {
  128. // Hardcoded space temperature.
  129. var deltaTemperature = (tile.AirArchived.Temperature - Atmospherics.TCMB);
  130. if ((tile.HeatCapacity > 0) && (MathF.Abs(deltaTemperature) > Atmospherics.MinimumTemperatureDeltaToConsider))
  131. {
  132. var heat = tile.ThermalConductivity * deltaTemperature * (tile.HeatCapacity *
  133. Atmospherics.HeatCapacityVacuum / (tile.HeatCapacity + Atmospherics.HeatCapacityVacuum));
  134. tile.Temperature -= heat;
  135. }
  136. }
  137. }
  138. }
  139. }