diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index cfa670e1445..e8a1ff4f48f 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -2,14 +2,17 @@ using System.Text; using System.Threading; using Content.Server.Administration.Managers; -using Robust.Shared.CPUJob.JobQueues; -using Robust.Shared.CPUJob.JobQueues.Queues; using Content.Server.NPC.HTN.PrimitiveTasks; using Content.Server.NPC.Systems; +using Content.Server.Worldgen; +using Content.Server.Worldgen.Components; +using Content.Server.Worldgen.Systems; using Content.Shared.Administration; using Content.Shared.Mobs; using Content.Shared.NPC; using JetBrains.Annotations; +using Robust.Shared.CPUJob.JobQueues; +using Robust.Shared.CPUJob.JobQueues.Queues; using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Utility; @@ -22,6 +25,11 @@ public sealed class HTNSystem : EntitySystem [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly NPCSystem _npc = default!; [Dependency] private readonly NPCUtilitySystem _utility = default!; + [Dependency] private readonly WorldControllerSystem _world = default!; + + private EntityQuery _xformQuery; + private EntityQuery _loadedQuery; + private EntityQuery _mapQuery; private readonly JobQueue _planQueue = new(0.004); @@ -31,6 +39,9 @@ public sealed class HTNSystem : EntitySystem public override void Initialize() { base.Initialize(); + _xformQuery = GetEntityQuery(); + _loadedQuery = GetEntityQuery(); + _mapQuery = GetEntityQuery(); SubscribeLocalEvent(_npc.OnMobStateChange); SubscribeLocalEvent(_npc.OnNPCMapInit); SubscribeLocalEvent(_npc.OnPlayerNPCAttach); @@ -153,6 +164,9 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) if (count >= maxUpdates) break; + if (!IsNPCActive(comp)) + continue; + if (comp.PlanningJob != null) { if (comp.PlanningJob.Exception != null) @@ -241,6 +255,14 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) } } + private bool IsNPCActive(HTNComponent component) + { + if (!_xformQuery.TryGetComponent(component.Owner, out TransformComponent? xform) || !_mapQuery.TryGetComponent(xform.MapUid, out var worldComponent)) + return true; + + return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(xform.WorldPosition).Floored(), xform.MapUid.Value, worldComponent)); + } + private void AppendDebugText(HTNTask task, StringBuilder text, List planBtr, List btr, ref int level) { // If it's the selected BTR then highlight.