Add heat conduction (#1653)

This commit is contained in:
Víctor Aguilera Puerto
2020-08-13 14:18:26 +02:00
committed by GitHub
parent 619386a04a
commit ca68fbe818
8 changed files with 301 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Shared.Atmos;
using Content.Shared.Audio;
using Content.Shared.Maps;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Systems;
@@ -27,9 +28,21 @@ namespace Content.Server.Atmos
[Robust.Shared.IoC.Dependency] private IEntityManager _entityManager = default!;
[Robust.Shared.IoC.Dependency] private IMapManager _mapManager = default!;
[ViewVariables]
private int _archivedCycle = 0;
[ViewVariables]
private int _currentCycle = 0;
[ViewVariables]
private static GasTileOverlaySystem _gasTileOverlaySystem;
[ViewVariables]
private float _temperature = Atmospherics.T20C;
[ViewVariables]
private float _temperatureArchived = Atmospherics.T20C;
// I know this being static is evil, but I seriously can't come up with a better solution to sound spam.
private static int _soundCooldown = 0;
@@ -39,6 +52,12 @@ namespace Content.Server.Atmos
[ViewVariables]
public float PressureDifference { get; set; } = 0;
[ViewVariables(VVAccess.ReadWrite)]
public float HeatCapacity { get; set; } = 1f;
[ViewVariables]
public float ThermalConductivity => Tile?.Tile.GetContentTileDefinition().ThermalConductivity ?? 0.05f;
[ViewVariables]
public bool Excited { get; set; } = false;
@@ -54,11 +73,15 @@ namespace Content.Server.Atmos
[ViewVariables]
public Hotspot Hotspot;
[ViewVariables]
private Direction _pressureDirection;
[ViewVariables]
public GridId GridIndex { get; }
[ViewVariables]
public TileRef? Tile => GridIndices.GetTileRef(GridIndex);
[ViewVariables]
public MapIndices GridIndices { get; }
@@ -68,6 +91,9 @@ namespace Content.Server.Atmos
[ViewVariables]
public GasMixture Air { get; set; }
[ViewVariables]
public bool BlocksAir => _gridAtmosphereComponent.IsAirBlocked(GridIndices);
public TileAtmosphere(GridAtmosphereComponent atmosphereComponent, GridId gridIndex, MapIndices gridIndices, GasMixture mixture = null)
{
IoCManager.InjectDependencies(this);
@@ -80,8 +106,9 @@ namespace Content.Server.Atmos
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Archive(int fireCount)
{
_archivedCycle = fireCount;
Air?.Archive();
_archivedCycle = fireCount;
_temperatureArchived = _temperature;
}
public void HotspotExpose(float exposedTemperature, float exposedVolume, bool soh = false)
@@ -704,10 +731,150 @@ namespace Content.Server.Atmos
// TODO ATMOS Let all entities in this tile know about the fire?
}
private bool ConsiderSuperconductivity()
{
if (ThermalConductivity == 0f)
return false;
_gridAtmosphereComponent.AddSuperconductivityTile(this);
return true;
}
private bool ConsiderSuperconductivity(bool starting)
{
// TODO ATMOS
return false;
if (Air.Temperature < (starting
? Atmospherics.MinimumTemperatureStartSuperConduction
: Atmospherics.MinimumTemperatureForSuperconduction))
return false;
return !(Air.HeatCapacity < Atmospherics.MCellWithRatio) && ConsiderSuperconductivity();
}
public void Superconduct()
{
var directions = ConductivityDirections();
var adjacentTiles = _gridAtmosphereComponent.GetAdjacentTiles(GridIndices, true);
if (directions.Length > 0)
{
foreach (var direction in directions)
{
if (!adjacentTiles.TryGetValue(direction, out var adjacent)) continue;
if (adjacent.ThermalConductivity == 0f)
continue;
if(adjacent._archivedCycle < _gridAtmosphereComponent.UpdateCounter)
adjacent.Archive(_gridAtmosphereComponent.UpdateCounter);
adjacent.NeighborConductWithSource(this);
adjacent.ConsiderSuperconductivity();
}
}
RadiateToSpace();
FinishSuperconduction();
}
private void FinishSuperconduction()
{
// Conduct with air on my tile if I have it
if (!BlocksAir)
{
_temperature = Air.TemperatureShare(ThermalConductivity, _temperature, HeatCapacity);
}
FinishSuperconduction(BlocksAir ? _temperature : Air.Temperature);
}
private void FinishSuperconduction(float temperature)
{
// Make sure it's still hot enough to continue conducting.
if (temperature < Atmospherics.MinimumTemperatureForSuperconduction)
{
_gridAtmosphereComponent.RemoveSuperconductivityTile(this);
}
}
private void NeighborConductWithSource(TileAtmosphere other)
{
if (BlocksAir)
{
if (!other.BlocksAir)
{
other.TemperatureShareOpenToSolid(this);
}
else
{
other.TemperatureShareMutualSolid(this, ThermalConductivity);
}
TemperatureExpose(null, _temperature, _gridAtmosphereComponent.GetVolumeForCells(1));
return;
}
if (!other.BlocksAir)
{
other.Air.TemperatureShare(Air, Atmospherics.WindowHeatTransferCoefficient);
}
else
{
TemperatureShareOpenToSolid(other);
}
_gridAtmosphereComponent.AddActiveTile(this);
}
private void TemperatureShareOpenToSolid(TileAtmosphere other)
{
other._temperature =
Air.TemperatureShare(other.ThermalConductivity, other._temperature, other.HeatCapacity);
}
private void TemperatureShareMutualSolid(TileAtmosphere other, float conductionCoefficient)
{
var deltaTemperature = (_temperatureArchived - other._temperatureArchived);
if (MathF.Abs(deltaTemperature) > Atmospherics.MinimumTemperatureDeltaToConsider
&& HeatCapacity != 0f && other.HeatCapacity != 0f)
{
var heat = conductionCoefficient * deltaTemperature *
(HeatCapacity * other.HeatCapacity / (HeatCapacity + other.HeatCapacity));
_temperature -= heat / HeatCapacity;
other._temperature += heat / other.HeatCapacity;
}
}
public void RadiateToSpace()
{
// Considering 0ºC as the break even point for radiation in and out.
if (_temperature > Atmospherics.T0C)
{
// Hardcoded space temperature.
var deltaTemperature = (_temperatureArchived - Atmospherics.TCMB);
if ((HeatCapacity > 0) && (MathF.Abs(deltaTemperature) > Atmospherics.MinimumTemperatureDeltaToConsider))
{
var heat = ThermalConductivity * deltaTemperature * (HeatCapacity *
Atmospherics.HeatCapacityVacuum / (HeatCapacity + Atmospherics.HeatCapacityVacuum));
_temperature -= heat;
}
}
}
public Direction[] ConductivityDirections()
{
if(BlocksAir)
{
if(_archivedCycle < _gridAtmosphereComponent.UpdateCounter)
Archive(_gridAtmosphereComponent.UpdateCounter);
return Cardinal;
}
// TODO ATMOS check if this is correct
return Cardinal;
}
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -820,7 +987,6 @@ namespace Content.Server.Atmos
//throw new NotImplementedException();
}
private void React()
{
// TODO ATMOS I think this is enough? gotta make sure...
@@ -880,8 +1046,6 @@ namespace Content.Server.Atmos
Direction.North, Direction.East, Direction.South, Direction.West
};
private static GasTileOverlaySystem _gasTileOverlaySystem;
public void TemperatureExpose(GasMixture mixture, float temperature, float cellVolume)
{
// TODO ATMOS do this