Fix disappearing decals bug (#9608)

This commit is contained in:
Leon Friedrich
2022-07-15 14:33:11 +12:00
committed by GitHub
parent 21fca0f6e3
commit 72599e5282
6 changed files with 204 additions and 90 deletions

View File

@@ -0,0 +1,58 @@
using JetBrains.Annotations;
using Robust.Client.Graphics;
using Robust.Shared.Console;
namespace Content.Client.Commands;
[UsedImplicitly]
public sealed class ZoomCommand : IConsoleCommand
{
[Dependency] private readonly IEyeManager _eyeMan = default!;
public string Command => "zoom";
public string Description => Loc.GetString("zoom-command-description");
public string Help => Loc.GetString("zoom-command-help");
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
Vector2 zoom;
if (args.Length is not (1 or 2))
{
shell.WriteLine(Help);
return;
}
if (!float.TryParse(args[0], out var arg0))
{
shell.WriteError(Loc.GetString("cmd-parse-failure-float", ("arg", args[0])));
return;
}
if (arg0 > 0)
zoom = new(arg0, arg0);
else
{
shell.WriteError(Loc.GetString("zoom-command-error"));
return;
}
if (args.Length == 2)
{
if (!float.TryParse(args[1], out var arg1))
{
shell.WriteError(Loc.GetString("cmd-parse-failure-float", ("arg", args[1])));
return;
}
if (arg1 > 0)
zoom.Y = arg1;
else
{
shell.WriteError(Loc.GetString("zoom-command-error"));
return;
}
}
_eyeMan.CurrentEye.Zoom = zoom;
}
}

View File

