SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
AI.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <unordered_map>
7 #include <memory>
8 
9 #include "group/GroupId.h"
10 #include "aggro/AggroMgr.h"
11 #include "ICharacter.h"
12 #include "tree/TreeNode.h"
14 #include "common/Thread.h"
15 #include "common/Types.h"
16 #include "common/NonCopyable.h"
17 #include "common/Math.h"
18 
19 namespace ai {
20 
21 class ICharacter;
22 typedef std::shared_ptr<ICharacter> ICharacterPtr;
23 class Zone;
24 
25 typedef std::vector<CharacterId> FilteredEntities;
26 
27 #ifndef AI_NOTHING_SELECTED
28 #define AI_NOTHING_SELECTED (-1)
29 #endif
30 
41 class AI : public NonCopyable, public std::enable_shared_from_this<AI> {
42  friend class TreeNode;
43  friend class LUAAIRegistry;
44  friend class IFilter;
45  friend class Filter;
46  friend class Server;
47 protected:
51  typedef std::unordered_map<int, TreeNodeStatus> NodeStates;
52  NodeStates _lastStatus;
56  typedef std::unordered_map<int, uint64_t> LastExecMap;
57  LastExecMap _lastExecMillis;
58 
64  mutable FilteredEntities _filteredEntities;
65 
66  void setFilteredEntities(const FilteredEntities& filteredEntities);
67 
68  void addFilteredEntity(CharacterId id);
69 
74  typedef std::unordered_map<int, int> SelectorStates;
75  SelectorStates _selectorStates;
76 
80  typedef std::unordered_map<int, int> LimitStates;
81  LimitStates _limitStates;
82 
83  TreeNodePtr _behaviour;
84  AggroMgr _aggroMgr;
85 
86  ICharacterPtr _character;
87 
88  bool _pause;
89  bool _debuggingActive;
90 
91  int64_t _time;
92 
93  Zone* _zone;
94 
95  std::atomic_bool _reset;
96 public:
100  explicit AI(const TreeNodePtr& behaviour) :
101  _behaviour(behaviour), _pause(false), _debuggingActive(false), _time(0L), _zone(nullptr), _reset(false) {
102  }
103  virtual ~AI() {
104  }
105 
111  virtual void update(int64_t dt, bool debuggingActive);
112 
116  void setZone(Zone* zone);
120  Zone* getZone() const;
125  bool hasZone() const;
126 
131  void setPause(bool pause);
132 
137  bool isPause() const;
138 
142  bool isDebuggingActive() const;
143 
147  TreeNodePtr getBehaviour() const;
152  TreeNodePtr setBehaviour(const TreeNodePtr& newBehaviour);
156  ICharacterPtr getCharacter() const;
160  void setCharacter(const ICharacterPtr& character);
161 
162  template <typename CharacterType>
163  inline CharacterType& getCharacterCast() const {
164  return *static_cast<CharacterType*>(_character.get());
165  }
166 
167  int64_t getTime() const;
168 
169  CharacterId getId() const;
170 
174  AggroMgr& getAggroMgr();
178  const AggroMgr& getAggroMgr() const;
179 
189  const FilteredEntities& getFilteredEntities() const;
190 
195  inline std::shared_ptr<AI> ptr() {
196  return shared_from_this();
197  }
198 };
199 
200 inline TreeNodePtr AI::getBehaviour() const {
201  return _behaviour;
202 }
203 
204 inline TreeNodePtr AI::setBehaviour(const TreeNodePtr& newBehaviour) {
205  TreeNodePtr current = _behaviour;
206  _behaviour = newBehaviour;
207  _reset = true;
208  return current;
209 }
210 
211 inline void AI::setPause(bool pause) {
212  _pause = pause;
213 }
214 
215 inline bool AI::isPause() const {
216  return _pause;
217 }
218 
219 inline ICharacterPtr AI::getCharacter() const {
220  return _character;
221 }
222 
223 inline void AI::setCharacter(const ICharacterPtr& character) {
224  ai_assert(!_character, "There is already a character set");
225  _character = character;
226 }
227 
229  return _aggroMgr;
230 }
231 
232 inline const AggroMgr& AI::getAggroMgr() const {
233  return _aggroMgr;
234 }
235 
236 inline const FilteredEntities& AI::getFilteredEntities() const {
237  return _filteredEntities;
238 }
239 
240 inline void AI::setFilteredEntities(const FilteredEntities& filteredEntities) {
241  _filteredEntities = filteredEntities;
242 }
243 
244 inline void AI::addFilteredEntity(CharacterId id) {
245  _filteredEntities.push_back(id);
246 }
247 
248 inline bool AI::isDebuggingActive() const {
249  return _debuggingActive;
250 }
251 
252 inline void AI::setZone(Zone* zone) {
253  _zone = zone;
254 }
255 
256 inline Zone* AI::getZone() const {
257  return _zone;
258 }
259 
260 inline bool AI::hasZone() const {
261  return _zone != nullptr;
262 }
263 
264 inline int64_t AI::getTime() const {
265  return _time;
266 }
267 
268 inline CharacterId AI::getId() const {
269  if (!_character) {
270  return AI_NOTHING_SELECTED;
271  }
272  return _character->getId();
273 }
274 
275 inline void AI::update(int64_t dt, bool debuggingActive) {
276  if (isPause()) {
277  return;
278  }
279 
280  if (_character) {
281  _character->update(dt, debuggingActive);
282  }
283 
284  if (_reset) {
285  // safe to do it like this, because update is not called from multiple threads
286  _reset = false;
287  _lastStatus.clear();
288  _lastExecMillis.clear();
289  _filteredEntities.clear();
290  _selectorStates.clear();
291  }
292 
293  _debuggingActive = debuggingActive;
294  _time += dt;
295  _aggroMgr.update(dt);
296 }
297 
298 typedef std::shared_ptr<AI> AIPtr;
299 
300 }
virtual void update(int64_t dt, bool debuggingActive)
Update the behaviour and the aggro values if the entity is not on hold.
Definition: AI.h:275
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
Manages the aggro values for one AI instance. There are several ways to degrade the aggro values...
Definition: AggroMgr.h:21
Zone * getZone() const
Definition: AI.h:256
std::shared_ptr< AI > ptr()
Definition: AI.h:195
const FilteredEntities & getFilteredEntities() const
FilteredEntities is holding a list of CharacterIds that were selected by the Select condition...
Definition: AI.h:236
bool hasZone() const
Returns true if the entity is already in a Zone. This must not be managed manually, the Zone is doing that already.
Definition: AI.h:260
TreeNodePtr setBehaviour(const TreeNodePtr &newBehaviour)
Set a new behaviour.
Definition: AI.h:204
std::unordered_map< int, TreeNodeStatus > NodeStates
Definition: AI.h:51
AI(const TreeNodePtr &behaviour)
Definition: AI.h:100
std::unordered_map< int, int > LimitStates
Definition: AI.h:80
std::unordered_map< int, int > SelectorStates
Definition: AI.h:74
This is the type the library works with. It interacts with it's real world entity by the ICharacter i...
Definition: AI.h:41
The base class for all behaviour tree actions.
Definition: TreeNode.h:88
void setZone(Zone *zone)
Set the new Zone this entity is in.
Definition: AI.h:252
TreeNodePtr getBehaviour() const
Get the current behaviour for this ai.
Definition: AI.h:200
Allows you to register lua TreeNodes, Conditions, Filters and ISteerings.
Definition: LUAAIRegistry.h:69
bool isPause() const
don't update the entity as long as it is paused
Definition: AI.h:215
Definition: NonCopyable.h:8
ICharacterPtr getCharacter() const
Definition: AI.h:219
void setCharacter(const ICharacterPtr &character)
Definition: AI.h:223
AggroMgr & getAggroMgr()
Definition: AI.h:228
void update(int64_t deltaMillis)
this will update the aggro list according to the reduction type of an entry.
Definition: AggroMgr.h:131
std::unordered_map< int, uint64_t > LastExecMap
Definition: AI.h:56
The filter condition executes some selection filters (IFilter) and evaluates to true if the resulting...
Definition: Filter.h:20
FilteredEntities _filteredEntities
Definition: AI.h:64
The server can serialize the state of the AI and broadcast it to all connected clients.
Definition: Server.h:50
bool isDebuggingActive() const
Definition: AI.h:248
void setPause(bool pause)
don't update the entity as long as it is paused
Definition: AI.h:211