From 01382ee6e7cb0a408b0cd7e40d338815361a270f Mon Sep 17 00:00:00 2001 From: FN Date: Sat, 27 Apr 2024 14:06:56 +0700 Subject: [PATCH 1/6] Don't update HTN in unloaded chunks --- Content.Server/NPC/HTN/HTNSystem.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index cfa670e1445..facb3b93a72 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -13,6 +13,9 @@ using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Utility; +using Robust.Shared.Physics.Systems; +using Content.Server.Worldgen.Systems; +using Content.Server.Worldgen.Components; namespace Content.Server.NPC.HTN; @@ -22,6 +25,13 @@ 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 SharedPhysicsSystem _physics = default!; + //[Dependency] private readonly SharedTransformSystem _transform = default!; + [Dependency] private readonly BaseWorldSystem _world = default!; + + private EntityQuery _xformQuery; + + private EntityQuery _loadedQuery; private readonly JobQueue _planQueue = new(0.004); @@ -31,6 +41,8 @@ public sealed class HTNSystem : EntitySystem public override void Initialize() { base.Initialize(); + _xformQuery = GetEntityQuery(); + _loadedQuery = GetEntityQuery(); SubscribeLocalEvent(_npc.OnMobStateChange); SubscribeLocalEvent(_npc.OnNPCMapInit); SubscribeLocalEvent(_npc.OnPlayerNPCAttach); @@ -153,6 +165,9 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) if (count >= maxUpdates) break; + if (!IsNPCActive(comp)) + return; + if (comp.PlanningJob != null) { if (comp.PlanningJob.Exception != null) @@ -241,6 +256,18 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) } } + private bool IsNPCActive(HTNComponent component) + { + EntityUid entity = component.Owner; + + if (!_xformQuery.TryGetComponent(entity, out TransformComponent? xform)) + return true; + + /*return _physics.GetCollidingEntities(xform.MapID, Box2.CenteredAround(_transform.GetWorldPosition(xform), new(12))).Any(physicsComponent => physicsComponent.);*/ + + return _loadedQuery.HasComponent(_world.GetOrCreateChunk(_world.GetChunkCoords(entity, xform), xform.MapUid!.Value)); + } + private void AppendDebugText(HTNTask task, StringBuilder text, List planBtr, List btr, ref int level) { // If it's the selected BTR then highlight. From 47e215cbb229ae23e81fdb4829124cac8f364d63 Mon Sep 17 00:00:00 2001 From: FN Date: Sat, 27 Apr 2024 14:29:44 +0700 Subject: [PATCH 2/6] Fix wrong operator --- Content.Server/NPC/HTN/HTNSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index facb3b93a72..06ae9ad9372 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -166,7 +166,7 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) break; if (!IsNPCActive(comp)) - return; + continue; if (comp.PlanningJob != null) { From 963697052e2c48621d73983672ee72f3c8f52dfa Mon Sep 17 00:00:00 2001 From: FN Date: Sat, 27 Apr 2024 15:15:35 +0700 Subject: [PATCH 3/6] Remove BaseWorldSystem dependency --- Content.Server/NPC/HTN/HTNSystem.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index 06ae9ad9372..547fa9aa2c7 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -2,20 +2,20 @@ 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; -using Robust.Shared.Physics.Systems; -using Content.Server.Worldgen.Systems; -using Content.Server.Worldgen.Components; namespace Content.Server.NPC.HTN; @@ -27,7 +27,7 @@ public sealed class HTNSystem : EntitySystem [Dependency] private readonly NPCUtilitySystem _utility = default!; //[Dependency] private readonly SharedPhysicsSystem _physics = default!; //[Dependency] private readonly SharedTransformSystem _transform = default!; - [Dependency] private readonly BaseWorldSystem _world = default!; + [Dependency] private readonly WorldControllerSystem _world = default!; private EntityQuery _xformQuery; @@ -258,14 +258,12 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) private bool IsNPCActive(HTNComponent component) { - EntityUid entity = component.Owner; - - if (!_xformQuery.TryGetComponent(entity, out TransformComponent? xform)) + if (!_xformQuery.TryGetComponent(component.Owner, out TransformComponent? xform)) return true; /*return _physics.GetCollidingEntities(xform.MapID, Box2.CenteredAround(_transform.GetWorldPosition(xform), new(12))).Any(physicsComponent => physicsComponent.);*/ - return _loadedQuery.HasComponent(_world.GetOrCreateChunk(_world.GetChunkCoords(entity, xform), xform.MapUid!.Value)); + return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(xform.WorldPosition).Floored(), xform.MapUid!.Value)); } private void AppendDebugText(HTNTask task, StringBuilder text, List planBtr, List btr, ref int level) From c3aa1c6c44870e59f5aa0869be918fd0556135c0 Mon Sep 17 00:00:00 2001 From: FN Date: Sat, 27 Apr 2024 16:35:24 +0700 Subject: [PATCH 4/6] Check for MapComponent --- Content.Server/NPC/HTN/HTNSystem.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index 547fa9aa2c7..0a65549b802 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -13,6 +13,7 @@ using JetBrains.Annotations; using Robust.Shared.CPUJob.JobQueues; using Robust.Shared.CPUJob.JobQueues.Queues; +using Robust.Shared.Map.Components; using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Utility; @@ -30,8 +31,8 @@ public sealed class HTNSystem : EntitySystem [Dependency] private readonly WorldControllerSystem _world = default!; private EntityQuery _xformQuery; - private EntityQuery _loadedQuery; + private EntityQuery _mapQuery; private readonly JobQueue _planQueue = new(0.004); @@ -43,6 +44,7 @@ public override void Initialize() base.Initialize(); _xformQuery = GetEntityQuery(); _loadedQuery = GetEntityQuery(); + _mapQuery = GetEntityQuery(); SubscribeLocalEvent(_npc.OnMobStateChange); SubscribeLocalEvent(_npc.OnNPCMapInit); SubscribeLocalEvent(_npc.OnPlayerNPCAttach); @@ -263,6 +265,9 @@ private bool IsNPCActive(HTNComponent component) /*return _physics.GetCollidingEntities(xform.MapID, Box2.CenteredAround(_transform.GetWorldPosition(xform), new(12))).Any(physicsComponent => physicsComponent.);*/ + if (!_mapQuery.HasComponent(xform.MapUid)) + return true; + return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(xform.WorldPosition).Floored(), xform.MapUid!.Value)); } From 7823f9eb852b82207f0e9032fd651fd636329d9d Mon Sep 17 00:00:00 2001 From: FN Date: Sat, 27 Apr 2024 17:10:04 +0700 Subject: [PATCH 5/6] Check for WorldControllerComponent --- Content.Server/NPC/HTN/HTNSystem.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index 0a65549b802..acb0f22983d 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -13,7 +13,6 @@ using JetBrains.Annotations; using Robust.Shared.CPUJob.JobQueues; using Robust.Shared.CPUJob.JobQueues.Queues; -using Robust.Shared.Map.Components; using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Utility; @@ -32,7 +31,7 @@ public sealed class HTNSystem : EntitySystem private EntityQuery _xformQuery; private EntityQuery _loadedQuery; - private EntityQuery _mapQuery; + private EntityQuery _mapQuery; private readonly JobQueue _planQueue = new(0.004); @@ -44,7 +43,7 @@ public override void Initialize() base.Initialize(); _xformQuery = GetEntityQuery(); _loadedQuery = GetEntityQuery(); - _mapQuery = GetEntityQuery(); + _mapQuery = GetEntityQuery(); SubscribeLocalEvent(_npc.OnMobStateChange); SubscribeLocalEvent(_npc.OnNPCMapInit); SubscribeLocalEvent(_npc.OnPlayerNPCAttach); @@ -265,10 +264,10 @@ private bool IsNPCActive(HTNComponent component) /*return _physics.GetCollidingEntities(xform.MapID, Box2.CenteredAround(_transform.GetWorldPosition(xform), new(12))).Any(physicsComponent => physicsComponent.);*/ - if (!_mapQuery.HasComponent(xform.MapUid)) + if (!_mapQuery.TryGetComponent(xform.MapUid, out var worldComponent)) return true; - return _loadedQuery.HasComponent(_world.GetOrCreateChunk(WorldGen.WorldToChunkCoords(xform.WorldPosition).Floored(), xform.MapUid!.Value)); + 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) From 9f5d89b5f0e6e7fe6bccd7b909c62a4e6f8736b6 Mon Sep 17 00:00:00 2001 From: FN Date: Sat, 27 Apr 2024 17:40:39 +0700 Subject: [PATCH 6/6] Clean --- Content.Server/NPC/HTN/HTNSystem.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Content.Server/NPC/HTN/HTNSystem.cs b/Content.Server/NPC/HTN/HTNSystem.cs index acb0f22983d..e8a1ff4f48f 100644 --- a/Content.Server/NPC/HTN/HTNSystem.cs +++ b/Content.Server/NPC/HTN/HTNSystem.cs @@ -25,8 +25,6 @@ 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 SharedPhysicsSystem _physics = default!; - //[Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly WorldControllerSystem _world = default!; private EntityQuery _xformQuery; @@ -259,15 +257,10 @@ public void UpdateNPC(ref int count, int maxUpdates, float frameTime) private bool IsNPCActive(HTNComponent component) { - if (!_xformQuery.TryGetComponent(component.Owner, out TransformComponent? xform)) + if (!_xformQuery.TryGetComponent(component.Owner, out TransformComponent? xform) || !_mapQuery.TryGetComponent(xform.MapUid, out var worldComponent)) return true; - /*return _physics.GetCollidingEntities(xform.MapID, Box2.CenteredAround(_transform.GetWorldPosition(xform), new(12))).Any(physicsComponent => physicsComponent.);*/ - - if (!_mapQuery.TryGetComponent(xform.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(xform.WorldPosition).Floored(), xform.MapUid.Value, worldComponent)); } private void AppendDebugText(HTNTask task, StringBuilder text, List planBtr, List btr, ref int level)