Nuke gridtilelookup (#6030)

This commit is contained in:
metalgearsloth
2022-01-09 23:47:01 +11:00
committed by GitHub
parent e648ad72b9
commit 64de4bbed9
8 changed files with 22 additions and 125 deletions

View File

@@ -1,70 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.IntegrationTests.Tests
{
[TestFixture]
public class GridTileLookupTest : ContentIntegrationTest
{
private const string Prototypes = @"
- type: entity
name: Dummy
id: Dummy
";
[Test]
public async Task Test()
{
var options = new ServerIntegrationOptions{ExtraPrototypes = Prototypes};
var server = StartServer(options);
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var tileLookup = server.ResolveDependency<IEntitySystemManager>().GetEntitySystem<GridTileLookupSystem>();
var mapManager = server.ResolveDependency<IMapManager>();
var tileDefinitionManager = server.ResolveDependency<ITileDefinitionManager>();
server.Assert(() =>
{
List<EntityUid> entities;
var mapOne = mapManager.CreateMap();
var gridOne = mapManager.CreateGrid(mapOne);
var tileDefinition = tileDefinitionManager["underplating"];
var underplating = new Tile(tileDefinition.TileId);
gridOne.SetTile(new Vector2i(0, 0), underplating);
gridOne.SetTile(new Vector2i(-1, -1), underplating);
entities = tileLookup.GetEntitiesIntersecting(gridOne.Index, new Vector2i(0, 0)).ToList();
Assert.That(entities.Count, Is.EqualTo(0));
// Space entity, check that nothing intersects it and that also it doesn't throw.
entityManager.SpawnEntity("Dummy", new MapCoordinates(Vector2.One * 1000, mapOne));
entities = tileLookup.GetEntitiesIntersecting(gridOne.Index, new Vector2i(1000, 1000)).ToList();
Assert.That(entities.Count, Is.EqualTo(0));
var entityOne = entityManager.SpawnEntity("Dummy", new EntityCoordinates(gridOne.GridEntityId, Vector2.Zero));
entityManager.SpawnEntity("Dummy", new EntityCoordinates(gridOne.GridEntityId, Vector2.One));
var entityTiles = tileLookup.GetIndices(entityOne);
Assert.That(entityTiles.Count, Is.EqualTo(1));
entities = tileLookup.GetEntitiesIntersecting(entityOne).ToList();
Assert.That(entities.Count, Is.EqualTo(1));
entityManager.SpawnEntity("Dummy", new EntityCoordinates(gridOne.GridEntityId, Vector2.Zero));
entities = tileLookup.GetEntitiesIntersecting(gridOne.Index, new Vector2i(0, 0)).ToList();
Assert.That(entities.Count, Is.EqualTo(2));
});
await server.WaitIdleAsync();
}
}
}

View File

@@ -31,7 +31,7 @@ namespace Content.Server.Atmos.EntitySystems
}
}
foreach (var entity in _gridtileLookupSystem.GetEntitiesIntersecting(tile.GridIndex, tile.GridIndices))
foreach (var entity in _lookup.GetEntitiesIntersecting(tile.GridIndex, tile.GridIndices))
{
if (!HasComp<IPhysBody>(entity)
|| !entity.IsMovedByPressure(out var pressure)

View File

@@ -9,7 +9,7 @@ namespace Content.Server.Atmos.EntitySystems
{
public partial class AtmosphereSystem
{
[Dependency] private readonly GridTileLookupSystem _gridtileLookupSystem = default!;
[Dependency] private readonly IEntityLookup _lookup = default!;
private void ProcessHotspot(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile)
{
@@ -142,7 +142,7 @@ namespace Content.Server.Atmos.EntitySystems
var fireEvent = new TileFireEvent(tile.Hotspot.Temperature, tile.Hotspot.Volume);
foreach (var entity in _gridtileLookupSystem.GetEntitiesIntersecting(tile.GridIndex, tile.GridIndices))
foreach (var entity in _lookup.GetEntitiesIntersecting(tile.GridIndex, tile.GridIndices))
{
RaiseLocalEvent(entity, fireEvent, false);
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Coordinates.Helpers;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
@@ -155,6 +156,7 @@ namespace Content.Server.Cargo.Components
}
case CargoConsoleShuttleMessage _:
{
// Jesus fucking christ Glass
//var approvedOrders = _cargoOrderDataManager.RemoveAndGetApprovedFrom(orders.Database);
//orders.Database.ClearOrderCapacity();
@@ -164,23 +166,20 @@ namespace Content.Server.Cargo.Components
var indices = _entMan.GetComponent<TransformComponent>(Owner).Coordinates.ToVector2i(_entMan, _mapManager);
var offsets = new Vector2i[] { new Vector2i(0, 1), new Vector2i(1, 1), new Vector2i(1, 0), new Vector2i(1, -1),
new Vector2i(0, -1), new Vector2i(-1, -1), new Vector2i(-1, 0), new Vector2i(-1, 1), };
var adjacentEntities = new List<IEnumerable<EntityUid>>(); //Probably better than IEnumerable.concat
foreach (var offset in offsets)
{
adjacentEntities.Add((indices+offset).GetEntitiesInTileFast(_entMan.GetComponent<TransformComponent>(Owner).GridID));
}
foreach (var enumerator in adjacentEntities)
var lookup = IoCManager.Resolve<IEntityLookup>();
var gridId = _entMan.GetComponent<TransformComponent>(Owner).GridID;
// TODO: Should use anchoring.
foreach (var entity in lookup.GetEntitiesIntersecting(gridId, offsets.Select(o => o + indices)))
{
foreach (EntityUid entity in enumerator)
if (_entMan.HasComponent<CargoTelepadComponent>(entity) && _entMan.TryGetComponent<ApcPowerReceiverComponent?>(entity, out var powerReceiver) && powerReceiver.Powered)
{
if (_entMan.HasComponent<CargoTelepadComponent>(entity) && _entMan.TryGetComponent<ApcPowerReceiverComponent?>(entity, out var powerReceiver) && powerReceiver.Powered)
{
cargoTelepad = entity;
break;
}
cargoTelepad = entity;
break;
}
}
if (cargoTelepad != null)
{
if (_entMan.TryGetComponent<CargoTelepadComponent?>(cargoTelepad.Value, out var telepadComponent))

View File

@@ -136,8 +136,10 @@ namespace Content.Server.Chemistry.Components
return;
var chemistry = EntitySystem.Get<ReactiveSystem>();
var mapGrid = MapManager.GetGrid(_entities.GetComponent<TransformComponent>(Owner).GridID);
var tile = mapGrid.GetTileRef(_entities.GetComponent<TransformComponent>(Owner).Coordinates.ToVector2i(_entities, MapManager));
var xform = _entities.GetComponent<TransformComponent>(Owner);
var mapGrid = MapManager.GetGrid(xform.GridID);
var tile = mapGrid.GetTileRef(xform.Coordinates.ToVector2i(_entities, MapManager));
var lookup = IoCManager.Resolve<IEntityLookup>();
var solutionFraction = 1 / Math.Floor(averageExposures);
@@ -155,14 +157,14 @@ namespace Content.Server.Chemistry.Components
}
// Touch every entity on the tile
foreach (var entity in tile.GetEntitiesInTileFast().ToArray())
foreach (var entity in lookup.GetEntitiesIntersecting(tile).ToArray())
{
chemistry.ReactionEntity(entity, ReactionMethod.Touch, reagent,
reagentQuantity.Quantity * solutionFraction, solution);
}
}
foreach (var entity in tile.GetEntitiesInTileFast().ToArray())
foreach (var entity in lookup.GetEntitiesIntersecting(tile).ToArray())
{
ReactWithEntity(entity, solutionFraction);
}

View File

@@ -22,7 +22,7 @@ namespace Content.Server.Chemistry.TileReactions
FixedPoint2 ITileReaction.TileReact(TileRef tile, ReagentPrototype reagent, FixedPoint2 reactVolume)
{
var entities = tile.GetEntitiesInTileFast().ToArray();
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(tile).ToArray();
var amount = FixedPoint2.Zero;
var entMan = IoCManager.Resolve<IEntityManager>();
foreach (var entity in entities)

View File

@@ -1,33 +0,0 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Server.Coordinates.Helpers
{
public static class GridTileLookupHelpers
{
/// <summary>
/// Helper that returns all entities in a turf very fast.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<EntityUid> GetEntitiesInTileFast(this TileRef turf, GridTileLookupSystem? gridTileLookup = null)
{
gridTileLookup ??= EntitySystem.Get<GridTileLookupSystem>();
return gridTileLookup.GetEntitiesIntersecting(turf.GridIndex, turf.GridIndices);
}
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<EntityUid> GetEntitiesInTileFast(this Vector2i indices, GridId gridId, GridTileLookupSystem? gridTileLookup = null)
{
gridTileLookup ??= EntitySystem.Get<GridTileLookupSystem>();
return gridTileLookup.GetEntitiesIntersecting(gridId, indices);
}
}
}

View File

@@ -28,7 +28,6 @@ public class SpillableSystem : EntitySystem
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityLookup _entityLookup = default!;
[Dependency] private readonly GridTileLookupSystem _gridTileLookupSystem = default!;
[Dependency] private readonly AdminLogSystem _logSystem = default!;
public override void Initialize()
@@ -121,7 +120,7 @@ public class SpillableSystem : EntitySystem
public bool TryGetPuddle(TileRef tileRef, [NotNullWhen(true)] out PuddleComponent? puddle)
{
foreach (var entity in tileRef.GetEntitiesInTileFast(_gridTileLookupSystem))
foreach (var entity in _entityLookup.GetEntitiesIntersecting(tileRef))
{
if (EntityManager.TryGetComponent(entity, out PuddleComponent? p))
{