Fix NPCs stalling when too many exist (#37056)

Fix NPC stalling when too many exist
This commit is contained in:
Nemanja
2025-04-29 20:51:41 -04:00
committed by GitHub
parent 97d4e80685
commit e3c3f4898f
2 changed files with 20 additions and 3 deletions

View File

@@ -180,11 +180,24 @@ public sealed class HTNSystem : EntitySystem
_planQueue.Process(); _planQueue.Process();
var query = EntityQueryEnumerator<ActiveNPCComponent, HTNComponent>(); 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)) 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 we're over our max count or it's not MapInit then ignore the NPC.
if (count >= maxUpdates) if (updates >= maxUpdates)
break; {
// Intentional return. We don't want to go to the end logic and reset count.
return;
}
if (!comp.Enabled) if (!comp.Enabled)
continue; continue;
@@ -274,7 +287,12 @@ public sealed class HTNSystem : EntitySystem
Update(comp, frameTime); Update(comp, frameTime);
count++; 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) private void AppendDebugText(HTNTask task, StringBuilder text, List<int> planBtr, List<int> btr, ref int level)

View File

@@ -136,7 +136,6 @@ namespace Content.Server.NPC.Systems
if (!Enabled) if (!Enabled)
return; return;
_count = 0;
// Add your system here. // Add your system here.
_htn.UpdateNPC(ref _count, _maxUpdates, frameTime); _htn.UpdateNPC(ref _count, _maxUpdates, frameTime);
} }