Decal system cleanup (#13493)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2023-01-16 13:46:22 +13:00
committed by GitHub
parent 3cfd814503
commit dbe99f9fa6
7 changed files with 141 additions and 283 deletions

View File

@@ -15,15 +15,11 @@ namespace Content.Client.Decals
private DecalOverlay _overlay = default!; private DecalOverlay _overlay = default!;
// TODO move this data to the component
public readonly Dictionary<EntityUid, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
private readonly Dictionary<EntityUid, Dictionary<uint, int>> _decalZIndexIndex = new();
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_overlay = new DecalOverlay(this, _sprites, EntityManager, PrototypeManager); _overlay = new DecalOverlay(_sprites, EntityManager, PrototypeManager);
_overlayManager.AddOverlay(_overlay); _overlayManager.AddOverlay(_overlay);
SubscribeLocalEvent<DecalGridComponent, ComponentHandleState>(OnHandleState); SubscribeLocalEvent<DecalGridComponent, ComponentHandleState>(OnHandleState);
@@ -42,41 +38,25 @@ namespace Content.Client.Decals
} }
} }
protected override void OnCompRemove(EntityUid uid, DecalGridComponent component, ComponentRemove args)
{
DecalRenderIndex.Remove(uid);
_decalZIndexIndex.Remove(uid);
base.OnCompRemove(uid, component, args);
}
protected override void OnCompAdd(EntityUid uid, DecalGridComponent component, ComponentAdd args)
{
DecalRenderIndex[uid] = new();
_decalZIndexIndex[uid] = new();
base.OnCompAdd(uid, component, args);
}
public override void Shutdown() public override void Shutdown()
{ {
base.Shutdown(); base.Shutdown();
_overlayManager.RemoveOverlay(_overlay); _overlayManager.RemoveOverlay(_overlay);
} }
protected override bool RemoveDecalHook(EntityUid gridId, uint uid) protected override void OnDecalRemoved(EntityUid gridId, uint decalId, DecalGridComponent component, Vector2i indices, DecalChunk chunk)
{ {
RemoveDecalFromRenderIndex(gridId, uid); base.OnDecalRemoved(gridId, decalId, component, indices, chunk);
return base.RemoveDecalHook(gridId, uid);
}
private void RemoveDecalFromRenderIndex(EntityUid gridId, uint uid) if (!component.DecalZIndexIndex.Remove(decalId, out var zIndex))
{ return;
var zIndex = _decalZIndexIndex[gridId][uid];
DecalRenderIndex[gridId][zIndex].Remove(uid); if (!component.DecalRenderIndex.TryGetValue(zIndex, out var renderIndex))
if (DecalRenderIndex[gridId][zIndex].Count == 0) return;
DecalRenderIndex[gridId].Remove(zIndex);
_decalZIndexIndex[gridId].Remove(uid); renderIndex.Remove(decalId);
if (renderIndex.Count == 0)
component.DecalRenderIndex.Remove(zIndex);
} }
private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args) private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
@@ -143,14 +123,8 @@ namespace Content.Client.Decals
private void UpdateChunks(EntityUid gridId, DecalGridComponent gridComp, Dictionary<Vector2i, DecalChunk> updatedGridChunks) private void UpdateChunks(EntityUid gridId, DecalGridComponent gridComp, Dictionary<Vector2i, DecalChunk> updatedGridChunks)
{ {
var chunkCollection = gridComp.ChunkCollection.ChunkCollection; var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var renderIndex = gridComp.DecalRenderIndex;
if (!ChunkIndex.TryGetValue(gridId, out var chunkIndex) || var zIndexIndex = gridComp.DecalZIndexIndex;
!DecalRenderIndex.TryGetValue(gridId, out var renderIndex) ||
!_decalZIndexIndex.TryGetValue(gridId, out var zIndexIndex))
{
Logger.Error($"Grid missing from dictionaries while updating decal chunks for grid {ToPrettyString(gridId)}");
return;
}
// Update any existing data / remove decals we didn't receive data for. // Update any existing data / remove decals we didn't receive data for.
foreach (var (indices, newChunkData) in updatedGridChunks) foreach (var (indices, newChunkData) in updatedGridChunks)
@@ -161,8 +135,8 @@ namespace Content.Client.Decals
removedUids.ExceptWith(newChunkData.Decals.Keys); removedUids.ExceptWith(newChunkData.Decals.Keys);
foreach (var removedUid in removedUids) foreach (var removedUid in removedUids)
{ {
RemoveDecalHook(gridId, removedUid); OnDecalRemoved(gridId, removedUid, gridComp, indices, chunk);
chunkIndex.Remove(removedUid); gridComp.DecalIndex.Remove(removedUid);
} }
} }
@@ -175,7 +149,7 @@ namespace Content.Client.Decals
renderIndex.GetOrNew(decal.ZIndex)[uid] = decal; renderIndex.GetOrNew(decal.ZIndex)[uid] = decal;
zIndexIndex[uid] = decal.ZIndex; zIndexIndex[uid] = decal.ZIndex;
chunkIndex[uid] = indices; gridComp.DecalIndex[uid] = indices;
} }
} }
} }
@@ -184,20 +158,14 @@ namespace Content.Client.Decals
{ {
var chunkCollection = gridComp.ChunkCollection.ChunkCollection; var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
if (!ChunkIndex.TryGetValue(gridId, out var chunkIndex))
{
Logger.Error($"Missing grid in ChunkIndex dictionary while removing chunks from grid {ToPrettyString(gridId)}");
return;
}
foreach (var index in chunks) foreach (var index in chunks)
{ {
if (!chunkCollection.TryGetValue(index, out var chunk)) continue; if (!chunkCollection.TryGetValue(index, out var chunk)) continue;
foreach (var uid in chunk.Decals.Keys) foreach (var decalId in chunk.Decals.Keys)
{ {
RemoveDecalHook(gridId, uid); OnDecalRemoved(gridId, decalId, gridComp, index, chunk);
chunkIndex.Remove(uid); gridComp.DecalIndex.Remove(decalId);
} }
chunkCollection.Remove(index); chunkCollection.Remove(index);

View File

@@ -3,13 +3,11 @@ using Robust.Client.GameObjects;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Shared.Enums; using Robust.Shared.Enums;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Client.Decals.Overlays namespace Content.Client.Decals.Overlays
{ {
public sealed class DecalOverlay : Overlay public sealed class DecalOverlay : Overlay
{ {
private readonly DecalSystem _decals;
private readonly SpriteSystem _sprites; private readonly SpriteSystem _sprites;
private readonly IEntityManager _entManager; private readonly IEntityManager _entManager;
private readonly IPrototypeManager _prototypeManager; private readonly IPrototypeManager _prototypeManager;
@@ -19,12 +17,10 @@ namespace Content.Client.Decals.Overlays
private readonly Dictionary<string, (Texture Texture, bool SnapCardinals)> _cachedTextures = new(64); private readonly Dictionary<string, (Texture Texture, bool SnapCardinals)> _cachedTextures = new(64);
public DecalOverlay( public DecalOverlay(
DecalSystem decals,
SpriteSystem sprites, SpriteSystem sprites,
IEntityManager entManager, IEntityManager entManager,
IPrototypeManager prototypeManager) IPrototypeManager prototypeManager)
{ {
_decals = decals;
_sprites = sprites; _sprites = sprites;
_entManager = entManager; _entManager = entManager;
_prototypeManager = prototypeManager; _prototypeManager = prototypeManager;
@@ -37,17 +33,14 @@ namespace Content.Client.Decals.Overlays
var xformQuery = _entManager.GetEntityQuery<TransformComponent>(); var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
var eyeAngle = args.Viewport.Eye?.Rotation ?? Angle.Zero; var eyeAngle = args.Viewport.Eye?.Rotation ?? Angle.Zero;
foreach (var (gridId, zIndexDictionary) in _decals.DecalRenderIndex) foreach (var (decalGrid, xform) in _entManager.EntityQuery<DecalGridComponent, TransformComponent>(true))
{ {
var gridId = decalGrid.Owner;
var zIndexDictionary = decalGrid.DecalRenderIndex;
if (zIndexDictionary.Count == 0) if (zIndexDictionary.Count == 0)
continue; continue;
if (!xformQuery.TryGetComponent(gridId, out var xform))
{
Logger.Error($"Tried to draw decals on a non-existent grid. GridUid: {gridId}");
continue;
}
if (xform.MapID != args.MapId) if (xform.MapID != args.MapId)
continue; continue;
@@ -61,8 +54,7 @@ namespace Content.Client.Decals.Overlays
{ {
if (!_cachedTextures.TryGetValue(decal.Id, out var cache) && _prototypeManager.TryIndex<DecalPrototype>(decal.Id, out var decalProto)) if (!_cachedTextures.TryGetValue(decal.Id, out var cache) && _prototypeManager.TryIndex<DecalPrototype>(decal.Id, out var decalProto))
{ {
var sprite = GetDecalSprite(decal.Id); cache = (_sprites.Frame0(decalProto.Sprite), decalProto.SnapCardinals);
cache = (_sprites.Frame0(sprite), decalProto.SnapCardinals);
_cachedTextures[decal.Id] = cache; _cachedTextures[decal.Id] = cache;
} }
@@ -86,14 +78,5 @@ namespace Content.Client.Decals.Overlays
handle.SetTransform(Matrix3.Identity); handle.SetTransform(Matrix3.Identity);
} }
public SpriteSpecifier GetDecalSprite(string id)
{
if (_prototypeManager.TryIndex<DecalPrototype>(id, out var proto))
return proto.Sprite;
Logger.Error($"Unknown decal prototype: {id}");
return new SpriteSpecifier.Texture(new ResourcePath("/Textures/noSprite.png"));
}
} }
} }

View File

@@ -61,7 +61,7 @@ Possible modes are:\n
return; return;
} }
if (!decalSystem.SetDecalPosition(gridId, uid, gridId, new Vector2(x, y))) if (!decalSystem.SetDecalPosition(gridId, uid, new(gridId, (x, y))))
{ {
shell.WriteError("Failed changing decalposition."); shell.WriteError("Failed changing decalposition.");
} }

View File

@@ -104,46 +104,51 @@ namespace Content.Server.Decals
private void OnGridSplit(ref PostGridSplitEvent ev) private void OnGridSplit(ref PostGridSplitEvent ev)
{ {
if (!TryComp(ev.OldGrid, out DecalGridComponent? oldComp))
return;
if (!TryComp(ev.Grid, out DecalGridComponent? newComp))
return;
// Transfer decals over to the new grid. // Transfer decals over to the new grid.
var enumerator = MapManager.GetGrid(ev.Grid).GetAllTilesEnumerator(); var enumerator = MapManager.GetGrid(ev.Grid).GetAllTilesEnumerator();
var oldChunkCollection = DecalGridChunkCollection(ev.OldGrid);
var chunkCollection = DecalGridChunkCollection(ev.Grid);
if (chunkCollection == null || oldChunkCollection == null) var oldChunkCollection = oldComp.ChunkCollection.ChunkCollection;
return; var chunkCollection = newComp.ChunkCollection.ChunkCollection;
while (enumerator.MoveNext(out var tile)) while (enumerator.MoveNext(out var tile))
{ {
var tilePos = (Vector2) tile.Value.GridIndices; var tilePos = (Vector2) tile.Value.GridIndices;
var chunkIndices = GetChunkIndices(tilePos); var chunkIndices = GetChunkIndices(tilePos);
if (!oldChunkCollection.ChunkCollection.TryGetValue(chunkIndices, out var oldChunk)) continue; if (!oldChunkCollection.TryGetValue(chunkIndices, out var oldChunk))
continue;
var bounds = new Box2(tilePos - 0.01f, tilePos + 1.01f); var bounds = new Box2(tilePos - 0.01f, tilePos + 1.01f);
var toRemove = new RemQueue<uint>(); var toRemove = new RemQueue<uint>();
foreach (var (oldUid, decal) in oldChunk.Decals) foreach (var (oldDecalId, decal) in oldChunk.Decals)
{ {
if (!bounds.Contains(decal.Coordinates)) continue; if (!bounds.Contains(decal.Coordinates))
continue;
var uid = chunkCollection.NextUid++; var newDecalId = newComp.ChunkCollection.NextDecalId++;
var chunk = chunkCollection.ChunkCollection.GetOrNew(chunkIndices); var newChunk = chunkCollection.GetOrNew(chunkIndices);
newChunk.Decals[newDecalId] = decal;
chunk.Decals[uid] = decal; newComp.DecalIndex[newDecalId] = chunkIndices;
ChunkIndex[ev.Grid][uid] = chunkIndices; toRemove.Add(oldDecalId);
DirtyChunk(ev.Grid, chunkIndices, chunk);
toRemove.Add(oldUid);
ChunkIndex[ev.OldGrid].Remove(oldUid);
} }
foreach (var uid in toRemove) foreach (var oldDecalId in toRemove)
{ {
oldChunk.Decals.Remove(uid); oldChunk.Decals.Remove(oldDecalId);
oldComp.DecalIndex.Remove(oldDecalId);
} }
DirtyChunk(ev.Grid, chunkIndices, chunkCollection.GetOrNew(chunkIndices));
if (oldChunk.Decals.Count == 0) if (oldChunk.Decals.Count == 0)
oldChunkCollection.ChunkCollection.Remove(chunkIndices); oldChunkCollection.Remove(chunkIndices);
if (toRemove.List?.Count > 0) if (toRemove.List?.Count > 0)
DirtyChunk(ev.OldGrid, chunkIndices, oldChunk); DirtyChunk(ev.OldGrid, chunkIndices, oldChunk);
@@ -163,32 +168,35 @@ namespace Content.Server.Decals
if (!args.NewTile.IsSpace(_tileDefMan)) if (!args.NewTile.IsSpace(_tileDefMan))
return; return;
var chunkCollection = ChunkCollection(args.Entity); if (!TryComp(args.Entity, out DecalGridComponent? grid))
if (chunkCollection == null)
return; return;
var indices = GetChunkIndices(args.NewTile.GridIndices); var indices = GetChunkIndices(args.NewTile.GridIndices);
var toDelete = new HashSet<uint>(); var toDelete = new HashSet<uint>();
if (chunkCollection.TryGetValue(indices, out var chunk)) if (!grid.ChunkCollection.ChunkCollection.TryGetValue(indices, out var chunk))
return;
foreach (var (uid, decal) in chunk.Decals)
{ {
foreach (var (uid, decal) in chunk.Decals) if (new Vector2((int) Math.Floor(decal.Coordinates.X), (int) Math.Floor(decal.Coordinates.Y)) ==
args.NewTile.GridIndices)
{ {
if (new Vector2((int) Math.Floor(decal.Coordinates.X), (int) Math.Floor(decal.Coordinates.Y)) == toDelete.Add(uid);
args.NewTile.GridIndices)
{
toDelete.Add(uid);
}
} }
} }
if (toDelete.Count == 0) return; if (toDelete.Count == 0)
return;
foreach (var uid in toDelete) foreach (var decalId in toDelete)
{ {
RemoveDecalInternal( args.NewTile.GridUid, uid); grid.DecalIndex.Remove(decalId);
chunk.Decals.Remove(decalId);
} }
DirtyChunk(args.NewTile.GridUid, indices, chunk!); DirtyChunk(args.Entity, indices, chunk);
if (chunk.Decals.Count == 0)
grid.ChunkCollection.ChunkCollection.Remove(indices);
} }
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e) private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
@@ -237,10 +245,9 @@ namespace Content.Server.Decals
return; return;
// remove all decals on the same tile // remove all decals on the same tile
foreach (var (uid, decal) in GetDecalsInRange(gridId.Value, ev.Coordinates.Position)) foreach (var (decalId, _) in GetDecalsInRange(gridId.Value, ev.Coordinates.Position))
{ {
var chunkIndices = GetChunkIndices(decal.Coordinates); RemoveDecal(gridId.Value, decalId);
RemoveDecal(gridId.Value, uid);
} }
} }
@@ -252,19 +259,17 @@ namespace Content.Server.Decals
_dirtyChunks[id].Add(chunkIndices); _dirtyChunks[id].Add(chunkIndices);
} }
public bool TryAddDecal(string id, EntityCoordinates coordinates, [NotNullWhen(true)] out uint? uid, Color? color = null, Angle? rotation = null, int zIndex = 0, bool cleanable = false) public bool TryAddDecal(string id, EntityCoordinates coordinates, out uint decalId, Color? color = null, Angle? rotation = null, int zIndex = 0, bool cleanable = false)
{ {
uid = 0;
rotation ??= Angle.Zero; rotation ??= Angle.Zero;
var decal = new Decal(coordinates.Position, id, color, rotation.Value, zIndex, cleanable); var decal = new Decal(coordinates.Position, id, color, rotation.Value, zIndex, cleanable);
return TryAddDecal(decal, coordinates, out uid); return TryAddDecal(decal, coordinates, out decalId);
} }
public bool TryAddDecal(Decal decal, EntityCoordinates coordinates, [NotNull] out uint? uid) public bool TryAddDecal(Decal decal, EntityCoordinates coordinates, out uint decalId)
{ {
uid = 0; decalId = 0;
if (!PrototypeManager.HasIndex<DecalPrototype>(decal.Id)) if (!PrototypeManager.HasIndex<DecalPrototype>(decal.Id))
return false; return false;
@@ -276,29 +281,29 @@ namespace Content.Server.Decals
if (grid.GetTileRef(coordinates).IsSpace(_tileDefMan)) if (grid.GetTileRef(coordinates).IsSpace(_tileDefMan))
return false; return false;
var chunkCollection = DecalGridChunkCollection(gridId.Value); if (!TryComp(gridId, out DecalGridComponent? comp))
if (chunkCollection == null)
return false; return false;
uid = chunkCollection.NextUid++; decalId = comp.ChunkCollection.NextDecalId++;
var chunkIndices = GetChunkIndices(decal.Coordinates); var chunkIndices = GetChunkIndices(decal.Coordinates);
var chunk = chunkCollection.ChunkCollection.GetOrNew(chunkIndices); var chunk = comp.ChunkCollection.ChunkCollection.GetOrNew(chunkIndices);
chunk.Decals[uid.Value] = decal; chunk.Decals[decalId] = decal;
ChunkIndex[gridId.Value][uid.Value] = chunkIndices; comp.DecalIndex[decalId] = chunkIndices;
DirtyChunk(gridId.Value, chunkIndices, chunk); DirtyChunk(gridId.Value, chunkIndices, chunk);
return true; return true;
} }
public bool RemoveDecal(EntityUid gridId, uint uid) => RemoveDecalInternal(gridId, uid); public bool RemoveDecal(EntityUid gridId, uint decalId, DecalGridComponent? component = null)
=> RemoveDecalInternal(gridId, decalId, out _, component);
public HashSet<(uint Index, Decal Decal)> GetDecalsInRange(EntityUid gridId, Vector2 position, float distance = 0.75f, Func<Decal, bool>? validDelegate = null) public HashSet<(uint Index, Decal Decal)> GetDecalsInRange(EntityUid gridId, Vector2 position, float distance = 0.75f, Func<Decal, bool>? validDelegate = null)
{ {
var uids = new HashSet<(uint, Decal)>(); var decalIds = new HashSet<(uint, Decal)>();
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
var chunkIndices = GetChunkIndices(position); var chunkIndices = GetChunkIndices(position);
if (chunkCollection == null || !chunkCollection.TryGetValue(chunkIndices, out var chunk)) if (chunkCollection == null || !chunkCollection.TryGetValue(chunkIndices, out var chunk))
return uids; return decalIds;
foreach (var (uid, decal) in chunk.Decals) foreach (var (uid, decal) in chunk.Decals)
{ {
@@ -307,150 +312,63 @@ namespace Content.Server.Decals
if (validDelegate == null || validDelegate(decal)) if (validDelegate == null || validDelegate(decal))
{ {
uids.Add((uid, decal)); decalIds.Add((uid, decal));
} }
} }
return uids; return decalIds;
} }
public bool SetDecalPosition(EntityUid gridId, uint uid, EntityCoordinates coordinates) /// <summary>
/// Changes a decals position. Note this will actually result in a new decal being created, possibly on a new grid or chunk.
/// </summary>
/// <remarks>
/// If the new position is invalid, this will result in the decal getting deleted.
/// </remarks>
public bool SetDecalPosition(EntityUid gridId, uint decalId, EntityCoordinates coordinates, DecalGridComponent? comp = null)
{ {
var newGridId = coordinates.GetGridUid(EntityManager); if (!Resolve(gridId, ref comp))
if (newGridId == null)
return false; return false;
return SetDecalPosition(gridId, uid, newGridId.Value, coordinates.Position); if (!RemoveDecalInternal(gridId, decalId, out var removed, comp))
return false;
return TryAddDecal(removed.WithCoordinates(coordinates.Position), coordinates, out _);
} }
public bool SetDecalPosition(EntityUid gridId, uint uid, EntityUid newGridId, Vector2 position) private bool ModifyDecal(EntityUid gridId, uint decalId, Func<Decal, Decal> modifyDecal, DecalGridComponent? comp = null)
{ {
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices)) if (!Resolve(gridId, ref comp))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
if (chunkCollection == null)
return false; return false;
var decal = chunkCollection[indices].Decals[uid]; if (!comp.DecalIndex.TryGetValue(decalId, out var indices))
if (newGridId == gridId)
{
var existingChunk = chunkCollection[indices];
existingChunk.Decals[uid] = decal.WithCoordinates(position);
DirtyChunk(gridId, indices, existingChunk);
return true;
}
RemoveDecalInternal(gridId, uid);
var newChunkCollection = ChunkCollection(newGridId);
if (newChunkCollection == null)
return false; return false;
var chunkIndices = GetChunkIndices(position); var chunk = comp.ChunkCollection.ChunkCollection[indices];
if(!newChunkCollection.ContainsKey(chunkIndices)) var decal = chunk.Decals[decalId];
newChunkCollection[chunkIndices] = new(); chunk.Decals[decalId] = modifyDecal(decal);
var chunk = newChunkCollection[chunkIndices];
chunk.Decals[uid] = decal.WithCoordinates(position);
ChunkIndex[newGridId][uid] = chunkIndices;
DirtyChunk(newGridId, chunkIndices, chunk);
return true;
}
public bool SetDecalColor(EntityUid gridId, uint uid, Color? color)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
if (chunkCollection == null)
return false;
var chunk = chunkCollection[indices];
var decal = chunk.Decals[uid];
chunk.Decals[uid] = decal.WithColor(color);
DirtyChunk(gridId, indices, chunk); DirtyChunk(gridId, indices, chunk);
return true; return true;
} }
public bool SetDecalId(EntityUid gridId, uint uid, string id) public bool SetDecalColor(EntityUid gridId, uint decalId, Color? value, DecalGridComponent? comp = null)
{ => ModifyDecal(gridId, decalId, x => x.WithColor(value), comp);
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
public bool SetDecalRotation(EntityUid gridId, uint decalId, Angle value, DecalGridComponent? comp = null)
=> ModifyDecal(gridId, decalId, x => x.WithRotation(value), comp);
public bool SetDecalZIndex(EntityUid gridId, uint decalId, int value, DecalGridComponent? comp = null)
=> ModifyDecal(gridId, decalId, x => x.WithZIndex(value), comp);
public bool SetDecalCleanable(EntityUid gridId, uint decalId, bool value, DecalGridComponent? comp = null)
=> ModifyDecal(gridId, decalId, x => x.WithCleanable(value), comp);
public bool SetDecalId(EntityUid gridId, uint decalId, string id, DecalGridComponent? comp = null)
{
if (!PrototypeManager.HasIndex<DecalPrototype>(id)) if (!PrototypeManager.HasIndex<DecalPrototype>(id))
throw new ArgumentOutOfRangeException($"Tried to set decal id to invalid prototypeid: {id}"); throw new ArgumentOutOfRangeException($"Tried to set decal id to invalid prototypeid: {id}");
var chunkCollection = ChunkCollection(gridId); return ModifyDecal(gridId, decalId, x => x.WithId(id), comp);
if (chunkCollection == null)
return false;
var chunk = chunkCollection[indices];
var decal = chunk.Decals[uid];
chunk.Decals[uid] = decal.WithId(id);
DirtyChunk(gridId, indices , chunk);
return true;
}
public bool SetDecalRotation(EntityUid gridId, uint uid, Angle angle)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
if (chunkCollection == null)
return false;
var chunk = chunkCollection[indices];
var decal = chunk.Decals[uid];
chunk.Decals[uid] = decal.WithRotation(angle);
DirtyChunk(gridId, indices, chunk);
return true;
}
public bool SetDecalZIndex(EntityUid gridId, uint uid, int zIndex)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
if (chunkCollection == null)
return false;
var chunk = chunkCollection[indices];
var decal = chunk.Decals[uid];
chunk.Decals[uid] = decal.WithZIndex(zIndex);
DirtyChunk(gridId, indices, chunk);
return true;
}
public bool SetDecalCleanable(EntityUid gridId, uint uid, bool cleanable)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
if (chunkCollection == null)
return false;
var chunk = chunkCollection[indices];
var decal = chunk.Decals[uid];
chunk.Decals[uid] = decal.WithCleanable(cleanable);
DirtyChunk(gridId, indices, chunk);
return true;
} }
public override void Update(float frameTime) public override void Update(float frameTime)

