SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
RandomSelector.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 
18 class RandomSelector: public Selector {
19 public:
20  SELECTOR_CLASS(RandomSelector)
21 
22  TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis) override {
23  if (Selector::execute(entity, deltaMillis) == CANNOTEXECUTE) {
24  return CANNOTEXECUTE;
25  }
26 
27  TreeNodes childrenShuffled = _children;
28  const std::size_t size = childrenShuffled.size();
29  ai::shuffle(childrenShuffled.begin(), childrenShuffled.end());
30  TreeNodeStatus overallResult = FINISHED;
31  std::size_t i;
32  for (i = 0; i < size; ++i) {
33  const TreeNodePtr& child = childrenShuffled[i];
34  const TreeNodeStatus result = child->execute(entity, deltaMillis);
35  if (result == RUNNING) {
36  continue;
37  } else if (result == CANNOTEXECUTE || result == FAILED) {
38  overallResult = result;
39  }
40  child->resetState(entity);
41  }
42  for (++i; i < size; ++i) {
43  childrenShuffled[i]->resetState(entity);
44  }
45  return state(entity, overallResult);
46  }
47 };
48 
49 }
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
Base class for all type of TreeNode selectors.
Definition: Selector.h:25
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: RandomSelector.h:22
This node executes all the attached children in random order. This composite only fails if all childr...
Definition: RandomSelector.h:18