From 8705241fa45a01bf41c1298759edfa0ba2c8e0b4 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sun, 28 Aug 2022 22:58:30 +0200 Subject: [PATCH] Add Probability decorator and reorganize --- limboai/SCsub | 3 +- limboai/{ => bt}/bt_action.cpp | 0 limboai/{ => bt}/bt_action.h | 0 limboai/{ => bt}/bt_always_fail.cpp | 0 limboai/{ => bt}/bt_always_fail.h | 0 limboai/{ => bt}/bt_always_succeed.cpp | 0 limboai/{ => bt}/bt_always_succeed.h | 0 limboai/{ => bt}/bt_composite.cpp | 0 limboai/{ => bt}/bt_composite.h | 0 limboai/{ => bt}/bt_condition.cpp | 0 limboai/{ => bt}/bt_condition.h | 0 limboai/{ => bt}/bt_cooldown.cpp | 0 limboai/{ => bt}/bt_cooldown.h | 0 limboai/{ => bt}/bt_decorator.cpp | 0 limboai/{ => bt}/bt_decorator.h | 0 limboai/{ => bt}/bt_delay.cpp | 0 limboai/{ => bt}/bt_delay.h | 0 limboai/{ => bt}/bt_invert.cpp | 0 limboai/{ => bt}/bt_invert.h | 0 limboai/{ => bt}/bt_parallel.cpp | 0 limboai/{ => bt}/bt_parallel.h | 0 limboai/bt/bt_probability.cpp | 21 +++++++++++ limboai/bt/bt_probability.h | 29 +++++++++++++++ limboai/{ => bt}/bt_repeat.cpp | 0 limboai/{ => bt}/bt_repeat.h | 0 limboai/{ => bt}/bt_repeat_until_failure.cpp | 0 limboai/{ => bt}/bt_repeat_until_failure.h | 0 limboai/{ => bt}/bt_repeat_until_success.cpp | 0 limboai/{ => bt}/bt_repeat_until_success.h | 0 limboai/{ => bt}/bt_run_limit.cpp | 0 limboai/{ => bt}/bt_run_limit.h | 0 limboai/{ => bt}/bt_selector.cpp | 0 limboai/{ => bt}/bt_selector.h | 0 limboai/{ => bt}/bt_sequence.cpp | 0 limboai/{ => bt}/bt_sequence.h | 0 limboai/{ => bt}/bt_task.cpp | 6 ++-- limboai/{ => bt}/bt_task.h | 0 limboai/{ => bt}/bt_time_limit.cpp | 0 limboai/{ => bt}/bt_time_limit.h | 0 limboai/register_types.cpp | 38 ++++++++++---------- limboai/register_types.h | 5 +++ 41 files changed, 80 insertions(+), 22 deletions(-) rename limboai/{ => bt}/bt_action.cpp (100%) rename limboai/{ => bt}/bt_action.h (100%) rename limboai/{ => bt}/bt_always_fail.cpp (100%) rename limboai/{ => bt}/bt_always_fail.h (100%) rename limboai/{ => bt}/bt_always_succeed.cpp (100%) rename limboai/{ => bt}/bt_always_succeed.h (100%) rename limboai/{ => bt}/bt_composite.cpp (100%) rename limboai/{ => bt}/bt_composite.h (100%) rename limboai/{ => bt}/bt_condition.cpp (100%) rename limboai/{ => bt}/bt_condition.h (100%) rename limboai/{ => bt}/bt_cooldown.cpp (100%) rename limboai/{ => bt}/bt_cooldown.h (100%) rename limboai/{ => bt}/bt_decorator.cpp (100%) rename limboai/{ => bt}/bt_decorator.h (100%) rename limboai/{ => bt}/bt_delay.cpp (100%) rename limboai/{ => bt}/bt_delay.h (100%) rename limboai/{ => bt}/bt_invert.cpp (100%) rename limboai/{ => bt}/bt_invert.h (100%) rename limboai/{ => bt}/bt_parallel.cpp (100%) rename limboai/{ => bt}/bt_parallel.h (100%) create mode 100644 limboai/bt/bt_probability.cpp create mode 100644 limboai/bt/bt_probability.h rename limboai/{ => bt}/bt_repeat.cpp (100%) rename limboai/{ => bt}/bt_repeat.h (100%) rename limboai/{ => bt}/bt_repeat_until_failure.cpp (100%) rename limboai/{ => bt}/bt_repeat_until_failure.h (100%) rename limboai/{ => bt}/bt_repeat_until_success.cpp (100%) rename limboai/{ => bt}/bt_repeat_until_success.h (100%) rename limboai/{ => bt}/bt_run_limit.cpp (100%) rename limboai/{ => bt}/bt_run_limit.h (100%) rename limboai/{ => bt}/bt_selector.cpp (100%) rename limboai/{ => bt}/bt_selector.h (100%) rename limboai/{ => bt}/bt_sequence.cpp (100%) rename limboai/{ => bt}/bt_sequence.h (100%) rename limboai/{ => bt}/bt_task.cpp (98%) rename limboai/{ => bt}/bt_task.h (100%) rename limboai/{ => bt}/bt_time_limit.cpp (100%) rename limboai/{ => bt}/bt_time_limit.h (100%) diff --git a/limboai/SCsub b/limboai/SCsub index e208d38..f332202 100644 --- a/limboai/SCsub +++ b/limboai/SCsub @@ -2,4 +2,5 @@ Import('env') -env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build +env.add_source_files(env.modules_sources, "*.cpp") +env.add_source_files(env.modules_sources, "bt/*.cpp") diff --git a/limboai/bt_action.cpp b/limboai/bt/bt_action.cpp similarity index 100% rename from limboai/bt_action.cpp rename to limboai/bt/bt_action.cpp diff --git a/limboai/bt_action.h b/limboai/bt/bt_action.h similarity index 100% rename from limboai/bt_action.h rename to limboai/bt/bt_action.h diff --git a/limboai/bt_always_fail.cpp b/limboai/bt/bt_always_fail.cpp similarity index 100% rename from limboai/bt_always_fail.cpp rename to limboai/bt/bt_always_fail.cpp diff --git a/limboai/bt_always_fail.h b/limboai/bt/bt_always_fail.h similarity index 100% rename from limboai/bt_always_fail.h rename to limboai/bt/bt_always_fail.h diff --git a/limboai/bt_always_succeed.cpp b/limboai/bt/bt_always_succeed.cpp similarity index 100% rename from limboai/bt_always_succeed.cpp rename to limboai/bt/bt_always_succeed.cpp diff --git a/limboai/bt_always_succeed.h b/limboai/bt/bt_always_succeed.h similarity index 100% rename from limboai/bt_always_succeed.h rename to limboai/bt/bt_always_succeed.h diff --git a/limboai/bt_composite.cpp b/limboai/bt/bt_composite.cpp similarity index 100% rename from limboai/bt_composite.cpp rename to limboai/bt/bt_composite.cpp diff --git a/limboai/bt_composite.h b/limboai/bt/bt_composite.h similarity index 100% rename from limboai/bt_composite.h rename to limboai/bt/bt_composite.h diff --git a/limboai/bt_condition.cpp b/limboai/bt/bt_condition.cpp similarity index 100% rename from limboai/bt_condition.cpp rename to limboai/bt/bt_condition.cpp diff --git a/limboai/bt_condition.h b/limboai/bt/bt_condition.h similarity index 100% rename from limboai/bt_condition.h rename to limboai/bt/bt_condition.h diff --git a/limboai/bt_cooldown.cpp b/limboai/bt/bt_cooldown.cpp similarity index 100% rename from limboai/bt_cooldown.cpp rename to limboai/bt/bt_cooldown.cpp diff --git a/limboai/bt_cooldown.h b/limboai/bt/bt_cooldown.h similarity index 100% rename from limboai/bt_cooldown.h rename to limboai/bt/bt_cooldown.h diff --git a/limboai/bt_decorator.cpp b/limboai/bt/bt_decorator.cpp similarity index 100% rename from limboai/bt_decorator.cpp rename to limboai/bt/bt_decorator.cpp diff --git a/limboai/bt_decorator.h b/limboai/bt/bt_decorator.h similarity index 100% rename from limboai/bt_decorator.h rename to limboai/bt/bt_decorator.h diff --git a/limboai/bt_delay.cpp b/limboai/bt/bt_delay.cpp similarity index 100% rename from limboai/bt_delay.cpp rename to limboai/bt/bt_delay.cpp diff --git a/limboai/bt_delay.h b/limboai/bt/bt_delay.h similarity index 100% rename from limboai/bt_delay.h rename to limboai/bt/bt_delay.h diff --git a/limboai/bt_invert.cpp b/limboai/bt/bt_invert.cpp similarity index 100% rename from limboai/bt_invert.cpp rename to limboai/bt/bt_invert.cpp diff --git a/limboai/bt_invert.h b/limboai/bt/bt_invert.h similarity index 100% rename from limboai/bt_invert.h rename to limboai/bt/bt_invert.h diff --git a/limboai/bt_parallel.cpp b/limboai/bt/bt_parallel.cpp similarity index 100% rename from limboai/bt_parallel.cpp rename to limboai/bt/bt_parallel.cpp diff --git a/limboai/bt_parallel.h b/limboai/bt/bt_parallel.h similarity index 100% rename from limboai/bt_parallel.h rename to limboai/bt/bt_parallel.h diff --git a/limboai/bt/bt_probability.cpp b/limboai/bt/bt_probability.cpp new file mode 100644 index 0000000..e70c2fc --- /dev/null +++ b/limboai/bt/bt_probability.cpp @@ -0,0 +1,21 @@ +/* bt_probability.cpp */ + +#include "bt_probability.h" +#include "core/object.h" + +String BTProbability::_generate_name() const { + return vformat("Probability %.1f%%", run_chance); +} + +int BTProbability::_tick(float p_delta) { + if (get_child(0)->get_status() == RUNNING or Math::randf() <= run_chance) { + return get_child(0)->execute(p_delta); + } + return FAILURE; +} + +void BTProbability::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_run_chance", "p_value"), &BTProbability::set_run_chance); + ClassDB::bind_method(D_METHOD("get_run_chance"), &BTProbability::get_run_chance); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "run_chance", PROPERTY_HINT_RANGE, "0.0,1.0"), "set_run_chance", "get_run_chance"); +} diff --git a/limboai/bt/bt_probability.h b/limboai/bt/bt_probability.h new file mode 100644 index 0000000..3e73202 --- /dev/null +++ b/limboai/bt/bt_probability.h @@ -0,0 +1,29 @@ +/* bt_probability.h */ + +#ifndef BT_PROBABILITY_H +#define BT_PROBABILITY_H + +#include "bt_decorator.h" +#include "core/object.h" + +class BTProbability : public BTDecorator { + GDCLASS(BTProbability, BTDecorator); + +private: + float run_chance = 0.5; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + virtual int _tick(float p_delta); + +public: + void set_run_chance(float p_value) { + run_chance = p_value; + emit_changed(); + } + float get_run_chance() const { return run_chance; } +}; + +#endif // BT_PROBABILITY_H \ No newline at end of file diff --git a/limboai/bt_repeat.cpp b/limboai/bt/bt_repeat.cpp similarity index 100% rename from limboai/bt_repeat.cpp rename to limboai/bt/bt_repeat.cpp diff --git a/limboai/bt_repeat.h b/limboai/bt/bt_repeat.h similarity index 100% rename from limboai/bt_repeat.h rename to limboai/bt/bt_repeat.h diff --git a/limboai/bt_repeat_until_failure.cpp b/limboai/bt/bt_repeat_until_failure.cpp similarity index 100% rename from limboai/bt_repeat_until_failure.cpp rename to limboai/bt/bt_repeat_until_failure.cpp diff --git a/limboai/bt_repeat_until_failure.h b/limboai/bt/bt_repeat_until_failure.h similarity index 100% rename from limboai/bt_repeat_until_failure.h rename to limboai/bt/bt_repeat_until_failure.h diff --git a/limboai/bt_repeat_until_success.cpp b/limboai/bt/bt_repeat_until_success.cpp similarity index 100% rename from limboai/bt_repeat_until_success.cpp rename to limboai/bt/bt_repeat_until_success.cpp diff --git a/limboai/bt_repeat_until_success.h b/limboai/bt/bt_repeat_until_success.h similarity index 100% rename from limboai/bt_repeat_until_success.h rename to limboai/bt/bt_repeat_until_success.h diff --git a/limboai/bt_run_limit.cpp b/limboai/bt/bt_run_limit.cpp similarity index 100% rename from limboai/bt_run_limit.cpp rename to limboai/bt/bt_run_limit.cpp diff --git a/limboai/bt_run_limit.h b/limboai/bt/bt_run_limit.h similarity index 100% rename from limboai/bt_run_limit.h rename to limboai/bt/bt_run_limit.h diff --git a/limboai/bt_selector.cpp b/limboai/bt/bt_selector.cpp similarity index 100% rename from limboai/bt_selector.cpp rename to limboai/bt/bt_selector.cpp diff --git a/limboai/bt_selector.h b/limboai/bt/bt_selector.h similarity index 100% rename from limboai/bt_selector.h rename to limboai/bt/bt_selector.h diff --git a/limboai/bt_sequence.cpp b/limboai/bt/bt_sequence.cpp similarity index 100% rename from limboai/bt_sequence.cpp rename to limboai/bt/bt_sequence.cpp diff --git a/limboai/bt_sequence.h b/limboai/bt/bt_sequence.h similarity index 100% rename from limboai/bt_sequence.h rename to limboai/bt/bt_sequence.h diff --git a/limboai/bt_task.cpp b/limboai/bt/bt_task.cpp similarity index 98% rename from limboai/bt_task.cpp rename to limboai/bt/bt_task.cpp index dd2d166..94ca285 100644 --- a/limboai/bt_task.cpp +++ b/limboai/bt/bt_task.cpp @@ -8,8 +8,8 @@ #include "core/variant.h" #include "editor/editor_node.h" -#include "limbo_string_names.h" -#include "limbo_utility.h" +#include "../limbo_string_names.h" +#include "../limbo_utility.h" String BTTask::_generate_name() const { if (get_script_instance()) { @@ -23,7 +23,7 @@ String BTTask::_generate_name() const { return name; } } - return get_class(); + return get_class().trim_prefix("BT"); } Array BTTask::_get_children() const { diff --git a/limboai/bt_task.h b/limboai/bt/bt_task.h similarity index 100% rename from limboai/bt_task.h rename to limboai/bt/bt_task.h diff --git a/limboai/bt_time_limit.cpp b/limboai/bt/bt_time_limit.cpp similarity index 100% rename from limboai/bt_time_limit.cpp rename to limboai/bt/bt_time_limit.cpp diff --git a/limboai/bt_time_limit.h b/limboai/bt/bt_time_limit.h similarity index 100% rename from limboai/bt_time_limit.h rename to limboai/bt/bt_time_limit.h diff --git a/limboai/register_types.cpp b/limboai/register_types.cpp index b4d76cf..fc270f0 100644 --- a/limboai/register_types.cpp +++ b/limboai/register_types.cpp @@ -4,24 +4,25 @@ #include "core/class_db.h" -#include "bt_action.h" -#include "bt_always_fail.h" -#include "bt_always_succeed.h" -#include "bt_composite.h" -#include "bt_condition.h" -#include "bt_cooldown.h" -#include "bt_decorator.h" -#include "bt_delay.h" -#include "bt_invert.h" -#include "bt_parallel.h" -#include "bt_repeat.h" -#include "bt_repeat_until_failure.h" -#include "bt_repeat_until_success.h" -#include "bt_run_limit.h" -#include "bt_selector.h" -#include "bt_sequence.h" -#include "bt_task.h" -#include "bt_time_limit.h" +#include "bt/bt_action.h" +#include "bt/bt_always_fail.h" +#include "bt/bt_always_succeed.h" +#include "bt/bt_composite.h" +#include "bt/bt_condition.h" +#include "bt/bt_cooldown.h" +#include "bt/bt_decorator.h" +#include "bt/bt_delay.h" +#include "bt/bt_invert.h" +#include "bt/bt_parallel.h" +#include "bt/bt_probability.h" +#include "bt/bt_repeat.h" +#include "bt/bt_repeat_until_failure.h" +#include "bt/bt_repeat_until_success.h" +#include "bt/bt_run_limit.h" +#include "bt/bt_selector.h" +#include "bt/bt_sequence.h" +#include "bt/bt_task.h" +#include "bt/bt_time_limit.h" #include "limbo_string_names.h" #include "limbo_utility.h" @@ -46,6 +47,7 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); LimboStringNames::create(); } diff --git a/limboai/register_types.h b/limboai/register_types.h index 8417c05..b91530c 100644 --- a/limboai/register_types.h +++ b/limboai/register_types.h @@ -1,4 +1,9 @@ /* register_types.h */ +#ifndef LIMBOAI_REGISTER_TYPES_H +#define LIMBOAI_REGISTER_TYPES_H + void register_limboai_types(); void unregister_limboai_types(); + +#endif // LIMBOAI_REGISTER_TYPES_H