diff --git a/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs b/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs index ca2fe7bd93a..fdbca9a2a26 100644 --- a/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs +++ b/Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs @@ -99,7 +99,7 @@ private void AddEntitiesIntersecting(MapId mapId, { var localTransform = state.Physics.GetRelativePhysicsTransform(state.Transform, uid); var localAabb = state.Shape.ComputeAABB(localTransform, 0); - state.Lookup.AddEntitiesIntersecting(uid, state.Intersecting, state.Shape, localAabb, state.Transform, state.Flags); + state.Lookup.AddEntitiesIntersecting(uid, state.Intersecting, state.Shape, localAabb, localTransform, state.Flags); return true; }, approx: true, includeMap: false); @@ -107,7 +107,7 @@ private void AddEntitiesIntersecting(MapId mapId, var localTransform = state.Physics.GetRelativePhysicsTransform(state.Transform, mapUid); var localAabb = state.Shape.ComputeAABB(localTransform, 0); - AddEntitiesIntersecting(mapUid, intersecting, shape, localAabb, shapeTransform, flags); + AddEntitiesIntersecting(mapUid, intersecting, shape, localAabb, localTransform, flags); AddContained(intersecting, flags); } diff --git a/Robust.UnitTesting/Shared/EntityLookup_Test.cs b/Robust.UnitTesting/Shared/EntityLookup_Test.cs index e4deb6d1203..6839f90f725 100644 --- a/Robust.UnitTesting/Shared/EntityLookup_Test.cs +++ b/Robust.UnitTesting/Shared/EntityLookup_Test.cs @@ -1,9 +1,14 @@ +using System.Collections.Generic; using System.Linq; using System.Numerics; using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.Map; +using Robust.Shared.Map.Components; using Robust.Shared.Maths; +using Robust.Shared.Physics.Collision.Shapes; +using Robust.Shared.Physics.Components; +using Robust.Shared.Physics.Systems; using Robust.UnitTesting.Server; namespace Robust.UnitTesting.Shared @@ -11,6 +16,142 @@ namespace Robust.UnitTesting.Shared [TestFixture, TestOf(typeof(EntityLookupSystem))] public sealed class EntityLookupTest { + private static readonly MapId _mapId = new MapId(1); + + private EntityUid GetPhysicsEntity(IEntityManager entManager, MapCoordinates spawnPos) + { + var ent = entManager.SpawnEntity(null, spawnPos); + var comp = entManager.AddComponent(ent); + entManager.System().TryCreateFixture(ent, new PhysShapeCircle(0.35f, Vector2.Zero), "fix1"); + return ent; + } + + private Entity SetupGrid(MapId mapId, SharedMapSystem mapSystem, IEntityManager entManager, IMapManager mapManager) + { + mapSystem.CreateMap(mapId); + var grid = mapManager.CreateGridEntity(mapId); + entManager.System().SetLocalPosition(grid.Owner, new Vector2(10f, 10f)); + mapSystem.SetTile(grid, Vector2i.Zero, new Tile(1)); + return grid; + } + + private static readonly TestCaseData[] MapInRangeCases = new[] + { + new TestCaseData(new MapCoordinates(Vector2.One, _mapId), new MapCoordinates(Vector2.Zero, _mapId), 0.5f, false), + new TestCaseData(new MapCoordinates(Vector2.One, _mapId), new MapCoordinates(Vector2.One, _mapId), 0.5f, true), + }; + + private static readonly TestCaseData[] GridInRangeCases = new[] + { + new TestCaseData(new MapCoordinates(Vector2.One, _mapId), new MapCoordinates(Vector2.Zero, _mapId), 0.5f, false), + new TestCaseData(new MapCoordinates(new Vector2(10f, 10f), _mapId), new MapCoordinates(new Vector2(10f, 10f), _mapId), 0.5f, true), + }; + + // Remember this test data is relative. + private static readonly TestCaseData[] GridBox2Cases = new[] + { + new TestCaseData(new MapCoordinates(Vector2.One, _mapId), Box2.UnitCentered, false), + new TestCaseData(new MapCoordinates(new Vector2(10f, 10f), _mapId), Box2.UnitCentered, true), + }; + + [Test, TestCaseSource(nameof(MapInRangeCases))] + public void TestMapInRange(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(); + + entManager.System().CreateMap(spawnPos.MapId); + GetPhysicsEntity(entManager, spawnPos); + + Assert.That(lookup.GetEntitiesInRange(queryPos.MapId, queryPos.Position, range).Count > 0, Is.EqualTo(result)); + mapManager.DeleteMap(spawnPos.MapId); + } + + [Test, TestCaseSource(nameof(GridInRangeCases))] + public void TestGridInRange(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 = mapManager.CreateGridEntity(spawnPos.MapId); + entManager.System().SetLocalPosition(grid.Owner, new Vector2(10f, 10f)); + + GetPhysicsEntity(entManager, spawnPos); + + _ = entManager.SpawnEntity(null, spawnPos); + Assert.That(lookup.GetEntitiesInRange(queryPos.MapId, queryPos.Position, range).Count > 0, Is.EqualTo(result)); + mapManager.DeleteMap(spawnPos.MapId); + } + + [Test, TestCaseSource(nameof(MapInRangeCases))] + public void TestMapNoFixtureInRange(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(); + + entManager.System().CreateMap(spawnPos.MapId); + + _ = entManager.SpawnEntity(null, spawnPos); + Assert.That(lookup.GetEntitiesInRange(queryPos.MapId, queryPos.Position, range).Count > 0, Is.EqualTo(result)); + mapManager.DeleteMap(spawnPos.MapId); + } + + /// + /// Tests Box2 local queries for a particular lookup ID. + /// + [Test, TestCaseSource(nameof(GridBox2Cases))] + public void TestGridLocalIntersecting(MapCoordinates spawnPos, Box2 queryBounds, 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(); + + var grid = SetupGrid(spawnPos.MapId, mapSystem, entManager, mapManager); + + GetPhysicsEntity(entManager, spawnPos); + var entities = new HashSet(); + lookup.GetLocalEntitiesIntersecting(grid.Owner, queryBounds, entities); + + Assert.That(entities.Count > 0, Is.EqualTo(result)); + mapManager.DeleteMap(spawnPos.MapId); + } + + [Test, TestCaseSource(nameof(GridInRangeCases))] + public void TestGridNoFixtureInRange(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(); + + var grid = SetupGrid(spawnPos.MapId, mapSystem, entManager, mapManager); + + var ent = entManager.SpawnEntity(null, spawnPos); + Assert.That(lookup.GetEntitiesInRange(queryPos.MapId, queryPos.Position, range).Count > 0, Is.EqualTo(result)); + mapManager.DeleteMap(spawnPos.MapId); + } + [Test] public void AnyIntersecting() { @@ -25,7 +166,7 @@ public void AnyIntersecting() var theMapSpotBeingUsed = new Box2(Vector2.Zero, Vector2.One); - var dummy = entManager.SpawnEntity(null, new MapCoordinates(Vector2.Zero, mapId)); + _ = entManager.SpawnEntity(null, new MapCoordinates(Vector2.Zero, mapId)); Assert.That(lookup.AnyEntitiesIntersecting(mapId, theMapSpotBeingUsed)); mapManager.DeleteMap(mapId); }