Add Random Sequence and Selector

This commit is contained in:
Serhii Snitsaruk 2022-08-29 11:41:42 +02:00
parent e9b3b9eb54
commit 8b1431f712
5 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,26 @@
/* bt_random_selector.cpp */
#include "bt_random_selector.h"
void BTRandomSelector::_enter() {
last_running_idx = 0;
if (_indicies.size() != get_child_count()) {
_indicies.resize(get_child_count());
for (int i = 0; i < get_child_count(); i++) {
_indicies.set(i, i);
}
}
_indicies.shuffle();
}
int BTRandomSelector::_tick(float p_delta) {
int status = FAILURE;
for (int i = 0; i < get_child_count(); i++) {
status = get_child(_indicies[i])->execute(p_delta);
if (status != FAILURE) {
last_running_idx = i;
break;
}
}
return status;
}

View File

@ -0,0 +1,20 @@
/* bt_random_selector.h */
#ifndef BT_RANDOM_SELECTOR_H
#define BT_RANDOM_SELECTOR_H
#include "bt_composite.h"
#include "core/vector.h"
class BTRandomSelector : public BTComposite {
GDCLASS(BTRandomSelector, BTComposite);
private:
int last_running_idx = 0;
Array _indicies;
protected:
virtual void _enter();
virtual int _tick(float p_delta);
};
#endif // BT_RANDOM_SELECTOR_H

View File

@ -0,0 +1,26 @@
/* bt_random_sequence.cpp */
#include "bt_random_sequence.h"
void BTRandomSequence::_enter() {
last_running_idx = 0;
if (_indicies.size() != get_child_count()) {
_indicies.resize(get_child_count());
for (int i = 0; i < get_child_count(); i++) {
_indicies.set(i, i);
}
}
_indicies.shuffle();
}
int BTRandomSequence::_tick(float p_delta) {
int status = SUCCESS;
for (int i = 0; i < get_child_count(); i++) {
status = get_child(_indicies[i])->execute(p_delta);
if (status != SUCCESS) {
last_running_idx = i;
break;
}
}
return status;
}

View File

@ -0,0 +1,20 @@
/* bt_random_sequence.h */
#ifndef BT_RANDOM_SEQUENCE_H
#define BT_RANDOM_SEQUENCE_H
#include "bt_composite.h"
#include "core/vector.h"
class BTRandomSequence : public BTComposite {
GDCLASS(BTRandomSequence, BTComposite);
private:
int last_running_idx = 0;
Array _indicies;
protected:
virtual void _enter();
virtual int _tick(float p_delta);
};
#endif // BT_RANDOM_SEQUENCE_H

View File

@ -17,6 +17,8 @@
#include "bt/bt_invert.h"
#include "bt/bt_parallel.h"
#include "bt/bt_probability.h"
#include "bt/bt_random_selector.h"
#include "bt/bt_random_sequence.h"
#include "bt/bt_repeat.h"
#include "bt/bt_repeat_until_failure.h"
#include "bt/bt_repeat_until_success.h"
@ -40,6 +42,9 @@ void register_limboai_types() {
ClassDB::register_class<BTDynamicSequence>();
ClassDB::register_class<BTDynamicSelector>();
ClassDB::register_class<BTSelector>();
ClassDB::register_class<BTRandomSelector>();
ClassDB::register_class<BTRandomSequence>();
ClassDB::register_class<BTSelector>();
ClassDB::register_class<BTParallel>();
ClassDB::register_class<BTInvert>();