From 14ccd7d990170da57b53e1412110645a0ae0c45d Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Sat, 31 Aug 2024 14:11:24 +1000 Subject: [PATCH] test --- .../Systems/EntityLookup.Queries.cs | 60 +------------------ .../Shared/EntityLookup_Test.cs | 35 ++++++++++- 2 files changed, 35 insertions(+), 60 deletions(-) diff --git a/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs b/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs index e3d49ee1b42..393e7498b0b 100644 --- a/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs +++ b/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs @@ -487,23 +487,8 @@ public void GetEntitiesIntersecting(MapId mapId, Box2 worldAABB, HashSet 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 @@ -539,47 +524,8 @@ public bool AnyEntitiesIntersecting(MapId mapId, Box2Rotated worldBounds, Lookup public HashSet GetEntitiesIntersecting(MapId mapId, Box2Rotated worldBounds, LookupFlags flags = DefaultFlags) { var intersecting = new HashSet(); - - 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 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; } diff --git a/Robust.UnitTesting/Shared/EntityLookup_Test.cs b/Robust.UnitTesting/Shared/EntityLookup_Test.cs index cc9352bb106..56d9fc7ed8b 100644 --- a/Robust.UnitTesting/Shared/EntityLookup_Test.cs +++ b/Robust.UnitTesting/Shared/EntityLookup_Test.cs @@ -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), @@ -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. @@ -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().GetEntitySystem(); + var entManager = server.Resolve(); + var mapManager = server.Resolve(); + var mapSystem = entManager.System(); + + 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) {