DecalGridComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Robust.Shared.GameStates;
  2. using Robust.Shared.Serialization;
  3. using Robust.Shared.Serialization.TypeSerializers.Implementations.Generic;
  4. using Robust.Shared.Timing;
  5. using Robust.Shared.Utility;
  6. using static Content.Shared.Decals.DecalGridComponent;
  7. namespace Content.Shared.Decals
  8. {
  9. [RegisterComponent]
  10. [Access(typeof(SharedDecalSystem))]
  11. [NetworkedComponent]
  12. public sealed partial class DecalGridComponent : Component
  13. {
  14. [Access(Other = AccessPermissions.ReadExecute)]
  15. [DataField(serverOnly: true)]
  16. public DecalGridChunkCollection ChunkCollection = new(new ());
  17. /// <summary>
  18. /// Dictionary mapping decals to their corresponding grid chunks.
  19. /// </summary>
  20. public readonly Dictionary<uint, Vector2i> DecalIndex = new();
  21. /// <summary>
  22. /// Tick at which PVS was last toggled. Ensures that all players receive a full update when toggling PVS.
  23. /// </summary>
  24. public GameTick ForceTick { get; set; }
  25. [DataDefinition]
  26. [Serializable, NetSerializable]
  27. public sealed partial class DecalChunk
  28. {
  29. [IncludeDataField(customTypeSerializer:typeof(DictionarySerializer<uint, Decal>))]
  30. public Dictionary<uint, Decal> Decals;
  31. [NonSerialized]
  32. public GameTick LastModified;
  33. public DecalChunk()
  34. {
  35. Decals = new();
  36. }
  37. public DecalChunk(Dictionary<uint, Decal> decals)
  38. {
  39. Decals = decals;
  40. }
  41. public DecalChunk(DecalChunk chunk)
  42. {
  43. // decals are readonly, so this should be fine.
  44. Decals = chunk.Decals.ShallowClone();
  45. LastModified = chunk.LastModified;
  46. }
  47. }
  48. [DataRecord, Serializable, NetSerializable]
  49. public partial record DecalGridChunkCollection(Dictionary<Vector2i, DecalChunk> ChunkCollection)
  50. {
  51. public uint NextDecalId;
  52. }
  53. }
  54. [Serializable, NetSerializable]
  55. public sealed class DecalGridState(Dictionary<Vector2i, DecalChunk> chunks) : ComponentState
  56. {
  57. public Dictionary<Vector2i, DecalChunk> Chunks = chunks;
  58. }
  59. [Serializable, NetSerializable]
  60. public sealed class DecalGridDeltaState(Dictionary<Vector2i, DecalChunk> modifiedChunks, HashSet<Vector2i> allChunks)
  61. : ComponentState, IComponentDeltaState<DecalGridState>
  62. {
  63. public Dictionary<Vector2i, DecalChunk> ModifiedChunks = modifiedChunks;
  64. public HashSet<Vector2i> AllChunks = allChunks;
  65. public void ApplyToFullState(DecalGridState state)
  66. {
  67. foreach (var key in state.Chunks.Keys)
  68. {
  69. if (!AllChunks!.Contains(key))
  70. state.Chunks.Remove(key);
  71. }
  72. foreach (var (chunk, data) in ModifiedChunks)
  73. {
  74. state.Chunks[chunk] = new(data);
  75. }
  76. }
  77. public DecalGridState CreateNewFullState(DecalGridState state)
  78. {
  79. var chunks = new Dictionary<Vector2i, DecalChunk>(state.Chunks.Count);
  80. foreach (var (chunk, data) in ModifiedChunks)
  81. {
  82. chunks[chunk] = new(data);
  83. }
  84. foreach (var (chunk, data) in state.Chunks)
  85. {
  86. if (AllChunks!.Contains(chunk))
  87. chunks.TryAdd(chunk, new(data));
  88. }
  89. return new DecalGridState(chunks);
  90. }
  91. }
  92. }