Add BTCheckVar condition task

This commit is contained in:
Serhii Snitsaruk 2023-08-08 15:06:01 +02:00
parent 5204bf644e
commit 2795c6dcf2
5 changed files with 198 additions and 0 deletions

View File

@ -0,0 +1,135 @@
/**
* bt_check_var.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.
* =============================================================================
*/
#include "bt_check_var.h"
#include "modules/limboai/util/limbo_utility.h"
#include "core/variant/callable.h"
VARIANT_ENUM_CAST(BTCheckVar::CheckType);
void BTCheckVar::set_variable(String p_variable) {
variable = p_variable;
emit_changed();
}
void BTCheckVar::set_check_type(CheckType p_check_type) {
check_type = p_check_type;
emit_changed();
}
void BTCheckVar::set_value(Ref<BBVariant> p_value) {
value = p_value;
emit_changed();
if (Engine::get_singleton()->is_editor_hint() && value.is_valid()) {
value->connect(SNAME("changed"), Callable(this, SNAME("emit_changed")));
}
}
String BTCheckVar::get_configuration_warning() const {
String warning = BTCondition::get_configuration_warning();
if (!warning.is_empty()) {
warning += "\n";
}
if (variable.is_empty()) {
warning += "`variable` should be assigned.\n";
}
if (!value.is_valid()) {
warning += "`value` should be assigned.\n";
}
return warning;
}
String BTCheckVar::_generate_name() const {
if (variable.is_empty()) {
return "CheckVar ???";
}
String check_str = "?";
switch (check_type) {
case CheckType::CHECK_EQUAL: {
check_str = "==";
} break;
case CheckType::CHECK_LESS_THAN: {
check_str = "<";
} break;
case CheckType::CHECK_LESS_THAN_OR_EQUAL: {
check_str = "<=";
} break;
case CheckType::CHECK_GREATER_THAN: {
check_str = ">";
} break;
case CheckType::CHECK_GREATER_THAN_OR_EQUAL: {
check_str = ">=";
} break;
case CheckType::CHECK_NOT_EQUAL: {
check_str = "!=";
} break;
}
return vformat("Check if %s %s %s", LimboUtility::get_singleton()->decorate_var(variable), check_str,
value.is_valid() ? Variant(value) : Variant("???"));
}
int BTCheckVar::_tick(double p_delta) {
ERR_FAIL_COND_V_MSG(variable.is_empty(), FAILURE, "BBCheckVar: `variable` is not set.");
ERR_FAIL_COND_V_MSG(!value.is_valid(), FAILURE, "BBCheckVar: `value` is not set.");
ERR_FAIL_COND_V_MSG(!get_blackboard()->has_var(variable), FAILURE, vformat("BBCheckVar: Blackboard variable doesn't exist: \"%s\". Returning FAILURE.", variable));
Variant left_value = get_blackboard()->get_var(variable, Variant());
Variant right_value = value->get_value(get_agent(), get_blackboard());
switch (check_type) {
case CheckType::CHECK_EQUAL: {
return Variant::evaluate(Variant::OP_EQUAL, left_value, right_value) ? SUCCESS : FAILURE;
} break;
case CheckType::CHECK_LESS_THAN: {
return Variant::evaluate(Variant::OP_LESS, left_value, right_value) ? SUCCESS : FAILURE;
} break;
case CheckType::CHECK_LESS_THAN_OR_EQUAL: {
return Variant::evaluate(Variant::OP_LESS_EQUAL, left_value, right_value) ? SUCCESS : FAILURE;
} break;
case CheckType::CHECK_GREATER_THAN: {
return Variant::evaluate(Variant::OP_GREATER, left_value, right_value) ? SUCCESS : FAILURE;
} break;
case CheckType::CHECK_GREATER_THAN_OR_EQUAL: {
return Variant::evaluate(Variant::OP_GREATER_EQUAL, left_value, right_value) ? SUCCESS : FAILURE;
} break;
case CheckType::CHECK_NOT_EQUAL: {
return Variant::evaluate(Variant::OP_NOT_EQUAL, left_value, right_value) ? SUCCESS : FAILURE;
} break;
default: {
return FAILURE;
} break;
}
}
void BTCheckVar::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_variable", "p_variable"), &BTCheckVar::set_variable);
ClassDB::bind_method(D_METHOD("get_variable"), &BTCheckVar::get_variable);
ClassDB::bind_method(D_METHOD("set_check_type", "p_check_type"), &BTCheckVar::set_check_type);
ClassDB::bind_method(D_METHOD("get_check_type"), &BTCheckVar::get_check_type);
ClassDB::bind_method(D_METHOD("set_value", "p_value"), &BTCheckVar::set_value);
ClassDB::bind_method(D_METHOD("get_value"), &BTCheckVar::get_value);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "variable"), "set_variable", "get_variable");
ADD_PROPERTY(PropertyInfo(Variant::INT, "check_type", PROPERTY_HINT_ENUM, "Equal,Less Than,Less Than Or Equal,Greater Than,Greater Than Or Equal,Not Equal"), "set_check_type", "get_check_type");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "BBVariant"), "set_value", "get_value");
BIND_ENUM_CONSTANT(CHECK_EQUAL);
BIND_ENUM_CONSTANT(CHECK_LESS_THAN);
BIND_ENUM_CONSTANT(CHECK_LESS_THAN_OR_EQUAL);
BIND_ENUM_CONSTANT(CHECK_GREATER_THAN);
BIND_ENUM_CONSTANT(CHECK_GREATER_THAN_OR_EQUAL);
BIND_ENUM_CONSTANT(CHECK_NOT_EQUAL);
}

