Content changes for engine delta-state PR (#28134)

* Update GasTileOverlayState

* Update DecalGridState

* Update NavMapState

* poke

* poke2

* poke3

* Poke dem tests
This commit is contained in:
Leon Friedrich
2024-05-24 16:08:23 +12:00
committed by GitHub
parent 8a95cb186c
commit 5d50467466
8 changed files with 129 additions and 120 deletions

View File

@@ -1,4 +1,5 @@
using Content.Client.Atmos.Overlays; using Content.Client.Atmos.Overlays;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components; using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems; using Content.Shared.Atmos.EntitySystems;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -36,28 +37,38 @@ namespace Content.Client.Atmos.EntitySystems
private void OnHandleState(EntityUid gridUid, GasTileOverlayComponent comp, ref ComponentHandleState args) private void OnHandleState(EntityUid gridUid, GasTileOverlayComponent comp, ref ComponentHandleState args)
{ {
if (args.Current is not GasTileOverlayState state) Dictionary<Vector2i, GasOverlayChunk> modifiedChunks;
return;
// is this a delta or full state? switch (args.Current)
if (!state.FullState)
{ {
foreach (var index in comp.Chunks.Keys) // is this a delta or full state?
case GasTileOverlayDeltaState delta:
{ {
if (!state.AllChunks!.Contains(index)) modifiedChunks = delta.ModifiedChunks;
comp.Chunks.Remove(index); foreach (var index in comp.Chunks.Keys)
{
if (!delta.AllChunks.Contains(index))
comp.Chunks.Remove(index);
}
break;
} }
} case GasTileOverlayState state:
else
{
foreach (var index in comp.Chunks.Keys)
{ {
if (!state.Chunks.ContainsKey(index)) modifiedChunks = state.Chunks;
comp.Chunks.Remove(index); 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; comp.Chunks[index] = data;
} }

View File

@@ -56,34 +56,43 @@ namespace Content.Client.Decals
private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args) 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? // is this a delta or full state?
_removedChunks.Clear(); _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)) modifiedChunks = delta.ModifiedChunks;
_removedChunks.Add(key); foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
{
if (!delta.AllChunks.Contains(key))
_removedChunks.Add(key);
}
break;
} }
} case DecalGridState state:
else
{
foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
{ {
if (!state.Chunks.ContainsKey(key)) modifiedChunks = state.Chunks;
_removedChunks.Add(key); foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
{
if (!state.Chunks.ContainsKey(key))
_removedChunks.Add(key);
}
break;
} }
default:
return;
} }
if (_removedChunks.Count > 0) if (_removedChunks.Count > 0)
RemoveChunks(gridUid, gridComp, _removedChunks); RemoveChunks(gridUid, gridComp, _removedChunks);
if (state.Chunks.Count > 0) if (modifiedChunks.Count > 0)
UpdateChunks(gridUid, gridComp, state.Chunks); UpdateChunks(gridUid, gridComp, modifiedChunks);
} }
private void OnChunkUpdate(DecalChunkUpdateEvent ev) private void OnChunkUpdate(DecalChunkUpdateEvent ev)

View File

@@ -14,27 +14,40 @@ public sealed partial class NavMapSystem : SharedNavMapSystem
private void OnHandleState(EntityUid uid, NavMapComponent component, ref ComponentHandleState args) private void OnHandleState(EntityUid uid, NavMapComponent component, ref ComponentHandleState args)
{ {
if (args.Current is not NavMapComponentState state) Dictionary<Vector2i, int[]> modifiedChunks;
return; 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)) modifiedChunks = delta.ModifiedChunks;
component.Chunks.Remove(index); beacons = delta.Beacons;
foreach (var index in component.Chunks.Keys)
{
if (!delta.AllChunks!.Contains(index))
component.Chunks.Remove(index);
}
break;
} }
} case NavMapState state:
else
{
foreach (var index in component.Chunks.Keys)
{ {
if (!state.Chunks.ContainsKey(index)) modifiedChunks = state.Chunks;
component.Chunks.Remove(index); 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); var newChunk = new NavMapChunk(origin);
Array.Copy(chunk, newChunk.TileData, chunk.Length); Array.Copy(chunk, newChunk.TileData, chunk.Length);
@@ -42,7 +55,7 @@ public sealed partial class NavMapSystem : SharedNavMapSystem
} }
component.Beacons.Clear(); component.Beacons.Clear();
foreach (var (nuid, beacon) in state.Beacons) foreach (var (nuid, beacon) in beacons)
{ {
component.Beacons[nuid] = beacon; component.Beacons[nuid] = beacon;
} }

