From 8155c2764a52885fc89d70cb226899d525ce2926 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Thu, 25 Jan 2024 14:27:14 +0100 Subject: [PATCH] Refactoring and clean up --- blackboard/blackboard.cpp | 14 ++++++-------- blackboard/blackboard.h | 8 +++----- blackboard/blackboard_plan.cpp | 16 ++++------------ blackboard/blackboard_plan.h | 3 --- bt/behavior_tree.cpp | 12 +++++++++++- bt/behavior_tree.h | 14 ++++---------- bt/tasks/decorators/bt_new_scope.cpp | 2 +- demo/demo/ai/trees/waypoints.tres | 4 ++-- .../demo/examples/waypoints/example_waypoints.gd | 2 -- .../examples/waypoints/patrolling_agent.tscn | 4 ++-- editor/blackboard_plan_editor.h | 1 - hsm/limbo_hsm.cpp | 2 +- hsm/limbo_state.cpp | 2 +- tests/test_new_scope.h | 2 +- 14 files changed, 36 insertions(+), 50 deletions(-) diff --git a/blackboard/blackboard.cpp b/blackboard/blackboard.cpp index 68727a7..d056152 100644 --- a/blackboard/blackboard.cpp +++ b/blackboard/blackboard.cpp @@ -28,8 +28,8 @@ using namespace godot; Ref Blackboard::top() const { Ref bb(this); - while (bb->get_parent_scope().is_valid()) { - bb = bb->get_parent_scope(); + while (bb->get_parent().is_valid()) { + bb = bb->get_parent(); } return bb; } @@ -45,11 +45,11 @@ Variant Blackboard::get_var(const String &p_name, const Variant &p_default) cons } void Blackboard::set_var(const String &p_name, const Variant &p_value) { - // TODO: Check if p_value can be converted into required type! if (data.has(p_name)) { + // Not checking type - allowing duck-typing. data[p_name].set_value(p_value); } else { - BBVariable var; + BBVariable var(p_value.get_type()); var.set_value(p_value); data.insert(p_name, var); } @@ -85,11 +85,9 @@ void Blackboard::_bind_methods() { ClassDB::bind_method(D_METHOD("get_var", "p_name", "p_default"), &Blackboard::get_var, Variant()); ClassDB::bind_method(D_METHOD("set_var", "p_name", "p_value"), &Blackboard::set_var); ClassDB::bind_method(D_METHOD("has_var", "p_name"), &Blackboard::has_var); - ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent_scope); - ClassDB::bind_method(D_METHOD("get_parent_scope"), &Blackboard::get_parent_scope); + ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent); + ClassDB::bind_method(D_METHOD("get_parent_scope"), &Blackboard::get_parent); ClassDB::bind_method(D_METHOD("erase_var", "p_name"), &Blackboard::erase_var); ClassDB::bind_method(D_METHOD("prefetch_nodepath_vars", "p_node"), &Blackboard::prefetch_nodepath_vars); ClassDB::bind_method(D_METHOD("top"), &Blackboard::top); - // ClassDB::bind_method(D_METHOD("get_data"), &Blackboard::get_data); - // ClassDB::bind_method(D_METHOD("set_data", "p_data"), &Blackboard::set_data); } diff --git a/blackboard/blackboard.h b/blackboard/blackboard.h index 062d8b2..1b79f82 100644 --- a/blackboard/blackboard.h +++ b/blackboard/blackboard.h @@ -42,8 +42,8 @@ protected: static void _bind_methods(); public: - void set_parent_scope(const Ref &p_blackboard) { parent = p_blackboard; } - Ref get_parent_scope() const { return parent; } + void set_parent(const Ref &p_blackboard) { parent = p_blackboard; } + Ref get_parent() const { return parent; } Ref top() const; @@ -56,9 +56,7 @@ public: void prefetch_nodepath_vars(Node *p_node); - // TODO: Rework serialization API. - // void set_data(const Dictionary &p_value); - // Dictionary get_data() const; + // TODO: Add serialization API. }; #endif // BLACKBOARD_H diff --git a/blackboard/blackboard_plan.cpp b/blackboard/blackboard_plan.cpp index 261b60c..f359b5e 100644 --- a/blackboard/blackboard_plan.cpp +++ b/blackboard/blackboard_plan.cpp @@ -63,7 +63,10 @@ bool BlackboardPlan::_get(const StringName &p_name, Variant &r_ret) const { String var_name = prop_name.get_slicec('/', 1); String what = prop_name.get_slicec('/', 2); ERR_FAIL_COND_V(!data.has(var_name), false); - if (what == "type") { + + if (what == "name") { + r_ret = var_name; + } else if (what == "type") { r_ret = data[var_name].get_type(); } else if (what == "value") { r_ret = data[var_name].get_value(); @@ -111,17 +114,6 @@ void BlackboardPlan::set_base_plan(const Ref &p_base) { sync_with_base_plan(); } -void BlackboardPlan::set_value(const String &p_name, const Variant &p_value) { - ERR_FAIL_COND(!data.has(p_name)); - data.get(p_name).set_value(p_value); - emit_changed(); -} - -Variant BlackboardPlan::get_value(const String &p_name) const { - ERR_FAIL_COND_V(!data.has(p_name), Variant()); - return data.get(p_name).get_value(); -} - void BlackboardPlan::add_var(const String &p_name, const BBVariable &p_var) { ERR_FAIL_COND(data.has(p_name)); ERR_FAIL_COND(base.is_valid()); diff --git a/blackboard/blackboard_plan.h b/blackboard/blackboard_plan.h index ecd9376..869e851 100644 --- a/blackboard/blackboard_plan.h +++ b/blackboard/blackboard_plan.h @@ -39,9 +39,6 @@ public: void set_base_plan(const Ref &p_base); Ref get_base_plan() const { return base; } - void set_value(const String &p_name, const Variant &p_value); - Variant get_value(const String &p_name) const; - void add_var(const String &p_name, const BBVariable &p_var); void remove_var(const String &p_name); BBVariable get_var(const String &p_name); diff --git a/bt/behavior_tree.cpp b/bt/behavior_tree.cpp index 3c425d7..53d2442 100644 --- a/bt/behavior_tree.cpp +++ b/bt/behavior_tree.cpp @@ -22,6 +22,11 @@ #include "godot_cpp/core/error_macros.hpp" #endif // ! LIMBOAI_GDEXTENSION +void BehaviorTree::set_description(const String &p_value) { + description = p_value; + emit_changed(); +} + void BehaviorTree::set_blackboard_plan(const Ref &p_plan) { blackboard_plan = p_plan; if (blackboard_plan.is_null()) { @@ -30,6 +35,11 @@ void BehaviorTree::set_blackboard_plan(const Ref &p_plan) { emit_changed(); } +void BehaviorTree::set_root_task(const Ref &p_value) { + root_task = p_value; + emit_changed(); +} + Ref BehaviorTree::clone() const { Ref copy = duplicate(false); copy->set_path(""); @@ -64,8 +74,8 @@ void BehaviorTree::_bind_methods() { ClassDB::bind_method(D_METHOD("instantiate", "p_agent", "p_blackboard"), &BehaviorTree::instantiate); ADD_PROPERTY(PropertyInfo(Variant::STRING, "description", PROPERTY_HINT_MULTILINE_TEXT), "set_description", "get_description"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_root_task", "get_root_task"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard_plan", PROPERTY_HINT_RESOURCE_TYPE, "BlackboardPlan", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_blackboard_plan", "get_blackboard_plan"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_root_task", "get_root_task"); } BehaviorTree::BehaviorTree() { diff --git a/bt/behavior_tree.h b/bt/behavior_tree.h index a92fa56..c04cd11 100644 --- a/bt/behavior_tree.h +++ b/bt/behavior_tree.h @@ -40,19 +40,13 @@ public: virtual bool editor_can_reload_from_file() override { return false; } #endif + void set_description(const String &p_value); + String get_description() const { return description; } + void set_blackboard_plan(const Ref &p_plan); Ref get_blackboard_plan() const { return blackboard_plan; } - void set_description(String p_value) { - description = p_value; - emit_changed(); - } - String get_description() const { return description; } - - void set_root_task(const Ref &p_value) { - root_task = p_value; - emit_changed(); - } + void set_root_task(const Ref &p_value); Ref get_root_task() const { return root_task; } Ref clone() const; diff --git a/bt/tasks/decorators/bt_new_scope.cpp b/bt/tasks/decorators/bt_new_scope.cpp index 24e38d8..dd462f1 100644 --- a/bt/tasks/decorators/bt_new_scope.cpp +++ b/bt/tasks/decorators/bt_new_scope.cpp @@ -22,7 +22,7 @@ void BTNewScope::initialize(Node *p_agent, const Ref &p_blackboard) bb = Ref(memnew(Blackboard)); } - bb->set_parent_scope(p_blackboard); + bb->set_parent(p_blackboard); BTDecorator::initialize(p_agent, bb); } diff --git a/demo/demo/ai/trees/waypoints.tres b/demo/demo/ai/trees/waypoints.tres index 5254c91..e48e485 100644 --- a/demo/demo/ai/trees/waypoints.tres +++ b/demo/demo/ai/trees/waypoints.tres @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://demo/ai/tasks/arrive_pos.gd" id="1_rhs33"] [sub_resource type="BlackboardPlan" id="BlackboardPlan_2hcqi"] -var/speed/name = null +var/speed/name = "speed" var/speed/type = 3 var/speed/value = 200.0 var/speed/hint = 1 @@ -43,5 +43,5 @@ duration = 3.0 children = [SubResource("BTCooldown_gen0l")] [resource] -root_task = SubResource("BTSelector_5dclr") blackboard_plan = SubResource("BlackboardPlan_2hcqi") +root_task = SubResource("BTSelector_5dclr") diff --git a/demo/demo/examples/waypoints/example_waypoints.gd b/demo/demo/examples/waypoints/example_waypoints.gd index 19d153d..c9ddbe8 100644 --- a/demo/demo/examples/waypoints/example_waypoints.gd +++ b/demo/demo/examples/waypoints/example_waypoints.gd @@ -24,5 +24,3 @@ func _ready() -> void: waypoints.reverse() for wp in waypoints: agent2.add_waypoint(wp.global_position) - - diff --git a/demo/demo/examples/waypoints/patrolling_agent.tscn b/demo/demo/examples/waypoints/patrolling_agent.tscn index 4b1c609..a91dc6b 100644 --- a/demo/demo/examples/waypoints/patrolling_agent.tscn +++ b/demo/demo/examples/waypoints/patrolling_agent.tscn @@ -5,9 +5,9 @@ [ext_resource type="Texture2D" uid="uid://d0mht3ntak7e5" path="res://demo/godot.png" id="3_64ge2"] [sub_resource type="BlackboardPlan" id="BlackboardPlan_b86q8"] -var/speed/name = null +var/speed/name = "speed" var/speed/type = 3 -var/speed/value = 200.0 +var/speed/value = 300.0 var/speed/hint = 1 var/speed/hint_string = "10,1000,10" diff --git a/editor/blackboard_plan_editor.h b/editor/blackboard_plan_editor.h index 8812ab2..a69c9af 100644 --- a/editor/blackboard_plan_editor.h +++ b/editor/blackboard_plan_editor.h @@ -34,7 +34,6 @@ private: } theme_cache; int last_index = 0; - int drag_mouse_y_delta = 0; int drag_index = -1; diff --git a/hsm/limbo_hsm.cpp b/hsm/limbo_hsm.cpp index 1eb8616..8834f52 100644 --- a/hsm/limbo_hsm.cpp +++ b/hsm/limbo_hsm.cpp @@ -190,7 +190,7 @@ bool LimboHSM::dispatch(const String &p_event, const Variant &p_cargo) { void LimboHSM::initialize(Node *p_agent, const Ref &p_parent_scope) { ERR_FAIL_COND(p_agent == nullptr); if (!p_parent_scope.is_null()) { - blackboard->set_parent_scope(p_parent_scope); + blackboard->set_parent(p_parent_scope); } _initialize(p_agent, nullptr); diff --git a/hsm/limbo_state.cpp b/hsm/limbo_state.cpp index 7539ec3..d0e7166 100644 --- a/hsm/limbo_state.cpp +++ b/hsm/limbo_state.cpp @@ -71,7 +71,7 @@ void LimboState::_initialize(Node *p_agent, const Ref &p_blackboard) if (!p_blackboard.is_null()) { if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) { blackboard = blackboard_plan->create_blackboard(); - blackboard->set_parent_scope(p_blackboard); + blackboard->set_parent(p_blackboard); } else { blackboard = p_blackboard; } diff --git a/tests/test_new_scope.h b/tests/test_new_scope.h index a16a45b..421ffbd 100644 --- a/tests/test_new_scope.h +++ b/tests/test_new_scope.h @@ -50,7 +50,7 @@ TEST_CASE("[Modules][LimboAI] BTNewScope") { CHECK(ns->get_blackboard() != parent->get_blackboard()); CHECK(ns->get_blackboard() == child->get_blackboard()); CHECK(parent->get_blackboard() == parent_bb); - CHECK(ns->get_blackboard()->get_parent_scope() == parent_bb); + CHECK(ns->get_blackboard()->get_parent() == parent_bb); ns->get_blackboard()->set_var("fruit", "pear"); // * override "fruit"