(C++)  1.1.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
UtilityReasoner.hh
1 #ifndef GRAIL_UTILITY_REASONER_H
2 #define GRAIL_UTILITY_REASONER_H
3 
4 #include "../../GrailCore/Reasoner.hh"
5 #include "../../GrailEvaluators/Evaluator.hh"
6 #include "../Selector.hh"
7 #include "../../GrailData/DebugInfo/UtilityReasonerSnapshot.h"
8 #include "../../GrailData/UtilityModel/UtilitySelectorModel.h"
9 
10 #include <functional>
11 #include <stack>
12 #include <tuple>
13 #include <map>
14 #include <mutex>
15 
16 
17 namespace grail
18 {
20  {
21  std::shared_ptr<utility::Evaluator<utility::EntityBlackboardPair>> evaluator;
22  utility::Blueprint<Behavior> behaviorBlueprint;
23  int rank;
24 
26  utility::Blueprint<Behavior>&& behaviorBlueprint, int rank);
28  const utility::Blueprint<Behavior>& behaviorBlueprint, int rank);
29  };
30 
36  class UtilityReasoner : public Reasoner
37  {
38  public:
46  UtilityReasoner(std::vector<BlueprintOption>&& blueprintOptions,
47  SelectionMethod selectionMethod = SelectionMethod::MAX,
48  float persistence = 0.1f);
49  UtilityReasoner(const UtilityReasoner& other) = delete;
50  UtilityReasoner(UtilityReasoner&& other) = delete;
51 
52  virtual ~UtilityReasoner() override = default;
53 
54  UtilityReasoner& operator = (const UtilityReasoner& other) = delete;
55  UtilityReasoner& operator = (UtilityReasoner&& other) = delete;
56 
61  void SelectBehavior(AIEntity& entity) override;
62 
68  std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(size_t entityId) override;
78  void SetSnapshotProduction(bool isEnabled);
79 
80  private:
81  void AssignBehaviorInstance(AIEntity& entity, std::unique_ptr<Behavior> instance, utility::Option<utility::EntityBlackboardPair>&& option);
82  std::vector<std::unique_ptr<Behavior>> GenerateContextsAndInstances(const AIEntity& entity);
83  void ValidateOperationStack(const AIEntity& entity);
84  void SelectBehaviorFromStack(AIEntity& entity);
85 
86  std::vector<BlueprintOption> blueprintOptions = {};
87  std::stack<std::pair<std::unique_ptr<Behavior>, utility::Option<utility::EntityBlackboardPair>>> operationStack{};
88  std::vector<utility::Option<utility::EntityBlackboardPair>> generatedOptions{};
89  utility::Option<utility::EntityBlackboardPair> currentBehaviorOption{{}, nullptr, "", "", 0};
90 
91  SelectionMethod selectionMethod = SelectionMethod::MAX;
92 
93  float persistence = 0.0f;
94 
95  bool produceSnapshot = false;
96  UtilityReasonerSnapshot cachedSnapshot{};
97  mutable std::mutex snapshotMutex;
98  };
99 }
100 #endif //GRAIL_UTILITY_REASONER_H
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:20
The UtilityReasoner class - Class responsible for assigning behaviors using Utility System algorithm....
Definition: UtilityReasoner.hh:37
void SetSnapshotProduction(bool isEnabled)
SetSnapshotProduction - Tells reasoner whether it should produce debug snapshots of its state.
Definition: UtilityReasoner.cpp:165
UtilityReasoner(std::vector< BlueprintOption > &&blueprintOptions, SelectionMethod selectionMethod=SelectionMethod::MAX, float persistence=0.1f)
UtilityReasoner - Constructor.
Definition: UtilityReasoner.cpp:21
std::unique_ptr< ISnapshotGenerator > CreateSnapshotGenerator(size_t entityId) override
CreateSnapshotGenerator - Creates object responsible for producing snapshots for this reasoner assign...
Definition: UtilityReasoner.cpp:153
void SelectBehavior(AIEntity &entity) override
SelectBehavior - Selects behavior, if able, and assigns it to entity.
Definition: UtilityReasoner.cpp:27
UtilityReasonerSnapshot GetUtilityReasonerSnapshot() const
GetUtilityReasonerSnapshot - Returns copy of current snapshot and clear the original data.
Definition: UtilityReasoner.cpp:158
The Blueprint class - Responsible for producing contexts and instances of data used by UtilityReasone...
Definition: Blueprint.hh:29
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:21
Definition: UtilityReasoner.hh:20
Definition: UtilityReasonerSnapshot.h:11