Skip to content

Commit

Permalink
Tests and confidence
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Aug 26, 2024
1 parent 692916d commit 78f4d6a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ 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);

var mapUid = _map.GetMapOrInvalid(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);
}

Expand Down
143 changes: 142 additions & 1 deletion Robust.UnitTesting/Shared/EntityLookup_Test.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,157 @@
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
{
[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<PhysicsComponent>(ent);
entManager.System<FixtureSystem>().TryCreateFixture(ent, new PhysShapeCircle(0.35f, Vector2.Zero), "fix1");
return ent;
}

private Entity<MapGridComponent> SetupGrid(MapId mapId, SharedMapSystem mapSystem, IEntityManager entManager, IMapManager mapManager)
{
mapSystem.CreateMap(mapId);
var grid = mapManager.CreateGridEntity(mapId);
entManager.System<SharedTransformSystem>().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<IEntitySystemManager>().GetEntitySystem<EntityLookupSystem>();
var entManager = server.Resolve<IEntityManager>();
var mapManager = server.Resolve<IMapManager>();

entManager.System<SharedMapSystem>().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<IEntitySystemManager>().GetEntitySystem<EntityLookupSystem>();
var entManager = server.Resolve<IEntityManager>();
var mapManager = server.Resolve<IMapManager>();
var mapSystem = entManager.System<SharedMapSystem>();

mapSystem.CreateMap(spawnPos.MapId);
var grid = mapManager.CreateGridEntity(spawnPos.MapId);
entManager.System<SharedTransformSystem>().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<IEntitySystemManager>().GetEntitySystem<EntityLookupSystem>();
var entManager = server.Resolve<IEntityManager>();
var mapManager = server.Resolve<IMapManager>();

entManager.System<SharedMapSystem>().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);
}

/// <summary>
/// Tests Box2 local queries for a particular lookup ID.
/// </summary>
[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<IEntitySystemManager>().GetEntitySystem<EntityLookupSystem>();
var entManager = server.Resolve<IEntityManager>();
var mapManager = server.Resolve<IMapManager>();
var mapSystem = entManager.System<SharedMapSystem>();

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

GetPhysicsEntity(entManager, spawnPos);
var entities = new HashSet<EntityUid>();
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<IEntitySystemManager>().GetEntitySystem<EntityLookupSystem>();
var entManager = server.Resolve<IEntityManager>();
var mapManager = server.Resolve<IMapManager>();
var mapSystem = entManager.System<SharedMapSystem>();

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()
{
Expand All @@ -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);
}
Expand Down

0 comments on commit 78f4d6a

Please sign in to comment.