Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Aug 31, 2024
1 parent 98fa764 commit 14ccd7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 60 deletions.
60 changes: 3 additions & 57 deletions Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,23 +487,8 @@ public void GetEntitiesIntersecting(MapId mapId, Box2 worldAABB, HashSet<EntityU
{
if (mapId == MapId.Nullspace) return;

// Get grid entities
var state = (this, intersecting, worldAABB, _transform, flags);

_mapManager.FindGridsIntersecting(mapId, worldAABB, ref state,
static (EntityUid gridUid, MapGridComponent _, ref (
EntityLookupSystem lookup, HashSet<EntityUid> intersecting,
Box2 worldAABB, SharedTransformSystem xformSystem, LookupFlags flags) tuple) =>
{
var localAABB = tuple.xformSystem.GetInvWorldMatrix(gridUid).TransformBox(tuple.worldAABB);
tuple.lookup.AddLocalEntitiesIntersecting(gridUid, tuple.intersecting, localAABB, tuple.flags);
return true;
}, approx: true, includeMap: false);

// Get map entities
var mapUid = _map.GetMapOrInvalid(mapId);
AddLocalEntitiesIntersecting(mapUid, intersecting, worldAABB, flags);
AddContained(intersecting, flags);
var shape = new Polygon(worldAABB);
AddEntitiesIntersecting(mapId, intersecting, shape, Physics.Transform.Empty, flags);
}

#endregion
Expand Down Expand Up @@ -539,47 +524,8 @@ public bool AnyEntitiesIntersecting(MapId mapId, Box2Rotated worldBounds, Lookup
public HashSet<EntityUid> GetEntitiesIntersecting(MapId mapId, Box2Rotated worldBounds, LookupFlags flags = DefaultFlags)
{
var intersecting = new HashSet<EntityUid>();

if (mapId == MapId.Nullspace)
return intersecting;

var mapUid = _map.GetMapOrInvalid(mapId);

// Get grid entities
var shape = new Polygon(worldBounds);
var transform = Physics.Transform.Empty;

var state = (this, _physics, intersecting, transform, shape, flags);

_mapManager.FindGridsIntersecting(mapUid, shape, transform, ref state,
static (
EntityUid uid,
MapGridComponent grid,
ref (EntityLookupSystem lookup,
SharedPhysicsSystem _physics,
HashSet<EntityUid> intersecting,
Transform transform,
Polygon shape, LookupFlags flags) state) =>
{
var localTransform = state._physics.GetRelativePhysicsTransform(state.transform, uid);
var localAabb = state.shape.ComputeAABB(localTransform, 0);
state.lookup.AddEntitiesIntersecting(uid,
state.intersecting,
state.shape,
localAabb,
localTransform,
state.flags);
return true;
});

// Get map entities
var localTransform = _physics.GetRelativePhysicsTransform(transform, mapUid);
var localAabb = shape.ComputeAABB(localTransform, 0);

AddEntitiesIntersecting(mapUid, intersecting, shape, localAabb, transform, flags);
AddContained(intersecting, flags);

AddEntitiesIntersecting(mapId, intersecting, shape, Physics.Transform.Empty, flags);
return intersecting;
}

Expand Down
35 changes: 32 additions & 3 deletions Robust.UnitTesting/Shared/EntityLookup_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public sealed class EntityLookupTest
{
private static readonly MapId MapId = new MapId(1);

private static readonly TestCaseData[] IntersectingCases = new[]
{
// Big offset
new TestCaseData(true, new MapCoordinates(new Vector2(100f, 0f), MapId), new MapCoordinates(new Vector2(100f, 0f), MapId), 0.25f, true),
};

private static readonly TestCaseData[] InRangeCases = new[]
{
new TestCaseData(true, new MapCoordinates(Vector2.One, MapId), new MapCoordinates(Vector2.Zero, MapId), 0.5f, false),
Expand All @@ -33,9 +39,6 @@ public sealed class EntityLookupTest
new TestCaseData(false, new MapCoordinates(new Vector2(10f, 10f), MapId), new MapCoordinates(new Vector2(9.5f, 9.5f), MapId), 0.5f, false),
// Close but no cigar
new TestCaseData(false, new MapCoordinates(new Vector2(10f, 10f), MapId), new MapCoordinates(new Vector2(9f, 9f), MapId), 0.5f, false),

// Big offset
new TestCaseData(true, new MapCoordinates(new Vector2(100f, 0f), MapId), new MapCoordinates(new Vector2(100f, 0f), MapId), 0.25f, true),
};

// Remember this test data is relative.
Expand Down Expand Up @@ -210,6 +213,32 @@ public void TestMapInRange(bool physics, MapCoordinates spawnPos, MapCoordinates
mapManager.DeleteMap(spawnPos.MapId);
}

[Test, TestCaseSource(nameof(IntersectingCases))]
public void TestGridIntersecting(bool physics, MapCoordinates spawnPos, MapCoordinates queryPos, float range, bool result)
{
var sim = RobustServerSimulation.NewSimulation();
var server = sim.InitializeInstance();

var lookup = server.Resolve<IEntitySystemManager>().GetEntitySystem<EntityLookupSystem>();
var entManager = server.Resolve<IEntityManager>();
var mapManager = server.Resolve<IMapManager>();
var mapSystem = entManager.System<SharedMapSystem>();

mapSystem.CreateMap(spawnPos.MapId);
var grid = SetupGrid(spawnPos.MapId, mapSystem, entManager, mapManager);

if (physics)
GetPhysicsEntity(entManager, spawnPos);
else
entManager.Spawn(null, spawnPos);

_ = entManager.SpawnEntity(null, spawnPos);
var bounds = Box2.CenteredAround(queryPos.Position, new Vector2(range, range));

Assert.That(lookup.GetEntitiesIntersecting(queryPos.MapId, bounds).Count > 0, Is.EqualTo(result));
mapManager.DeleteMap(spawnPos.MapId);
}

[Test, TestCaseSource(nameof(InRangeCases))]
public void TestGridInRange(bool physics, MapCoordinates spawnPos, MapCoordinates queryPos, float range, bool result)
{
Expand Down

0 comments on commit 14ccd7d

Please sign in to comment.