using Robust.Shared.GameStates; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.Timing; namespace Content.Shared.Maps; [RegisterComponent, NetworkedComponent] public sealed partial class TileHistoryComponent : Component { // History of tiles for each grid chunk. [DataField] public Dictionary ChunkHistory = new(); /// /// Tick at which PVS was last toggled. Ensures that all players receive a full update when toggling PVS. /// public GameTick ForceTick { get; set; } } [Serializable, NetSerializable] public sealed class TileHistoryState : ComponentState { public Dictionary ChunkHistory; public TileHistoryState(Dictionary chunkHistory) { ChunkHistory = chunkHistory; } } [Serializable, NetSerializable] public sealed class TileHistoryDeltaState : ComponentState, IComponentDeltaState { public Dictionary ChunkHistory; public HashSet AllHistoryChunks; public TileHistoryDeltaState(Dictionary chunkHistory, HashSet allHistoryChunks) { ChunkHistory = chunkHistory; AllHistoryChunks = allHistoryChunks; } public void ApplyToFullState(TileHistoryState state) { var toRemove = new List(); foreach (var key in state.ChunkHistory.Keys) { if (!AllHistoryChunks.Contains(key)) toRemove.Add(key); } foreach (var key in toRemove) { state.ChunkHistory.Remove(key); } foreach (var (indices, chunk) in ChunkHistory) { state.ChunkHistory[indices] = new TileHistoryChunk(chunk); } } public void ApplyToComponent(TileHistoryComponent component) { var toRemove = new List(); foreach (var key in component.ChunkHistory.Keys) { if (!AllHistoryChunks.Contains(key)) toRemove.Add(key); } foreach (var key in toRemove) { component.ChunkHistory.Remove(key); } foreach (var (indices, chunk) in ChunkHistory) { component.ChunkHistory[indices] = new TileHistoryChunk(chunk); } } public TileHistoryState CreateNewFullState(TileHistoryState state) { var chunks = new Dictionary(state.ChunkHistory.Count); foreach (var (indices, chunk) in ChunkHistory) { chunks[indices] = new TileHistoryChunk(chunk); } foreach (var (indices, chunk) in state.ChunkHistory) { if (AllHistoryChunks.Contains(indices)) chunks.TryAdd(indices, new TileHistoryChunk(chunk)); } return new TileHistoryState(chunks); } } [DataDefinition, Serializable, NetSerializable] public sealed partial class TileHistoryChunk { [DataField] public Dictionary>> History = new(); [ViewVariables] public GameTick LastModified; public TileHistoryChunk() { } public TileHistoryChunk(TileHistoryChunk other) { History = new Dictionary>>(other.History.Count); foreach (var (key, value) in other.History) { History[key] = new List>(value); } LastModified = other.LastModified; } }