]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix NPCs stalling when too many exist (#37056)
authorNemanja <98561806+EmoGarbage404@users.noreply.github.com>
Wed, 30 Apr 2025 00:51:41 +0000 (20:51 -0400)
committerGitHub <noreply@github.com>
Wed, 30 Apr 2025 00:51:41 +0000 (10:51 +1000)
Fix NPC stalling when too many exist

Content.Server/NPC/HTN/HTNSystem.cs
Content.Server/NPC/Systems/NPCSystem.cs

index 89b4ae778660c77c41195a35ffa9e1903253af66..4d9e321dd91f0421a8488a2a9ac25102aea2f512 100644 (file)
@@ -180,11 +180,24 @@ public sealed class HTNSystem : EntitySystem
         _planQueue.Process();
         var query = EntityQueryEnumerator<ActiveNPCComponent, HTNComponent>();
 
+        // Move ahead "count" entries in the query.
+        // This is to ensure that if we didn't process all the npcs the first time,
+        // we get to the remaining ones instead of iterating over the beginning again.
+        for (var i = 0; i < count; i++)
+        {
+            query.MoveNext(out _, out _);
+        }
+
+        // the amount of updates we've processed during this iteration.
+        var updates = 0;
         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 (updates >= maxUpdates)
+            {
+                // Intentional return. We don't want to go to the end logic and reset count.
+                return;
+            }
 
             if (!comp.Enabled)
                 continue;
@@ -274,7 +287,12 @@ public sealed class HTNSystem : EntitySystem
 
             Update(comp, frameTime);
             count++;
+            updates++;
         }
+
+        // only reset our counter back to 0 if we finish iterating.
+        // otherwise it lets us know where we left off.
+        count = 0;
     }
 
     private void AppendDebugText(HTNTask task, StringBuilder text, List<int> planBtr, List<int> btr, ref int level)
index bc21916976e59592b54e1a2fc2e113f213f3cb52..c7690cb295b134c4e142fddc4a1d3f04dfef8170 100644 (file)
@@ -136,7 +136,6 @@ namespace Content.Server.NPC.Systems
             if (!Enabled)
                 return;
 
-            _count = 0;
             // Add your system here.
             _htn.UpdateNPC(ref _count, _maxUpdates, frameTime);
         }