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

@@ -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));
}
}