SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
PrioritySelector.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/Selector.h"
7 #include "AI.h"
8 
9 namespace ai {
10 
17 class PrioritySelector: public Selector {
18 public:
19  SELECTOR_CLASS(PrioritySelector)
20 
21  TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis) override {
22  if (Selector::execute(entity, deltaMillis) == CANNOTEXECUTE)
23  return CANNOTEXECUTE;
24 
25  int index = getSelectorState(entity);
26  if (index == AI_NOTHING_SELECTED) {
27  index = 0;
28  }
29  const std::size_t size = _children.size();
30  TreeNodeStatus overallResult = FINISHED;
31  std::size_t i = index;
32  for (std::size_t j = 0; j < i; ++j) {
33  _children[j]->resetState(entity);
34  }
35  for (; i < size; ++i) {
36  const TreeNodePtr& child = _children[i];
37  const TreeNodeStatus result = child->execute(entity, deltaMillis);
38  if (result == RUNNING) {
39  setSelectorState(entity, static_cast<int>(i));
40  } else if (result == CANNOTEXECUTE || result == FAILED) {
41  child->resetState(entity);
42  setSelectorState(entity, AI_NOTHING_SELECTED);
43  continue;
44  } else {
45  setSelectorState(entity, AI_NOTHING_SELECTED);
46  }
47  child->resetState(entity);
48  overallResult = result;
49  break;
50  }
51  for (++i; i < size; ++i) {
52  _children[i]->resetState(entity);
53  }
54  return state(entity, overallResult);
55  }
56 };
57 
58 }
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
This node tries to execute all the attached children until one succeeds. This composite only fails if...
Definition: PrioritySelector.h:17
Base class for all type of TreeNode selectors.
Definition: Selector.h:25
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: PrioritySelector.h:21