Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
UtilityReasoner.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_UTILITY_REASONER_H
4 #define GRAIL_UTILITY_REASONER_H
5 
6 #include "../Selectors/SelectionOption.hh"
7 #include "../Selectors/SelectionResult.hh"
8 #include "../../GrailCore/Reasoner.hh"
9 #include "../../GrailCore/EntityBlackboardPair.hh"
10 #include "../../GrailData/DebugInfo/UtilityReasonerSnapshot.h"
11 #include "../Selectors/BlueprintOption.hh"
12 
13 #include <functional>
14 #include <mutex>
15 #include <stack>
16 
17 namespace grail
18 {
19 namespace utility
20 {
24  class UtilityReasoner : public Reasoner
25  {
26  public:
32  float persistence = 0.1f);
33  UtilityReasoner(const UtilityReasoner& other) = delete;
34  UtilityReasoner(UtilityReasoner&& other) = delete;
35 
36  virtual ~UtilityReasoner() override = default;
37 
38  UtilityReasoner& operator =(const UtilityReasoner& other) = delete;
39  UtilityReasoner& operator =(UtilityReasoner&& other) = delete;
40 
43  virtual void StageBehavior(AIEntity& entity) override;
44 
48  virtual std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(size_t entityId) override;
49 
53 
56  void SetSnapshotProduction(bool isEnabled);
57 
58  protected:
64  virtual void GetBestOption(const std::vector<SelectionOption<EntityBlackboardPair>>& options,
65  SelectionResult& result,
66  std::function<bool(const std::size_t&)> validator) = 0;
67 
68  virtual void ClearCache() override;
69 
70  private:
71  void AssignBehaviorInstance(AIEntity& entity,
72  std::unique_ptr<Behavior> instance,
74  std::vector<std::unique_ptr<Behavior>> GenerateContextsAndInstances(const AIEntity& entity);
75  void ValidateOperationStack(const AIEntity& entity);
76  void SelectBehaviorFromStack(AIEntity& entity);
77 
78  std::vector<BlueprintOption<Behavior, EntityBlackboardPair, grail::AIEntity>> blueprintOptions = {};
79  std::stack<std::pair<std::unique_ptr<Behavior>, utility::SelectionOption<EntityBlackboardPair>>> operationStack
80  {};
81  std::vector<utility::SelectionOption<EntityBlackboardPair>> generatedOptions{};
82  utility::SelectionOption<EntityBlackboardPair> currentBehaviorOption{{}, nullptr, "", "", 0};
83  utility::SelectionOption<EntityBlackboardPair> previousBehaviorOption{{}, nullptr, "", "", 0};
84 
85  float persistence = 0.0f;
86 
87  bool produceSnapshot = false;
88  data::UtilityReasonerSnapshot cachedSnapshot{};
89  mutable std::mutex snapshotMutex;
90  };
91 }
92 }
93 
94 #endif //GRAIL_UTILITY_REASONER_H
grail::AIEntity
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:50
grail::Reasoner
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:21
grail::utility::BlueprintOption
Definition: BlueprintOption.hh:14
grail::utility::UtilityReasoner::ClearCache
virtual void ClearCache() override
ClearCache - called when assigning this reasoner to an entity. Clears internally stored data,...
Definition: UtilityReasoner.cpp:184
grail::data::UtilityReasonerSnapshot
Definition: UtilityReasonerSnapshot.h:15
grail::utility::SelectionOption< EntityBlackboardPair >
grail::utility::UtilityReasoner::GetBestOption
virtual void GetBestOption(const std::vector< SelectionOption< EntityBlackboardPair >> &options, SelectionResult &result, std::function< bool(const std::size_t &)> validator)=0
GetBestOption - Selects most suitable option. Provides different context for each evaluated objects.
grail::utility::SelectionResult
The SelectionResult struct - Structure containing results of operations done by Selector.
Definition: SelectionResult.hh:19
grail::utility::UtilityReasoner
The UtilityReasoner class - Class responsible for assigning behaviors using Utility System algorithm....
Definition: UtilityReasoner.hh:24
grail::utility::UtilityReasoner::GetUtilityReasonerSnapshot
data::UtilityReasonerSnapshot GetUtilityReasonerSnapshot() const
GetUtilityReasonerSnapshot - Returns copy of current snapshot and clear the original data.
Definition: UtilityReasoner.cpp:172
grail::utility::UtilityReasoner::StageBehavior
virtual void StageBehavior(AIEntity &entity) override
SelectBehavior - Selects behavior, if able, and assigns it to entity.
Definition: UtilityReasoner.cpp:23
grail::utility::UtilityReasoner::UtilityReasoner
UtilityReasoner(std::vector< BlueprintOption< Behavior, EntityBlackboardPair, grail::AIEntity >> blueprintOptions, float persistence=0.1f)
UtilityReasoner - Constructor.
Definition: UtilityReasoner.cpp:14
grail::utility::UtilityReasoner::SetSnapshotProduction
void SetSnapshotProduction(bool isEnabled)
SetSnapshotProduction - Tells reasoner whether it should produce debug snapshots of its state.
Definition: UtilityReasoner.cpp:179
grail::utility::UtilityReasoner::CreateSnapshotGenerator
virtual std::unique_ptr< ISnapshotGenerator > CreateSnapshotGenerator(size_t entityId) override
CreateSnapshotGenerator - Creates object responsible for producing snapshots for this reasoner assign...
Definition: UtilityReasoner.cpp:167