From 700cb02d5e46ba6f5861e4076f3a0b28e399dc2e Mon Sep 17 00:00:00 2001 From: osjarw <62134478+osjarw@users.noreply.github.com> Date: Thu, 17 Apr 2025 15:42:31 +0300 Subject: [PATCH] Fix HTN task ordering bug (#32909) * first, bad ordering * fix * Revert "first, bad ordering", recording artifacts This reverts commit 22307675d79b417371775a0558c39781f59423d6. --- Content.Server/NPC/HTN/HTNPlanJob.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Content.Server/NPC/HTN/HTNPlanJob.cs b/Content.Server/NPC/HTN/HTNPlanJob.cs index 9c62f5840a..8a57d52a66 100644 --- a/Content.Server/NPC/HTN/HTNPlanJob.cs +++ b/Content.Server/NPC/HTN/HTNPlanJob.cs @@ -56,16 +56,16 @@ public sealed class HTNPlanJob : Job // hence we'll store it here. var appliedStates = new List?>(); - var tasksToProcess = new Queue(); + var tasksToProcess = new Stack(); var finalPlan = new List(); - tasksToProcess.Enqueue(_rootTask); + tasksToProcess.Push(_rootTask); // How many primitive tasks we've added since last record. var primitiveCount = 0; int tasksProcessed = 0; - while (tasksToProcess.TryDequeue(out var currentTask)) + while (tasksToProcess.TryPop(out var currentTask)) { if (tasksProcessed++ > _rootTask.MaximumTasks) throw new Exception("HTN Planner exceeded maximum tasks"); @@ -161,7 +161,7 @@ public sealed class HTNPlanJob : Job /// /// Goes through each compound task branch and tries to find an appropriate one. /// - private bool TryFindSatisfiedMethod(HTNCompoundTask compoundId, Queue tasksToProcess, NPCBlackboard blackboard, ref int mtrIndex) + private bool TryFindSatisfiedMethod(HTNCompoundTask compoundId, Stack tasksToProcess, NPCBlackboard blackboard, ref int mtrIndex) { var compound = _protoManager.Index(compoundId.Task); @@ -182,9 +182,9 @@ public sealed class HTNPlanJob : Job if (!isValid) continue; - foreach (var task in branch.Tasks) + foreach (var task in branch.Tasks.AsEnumerable().Reverse()) { - tasksToProcess.Enqueue(task); + tasksToProcess.Push(task); } return true; @@ -198,7 +198,7 @@ public sealed class HTNPlanJob : Job /// private void RestoreTolastDecomposedTask( Stack decompHistory, - Queue tasksToProcess, + Stack tasksToProcess, List?> appliedStates, List finalPlan, ref int primitiveCount, @@ -223,7 +223,7 @@ public sealed class HTNPlanJob : Job primitiveCount = lastDecomp.PrimitiveCount; blackboard = lastDecomp.Blackboard; - tasksToProcess.Enqueue(lastDecomp.CompoundTask); + tasksToProcess.Push(lastDecomp.CompoundTask); } ///