Files
tbd-station-14/Content.Server/GameObjects/Components/Atmos/GridAtmosphereComponent.cs
Víctor Aguilera Puerto 85df48a700 Adds atmos (#1389)
* Add initial atmospherics framework

* Make walls and airlocks airtight

* Add the basic atmosphere gas system

* Atmos: move CardinalToIntVec and Offset into extention methods in EntityNetworkUtils

* Optimize vending machine code a bit.

* Address feedback from Atmos PR, make code compile

Fix use of OnMove -> SnapGridComponent.OnPositionChanged.
Added pause checking to atmos simulations.

* Improvements to the existing ZAS prototype (#996)

* Rename Volume -> Quantity in GasProperty

This makes the name consistent with the rest of the code, given that
it's meant to be a mol value.

* Replace Gas enum with GasPrototype

Unused as of yet, but laying the groundwork for future changes.

* Update AtmosphereMap, improving maths

Adds a temporary default atmosphere, hardcoded in for testing. It will
be replaced eventually.

Fixed a maths mistake in the original code involving unit conversions.

Added the Take() method to IAtmosphere, for taking some volume of
mixture out of a gas mix. This will be pretty handy in the future, but
for now it's used by walls to delete air where they are built.

* Fix merging, splitting logic for zones

Removing a cell from a zone now correctly reduces its volume.
Adding a cell to a zone now correctly increases its volume.

* Improvements to atmos code, reorganising of types

Moved GasPrototype to shared, because it's going to be used by the
client later.

Split up the atmosphere code. Now zones are explicitly their own kind of
atmosphere, which should speed up some loops and also solve
inconsistencies in the volume calculation.
GasMixtures are another type of atmosphere, for more general use.

Try to fix the splitting/merging code, which was quite cryptic and not
clear at all. It *should* work now.

* Switch zones back to MapIndices

Turns out I'm an idiot who misunderstood the code. MapIndices can be
used as one-per-tile, which is what is needed for atmos. GridCoordinates
are many-per-tile, and were causing lots of problems.

* Add zone debugging overlay

This is the first example of zone information being sent to a client.

It adds the `togglezoneinfo` command, which overlays the tiles and gas
information for the zone currently occupied by the user on the screen.
This was helpful for debugging the GridCoordinates problem.

* Fix position of atmos zone debug text

* Make AirtightComponent only activate on MapInit

This should stop it splitting atmospheres in mapping.

* Doc comments improvements to AtmosphereMap

Fix some malformed comments, inherit some useful docs, document some
more functions.

* Add zone logic for changing tiles to/from space

Zones are now correctly created when all the tiles in a room are built,
and destroyed when one of the tiles is destroyed.

* update engine

* right

* Cleanup code

* Port GasMixture, further cleanup

* Fix windows not being airtight, some other stuff

* Work on atmos

* Difference between ZoneBlocked and AirBlocked

* Big GridAtmosphereManager cleanup, zones are broken now oops

* Remove zones, add excited group implementation

* Further cleanup

* Further work on atmos

* Work on gas overlay.

* PumpGasTo and ReleaseGasTo methods for GasMixture.

* Adds Tile Overlay System.

* More work on atmos

* Gasses spread, equalize and all that

* Fix a few crashes

* Gas can actually spread from room to room after opening airlocks

* Add explosive depressurization, tile prying on depressurization, gas spreading on wall break. Etc.

* More work

* Remove atmoschem, add "performant" gas overlays

* what the fuck git

* More work I guess?

* Fix stuff, create a few (empty) components for future work

* Fix temperature

* Fix tile air sharing

* Atmos optimizations

* Further atmos optimizations

* Even more optimizations!

* Update Content.Client/GameObjects/EntitySystems/GasTileOverlaySystem.cs

Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>

* Update Content.Client/GameObjects/EntitySystems/GasTileOverlaySystem.cs

Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>

* Update Content.Client/GameObjects/EntitySystems/GasTileOverlaySystem.cs

Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>

* Address a few reviews

* Oops, forgot to remove this

* Update Content.Server/Atmos/AtmosphereMap.cs

Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>

* Fix compile

* Improved client gas tile overlays

* Less allocations

* Even less allocations!

* some stuff dunno

* Big refactor time, oh yeah

* it truly is 1 AM

* bruh

* No idea why now it doesn't work.
Oh well! I'll fix this on thursday 😌

* Basic atmos serialization

* Fix bugs, add VV attributes

* Start adding stuff for gas reactions

* Add a few atmos commands

* Fill gas command

* Changes to gas opacity

* Fixes I guess

* Fixes, add space wind sound

* High pressure movement!

* Better high pressure movements.

* Fix direction, maybe?

* And now it works great

* Science!

* and this is why you don't trust people

* remove logging

* Turns out I'm fucking dumb

* Work on hotspots and reactions, add tritium gas

* IGridAtmosphereComponent interface! For future unsimulated grids.

* Makes atmos updates NoGC.

* C E A S E

* Add settemp atmos command

* Important reminder.

* Remove useless AtmosCooldown.

* More work on hotspots

* Overlays for hotspots. Fire works!

* Turns out I suck at coding

* Fire texture change

* Yeah let's make that an absolute value, hmm?

* Support for atmos after teleporting grid (more or less)

* fix attempt (doesn't actually fix it)

* Make static variable not static

* Remove magic numbers

* Stopwatch moment

* Slight cleanup.

* More cleanup.

* Atmos optimizations

* Fix build

* Remove useless ghost atmos shit

* Adds CanSeeGases component for gas overlay stuff

* Component and prototype ignores

* ExcitedGroups now dispose on being merged with others

* Some tweaking.

* Atmos now uses frame time for updates.

* Nullable boogaloo

* IExamine fix

* Fix build

* Fix restartround

* Atmos tweaking, use invalid direction

* Increase high pressure movement force

* Better sort

* Update submodule.

* NULLABILITY AAAAH

Special thanks to monster860 and all monstermos contributors!

Co-authored-by: Campbell Suter <znix@znix.xyz>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: ComicIronic <comicironic@gmail.com>
Co-authored-by: silicons <2003111+silicons@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
2020-08-02 20:08:20 +02:00

524 lines
17 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Atmos;
using Content.Shared.Atmos;
using Content.Shared.Maps;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Map;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Atmos
{
/// <summary>
/// This is our SSAir equivalent.
/// </summary>
[RegisterComponent, Serializable]
public class GridAtmosphereComponent : Component, IGridAtmosphereComponent
{
[Robust.Shared.IoC.Dependency] private IGameTiming _gameTiming = default!;
[Robust.Shared.IoC.Dependency] private IMapManager _mapManager = default!;
/// <summary>
/// Check current execution time every n instances processed.
/// </summary>
private const int LagCheckIterations = 15;
/// <summary>
/// Max milliseconds allowed for atmos updates.
/// </summary>
private const float LagCheckMaxMilliseconds = 5f;
/// <summary>
/// How much time before atmos updates are ran.
/// </summary>
private const float AtmosTime = 1/26f;
public override string Name => "GridAtmosphere";
private float _timer = 0f;
private Stopwatch _stopwatch = new Stopwatch();
public int UpdateCounter { get; private set; } = 0;
private IMapGrid _grid;
[ViewVariables]
private readonly HashSet<ExcitedGroup> _excitedGroups = new HashSet<ExcitedGroup>(1000);
[ViewVariables]
private readonly Dictionary<MapIndices, TileAtmosphere> _tiles = new Dictionary<MapIndices, TileAtmosphere>(1000);
[ViewVariables]
private readonly HashSet<TileAtmosphere> _activeTiles = new HashSet<TileAtmosphere>(1000);
[ViewVariables]
private readonly HashSet<TileAtmosphere> _hotspotTiles = new HashSet<TileAtmosphere>(1000);
[ViewVariables]
private readonly HashSet<MapIndices> _invalidatedCoords = new HashSet<MapIndices>(1000);
[ViewVariables]
private HashSet<TileAtmosphere> _highPressureDelta = new HashSet<TileAtmosphere>(1000);
[ViewVariables]
private ProcessState _state = ProcessState.TileEqualize;
private enum ProcessState
{
TileEqualize,
ActiveTiles,
ExcitedGroups,
HighPressureDelta,
Hotspots,
}
/// <inheritdoc />
public void PryTile(MapIndices indices)
{
if (IsSpace(indices) || IsAirBlocked(indices)) return;
var tile = _grid.GetTileRef(indices).Tile;
var tileDefinitionManager = IoCManager.Resolve<ITileDefinitionManager>();
var tileDef = (ContentTileDefinition)tileDefinitionManager[tile.TypeId];
var underplating = tileDefinitionManager["underplating"];
_grid.SetTile(indices, new Tile(underplating.TileId));
//Actually spawn the relevant tile item at the right position and give it some offset to the corner.
var tileItem = IoCManager.Resolve<IServerEntityManager>().SpawnEntity(tileDef.ItemDropPrototypeName, new GridCoordinates(indices.X, indices.Y, _grid));
tileItem.Transform.WorldPosition += (0.2f, 0.2f);
}
public override void Initialize()
{
base.Initialize();
_grid = Owner.GetComponent<IMapGridComponent>().Grid;
RepopulateTiles();
}
public override void OnAdd()
{
base.OnAdd();
_grid = Owner.GetComponent<IMapGridComponent>().Grid;
RepopulateTiles();
}
public void RepopulateTiles()
{
_tiles.Clear();
foreach (var tile in _grid.GetAllTiles())
{
if(!_tiles.ContainsKey(tile.GridIndices))
_tiles.Add(tile.GridIndices, new TileAtmosphere(this, tile.GridIndex, tile.GridIndices, new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C}));
}
foreach (var (_, tile) in _tiles.ToArray())
{
tile.UpdateAdjacent();
tile.UpdateVisuals();
}
}
/// <inheritdoc />
public void Invalidate(MapIndices indices)
{
_invalidatedCoords.Add(indices);
}
private void Revalidate()
{
foreach (var indices in _invalidatedCoords.ToArray())
{
var tile = GetTile(indices);
AddActiveTile(tile);
if (tile == null)
{
tile = new TileAtmosphere(this, _grid.Index, indices, new GasMixture(GetVolumeForCells(1)){Temperature = Atmospherics.T20C});
_tiles.Add(indices, tile);
}
if (IsSpace(indices))
{
tile.Air = new GasMixture(GetVolumeForCells(1));
tile.Air.MarkImmutable();
} else if (IsAirBlocked(indices))
{
tile.Air = null;
}
else
{
tile.Air ??= new GasMixture(GetVolumeForCells(1));
}
tile.UpdateAdjacent();
tile.UpdateVisuals();
foreach (var direction in Cardinal())
{
var otherIndices = indices.Offset(direction);
var otherTile = GetTile(otherIndices);
AddActiveTile(otherTile);
otherTile?.UpdateAdjacent(direction.GetOpposite());
}
}
_invalidatedCoords.Clear();
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddActiveTile(TileAtmosphere tile)
{
if (tile?.GridIndex != _grid.Index || tile?.Air == null) return;
tile.Excited = true;
_activeTiles.Add(tile);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveActiveTile(TileAtmosphere tile)
{
if (tile == null) return;
_activeTiles.Remove(tile);
tile.Excited = false;
tile.ExcitedGroup?.Dispose();
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHotspotTile(TileAtmosphere tile)
{
if (tile?.GridIndex != _grid.Index || tile?.Air == null) return;
_hotspotTiles.Add(tile);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveHotspotTile(TileAtmosphere tile)
{
if (tile == null) return;
_hotspotTiles.Remove(tile);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddHighPressureDelta(TileAtmosphere tile)
{
if (tile?.GridIndex != _grid.Index) return;
_highPressureDelta.Add(tile);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasHighPressureDelta(TileAtmosphere tile)
{
return _highPressureDelta.Contains(tile);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddExcitedGroup(ExcitedGroup excitedGroup)
{
_excitedGroups.Add(excitedGroup);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveExcitedGroup(ExcitedGroup excitedGroup)
{
_excitedGroups.Remove(excitedGroup);
}
/// <inheritdoc />
public TileAtmosphere GetTile(GridCoordinates coordinates)
{
return GetTile(coordinates.ToMapIndices(_mapManager));
}
/// <inheritdoc />
public TileAtmosphere GetTile(MapIndices indices)
{
if (_tiles.TryGetValue(indices, out var tile)) return tile;
// We don't have that tile!
if (IsSpace(indices))
{
var space = new TileAtmosphere(this, _grid.Index, indices, new GasMixture(int.MaxValue){Temperature = Atmospherics.TCMB});
space.Air.MarkImmutable();
return space;
}
return null;
}
/// <inheritdoc />
public bool IsAirBlocked(MapIndices indices)
{
var ac = GetObstructingComponent(indices);
return ac != null && ac.AirBlocked;
}
/// <inheritdoc />
public bool IsSpace(MapIndices indices)
{
// TODO ATMOS use ContentTileDefinition to define in YAML whether or not a tile is considered space
return _grid.GetTileRef(indices).Tile.IsEmpty;
}
public Dictionary<Direction, TileAtmosphere> GetAdjacentTiles(MapIndices indices)
{
var sides = new Dictionary<Direction, TileAtmosphere>();
foreach (var dir in Cardinal())
{
var side = indices.Offset(dir);
sides[dir] = GetTile(side);
}
return sides;
}
/// <inheritdoc />
public int HighPressureDeltaCount => _highPressureDelta.Count;
public long EqualizationQueueCycleControl { get; set; }
/// <inheritdoc />
public float GetVolumeForCells(int cellCount)
{
return _grid.TileSize * cellCount * Atmospherics.CellVolume;
}
/// <inheritdoc />
public void Update(float frameTime)
{
_timer += frameTime;
if (_invalidatedCoords.Count != 0)
Revalidate();
if (_timer < AtmosTime)
return;
// We subtract it so it takes lost time into account.
_timer -= AtmosTime;
switch (_state)
{
case ProcessState.TileEqualize:
if(ProcessTileEqualize())
_state = ProcessState.ActiveTiles;
return;
case ProcessState.ActiveTiles:
if(ProcessActiveTiles())
_state = ProcessState.ExcitedGroups;
return;
case ProcessState.ExcitedGroups:
if(ProcessExcitedGroups())
_state = ProcessState.HighPressureDelta;
return;
case ProcessState.HighPressureDelta:
if(ProcessHighPressureDelta())
_state = ProcessState.Hotspots;
break;
case ProcessState.Hotspots:
if(ProcessHotspots())
_state = ProcessState.TileEqualize;
break;
}
UpdateCounter++;
}
public bool ProcessTileEqualize()
{
_stopwatch.Restart();
var number = 0;
foreach (var tile in _activeTiles.ToArray())
{
tile.EqualizePressureInZone(UpdateCounter);
if (number++ < LagCheckIterations) continue;
number = 0;
// Process the rest next time.
if (_stopwatch.Elapsed.TotalMilliseconds >= LagCheckMaxMilliseconds)
return false;
}
return true;
}
public bool ProcessActiveTiles()
{
_stopwatch.Restart();
var number = 0;
foreach (var tile in _activeTiles.ToArray())
{
tile.ProcessCell(UpdateCounter);
if (number++ < LagCheckIterations) continue;
number = 0;
// Process the rest next time.
if (_stopwatch.Elapsed.TotalMilliseconds >= LagCheckMaxMilliseconds)
return false;
}
return true;
}
public bool ProcessExcitedGroups()
{
_stopwatch.Restart();
var number = 0;
foreach (var excitedGroup in _excitedGroups.ToArray())
{
excitedGroup.BreakdownCooldown++;
excitedGroup.DismantleCooldown++;
if(excitedGroup.BreakdownCooldown > Atmospherics.ExcitedGroupBreakdownCycles)
excitedGroup.SelfBreakdown();
else if(excitedGroup.DismantleCooldown > Atmospherics.ExcitedGroupsDismantleCycles)
excitedGroup.Dismantle();
if (number++ < LagCheckIterations) continue;
number = 0;
// Process the rest next time.
if (_stopwatch.Elapsed.TotalMilliseconds >= LagCheckMaxMilliseconds)
return false;
}
return true;
}
public bool ProcessHighPressureDelta()
{
_stopwatch.Restart();
var number = 0;
foreach (var tile in _highPressureDelta.ToArray())
{
tile.HighPressureMovements();
tile.PressureDifference = 0f;
tile.PressureSpecificTarget = null;
_highPressureDelta.Remove(tile);
if (number++ < LagCheckIterations) continue;
number = 0;
// Process the rest next time.
if (_stopwatch.Elapsed.TotalMilliseconds >= LagCheckMaxMilliseconds)
return false;
}
return true;
}
private bool ProcessHotspots()
{
_stopwatch.Restart();
var number = 0;
foreach (var hotspot in _hotspotTiles.ToArray())
{
hotspot.ProcessHotspot();
if (number++ < LagCheckIterations) continue;
number = 0;
// Process the rest next time.
if (_stopwatch.Elapsed.TotalMilliseconds >= LagCheckMaxMilliseconds)
return false;
}
return true;
}
private AirtightComponent GetObstructingComponent(MapIndices indices)
{
foreach (var v in _grid.GetSnapGridCell(indices, SnapGridOffset.Center))
{
if (v.Owner.TryGetComponent<AirtightComponent>(out var ac))
return ac;
}
return null;
}
private static IEnumerable<Direction> Cardinal() =>
new[]
{
Direction.North, Direction.East, Direction.South, Direction.West
};
public void Dispose()
{
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
if (serializer.Reading)
{
var gridId = Owner.GetComponent<IMapGridComponent>().Grid.Index;
if (!serializer.TryReadDataField("uniqueMixes", out List<GasMixture> uniqueMixes) ||
!serializer.TryReadDataField("tiles", out Dictionary<MapIndices, int> tiles))
return;
foreach (var (indices, mix) in tiles)
{
_tiles.Add(indices, new TileAtmosphere(this, gridId, indices, (GasMixture)uniqueMixes[mix].Clone()));
Invalidate(indices);
}
} else if (serializer.Writing)
{
var uniqueMixes = new List<GasMixture>();
var tiles = new Dictionary<MapIndices, int>();
foreach (var (indices, tile) in _tiles)
{
if (tile.Air == null) continue;
uniqueMixes.Add(tile.Air);
tiles[indices] = uniqueMixes.Count - 1;
}
serializer.DataField(ref uniqueMixes, "uniqueMixes", new List<GasMixture>());
serializer.DataField(ref tiles, "tiles", new Dictionary<MapIndices, int>());
}
}
public IEnumerator<TileAtmosphere> GetEnumerator()
{
return _tiles.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <inheritdoc />
public void BurnTile(MapIndices gridIndices)
{
// TODO ATMOS
}
}
}