Gas tile overlay rejig (#9619)

This commit is contained in:
Leon Friedrich
2022-07-25 14:10:18 +12:00
committed by GitHub
parent 2131293788
commit ed068b166f
10 changed files with 607 additions and 864 deletions

View File

@@ -1,4 +1,4 @@
using Content.Shared.Atmos.Prototypes;
using Content.Shared.Atmos.Prototypes;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
@@ -10,22 +10,13 @@ namespace Content.Shared.Atmos.EntitySystems
protected readonly GasPrototype[] GasPrototypes = new GasPrototype[Atmospherics.TotalNumberOfGases];
private readonly SpriteSpecifier?[] _gasOverlays = new SpriteSpecifier[Atmospherics.TotalNumberOfGases];
public override void Initialize()
{
base.Initialize();
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var gasPrototype = _prototypeManager.Index<GasPrototype>(i.ToString());
GasPrototypes[i] = gasPrototype;
if(string.IsNullOrEmpty(gasPrototype.GasOverlaySprite) && !string.IsNullOrEmpty(gasPrototype.GasOverlayTexture))
_gasOverlays[i] = new SpriteSpecifier.Texture(new ResourcePath(gasPrototype.GasOverlayTexture));
if(!string.IsNullOrEmpty(gasPrototype.GasOverlaySprite) && !string.IsNullOrEmpty(gasPrototype.GasOverlayState))
_gasOverlays[i] = new SpriteSpecifier.Rsi(new ResourcePath(gasPrototype.GasOverlaySprite), gasPrototype.GasOverlayState);
GasPrototypes[i] = _prototypeManager.Index<GasPrototype>(i.ToString());
}
}
@@ -34,7 +25,5 @@ namespace Content.Shared.Atmos.EntitySystems
public GasPrototype GetGas(Gas gasId) => GasPrototypes[(int) gasId];
public IEnumerable<GasPrototype> Gases => GasPrototypes;
public SpriteSpecifier? GetOverlay(int overlayId) => _gasOverlays[overlayId];
}
}

View File