View File

@@ -1,7 +1,6 @@
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Atmos.Components; namespace Content.Shared.Atmos.Components;
@@ -24,55 +23,47 @@ public sealed partial class GasTileOverlayComponent : Component
public GameTick ForceTick { get; set; } public GameTick ForceTick { get; set; }
} }
[Serializable, NetSerializable]
public sealed class GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks) : ComponentState
{
public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks = chunks;
}
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class GasTileOverlayState : ComponentState, IComponentDeltaState public sealed class GasTileOverlayDeltaState(
Dictionary<Vector2i, GasOverlayChunk> modifiedChunks,
HashSet<Vector2i> allChunks)
: ComponentState, IComponentDeltaState<GasTileOverlayState>
{ {
public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks; public readonly Dictionary<Vector2i, GasOverlayChunk> ModifiedChunks = modifiedChunks;
public bool FullState => AllChunks == null; public readonly HashSet<Vector2i> AllChunks = allChunks;
// required to infer deleted/missing chunks for delta states public void ApplyToFullState(GasTileOverlayState state)
public HashSet<Vector2i>? AllChunks;
public GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks)
{ {
Chunks = chunks;
}
public void ApplyToFullState(IComponentState fullState)
{
DebugTools.Assert(!FullState);
var state = (GasTileOverlayState) fullState;
DebugTools.Assert(state.FullState);
foreach (var key in state.Chunks.Keys) foreach (var key in state.Chunks.Keys)
{ {
if (!AllChunks!.Contains(key)) if (!AllChunks.Contains(key))
state.Chunks.Remove(key); state.Chunks.Remove(key);
} }
foreach (var (chunk, data) in Chunks) foreach (var (chunk, data) in ModifiedChunks)
{ {
state.Chunks[chunk] = new(data); state.Chunks[chunk] = new(data);
} }
} }
public IComponentState CreateNewFullState(IComponentState fullState) public GasTileOverlayState CreateNewFullState(GasTileOverlayState state)
{ {
DebugTools.Assert(!FullState); var chunks = new Dictionary<Vector2i, GasOverlayChunk>(AllChunks.Count);
var state = (GasTileOverlayState) fullState;
DebugTools.Assert(state.FullState);
var chunks = new Dictionary<Vector2i, GasOverlayChunk>(state.Chunks.Count); foreach (var (chunk, data) in ModifiedChunks)
foreach (var (chunk, data) in Chunks)
{ {
chunks[chunk] = new(data); chunks[chunk] = new(data);
} }
foreach (var (chunk, data) in state.Chunks) foreach (var (chunk, data) in state.Chunks)
{ {
if (AllChunks!.Contains(chunk)) if (AllChunks.Contains(chunk))
chunks.TryAdd(chunk, new(data)); chunks.TryAdd(chunk, new(data));
} }

View File

@@ -55,7 +55,7 @@ namespace Content.Shared.Atmos.EntitySystems
data[index] = chunk; 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) public static Vector2i GetGasChunkIndices(Vector2i indices)

View File

@@ -62,46 +62,37 @@ namespace Content.Shared.Decals
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class DecalGridState : ComponentState, IComponentDeltaState public sealed class DecalGridState(Dictionary<Vector2i, DecalChunk> chunks) : ComponentState
{ {
public Dictionary<Vector2i, DecalChunk> Chunks; public Dictionary<Vector2i, DecalChunk> Chunks = chunks;
public bool FullState => AllChunks == null; }
// required to infer deleted/missing chunks for delta states [Serializable, NetSerializable]
public HashSet<Vector2i>? AllChunks; 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 DecalGridState(Dictionary<Vector2i, DecalChunk> chunks) public void ApplyToFullState(DecalGridState state)
{ {
Chunks = chunks;
}
public void ApplyToFullState(IComponentState fullState)
{
DebugTools.Assert(!FullState);
var state = (DecalGridState) fullState;
DebugTools.Assert(state.FullState);
foreach (var key in state.Chunks.Keys) foreach (var key in state.Chunks.Keys)
{ {
if (!AllChunks!.Contains(key)) if (!AllChunks!.Contains(key))
state.Chunks.Remove(key); state.Chunks.Remove(key);
} }
foreach (var (chunk, data) in Chunks) foreach (var (chunk, data) in ModifiedChunks)
{ {
state.Chunks[chunk] = new(data); 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); 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); chunks[chunk] = new(data);
} }

View File

@@ -49,7 +49,7 @@ namespace Content.Shared.Decals
data[index] = chunk; 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) private void OnGridInitialize(GridInitializeEvent msg)

View File

@@ -96,7 +96,7 @@ public abstract class SharedNavMapSystem : EntitySystem
chunks.Add(origin, chunk.TileData); chunks.Add(origin, chunk.TileData);
} }
args.State = new NavMapComponentState(chunks, component.Beacons); args.State = new NavMapState(chunks, component.Beacons);
return; return;
} }
@@ -109,12 +109,7 @@ public abstract class SharedNavMapSystem : EntitySystem
chunks.Add(origin, chunk.TileData); chunks.Add(origin, chunk.TileData);
} }
args.State = new NavMapComponentState(chunks, component.Beacons) args.State = new NavMapDeltaState(chunks, component.Beacons, new(component.Chunks.Keys));
{
// 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),
};
} }
#endregion #endregion
@@ -122,32 +117,35 @@ public abstract class SharedNavMapSystem : EntitySystem
#region: System messages #region: System messages
[Serializable, NetSerializable] [Serializable, NetSerializable]
protected sealed class NavMapComponentState( protected sealed class NavMapState(
Dictionary<Vector2i, int[]> chunks, Dictionary<Vector2i, int[]> chunks,
Dictionary<NetEntity, NavMapBeacon> beacons) Dictionary<NetEntity, NavMapBeacon> beacons)
: ComponentState, IComponentDeltaState : ComponentState
{ {
public Dictionary<Vector2i, int[]> Chunks = chunks; public Dictionary<Vector2i, int[]> Chunks = chunks;
public Dictionary<NetEntity, NavMapBeacon> Beacons = beacons; public Dictionary<NetEntity, NavMapBeacon> Beacons = beacons;
}
// Required to infer deleted/missing chunks for delta states [Serializable, NetSerializable]
public HashSet<Vector2i>? AllChunks; 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 bool FullState => AllChunks == null; public void ApplyToFullState(NavMapState state)
public void ApplyToFullState(IComponentState fullState)
{ {
DebugTools.Assert(!FullState);
var state = (NavMapComponentState) fullState;
DebugTools.Assert(state.FullState);
foreach (var key in state.Chunks.Keys) foreach (var key in state.Chunks.Keys)
{ {
if (!AllChunks!.Contains(key)) if (!AllChunks!.Contains(key))
state.Chunks.Remove(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)) if (!state.Chunks.TryGetValue(index, out var stateValue))
state.Chunks[index] = stateValue = new int[data.Length]; state.Chunks[index] = stateValue = new int[data.Length];
@@ -162,12 +160,8 @@ public abstract class SharedNavMapSystem : EntitySystem
} }
} }
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); var chunks = new Dictionary<Vector2i, int[]>(state.Chunks.Count);
foreach (var (index, data) in state.Chunks) foreach (var (index, data) in state.Chunks)
{ {
@@ -176,13 +170,13 @@ public abstract class SharedNavMapSystem : EntitySystem
var newData = chunks[index] = new int[ArraySize]; 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); Array.Copy(newData, updatedData, ArraySize);
else else
Array.Copy(newData, data, ArraySize); Array.Copy(newData, data, ArraySize);
} }
return new NavMapComponentState(chunks, new(Beacons)); return new NavMapState(chunks, new(Beacons));
} }
} }