From a215a2202c5f12cb9484b7a240a137816b0da28a Mon Sep 17 00:00:00 2001 From: FN Date: Sun, 12 May 2024 17:09:03 +0700 Subject: [PATCH 1/2] Update my first commit --- Content.Server/NPC/HTN/HTNSystem.cs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index e8a1ff4f48f..dd5dca52dc2 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -11,6 +11,7 @@ using Content.Shared.Mobs; using Content.Shared.NPC; using JetBrains.Annotations; +using Robust.Server.GameObjects; using Robust.Shared.CPUJob.JobQueues; using Robust.Shared.CPUJob.JobQueues.Queues; using Robust.Shared.Player; @@ -26,10 +27,10 @@ public sealed class HTNSystem : EntitySystem [Dependency] private readonly NPCSystem _npc = default!; [Dependency] private readonly NPCUtilitySystem _utility = default!; [Dependency] private readonly WorldControllerSystem _world = default!; + [Dependency] private readonly TransformSystem _transform = default!; - private EntityQuery _xformQuery; - private EntityQuery _loadedQuery; private EntityQuery _mapQuery; + private EntityQuery _loadedQuery; private readonly JobQueue _planQueue = new(0.004); @@ -39,9 +40,8 @@ public sealed class HTNSystem : EntitySystem public override void Initialize() { base.Initialize(); - _xformQuery = GetEntityQuery(); - _loadedQuery = GetEntityQuery(); _mapQuery = GetEntityQuery(); + _loadedQuery = GetEntityQuery(); SubscribeLocalEvent(_npc.OnMobStateChange); SubscribeLocalEvent(_npc.OnNPCMapInit); SubscribeLocalEvent(_npc.OnPlayerNPCAttach); @@ -158,13 +158,13 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) _planQueue.Process(); var query = EntityQueryEnumerator(); - while(query.MoveNext(out var uid, out _, out var comp)) + while (query.MoveNext(out var uid, out _, out var comp)) { // If we're over our max count or it's not MapInit then ignore the NPC. if (count >= maxUpdates) break; - if (!IsNPCActive(comp)) + if (!IsNPCActive(uid)) continue; if (comp.PlanningJob != null) @@ -255,12 +255,14 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) } } - private bool IsNPCActive(HTNComponent component) + private bool IsNPCActive(EntityUid entity) { - if (!_xformQuery.TryGetComponent(component.Owner, out TransformComponent? xform) || !_mapQuery.TryGetComponent(xform.MapUid, out var worldComponent)) + var transform = Transform(entity); + + if (!_mapQuery.TryGetComponent(transform.MapUid, out var worldComponent)) return true; - return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(xform.WorldPosition).Floored(), xform.MapUid.Value, worldComponent)); + return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(_transform.GetWorldPosition(transform)).Floored(), transform.MapUid.Value, worldComponent)); } private void AppendDebugText(HTNTask task, StringBuilder text, List planBtr, List btr, ref int level) From 8a92512226faebe192d1d68c5b6da8e05518e976 Mon Sep 17 00:00:00 2001 From: FN Date: Sun, 12 May 2024 20:23:58 +0700 Subject: [PATCH 2/2] Fix loader check --- Content.Server/NPC/HTN/HTNSystem.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index dd5dca52dc2..2c0a32c72b5 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -262,7 +262,9 @@ private bool IsNPCActive(EntityUid entity) if (!_mapQuery.TryGetComponent(transform.MapUid, out var worldComponent)) return true; - return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(_transform.GetWorldPosition(transform)).Floored(), transform.MapUid.Value, worldComponent)); + var chunk = _world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(_transform.GetWorldPosition(transform)).Floored(), transform.MapUid.Value, worldComponent); + + return _loadedQuery.TryGetComponent(chunk, out var loaded) && loaded.Loaders is not null; } private void AppendDebugText(HTNTask task, StringBuilder text, List planBtr, List btr, ref int level)