Fix T-ray scanner PVS issues (#6554)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2022-02-12 12:00:33 +13:00
committed by GitHub
parent b4bef71424
commit 4ed7ec9419
6 changed files with 173 additions and 162 deletions

View File

@@ -1,19 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using Content.Shared.SubFloor;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Map;
using Robust.Shared.Utility;
namespace Content.Client.SubFloor;
public class TrayScannerSystem : SharedTrayScannerSystem
public sealed class TrayScannerSystem : SharedTrayScannerSystem
{
[Dependency] private IEntityLookup _entityLookup = default!;
[Dependency] private IMapManager _mapManager = default!;
[Dependency] private SubFloorHideSystem _subfloorSystem = default!;
[Dependency] private SharedContainerSystem _containerSystem = default!;
@@ -28,7 +23,7 @@ public class TrayScannerSystem : SharedTrayScannerSystem
public void OnComponentShutdown(EntityUid uid, TrayScannerComponent scanner, ComponentShutdown args)
{
_subfloorSystem.ToggleSubfloorEntities(scanner.RevealedSubfloors, false, uid, _visualizerKeys);
_subfloorSystem.SetEntitiesRevealed(scanner.RevealedSubfloors, uid, false, _visualizerKeys);
_invalidScanners.Add(uid);
}
@@ -66,6 +61,29 @@ public class TrayScannerSystem : SharedTrayScannerSystem
if (_invalidScanners.List != null) _invalidScanners.List.Clear();
}
/// <summary>
/// When a subfloor entity gets anchored (which includes spawning & coming into PVS range), Check for nearby scanners.
/// </summary>
public override void OnSubfloorAnchored(EntityUid uid, SubFloorHideComponent? hideComp = null, TransformComponent? xform = null)
{
if (!Resolve(uid, ref hideComp, ref xform))
return;
var pos = xform.MapPosition;
foreach (var entity in _activeScanners)
{
if (!TryComp(entity, out TrayScannerComponent? scanner))
continue;
if (!Transform(entity).MapPosition.InRange(pos, scanner.Range))
continue;
hideComp.RevealedBy.Add(entity);
scanner.RevealedSubfloors.Add(uid);
}
}
/// <summary>
/// Updates a T-Ray scanner. Should be called on immediate
/// state change (turned on/off), or during the update
@@ -84,9 +102,9 @@ public class TrayScannerSystem : SharedTrayScannerSystem
// set all the known subfloor to invisible,
// and return false so it's removed from
// the active scanner list
if (!scanner.Toggled)
if (!scanner.Toggled || transform.MapID == MapId.Nullspace)
{
_subfloorSystem.ToggleSubfloorEntities(scanner.RevealedSubfloors, false, uid, _visualizerKeys);
_subfloorSystem.SetEntitiesRevealed(scanner.RevealedSubfloors, uid, false, _visualizerKeys);
scanner.LastLocation = Vector2.Zero;
scanner.RevealedSubfloors.Clear();
return false;
@@ -130,36 +148,56 @@ public class TrayScannerSystem : SharedTrayScannerSystem
// is still technically on
if (flooredPos == Vector2.Zero)
{
_subfloorSystem.ToggleSubfloorEntities(scanner.RevealedSubfloors, false, uid, _visualizerKeys);
_subfloorSystem.SetEntitiesRevealed(scanner.RevealedSubfloors, uid, false, _visualizerKeys);
scanner.RevealedSubfloors.Clear();
return true;
}
// MAYBE redo this. Currently different players can see different entities
//
// Here we avoid the entity lookup & return early if the scanner's position hasn't appreciably changed. However,
// if a new player enters PVS-range, they will update the in-range entities on their end and use that to set
// LastLocation. This means that different players can technically see different entities being revealed by the
// same scanner. The correct fix for this is probably just to network the revealed entity set.... But I CBF
// doing that right now....
if (flooredPos == scanner.LastLocation
|| (float.IsNaN(flooredPos.X) && float.IsNaN(flooredPos.Y)))
return true;
scanner.LastLocation = flooredPos;
// get all entities in range by uid
// but without using LINQ
// Update entities in Range
HashSet<EntityUid> nearby = new();
var coords = transform.MapPosition;
var worldBox = Box2.CenteredAround(coords.Position, (scanner.Range * 2, scanner.Range * 2));
foreach (var entityInRange in _entityLookup.GetEntitiesInRange(uid, scanner.Range))
if (FilterAnchored(entityInRange)) nearby.Add(entityInRange);
// For now, limiting to the scanner's own grid. We could do a grid-lookup, but then what do we do if one grid
// flies away, while the scanner's local-position remains unchanged?
if (_mapManager.TryGetGrid(transform.GridID, out var grid))
{
foreach (var entity in grid.GetAnchoredEntities(worldBox))
{
if (!Transform(entity).MapPosition.InRange(coords, scanner.Range))
continue;
if (!TryComp(entity, out SubFloorHideComponent? hideComp))
continue; // Not a hide-able entity.
nearby.Add(entity);
if (scanner.RevealedSubfloors.Add(entity))
_subfloorSystem.SetEntityRevealed(entity, uid, true, hideComp, _visualizerKeys);
}
}
// get all the old elements that are no longer detected
scanner.RevealedSubfloors.ExceptWith(nearby);
HashSet<EntityUid> missing = new(scanner.RevealedSubfloors.Except(nearby));
// hide all of them, since they're no longer needed
_subfloorSystem.ToggleSubfloorEntities(scanner.RevealedSubfloors, false, uid, _visualizerKeys);
scanner.RevealedSubfloors.Clear();
// remove those from the list
scanner.RevealedSubfloors.ExceptWith(missing);
// set the revealedsubfloor set to the new nearby set
scanner.RevealedSubfloors.UnionWith(nearby);
// show all the new subfloor
_subfloorSystem.ToggleSubfloorEntities(scanner.RevealedSubfloors, true, uid, _visualizerKeys);
// and hide them
_subfloorSystem.SetEntitiesRevealed(missing, uid, false, _visualizerKeys);
return true;
}
@@ -169,10 +207,4 @@ public class TrayScannerSystem : SharedTrayScannerSystem
SubFloorVisuals.SubFloor,
TrayScannerTransparency.Key
};
private bool FilterAnchored(EntityUid uid)
{
return EntityManager.TryGetComponent<TransformComponent>(uid, out var transform)
&& transform.Anchored;
}
}