View File

@@ -51,7 +51,7 @@ namespace Content.Shared.Decals
newDict[indices].Decals[newUid] = dictionary[indices].Decals[oldUid]; newDict[indices].Decals[newUid] = dictionary[indices].Decals[oldUid];
} }
return new DecalGridComponent.DecalGridChunkCollection(newDict){NextUid = nextIndex}; return new DecalGridComponent.DecalGridChunkCollection(newDict){NextDecalId = nextIndex};
} }
public DataNode Write(ISerializationManager serializationManager, public DataNode Write(ISerializationManager serializationManager,

View File

@@ -15,11 +15,20 @@ namespace Content.Shared.Decals
[DataField("chunkCollection", serverOnly: true)] [DataField("chunkCollection", serverOnly: true)]
public DecalGridChunkCollection ChunkCollection = new(new ()); public DecalGridChunkCollection ChunkCollection = new(new ());
/// <summary>
/// Dictionary mapping decals to their corresponding grid chunks.
/// </summary>
public readonly Dictionary<uint, Vector2i> DecalIndex = new();
/// <summary> /// <summary>
/// Tick at which PVS was last toggled. Ensures that all players receive a full update when toggling PVS. /// Tick at which PVS was last toggled. Ensures that all players receive a full update when toggling PVS.
/// </summary> /// </summary>
public GameTick ForceTick { get; set; } public GameTick ForceTick { get; set; }
// client-side data. I CBF creating a separate client-side comp for this. The server can survive with some empty dictionaries.
public readonly Dictionary<uint, int> DecalZIndexIndex = new();
public readonly SortedDictionary<int, SortedDictionary<uint, Decal>> DecalRenderIndex = new();
[DataDefinition] [DataDefinition]
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class DecalChunk public sealed class DecalChunk
@@ -51,7 +60,7 @@ namespace Content.Shared.Decals
[DataRecord, Serializable, NetSerializable] [DataRecord, Serializable, NetSerializable]
public record DecalGridChunkCollection(Dictionary<Vector2i, DecalChunk> ChunkCollection) public record DecalGridChunkCollection(Dictionary<Vector2i, DecalChunk> ChunkCollection)
{ {
public uint NextUid; public uint NextDecalId;
} }
} }

