SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
AIRegistry.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "IAIFactory.h"
7 #include "AIFactories.h"
9 #include "tree/TreeNode.h"
10 #include "conditions/ICondition.h"
11 #include "tree/Fail.h"
12 #include "tree/Limit.h"
13 #include "tree/Invert.h"
14 #include "tree/Idle.h"
15 #include "tree/Parallel.h"
16 #include "tree/PrioritySelector.h"
18 #include "tree/RandomSelector.h"
19 #include "tree/Sequence.h"
20 #include "tree/Steer.h"
21 #include "tree/Succeed.h"
22 #include "conditions/And.h"
23 #include "conditions/False.h"
24 #include "conditions/HasEnemies.h"
25 #include "conditions/Not.h"
26 #include "conditions/Filter.h"
27 #include "conditions/Or.h"
28 #include "conditions/True.h"
29 #include "conditions/IsInGroup.h"
32 #include "filter/IFilter.h"
33 #include "filter/SelectEmpty.h"
37 #include "filter/SelectZone.h"
38 #include "filter/Union.h"
39 #include "filter/Intersection.h"
40 #include "filter/Last.h"
41 #include "filter/First.h"
42 #include "filter/Random.h"
43 #include "filter/Difference.h"
44 #include "filter/Complement.h"
45 #include "filter/SelectAll.h"
46 #include "movement/SelectionSeek.h"
47 #include "movement/SelectionFlee.h"
48 #include "movement/GroupFlee.h"
49 #include "movement/GroupSeek.h"
50 #include "movement/Steering.h"
51 #include "movement/TargetFlee.h"
52 #include "movement/TargetSeek.h"
53 #include "movement/Wander.h"
55 
56 namespace ai {
57 
58 #define R_GET(Name) registerFactory(#Name, Name::getFactory());
59 #define R_MOVE(Name) registerFactory(#Name, movement::Name::getFactory());
60 
64 class AIRegistry: public IAIFactory {
65 protected:
66  class TreeNodeFactory: public IFactoryRegistry<std::string, TreeNode, TreeNodeFactoryContext> {
67  public:
68  TreeNodeFactory() {
69  R_GET(Fail);
70  R_GET(Limit);
71  R_GET(Invert);
72  R_GET(Succeed);
73  R_GET(Parallel);
74  R_GET(PrioritySelector);
75  R_GET(ProbabilitySelector);
76  R_GET(RandomSelector);
77  R_GET(Sequence);
78  R_GET(Idle);
79  }
80  };
81 
82  TreeNodeFactory _treeNodeFactory;
83 
84  class SteerNodeFactory: public IFactoryRegistry<std::string, TreeNode, SteerNodeFactoryContext> {
85  public:
87  R_GET(Steer);
88  }
89  };
90 
91  SteerNodeFactory _steerNodeFactory;
92 
93  class SteeringFactory: public IFactoryRegistry<std::string, movement::ISteering, SteeringFactoryContext> {
94  public:
95  SteeringFactory() {
96  R_MOVE(Wander);
97  R_MOVE(GroupSeek);
98  R_MOVE(GroupFlee);
99  R_MOVE(TargetSeek);
100  R_MOVE(TargetFlee);
101  R_MOVE(SelectionSeek);
102  R_MOVE(SelectionFlee);
103  }
104  };
105 
106  SteeringFactory _steeringFactory;
107 
108  class FilterFactory: public IFactoryRegistry<std::string, IFilter, FilterFactoryContext> {
109  public:
110  FilterFactory() {
111  R_GET(SelectEmpty);
112  R_GET(SelectGroupLeader);
113  R_GET(SelectGroupMembers);
114  R_GET(SelectHighestAggro);
115  R_GET(SelectZone);
116  R_GET(Union);
117  R_GET(Intersection);
118  R_GET(Last);
119  R_GET(First);
120  R_GET(Random);
121  R_GET(Difference);
122  R_GET(Complement);
123  R_GET(SelectAll);
124  }
125  };
126 
127  FilterFactory _filterFactory;
128 
129  class ConditionFactory: public IFactoryRegistry<std::string, ICondition, ConditionFactoryContext> {
130  public:
131  ConditionFactory() {
132  R_GET(And);
133  R_GET(False);
134  R_GET(HasEnemies);
135  R_GET(Not);
136  R_GET(Or);
137  R_GET(True);
138  R_GET(Filter);
139  R_GET(IsGroupLeader);
140  R_GET(IsInGroup);
141  R_GET(IsCloseToGroup);
142  }
143  };
144 
145  ConditionFactory _conditionFactory;
146 public:
147  AIRegistry() :
148  IAIFactory() {
149  }
150  virtual ~AIRegistry() {}
151 
159  bool registerNodeFactory(const std::string& type, const ITreeNodeFactory& factory);
166  bool unregisterNodeFactory(const std::string& type);
167 
168  bool registerSteerNodeFactory(const std::string& type, const ISteerNodeFactory& factory);
175  bool unregisterSteerNodeFactory(const std::string& type);
176 
177  bool registerSteeringFactory(const std::string& type, const ISteeringFactory& factory);
178  bool unregisterSteeringFactory(const std::string& type);
179 
180  bool registerFilterFactory(const std::string& type, const IFilterFactory& factory);
181 
188  bool unregisterFilterFactory(const std::string& type);
189 
190  bool registerConditionFactory(const std::string& type, const IConditionFactory& factory);
197  bool unregisterConditionFactory(const std::string& type);
198 
199  TreeNodePtr createNode(const std::string& type, const TreeNodeFactoryContext& ctx) const override;
200  TreeNodePtr createSteerNode(const std::string& type, const SteerNodeFactoryContext& ctx) const override;
201  FilterPtr createFilter(const std::string& type, const FilterFactoryContext& ctx) const override;
202  ConditionPtr createCondition(const std::string& type, const ConditionFactoryContext& ctx) const override;
203  SteeringPtr createSteering(const std::string& type, const SteeringFactoryContext& ctx) const override;
204 };
205 
206 #undef R_GET
207 #undef R_MOVE
208 
209 inline TreeNodePtr AIRegistry::createNode(const std::string& nodeType, const TreeNodeFactoryContext& ctx) const {
210  return _treeNodeFactory.create(nodeType, &ctx);
211 }
212 
213 inline TreeNodePtr AIRegistry::createSteerNode(const std::string& nodeType, const SteerNodeFactoryContext& ctx) const {
214  return _steerNodeFactory.create(nodeType, &ctx);
215 }
216 
217 inline FilterPtr AIRegistry::createFilter(const std::string& nodeType, const FilterFactoryContext& ctx) const {
218  return _filterFactory.create(nodeType, &ctx);
219 }
220 
221 inline bool AIRegistry::registerNodeFactory(const std::string& nodeType, const ITreeNodeFactory& factory) {
222  return _treeNodeFactory.registerFactory(nodeType, factory);
223 }
224 
225 inline bool AIRegistry::registerFilterFactory(const std::string& nodeType, const IFilterFactory& factory) {
226  return _filterFactory.registerFactory(nodeType, factory);
227 }
228 
229 inline bool AIRegistry::registerConditionFactory(const std::string& nodeType, const IConditionFactory& factory) {
230  return _conditionFactory.registerFactory(nodeType, factory);
231 }
232 
233 inline bool AIRegistry::unregisterNodeFactory(const std::string& nodeType) {
234  return _treeNodeFactory.unregisterFactory(nodeType);
235 }
236 
237 inline bool AIRegistry::registerSteerNodeFactory(const std::string& type, const ISteerNodeFactory& factory) {
238  return _steerNodeFactory.registerFactory(type, factory);
239 }
240 
241 inline bool AIRegistry::unregisterSteerNodeFactory(const std::string& type) {
242  return _steerNodeFactory.unregisterFactory(type);
243 }
244 
245 inline bool AIRegistry::registerSteeringFactory(const std::string& type, const ISteeringFactory& factory) {
246  return _steeringFactory.registerFactory(type, factory);
247 }
248 
249 inline bool AIRegistry::unregisterSteeringFactory(const std::string& type) {
250  return _steeringFactory.unregisterFactory(type);
251 }
252 
253 inline bool AIRegistry::unregisterFilterFactory(const std::string& nodeType) {
254  return _filterFactory.unregisterFactory(nodeType);
255 }
256 
257 inline bool AIRegistry::unregisterConditionFactory(const std::string& nodeType) {
258  return _conditionFactory.unregisterFactory(nodeType);
259 }
260 
261 inline ConditionPtr AIRegistry::createCondition(const std::string& nodeType, const ConditionFactoryContext& ctx) const {
262  return _conditionFactory.create(nodeType, &ctx);
263 }
264 
265 inline SteeringPtr AIRegistry::createSteering(const std::string& type, const SteeringFactoryContext& ctx) const {
266  return _steeringFactory.create(type, &ctx);
267 }
268 
269 }
This condition will logically or all contained conditions.
Definition: Or.h:14
This condition will just swap the result of the contained condition.
Definition: Not.h:14
A node with only one child attached. The result of the attached child is inverted.
Definition: Invert.h:17
This filter just clears the selection.
Definition: SelectEmpty.h:14
This filter will just preserve the first entry of other filters.
Definition: First.h:14
This factory will create tree nodes. It uses the TreeNodeFactoryContext to collect all the needed dat...
Definition: AIFactories.h:68
Definition: AIRegistry.h:93
Definition: AIRegistry.h:66
Checks whether the controlled AI is close to a particular group.
Definition: IsCloseToGroup.h:20
bool unregisterConditionFactory(const std::string &type)
Unregisters a condition factory of the given type. This can also be used to replace a built-in type w...
Definition: AIRegistry.h:257
This filter performs an intersection between several filter results.
Definition: Intersection.h:16
Context for ITreeNodeFactory.
Definition: AIFactories.h:15
A decorator node which limits the execution of the attached child to a specified amount of runs...
Definition: Limit.h:17
The place to register your TreeNode and ICondition factories at.
Definition: AIRegistry.h:64
This filter will just preserve the last entry of other filters.
Definition: Last.h:14
This filter will pick the entity with the highest aggro value.
Definition: SelectHighestAggro.h:14
bool unregisterNodeFactory(const std::string &type)
Unregisters a tree node factory of the given type. This can also be used to replace a built-in type w...
Definition: AIRegistry.h:233
ConditionPtr createCondition(const std::string &type, const ConditionFactoryContext &ctx) const override
Allocates a new ICondition for the given type. The type must be registered in the AIRegistry for this...
Definition: AIRegistry.h:261
Definition: AIFactories.h:51
TreeNodePtr createSteerNode(const std::string &type, const SteerNodeFactoryContext &ctx) const override
Allocates a new TreeNode for the given type. The type must be registered in the AIRegistry for this t...
Definition: AIRegistry.h:213
Defines some basic movement algorithms like Wandering, Seeking and Fleeing.
This filter will pick the entities from the zone of the given entity.
Definition: SelectZone.h:15
This node tries to execute all the attached children until one succeeds. This composite only fails if...
Definition: PrioritySelector.h:17
ITimedNode that is just idling until the given time is elapsed.
Definition: Idle.h:13
Definition: AIFactories.h:43
A decorator node with only one child attached. The result of the attached child is only taken into ac...
Definition: Fail.h:15
Checks whether the AI is in any or in a particular group.
Definition: IsInGroup.h:22
This node executes one of the attached children randomly based on the given weights. The node is executed until it is no longer in the running state.
Definition: ProbabilitySelector.h:20
Condition related stuff.
A decorator node with only one child attached. The result of the attached child is only taken into ac...
Definition: Succeed.h:16
TreeNodePtr createNode(const std::string &type, const TreeNodeFactoryContext &ctx) const override
Allocates a new TreeNode for the given type. The type must be registered in the AIRegistry for this t...
Definition: AIRegistry.h:209
SteeringPtr createSteering(const std::string &type, const SteeringFactoryContext &ctx) const override
Creates a new ISteering for the given type. The type must be registered in the AIRegistry for this to...
Definition: AIRegistry.h:265
Definition: AIFactories.h:24
FilterPtr createFilter(const std::string &type, const FilterFactoryContext &ctx) const override
Allocates a new IFilter for the given type. The type must be registered in the AIRegistry for this to...
Definition: AIRegistry.h:217
Definition: Steer.h:16
This filter will preserve only a few random entries.
Definition: Random.h:15
Definition: AIFactories.h:89
bool unregisterFilterFactory(const std::string &type)
Unregisters a filter factory of the given type. This can also be used to replace a built-in type with...
Definition: AIRegistry.h:253
Definition: AIFactories.h:82
This condition just always evaluates to false.
Definition: False.h:14
Definition: AIFactories.h:34
Evaluates to true if you are the first member in a particular group.
Definition: IsGroupLeader.h:19
Definition: IAIFactory.h:39
bool unregisterSteerNodeFactory(const std::string &type)
Unregisters a tree node factory of the given type. This can also be used to replace a built-in type w...
Definition: AIRegistry.h:241
The sequence continues to execute their children until one of the children returned a state that is n...
Definition: Sequence.h:19
This filter will pick the entities from the groups the given AI instance is in.
Definition: SelectGroupMembers.h:15
Definition: AIRegistry.h:129
Definition: AIRegistry.h:84
This filter is a nop - it will just use the already filtered entities.
Definition: SelectAll.h:14
Definition: AIFactories.h:75
Definition: IFactoryRegistry.h:21
This filter merges several other filter results.
Definition: Union.h:16
bool registerNodeFactory(const std::string &type, const ITreeNodeFactory &factory)
Registers a tree node factory of the given type.
Definition: AIRegistry.h:221
This filter performs a complement operation on already filtered entities with the results given by th...
Definition: Complement.h:17
Definition: AIRegistry.h:108
The filter condition executes some selection filters (IFilter) and evaluates to true if the resulting...
Definition: Filter.h:20
This node executes all the attached children in random order. This composite only fails if all childr...
Definition: RandomSelector.h:18
This condition just always evaluates to true.
Definition: True.h:14
This condition will logically and all contained conditions.
Definition: And.h:14
This filter performs a difference operation between several filter results. The result consists of el...
Definition: Difference.h:17
This filter will pick the group leader of the specified group.
Definition: SelectGroupLeader.h:15
Executes all the connected children in the order they were added (no matter what the TreeNodeStatus o...
Definition: Parallel.h:17
This condition checks whether there are enemies.
Definition: HasEnemies.h:21