@@ -1,4 +1,6 @@
using Robust.Shared.Map;
using Content.Shared.Atmos.Prototypes;
using Content.Shared.GameTicking;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Atmos.EntitySystems
@@ -8,91 +10,74 @@ namespace Content.Shared.Atmos.EntitySystems
public const byte ChunkSize = 8;
protected float AccumulatedFrameTime;
public static Vector2i GetGasChunkIndices(Vector2i indices)
[Dependency] protected readonly IPrototypeManager ProtoMan = default!;
/// <summary>
/// array of the ids of all visible gases.
/// </summary>
public int[] VisibleGasId = default!;
public override void Initialize()
{
return new((int) Math.Floor((float) indices.X / ChunkSize) * ChunkSize, (int) MathF.Floor((float) indices.Y / ChunkSize) * ChunkSize);
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
List<int> visibleGases = new();
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var gasPrototype = ProtoMan.Index<GasPrototype>(i.ToString());
if (!string.IsNullOrEmpty(gasPrototype.GasOverlayTexture) || !string.IsNullOrEmpty(gasPrototype.GasOverlaySprite) && !string.IsNullOrEmpty(gasPrototype.GasOverlayState))
visibleGases.Add(i);
}
VisibleGasId = visibleGases.ToArray();
}
[Serializable, NetSerializable]
public readonly struct GasData : IEquatable<GasData>
public abstract void Reset(RoundRestartCleanupEvent ev);
public static Vector2i GetGasChunkIndices(Vector2i indices)
{
public readonly byte Index;
public readonly byte Opacity;
public GasData(byte gasId, byte opacity)
{
Index = gasId;
Opacity = opacity;
}
public override int GetHashCode()
{
return HashCode.Combine(Index, Opacity);
}
public bool Equals(GasData other)
{
return other.Index == Index && other.Opacity == Opacity;
}
return new((int) MathF.Floor((float) indices.X / ChunkSize), (int) MathF.Floor((float) indices.Y / ChunkSize));
}
[Serializable, NetSerializable]
public readonly struct GasOverlayData : IEquatable<GasOverlayData>
{
public readonly byte FireState;
public readonly float FireTemperature;
public readonly GasData[]? Gas;
public readonly int HashCode;
public readonly byte[] Opacity;
public GasOverlayData(byte fireState, float fireTemperature, GasData[] gas)
// TODO change fire color based on temps
// But also: dont dirty on a 0.01 kelvin change in temperatures.
// Either have a temp tolerance, or map temperature -> byte levels
public GasOverlayData(byte fireState, byte[] opacity)
{
FireState = fireState;
FireTemperature = fireTemperature;
Gas = gas;
Array.Sort(Gas, (a, b) => a.Index.CompareTo(b.Index));
var hash = new HashCode();
hash.Add(FireState);
hash.Add(FireTemperature);
foreach (var gasData in Gas)
{
hash.Add(gasData);
}
HashCode = hash.ToHashCode();
}
public override int GetHashCode()
{
return HashCode;
Opacity = opacity;
}
public bool Equals(GasOverlayData other)
{
// If you revert this then you need to make sure the hash comparison between
// our Gas[] and the other.Gas[] works.
return HashCode == other.HashCode;
if (FireState != other.FireState)
return false;
for (var i = 0; i < Opacity.Length; i++)
{
if (Opacity[i] != other.Opacity[i])
return false;
}
return true;
}
}
/// <summary>
/// Invalid tiles for the gas overlay.
/// No point re-sending every tile if only a subset might have been updated.
/// </summary>
[Serializable, NetSerializable]
public sealed class GasOverlayMessage : EntityEventArgs
public sealed class GasOverlayUpdateEvent : EntityEventArgs
{
public EntityUid GridId { get; }
public List<(Vector2i, GasOverlayData)> OverlayData { get; }
public GasOverlayMessage(EntityUid gridIndices, List<(Vector2i,GasOverlayData)> overlayData)
{
GridId = gridIndices;
OverlayData = overlayData;
}
public Dictionary<EntityUid, List<GasOverlayChunk>> UpdatedChunks = new();
public Dictionary<EntityUid, HashSet<Vector2i>> RemovedChunks = new();
}
}
}

View File

