Add NewScope and refactor Subtree

This commit is contained in:
Serhii Snitsaruk 2022-09-20 19:15:48 +02:00
parent e4f92893a1
commit a3cac93174
8 changed files with 122 additions and 40 deletions

View File

@ -1,35 +0,0 @@
/* bt_subtree.cpp */
#include "bt_subtree.h"
#include "core/error_macros.h"
#include "core/object.h"
#include "core/variant.h"
#include "modules/limboai/bt/actions/bt_action.h"
String BTSubtree::_generate_name() const {
return vformat("Subtree '%s'", subtree.is_null() ? "?" : subtree->get_path());
}
Ref<BTTask> BTSubtree::clone() const {
ERR_FAIL_COND_V_MSG(!subtree.is_valid(), nullptr, vformat("Subtree is not valid (%s)", get_agent()));
ERR_FAIL_COND_V_MSG(!subtree->get_root_task().is_valid(), nullptr, vformat("Subtree root task is not valid (%s)", get_agent()));
return subtree->get_root_task()->clone();
}
String BTSubtree::get_configuration_warning() const {
String warning = BTAction::get_configuration_warning();
if (!warning.empty()) {
warning += "\n";
}
if (subtree.is_null()) {
warning += "Subtree needs to be assigned.\n";
}
return warning;
}
void BTSubtree::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_subtree", "p_value"), &BTSubtree::set_subtree);
ClassDB::bind_method(D_METHOD("get_subtree"), &BTSubtree::get_subtree);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "subtree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_subtree", "get_subtree");
}

View File

@ -57,7 +57,7 @@ public:
void set_custom_name(const String &p_name);
String get_task_name() const;
void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
virtual void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
virtual Ref<BTTask> clone() const;
int execute(float p_delta);
void cancel();

View File

@ -0,0 +1,26 @@
/* bt_new_scope.cpp */
#include "bt_new_scope.h"
#include "core/error_macros.h"
#include "core/ustring.h"
void BTNewScope::initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard) {
ERR_FAIL_COND(p_agent == nullptr);
ERR_FAIL_COND(p_blackboard == nullptr);
Ref<Blackboard> bb = memnew(Blackboard);
bb->set_parent_scope(p_blackboard);
bb->set_data(blackboard_data);
BTDecorator::initialize(p_agent, bb);
}
int BTNewScope::_tick(float p_delta) {
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator doesn't have a child.");
return get_child(0)->execute(p_delta);
}
void BTNewScope::_bind_methods() {
ClassDB::bind_method(D_METHOD("_set_blackboard_data", "p_data"), &BTNewScope::_set_blackboard_data);
ClassDB::bind_method(D_METHOD("_get_blackboard_data"), &BTNewScope::_get_blackboard_data);
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_blackboard_data"), "_set_blackboard_data", "_get_blackboard_data");
}

View File

@ -0,0 +1,28 @@
/* bt_new_scope.h */
#ifndef BT_NEW_SCOPE_H
#define BT_NEW_SCOPE_H
#include "bt_decorator.h"
#include "core/object.h"
#include "bt_decorator.h"
class BTNewScope : public BTDecorator {
GDCLASS(BTNewScope, BTDecorator);
private:
Dictionary blackboard_data;
protected:
static void _bind_methods();
void _set_blackboard_data(const Dictionary &p_value) { blackboard_data = p_value; }
Dictionary _get_blackboard_data() const { return blackboard_data; }
virtual int _tick(float p_delta);
public:
virtual void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
};
#endif // BT_NEW_SCOPE_H

View File

@ -0,0 +1,59 @@
/* bt_subtree.cpp */
#include "bt_subtree.h"
#include "core/engine.h"
#include "core/error_macros.h"
#include "core/object.h"
#include "core/typedefs.h"
#include "core/variant.h"
#include "modules/limboai/blackboard.h"
#include "modules/limboai/bt/actions/bt_action.h"
#include "modules/limboai/bt/actions/bt_fail.h"
#include "modules/limboai/bt/bt_task.h"
#include "modules/limboai/bt/decorators/bt_decorator.h"
String BTSubtree::_generate_name() const {
String s;
if (subtree.is_null()) {
s = "(unassigned)";
} else if (subtree->get_path().empty()) {
s = "(unsaved)";
} else {
s = vformat("\"%s\"", subtree->get_path());
}
return vformat("Subtree %s", s);
}
Ref<BTTask> BTSubtree::clone() const {
Ref<BTTask> copy = BTDecorator::clone();
if (!Engine::get_singleton()->is_editor_hint()) {
ERR_FAIL_COND_V_MSG(!subtree.is_valid(), copy, "Subtree is not assigned.");
ERR_FAIL_COND_V_MSG(!subtree->get_root_task().is_valid(), copy, "Subtree root task is not valid.");
copy->add_child(subtree->get_root_task()->clone());
}
return copy;
}
int BTSubtree::_tick(float p_delta) {
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator doesn't have a child.");
return get_child(0)->execute(p_delta);
}
String BTSubtree::get_configuration_warning() const {
String warning = BTTask::get_configuration_warning(); // BTDecorator skipped intentionally
if (!warning.empty()) {
warning += "\n";
}
if (subtree.is_null()) {
warning += "Subtree needs to be assigned.\n";
}
return warning;
}
void BTSubtree::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_subtree", "p_value"), &BTSubtree::set_subtree);
ClassDB::bind_method(D_METHOD("get_subtree"), &BTSubtree::get_subtree);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "subtree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_subtree", "get_subtree");
}

View File

@ -3,12 +3,12 @@
#ifndef BT_SUBTREE_H
#define BT_SUBTREE_H
#include "bt_action.h"
#include "bt_new_scope.h"
#include "core/object.h"
#include "modules/limboai/bt/behavior_tree.h"
class BTSubtree : public BTAction {
GDCLASS(BTSubtree, BTAction);
class BTSubtree : public BTNewScope {
GDCLASS(BTSubtree, BTNewScope);
private:
Ref<BehaviorTree> subtree;
@ -17,6 +17,7 @@ protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual int _tick(float p_delta);
public:
void set_subtree(const Ref<BehaviorTree> &p_value) {

View File

@ -0,0 +1 @@
<svg enable-background="new 0 0 16 16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><g fill="#cea4f1"><path d="m2.91 14.5c-1.37-2.27-1.91-4.36-1.91-6.5s.54-4.23 1.91-6.5l1.09 1.06c-1.26 2.37-1.51 3.87-1.51 5.44s.25 3.08 1.51 5.44z"/><path d="m13.09 1.5c1.37 2.27 1.91 4.36 1.91 6.5s-.54 4.23-1.91 6.5l-1.09-1.06c1.26-2.37 1.51-3.87 1.51-5.44s-.25-3.08-1.51-5.44z"/><path d="m10.87 11.43h-1.83l-1.21-2.33-1.93 2.33h-1.9l2.96-3.53-1.84-3.47h1.89l1.18 2.23 1.86-2.23h1.95l-2.98 3.49z"/></g></svg>

After

Width:  |  Height:  |  Size: 503 B

View File

@ -9,7 +9,6 @@
#include "bt/actions/bt_console_print.h"
#include "bt/actions/bt_fail.h"
#include "bt/actions/bt_random_wait.h"
#include "bt/actions/bt_subtree.h"
#include "bt/actions/bt_wait.h"
#include "bt/actions/bt_wait_ticks.h"
#include "bt/behavior_tree.h"
@ -30,11 +29,13 @@
#include "bt/decorators/bt_decorator.h"
#include "bt/decorators/bt_delay.h"
#include "bt/decorators/bt_invert.h"
#include "bt/decorators/bt_new_scope.h"
#include "bt/decorators/bt_probability.h"
#include "bt/decorators/bt_repeat.h"
#include "bt/decorators/bt_repeat_until_failure.h"
#include "bt/decorators/bt_repeat_until_success.h"
#include "bt/decorators/bt_run_limit.h"
#include "bt/decorators/bt_subtree.h"
#include "bt/decorators/bt_time_limit.h"
#include "limbo_string_names.h"
#include "limbo_utility.h"
@ -77,6 +78,7 @@ void register_limboai_types() {
ClassDB::register_class<BTWait>();
ClassDB::register_class<BTRandomWait>();
ClassDB::register_class<BTWaitTicks>();
ClassDB::register_class<BTNewScope>();
ClassDB::register_class<BTSubtree>();
ClassDB::register_class<BTConsolePrint>();