From 7eaa5a81c3a5eeb4c6e88b2b03b8b1cf450d9565 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 6 Dec 2023 18:30:57 +1100 Subject: [PATCH] Pool NPC entitylookup fields (#21806) --- .../NPC/Systems/NPCSteeringSystem.Context.cs | 17 +++++++++++------ Content.Server/NPC/Systems/NPCSteeringSystem.cs | 8 ++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs b/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs index 6507f24edf..89e3e64e9f 100644 --- a/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs +++ b/Content.Server/NPC/Systems/NPCSteeringSystem.Context.cs @@ -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 diff --git a/Content.Server/NPC/Systems/NPCSteeringSystem.cs b/Content.Server/NPC/Systems/NPCSteeringSystem.cs index e5b62acfe8..3db7c58674 100644 --- a/Content.Server/NPC/Systems/NPCSteeringSystem.cs +++ b/Content.Server/NPC/Systems/NPCSteeringSystem.cs @@ -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 _fixturesQuery; private EntityQuery _modifierQuery; @@ -67,6 +68,9 @@ public sealed partial class NPCSteeringSystem : SharedNPCSteeringSystem private EntityQuery _physicsQuery; private EntityQuery _xformQuery; + private ObjectPool> _entSetPool = + new DefaultObjectPool>(new SetPolicy()); + /// /// Enabled antistuck detection so if an NPC is in the same spot for a while it will re-path. ///