@@ -1,99 +1,97 @@
using Content.Shared.Atmos.EntitySystems;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using System.Diagnostics.CodeAnalysis;
using static Content.Shared.Atmos.EntitySystems.SharedGasTileOverlaySystem;
namespace Content.Shared.Atmos
{
[Serializable, NetSerializable]
[Access(typeof(SharedGasTileOverlaySystem))]
public sealed class GasOverlayChunk
{
/// <summary>
/// Grid for this chunk
/// The index of this chunk
/// </summary>
public EntityUid GridIndices { get; }
public readonly Vector2i Index;
public readonly Vector2i Origin;
/// <summary>
/// Origin of this chunk
/// </summary>
public Vector2i Vector2i { get; }
public GasOverlayData?[][] TileData = new GasOverlayData?[ChunkSize][];
public SharedGasTileOverlaySystem.GasOverlayData[,] TileData = new SharedGasTileOverlaySystem.GasOverlayData[SharedGasTileOverlaySystem.ChunkSize, SharedGasTileOverlaySystem.ChunkSize];
[NonSerialized]
public GameTick LastUpdate;
public GameTick LastUpdate { get; private set; }
public GasOverlayChunk(EntityUid gridIndices, Vector2i vector2i)
public GasOverlayChunk(Vector2i index)
{
GridIndices = gridIndices;
Vector2i = vector2i;
}
Index = index;
Origin = Index * ChunkSize;
public void Dirty(GameTick currentTick)
{
LastUpdate = currentTick;
}
/// <summary>
/// Flags Dirty if the data is different.
/// </summary>
/// <param name="data"></param>
/// <param name="indices"></param>
public void Update(SharedGasTileOverlaySystem.GasOverlayData data, Vector2i indices)
{
DebugTools.Assert(InBounds(indices));
var (offsetX, offsetY) = (indices.X - Vector2i.X,
indices.Y - Vector2i.Y);
TileData[offsetX, offsetY] = data;
}
public void Update(SharedGasTileOverlaySystem.GasOverlayData data, byte x, byte y)
{
DebugTools.Assert(x < SharedGasTileOverlaySystem.ChunkSize && y < SharedGasTileOverlaySystem.ChunkSize);
TileData[x, y] = data;
}
public IEnumerable<SharedGasTileOverlaySystem.GasOverlayData> GetAllData()
{
for (var x = 0; x < SharedGasTileOverlaySystem.ChunkSize; x++)
// For whatever reason, net serialize does not like multi_D arrays. So Jagged it is.
for (var i = 0; i < ChunkSize; i++)
{
for (var y = 0; y < SharedGasTileOverlaySystem.ChunkSize; y++)
TileData[i] = new GasOverlayData?[ChunkSize];
}
}
public GasOverlayData? GetData(Vector2i gridIndices)
{
DebugTools.Assert(InBounds(gridIndices));
return TileData[gridIndices.X - Origin.X][gridIndices.Y - Origin.Y];
}
public GasOverlayData? SetData(Vector2i gridIndices, GasOverlayData? data)
{
DebugTools.Assert(InBounds(gridIndices));
return TileData[gridIndices.X - Origin.X][gridIndices.Y - Origin.Y] = data;
}
private bool InBounds(Vector2i gridIndices)
{
return gridIndices.X >= Origin.X &&
gridIndices.Y >= Origin.Y &&
gridIndices.X < Origin.X + ChunkSize &&
gridIndices.Y < Origin.Y + ChunkSize;
}
}
public struct GasChunkEnumerator
{
private GasOverlayChunk _chunk;
public int X = 0;
public int Y = -1;
private GasOverlayData?[] _column;
public GasChunkEnumerator(GasOverlayChunk chunk)
{
_chunk = chunk;
_column = _chunk.TileData[0];
}
public bool MoveNext([NotNullWhen(true)] out GasOverlayData? gas)
{
while (X < ChunkSize)
{
// We want to increment Y before returning, but we also want it to match the current Y coordinate for
// the returned gas, so using a slightly different logic for the Y loop.
while (Y < ChunkSize - 1)
{
yield return TileData[x, y];
Y++;
gas = _column[Y];
if (gas != null)
return true;
}
X++;
if (X < ChunkSize)
_column = _chunk.TileData[X];
Y = -1;
}
}
public void GetData(List<(Vector2i, SharedGasTileOverlaySystem.GasOverlayData)> existingData, HashSet<Vector2i> indices)
{
foreach (var index in indices)
{
existingData.Add((index, GetData(index)));
}
}
public IEnumerable<Vector2i> GetAllIndices()
{
for (var x = 0; x < SharedGasTileOverlaySystem.ChunkSize; x++)
{
for (var y = 0; y < SharedGasTileOverlaySystem.ChunkSize; y++)
{
yield return new Vector2i(Vector2i.X + x, Vector2i.Y + y);
}
}
}
public SharedGasTileOverlaySystem.GasOverlayData GetData(Vector2i indices)
{
DebugTools.Assert(InBounds(indices));
return TileData[indices.X - Vector2i.X, indices.Y - Vector2i.Y];
}
private bool InBounds(Vector2i indices)
{
if (indices.X < Vector2i.X || indices.Y < Vector2i.Y) return false;
if (indices.X >= Vector2i.X + SharedGasTileOverlaySystem.ChunkSize || indices.Y >= Vector2i.Y + SharedGasTileOverlaySystem.ChunkSize) return false;
return true;
gas = null;
return false;
}
}
}