using Content.Client.Atmos.Overlays;
+using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems;
using JetBrains.Annotations;
private void OnHandleState(EntityUid gridUid, GasTileOverlayComponent comp, ref ComponentHandleState args)
{
- if (args.Current is not GasTileOverlayState state)
- return;
+ Dictionary<Vector2i, GasOverlayChunk> modifiedChunks;
- // is this a delta or full state?
- if (!state.FullState)
+ switch (args.Current)
{
- foreach (var index in comp.Chunks.Keys)
+ // is this a delta or full state?
+ case GasTileOverlayDeltaState delta:
{
- if (!state.AllChunks!.Contains(index))
- comp.Chunks.Remove(index);
+ modifiedChunks = delta.ModifiedChunks;
+ foreach (var index in comp.Chunks.Keys)
+ {
+ if (!delta.AllChunks.Contains(index))
+ comp.Chunks.Remove(index);
+ }
+
+ break;
}
- }
- else
- {
- foreach (var index in comp.Chunks.Keys)
+ case GasTileOverlayState state:
{
- if (!state.Chunks.ContainsKey(index))
- comp.Chunks.Remove(index);
+ modifiedChunks = state.Chunks;
+ foreach (var index in comp.Chunks.Keys)
+ {
+ if (!state.Chunks.ContainsKey(index))
+ comp.Chunks.Remove(index);
+ }
+
+ break;
}
+ default:
+ return;
}
- foreach (var (index, data) in state.Chunks)
+ foreach (var (index, data) in modifiedChunks)
{
comp.Chunks[index] = data;
}
private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
{
- if (args.Current is not DecalGridState state)
- return;
-
// is this a delta or full state?
_removedChunks.Clear();
+ Dictionary<Vector2i, DecalChunk> modifiedChunks;
- if (!state.FullState)
+ switch (args.Current)
{
- foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+ case DecalGridDeltaState delta:
{
- if (!state.AllChunks!.Contains(key))
- _removedChunks.Add(key);
+ modifiedChunks = delta.ModifiedChunks;
+ foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+ {
+ if (!delta.AllChunks.Contains(key))
+ _removedChunks.Add(key);
+ }
+
+ break;
}
- }
- else
- {
- foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+ case DecalGridState state:
{
- if (!state.Chunks.ContainsKey(key))
- _removedChunks.Add(key);
+ modifiedChunks = state.Chunks;
+ foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+ {
+ if (!state.Chunks.ContainsKey(key))
+ _removedChunks.Add(key);
+ }
+
+ break;
}
+ default:
+ return;
}
if (_removedChunks.Count > 0)
RemoveChunks(gridUid, gridComp, _removedChunks);
- if (state.Chunks.Count > 0)
- UpdateChunks(gridUid, gridComp, state.Chunks);
+ if (modifiedChunks.Count > 0)
+ UpdateChunks(gridUid, gridComp, modifiedChunks);
}
private void OnChunkUpdate(DecalChunkUpdateEvent ev)
private void OnHandleState(EntityUid uid, NavMapComponent component, ref ComponentHandleState args)
{
- if (args.Current is not NavMapComponentState state)
- return;
+ Dictionary<Vector2i, int[]> modifiedChunks;
+ Dictionary<NetEntity, NavMapBeacon> beacons;
- if (!state.FullState)
+ switch (args.Current)
{
- foreach (var index in component.Chunks.Keys)
+ case NavMapDeltaState delta:
{
- if (!state.AllChunks!.Contains(index))
- component.Chunks.Remove(index);
+ modifiedChunks = delta.ModifiedChunks;
+ beacons = delta.Beacons;
+ foreach (var index in component.Chunks.Keys)
+ {
+ if (!delta.AllChunks!.Contains(index))
+ component.Chunks.Remove(index);
+ }
+
+ break;
}
- }
- else
- {
- foreach (var index in component.Chunks.Keys)
+ case NavMapState state:
{
- if (!state.Chunks.ContainsKey(index))
- component.Chunks.Remove(index);
+ modifiedChunks = state.Chunks;
+ beacons = state.Beacons;
+ foreach (var index in component.Chunks.Keys)
+ {
+ if (!state.Chunks.ContainsKey(index))
+ component.Chunks.Remove(index);
+ }
+
+ break;
}
+ default:
+ return;
}
- foreach (var (origin, chunk) in state.Chunks)
+ foreach (var (origin, chunk) in modifiedChunks)
{
var newChunk = new NavMapChunk(origin);
Array.Copy(chunk, newChunk.TileData, chunk.Length);
}
component.Beacons.Clear();
- foreach (var (nuid, beacon) in state.Beacons)
+ foreach (var (nuid, beacon) in beacons)
{
component.Beacons[nuid] = beacon;
}
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
-using Robust.Shared.Utility;
namespace Content.Shared.Atmos.Components;
public GameTick ForceTick { get; set; }
}
-
[Serializable, NetSerializable]
-public sealed class GasTileOverlayState : ComponentState, IComponentDeltaState
+public sealed class GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks) : ComponentState
{
- public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks;
- public bool FullState => AllChunks == null;
-
- // required to infer deleted/missing chunks for delta states
- public HashSet<Vector2i>? AllChunks;
+ public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks = chunks;
+}
- public GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks)
- {
- Chunks = chunks;
- }
+[Serializable, NetSerializable]
+public sealed class GasTileOverlayDeltaState(
+ Dictionary<Vector2i, GasOverlayChunk> modifiedChunks,
+ HashSet<Vector2i> allChunks)
+ : ComponentState, IComponentDeltaState<GasTileOverlayState>
+{
+ public readonly Dictionary<Vector2i, GasOverlayChunk> ModifiedChunks = modifiedChunks;
+ public readonly HashSet<Vector2i> AllChunks = allChunks;
- public void ApplyToFullState(IComponentState fullState)
+ public void ApplyToFullState(GasTileOverlayState state)
{
- DebugTools.Assert(!FullState);
- var state = (GasTileOverlayState) fullState;
- DebugTools.Assert(state.FullState);
-
foreach (var key in state.Chunks.Keys)
{
- if (!AllChunks!.Contains(key))
+ if (!AllChunks.Contains(key))
state.Chunks.Remove(key);
}
- foreach (var (chunk, data) in Chunks)
+ foreach (var (chunk, data) in ModifiedChunks)
{
state.Chunks[chunk] = new(data);
}
}
- public IComponentState CreateNewFullState(IComponentState fullState)
+ public GasTileOverlayState CreateNewFullState(GasTileOverlayState state)
{
- DebugTools.Assert(!FullState);
- var state = (GasTileOverlayState) fullState;
- DebugTools.Assert(state.FullState);
-
- var chunks = new Dictionary<Vector2i, GasOverlayChunk>(state.Chunks.Count);
+ var chunks = new Dictionary<Vector2i, GasOverlayChunk>(AllChunks.Count);
- foreach (var (chunk, data) in Chunks)
+ foreach (var (chunk, data) in ModifiedChunks)
{
chunks[chunk] = new(data);
}
foreach (var (chunk, data) in state.Chunks)
{
- if (AllChunks!.Contains(chunk))
+ if (AllChunks.Contains(chunk))
chunks.TryAdd(chunk, new(data));
}
data[index] = chunk;
}
- args.State = new GasTileOverlayState(data) { AllChunks = new(component.Chunks.Keys) };
+ args.State = new GasTileOverlayDeltaState(data, new(component.Chunks.Keys));
}
public static Vector2i GetGasChunkIndices(Vector2i indices)
}
[Serializable, NetSerializable]
- public sealed class DecalGridState : ComponentState, IComponentDeltaState
+ public sealed class DecalGridState(Dictionary<Vector2i, DecalChunk> chunks) : ComponentState
{
- public Dictionary<Vector2i, DecalChunk> Chunks;
- public bool FullState => AllChunks == null;
-
- // required to infer deleted/missing chunks for delta states
- public HashSet<Vector2i>? AllChunks;
+ public Dictionary<Vector2i, DecalChunk> Chunks = chunks;
+ }
- public DecalGridState(Dictionary<Vector2i, DecalChunk> chunks)
- {
- Chunks = chunks;
- }
+ [Serializable, NetSerializable]
+ public sealed class DecalGridDeltaState(Dictionary<Vector2i, DecalChunk> modifiedChunks, HashSet<Vector2i> allChunks)
+ : ComponentState, IComponentDeltaState<DecalGridState>
+ {
+ public Dictionary<Vector2i, DecalChunk> ModifiedChunks = modifiedChunks;
+ public HashSet<Vector2i> AllChunks = allChunks;
- public void ApplyToFullState(IComponentState fullState)
+ public void ApplyToFullState(DecalGridState state)
{
- DebugTools.Assert(!FullState);
- var state = (DecalGridState) fullState;
- DebugTools.Assert(state.FullState);
-
foreach (var key in state.Chunks.Keys)
{
if (!AllChunks!.Contains(key))
state.Chunks.Remove(key);
}
- foreach (var (chunk, data) in Chunks)
+ foreach (var (chunk, data) in ModifiedChunks)
{
state.Chunks[chunk] = new(data);
}
}
- public IComponentState CreateNewFullState(IComponentState fullState)
+ public DecalGridState CreateNewFullState(DecalGridState state)
{
- DebugTools.Assert(!FullState);
- var state = (DecalGridState) fullState;
- DebugTools.Assert(state.FullState);
-
var chunks = new Dictionary<Vector2i, DecalChunk>(state.Chunks.Count);
- foreach (var (chunk, data) in Chunks)
+ foreach (var (chunk, data) in ModifiedChunks)
{
chunks[chunk] = new(data);
}
data[index] = chunk;
}
- args.State = new DecalGridState(data) { AllChunks = new(component.ChunkCollection.ChunkCollection.Keys) };
+ args.State = new DecalGridDeltaState(data, new(component.ChunkCollection.ChunkCollection.Keys));
}
private void OnGridInitialize(GridInitializeEvent msg)
chunks.Add(origin, chunk.TileData);
}
- args.State = new NavMapComponentState(chunks, component.Beacons);
+ args.State = new NavMapState(chunks, component.Beacons);
return;
}
chunks.Add(origin, chunk.TileData);
}
- args.State = new NavMapComponentState(chunks, component.Beacons)
- {
- // TODO NAVMAP cache a single AllChunks hashset in the component.
- // Or maybe just only send them if a chunk gets removed.
- AllChunks = new(component.Chunks.Keys),
- };
+ args.State = new NavMapDeltaState(chunks, component.Beacons, new(component.Chunks.Keys));
}
#endregion
#region: System messages
[Serializable, NetSerializable]
- protected sealed class NavMapComponentState(
+ protected sealed class NavMapState(
Dictionary<Vector2i, int[]> chunks,
Dictionary<NetEntity, NavMapBeacon> beacons)
- : ComponentState, IComponentDeltaState
+ : ComponentState
{
public Dictionary<Vector2i, int[]> Chunks = chunks;
public Dictionary<NetEntity, NavMapBeacon> Beacons = beacons;
+ }
- // Required to infer deleted/missing chunks for delta states
- public HashSet<Vector2i>? AllChunks;
-
- public bool FullState => AllChunks == null;
+ [Serializable, NetSerializable]
+ protected sealed class NavMapDeltaState(
+ Dictionary<Vector2i, int[]> modifiedChunks,
+ Dictionary<NetEntity, NavMapBeacon> beacons,
+ HashSet<Vector2i> allChunks)
+ : ComponentState, IComponentDeltaState<NavMapState>
+ {
+ public Dictionary<Vector2i, int[]> ModifiedChunks = modifiedChunks;
+ public Dictionary<NetEntity, NavMapBeacon> Beacons = beacons;
+ public HashSet<Vector2i> AllChunks = allChunks;
- public void ApplyToFullState(IComponentState fullState)
+ public void ApplyToFullState(NavMapState state)
{
- DebugTools.Assert(!FullState);
- var state = (NavMapComponentState) fullState;
- DebugTools.Assert(state.FullState);
-
foreach (var key in state.Chunks.Keys)
{
if (!AllChunks!.Contains(key))
state.Chunks.Remove(key);
}
- foreach (var (index, data) in Chunks)
+ foreach (var (index, data) in ModifiedChunks)
{
if (!state.Chunks.TryGetValue(index, out var stateValue))
state.Chunks[index] = stateValue = new int[data.Length];
}
}
- public IComponentState CreateNewFullState(IComponentState fullState)
+ public NavMapState CreateNewFullState(NavMapState state)
{
- DebugTools.Assert(!FullState);
- var state = (NavMapComponentState) fullState;
- DebugTools.Assert(state.FullState);
-
var chunks = new Dictionary<Vector2i, int[]>(state.Chunks.Count);
foreach (var (index, data) in state.Chunks)
{
var newData = chunks[index] = new int[ArraySize];
- if (Chunks.TryGetValue(index, out var updatedData))
+ if (ModifiedChunks.TryGetValue(index, out var updatedData))
Array.Copy(newData, updatedData, ArraySize);
else
Array.Copy(newData, data, ArraySize);
}
- return new NavMapComponentState(chunks, new(Beacons));
+ return new NavMapState(chunks, new(Beacons));
}
}