@@ -1,6 +1,7 @@
using Content.Shared.Decals; using Content.Shared.Decals;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Shared.Utility;
namespace Content.Client.Decals namespace Content.Client.Decals
{ {
@@ -12,7 +13,7 @@ namespace Content.Client.Decals
private DecalOverlay _overlay = default!; private DecalOverlay _overlay = default!;
public Dictionary<EntityUid, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new(); public Dictionary<EntityUid, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
private Dictionary<EntityUid, Dictionary<uint, int>> DecalZIndexIndex = new(); private Dictionary<EntityUid, Dictionary<uint, int>> _decalZIndexIndex = new();
public override void Initialize() public override void Initialize()
{ {
@@ -41,13 +42,13 @@ namespace Content.Client.Decals
private void OnGridRemoval(GridRemovalEvent ev) private void OnGridRemoval(GridRemovalEvent ev)
{ {
DecalRenderIndex.Remove(ev.EntityUid); DecalRenderIndex.Remove(ev.EntityUid);
DecalZIndexIndex.Remove(ev.EntityUid); _decalZIndexIndex.Remove(ev.EntityUid);
} }
private void OnGridInitialize(GridInitializeEvent ev) private void OnGridInitialize(GridInitializeEvent ev)
{ {
DecalRenderIndex[ev.EntityUid] = new(); DecalRenderIndex[ev.EntityUid] = new();
DecalZIndexIndex[ev.EntityUid] = new(); _decalZIndexIndex[ev.EntityUid] = new();
} }
public override void Shutdown() public override void Shutdown()
@@ -64,25 +65,34 @@ namespace Content.Client.Decals
private void RemoveDecalFromRenderIndex(EntityUid gridId, uint uid) private void RemoveDecalFromRenderIndex(EntityUid gridId, uint uid)
{ {
var zIndex = DecalZIndexIndex[gridId][uid]; var zIndex = _decalZIndexIndex[gridId][uid];
DecalRenderIndex[gridId][zIndex].Remove(uid); DecalRenderIndex[gridId][zIndex].Remove(uid);
if (DecalRenderIndex[gridId][zIndex].Count == 0) if (DecalRenderIndex[gridId][zIndex].Count == 0)
DecalRenderIndex[gridId].Remove(zIndex); DecalRenderIndex[gridId].Remove(zIndex);
DecalZIndexIndex[gridId].Remove(uid); _decalZIndexIndex[gridId].Remove(uid);
} }
private void OnChunkUpdate(DecalChunkUpdateEvent ev) private void OnChunkUpdate(DecalChunkUpdateEvent ev)
{ {
foreach (var (gridId, gridChunks) in ev.Data) foreach (var (gridId, updatedGridChunks) in ev.Data)
{ {
if (gridChunks.Count == 0) continue; if (updatedGridChunks.Count == 0) continue;
var chunkCollection = ChunkCollection(gridId); if (!TryComp(gridId, out DecalGridComponent? gridComp))
{
Logger.Error($"Received decal information for an entity without a decal component: {ToPrettyString(gridId)}");
continue;
}
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var chunkIndex = ChunkIndex[gridId];
var renderIndex = DecalRenderIndex[gridId];
var zIndexIndex = _decalZIndexIndex[gridId];
// 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 gridChunks) foreach (var (indices, newChunkData) in updatedGridChunks)
{ {
if (chunkCollection.TryGetValue(indices, out var chunk)) if (chunkCollection.TryGetValue(indices, out var chunk))
{ {
@@ -90,30 +100,21 @@ namespace Content.Client.Decals
removedUids.ExceptWith(newChunkData.Keys); removedUids.ExceptWith(newChunkData.Keys);
foreach (var removedUid in removedUids) foreach (var removedUid in removedUids)
{ {
RemoveDecalInternal(gridId, removedUid); RemoveDecalHook(gridId, removedUid);
chunkIndex.Remove(removedUid);
}
} }
chunkCollection[indices] = newChunkData; chunkCollection[indices] = newChunkData;
}
else
{
chunkCollection.Add(indices, newChunkData);
}
foreach (var (uid, decal) in newChunkData) foreach (var (uid, decal) in newChunkData)
{ {
if (!DecalRenderIndex[gridId].ContainsKey(decal.ZIndex)) if (zIndexIndex.TryGetValue(uid, out var zIndex))
DecalRenderIndex[gridId][decal.ZIndex] = new(); renderIndex[zIndex].Remove(uid);
if (DecalZIndexIndex.TryGetValue(gridId, out var values) && renderIndex.GetOrNew(decal.ZIndex)[uid] = decal;
values.TryGetValue(uid, out var zIndex)) zIndexIndex[uid] = decal.ZIndex;
{ chunkIndex[uid] = indices;
DecalRenderIndex[gridId][zIndex].Remove(uid);
}
DecalRenderIndex[gridId][decal.ZIndex][uid] = decal;
DecalZIndexIndex[gridId][uid] = decal.ZIndex;
ChunkIndex[gridId][uid] = indices;
} }
} }
} }
@@ -123,7 +124,14 @@ namespace Content.Client.Decals
{ {
if (chunks.Count == 0) continue; if (chunks.Count == 0) continue;
var chunkCollection = ChunkCollection(gridId); if (!TryComp(gridId, out DecalGridComponent? gridComp))
{
Logger.Error($"Received decal information for an entity without a decal component: {ToPrettyString(gridId)}");
continue;
}
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var chunkIndex = ChunkIndex[gridId];
foreach (var index in chunks) foreach (var index in chunks)
{ {
@@ -131,7 +139,8 @@ namespace Content.Client.Decals
foreach (var (uid, _) in chunk) foreach (var (uid, _) in chunk)
{ {
RemoveDecalInternal(gridId, uid); RemoveDecalHook(gridId, uid);
chunkIndex.Remove(uid);
} }
chunkCollection.Remove(index); chunkCollection.Remove(index);

View File

@@ -53,6 +53,9 @@ namespace Content.Server.Decals
var oldChunkCollection = DecalGridChunkCollection(ev.OldGrid); var oldChunkCollection = DecalGridChunkCollection(ev.OldGrid);
var chunkCollection = DecalGridChunkCollection(ev.Grid); var chunkCollection = DecalGridChunkCollection(ev.Grid);
if (chunkCollection == null || oldChunkCollection == null)
return;
while (enumerator.MoveNext(out var tile)) while (enumerator.MoveNext(out var tile))
{ {
var tilePos = (Vector2) tile.Value.GridIndices; var tilePos = (Vector2) tile.Value.GridIndices;
@@ -104,6 +107,9 @@ namespace Content.Server.Decals
return; return;
var chunkCollection = ChunkCollection(args.Entity); var chunkCollection = ChunkCollection(args.Entity);
if (chunkCollection == null)
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 (chunkCollection.TryGetValue(indices, out var chunk))
@@ -213,6 +219,9 @@ namespace Content.Server.Decals
return false; return false;
var chunkCollection = DecalGridChunkCollection(gridId.Value); var chunkCollection = DecalGridChunkCollection(gridId.Value);
if (chunkCollection == null)
return false;
uid = chunkCollection.NextUid++; uid = chunkCollection.NextUid++;
var chunkIndices = GetChunkIndices(decal.Coordinates); var chunkIndices = GetChunkIndices(decal.Coordinates);
if(!chunkCollection.ChunkCollection.ContainsKey(chunkIndices)) if(!chunkCollection.ChunkCollection.ContainsKey(chunkIndices))
@@ -231,7 +240,7 @@ namespace Content.Server.Decals
var uids = new HashSet<(uint, Decal)>(); var uids = new HashSet<(uint, Decal)>();
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
var chunkIndices = GetChunkIndices(position); var chunkIndices = GetChunkIndices(position);
if (!chunkCollection.TryGetValue(chunkIndices, out var chunk)) if (chunkCollection == null || !chunkCollection.TryGetValue(chunkIndices, out var chunk))
return uids; return uids;
foreach (var (uid, decal) in chunk) foreach (var (uid, decal) in chunk)
@@ -266,6 +275,9 @@ namespace Content.Server.Decals
DirtyChunk(gridId, indices); DirtyChunk(gridId, indices);
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
if (chunkCollection == null)
return false;
var decal = chunkCollection[indices][uid]; var decal = chunkCollection[indices][uid];
if (newGridId == gridId) if (newGridId == gridId)
{ {
@@ -276,6 +288,9 @@ namespace Content.Server.Decals
RemoveDecalInternal(gridId, uid); RemoveDecalInternal(gridId, uid);
var newChunkCollection = ChunkCollection(newGridId); var newChunkCollection = ChunkCollection(newGridId);
if (newChunkCollection == null)
return false;
var chunkIndices = GetChunkIndices(position); var chunkIndices = GetChunkIndices(position);
if(!newChunkCollection.ContainsKey(chunkIndices)) if(!newChunkCollection.ContainsKey(chunkIndices))
newChunkCollection[chunkIndices] = new(); newChunkCollection[chunkIndices] = new();
@@ -293,8 +308,12 @@ namespace Content.Server.Decals
} }
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid]; if (chunkCollection == null)
chunkCollection[indices][uid] = decal.WithColor(color); return false;
var chunk = chunkCollection[indices];
var decal = chunk[uid];
chunk[uid] = decal.WithColor(color);
DirtyChunk(gridId, indices); DirtyChunk(gridId, indices);
return true; return true;
} }
@@ -310,8 +329,12 @@ namespace Content.Server.Decals
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); var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid]; if (chunkCollection == null)
chunkCollection[indices][uid] = decal.WithId(id); return false;
var chunk = chunkCollection[indices];
var decal = chunk[uid];
chunk[uid] = decal.WithId(id);
DirtyChunk(gridId, indices); DirtyChunk(gridId, indices);
return true; return true;
} }
@@ -324,8 +347,12 @@ namespace Content.Server.Decals
} }
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid]; if (chunkCollection == null)
chunkCollection[indices][uid] = decal.WithRotation(angle); return false;
var chunk = chunkCollection[indices];
var decal = chunk[uid];
chunk[uid] = decal.WithRotation(angle);
DirtyChunk(gridId, indices); DirtyChunk(gridId, indices);
return true; return true;
} }
@@ -338,8 +365,12 @@ namespace Content.Server.Decals
} }
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid]; if (chunkCollection == null)
chunkCollection[indices][uid] = decal.WithZIndex(zIndex); return false;
var chunk = chunkCollection[indices];
var decal = chunk[uid];
chunk[uid] = decal.WithZIndex(zIndex);
DirtyChunk(gridId, indices); DirtyChunk(gridId, indices);
return true; return true;
} }
@@ -352,8 +383,12 @@ namespace Content.Server.Decals
} }
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid]; if (chunkCollection == null)
chunkCollection[indices][uid] = decal.WithCleanable(cleanable); return false;
var chunk = chunkCollection[indices];
var decal = chunk[uid];
chunk[uid] = decal.WithCleanable(cleanable);
DirtyChunk(gridId, indices); DirtyChunk(gridId, indices);
return true; return true;
} }
@@ -368,24 +403,29 @@ namespace Content.Server.Decals
continue; continue;
var chunksInRange = GetChunksForSession(playerSession); var chunksInRange = GetChunksForSession(playerSession);
var staleChunks = new Dictionary<EntityUid, HashSet<Vector2i>>(); var staleChunks = _chunkViewerPool.Get();
var previouslySent = _previousSentChunks[playerSession];
// Get any chunks not in range anymore // Get any chunks not in range anymore
// Then, remove them from previousSentChunks (for stuff like grids out of range) // Then, remove them from previousSentChunks (for stuff like grids out of range)
// and also mark them as stale for networking. // and also mark them as stale for networking.
var toRemoveGrids = new RemQueue<EntityUid>();
// Store the chunks for later to remove.
foreach (var (gridId, oldIndices) in _previousSentChunks[playerSession]) foreach (var (gridId, oldIndices) in previouslySent)
{ {
// Mark the whole grid as stale and flag for removal. // Mark the whole grid as stale and flag for removal.
if (!chunksInRange.TryGetValue(gridId, out var chunks)) if (!chunksInRange.TryGetValue(gridId, out var chunks))
{ {
toRemoveGrids.Add(gridId); previouslySent.Remove(gridId);
// If grid was deleted then don't worry about sending it to the client. // Was the grid deleted?
if (MapManager.TryGetGrid(gridId, out _)) if (MapManager.IsGrid(gridId))
staleChunks[gridId] = oldIndices; staleChunks[gridId] = oldIndices;
else
{
// If grid was deleted then don't worry about telling the client to delete the chunk.
oldIndices.Clear();
_chunkIndexPool.Return(oldIndices);
}
continue; continue;
} }
@@ -412,46 +452,30 @@ namespace Content.Server.Decals
foreach (var (gridId, gridChunks) in chunksInRange) foreach (var (gridId, gridChunks) in chunksInRange)
{ {
var newChunks = _chunkIndexPool.Get(); var newChunks = _chunkIndexPool.Get();
_dirtyChunks.TryGetValue(gridId, out var dirtyChunks);
if (!previouslySent.TryGetValue(gridId, out var previousChunks))
newChunks.UnionWith(gridChunks); newChunks.UnionWith(gridChunks);
if (_previousSentChunks[playerSession].TryGetValue(gridId, out var previousChunks)) else
{ {
newChunks.ExceptWith(previousChunks); foreach (var index in gridChunks)
{
if (!previousChunks.Contains(index) || dirtyChunks != null && dirtyChunks.Contains(index))
newChunks.Add(index);
} }
if (_dirtyChunks.TryGetValue(gridId, out var dirtyChunks)) previousChunks.Clear();
{ _chunkIndexPool.Return(previousChunks);
var inRange = new HashSet<Vector2i>();
inRange.UnionWith(gridChunks);
inRange.IntersectWith(dirtyChunks);
newChunks.UnionWith(inRange);
} }
previouslySent[gridId] = gridChunks;
if (newChunks.Count == 0) if (newChunks.Count == 0)
{
_chunkIndexPool.Return(newChunks); _chunkIndexPool.Return(newChunks);
continue; else
}
// TODO: This is gonna have churn but mainly I want to fix the bugs rn.
_previousSentChunks[playerSession][gridId] = gridChunks;
updatedChunks[gridId] = newChunks; updatedChunks[gridId] = newChunks;
} }
// We'll only remove stale grids after the above iteration.
foreach (var gridId in toRemoveGrids)
{
_previousSentChunks[playerSession].Remove(gridId);
}
if (updatedChunks.Count == 0)
{
// ReturnToPool(chunksInRange);
// Even if updatedChunks is empty we'll still return it to the pool as it may have been allocated higher.
ReturnToPool(updatedChunks);
}
if (updatedChunks.Count == 0 && staleChunks.Count == 0) continue;
//send all gridChunks to client //send all gridChunks to client
SendChunkUpdates(playerSession, updatedChunks, staleChunks); SendChunkUpdates(playerSession, updatedChunks, staleChunks);
} }
@@ -479,18 +503,25 @@ namespace Content.Server.Decals
var updatedDecals = new Dictionary<EntityUid, Dictionary<Vector2i, Dictionary<uint, Decal>>>(); var updatedDecals = new Dictionary<EntityUid, Dictionary<Vector2i, Dictionary<uint, Decal>>>();
foreach (var (gridId, chunks) in updatedChunks) foreach (var (gridId, chunks) in updatedChunks)
{ {
var collection = ChunkCollection(gridId);
if (collection == null)
continue;
var gridChunks = new Dictionary<Vector2i, Dictionary<uint, Decal>>(); var gridChunks = new Dictionary<Vector2i, Dictionary<uint, Decal>>();
foreach (var indices in chunks) foreach (var indices in chunks)
{ {
gridChunks.Add(indices, gridChunks.Add(indices,
ChunkCollection(gridId).TryGetValue(indices, out var chunk) collection.TryGetValue(indices, out var chunk)
? chunk ? chunk
: new Dictionary<uint, Decal>()); : new Dictionary<uint, Decal>(0));
} }
updatedDecals[gridId] = gridChunks; updatedDecals[gridId] = gridChunks;
} }
RaiseNetworkEvent(new DecalChunkUpdateEvent{Data = updatedDecals, RemovedChunks = staleChunks}, Filter.SinglePlayer(session)); RaiseNetworkEvent(new DecalChunkUpdateEvent{Data = updatedDecals, RemovedChunks = staleChunks}, Filter.SinglePlayer(session));
ReturnToPool(updatedChunks);
ReturnToPool(staleChunks);
} }
private HashSet<EntityUid> GetSessionViewers(IPlayerSession session) private HashSet<EntityUid> GetSessionViewers(IPlayerSession session)
@@ -528,14 +559,14 @@ namespace Content.Server.Decals
foreach (var grid in MapManager.FindGridsIntersecting(mapId, bounds)) foreach (var grid in MapManager.FindGridsIntersecting(mapId, bounds))
{ {
if (!chunks.ContainsKey(grid.GridEntityId)) if (!chunks.TryGetValue(grid.GridEntityId, out var chunk))
chunks[grid.GridEntityId] = _chunkIndexPool.Get(); chunks[grid.GridEntityId] = chunk = _chunkIndexPool.Get();
var enumerator = new ChunkIndicesEnumerator(_transform.GetInvWorldMatrix(grid.GridEntityId, xformQuery).TransformBox(bounds), ChunkSize); var enumerator = new ChunkIndicesEnumerator(_transform.GetInvWorldMatrix(grid.GridEntityId, xformQuery).TransformBox(bounds), ChunkSize);
while (enumerator.MoveNext(out var indices)) while (enumerator.MoveNext(out var indices))
{ {
chunks[grid.GridEntityId].Add(indices.Value); chunk.Add(indices.Value);
} }
} }
} }

