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-08-03 09:07:06 +00:00
|
|
|
Array BehaviorTreeData::serialize(const Ref<BTInstance> &p_instance) {
|
2024-02-03 15:31:21 +00:00
|
|
|
Array arr;
|
2024-08-03 09:07:06 +00:00
|
|
|
arr.push_back(uint64_t(p_instance->get_instance_id()));
|
|
|
|
arr.push_back(p_instance->get_owner_node() ? p_instance->get_owner_node()->get_path() : NodePath());
|
|
|
|
arr.push_back(p_instance->get_source_bt_path());
|
2024-02-03 15:31:21 +00:00
|
|
|
|
|
|
|
// Flatten tree into list depth first
|
|
|
|
List<Ref<BTTask>> stack;
|
2024-08-03 09:07:06 +00:00
|
|
|
stack.push_back(p_instance->get_root_task());
|
2024-02-03 15:31:21 +00:00
|
|
|
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) {
|
2024-08-03 09:07:06 +00:00
|
|
|
ERR_FAIL_COND_V(p_array.size() < 3, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[0].get_type() != Variant::INT, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[1].get_type() != Variant::NODE_PATH, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_array[2].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);
|
2024-08-03 09:07:06 +00:00
|
|
|
data->bt_instance_id = uint64_t(p_array[0]);
|
|
|
|
data->node_owner_path = p_array[1];
|
|
|
|
data->source_bt_path = p_array[2];
|
2023-12-12 22:37:28 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
int idx = 3;
|
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-08-03 09:07:06 +00:00
|
|
|
Ref<BehaviorTreeData> BehaviorTreeData::create_from_bt_instance(const Ref<BTInstance> &p_bt_instance) {
|
2024-02-03 15:31:21 +00:00
|
|
|
Ref<BehaviorTreeData> data = memnew(BehaviorTreeData);
|
2023-12-12 22:37:28 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
data->bt_instance_id = p_bt_instance->get_instance_id();
|
|
|
|
data->node_owner_path = p_bt_instance->get_owner_node() ? p_bt_instance->get_owner_node()->get_path() : NodePath();
|
|
|
|
data->source_bt_path = p_bt_instance->get_source_bt_path();
|
|
|
|
|
2023-12-12 22:37:28 +00:00
|
|
|
// Flatten tree into list depth first
|
|
|
|
List<Ref<BTTask>> stack;
|
2024-08-03 09:07:06 +00:00
|
|
|
stack.push_back(p_bt_instance->get_root_task());
|
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-08-03 09:07:06 +00:00
|
|
|
ClassDB::bind_static_method("BehaviorTreeData", D_METHOD("create_from_bt_instance", "bt_instance"), &BehaviorTreeData::create_from_bt_instance);
|
2024-02-03 15:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BehaviorTreeData::BehaviorTreeData() {
|
2023-12-12 22:37:28 +00:00
|
|
|
}
|