2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* behavior_tree_data.cpp
|
|
|
|
* =============================================================================
|
|
|
|
* Copyright 2021-2023 Serhii Snitsaruk
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
void BehaviorTreeData::serialize(Array &p_arr) {
|
2023-04-15 07:01:37 +00:00
|
|
|
p_arr.push_back(bt_player_path);
|
2023-12-12 22:37:28 +00:00
|
|
|
p_arr.push_back(bt_resource_path);
|
2023-04-13 07:29:45 +00:00
|
|
|
for (const TaskData &td : tasks) {
|
2023-04-14 08:16:26 +00:00
|
|
|
p_arr.push_back(td.id);
|
2023-04-13 07:29:45 +00:00
|
|
|
p_arr.push_back(td.name);
|
2023-08-29 08:45:00 +00:00
|
|
|
p_arr.push_back(td.is_custom_name);
|
2023-04-13 07:29:45 +00:00
|
|
|
p_arr.push_back(td.num_children);
|
|
|
|
p_arr.push_back(td.status);
|
2023-04-14 08:16:26 +00:00
|
|
|
p_arr.push_back(td.elapsed_time);
|
2023-04-13 07:29:45 +00:00
|
|
|
p_arr.push_back(td.type_name);
|
2023-04-16 06:26:47 +00:00
|
|
|
p_arr.push_back(td.script_path);
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BehaviorTreeData::deserialize(const Array &p_arr) {
|
|
|
|
ERR_FAIL_COND(tasks.size() != 0);
|
2023-12-12 22:37:28 +00:00
|
|
|
ERR_FAIL_COND(p_arr.size() < 2);
|
2023-04-16 06:26:47 +00:00
|
|
|
|
2023-04-15 07:01:37 +00:00
|
|
|
ERR_FAIL_COND(p_arr[0].get_type() != Variant::NODE_PATH);
|
|
|
|
bt_player_path = p_arr[0];
|
2023-04-16 06:26:47 +00:00
|
|
|
|
2023-12-12 22:37:28 +00:00
|
|
|
ERR_FAIL_COND(p_arr[1].get_type() != Variant::STRING);
|
|
|
|
bt_resource_path = p_arr[1];
|
|
|
|
|
|
|
|
int idx = 2;
|
2023-04-15 07:01:37 +00:00
|
|
|
while (p_arr.size() > idx + 1) {
|
2023-04-16 06:26:47 +00:00
|
|
|
ERR_FAIL_COND(p_arr.size() < idx + 7);
|
2023-04-14 08:16:26 +00:00
|
|
|
ERR_FAIL_COND(p_arr[idx].get_type() != Variant::INT);
|
|
|
|
ERR_FAIL_COND(p_arr[idx + 1].get_type() != Variant::STRING);
|
2023-08-29 08:45:00 +00:00
|
|
|
ERR_FAIL_COND(p_arr[idx + 2].get_type() != Variant::BOOL);
|
2023-04-13 07:29:45 +00:00
|
|
|
ERR_FAIL_COND(p_arr[idx + 3].get_type() != Variant::INT);
|
2023-08-29 08:45:00 +00:00
|
|
|
ERR_FAIL_COND(p_arr[idx + 4].get_type() != Variant::INT);
|
|
|
|
ERR_FAIL_COND(p_arr[idx + 5].get_type() != Variant::FLOAT);
|
2023-04-16 06:26:47 +00:00
|
|
|
ERR_FAIL_COND(p_arr[idx + 6].get_type() != Variant::STRING);
|
2023-08-29 08:45:00 +00:00
|
|
|
ERR_FAIL_COND(p_arr[idx + 7].get_type() != Variant::STRING);
|
|
|
|
tasks.push_back(TaskData(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5], p_arr[idx + 6], p_arr[idx + 7]));
|
|
|
|
idx += 8;
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-12 22:37:28 +00:00
|
|
|
|
|
|
|
BehaviorTreeData::BehaviorTreeData(const Ref<BTTask> &p_instance, const NodePath &p_player_path, const String &p_bt_resource) {
|
|
|
|
bt_player_path = p_player_path;
|
|
|
|
bt_resource_path = p_bt_resource;
|
|
|
|
|
|
|
|
// Flatten tree into list depth first
|
|
|
|
List<Ref<BTTask>> stack;
|
|
|
|
stack.push_back(p_instance);
|
|
|
|
int id = 0;
|
|
|
|
while (stack.size()) {
|
|
|
|
Ref<BTTask> task = stack[0];
|
|
|
|
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-01-07 01:40:51 +00:00
|
|
|
Ref<Resource> script = task->get_script();
|
2023-12-12 22:37:28 +00:00
|
|
|
script_path = script->get_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks.push_back(TaskData(
|
|
|
|
id,
|
|
|
|
task->get_task_name(),
|
|
|
|
!task->get_custom_name().is_empty(),
|
|
|
|
num_children,
|
|
|
|
task->get_status(),
|
|
|
|
task->get_elapsed_time(),
|
|
|
|
task->get_class(),
|
|
|
|
script_path));
|
|
|
|
id += 1;
|
|
|
|
}
|
|
|
|
}
|