(C++)  1.0.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 "../GrailSystem/Reasoner.hh"
5 #include "Evaluator.hh"
6 #include "Selector.hh"
7 #include "../GrailData/GrailDebugInfo/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 {
19  class UtilityReasoner : public Reasoner
20  {
21  public:
28  UtilityReasoner(std::map<std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>>, Prefab<Behavior> >&& prefabOptions,
29  SelectionMethod selectionMethod = SelectionMethod::MAX,
30  float persistence = 0.1f);
31  UtilityReasoner(const UtilityReasoner& other) = delete;
32  UtilityReasoner(UtilityReasoner&& other) = default;
33 
34  virtual ~UtilityReasoner() override;
35 
36  UtilityReasoner& operator = (const UtilityReasoner& other) = delete;
37  UtilityReasoner& operator = (UtilityReasoner&& other) = default;
38 
39  void SelectBehavior(AIEntity& entity) override;
40 
41  std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(size_t) override;
42  UtilityReasonerSnapshot GetUtilityReasonerSnapshot() const;
43  void SetSnapshotProduction(bool value);
44 
45  private:
46  void AssignBehaviorInstance(AIEntity& entity, std::unique_ptr<Behavior> instance, std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>> evaluator, const EntityBlackboardPair& context);
47  void GenerateOptions(const AIEntity& entity);
48  std::vector<std::unique_ptr<Behavior>> GenerateInstances(const std::vector< std::pair<EntityBlackboardPair, std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>>>>& options);
49  void ValidateOperationStack(const AIEntity& entity);
50  void SelectBehaviorFromStack(AIEntity& entity);
51 
52  std::map<std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>>, Prefab<Behavior> > prefabOptions = {};
53  std::stack<std::tuple<Behavior*, std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>>, EntityBlackboardPair>> operationStack{};
54  std::vector< std::pair<EntityBlackboardPair, std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>>>> generatedOptions{};
55  std::shared_ptr<utility::IEvaluator<EntityBlackboardPair>> currentBehaviorEvaluator = nullptr;
56  EntityBlackboardPair currentBehaviorContext{};
57 
58  SelectionMethod selectionMethod = SelectionMethod::MAX;
59 
60  float persistence = 0.0f;
61 
62  bool produceSnapshot = false;
63  UtilityReasonerSnapshot cachedSnapshot{};
64  mutable std::mutex snapshotMutex;
65  };
66 }
67 #endif //GRAIL_UTILITY_REASONER_H
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
The Prefab class - responsible for producing contexts and instances of data used by UtilityReasoner.
Definition: Prefab.hh:21
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:20
Definition: UtilityReasoner.hh:20
void SelectBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: UtilityReasoner.cpp:25
UtilityReasoner(std::map< std::shared_ptr< utility::IEvaluator< EntityBlackboardPair >>, Prefab< Behavior > > &&prefabOptions, SelectionMethod selectionMethod=SelectionMethod::MAX, float persistence=0.1f)
UtilityReasoner - selects behaviors based on their scores calculated by utility system.
Definition: UtilityReasoner.cpp:9
The Evaluator class - Evaluates object in terms of provided Context.
Definition: IEvaluator.hh:15
Definition: UtilityReasonerSnapshot.h:11