Files
crystall-punk-14/Content.Shared/Decals/DecalGridComponent.cs

118 lines
3.6 KiB
C#
Raw Normal View History

2022-12-19 08:28:46 +13:00
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
2022-12-19 15:18:58 +13:00
using Robust.Shared.Serialization.TypeSerializers.Implementations.Generic;
2022-12-19 08:28:46 +13:00
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using static Content.Shared.Decals.DecalGridComponent;
namespace Content.Shared.Decals
{
[RegisterComponent]
[Access(typeof(SharedDecalSystem))]
2022-12-19 08:28:46 +13:00
[NetworkedComponent]
public sealed partial class DecalGridComponent : Component
{
[Access(Other = AccessPermissions.ReadExecute)]
[DataField(serverOnly: true)]
public DecalGridChunkCollection ChunkCollection = new(new ());
/// <summary>
/// Dictionary mapping decals to their corresponding grid chunks.
/// </summary>
public readonly Dictionary<uint, Vector2i> DecalIndex = new();
2022-12-19 08:28:46 +13:00
/// <summary>
/// Tick at which PVS was last toggled. Ensures that all players receive a full update when toggling PVS.
/// </summary>
public GameTick ForceTick { get; set; }
[DataDefinition]
[Serializable, NetSerializable]
public sealed partial class DecalChunk
2022-12-19 08:28:46 +13:00
{
2022-12-19 15:18:58 +13:00
[IncludeDataField(customTypeSerializer:typeof(DictionarySerializer<uint, Decal>))]
2022-12-19 08:28:46 +13:00
public Dictionary<uint, Decal> Decals;
[NonSerialized]
public GameTick LastModified;
public DecalChunk()
{
Decals = new();
}
public DecalChunk(Dictionary<uint, Decal> decals)
{
Decals = decals;
}
public DecalChunk(DecalChunk chunk)
{
// decals are readonly, so this should be fine.
Decals = chunk.Decals.ShallowClone();
LastModified = chunk.LastModified;
}
}
[DataRecord, Serializable, NetSerializable]
public record DecalGridChunkCollection(Dictionary<Vector2i, DecalChunk> ChunkCollection)
{
public uint NextDecalId;
}
}
2022-12-19 08:28:46 +13:00
[Serializable, NetSerializable]
public sealed class DecalGridState : ComponentState, IComponentDeltaState
{
public Dictionary<Vector2i, DecalChunk> Chunks;
public bool FullState => AllChunks == null;
// required to infer deleted/missing chunks for delta states
public HashSet<Vector2i>? AllChunks;
public DecalGridState(Dictionary<Vector2i, DecalChunk> chunks)
{
Chunks = chunks;
}
public void ApplyToFullState(IComponentState fullState)
2022-12-19 08:28:46 +13:00
{
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)
{
state.Chunks[chunk] = new(data);
}
}
public IComponentState CreateNewFullState(IComponentState fullState)
2022-12-19 08:28:46 +13:00
{
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)
{
chunks[chunk] = new(data);
}
foreach (var (chunk, data) in state.Chunks)
{
if (AllChunks!.Contains(chunk))
chunks.TryAdd(chunk, new(data));
}
return new DecalGridState(chunks);
}
}
}