SharedRoofSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Diagnostics.Contracts;
  2. using Content.Shared.Light.Components;
  3. using Content.Shared.Maps;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Map.Components;
  6. namespace Content.Shared.Light.EntitySystems;
  7. /// <summary>
  8. /// Handles the roof flag for tiles that gets used for the RoofOverlay.
  9. /// </summary>
  10. public abstract class SharedRoofSystem : EntitySystem
  11. {
  12. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  13. private HashSet<Entity<IsRoofComponent>> _roofSet = new();
  14. /// <summary>
  15. /// Returns whether the specified tile is roof-occupied.
  16. /// </summary>
  17. /// <returns>Returns false if no data or not rooved.</returns>
  18. [Pure]
  19. public bool IsRooved(Entity<MapGridComponent, RoofComponent> grid, Vector2i index)
  20. {
  21. var roof = grid.Comp2;
  22. var chunkOrigin = SharedMapSystem.GetChunkIndices(index, RoofComponent.ChunkSize);
  23. if (roof.Data.TryGetValue(chunkOrigin, out var bitMask))
  24. {
  25. var chunkRelative = SharedMapSystem.GetChunkRelative(index, RoofComponent.ChunkSize);
  26. var bitFlag = (ulong) 1 << (chunkRelative.X + chunkRelative.Y * RoofComponent.ChunkSize);
  27. var isRoof = (bitMask & bitFlag) == bitFlag;
  28. // Early out, otherwise check for components on tile.
  29. if (isRoof)
  30. return true;
  31. }
  32. _roofSet.Clear();
  33. _lookup.GetLocalEntitiesIntersecting(grid.Owner, index, _roofSet);
  34. foreach (var isRoofEnt in _roofSet)
  35. {
  36. if (!isRoofEnt.Comp.Enabled)
  37. continue;
  38. return true;
  39. }
  40. return false;
  41. }
  42. [Pure]
  43. public Color? GetColor(Entity<MapGridComponent, RoofComponent> grid, Vector2i index)
  44. {
  45. var roof = grid.Comp2;
  46. var chunkOrigin = SharedMapSystem.GetChunkIndices(index, RoofComponent.ChunkSize);
  47. if (roof.Data.TryGetValue(chunkOrigin, out var bitMask))
  48. {
  49. var chunkRelative = SharedMapSystem.GetChunkRelative(index, RoofComponent.ChunkSize);
  50. var bitFlag = (ulong) 1 << (chunkRelative.X + chunkRelative.Y * RoofComponent.ChunkSize);
  51. var isRoof = (bitMask & bitFlag) == bitFlag;
  52. // Early out, otherwise check for components on tile.
  53. if (isRoof)
  54. {
  55. return roof.Color;
  56. }
  57. }
  58. _roofSet.Clear();
  59. _lookup.GetLocalEntitiesIntersecting(grid.Owner, index, _roofSet);
  60. foreach (var isRoofEnt in _roofSet)
  61. {
  62. if (!isRoofEnt.Comp.Enabled)
  63. continue;
  64. return isRoofEnt.Comp.Color ?? roof.Color;
  65. }
  66. return null;
  67. }
  68. public void SetRoof(Entity<MapGridComponent?, RoofComponent?> grid, Vector2i index, bool value)
  69. {
  70. if (!Resolve(grid, ref grid.Comp1, ref grid.Comp2, false))
  71. return;
  72. var chunkOrigin = SharedMapSystem.GetChunkIndices(index, RoofComponent.ChunkSize);
  73. var roof = grid.Comp2;
  74. if (!roof.Data.TryGetValue(chunkOrigin, out var chunkData))
  75. {
  76. // No value to remove so leave it.
  77. if (!value)
  78. {
  79. return;
  80. }
  81. chunkData = 0;
  82. }
  83. var chunkRelative = SharedMapSystem.GetChunkRelative(index, RoofComponent.ChunkSize);
  84. var bitFlag = (ulong) 1 << (chunkRelative.X + chunkRelative.Y * RoofComponent.ChunkSize);
  85. if (value)
  86. {
  87. // Already set
  88. if ((chunkData & bitFlag) == bitFlag)
  89. return;
  90. chunkData |= bitFlag;
  91. }
  92. else
  93. {
  94. // Not already set
  95. if ((chunkData & bitFlag) == 0x0)
  96. return;
  97. chunkData &= ~bitFlag;
  98. }
  99. roof.Data[chunkOrigin] = chunkData;
  100. Dirty(grid.Owner, roof);
  101. }
  102. }