HTNOperator.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using JetBrains.Annotations;
  4. namespace Content.Server.NPC.HTN.PrimitiveTasks;
  5. /// <summary>
  6. /// Concrete code that gets run for an NPC task.
  7. /// </summary>
  8. [ImplicitDataDefinitionForInheritors, MeansImplicitUse]
  9. public abstract partial class HTNOperator
  10. {
  11. /// <summary>
  12. /// Called once whenever prototypes reload. Typically used to inject dependencies.
  13. /// </summary>
  14. public virtual void Initialize(IEntitySystemManager sysManager)
  15. {
  16. IoCManager.InjectDependencies(this);
  17. }
  18. /// <summary>
  19. /// Called during planning.
  20. /// </summary>
  21. /// <param name="blackboard">The blackboard for the NPC.</param>
  22. /// <param name="cancelToken"></param>
  23. /// <returns>Whether the plan is still valid and the effects to apply to the blackboard.
  24. /// These get re-applied during execution and are up to the operator to use or discard.</returns>
  25. public virtual async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  26. CancellationToken cancelToken)
  27. {
  28. return (true, null);
  29. }
  30. /// <summary>
  31. /// Called during the NPC's regular updates. If the logic requires coordination between NPCs (e.g. steering or combat)
  32. /// this may be better off using a component and letting an external system handling it.
  33. /// </summary>
  34. public virtual HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTime)
  35. {
  36. return HTNOperatorStatus.Finished;
  37. }
  38. /// <summary>
  39. /// Called when the plan has finished running.
  40. /// </summary>
  41. public virtual void PlanShutdown(NPCBlackboard blackboard)
  42. {
  43. }
  44. /// <summary>
  45. /// Called the first time an operator runs.
  46. /// </summary>
  47. public virtual void Startup(NPCBlackboard blackboard) {}
  48. /// <summary>
  49. /// Called whenever the operator stops running.
  50. /// </summary>
  51. public virtual void TaskShutdown(NPCBlackboard blackboard, HTNOperatorStatus status) {}
  52. }