2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* behavior_tree_data.cpp
|
|
|
|
* =============================================================================
|
2024-03-21 20:38:57 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2023-04-13 07:29:45 +00:00
|
|
|
|
|
|
|
#include "behavior_tree_data.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-07 01:40:51 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2023-04-13 07:29:45 +00:00
|
|
|
#include "core/templates/list.h"
|
2024-01-07 01:40:51 +00:00
|
|
|
#endif
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-01-13 16:10:42 +00:00
|
|
|
//**** BehaviorTreeData
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-02-03 15:31:21 +00:00
|
|
|
Array BehaviorTreeData::serialize(const Ref<BTTask> &p_tree_instance, const NodePath &p_player_path, const String &p_bt_resource_path) {
|
|
|
|
Array arr;
|
|
|
|
arr.push_back(p_player_path);
|
|
|
|
arr.push_back(p_bt_resource_path);
|
|
|
|
|
|
|
|
// Flatten tree into list depth first
|
|
|
|
List<Ref<BTTask>> stack;
|
|
|
|
stack.push_back(p_tree_instance);
|
|
|
|
while (stack.size()) {
|
2024-05-07 13:17:04 +00:00
|
|
|
Ref<BTTask> task = stack.front()->get();
|
2024-02-03 15:31:21 +00:00
|
|
|
stack.pop_front();
|
|
|
|
|
|
|
|
int num_children = task->get_child_count();
|
|
|
|
for (int i = 0; i < num_children; i++) {
|
|
|
|
stack.push_front(task->get_child(num_children - 1 - i));
|
|
|
|
}
|
|
|
|
|
|
|
|
String script_path;
|
|
|
|
if (task->get_script()) {
|
|
|
|
Ref<Resource> s = task->get_script();
|
|
|
|
script_path = s->get_path();
|
|
|
|
}
|
|
|
|
|
2024-02-17 10:42:48 +00:00
|
|
|
arr.push_back(task->get_instance_id());
|
2024-02-03 15:31:21 +00:00
|
|
|
arr.push_back(task->get_task_name());
|
|
|
|
arr.push_back(!task->get_custom_name().is_empty());
|
|
|
|
arr.push_back(num_children);
|
|
|
|
arr.push_back(task->get_status());
|
|
|
|
arr.push_back(task->get_elapsed_time());
|
|
|
|
arr.push_back(task->get_class());
|
|
|
|
arr.push_back(script_path);
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 15:31:21 +00:00
|
|
|
return arr;
|
|
|
|
}
|
2023-04-16 06:26:47 +00:00
|
|
|
|
2024-02-03 15:31:21 +00:00
|
|
|
Ref<BehaviorTreeData> BehaviorTreeData::deserialize(const Array &p_array) {
|
|
|
|
ERR_FAIL_COND_V(p_array.size() < 2, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[0].get_type() != Variant::NODE_PATH, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[1].get_type() != Variant::STRING, nullptr);
|
2023-04-16 06:26:47 +00:00
|
|
|
|
2024-02-03 15:31:21 +00:00
|
|
|
Ref<BehaviorTreeData> data = memnew(BehaviorTreeData);
|
|
|
|
data->bt_player_path = p_array[0];
|
|
|
|
data->bt_resource_path = p_array[1];
|
2023-12-12 22:37:28 +00:00
|
|
|
|
|
|
|
int idx = 2;
|
2024-02-03 15:31:21 +00:00
|
|
|
while (p_array.size() > idx + 1) {
|
|
|
|
ERR_FAIL_COND_V(p_array.size() < idx + 7, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx].get_type() != Variant::INT, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 1].get_type() != Variant::STRING, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 2].get_type() != Variant::BOOL, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 3].get_type() != Variant::INT, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 4].get_type() != Variant::INT, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 5].get_type() != Variant::FLOAT, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 6].get_type() != Variant::STRING, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[idx + 7].get_type() != Variant::STRING, nullptr);
|
|
|
|
data->tasks.push_back(TaskData(p_array[idx], p_array[idx + 1], p_array[idx + 2], p_array[idx + 3], p_array[idx + 4], p_array[idx + 5], p_array[idx + 6], p_array[idx + 7]));
|
2023-08-29 08:45:00 +00:00
|
|
|
idx += 8;
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
2024-02-03 15:31:21 +00:00
|
|
|
|
|
|
|
return data;
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
2023-12-12 22:37:28 +00:00
|
|
|
|
2024-02-03 15:31:21 +00:00
|
|
|
Ref<BehaviorTreeData> BehaviorTreeData::create_from_tree_instance(const Ref<BTTask> &p_tree_instance) {
|
|
|
|
Ref<BehaviorTreeData> data = memnew(BehaviorTreeData);
|
2023-12-12 22:37:28 +00:00
|
|
|
|
|
|
|
// Flatten tree into list depth first
|
|
|
|
List<Ref<BTTask>> stack;
|
2024-02-03 15:31:21 +00:00
|
|
|
stack.push_back(p_tree_instance);
|
2023-12-12 22:37:28 +00:00
|
|
|
while (stack.size()) {
|
2024-05-07 13:17:04 +00:00
|
|
|
Ref<BTTask> task = stack.front()->get();
|
2023-12-12 22:37:28 +00:00
|
|
|
stack.pop_front();
|
|
|
|
|
|
|
|
int num_children = task->get_child_count();
|
|
|
|
for (int i = 0; i < num_children; i++) {
|
|
|
|
stack.push_front(task->get_child(num_children - 1 - i));
|
|
|
|
}
|
|
|
|
|
|
|
|
String script_path;
|
|
|
|
if (task->get_script()) {
|
2024-02-03 15:31:21 +00:00
|
|
|
Ref<Resource> s = task->get_script();
|
|
|
|
script_path = s->get_path();
|
2023-12-12 22:37:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 15:31:21 +00:00
|
|
|
data->tasks.push_back(TaskData(
|
2024-02-17 10:42:48 +00:00
|
|
|
task->get_instance_id(),
|
2023-12-12 22:37:28 +00:00
|
|
|
task->get_task_name(),
|
|
|
|
!task->get_custom_name().is_empty(),
|
|
|
|
num_children,
|
|
|
|
task->get_status(),
|
|
|
|
task->get_elapsed_time(),
|
|
|
|
task->get_class(),
|
|
|
|
script_path));
|
|
|
|
}
|
2024-02-03 15:31:21 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BehaviorTreeData::_bind_methods() {
|
2024-02-03 16:28:56 +00:00
|
|
|
// ClassDB::bind_static_method("BehaviorTreeData", D_METHOD("serialize", "p_tree_instance", "p_player_path", "p_bt_resource_path"), &BehaviorTreeData::serialize);
|
|
|
|
// ClassDB::bind_static_method("BehaviorTreeData", D_METHOD("deserialize", "p_array"), &BehaviorTreeData::deserialize);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_static_method("BehaviorTreeData", D_METHOD("create_from_tree_instance", "tree_instance"), &BehaviorTreeData::create_from_tree_instance);
|
2024-02-03 15:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BehaviorTreeData::BehaviorTreeData() {
|
2023-12-12 22:37:28 +00:00
|
|
|
}
|