|
| 1 | +/** |
| 2 | + * bt_evaluate_expression.cpp |
| 3 | + * ============================================================================= |
| 4 | + * Copyright 2021-2023 Serhii Snitsaruk |
| 5 | + * Copyright 2024 Wilson E. Alvarez |
| 6 | + * |
| 7 | + * Use of this source code is governed by an MIT-style |
| 8 | + * license that can be found in the LICENSE file or at |
| 9 | + * https://opensource.org/licenses/MIT. |
| 10 | + * ============================================================================= |
| 11 | + */ |
| 12 | + |
| 13 | +#include "bt_evaluate_expression.h" |
| 14 | + |
| 15 | +#include "../../../util/limbo_compat.h" |
| 16 | +#include "../../../util/limbo_utility.h" |
| 17 | + |
| 18 | +#ifdef LIMBOAI_GDEXTENSION |
| 19 | +#include "godot_cpp/classes/global_constants.hpp" |
| 20 | +#endif // LIMBOAI_GDEXTENSION |
| 21 | + |
| 22 | +//**** Setters / Getters |
| 23 | + |
| 24 | +void BTEvaluateExpression::set_expression_string(const String &p_expression_string) { |
| 25 | + expression_string = p_expression_string; |
| 26 | + emit_changed(); |
| 27 | +} |
| 28 | + |
| 29 | +void BTEvaluateExpression::set_node_param(Ref<BBNode> p_object) { |
| 30 | + node_param = p_object; |
| 31 | + emit_changed(); |
| 32 | + if (Engine::get_singleton()->is_editor_hint() && node_param.is_valid()) { |
| 33 | + node_param->connect(LW_NAME(changed), Callable(this, LW_NAME(emit_changed))); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void BTEvaluateExpression::set_input_include_delta(bool p_input_include_delta) { |
| 38 | + if (input_include_delta != p_input_include_delta) { |
| 39 | + processed_input_values.resize(input_values.size() + int(p_input_include_delta)); |
| 40 | + } |
| 41 | + input_include_delta = p_input_include_delta; |
| 42 | + emit_changed(); |
| 43 | +} |
| 44 | + |
| 45 | +void BTEvaluateExpression::set_input_names(const PackedStringArray &p_input_names) { |
| 46 | + input_names = p_input_names; |
| 47 | + emit_changed(); |
| 48 | +} |
| 49 | + |
| 50 | +void BTEvaluateExpression::set_input_values(const TypedArray<BBVariant> &p_input_values) { |
| 51 | + if (input_values.size() != p_input_values.size()) { |
| 52 | + processed_input_values.resize(p_input_values.size() + int(input_include_delta)); |
| 53 | + } |
| 54 | + input_values = p_input_values; |
| 55 | + emit_changed(); |
| 56 | +} |
| 57 | + |
| 58 | +void BTEvaluateExpression::set_result_var(const String &p_result_var) { |
| 59 | + result_var = p_result_var; |
| 60 | + emit_changed(); |
| 61 | +} |
| 62 | + |
| 63 | +//**** Task Implementation |
| 64 | + |
| 65 | +PackedStringArray BTEvaluateExpression::get_configuration_warnings() { |
| 66 | + PackedStringArray warnings = BTAction::get_configuration_warnings(); |
| 67 | + if (expression_string.is_empty()) { |
| 68 | + warnings.append("Expression string is not set."); |
| 69 | + } |
| 70 | + if (node_param.is_null()) { |
| 71 | + warnings.append("Node parameter is not set."); |
| 72 | + } else if (node_param->get_value_source() == BBParam::SAVED_VALUE && node_param->get_saved_value() == Variant()) { |
| 73 | + warnings.append("Path to node is not set."); |
| 74 | + } else if (node_param->get_value_source() == BBParam::BLACKBOARD_VAR && node_param->get_variable() == String()) { |
| 75 | + warnings.append("Node blackboard variable is not set."); |
| 76 | + } |
| 77 | + return warnings; |
| 78 | +} |
| 79 | + |
| 80 | +void BTEvaluateExpression::_setup() { |
| 81 | + parse(); |
| 82 | + ERR_FAIL_COND_MSG(is_parsed != Error::OK, "BTEvaluateExpression: Failed to parse expression: " + expression.get_error_text()); |
| 83 | +} |
| 84 | + |
| 85 | +Error BTEvaluateExpression::parse() { |
| 86 | + PackedStringArray processed_input_names; |
| 87 | + processed_input_names.resize(input_names.size() + int(input_include_delta)); |
| 88 | + String *processed_input_names_ptr = processed_input_names.ptrw(); |
| 89 | + if (input_include_delta) { |
| 90 | + processed_input_names_ptr[0] = "delta"; |
| 91 | + } |
| 92 | + for (int i = 0; i < input_names.size(); ++i) { |
| 93 | + processed_input_names_ptr[i + int(input_include_delta)] = input_names[i]; |
| 94 | + } |
| 95 | + |
| 96 | + is_parsed = expression.parse(expression_string, processed_input_names); |
| 97 | + return is_parsed; |
| 98 | +} |
| 99 | + |
| 100 | +String BTEvaluateExpression::_generate_name() { |
| 101 | + return vformat("EvaluateExpression %s node: %s %s", |
| 102 | + !expression_string.is_empty() ? expression_string : "???", |
| 103 | + node_param.is_valid() && !node_param->to_string().is_empty() ? node_param->to_string() : "???", |
| 104 | + result_var.is_empty() ? "" : LimboUtility::get_singleton()->decorate_output_var(result_var)); |
| 105 | +} |
| 106 | + |
| 107 | +BT::Status BTEvaluateExpression::_tick(double p_delta) { |
| 108 | + ERR_FAIL_COND_V_MSG(expression_string.is_empty(), FAILURE, "BTEvaluateExpression: Expression String is not set."); |
| 109 | + ERR_FAIL_COND_V_MSG(node_param.is_null(), FAILURE, "BTEvaluateExpression: Node parameter is not set."); |
| 110 | + Object *obj = node_param->get_value(get_agent(), get_blackboard()); |
| 111 | + ERR_FAIL_COND_V_MSG(obj == nullptr, FAILURE, "BTEvaluateExpression: Failed to get object: " + node_param->to_string()); |
| 112 | + ERR_FAIL_COND_V_MSG(is_parsed != Error::OK, FAILURE, "BTEvaluateExpression: Failed to parse expression: " + expression.get_error_text()); |
| 113 | + |
| 114 | + if (input_include_delta) { |
| 115 | + processed_input_values[0] = p_delta; |
| 116 | + } |
| 117 | + for (int i = 0; i < input_values.size(); ++i) { |
| 118 | + const Ref<BBVariant> &bb_variant = input_values[i]; |
| 119 | + processed_input_values[i + int(input_include_delta)] = bb_variant->get_value(get_agent(), get_blackboard()); |
| 120 | + } |
| 121 | + |
| 122 | + Variant result = expression.execute(processed_input_values, obj, false); |
| 123 | + ERR_FAIL_COND_V_MSG(expression.has_execute_failed(), FAILURE, "BTEvaluateExpression: Failed to execute: " + expression.get_error_text()); |
| 124 | + |
| 125 | + if (!result_var.is_empty()) { |
| 126 | + get_blackboard()->set_var(result_var, result); |
| 127 | + } |
| 128 | + |
| 129 | + return SUCCESS; |
| 130 | +} |
| 131 | + |
| 132 | +//**** Godot |
| 133 | + |
| 134 | +void BTEvaluateExpression::_bind_methods() { |
| 135 | + ClassDB::bind_method(D_METHOD("parse"), &BTEvaluateExpression::parse); |
| 136 | + ClassDB::bind_method(D_METHOD("set_expression_string", "p_method"), &BTEvaluateExpression::set_expression_string); |
| 137 | + ClassDB::bind_method(D_METHOD("get_expression_string"), &BTEvaluateExpression::get_expression_string); |
| 138 | + ClassDB::bind_method(D_METHOD("set_node_param", "p_param"), &BTEvaluateExpression::set_node_param); |
| 139 | + ClassDB::bind_method(D_METHOD("get_node_param"), &BTEvaluateExpression::get_node_param); |
| 140 | + ClassDB::bind_method(D_METHOD("set_input_names", "p_input_names"), &BTEvaluateExpression::set_input_names); |
| 141 | + ClassDB::bind_method(D_METHOD("get_input_names"), &BTEvaluateExpression::get_input_names); |
| 142 | + ClassDB::bind_method(D_METHOD("set_input_values", "p_input_values"), &BTEvaluateExpression::set_input_values); |
| 143 | + ClassDB::bind_method(D_METHOD("get_input_values"), &BTEvaluateExpression::get_input_values); |
| 144 | + ClassDB::bind_method(D_METHOD("set_input_include_delta", "p_input_include_delta"), &BTEvaluateExpression::set_input_include_delta); |
| 145 | + ClassDB::bind_method(D_METHOD("is_input_delta_included"), &BTEvaluateExpression::is_input_delta_included); |
| 146 | + ClassDB::bind_method(D_METHOD("set_result_var", "p_result_var"), &BTEvaluateExpression::set_result_var); |
| 147 | + ClassDB::bind_method(D_METHOD("get_result_var"), &BTEvaluateExpression::get_result_var); |
| 148 | + |
| 149 | + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_node_param", "get_node_param"); |
| 150 | + ADD_PROPERTY(PropertyInfo(Variant::STRING, "expression_string"), "set_expression_string", "get_expression_string"); |
| 151 | + ADD_PROPERTY(PropertyInfo(Variant::STRING, "result_var"), "set_result_var", "get_result_var"); |
| 152 | + ADD_GROUP("Inputs", "input_"); |
| 153 | + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_include_delta"), "set_input_include_delta", "is_input_delta_included"); |
| 154 | + ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "input_names", PROPERTY_HINT_ARRAY_TYPE, "String"), "set_input_names", "get_input_names"); |
| 155 | + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "input_values", PROPERTY_HINT_ARRAY_TYPE, RESOURCE_TYPE_HINT("BBVariant")), "set_input_values", "get_input_values"); |
| 156 | +} |
| 157 | + |
| 158 | +BTEvaluateExpression::BTEvaluateExpression() { |
| 159 | +} |
0 commit comments