]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix HTN task ordering bug (#32909)
authorosjarw <62134478+osjarw@users.noreply.github.com>
Thu, 17 Apr 2025 12:42:31 +0000 (15:42 +0300)
committerGitHub <noreply@github.com>
Thu, 17 Apr 2025 12:42:31 +0000 (22:42 +1000)
* first, bad ordering

* fix

* Revert "first, bad ordering", recording artifacts

This reverts commit 22307675d79b417371775a0558c39781f59423d6.

Content.Server/NPC/HTN/HTNPlanJob.cs

index 9c62f5840a726e20cad28ac41b08cc6cc22d219d..8a57d52a667448a4768e50a3a9599e9ff0441458 100644 (file)
@@ -56,16 +56,16 @@ public sealed class HTNPlanJob : Job<HTNPlan>
         // hence we'll store it here.
         var appliedStates = new List<Dictionary<string, object>?>();
 
-        var tasksToProcess = new Queue<HTNTask>();
+        var tasksToProcess = new Stack<HTNTask>();
         var finalPlan = new List<HTNPrimitiveTask>();
-        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<HTNPlan>
     /// <summary>
     /// Goes through each compound task branch and tries to find an appropriate one.
     /// </summary>
-    private bool TryFindSatisfiedMethod(HTNCompoundTask compoundId, Queue<HTNTask> tasksToProcess, NPCBlackboard blackboard, ref int mtrIndex)
+    private bool TryFindSatisfiedMethod(HTNCompoundTask compoundId, Stack<HTNTask> tasksToProcess, NPCBlackboard blackboard, ref int mtrIndex)
     {
         var compound = _protoManager.Index<HTNCompoundPrototype>(compoundId.Task);
 
@@ -182,9 +182,9 @@ public sealed class HTNPlanJob : Job<HTNPlan>
             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<HTNPlan>
     /// </summary>
     private void RestoreTolastDecomposedTask(
         Stack<DecompositionState> decompHistory,
-        Queue<HTNTask> tasksToProcess,
+        Stack<HTNTask> tasksToProcess,
         List<Dictionary<string, object>?> appliedStates,
         List<HTNPrimitiveTask> finalPlan,
         ref int primitiveCount,
@@ -223,7 +223,7 @@ public sealed class HTNPlanJob : Job<HTNPlan>
 
         primitiveCount = lastDecomp.PrimitiveCount;
         blackboard = lastDecomp.Blackboard;
-        tasksToProcess.Enqueue(lastDecomp.CompoundTask);
+        tasksToProcess.Push(lastDecomp.CompoundTask);
     }
 
     /// <summary>