From 8b1431f7121472d33e55ef27ccd760753543dd4e Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Mon, 29 Aug 2022 11:41:42 +0200 Subject: [PATCH] Add Random Sequence and Selector --- limboai/bt/bt_random_selector.cpp | 26 ++++++++++++++++++++++++++ limboai/bt/bt_random_selector.h | 20 ++++++++++++++++++++ limboai/bt/bt_random_sequence.cpp | 26 ++++++++++++++++++++++++++ limboai/bt/bt_random_sequence.h | 20 ++++++++++++++++++++ limboai/register_types.cpp | 5 +++++ 5 files changed, 97 insertions(+) create mode 100644 limboai/bt/bt_random_selector.cpp create mode 100644 limboai/bt/bt_random_selector.h create mode 100644 limboai/bt/bt_random_sequence.cpp create mode 100644 limboai/bt/bt_random_sequence.h diff --git a/limboai/bt/bt_random_selector.cpp b/limboai/bt/bt_random_selector.cpp new file mode 100644 index 0000000..e0891b8 --- /dev/null +++ b/limboai/bt/bt_random_selector.cpp @@ -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; +} \ No newline at end of file diff --git a/limboai/bt/bt_random_selector.h b/limboai/bt/bt_random_selector.h new file mode 100644 index 0000000..1415072 --- /dev/null +++ b/limboai/bt/bt_random_selector.h @@ -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 \ No newline at end of file diff --git a/limboai/bt/bt_random_sequence.cpp b/limboai/bt/bt_random_sequence.cpp new file mode 100644 index 0000000..cecad21 --- /dev/null +++ b/limboai/bt/bt_random_sequence.cpp @@ -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; +} \ No newline at end of file diff --git a/limboai/bt/bt_random_sequence.h b/limboai/bt/bt_random_sequence.h new file mode 100644 index 0000000..0fd3404 --- /dev/null +++ b/limboai/bt/bt_random_sequence.h @@ -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 \ No newline at end of file diff --git a/limboai/register_types.cpp b/limboai/register_types.cpp index ba4277b..d67133d 100644 --- a/limboai/register_types.cpp +++ b/limboai/register_types.cpp @@ -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(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class();