diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index f0a0a13f33..5039419f9e 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -61,6 +61,20 @@ public sealed class HTNSystem : EntitySystem private void OnLoad() { + // Clear all NPCs in case they're hanging onto stale tasks + foreach (var comp in EntityQuery(true)) + { + comp.PlanningToken?.Cancel(); + comp.PlanningToken = null; + + if (comp.Plan != null) + { + var currentOperator = comp.Plan.CurrentOperator; + currentOperator.Shutdown(comp.Blackboard, HTNOperatorStatus.Failed); + comp.Plan = null; + } + } + // Add dependencies for all operators. // We put code on operators as I couldn't think of a clean way to put it on systems. foreach (var compound in _prototypeManager.EnumeratePrototypes()) diff --git a/Content.Server/NPC/Systems/NPCSteeringSystem.cs b/Content.Server/NPC/Systems/NPCSteeringSystem.cs index 3080ae28e6..a364556648 100644 --- a/Content.Server/NPC/Systems/NPCSteeringSystem.cs +++ b/Content.Server/NPC/Systems/NPCSteeringSystem.cs @@ -204,12 +204,19 @@ namespace Content.Server.NPC.Systems var npcs = EntityQuery() .ToArray(); - - foreach (var (steering, _, mover, xform) in npcs) + var options = new ParallelOptions { + MaxDegreeOfParallelism = _parallel.ParallelProcessCount, + }; + + Parallel.For(0, npcs.Length, options, i => + { + var (steering, _, mover, xform) = npcs[i]; + Steer(steering, mover, xform, modifierQuery, bodyQuery, xformQuery, frameTime); steering.LastSteer = mover.CurTickSprintMovement; - } + }); + if (_subscribedSessions.Count > 0) {