Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Content.Server/NPC/HTN/HTNSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
using System.Text;
using System.Threading;
using Content.Server.Administration.Managers;
// Starlight start
using Content.Server._Starlight.NPC.HTN;
using Content.Shared.CCVar;
using Robust.Shared.Configuration;
// Starlight end
using Robust.Shared.CPUJob.JobQueues;
using Robust.Shared.CPUJob.JobQueues.Queues;
using Content.Server.NPC.HTN.PrimitiveTasks;
Expand All @@ -22,8 +27,11 @@ 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 IConfigurationManager _cfg = default!; // Starlight

private readonly JobQueue _planQueue = new(0.004);
// Starlight start - HTN plan budget is cvar-driven (npc.htn_plan_budget) instead of a hardcoded 0.004
private readonly AdjustableJobQueue _planQueue = new(0.004);
// Starlight end

private readonly HashSet<ICommonSession> _subscribers = new();

Expand All @@ -40,6 +48,10 @@ public override void Initialize()
SubscribeNetworkEvent<RequestHTNMessage>(OnHTNMessage);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypeLoad);
OnLoad();

// Starlight
Subs.CVar(_cfg, CCVars.NPCHTNPlanBudget, value => _planQueue.SetMaxTime(value), true);
// Starlight end
}

private void OnHTNMessage(RequestHTNMessage msg, EntitySessionEventArgs args)
Expand Down
13 changes: 13 additions & 0 deletions Content.Server/_Starlight/NPC/HTN/AdjustableJobQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Robust.Shared.CPUJob.JobQueues.Queues;

namespace Content.Server._Starlight.NPC.HTN;

public sealed class AdjustableJobQueue(double maxTime) : JobQueue
{
private double _maxTime = maxTime;

public override double MaxTime => _maxTime;

public void SetMaxTime(double maxTime)
=> _maxTime = maxTime;
}
9 changes: 9 additions & 0 deletions Content.Shared/CCVar/CCVars.NPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ public sealed partial class CCVars
/// Should NPCs pathfind when steering. For debug purposes.
/// </summary>
public static readonly CVarDef<bool> NPCPathfinding = CVarDef.Create("npc.pathfinding", true);

// Starlight start
/// <summary>
/// Per-tick wall-clock budget (seconds) the HTN planner is allowed to spend running plan jobs.
/// Lower it to cap NPC planning cost under load. Was a hardcoded 0.004 (4ms) in HTNSystem.
/// </summary>
public static readonly CVarDef<float> NPCHTNPlanBudget =
CVarDef.Create("npc.htn_plan_budget", 0.004f, CVar.SERVERONLY);
// Starlight end
Comment on lines +17 to +24

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be in Starlight CCVars

}
Loading