diff --git a/bt/tasks/blackboard/bt_set_var.cpp b/bt/tasks/blackboard/bt_set_var.cpp index 5b0af9b..51b84ae 100644 --- a/bt/tasks/blackboard/bt_set_var.cpp +++ b/bt/tasks/blackboard/bt_set_var.cpp @@ -15,6 +15,7 @@ #include "modules/limboai/util/limbo_utility.h" #include "core/variant/callable.h" +#include "core/variant/variant.h" String BTSetVar::_generate_name() const { if (variable.is_empty()) { @@ -39,6 +40,7 @@ BT::Status BTSetVar::_tick(double p_delta) { Variant left_value = get_blackboard()->get_var(variable, error_result); ERR_FAIL_COND_V_MSG(left_value == error_result, FAILURE, vformat("BTSetVar: Failed to get \"%s\" blackboard variable. Returning FAILURE.", variable)); result = LimboUtility::get_singleton()->perform_operation(operation, left_value, right_value); + ERR_FAIL_COND_V_MSG(result == Variant(), FAILURE, "BTSetVar: Operation not valid. Returning FAILURE."); } get_blackboard()->set_var(variable, result); return SUCCESS; diff --git a/tests/test_set_var.h b/tests/test_set_var.h index f05e9a6..11a1def 100644 --- a/tests/test_set_var.h +++ b/tests/test_set_var.h @@ -43,7 +43,7 @@ TEST_CASE("[Modules][LimboAI] BTSetVar") { sv->set_value(value); sv->set_variable("var"); - SUBCASE("When setting to a provided value") { + SUBCASE("When assigning a raw value") { value->set_value_source(BBParam::SAVED_VALUE); value->set_saved_value(123); CHECK(sv->execute(0.01666) == BTTask::SUCCESS); @@ -74,6 +74,91 @@ TEST_CASE("[Modules][LimboAI] BTSetVar") { CHECK(bb->get_var("var", 0) == Variant(123)); } } + SUBCASE("When performing an operation") { + bb->set_var("var", 8); + value->set_value_source(BBParam::SAVED_VALUE); + value->set_saved_value(3); + + SUBCASE("Addition") { + sv->set_operation(LimboUtility::OPERATION_ADDITION); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(11)); + } + SUBCASE("Subtraction") { + sv->set_operation(LimboUtility::OPERATION_SUBTRACTION); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(5)); + } + SUBCASE("Multiplication") { + sv->set_operation(LimboUtility::OPERATION_MULTIPLICATION); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(24)); + } + SUBCASE("Division") { + sv->set_operation(LimboUtility::OPERATION_DIVISION); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(2)); + } + SUBCASE("Modulo") { + sv->set_operation(LimboUtility::OPERATION_MODULO); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(2)); + } + SUBCASE("Power") { + sv->set_operation(LimboUtility::OPERATION_POWER); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(512)); + } + SUBCASE("Bitwise shift left") { + sv->set_operation(LimboUtility::OPERATION_BIT_SHIFT_LEFT); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(64)); + } + SUBCASE("Bitwise shift right") { + sv->set_operation(LimboUtility::OPERATION_BIT_SHIFT_RIGHT); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(1)); + } + SUBCASE("Bitwise AND") { + bb->set_var("var", 6); + sv->set_operation(LimboUtility::OPERATION_BIT_AND); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(2)); + } + SUBCASE("Bitwise OR") { + bb->set_var("var", 6); + sv->set_operation(LimboUtility::OPERATION_BIT_OR); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(7)); + } + SUBCASE("Bitwise XOR") { + bb->set_var("var", 6); + sv->set_operation(LimboUtility::OPERATION_BIT_XOR); + CHECK(sv->execute(0.01666) == BTTask::SUCCESS); + CHECK(bb->get_var("var", 0) == Variant(5)); + } + } + SUBCASE("Performing an operation when assigned variable doesn't exist.") { + value->set_value_source(BBParam::SAVED_VALUE); + value->set_saved_value(3); + sv->set_operation(LimboUtility::OPERATION_ADDITION); + + ERR_PRINT_OFF; + CHECK(sv->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + CHECK_FALSE(bb->has_var("var")); + } + SUBCASE("Performing an operation with incompatible operand types.") { + bb->set_var("var", 2); // int + value->set_value_source(BBParam::SAVED_VALUE); + value->set_saved_value("3"); // String + sv->set_operation(LimboUtility::OPERATION_ADDITION); + + ERR_PRINT_OFF; + CHECK(sv->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + CHECK(bb->get_var("var", 0) == Variant(2)); + } } } diff --git a/util/limbo_utility.cpp b/util/limbo_utility.cpp index ad4ff11..9490ea6 100644 --- a/util/limbo_utility.cpp +++ b/util/limbo_utility.cpp @@ -211,7 +211,7 @@ Variant LimboUtility::perform_operation(Operation p_operation, const Variant &le return Variant::evaluate(Variant::OP_MODULE, left_value, right_value); } break; case OPERATION_POWER: { - return Variant::evaluate(Variant::OP_MODULE, left_value, right_value); + return Variant::evaluate(Variant::OP_POWER, left_value, right_value); } break; case OPERATION_BIT_SHIFT_LEFT: { return Variant::evaluate(Variant::OP_SHIFT_LEFT, left_value, right_value);