2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_player.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.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-08-30 16:48:49 +00:00
|
|
|
|
|
|
|
#include "bt_player.h"
|
|
|
|
|
|
|
|
#include "bt_task.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
#include "modules/limboai/blackboard/blackboard.h"
|
|
|
|
#include "modules/limboai/editor/debugger/limbo_debugger.h"
|
|
|
|
#include "modules/limboai/util/limbo_string_names.h"
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/config/engine.h"
|
2022-12-16 14:29:36 +00:00
|
|
|
#include "core/error/error_macros.h"
|
2022-08-30 16:48:49 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/object/class_db.h"
|
|
|
|
#include "core/object/object.h"
|
2022-09-08 13:56:51 +00:00
|
|
|
#include "core/os/memory.h"
|
2023-04-15 13:49:34 +00:00
|
|
|
#include "core/string/string_name.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/variant/variant.h"
|
2023-04-15 13:49:34 +00:00
|
|
|
#include "main/performance.h"
|
2022-08-30 16:48:49 +00:00
|
|
|
|
|
|
|
VARIANT_ENUM_CAST(BTPlayer::UpdateMode);
|
|
|
|
|
|
|
|
void BTPlayer::_load_tree() {
|
2023-04-13 07:29:45 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (tree_instance.is_valid()) {
|
|
|
|
LimboDebugger::get_singleton()->unregister_bt_instance(tree_instance, get_path());
|
|
|
|
}
|
|
|
|
#endif
|
2022-12-16 14:29:36 +00:00
|
|
|
tree_instance.unref();
|
|
|
|
ERR_FAIL_COND_MSG(!behavior_tree.is_valid(), "BTPlayer: Needs a valid behavior tree.");
|
|
|
|
ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "BTPlayer: Behavior tree has no valid root task.");
|
2022-10-19 14:01:16 +00:00
|
|
|
if (prefetch_nodepath_vars == true) {
|
|
|
|
blackboard->prefetch_nodepath_vars(this);
|
|
|
|
}
|
2022-12-16 14:29:36 +00:00
|
|
|
tree_instance = behavior_tree->instantiate(get_owner(), blackboard);
|
2023-04-13 07:29:45 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path());
|
|
|
|
#endif
|
2022-08-30 16:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTPlayer::set_behavior_tree(const Ref<BehaviorTree> &p_tree) {
|
|
|
|
behavior_tree = p_tree;
|
2022-09-05 14:30:41 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint() == false && get_owner()) {
|
2022-08-30 16:48:49 +00:00
|
|
|
_load_tree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTPlayer::set_update_mode(UpdateMode p_mode) {
|
|
|
|
update_mode = p_mode;
|
|
|
|
set_active(active);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTPlayer::set_active(bool p_active) {
|
|
|
|
active = p_active;
|
2022-12-16 14:29:36 +00:00
|
|
|
bool is_not_editor = !Engine::get_singleton()->is_editor_hint();
|
|
|
|
set_process(update_mode == UpdateMode::IDLE && active && is_not_editor);
|
|
|
|
set_physics_process(update_mode == UpdateMode::PHYSICS && active && is_not_editor);
|
|
|
|
set_process_input(active && is_not_editor);
|
2022-08-30 16:48:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 08:08:11 +00:00
|
|
|
void BTPlayer::update(double p_delta) {
|
2022-12-16 14:29:36 +00:00
|
|
|
if (!tree_instance.is_valid()) {
|
|
|
|
ERR_PRINT_ONCE(vformat("BTPlayer doesn't have a behavior tree with a valid root task to execute (owner: %s)", get_owner()));
|
2022-08-30 16:48:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-04-15 13:49:34 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
double start = OS::get_singleton()->get_ticks_usec();
|
|
|
|
#endif
|
|
|
|
|
2022-08-30 16:48:49 +00:00
|
|
|
if (active) {
|
2022-12-16 14:29:36 +00:00
|
|
|
last_status = tree_instance->execute(p_delta);
|
2023-04-13 07:29:45 +00:00
|
|
|
emit_signal(LimboStringNames::get_singleton()->updated, last_status);
|
2022-12-16 14:29:36 +00:00
|
|
|
if (last_status == BTTask::SUCCESS || last_status == BTTask::FAILURE) {
|
|
|
|
emit_signal(LimboStringNames::get_singleton()->behavior_tree_finished, last_status);
|
2022-08-30 16:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 13:49:34 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
double end = OS::get_singleton()->get_ticks_usec();
|
|
|
|
update_time_acc += (end - start);
|
|
|
|
update_time_n += 1.0;
|
|
|
|
#endif
|
2022-08-30 16:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTPlayer::restart() {
|
2022-12-16 14:29:36 +00:00
|
|
|
tree_instance->cancel();
|
2022-08-30 16:48:49 +00:00
|
|
|
set_active(true);
|
|
|
|
}
|
|
|
|
|
2023-04-15 13:49:34 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
|
|
|
|
void BTPlayer::_set_monitor_performance(bool p_monitor_performance) {
|
|
|
|
monitor_performance = p_monitor_performance;
|
|
|
|
|
|
|
|
if (!get_owner()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Performance *perf = Performance::get_singleton();
|
|
|
|
if (monitor_performance) {
|
|
|
|
if (monitor_id == StringName()) {
|
|
|
|
monitor_id = vformat("limboai/update_ms|%s_%s_%s", get_owner()->get_name(), get_name(),
|
|
|
|
String(itos(get_instance_id())).md5_text().substr(0, 4));
|
|
|
|
}
|
|
|
|
if (!perf->has_custom_monitor(monitor_id)) {
|
|
|
|
perf->add_custom_monitor(monitor_id, callable_mp(this, &BTPlayer::_get_mean_update_time_msec), Vector<Variant>());
|
|
|
|
}
|
|
|
|
} else if (monitor_id != StringName() && perf->has_custom_monitor(monitor_id)) {
|
|
|
|
perf->remove_custom_monitor(monitor_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double BTPlayer::_get_mean_update_time_msec() {
|
|
|
|
if (update_time_n) {
|
|
|
|
double mean_time_msec = (update_time_acc * 0.001) / update_time_n;
|
|
|
|
update_time_acc = 0.0;
|
|
|
|
update_time_n = 0.0;
|
|
|
|
return mean_time_msec;
|
|
|
|
}
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
|
2022-08-30 16:48:49 +00:00
|
|
|
void BTPlayer::_notification(int p_notification) {
|
|
|
|
switch (p_notification) {
|
|
|
|
case NOTIFICATION_PROCESS: {
|
2022-12-16 14:29:36 +00:00
|
|
|
Variant time = get_process_delta_time();
|
|
|
|
update(time);
|
2022-08-30 16:48:49 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_PHYSICS_PROCESS: {
|
2023-04-14 08:17:08 +00:00
|
|
|
Variant time = get_physics_process_delta_time();
|
2022-12-16 14:29:36 +00:00
|
|
|
update(time);
|
2022-08-30 16:48:49 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_READY: {
|
|
|
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
if (behavior_tree.is_valid()) {
|
|
|
|
_load_tree();
|
|
|
|
}
|
|
|
|
set_active(active);
|
2023-04-15 13:49:34 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
_set_monitor_performance(monitor_performance);
|
|
|
|
#endif
|
2022-08-30 16:48:49 +00:00
|
|
|
}
|
|
|
|
} break;
|
2023-04-13 07:29:45 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
if (tree_instance.is_valid()) {
|
|
|
|
LimboDebugger::get_singleton()->unregister_bt_instance(tree_instance, get_path());
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
#endif
|
2022-08-30 16:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTPlayer::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_behavior_tree", "p_path"), &BTPlayer::set_behavior_tree);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_behavior_tree"), &BTPlayer::get_behavior_tree);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_update_mode", "p_mode"), &BTPlayer::set_update_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_update_mode"), &BTPlayer::get_update_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_active", "p_active"), &BTPlayer::set_active);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_active"), &BTPlayer::get_active);
|
2022-09-05 14:30:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_blackboard", "p_blackboard"), &BTPlayer::set_blackboard);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blackboard"), &BTPlayer::get_blackboard);
|
2022-10-19 14:01:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_prefetch_nodepath_vars", "p_value"), &BTPlayer::set_prefetch_nodepath_vars);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_prefetch_nodepath_vars"), &BTPlayer::get_prefetch_nodepath_vars);
|
2022-08-30 16:48:49 +00:00
|
|
|
|
2022-09-08 13:56:51 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_blackboard_data", "p_blackboard"), &BTPlayer::_set_blackboard_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_blackboard_data"), &BTPlayer::_get_blackboard_data);
|
|
|
|
|
2022-08-30 16:48:49 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("update", "p_delta"), &BTPlayer::update);
|
|
|
|
ClassDB::bind_method(D_METHOD("restart"), &BTPlayer::restart);
|
2022-12-16 14:29:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_last_status"), &BTPlayer::get_last_status);
|
2022-08-30 16:48:49 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "behavior_tree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_behavior_tree", "get_behavior_tree");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Idle,Physics,Manual"), "set_update_mode", "get_update_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "get_active");
|
2022-09-08 13:56:51 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "Blackboard", 0), "", "get_blackboard");
|
2022-11-04 12:26:49 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_blackboard_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_blackboard_data", "_get_blackboard_data");
|
2022-10-19 14:01:16 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefetch_nodepath_vars"), "set_prefetch_nodepath_vars", "get_prefetch_nodepath_vars");
|
2022-08-30 16:48:49 +00:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(IDLE);
|
|
|
|
BIND_ENUM_CONSTANT(PHYSICS);
|
|
|
|
BIND_ENUM_CONSTANT(MANUAL);
|
2022-08-31 15:50:49 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("behavior_tree_finished", PropertyInfo(Variant::INT, "p_status")));
|
2023-04-13 07:29:45 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("updated", PropertyInfo(Variant::INT, "p_status")));
|
2023-04-15 13:49:34 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_monitor_performance"), &BTPlayer::_set_monitor_performance);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_monitor_performance"), &BTPlayer::_get_monitor_performance);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitor_performance"), "_set_monitor_performance", "_get_monitor_performance");
|
|
|
|
ADD_PROPERTY_DEFAULT("monitor_performance", false);
|
|
|
|
#endif // DEBUG_ENABLED
|
2022-09-08 13:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BTPlayer::BTPlayer() {
|
|
|
|
blackboard = Ref<Blackboard>(memnew(Blackboard));
|
|
|
|
}
|
|
|
|
|
|
|
|
BTPlayer::~BTPlayer() {
|
2023-04-15 13:49:34 +00:00
|
|
|
}
|