22 std::vector<float> _weights;
25 ProbabilitySelector(
const std::string& name,
const std::string& parameters,
const ConditionPtr& condition) :
26 Selector(name, parameters, condition), _weightSum(0.0f) {
27 std::vector<std::string> tokens;
28 Str::splitString(parameters, tokens,
",");
29 const int weightAmount =
static_cast<int>(tokens.size());
30 for (
int i = 0; i < weightAmount; i++) {
31 const float weight = Str::strToFloat(tokens[i]);
42 TreeNodeStatus
execute(const AIPtr& entity, int64_t deltaMillis)
override {
46 int index = getSelectorState(entity);
47 if (index == AI_NOTHING_SELECTED) {
48 float rndIndex = ai::randomf(_weightSum);
49 const int weightAmount =
static_cast<int>(_weights.size());
50 for (; index < weightAmount; ++index) {
51 if (rndIndex < _weights[index])
53 rndIndex -= _weights[index];
57 const TreeNodePtr& child = _children[index];
58 const TreeNodeStatus result = child->execute(entity, deltaMillis);
59 if (result == RUNNING) {
60 setSelectorState(entity, index);
62 setSelectorState(entity, AI_NOTHING_SELECTED);
64 child->resetState(entity);
66 const int size =
static_cast<int>(_children.size());
67 for (
int i = 0; i < size; ++i) {
70 _children[i]->resetState(entity);
72 return state(entity, result);
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
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
#define NODE_FACTORY(NodeName)
A node factory macro to ease and unify the registration at AIRegistry.
Definition: TreeNode.h:54
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: ProbabilitySelector.h:42