2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_state.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-10-19 18:54:21 +00:00
|
|
|
|
|
|
|
#include "bt_state.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-07 04:54:17 +00:00
|
|
|
#include "../editor/debugger/limbo_debugger.h"
|
2024-01-09 12:34:24 +00:00
|
|
|
#include "../util/limbo_compat.h"
|
2024-01-07 04:54:17 +00:00
|
|
|
#include "../util/limbo_string_names.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-07 04:54:17 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2023-12-28 13:41:58 +00:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2022-12-16 14:29:36 +00:00
|
|
|
#include "core/error/error_macros.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/object/class_db.h"
|
|
|
|
#include "core/variant/variant.h"
|
2024-01-07 04:54:17 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
#include <godot_cpp/classes/engine_debugger.hpp>
|
|
|
|
#endif // LIMBOAI_GDEXTENSION
|
2022-10-19 18:54:21 +00:00
|
|
|
|
2024-01-23 14:31:56 +00:00
|
|
|
void BTState::_update_blackboard_source() {
|
|
|
|
if (behavior_tree.is_valid() && behavior_tree->get_blackboard_source().is_valid()) {
|
|
|
|
if (get_blackboard_source().is_null()) {
|
|
|
|
set_blackboard_source(Ref<BlackboardSource>(memnew(BlackboardSource)));
|
|
|
|
}
|
|
|
|
if (get_blackboard_source() == behavior_tree->get_blackboard_source()) {
|
2024-01-23 16:54:20 +00:00
|
|
|
get_blackboard_source()->sync_with_base_source();
|
2024-01-23 14:31:56 +00:00
|
|
|
} else {
|
|
|
|
get_blackboard_source()->set_base_source(behavior_tree->get_blackboard_source());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 18:54:21 +00:00
|
|
|
void BTState::_setup() {
|
2022-12-16 14:29:36 +00:00
|
|
|
ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned.");
|
|
|
|
tree_instance = behavior_tree->instantiate(get_agent(), get_blackboard());
|
2023-04-13 07:29:45 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2024-01-07 04:54:17 +00:00
|
|
|
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) {
|
2023-12-28 13:41:58 +00:00
|
|
|
LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path());
|
|
|
|
}
|
2023-04-13 07:29:45 +00:00
|
|
|
#endif
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTState::_exit() {
|
2022-12-16 14:29:36 +00:00
|
|
|
ERR_FAIL_COND(tree_instance == nullptr);
|
2023-10-26 14:20:33 +00:00
|
|
|
tree_instance->abort();
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 08:08:11 +00:00
|
|
|
void BTState::_update(double p_delta) {
|
2022-12-16 14:29:36 +00:00
|
|
|
ERR_FAIL_COND(tree_instance == nullptr);
|
|
|
|
int status = tree_instance->execute(p_delta);
|
2023-04-13 07:29:45 +00:00
|
|
|
emit_signal(LimboStringNames::get_singleton()->updated, p_delta);
|
2022-10-19 18:54:21 +00:00
|
|
|
if (status == BTTask::SUCCESS) {
|
2022-11-04 12:26:49 +00:00
|
|
|
get_root()->dispatch(success_event, Variant());
|
2022-10-19 18:54:21 +00:00
|
|
|
} else if (status == BTTask::FAILURE) {
|
2022-11-04 12:26:49 +00:00
|
|
|
get_root()->dispatch(failure_event, Variant());
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:29:45 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
void BTState::_notification(int p_notification) {
|
|
|
|
switch (p_notification) {
|
2023-09-09 11:37:09 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2024-01-07 04:54:17 +00:00
|
|
|
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) {
|
2023-09-09 11:37:09 +00:00
|
|
|
LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path());
|
|
|
|
}
|
|
|
|
} break;
|
2023-04-13 07:29:45 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2024-01-07 04:54:17 +00:00
|
|
|
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) {
|
2023-04-15 08:53:57 +00:00
|
|
|
LimboDebugger::get_singleton()->unregister_bt_instance(tree_instance, get_path());
|
|
|
|
}
|
2023-04-13 07:29:45 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
|
2022-10-19 18:54:21 +00:00
|
|
|
void BTState::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_behavior_tree", "p_value"), &BTState::set_behavior_tree);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_behavior_tree"), &BTState::get_behavior_tree);
|
|
|
|
|
2022-11-04 12:26:49 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_success_event", "p_event_name"), &BTState::set_success_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_success_event"), &BTState::get_success_event);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_failure_event", "p_event_name"), &BTState::set_failure_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_failure_event"), &BTState::get_failure_event);
|
|
|
|
|
2022-10-19 18:54:21 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "behavior_tree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_behavior_tree", "get_behavior_tree");
|
2022-11-04 12:26:49 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "success_event"), "set_success_event", "get_success_event");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "failure_event"), "set_failure_event", "get_failure_event");
|
2022-10-19 21:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BTState::BTState() {
|
2022-11-04 12:26:49 +00:00
|
|
|
success_event = "success";
|
|
|
|
failure_event = "failure";
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|