diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index 16c74287816..a245aff35b9 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -171,7 +171,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_DIVIDE, Variant::VECTOR2, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::VECTOR2, Variant::INT); - register_op>(Variant::OP_DIVIDE, Variant::VECTOR2I, Variant::VECTOR2I); + register_op>(Variant::OP_DIVIDE, Variant::VECTOR2I, Variant::VECTOR2I); register_op>(Variant::OP_DIVIDE, Variant::VECTOR2I, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::VECTOR2I, Variant::INT); @@ -183,7 +183,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_DIVIDE, Variant::VECTOR3, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::VECTOR3, Variant::INT); - register_op>(Variant::OP_DIVIDE, Variant::VECTOR3I, Variant::VECTOR3I); + register_op>(Variant::OP_DIVIDE, Variant::VECTOR3I, Variant::VECTOR3I); register_op>(Variant::OP_DIVIDE, Variant::VECTOR3I, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::VECTOR3I, Variant::INT); @@ -195,10 +195,10 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_DIVIDE, Variant::COLOR, Variant::INT); register_op>(Variant::OP_MODULE, Variant::INT, Variant::INT); - register_op>(Variant::OP_MODULE, Variant::VECTOR2I, Variant::VECTOR2I); + register_op>(Variant::OP_MODULE, Variant::VECTOR2I, Variant::VECTOR2I); register_op>(Variant::OP_MODULE, Variant::VECTOR2I, Variant::INT); - register_op>(Variant::OP_MODULE, Variant::VECTOR3I, Variant::VECTOR3I); + register_op>(Variant::OP_MODULE, Variant::VECTOR3I, Variant::VECTOR3I); register_op>(Variant::OP_MODULE, Variant::VECTOR3I, Variant::INT); register_op(Variant::OP_MODULE, Variant::STRING, Variant::NIL); diff --git a/core/variant/variant_op.h b/core/variant/variant_op.h index cbdd60f4043..3c9f849a4f2 100644 --- a/core/variant/variant_op.h +++ b/core/variant/variant_op.h @@ -168,6 +168,54 @@ public: static Variant::Type get_return_type() { return GetTypeInfo::VARIANT_TYPE; } }; +template <> +class OperatorEvaluatorDivNZ { +public: + static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { + const Vector2i &a = *VariantGetInternalPtr::get_ptr(&p_left); + const Vector2i &b = *VariantGetInternalPtr::get_ptr(&p_right); + if (unlikely(b.x == 0 || b.y == 0)) { + r_valid = false; + *r_ret = "Division by zero error"; + return; + } + *r_ret = a / b; + r_valid = true; + } + static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger::change(r_ret); + *VariantGetInternalPtr::get_ptr(r_ret) = *VariantGetInternalPtr::get_ptr(left) / *VariantGetInternalPtr::get_ptr(right); + } + static void ptr_evaluate(const void *left, const void *right, void *r_ret) { + PtrToArg::encode(PtrToArg::convert(left) / PtrToArg::convert(right), r_ret); + } + static Variant::Type get_return_type() { return GetTypeInfo::VARIANT_TYPE; } +}; + +template <> +class OperatorEvaluatorDivNZ { +public: + static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { + const Vector3i &a = *VariantGetInternalPtr::get_ptr(&p_left); + const Vector3i &b = *VariantGetInternalPtr::get_ptr(&p_right); + if (unlikely(b.x == 0 || b.y == 0 || b.z == 0)) { + r_valid = false; + *r_ret = "Division by zero error"; + return; + } + *r_ret = a / b; + r_valid = true; + } + static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger::change(r_ret); + *VariantGetInternalPtr::get_ptr(r_ret) = *VariantGetInternalPtr::get_ptr(left) / *VariantGetInternalPtr::get_ptr(right); + } + static void ptr_evaluate(const void *left, const void *right, void *r_ret) { + PtrToArg::encode(PtrToArg::convert(left) / PtrToArg::convert(right), r_ret); + } + static Variant::Type get_return_type() { return GetTypeInfo::VARIANT_TYPE; } +}; + template class OperatorEvaluatorMod { public: @@ -209,6 +257,54 @@ public: static Variant::Type get_return_type() { return GetTypeInfo::VARIANT_TYPE; } }; +template <> +class OperatorEvaluatorModNZ { +public: + static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { + const Vector2i &a = *VariantGetInternalPtr::get_ptr(&p_left); + const Vector2i &b = *VariantGetInternalPtr::get_ptr(&p_right); + if (unlikely(b.x == 0 || b.y == 0)) { + r_valid = false; + *r_ret = "Module by zero error"; + return; + } + *r_ret = a % b; + r_valid = true; + } + static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger::change(r_ret); + *VariantGetInternalPtr::get_ptr(r_ret) = *VariantGetInternalPtr::get_ptr(left) % *VariantGetInternalPtr::get_ptr(right); + } + static void ptr_evaluate(const void *left, const void *right, void *r_ret) { + PtrToArg::encode(PtrToArg::convert(left) / PtrToArg::convert(right), r_ret); + } + static Variant::Type get_return_type() { return GetTypeInfo::VARIANT_TYPE; } +}; + +template <> +class OperatorEvaluatorModNZ { +public: + static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { + const Vector3i &a = *VariantGetInternalPtr::get_ptr(&p_left); + const Vector3i &b = *VariantGetInternalPtr::get_ptr(&p_right); + if (unlikely(b.x == 0 || b.y == 0 || b.z == 0)) { + r_valid = false; + *r_ret = "Module by zero error"; + return; + } + *r_ret = a % b; + r_valid = true; + } + static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger::change(r_ret); + *VariantGetInternalPtr::get_ptr(r_ret) = *VariantGetInternalPtr::get_ptr(left) % *VariantGetInternalPtr::get_ptr(right); + } + static void ptr_evaluate(const void *left, const void *right, void *r_ret) { + PtrToArg::encode(PtrToArg::convert(left) % PtrToArg::convert(right), r_ret); + } + static Variant::Type get_return_type() { return GetTypeInfo::VARIANT_TYPE; } +}; + template class OperatorEvaluatorNeg { public: