Add Probability decorator and reorganize

This commit is contained in:
Serhii Snitsaruk 2022-08-28 22:58:30 +02:00
parent cb46b5dff3
commit 8705241fa4
41 changed files with 80 additions and 22 deletions

View File

@ -2,4 +2,5 @@
Import('env') 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")

View File

@ -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");
}

View File

@ -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

View File

@ -8,8 +8,8 @@
#include "core/variant.h" #include "core/variant.h"
#include "editor/editor_node.h" #include "editor/editor_node.h"
#include "limbo_string_names.h" #include "../limbo_string_names.h"
#include "limbo_utility.h" #include "../limbo_utility.h"
String BTTask::_generate_name() const { String BTTask::_generate_name() const {
if (get_script_instance()) { if (get_script_instance()) {
@ -23,7 +23,7 @@ String BTTask::_generate_name() const {
return name; return name;
} }
} }
return get_class(); return get_class().trim_prefix("BT");
} }
Array BTTask::_get_children() const { Array BTTask::_get_children() const {

View File

@ -4,24 +4,25 @@
#include "core/class_db.h" #include "core/class_db.h"
#include "bt_action.h" #include "bt/bt_action.h"
#include "bt_always_fail.h" #include "bt/bt_always_fail.h"
#include "bt_always_succeed.h" #include "bt/bt_always_succeed.h"
#include "bt_composite.h" #include "bt/bt_composite.h"
#include "bt_condition.h" #include "bt/bt_condition.h"
#include "bt_cooldown.h" #include "bt/bt_cooldown.h"
#include "bt_decorator.h" #include "bt/bt_decorator.h"
#include "bt_delay.h" #include "bt/bt_delay.h"
#include "bt_invert.h" #include "bt/bt_invert.h"
#include "bt_parallel.h" #include "bt/bt_parallel.h"
#include "bt_repeat.h" #include "bt/bt_probability.h"
#include "bt_repeat_until_failure.h" #include "bt/bt_repeat.h"
#include "bt_repeat_until_success.h" #include "bt/bt_repeat_until_failure.h"
#include "bt_run_limit.h" #include "bt/bt_repeat_until_success.h"
#include "bt_selector.h" #include "bt/bt_run_limit.h"
#include "bt_sequence.h" #include "bt/bt_selector.h"
#include "bt_task.h" #include "bt/bt_sequence.h"
#include "bt_time_limit.h" #include "bt/bt_task.h"
#include "bt/bt_time_limit.h"
#include "limbo_string_names.h" #include "limbo_string_names.h"
#include "limbo_utility.h" #include "limbo_utility.h"
@ -46,6 +47,7 @@ void register_limboai_types() {
ClassDB::register_class<BTRunLimit>(); ClassDB::register_class<BTRunLimit>();
ClassDB::register_class<BTTimeLimit>(); ClassDB::register_class<BTTimeLimit>();
ClassDB::register_class<BTCooldown>(); ClassDB::register_class<BTCooldown>();
ClassDB::register_class<BTProbability>();
LimboStringNames::create(); LimboStringNames::create();
} }

View File

@ -1,4 +1,9 @@
/* register_types.h */ /* register_types.h */
#ifndef LIMBOAI_REGISTER_TYPES_H
#define LIMBOAI_REGISTER_TYPES_H
void register_limboai_types(); void register_limboai_types();
void unregister_limboai_types(); void unregister_limboai_types();
#endif // LIMBOAI_REGISTER_TYPES_H