Big optimizations

This commit is contained in:
Víctor Aguilera Puerto
2020-09-06 18:32:38 +02:00
parent 3e1015eeda
commit 2d76fbb3c4
2 changed files with 68 additions and 25 deletions

View File

@@ -67,7 +67,7 @@ namespace Content.Server.Atmos
public float HeatCapacity { get; set; } = 1f; public float HeatCapacity { get; set; } = 1f;
[ViewVariables] [ViewVariables]
public float ThermalConductivity => Tile?.Tile.GetContentTileDefinition().ThermalConductivity ?? 0.05f; public float ThermalConductivity { get; set; } = 0.05f;
[ViewVariables] [ViewVariables]
public bool Excited { get; set; } public bool Excited { get; set; }
@@ -112,8 +112,13 @@ namespace Content.Server.Atmos
[ViewVariables] [ViewVariables]
public GasMixture Air { get; set; } public GasMixture Air { get; set; }
[ViewVariables, UsedImplicitly]
private int _blockedAirflow => (int)BlockedAirflow;
public AtmosDirection BlockedAirflow { get; set; } = AtmosDirection.Invalid;
[ViewVariables] [ViewVariables]
public bool BlocksAir => _gridAtmosphereComponent.IsAirBlocked(GridIndices); public bool BlocksAllAir => BlockedAirflow == AtmosDirection.All;
public TileAtmosphere(GridAtmosphereComponent atmosphereComponent, GridId gridIndex, MapIndices gridIndices, GasMixture mixture = null, bool immutable = false) public TileAtmosphere(GridAtmosphereComponent atmosphereComponent, GridId gridIndex, MapIndices gridIndices, GasMixture mixture = null, bool immutable = false)
{ {
@@ -868,12 +873,12 @@ namespace Content.Server.Atmos
private void FinishSuperconduction() private void FinishSuperconduction()
{ {
// Conduct with air on my tile if I have it // Conduct with air on my tile if I have it
if (!BlocksAir) if (!BlocksAllAir)
{ {
_temperature = Air.TemperatureShare(ThermalConductivity, _temperature, HeatCapacity); _temperature = Air.TemperatureShare(ThermalConductivity, _temperature, HeatCapacity);
} }
FinishSuperconduction(BlocksAir ? _temperature : Air.Temperature); FinishSuperconduction(BlocksAllAir ? _temperature : Air.Temperature);
} }
private void FinishSuperconduction(float temperature) private void FinishSuperconduction(float temperature)
@@ -887,9 +892,9 @@ namespace Content.Server.Atmos
private void NeighborConductWithSource(TileAtmosphere other) private void NeighborConductWithSource(TileAtmosphere other)
{ {
if (BlocksAir) if (BlocksAllAir)
{ {
if (!other.BlocksAir) if (!other.BlocksAllAir)
{ {
other.TemperatureShareOpenToSolid(this); other.TemperatureShareOpenToSolid(this);
} }
@@ -902,7 +907,7 @@ namespace Content.Server.Atmos
return; return;
} }
if (!other.BlocksAir) if (!other.BlocksAllAir)
{ {
other.Air.TemperatureShare(Air, Atmospherics.WindowHeatTransferCoefficient); other.Air.TemperatureShare(Air, Atmospherics.WindowHeatTransferCoefficient);
} }
@@ -953,7 +958,7 @@ namespace Content.Server.Atmos
public AtmosDirection ConductivityDirections() public AtmosDirection ConductivityDirections()
{ {
if(BlocksAir) if(BlocksAllAir)
{ {
if(_archivedCycle < _gridAtmosphereComponent.UpdateCounter) if(_archivedCycle < _gridAtmosphereComponent.UpdateCounter)
Archive(_gridAtmosphereComponent.UpdateCounter); Archive(_gridAtmosphereComponent.UpdateCounter);
@@ -1150,7 +1155,7 @@ namespace Content.Server.Atmos
_adjacentTiles[direction.ToIndex()] = adjacent; _adjacentTiles[direction.ToIndex()] = adjacent;
adjacent?.UpdateAdjacent(direction.GetOpposite()); adjacent?.UpdateAdjacent(direction.GetOpposite());
if (adjacent != null && !_gridAtmosphereComponent.IsAirBlocked(adjacent.GridIndices, direction.GetOpposite())) if (adjacent != null && !BlockedAirflow.HasFlag(direction) && !_gridAtmosphereComponent.IsAirBlocked(adjacent.GridIndices, direction.GetOpposite()))
{ {
_adjacentBits |= direction; _adjacentBits |= direction;
} }
@@ -1161,7 +1166,7 @@ namespace Content.Server.Atmos
{ {
_adjacentTiles[direction.ToIndex()] = _gridAtmosphereComponent.GetTile(GridIndices.Offset(direction.ToDirection())); _adjacentTiles[direction.ToIndex()] = _gridAtmosphereComponent.GetTile(GridIndices.Offset(direction.ToDirection()));
if (!_gridAtmosphereComponent.IsAirBlocked(GridIndices.Offset(direction.ToDirection()), direction.GetOpposite())) if (!BlockedAirflow.HasFlag(direction) && !_gridAtmosphereComponent.IsAirBlocked(GridIndices.Offset(direction.ToDirection()), direction.GetOpposite()))
{ {
_adjacentBits |= direction; _adjacentBits |= direction;
} }

View File

@@ -9,10 +9,12 @@ using Content.Server.GameObjects.Components.Atmos.Piping;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Shared.Atmos; using Content.Shared.Atmos;
using Content.Shared.Maps; using Content.Shared.Maps;
using Robust.Server.GameObjects.EntitySystems.TileLookup;
using Robust.Server.Interfaces.GameObjects; using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Map; using Robust.Shared.GameObjects.Components.Map;
using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Map;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
@@ -223,20 +225,19 @@ namespace Content.Server.GameObjects.Components.Atmos
} }
else else
{ {
var obs = GetObstructingComponent(indices); if (tile.Air == null && NeedsVacuumFixing(indices))
if (obs != null)
{ {
if (tile.Air == null && obs.FixVacuum) FixVacuum(tile.GridIndices);
{
FixVacuum(tile.GridIndices);
}
} }
tile.Air ??= new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C}; tile.Air ??= new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C};
} }
AddActiveTile(tile); AddActiveTile(tile);
tile.BlockedAirflow = GetBlockedDirections(indices);
// TODO ATMOS: Query all the contents of this tile (like walls) and calculate the correct thermal conductivity
tile.ThermalConductivity = tile.Tile?.Tile.GetContentTileDefinition().ThermalConductivity ?? 0.5f;
tile.UpdateAdjacent(); tile.UpdateAdjacent();
tile.UpdateVisuals(); tile.UpdateVisuals();
@@ -283,6 +284,7 @@ namespace Content.Server.GameObjects.Components.Atmos
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void AddActiveTile(TileAtmosphere? tile) public virtual void AddActiveTile(TileAtmosphere? tile)
{ {
// TODO ATMOS Optimization: Cache the grid Id for faster superconduction. Do the same for all the other ones.
if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return; if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return;
if (tile?.GridIndex != mapGrid.Grid.Index) return; if (tile?.GridIndex != mapGrid.Grid.Index) return;
tile.Excited = true; tile.Excited = true;
@@ -404,8 +406,16 @@ namespace Content.Server.GameObjects.Components.Atmos
/// <inheritdoc /> /// <inheritdoc />
public bool IsAirBlocked(MapIndices indices, AtmosDirection direction = AtmosDirection.All) public bool IsAirBlocked(MapIndices indices, AtmosDirection direction = AtmosDirection.All)
{ {
var ac = GetObstructingComponent(indices); foreach (var obstructingComponent in GetObstructingComponents(indices))
return ac != null && ac.AirBlocked && ac.AirBlockedDirection.HasFlag(direction); {
if (!obstructingComponent.AirBlocked)
continue;
if (obstructingComponent.AirBlockedDirection.HasFlag(direction))
return true;
}
return false;
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -769,17 +779,45 @@ namespace Content.Server.GameObjects.Components.Atmos
return true; return true;
} }
private AirtightComponent? GetObstructingComponent(MapIndices indices) private IEnumerable<AirtightComponent> GetObstructingComponents(MapIndices indices)
{ {
if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return default; if (!Owner.TryGetComponent(out IMapGridComponent? mapGrid)) return Enumerable.Empty<AirtightComponent>();
foreach (var v in mapGrid.Grid.GetSnapGridCell(indices, SnapGridOffset.Center)) var gridLookup = EntitySystem.Get<GridTileLookupSystem>();
var list = new List<AirtightComponent>();
foreach (var v in gridLookup.GetEntitiesIntersecting(mapGrid.GridIndex, indices))
{ {
if (v.Owner.TryGetComponent<AirtightComponent>(out var ac)) if (v.TryGetComponent<AirtightComponent>(out var ac))
return ac; list.Add(ac);
} }
return null; return list;
}
private bool NeedsVacuumFixing(MapIndices indices)
{
var value = false;
foreach (var airtightComponent in GetObstructingComponents(indices))
{
value |= airtightComponent.FixVacuum;
}
return value;
}
private AtmosDirection GetBlockedDirections(MapIndices indices)
{
var value = AtmosDirection.Invalid;
foreach (var airtightComponent in GetObstructingComponents(indices))
{
value |= airtightComponent.AirBlockedDirection;
}
return value;
} }
public void Dispose() public void Dispose()