public string RootTask = default!;
/// <summary>
- /// The NPC's current plan.
+ /// Check any active services for our current plan. This is used to find new targets for example without changing our plan.
/// </summary>
- [ViewVariables]
- public HTNPlan? Plan;
+ [DataField("checkServices")]
+ public bool CheckServices = true;
- // TODO: Need dictionary timeoffsetserializer.
/// <summary>
- /// Last time we tried a particular <see cref="UtilityService"/>.
+ /// The NPC's current plan.
/// </summary>
- [DataField("serviceCooldowns")]
- public Dictionary<string, TimeSpan> ServiceCooldowns = new();
+ [ViewVariables]
+ public HTNPlan? Plan;
/// <summary>
/// How long to wait after having planned to try planning again.
public sealed class HTNSystem : EntitySystem
{
[Dependency] private readonly IAdminManager _admin = default!;
- [Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
- [Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly NPCSystem _npc = default!;
[Dependency] private readonly NPCUtilitySystem _utility = default!;
base.Initialize();
_sawmill = Logger.GetSawmill("npc.htn");
SubscribeLocalEvent<HTNComponent, ComponentShutdown>(OnHTNShutdown);
- SubscribeLocalEvent<HTNComponent, EntityUnpausedEvent>(OnHTNUnpaused);
SubscribeNetworkEvent<RequestHTNMessage>(OnHTNMessage);
_prototypeManager.PrototypesReloaded += OnPrototypeLoad;
OnLoad();
}
- private void OnHTNUnpaused(EntityUid uid, HTNComponent component, ref EntityUnpausedEvent args)
- {
- foreach (var (service, cooldown) in component.ServiceCooldowns)
- {
- var newCooldown = cooldown + args.PausedTime;
- component.ServiceCooldowns[service] = newCooldown;
- }
- }
-
private void OnHTNMessage(RequestHTNMessage msg, EntitySessionEventArgs args)
{
if (!_admin.HasAdminFlag((IPlayerSession) args.SenderSession, AdminFlags.Debug))
if (comp.Plan == null || newPlanBetter)
{
+ comp.CheckServices = false;
comp.Plan?.CurrentTask.Operator.Shutdown(comp.Blackboard, HTNOperatorStatus.BetterPlan);
comp.Plan = comp.PlanningJob.Result;
}, session.ConnectedClient);
}
}
+ // Keeping old plan
+ else
+ {
+ comp.CheckServices = true;
+ }
comp.PlanningJob = null;
comp.PlanningToken = null;
var currentTask = component.Plan.CurrentTask;
var blackboard = component.Blackboard;
- foreach (var service in currentTask.Services)
+ // Service still on cooldown.
+ if (component.CheckServices)
{
- // Service still on cooldown.
- if (component.ServiceCooldowns.TryGetValue(service.ID, out var lastService) &&
- _timing.CurTime < lastService)
+ foreach (var service in currentTask.Services)
{
- continue;
+ var serviceResult = _utility.GetEntities(blackboard, service.Prototype);
+ blackboard.SetValue(service.Key, serviceResult.GetHighest());
}
- var serviceResult = _utility.GetEntities(blackboard, service.Prototype);
- blackboard.SetValue(service.Key, serviceResult.GetHighest());
-
- var cooldown = TimeSpan.FromSeconds(_random.NextFloat(service.MinCooldown, service.MaxCooldown));
- component.ServiceCooldowns[service.ID] = _timing.CurTime + cooldown;
+ component.CheckServices = false;
}
status = currentOperator.Update(blackboard, frameTime);
break;
case HTNOperatorStatus.Failed:
currentOperator.Shutdown(blackboard, status);
- component.ServiceCooldowns.Clear();
component.Plan = null;
break;
// Operator completed so go to the next one.
// Plan finished!
if (component.Plan.Tasks.Count <= component.Plan.Index)
{
- component.ServiceCooldowns.Clear();
component.Plan = null;
break;
}