Fix decals (finally) (#7771)

This commit is contained in:
metalgearsloth
2022-04-28 20:23:43 +10:00
committed by GitHub
parent c2b4a4acef
commit 77bd892bbd
6 changed files with 114 additions and 35 deletions

View File

@@ -8,6 +8,7 @@ using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Utility;
namespace Content.Server.Decals
{
@@ -42,6 +43,52 @@ namespace Content.Server.Decals
SubscribeNetworkEvent<RequestDecalPlacementEvent>(OnDecalPlacementRequest);
SubscribeNetworkEvent<RequestDecalRemovalEvent>(OnDecalRemovalRequest);
SubscribeLocalEvent<PostGridSplitEvent>(OnGridSplit);
}
private void OnGridSplit(ref PostGridSplitEvent ev)
{
// Transfer decals over to the new grid.
var enumerator = MapManager.GetGrid(ev.Grid).GetAllTilesEnumerator();
var oldChunkCollection = DecalGridChunkCollection(ev.OldGrid);
var chunkCollection = DecalGridChunkCollection(ev.Grid);
while (enumerator.MoveNext(out var tile))
{
var tilePos = (Vector2) tile.Value.GridIndices;
var chunkIndices = GetChunkIndices(tilePos);
if (!oldChunkCollection.ChunkCollection.TryGetValue(chunkIndices, out var oldChunk)) continue;
var bounds = new Box2(tilePos - 0.01f, tilePos + 1.01f);
var toRemove = new RemQueue<uint>();
foreach (var (oldUid, decal) in oldChunk)
{
if (!bounds.Contains(decal.Coordinates)) continue;
var uid = chunkCollection.NextUid++;
var chunk = chunkCollection.ChunkCollection.GetOrNew(chunkIndices);
chunk[uid] = decal;
ChunkIndex[ev.Grid][uid] = chunkIndices;
DirtyChunk(ev.Grid, chunkIndices);
toRemove.Add(oldUid);
ChunkIndex[ev.OldGrid].Remove(oldUid);
}
foreach (var uid in toRemove)
{
oldChunk.Remove(uid);
}
if (oldChunk.Count == 0)
oldChunkCollection.ChunkCollection.Remove(chunkIndices);
if (toRemove.List?.Count > 0)
DirtyChunk(ev.OldGrid, chunkIndices);
}
}
public override void Shutdown()
@@ -127,9 +174,10 @@ namespace Content.Server.Decals
return;
// remove all decals on the same tile
foreach (var decal in GetDecalsInRange(gridId, ev.Coordinates.Position))
foreach (var (uid, decal) in GetDecalsInRange(gridId, ev.Coordinates.Position))
{
RemoveDecal(gridId, decal);
var chunkIndices = GetChunkIndices(decal.Coordinates);
RemoveDecal(gridId, uid);
}
}
@@ -158,10 +206,10 @@ namespace Content.Server.Decals
return false;
var gridId = coordinates.GetGridId(EntityManager);
if (!gridId.IsValid())
if (!MapManager.TryGetGrid(gridId, out var grid))
return false;
if (MapManager.GetGrid(gridId).GetTileRef(coordinates).IsSpace())
if (grid.GetTileRef(coordinates).IsSpace(_tileDefMan))
return false;
var chunkCollection = DecalGridChunkCollection(gridId);
@@ -178,9 +226,9 @@ namespace Content.Server.Decals
public bool RemoveDecal(GridId gridId, uint uid) => RemoveDecalInternal(gridId, uid);
public HashSet<uint> GetDecalsInRange(GridId gridId, Vector2 position, float distance = 0.75f, Func<Decal, bool>? validDelegate = null)
public HashSet<(uint Index, Decal Decal)> GetDecalsInRange(GridId gridId, Vector2 position, float distance = 0.75f, Func<Decal, bool>? validDelegate = null)
{
var uids = new HashSet<uint>();
var uids = new HashSet<(uint, Decal)>();
var chunkCollection = ChunkCollection(gridId);
var chunkIndices = GetChunkIndices(position);
if (!chunkCollection.TryGetValue(chunkIndices, out var chunk))
@@ -193,7 +241,7 @@ namespace Content.Server.Decals
if (validDelegate == null || validDelegate(decal))
{
uids.Add(uid);
uids.Add((uid, decal));
}
}
@@ -310,15 +358,33 @@ namespace Content.Server.Decals
{
base.Update(frameTime);
foreach (var session in Filter.GetAllPlayers(_playerManager))
{
if (session is not IPlayerSession { Status: SessionStatus.InGame } playerSession)
continue;
var chunks = GetChunksForSession(playerSession);
var chunksInRange = GetChunksForSession(playerSession);
var staleChunks = new Dictionary<GridId, HashSet<Vector2i>>();
// Get any chunks not in range anymore
foreach (var (gridId, oldIndices) in _previousSentChunks[playerSession])
{
if (!chunksInRange.TryGetValue(gridId, out var chunks))
{
staleChunks[gridId] = oldIndices;
continue;
}
foreach (var chunk in oldIndices)
{
if (chunks.Contains(chunk)) continue;
var elmo = staleChunks.GetOrNew(gridId);
elmo.Add(chunk);
}
}
var updatedChunks = _chunkViewerPool.Get();
foreach (var (gridId, gridChunks) in chunks)
foreach (var (gridId, gridChunks) in chunksInRange)
{
var newChunks = _chunkIndexPool.Get();
newChunks.UnionWith(gridChunks);
@@ -344,17 +410,18 @@ namespace Content.Server.Decals
if (updatedChunks.Count == 0)
{
ReturnToPool(chunks);
ReturnToPool(chunksInRange);
// Even if updatedChunks is empty we'll still return it to the pool as it may have been allocated higher.
ReturnToPool(updatedChunks);
continue;
}
if (updatedChunks.Count == 0 && staleChunks.Count == 0) continue;
ReturnToPool(_previousSentChunks[playerSession]);
_previousSentChunks[playerSession] = chunks;
_previousSentChunks[playerSession] = chunksInRange;
//send all gridChunks to client
SendChunkUpdates(playerSession, updatedChunks);
SendChunkUpdates(playerSession, updatedChunks, staleChunks);
}
_dirtyChunks.Clear();
@@ -372,7 +439,10 @@ namespace Content.Server.Decals
_chunkViewerPool.Return(chunks);
}
private void SendChunkUpdates(IPlayerSession session, Dictionary<GridId, HashSet<Vector2i>> updatedChunks)
private void SendChunkUpdates(
IPlayerSession session,
Dictionary<GridId, HashSet<Vector2i>> updatedChunks,
Dictionary<GridId, HashSet<Vector2i>> staleChunks)
{
var updatedDecals = new Dictionary<GridId, Dictionary<Vector2i, Dictionary<uint, Decal>>>();
foreach (var (gridId, chunks) in updatedChunks)
@@ -388,7 +458,7 @@ namespace Content.Server.Decals
updatedDecals[gridId] = gridChunks;
}
RaiseNetworkEvent(new DecalChunkUpdateEvent{Data = updatedDecals}, Filter.SinglePlayer(session));
RaiseNetworkEvent(new DecalChunkUpdateEvent{Data = updatedDecals, RemovedChunks = staleChunks}, Filter.SinglePlayer(session));
}
private HashSet<EntityUid> GetSessionViewers(IPlayerSession session)