View File

@@ -15,6 +15,8 @@ namespace Content.Shared.Decals
protected readonly Dictionary<EntityUid, Dictionary<uint, Vector2i>> ChunkIndex = new(); 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.
// So if this ever needs changing, the maps need converting.
public const int ChunkSize = 32; public const int ChunkSize = 32;
public static Vector2i GetChunkIndices(Vector2 coordinates) => new ((int) Math.Floor(coordinates.X / ChunkSize), (int) Math.Floor(coordinates.Y / ChunkSize)); public static Vector2i GetChunkIndices(Vector2 coordinates) => new ((int) Math.Floor(coordinates.X / ChunkSize), (int) Math.Floor(coordinates.Y / ChunkSize));
@@ -52,9 +54,19 @@ namespace Content.Shared.Decals
} }
} }
protected DecalGridComponent.DecalGridChunkCollection DecalGridChunkCollection(EntityUid gridEuid) => protected DecalGridComponent.DecalGridChunkCollection? DecalGridChunkCollection(EntityUid gridEuid, DecalGridComponent? comp = null)
Comp<DecalGridComponent>(gridEuid).ChunkCollection; {
protected Dictionary<Vector2i, Dictionary<uint, Decal>> ChunkCollection(EntityUid gridEuid) => DecalGridChunkCollection(gridEuid).ChunkCollection; if (!Resolve(gridEuid, ref comp))
return null;
return comp.ChunkCollection;
}
protected Dictionary<Vector2i, Dictionary<uint, Decal>>? ChunkCollection(EntityUid gridEuid, DecalGridComponent? comp = null)
{
var collection = DecalGridChunkCollection(gridEuid, comp);
return collection?.ChunkCollection;
}
protected virtual void DirtyChunk(EntityUid id, Vector2i chunkIndices) {} protected virtual void DirtyChunk(EntityUid id, Vector2i chunkIndices) {}
@@ -68,12 +80,12 @@ namespace Content.Shared.Decals
} }
var chunkCollection = ChunkCollection(gridId); var chunkCollection = ChunkCollection(gridId);
if (!chunkCollection.TryGetValue(indices, out var chunk) || !chunk.Remove(uid)) if (chunkCollection == null || !chunkCollection.TryGetValue(indices, out var chunk) || !chunk.Remove(uid))
{ {
return false; return false;
} }
if (chunkCollection[indices].Count == 0) if (chunk.Count == 0)
chunkCollection.Remove(indices); chunkCollection.Remove(indices);
ChunkIndex[gridId].Remove(uid); ChunkIndex[gridId].Remove(uid);

View File

@@ -0,0 +1,3 @@
zoom-command-description = Sets the zoom of the main eye.
zoom-command-help = zoom ( <scale> | <X-scale> <Y-scale> )
zoom-command-error = scale has to be greater than 0

View File

@@ -45,6 +45,7 @@
- showspread - showspread
- showambient - showambient
- showemergencyshuttle - showemergencyshuttle
- zoom
- Flags: MAPPING - Flags: MAPPING
Commands: Commands: