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.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems;
using JetBrains.Annotations;
@@ -36,28 +37,38 @@ namespace Content.Client.Atmos.EntitySystems
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;
}

View File

@@ -56,34 +56,43 @@ namespace Content.Client.Decals
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)

View File

@@ -14,27 +14,40 @@ public sealed partial class NavMapSystem : SharedNavMapSystem
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);
@@ -42,7 +55,7 @@ public sealed partial class NavMapSystem : SharedNavMapSystem
}
component.Beacons.Clear();
foreach (var (nuid, beacon) in state.Beacons)
foreach (var (nuid, beacon) in beacons)
{
component.Beacons[nuid] = beacon;
}

View File

@@ -1,7 +1,6 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Atmos.Components;
@@ -24,55 +23,47 @@ public sealed partial class GasTileOverlayComponent : Component
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]
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 bool FullState => AllChunks == null;
public readonly Dictionary<Vector2i, GasOverlayChunk> ModifiedChunks = modifiedChunks;
public readonly HashSet<Vector2i> AllChunks = allChunks;
// required to infer deleted/missing chunks for delta states
public HashSet<Vector2i>? AllChunks;
public GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks)
public void ApplyToFullState(GasTileOverlayState state)
{
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)
{
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>(AllChunks.Count);
var chunks = new Dictionary<Vector2i, GasOverlayChunk>(state.Chunks.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));
}

View File

@@ -55,7 +55,7 @@ namespace Content.Shared.Atmos.EntitySystems
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)

View File

@@ -62,46 +62,37 @@ namespace Content.Shared.Decals
}
[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;
public Dictionary<Vector2i, DecalChunk> Chunks = chunks;
}
// required to infer deleted/missing chunks for delta states
public HashSet<Vector2i>? AllChunks;
[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 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)
{
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);
}

View File

@@ -49,7 +49,7 @@ namespace Content.Shared.Decals
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)

View File

@@ -96,7 +96,7 @@ public abstract class SharedNavMapSystem : EntitySystem
chunks.Add(origin, chunk.TileData);
}
args.State = new NavMapComponentState(chunks, component.Beacons);
args.State = new NavMapState(chunks, component.Beacons);
return;
}
@@ -109,12 +109,7 @@ public abstract class SharedNavMapSystem : EntitySystem
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
@@ -122,32 +117,35 @@ public abstract class SharedNavMapSystem : EntitySystem
#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;
[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 bool FullState => AllChunks == null;
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];
@@ -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);
foreach (var (index, data) in state.Chunks)
{
@@ -176,13 +170,13 @@ public abstract class SharedNavMapSystem : EntitySystem
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));
}
}