]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Pool NPC entitylookup fields (#21806)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Wed, 6 Dec 2023 07:30:57 +0000 (18:30 +1100)
committerGitHub <noreply@github.com>
Wed, 6 Dec 2023 07:30:57 +0000 (18:30 +1100)
Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs
Content.Server/NPC/Systems/NPCSteeringSystem.cs

index 6507f24edf648c2b4d83101961aed8d9ef1881a6..89e3e64e9fe7dac8930af7bfee244de6efa2b145 100644 (file)
@@ -469,12 +469,13 @@ public sealed partial class NPCSteeringSystem
     {
         var objectRadius = 0.15f;
         var detectionRadius = MathF.Max(0.35f, agentRadius + objectRadius);
+        var ents = _entSetPool.Get();
+        _lookup.GetEntitiesInRange(uid, detectionRadius, ents, LookupFlags.Static);
 
-        foreach (var ent in _lookup.GetEntitiesInRange(uid, detectionRadius, LookupFlags.Static))
+        foreach (var ent in ents)
         {
             // TODO: If we can access the door or smth.
-            if (ent == uid ||
-                !_physicsQuery.TryGetComponent(ent, out var otherBody) ||
+            if (!_physicsQuery.TryGetComponent(ent, out var otherBody) ||
                 !otherBody.Hard ||
                 !otherBody.CanCollide ||
                 (mask & otherBody.CollisionLayer) == 0x0 &&
@@ -521,6 +522,7 @@ public sealed partial class NPCSteeringSystem
             }
         }
 
+        _entSetPool.Return(ents);
     }
 
     #endregion
@@ -545,12 +547,13 @@ public sealed partial class NPCSteeringSystem
         var detectionRadius = MathF.Max(0.35f, agentRadius + objectRadius);
         var ourVelocity = body.LinearVelocity;
         _factionQuery.TryGetComponent(uid, out var ourFaction);
+        var ents = _entSetPool.Get();
+        _lookup.GetEntitiesInRange(uid, detectionRadius, ents, LookupFlags.Dynamic);
 
-        foreach (var ent in _lookup.GetEntitiesInRange(uid, detectionRadius, LookupFlags.Dynamic))
+        foreach (var ent in ents)
         {
             // TODO: If we can access the door or smth.
-            if (ent == uid ||
-                !_physicsQuery.TryGetComponent(ent, out var otherBody) ||
+            if (!_physicsQuery.TryGetComponent(ent, out var otherBody) ||
                 !otherBody.Hard ||
                 !otherBody.CanCollide ||
                 (mask & otherBody.CollisionLayer) == 0x0 &&
@@ -602,6 +605,8 @@ public sealed partial class NPCSteeringSystem
                 danger[i] = MathF.Max(dot * weight, danger[i]);
             }
         }
+
+        _entSetPool.Return(ents);
     }
 
     #endregion
index e5b62acfe80cb8c221d2c3b47e2ef173a1cdc0d0..3db7c586743dcccf4381b11ff079bdd9247938b8 100644 (file)
@@ -27,6 +27,8 @@ using Robust.Shared.Random;
 using Robust.Shared.Timing;
 using Robust.Shared.Utility;
 using Content.Shared.Prying.Systems;
+using Microsoft.Extensions.ObjectPool;
+using Robust.Shared.Threading;
 
 namespace Content.Server.NPC.Systems;
 
@@ -49,17 +51,16 @@ public sealed partial class NPCSteeringSystem : SharedNPCSteeringSystem
     [Dependency] private readonly IRobustRandom _random = default!;
     [Dependency] private readonly ClimbSystem _climb = default!;
     [Dependency] private readonly DoAfterSystem _doAfter = default!;
-    [Dependency] private readonly DoorSystem _doors = default!;
     [Dependency] private readonly EntityLookupSystem _lookup = default!;
     [Dependency] private readonly NpcFactionSystem _npcFaction = default!;
     [Dependency] private readonly PathfindingSystem _pathfindingSystem = default!;
+    [Dependency] private readonly PryingSystem _pryingSystem = default!;
     [Dependency] private readonly SharedInteractionSystem _interaction = default!;
     [Dependency] private readonly SharedMeleeWeaponSystem _melee = default!;
     [Dependency] private readonly SharedMoverController _mover = default!;
     [Dependency] private readonly SharedPhysicsSystem _physics = default!;
     [Dependency] private readonly SharedTransformSystem _transform = default!;
     [Dependency] private readonly SharedCombatModeSystem _combat = default!;
-    [Dependency] private readonly PryingSystem _pryingSystem = default!;
 
     private EntityQuery<FixturesComponent> _fixturesQuery;
     private EntityQuery<MovementSpeedModifierComponent> _modifierQuery;
@@ -67,6 +68,9 @@ public sealed partial class NPCSteeringSystem : SharedNPCSteeringSystem
     private EntityQuery<PhysicsComponent> _physicsQuery;
     private EntityQuery<TransformComponent> _xformQuery;
 
+    private ObjectPool<HashSet<EntityUid>> _entSetPool =
+        new DefaultObjectPool<HashSet<EntityUid>>(new SetPolicy<EntityUid>());
+
     /// <summary>
     /// Enabled antistuck detection so if an NPC is in the same spot for a while it will re-path.
     /// </summary>