Split entity lookups from entity manager (#3747)

* Split entity lookups from entity manager

* IoC instead

* IoC refactor

* Fix bad resolve

* Remove EntityManager EntityLookup

* Update submodule

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2021-04-06 13:31:07 +10:00
committed by GitHub
parent 677706b117
commit 67f9e9cb5e
24 changed files with 50 additions and 44 deletions

View File

@@ -372,7 +372,7 @@ namespace Content.Client.GameObjects.EntitySystems
// TODO: Duplicated in SpriteSystem
var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition).Position;
var bounds = new Box2(mousePos - 1.5f, mousePos + 1.5f);
var pvsEntities = EntityManager.GetEntitiesIntersecting(_eyeManager.CurrentMap, bounds, true);
var pvsEntities = IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(_eyeManager.CurrentMap, bounds, true);
foreach (var pvsEntity in pvsEntities)
{
if (!pvsEntity.TryGetComponent(out ISpriteComponent? inRangeSprite) ||

View File

@@ -127,7 +127,7 @@ namespace Content.Client.State
public IList<IEntity> GetEntitiesUnderPosition(MapCoordinates coordinates)
{
// Find all the entities intersecting our click
var entities = EntityManager.GetEntitiesIntersecting(coordinates.MapId,
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(coordinates.MapId,
Box2.CenteredAround(coordinates.Position, (1, 1)));
// Check the entities against whether or not we can click them

View File

@@ -68,7 +68,7 @@ namespace Content.IntegrationTests.Tests.Destructible
Assert.That(spawnEntitiesBehavior.Spawn.Keys.Single(), Is.EqualTo(SpawnedEntityId));
Assert.That(spawnEntitiesBehavior.Spawn.Values.Single(), Is.EqualTo(new MinMax {Min = 1, Max = 1}));
var entitiesInRange = sEntityManager.GetEntitiesInRange(coordinates, 2);
var entitiesInRange = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(coordinates, 2);
var found = false;
foreach (var entity in entitiesInRange)

View File

@@ -5,6 +5,8 @@ using Content.Shared.Actions;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.Utility;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -26,8 +28,7 @@ namespace Content.Server.Actions
if (!args.Performer.TryGetComponent<SharedActionsComponent>(out var actions)) return;
// find all IGhostBooAffected nearby and do boo on them
var entityMan = args.Performer.EntityManager;
var ents = entityMan.GetEntitiesInRange(args.Performer, _radius, false);
var ents = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(args.Performer, _radius, false);
var booCounter = 0;
foreach (var ent in ents)

View File

@@ -95,7 +95,7 @@ namespace Content.Server.Commands.Chat
}
}
// Get all entities in range of the suicider
var entities = owner.EntityManager.GetEntitiesInRange(owner, 1, true).ToArray();
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(owner, 1, true).ToArray();
if (entities.Length > 0)
{

View File

@@ -6,6 +6,7 @@ using Content.Shared.Administration;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Commands.Interactable
@@ -43,8 +44,7 @@ namespace Content.Server.Commands.Interactable
return;
}
var serverEntityManager = IoCManager.Resolve<IServerEntityManager>();
var entities = serverEntityManager.GetEntitiesInRange(player.AttachedEntity, radius).ToList();
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(player.AttachedEntity, radius).ToList();
foreach (var entity in entities)
{

View File

@@ -6,6 +6,7 @@ using Content.Shared.Administration;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Commands.Interactable
@@ -43,8 +44,7 @@ namespace Content.Server.Commands.Interactable
return;
}
var serverEntityManager = IoCManager.Resolve<IServerEntityManager>();
var entities = serverEntityManager.GetEntitiesInRange(player.AttachedEntity, radius).ToList();
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(player.AttachedEntity, radius).ToList();
foreach (var entity in entities)
{

View File

@@ -44,7 +44,7 @@ namespace Content.Server.Construction.Conditions
var type = _componentFactory.GetRegistration(Component).Type;
var indices = entity.Transform.Coordinates.ToVector2i(entity.EntityManager, _mapManager);
var entities = indices.GetEntitiesInTile(entity.Transform.GridID, true, entity.EntityManager);
var entities = indices.GetEntitiesInTile(entity.Transform.GridID, true, IoCManager.Resolve<IEntityLookup>());
foreach (var ent in entities)
{

View File

@@ -70,12 +70,10 @@ namespace Content.Server.Explosions
MapId mapId)
{
var entityManager = IoCManager.Resolve<IEntityManager>();
var serverEntityManager = IoCManager.Resolve<IServerEntityManager>();
var entitySystemManager = IoCManager.Resolve<IEntitySystemManager>();
var exAct = entitySystemManager.GetEntitySystem<ActSystem>();
var exAct = EntitySystem.Get<ActSystem>();
var entitiesInRange = serverEntityManager.GetEntitiesInRange(mapId, boundingBox, 0).ToList();
var entitiesInRange = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(mapId, boundingBox, 0).ToList();
var impassableEntities = new List<Tuple<IEntity, float>>();
var nonImpassableEntities = new List<Tuple<IEntity, float>>();

View File

@@ -116,7 +116,7 @@ namespace Content.Server.GameObjects.Components.Fluids
PuddleComponent? puddle = null;
var spilt = false;
var spillEntities = entityManager.GetEntitiesIntersecting(mapGrid.ParentMapId, spillGridCoords.Position).ToArray();
var spillEntities = IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(mapGrid.ParentMapId, spillGridCoords.Position).ToArray();
foreach (var spillEntity in spillEntities)
{
if (spillEntity.TryGetComponent(out ISolutionInteractionsComponent? solutionContainerComponent) &&

View File

@@ -19,6 +19,7 @@ using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
@@ -498,7 +499,7 @@ namespace Content.Server.GameObjects.Components.Items.Storage
if(_areaInsert && (eventArgs.Target == null || !eventArgs.Target.HasComponent<SharedItemComponent>()))
{
var validStorables = new List<IEntity>();
foreach (var entity in Owner.EntityManager.GetEntitiesInRange(eventArgs.ClickLocation, 1))
foreach (var entity in IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(eventArgs.ClickLocation, 1))
{
if (!entity.Transform.IsMapTransform
|| entity == eventArgs.User

View File

@@ -94,7 +94,7 @@ namespace Content.Server.GameObjects.Components.Portal
}
if (_avoidCollidable)
{
foreach (var entity in _serverEntityManager.GetEntitiesIntersecting(mapCoords))
foreach (var entity in IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(mapCoords))
{
// Added this component to avoid stacking portals and causing shenanigans
// TODO: Doesn't do a great job of stopping stacking portals for directed
@@ -139,7 +139,7 @@ namespace Content.Server.GameObjects.Components.Portal
private bool EmptySpace(IEntity user, Vector2 target)
{
// TODO: Check the user's spot? Upside is no stacking TPs but downside is they can't unstuck themselves from walls.
foreach (var entity in _serverEntityManager.GetEntitiesIntersecting(user.Transform.MapID, target))
foreach (var entity in IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(user.Transform.MapID, target))
{
if (entity.HasComponent<IPhysBody>() || entity.HasComponent<PortalComponent>())
{

View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -101,7 +102,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
private List<PowerReceiverComponent> FindAvailableReceivers()
{
var nearbyEntities = Owner.EntityManager
var nearbyEntities = IoCManager.Resolve<IEntityLookup>()
.GetEntitiesInRange(Owner, PowerTransferRange);
var receivers = new List<PowerReceiverComponent>();

View File

@@ -7,9 +7,7 @@ using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -22,8 +20,6 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
[RegisterComponent]
public class PowerReceiverComponent : Component, IExamine
{
[Dependency] private readonly IServerEntityManager _serverEntityManager = default!;
[ViewVariables] [ComponentDependency] private readonly IPhysBody? _physicsComponent = null;
public override string Name => "PowerReceiver";
@@ -131,7 +127,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
private bool TryFindAvailableProvider(out IPowerProvider foundProvider)
{
var nearbyEntities = _serverEntityManager
var nearbyEntities = IoCManager.Resolve<IEntityLookup>()
.GetEntitiesInRange(Owner, PowerReceptionRange);
foreach (var entity in nearbyEntities)
@@ -140,7 +136,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
if (provider.Connectable)
{
if (provider.Owner.Transform.Coordinates.TryDistance(_serverEntityManager, Owner.Transform.Coordinates, out var distance))
if (provider.Owner.Transform.Coordinates.TryDistance(Owner.EntityManager, Owner.Transform.Coordinates, out var distance))
{
if (distance < Math.Min(PowerReceptionRange, provider.PowerTransferRange))
{

View File

@@ -34,7 +34,7 @@ namespace Content.Server.GameObjects.Components.Weapon
public static void FlashAreaHelper(IEntity source, float range, float duration, string? sound = null)
{
foreach (var entity in source.EntityManager.GetEntitiesInRange(source.Transform.Coordinates, range))
foreach (var entity in IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(source.Transform.Coordinates, range))
{
if (!entity.TryGetComponent(out FlashableComponent? flashable) ||
!source.InRangeUnobstructed(entity, range, CollisionGroup.Opaque)) continue;

View File

@@ -7,6 +7,7 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -69,7 +70,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
return false;
}
foreach (var entity in Owner.EntityManager.GetEntitiesInRange(Owner.Transform.Coordinates, _range))
foreach (var entity in IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(Owner.Transform.Coordinates, _range))
{
Flash(entity, eventArgs.User, _aoeFlashDuration);
}

View File

@@ -80,7 +80,7 @@ namespace Content.Server.GameObjects.EntitySystems
}
}
foreach (var near in EntityManager.GetEntitiesInRange(user!, 2f, true))
foreach (var near in IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(user!, 2f, true))
{
yield return near;
}

View File

@@ -2,6 +2,7 @@ using Content.Server.GameObjects.Components.StationEvents;
using Content.Shared.Interfaces.GameObjects.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.GameObjects.EntitySystems.StationEvents
@@ -40,6 +41,8 @@ namespace Content.Server.GameObjects.EntitySystems.StationEvents
{
base.Update(frameTime);
var lookupSystem = IoCManager.Resolve<IEntityLookup>();
foreach (var comp in ComponentManager.EntityQuery<RadiationPulseComponent>(true))
{
comp.Update(frameTime);
@@ -47,7 +50,7 @@ namespace Content.Server.GameObjects.EntitySystems.StationEvents
if (ent.Deleted) continue;
foreach (var entity in EntityManager.GetEntitiesInRange(ent.Transform.Coordinates, comp.Range, true))
foreach (var entity in lookupSystem.GetEntitiesInRange(ent.Transform.Coordinates, comp.Range, true))
{
if (entity.Deleted) continue;

View File

@@ -5,6 +5,7 @@ using Content.Server.GameObjects.Components.Conveyor;
using Content.Server.GameObjects.Components.Recycling;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Controllers;
@@ -39,7 +40,7 @@ namespace Content.Server.Physics.Controllers
return;
}
var intersecting = EntityManager.GetEntitiesIntersecting(comp.Owner, true);
var intersecting = IoCManager.Resolve<IEntityLookup>().GetEntitiesIntersecting(comp.Owner, true);
var direction = comp.GetAngle().ToVec();
Vector2? ownerPos = null;
@@ -99,11 +100,12 @@ namespace Content.Server.Physics.Controllers
var direction = Vector2.UnitX;
Vector2? ownerPos = null;
// TODO: I know it sucks but conveyors need a refactor
for (var i = comp.Intersecting.Count - 1; i >= 0; i--)
{
var entity = comp.Intersecting[i];
if (entity.Deleted || !comp.CanMove(entity) || !EntityManager.IsIntersecting(comp.Owner, entity))
if (entity.Deleted || !comp.CanMove(entity) || !IoCManager.Resolve<IEntityLookup>().IsIntersecting(comp.Owner, entity))
{
comp.Intersecting.RemoveAt(i);
continue;

View File

@@ -74,7 +74,7 @@ namespace Content.Server.Physics.Controllers
{
var singularityCoords = component.Owner.Transform.Coordinates;
// TODO: Maybe if we have named fixtures needs to pull out the outer circle collider (inner will be for deleting).
var entitiesToPull = EntityManager.GetEntitiesInRange(singularityCoords, component.Level * 10);
var entitiesToPull = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(singularityCoords, component.Level * 10);
foreach (var entity in entitiesToPull)
{
if (!entity.TryGetComponent<PhysicsComponent>(out var collidableComponent) || collidableComponent.BodyType == BodyType.Static) continue;

View File

@@ -7,6 +7,7 @@ using Content.Shared.GameObjects.Components.Storage;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -102,11 +103,12 @@ namespace Content.Shared.GameObjects.Components.Disposal
{
if(_intersecting.Count == 0) return;
// TODO: Yeah look this sucks but we'll fix it someday.
for (var i = _intersecting.Count - 1; i >= 0; i--)
{
var entity = _intersecting[i];
if (!Owner.EntityManager.IsIntersecting(entity, Owner))
if (IoCManager.Resolve<IEntityLookup>().IsIntersecting(entity, Owner))
_intersecting.RemoveAt(i);
}
}

View File

@@ -5,6 +5,7 @@ using System.Linq;
using Content.Shared.Physics;
using Content.Shared.Utility;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -25,8 +26,8 @@ namespace Content.Shared.GameObjects.Verbs
contextEntities = null;
var length = buffer ? 1.0f: 0.5f;
var entities = EntityManager.GetEntitiesIntersecting(targetPos.MapId,
Box2.CenteredAround(targetPos.Position, (length, length))).ToList();
var entities = IoCManager.Resolve<IEntityLookup>().
GetEntitiesIntersecting(targetPos.MapId, Box2.CenteredAround(targetPos.Position, (length, length))).ToList();
if (entities.Count == 0)
{

View File

@@ -149,32 +149,32 @@ namespace Content.Shared.Maps
/// Helper that returns all entities in a turf.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IEntity> GetEntitiesInTile(this TileRef turf, bool approximate = false, IEntityManager? entityManager = null)
public static IEnumerable<IEntity> GetEntitiesInTile(this TileRef turf, bool approximate = false, IEntityLookup? lookupSystem = null)
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
lookupSystem ??= IoCManager.Resolve<IEntityLookup>();
return entityManager.GetEntitiesIntersecting(turf.MapIndex, GetWorldTileBox(turf), approximate);
return lookupSystem.GetEntitiesIntersecting(turf.MapIndex, GetWorldTileBox(turf), approximate);
}
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
public static IEnumerable<IEntity> GetEntitiesInTile(this EntityCoordinates coordinates, bool approximate = false, IEntityManager? entityManager = null)
public static IEnumerable<IEntity> GetEntitiesInTile(this EntityCoordinates coordinates, bool approximate = false, IEntityLookup? lookupSystem = null)
{
var turf = coordinates.GetTileRef();
if (turf == null)
return Enumerable.Empty<IEntity>();
return GetEntitiesInTile(turf.Value, approximate, entityManager);
return GetEntitiesInTile(turf.Value, approximate, lookupSystem);
}
/// <summary>
/// Helper that returns all entities in a turf.
/// </summary>
public static IEnumerable<IEntity> GetEntitiesInTile(this Vector2i indices, GridId gridId, bool approximate = false, IEntityManager? entityManager = null)
public static IEnumerable<IEntity> GetEntitiesInTile(this Vector2i indices, GridId gridId, bool approximate = false, IEntityLookup? lookupSystem = null)
{
return GetEntitiesInTile(indices.GetTileRef(gridId), approximate, entityManager);
return GetEntitiesInTile(indices.GetTileRef(gridId), approximate, lookupSystem);
}
/// <summary>