Files
tbd-station-14/Content.Server/Atmos/ExcitedGroup.cs
Víctor Aguilera Puerto 6969b79c1c Improves atmos even more.
* Adds AtmosCooldown to tiles, so they stop being active after a certain number of cycles without a cooldown reset.
* Fixes adjacentTileLength in ProcessCell. It wasn't being set beforehand, (probably) causing some incorrect air shares.
* Removing a tile from active tiles no longer unexcites every other tile in their excited group. (Basically, Dismantle in ExcitedGroup's Dispose() method no longer unexcites tiles)
2020-09-04 17:40:44 +02:00

141 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Atmos;
using Content.Shared.Atmos;
using Robust.Shared.ViewVariables;
namespace Content.Server.Atmos
{
public class ExcitedGroup : IDisposable
{
[ViewVariables]
private bool _disposed = false;
[ViewVariables]
private readonly HashSet<TileAtmosphere> _tile = new HashSet<TileAtmosphere>();
[ViewVariables]
private GridAtmosphereComponent _gridAtmosphereComponent;
[ViewVariables]
public int DismantleCooldown { get; set; }
[ViewVariables]
public int BreakdownCooldown { get; set; }
public void AddTile(TileAtmosphere tile)
{
_tile.Add(tile);
tile.ExcitedGroup = this;
ResetCooldowns();
}
public void MergeGroups(ExcitedGroup other)
{
var ourSize = _tile.Count;
var otherSize = other._tile.Count;
if (ourSize > otherSize)
{
foreach (var tile in other._tile)
{
tile.ExcitedGroup = this;
_tile.Add(tile);
}
other._tile.Clear();
other.Dispose();
ResetCooldowns();
}
else
{
foreach (var tile in _tile)
{
tile.ExcitedGroup = other;
other._tile.Add(tile);
}
_tile.Clear();
Dispose();
other.ResetCooldowns();
}
}
~ExcitedGroup()
{
Dispose();
}
public void Initialize(GridAtmosphereComponent gridAtmosphereComponent)
{
_gridAtmosphereComponent = gridAtmosphereComponent;
_gridAtmosphereComponent.AddExcitedGroup(this);
}
public void ResetCooldowns()
{
BreakdownCooldown = 0;
DismantleCooldown = 0;
}
public void SelfBreakdown(bool spaceIsAllConsuming = false)
{
var combined = new GasMixture(Atmospherics.CellVolume);
var tileSize = _tile.Count;
if (_disposed) return;
if (tileSize == 0)
{
Dispose();
return;
}
foreach (var tile in _tile)
{
if (tile?.Air == null) continue;
combined.Merge(tile.Air);
if (!spaceIsAllConsuming || !tile.Air.Immutable) continue;
combined.Clear();
break;
}
combined.Multiply(1 / (float)tileSize);
foreach (var tile in _tile)
{
if (tile?.Air == null) continue;
tile.Air.CopyFromMutable(combined);
tile.AtmosCooldown = 0;
tile.UpdateVisuals();
}
BreakdownCooldown = 0;
}
public void Dismantle(bool unexcite = true)
{
foreach (var tile in _tile)
{
if (tile == null) continue;
tile.ExcitedGroup = null;
if (!unexcite) continue;
tile.Excited = false;
_gridAtmosphereComponent.RemoveActiveTile(tile);
}
_tile.Clear();
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_gridAtmosphereComponent.RemoveExcitedGroup(this);
Dismantle(false);
_gridAtmosphereComponent = null;
}
}
}