2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* limbo_state.cpp
|
|
|
|
* =============================================================================
|
2024-03-04 14:53:39 +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.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-09-28 10:48:45 +00:00
|
|
|
|
|
|
|
#include "limbo_state.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-09 12:34:24 +00:00
|
|
|
#include "../util/limbo_compat.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-07 04:54:17 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
|
|
|
#endif // LIMBOAI_MODULE
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-01-07 04:54:17 +00:00
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
2024-03-12 20:09:28 +00:00
|
|
|
#include <godot_cpp/classes/engine.hpp>
|
2024-01-07 04:54:17 +00:00
|
|
|
#endif
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-03-11 17:58:40 +00:00
|
|
|
void LimboState::set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) {
|
|
|
|
blackboard_plan = p_plan;
|
2024-05-15 09:38:53 +00:00
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint() && blackboard_plan.is_valid()) {
|
2024-12-16 23:11:16 +00:00
|
|
|
blackboard_plan->set_parent_scope_plan_provider(Callable(this, "_get_parent_scope_plan"));
|
2024-05-15 09:38:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 17:58:40 +00:00
|
|
|
_update_blackboard_plan();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboState::_update_blackboard_plan() {
|
|
|
|
}
|
|
|
|
|
2024-09-15 12:29:23 +00:00
|
|
|
Node *LimboState::_get_prefetch_root_for_base_plan() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-05-15 09:38:53 +00:00
|
|
|
Ref<BlackboardPlan> LimboState::_get_parent_scope_plan() const {
|
|
|
|
BlackboardPlan *parent_plan = nullptr;
|
|
|
|
const LimboState *state = this;
|
|
|
|
while (state->get_parent() && IS_CLASS(state->get_parent(), LimboState)) {
|
|
|
|
state = Object::cast_to<LimboState>(state->get_parent());
|
|
|
|
ERR_FAIL_NULL_V(state, parent_plan);
|
|
|
|
if (state->blackboard_plan.is_valid()) {
|
|
|
|
parent_plan = state->blackboard_plan.ptr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent_plan;
|
|
|
|
}
|
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
LimboState *LimboState::get_root() const {
|
2024-04-21 14:08:37 +00:00
|
|
|
const Node *state = this;
|
2024-03-06 19:17:23 +00:00
|
|
|
while (state->get_parent() && IS_CLASS(state->get_parent(), LimboState)) {
|
2024-04-21 14:08:37 +00:00
|
|
|
state = state->get_parent();
|
2022-09-28 10:48:45 +00:00
|
|
|
}
|
2024-04-21 14:08:37 +00:00
|
|
|
return const_cast<LimboState *>(Object::cast_to<LimboState>(state));
|
2022-09-28 10:48:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
LimboState *LimboState::named(const String &p_name) {
|
2022-09-28 10:48:45 +00:00
|
|
|
set_name(p_name);
|
|
|
|
return this;
|
2024-01-18 10:32:32 +00:00
|
|
|
}
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
void LimboState::_enter() {
|
2022-09-28 10:48:45 +00:00
|
|
|
active = true;
|
2024-06-30 10:34:06 +00:00
|
|
|
GDVIRTUAL_CALL(_enter);
|
2024-08-21 18:04:35 +00:00
|
|
|
emit_signal(LW_NAME(entered));
|
2024-01-18 10:32:32 +00:00
|
|
|
}
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
void LimboState::_exit() {
|
2022-09-29 10:54:07 +00:00
|
|
|
if (!active) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-30 10:34:06 +00:00
|
|
|
GDVIRTUAL_CALL(_exit);
|
2024-08-21 18:04:35 +00:00
|
|
|
emit_signal(LW_NAME(exited));
|
2022-09-28 10:48:45 +00:00
|
|
|
active = false;
|
2024-01-18 10:32:32 +00:00
|
|
|
}
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
void LimboState::_update(double p_delta) {
|
2024-06-30 10:34:06 +00:00
|
|
|
GDVIRTUAL_CALL(_update, p_delta);
|
2024-08-21 18:04:35 +00:00
|
|
|
emit_signal(LW_NAME(updated), p_delta);
|
2024-01-18 10:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LimboState::_setup() {
|
2024-06-30 10:34:06 +00:00
|
|
|
GDVIRTUAL_CALL(_setup);
|
2024-08-21 18:04:35 +00:00
|
|
|
emit_signal(LW_NAME(setup));
|
2024-01-18 10:32:32 +00:00
|
|
|
}
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
void LimboState::_initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) {
|
2022-09-28 10:48:45 +00:00
|
|
|
ERR_FAIL_COND(p_agent == nullptr);
|
|
|
|
agent = p_agent;
|
2022-10-31 20:53:33 +00:00
|
|
|
|
2024-03-06 19:17:23 +00:00
|
|
|
if (_should_use_new_scope()) {
|
|
|
|
blackboard->set_parent(p_blackboard);
|
|
|
|
} else {
|
|
|
|
blackboard = p_blackboard;
|
|
|
|
}
|
|
|
|
if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) {
|
2024-08-15 20:15:53 +00:00
|
|
|
// Don't overwrite existing blackboard values as they may be initialized from code.
|
2024-09-15 12:29:23 +00:00
|
|
|
blackboard_plan->populate_blackboard(blackboard, false, this, _get_prefetch_root_for_base_plan());
|
2022-10-31 20:53:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
_setup();
|
2024-01-12 22:20:39 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
bool LimboState::_dispatch(const StringName &p_event, const Variant &p_cargo) {
|
|
|
|
ERR_FAIL_COND_V(p_event == StringName(), false);
|
2022-09-28 10:48:45 +00:00
|
|
|
if (handlers.size() > 0 && handlers.has(p_event)) {
|
2022-12-16 10:56:41 +00:00
|
|
|
Variant ret;
|
2024-01-07 04:54:17 +00:00
|
|
|
|
|
|
|
#ifdef LIMBOAI_MODULE
|
|
|
|
Callable::CallError ce;
|
2022-09-28 10:48:45 +00:00
|
|
|
if (p_cargo.get_type() == Variant::NIL) {
|
2022-12-16 10:56:41 +00:00
|
|
|
handlers[p_event].callp(nullptr, 0, ret, ce);
|
|
|
|
if (ce.error != Callable::CallError::CALL_OK) {
|
|
|
|
ERR_PRINT("Error calling event handler " + Variant::get_callable_error_text(handlers[p_event], nullptr, 0, ce));
|
|
|
|
}
|
2022-09-28 10:48:45 +00:00
|
|
|
} else {
|
2022-12-16 10:56:41 +00:00
|
|
|
const Variant *argptrs[1];
|
|
|
|
argptrs[0] = &p_cargo;
|
|
|
|
handlers[p_event].callp(argptrs, 1, ret, ce);
|
|
|
|
if (ce.error != Callable::CallError::CALL_OK) {
|
|
|
|
ERR_PRINT("Error calling event handler " + Variant::get_callable_error_text(handlers[p_event], argptrs, 1, ce));
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 04:54:17 +00:00
|
|
|
|
2024-01-13 16:10:42 +00:00
|
|
|
#elif LIMBOAI_GDEXTENSION
|
2024-01-07 04:54:17 +00:00
|
|
|
if (p_cargo.get_type() == Variant::NIL) {
|
|
|
|
ret = handlers[p_event].call();
|
|
|
|
} else {
|
|
|
|
Array args;
|
|
|
|
args.append(p_cargo);
|
|
|
|
ret = handlers[p_event].callv(args);
|
|
|
|
}
|
|
|
|
#endif // LIMBOAI_GDEXTENSION
|
2022-12-16 10:56:41 +00:00
|
|
|
|
|
|
|
if (unlikely(ret.get_type() != Variant::BOOL)) {
|
|
|
|
ERR_PRINT("Event handler returned unexpected type: " + Variant::get_type_name(ret.get_type()));
|
|
|
|
} else {
|
|
|
|
return ret;
|
2022-09-28 10:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-16 10:56:41 +00:00
|
|
|
return false;
|
2022-09-28 10:48:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
void LimboState::add_event_handler(const StringName &p_event, const Callable &p_handler) {
|
|
|
|
ERR_FAIL_COND(p_event == StringName());
|
2022-12-16 10:56:41 +00:00
|
|
|
ERR_FAIL_COND(!p_handler.is_valid());
|
|
|
|
handlers.insert(p_event, p_handler);
|
2022-09-28 10:48:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
bool LimboState::dispatch(const StringName &p_event, const Variant &p_cargo) {
|
2024-03-01 10:02:32 +00:00
|
|
|
return get_root()->_dispatch(p_event, p_cargo);
|
|
|
|
}
|
|
|
|
|
2023-03-30 21:37:21 +00:00
|
|
|
LimboState *LimboState::call_on_enter(const Callable &p_callable) {
|
2022-12-16 10:56:41 +00:00
|
|
|
ERR_FAIL_COND_V(!p_callable.is_valid(), this);
|
2024-08-21 18:04:35 +00:00
|
|
|
connect(LW_NAME(entered), p_callable);
|
2022-09-28 10:48:45 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-03-30 21:37:21 +00:00
|
|
|
LimboState *LimboState::call_on_exit(const Callable &p_callable) {
|
2022-12-16 10:56:41 +00:00
|
|
|
ERR_FAIL_COND_V(!p_callable.is_valid(), this);
|
2024-08-21 18:04:35 +00:00
|
|
|
connect(LW_NAME(exited), p_callable);
|
2022-09-28 10:48:45 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-03-30 21:37:21 +00:00
|
|
|
LimboState *LimboState::call_on_update(const Callable &p_callable) {
|
2022-12-16 10:56:41 +00:00
|
|
|
ERR_FAIL_COND_V(!p_callable.is_valid(), this);
|
2024-08-21 18:04:35 +00:00
|
|
|
connect(LW_NAME(updated), p_callable);
|
2022-09-28 10:48:45 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
void LimboState::set_guard(const Callable &p_guard_callable) {
|
|
|
|
ERR_FAIL_COND(!p_guard_callable.is_valid());
|
|
|
|
guard_callable = p_guard_callable;
|
2022-09-29 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
void LimboState::clear_guard() {
|
|
|
|
guard_callable = Callable();
|
2022-09-29 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
void LimboState::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2024-03-11 17:58:40 +00:00
|
|
|
case NOTIFICATION_READY: {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
_update_blackboard_plan();
|
|
|
|
}
|
|
|
|
} break;
|
2022-09-28 10:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboState::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_root"), &LimboState::get_root);
|
2022-10-19 21:39:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_agent"), &LimboState::get_agent);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_agent", "agent"), &LimboState::set_agent);
|
2022-09-28 10:48:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("event_finished"), &LimboState::event_finished);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_active"), &LimboState::is_active);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_initialize", "agent", "blackboard"), &LimboState::_initialize);
|
|
|
|
ClassDB::bind_method(D_METHOD("dispatch", "event", "cargo"), &LimboState::dispatch, Variant());
|
|
|
|
ClassDB::bind_method(D_METHOD("named", "name"), &LimboState::named);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_event_handler", "event", "handler"), &LimboState::add_event_handler);
|
|
|
|
ClassDB::bind_method(D_METHOD("call_on_enter", "callable"), &LimboState::call_on_enter);
|
|
|
|
ClassDB::bind_method(D_METHOD("call_on_exit", "callable"), &LimboState::call_on_exit);
|
|
|
|
ClassDB::bind_method(D_METHOD("call_on_update", "callable"), &LimboState::call_on_update);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_guard", "guard_callable"), &LimboState::set_guard);
|
2022-12-16 10:56:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_guard"), &LimboState::clear_guard);
|
2022-10-19 21:39:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_blackboard"), &LimboState::get_blackboard);
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_blackboard_plan", "plan"), &LimboState::set_blackboard_plan);
|
2024-01-23 19:02:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_blackboard_plan"), &LimboState::get_blackboard_plan);
|
2022-10-31 20:53:33 +00:00
|
|
|
|
2024-12-16 23:11:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_get_parent_scope_plan"), &LimboState::_get_parent_scope_plan);
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
GDVIRTUAL_BIND(_setup);
|
|
|
|
GDVIRTUAL_BIND(_enter);
|
|
|
|
GDVIRTUAL_BIND(_exit);
|
2024-03-04 20:36:16 +00:00
|
|
|
GDVIRTUAL_BIND(_update, "delta");
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "EVENT_FINISHED", PROPERTY_HINT_NONE, "", 0), "", "event_finished");
|
2022-12-16 10:56:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "agent", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_agent", "get_agent");
|
2022-10-26 21:12:29 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_RESOURCE_TYPE, "Blackboard", 0), "", "get_blackboard");
|
2024-03-31 23:11:51 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard_plan", PROPERTY_HINT_RESOURCE_TYPE, "BlackboardPlan", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE), "set_blackboard_plan", "get_blackboard_plan");
|
2022-10-12 12:02:39 +00:00
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("setup"));
|
|
|
|
ADD_SIGNAL(MethodInfo("entered"));
|
|
|
|
ADD_SIGNAL(MethodInfo("exited"));
|
2024-03-04 20:36:16 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("updated", PropertyInfo(Variant::FLOAT, "delta")));
|
2024-01-18 10:32:32 +00:00
|
|
|
}
|
2022-09-28 10:48:45 +00:00
|
|
|
|
|
|
|
LimboState::LimboState() {
|
|
|
|
agent = nullptr;
|
|
|
|
active = false;
|
2022-10-31 20:53:33 +00:00
|
|
|
blackboard = Ref<Blackboard>(memnew(Blackboard));
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
guard_callable = Callable();
|
2022-09-29 20:44:51 +00:00
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
set_process(false);
|
|
|
|
set_physics_process(false);
|
|
|
|
set_process_input(false);
|
|
|
|
}
|