using System.Diagnostics.Contracts;
using Content.Shared.Light.Components;
using Content.Shared.Maps;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
namespace Content.Shared.Light.EntitySystems;
/// <summary>
/// Handles the roof flag for tiles that gets used for the RoofOverlay.
/// </summary>
public abstract class SharedRoofSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
private HashSet<Entity<IsRoofComponent>> _roofSet = new();
/// Returns whether the specified tile is roof-occupied.
/// <returns>Returns false if no data or not rooved.</returns>
[Pure]
public bool IsRooved(Entity<MapGridComponent, RoofComponent> grid, Vector2i index)
var roof = grid.Comp2;
var chunkOrigin = SharedMapSystem.GetChunkIndices(index, RoofComponent.ChunkSize);
if (roof.Data.TryGetValue(chunkOrigin, out var bitMask))
var chunkRelative = SharedMapSystem.GetChunkRelative(index, RoofComponent.ChunkSize);
var bitFlag = (ulong) 1 << (chunkRelative.X + chunkRelative.Y * RoofComponent.ChunkSize);
var isRoof = (bitMask & bitFlag) == bitFlag;
// Early out, otherwise check for components on tile.
if (isRoof)
return true;
}
_roofSet.Clear();
_lookup.GetLocalEntitiesIntersecting(grid.Owner, index, _roofSet);
foreach (var isRoofEnt in _roofSet)
if (!isRoofEnt.Comp.Enabled)
continue;
return false;
public Color? GetColor(Entity<MapGridComponent, RoofComponent> grid, Vector2i index)
return roof.Color;
return isRoofEnt.Comp.Color ?? roof.Color;
return null;
public void SetRoof(Entity<MapGridComponent?, RoofComponent?> grid, Vector2i index, bool value)
if (!Resolve(grid, ref grid.Comp1, ref grid.Comp2, false))
return;
if (!roof.Data.TryGetValue(chunkOrigin, out var chunkData))
// No value to remove so leave it.
if (!value)
chunkData = 0;
if (value)
// Already set
if ((chunkData & bitFlag) == bitFlag)
chunkData |= bitFlag;
else
// Not already set
if ((chunkData & bitFlag) == 0x0)
chunkData &= ~bitFlag;
roof.Data[chunkOrigin] = chunkData;
Dirty(grid.Owner, roof);