View File

@ -0,0 +1,59 @@
/**
* bt_check_var.h
* =============================================================================
* 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.
* =============================================================================
*/
#ifndef BT_CHECK_VAR_H
#define BT_CHECK_VAR_H
#include "bt_condition.h"
#include "modules/limboai/blackboard/bb_param/bb_variant.h"
#include "core/object/class_db.h"
#include "core/object/object.h"
class BTCheckVar : public BTCondition {
GDCLASS(BTCheckVar, BTCondition);
public:
enum CheckType : unsigned int {
CHECK_EQUAL,
CHECK_LESS_THAN,
CHECK_LESS_THAN_OR_EQUAL,
CHECK_GREATER_THAN,
CHECK_GREATER_THAN_OR_EQUAL,
CHECK_NOT_EQUAL
};
private:
String variable;
CheckType check_type = CheckType::CHECK_EQUAL;
Ref<BBVariant> value;
protected:
static void _bind_methods();
virtual String _generate_name() const override;
virtual int _tick(double p_delta) override;
public:
virtual String get_configuration_warning() const override;
void set_variable(String p_variable);
String get_variable() const { return variable; }
void set_check_type(CheckType p_check_type);
CheckType get_check_type() const { return check_type; }
void set_value(Ref<BBVariant> p_value);
Ref<BBVariant> get_value() const { return value; }
};
#endif // BT_CHECK_VAR_H

View File

@ -60,6 +60,7 @@ def get_doc_classes():
"BTAction", "BTAction",
"BTAlwaysFail", "BTAlwaysFail",
"BTAlwaysSucceed", "BTAlwaysSucceed",
"BTCheckVar",
"BTComposite", "BTComposite",
"BTCondition", "BTCondition",
"BTConsolePrint", "BTConsolePrint",

1
icons/BTCheckVar.svg Normal file
View File

@ -0,0 +1 @@
<svg enable-background="new 0 0 16 16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><g fill="#ffca5f"><path d="m14.1 15.5h-1.46l-.96-1.87-1.54 1.87h-1.52l2.36-2.83-1.46-2.78h1.51l.94 1.79 1.49-1.79h1.54l-2.37 2.79z"/><path d="m5.7 11.5c-.83 0-1.5.67-1.5 1.49 0 .83.67 1.51 1.5 1.51.85 0 1.52-.66 1.52-1.51.01-.82-.68-1.49-1.52-1.49z"/><path d="m11.09 4.83c0-2.51-2.16-4.33-5.13-4.33-2.69 0-4.62 1.48-4.92 3.77l-.04.32h2.58l.05-.24c.2-1.03 1.17-1.72 2.41-1.72 1.47 0 2.49.91 2.49 2.21 0 1.23-1.77 2.59-3.38 2.59h-2.02l1.54 3.08h2.05l.08-1.45.01-.2.19-.03c2.52-.33 4.09-1.86 4.09-4z"/></g></svg>

After

Width:  |  Height:  |  Size: 603 B

View File

@ -63,6 +63,7 @@
#include "bt/composites/bt_random_sequence.h" #include "bt/composites/bt_random_sequence.h"
#include "bt/composites/bt_selector.h" #include "bt/composites/bt_selector.h"
#include "bt/composites/bt_sequence.h" #include "bt/composites/bt_sequence.h"
#include "bt/conditions/bt_check_var.h"
#include "bt/conditions/bt_condition.h" #include "bt/conditions/bt_condition.h"
#include "bt/decorators/bt_always_fail.h" #include "bt/decorators/bt_always_fail.h"
#include "bt/decorators/bt_always_succeed.h" #include "bt/decorators/bt_always_succeed.h"
@ -144,6 +145,7 @@ void initialize_limboai_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(BTWaitTicks); GDREGISTER_CLASS(BTWaitTicks);
GDREGISTER_CLASS(BTCondition); GDREGISTER_CLASS(BTCondition);
GDREGISTER_CLASS(BTCheckVar);
GDREGISTER_ABSTRACT_CLASS(BBParam); GDREGISTER_ABSTRACT_CLASS(BBParam);
GDREGISTER_CLASS(BBInt); GDREGISTER_CLASS(BBInt);