From 64de4bbed955ee655c9508c4427c52a80ffece9a Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sun, 9 Jan 2022 23:47:01 +1100 Subject: [PATCH] Nuke gridtilelookup (#6030) --- .../Tests/GridTileLookupTest.cs | 70 ------------------- .../AtmosphereSystem.HighPressureDelta.cs | 2 +- .../EntitySystems/AtmosphereSystem.Hotspot.cs | 4 +- .../Cargo/Components/CargoConsoleComponent.cs | 23 +++--- .../Components/SolutionAreaEffectComponent.cs | 10 +-- .../TileReactions/CleanTileReaction.cs | 2 +- .../Helpers/GridTileLookupHelpers.cs | 33 --------- .../Fluids/EntitySystems/SpillableSystem.cs | 3 +- 8 files changed, 22 insertions(+), 125 deletions(-) delete mode 100644 Content.IntegrationTests/Tests/GridTileLookupTest.cs delete mode 100644 Content.Server/Coordinates/Helpers/GridTileLookupHelpers.cs diff --git a/Content.IntegrationTests/Tests/GridTileLookupTest.cs b/Content.IntegrationTests/Tests/GridTileLookupTest.cs deleted file mode 100644 index 38d0f125be..0000000000 --- a/Content.IntegrationTests/Tests/GridTileLookupTest.cs +++ /dev/null @@ -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(); - var tileLookup = server.ResolveDependency().GetEntitySystem(); - var mapManager = server.ResolveDependency(); - var tileDefinitionManager = server.ResolveDependency(); - - server.Assert(() => - { - List 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(); - } - } -} diff --git a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.HighPressureDelta.cs b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.HighPressureDelta.cs index 7136b3a282..73056d60f0 100644 --- a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.HighPressureDelta.cs +++ b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.HighPressureDelta.cs @@ -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(entity) || !entity.IsMovedByPressure(out var pressure) diff --git a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Hotspot.cs b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Hotspot.cs index 07fdc654e4..7bb0c5baff 100644 --- a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Hotspot.cs +++ b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Hotspot.cs @@ -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); } diff --git a/Content.Server/Cargo/Components/CargoConsoleComponent.cs b/Content.Server/Cargo/Components/CargoConsoleComponent.cs index 7eee231ec9..c713623b78 100644 --- a/Content.Server/Cargo/Components/CargoConsoleComponent.cs +++ b/Content.Server/Cargo/Components/CargoConsoleComponent.cs @@ -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(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>(); //Probably better than IEnumerable.concat - foreach (var offset in offsets) - { - adjacentEntities.Add((indices+offset).GetEntitiesInTileFast(_entMan.GetComponent(Owner).GridID)); - } - foreach (var enumerator in adjacentEntities) + var lookup = IoCManager.Resolve(); + var gridId = _entMan.GetComponent(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(entity) && _entMan.TryGetComponent(entity, out var powerReceiver) && powerReceiver.Powered) { - if (_entMan.HasComponent(entity) && _entMan.TryGetComponent(entity, out var powerReceiver) && powerReceiver.Powered) - { - cargoTelepad = entity; - break; - } + cargoTelepad = entity; + break; } } + if (cargoTelepad != null) { if (_entMan.TryGetComponent(cargoTelepad.Value, out var telepadComponent)) diff --git a/Content.Server/Chemistry/Components/SolutionAreaEffectComponent.cs b/Content.Server/Chemistry/Components/SolutionAreaEffectComponent.cs index e7c7f7c66a..20ae64e6d9 100644 --- a/Content.Server/Chemistry/Components/SolutionAreaEffectComponent.cs +++ b/Content.Server/Chemistry/Components/SolutionAreaEffectComponent.cs @@ -136,8 +136,10 @@ namespace Content.Server.Chemistry.Components return; var chemistry = EntitySystem.Get(); - var mapGrid = MapManager.GetGrid(_entities.GetComponent(Owner).GridID); - var tile = mapGrid.GetTileRef(_entities.GetComponent(Owner).Coordinates.ToVector2i(_entities, MapManager)); + var xform = _entities.GetComponent(Owner); + var mapGrid = MapManager.GetGrid(xform.GridID); + var tile = mapGrid.GetTileRef(xform.Coordinates.ToVector2i(_entities, MapManager)); + var lookup = IoCManager.Resolve(); 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); } diff --git a/Content.Server/Chemistry/TileReactions/CleanTileReaction.cs b/Content.Server/Chemistry/TileReactions/CleanTileReaction.cs index 341a348341..6cf97d696f 100644 --- a/Content.Server/Chemistry/TileReactions/CleanTileReaction.cs +++ b/Content.Server/Chemistry/TileReactions/CleanTileReaction.cs @@ -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().GetEntitiesIntersecting(tile).ToArray(); var amount = FixedPoint2.Zero; var entMan = IoCManager.Resolve(); foreach (var entity in entities) diff --git a/Content.Server/Coordinates/Helpers/GridTileLookupHelpers.cs b/Content.Server/Coordinates/Helpers/GridTileLookupHelpers.cs deleted file mode 100644 index 6eb3297fbf..0000000000 --- a/Content.Server/Coordinates/Helpers/GridTileLookupHelpers.cs +++ /dev/null @@ -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 - { - /// - /// Helper that returns all entities in a turf very fast. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static IEnumerable GetEntitiesInTileFast(this TileRef turf, GridTileLookupSystem? gridTileLookup = null) - { - gridTileLookup ??= EntitySystem.Get(); - - return gridTileLookup.GetEntitiesIntersecting(turf.GridIndex, turf.GridIndices); - } - - /// - /// Helper that returns all entities in a turf. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static IEnumerable GetEntitiesInTileFast(this Vector2i indices, GridId gridId, GridTileLookupSystem? gridTileLookup = null) - { - gridTileLookup ??= EntitySystem.Get(); - return gridTileLookup.GetEntitiesIntersecting(gridId, indices); - } - } -} diff --git a/Content.Server/Fluids/EntitySystems/SpillableSystem.cs b/Content.Server/Fluids/EntitySystems/SpillableSystem.cs index a08868a2b9..1a99c6240e 100644 --- a/Content.Server/Fluids/EntitySystems/SpillableSystem.cs +++ b/Content.Server/Fluids/EntitySystems/SpillableSystem.cs @@ -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)) {