View File

@@ -11,9 +11,6 @@ namespace Content.Shared.Decals
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!; [Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
[Dependency] protected readonly IMapManager MapManager = default!; [Dependency] protected readonly IMapManager MapManager = default!;
// TODO move this data to the component
protected readonly Dictionary<EntityUid, Dictionary<uint, Vector2i>> ChunkIndex = new();
// Note that this constant is effectively baked into all map files, because of how they save the grid decal component. // Note that this constant is effectively baked into all map files, because of how they save the grid decal component.
// So if this ever needs changing, the maps need converting. // So if this ever needs changing, the maps need converting.
public const int ChunkSize = 32; public const int ChunkSize = 32;
@@ -24,9 +21,7 @@ namespace Content.Shared.Decals
base.Initialize(); base.Initialize();
SubscribeLocalEvent<GridInitializeEvent>(OnGridInitialize); SubscribeLocalEvent<GridInitializeEvent>(OnGridInitialize);
SubscribeLocalEvent<DecalGridComponent, ComponentAdd>(OnCompAdd);
SubscribeLocalEvent<DecalGridComponent, ComponentStartup>(OnCompStartup); SubscribeLocalEvent<DecalGridComponent, ComponentStartup>(OnCompStartup);
SubscribeLocalEvent<DecalGridComponent, ComponentRemove>(OnCompRemove);
} }
private void OnGridInitialize(GridInitializeEvent msg) private void OnGridInitialize(GridInitializeEvent msg)
@@ -34,68 +29,53 @@ namespace Content.Shared.Decals
EnsureComp<DecalGridComponent>(msg.EntityUid); EnsureComp<DecalGridComponent>(msg.EntityUid);
} }
protected virtual void OnCompRemove(EntityUid uid, DecalGridComponent component, ComponentRemove args)
{
ChunkIndex.Remove(uid);
}
protected virtual void OnCompAdd(EntityUid uid, DecalGridComponent component, ComponentAdd args)
{
ChunkIndex[uid] = new();
}
private void OnCompStartup(EntityUid uid, DecalGridComponent component, ComponentStartup args) private void OnCompStartup(EntityUid uid, DecalGridComponent component, ComponentStartup args)
{ {
var index = ChunkIndex[uid];
foreach (var (indices, decals) in component.ChunkCollection.ChunkCollection) foreach (var (indices, decals) in component.ChunkCollection.ChunkCollection)
{ {
foreach (var decalUid in decals.Decals.Keys) foreach (var decalUid in decals.Decals.Keys)
{ {
index[decalUid] = indices; component.DecalIndex[decalUid] = indices;
} }
} }
} }
protected DecalGridChunkCollection? DecalGridChunkCollection(EntityUid gridEuid, DecalGridComponent? comp = null) protected Dictionary<Vector2i, DecalChunk>? ChunkCollection(EntityUid gridEuid, DecalGridComponent? comp = null)
{ {
if (!Resolve(gridEuid, ref comp)) if (!Resolve(gridEuid, ref comp))
return null; return null;
return comp.ChunkCollection; return comp.ChunkCollection.ChunkCollection;
}
protected Dictionary<Vector2i, DecalChunk>? ChunkCollection(EntityUid gridEuid, DecalGridComponent? comp = null)
{
var collection = DecalGridChunkCollection(gridEuid, comp);
return collection?.ChunkCollection;
} }
protected virtual void DirtyChunk(EntityUid id, Vector2i chunkIndices, DecalChunk chunk) {} protected virtual void DirtyChunk(EntityUid id, Vector2i chunkIndices, DecalChunk chunk) {}
protected bool RemoveDecalInternal(EntityUid gridId, uint uid) // internal, so that client/predicted code doesn't accidentally remove decals. There is a public server-side function.
protected bool RemoveDecalInternal(EntityUid gridId, uint decalId, [NotNullWhen(true)] out Decal? removed, DecalGridComponent? component = null)
{ {
if (!RemoveDecalHook(gridId, uid)) return false; removed = null;
if (!Resolve(gridId, ref component))
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false; return false;
}
var chunkCollection = ChunkCollection(gridId); if (!component.DecalIndex.Remove(decalId, out var indices)
if (chunkCollection == null || !chunkCollection.TryGetValue(indices, out var chunk) || !chunk.Decals.Remove(uid)) || !component.ChunkCollection.ChunkCollection.TryGetValue(indices, out var chunk)
|| !chunk.Decals.Remove(decalId, out removed))
{ {
return false; return false;
} }
if (chunk.Decals.Count == 0) if (chunk.Decals.Count == 0)
chunkCollection.Remove(indices); component.ChunkCollection.ChunkCollection.Remove(indices);
ChunkIndex[gridId].Remove(uid);
DirtyChunk(gridId, indices, chunk); DirtyChunk(gridId, indices, chunk);
OnDecalRemoved(gridId, decalId, component, indices, chunk);
return true; return true;
} }
protected virtual bool RemoveDecalHook(EntityUid gridId, uint uid) => true; protected virtual void OnDecalRemoved(EntityUid gridId, uint decalId, DecalGridComponent component, Vector2i indices, DecalChunk chunk)
{
// used by client-side overlay code
}
} }
// TODO: Pretty sure paul was moving this somewhere but just so people know // TODO: Pretty sure paul was moving this somewhere but just so people know