From f2f88246787887fe463c608781590d879ee6712f Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Thu, 4 Jan 2024 23:17:44 -0500 Subject: [PATCH] Fix pvs error (#4812) * Fix pvs error * rename variable --- .../GameStates/PvsSystem.ToSendSet.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Robust.Server/GameStates/PvsSystem.ToSendSet.cs b/Robust.Server/GameStates/PvsSystem.ToSendSet.cs index c08c5b56ddf..4e400b9cf7b 100644 --- a/Robust.Server/GameStates/PvsSystem.ToSendSet.cs +++ b/Robust.Server/GameStates/PvsSystem.ToSendSet.cs @@ -24,7 +24,7 @@ private void AddPvsChunks(PvsSession pvsSession) } /// - /// A chunks that is visible to a player and add entities to the game-state. + /// Add all entities on a given PVS chunk to a clients game-state. /// private void AddPvsChunk(PvsChunk chunk, float distance, PvsSession session) { @@ -48,17 +48,28 @@ private void AddPvsChunk(PvsChunk chunk, float distance, PvsSession session) // We add chunk-size here so that its consistent with the normal PVS range setting. // I.e., distance here is the Chebyshev distance to the centre of each chunk, but the normal pvs range only // required that the chunk be touching the box, not the centre. - var count = distance < (_lowLodDistance + ChunkSize) / 2 + var limit = distance < (_lowLodDistance + ChunkSize) / 2 ? chunk.Contents.Count : chunk.LodCounts[0]; + // If the PVS budget is exceeded, it should still be safe to send all of the chunk's direct children, though + // after that we have no guarantee that an entity's parent got sent. + var directChildren = Math.Min(limit, chunk.LodCounts[2]); + // Send entities on the chunk. var span = CollectionsMarshal.AsSpan(chunk.Contents); - for (var i = 0; i < count; i++) + for (var i = 0; i < limit; i++) { var ent = span[i]; - if ((mask & ent.Comp.VisibilityMask) == ent.Comp.VisibilityMask) - AddEntity(session, ent, fromTick); + if ((mask & ent.Comp.VisibilityMask) != ent.Comp.VisibilityMask) + continue; + + // TODO PVS improve this somehow + // Having entities "leave" pvs view just because the pvs entry budget was exceeded sucks. + // This probably requires changing client game state manager to support receiving entities with unknown parents. + // Probably needs to do something similar to pending net entity states, but for entity spawning. + if (!AddEntity(session, ent, fromTick)) + limit = directChildren; } }