Merge pull request #23310 from aaronfranke/posmod-int

Add integer posmod and rename default arg names
This commit is contained in:
Rémi Verschelde 2019-07-19 15:07:49 +02:00 committed by GitHub
commit f796779f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 128 additions and 111 deletions

View File

@ -52,6 +52,7 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"sqrt", "sqrt",
"fmod", "fmod",
"fposmod", "fposmod",
"posmod",
"floor", "floor",
"ceil", "ceil",
"round", "round",
@ -175,6 +176,7 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case MATH_ATAN2: case MATH_ATAN2:
case MATH_FMOD: case MATH_FMOD:
case MATH_FPOSMOD: case MATH_FPOSMOD:
case MATH_POSMOD:
case MATH_POW: case MATH_POW:
case MATH_EASE: case MATH_EASE:
case MATH_STEPIFY: case MATH_STEPIFY:
@ -283,6 +285,12 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(1);
*r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]); *r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]);
} break; } break;
case MATH_POSMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case MATH_FLOOR: { case MATH_FLOOR: {
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);

View File

@ -51,6 +51,7 @@ public:
MATH_SQRT, MATH_SQRT,
MATH_FMOD, MATH_FMOD,
MATH_FPOSMOD, MATH_FPOSMOD,
MATH_POSMOD,
MATH_FLOOR, MATH_FLOOR,
MATH_CEIL, MATH_CEIL,
MATH_ROUND, MATH_ROUND,

View File

@ -198,6 +198,13 @@ public:
value += 0.0; value += 0.0;
return value; return value;
} }
static _ALWAYS_INLINE_ int posmod(int p_x, int p_y) {
int value = p_x % p_y;
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
value += p_y;
}
return value;
}
static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * Math_PI / 180.0; } static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * Math_PI / 180.0; }
static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * Math_PI / 180.0; } static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * Math_PI / 180.0; }

View File

@ -345,45 +345,78 @@
<method name="fmod"> <method name="fmod">
<return type="float"> <return type="float">
</return> </return>
<argument index="0" name="x" type="float"> <argument index="0" name="a" type="float">
</argument> </argument>
<argument index="1" name="y" type="float"> <argument index="1" name="b" type="float">
</argument> </argument>
<description> <description>
Returns the floating-point remainder of [code]x/y[/code]. Returns the floating-point remainder of [code]a/b[/code], keeping the sign of [code]a[/code].
[codeblock] [codeblock]
# Remainder is 1.5 # Remainder is 1.5
var remainder = fmod(7, 5.5) var remainder = fmod(7, 5.5)
[/codeblock] [/codeblock]
For the integer remainder operation, use the % operator.
</description> </description>
</method> </method>
<method name="fposmod"> <method name="fposmod">
<return type="float"> <return type="float">
</return> </return>
<argument index="0" name="x" type="float"> <argument index="0" name="a" type="float">
</argument> </argument>
<argument index="1" name="y" type="float"> <argument index="1" name="b" type="float">
</argument> </argument>
<description> <description>
Returns the floating-point remainder of [code]x/y[/code] that wraps equally in positive and negative. Returns the floating-point modulus of [code]a/b[/code] that wraps equally in positive and negative.
[codeblock] [codeblock]
var i = -10 var i = -6
while i &lt; 0: while i &lt; 5:
prints(i, fposmod(i, 10)) prints(i, fposmod(i, 3))
i += 1 i += 1
[/codeblock] [/codeblock]
Produces: Produces:
[codeblock] [codeblock]
-10 10 -6 0
-9 1 -5 1
-8 2 -4 2
-7 3 -3 0
-6 4 -2 1
-5 5 -1 2
-4 6 0 0
-3 7 1 1
-2 8 2 2
-1 9 3 0
4 1
[/codeblock]
</description>
</method>
<method name="posmod">
<return type="int">
</return>
<argument index="0" name="a" type="int">
</argument>
<argument index="1" name="b" type="int">
</argument>
<description>
Returns the integer modulus of [code]a/b[/code] that wraps equally in positive and negative.
[codeblock]
var i = -6
while i &lt; 5:
prints(i, posmod(i, 3))
i += 1
[/codeblock]
Produces:
[codeblock]
-6 0
-5 1
-4 2
-3 0
-2 1
-1 2
0 0
1 1
2 2
3 0
4 1
[/codeblock] [/codeblock]
</description> </description>
</method> </method>

View File

@ -58,6 +58,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"sqrt", "sqrt",
"fmod", "fmod",
"fposmod", "fposmod",
"posmod",
"floor", "floor",
"ceil", "ceil",
"round", "round",
@ -243,6 +244,12 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(1);
r_ret = Math::fposmod((double)*p_args[0], (double)*p_args[1]); r_ret = Math::fposmod((double)*p_args[0], (double)*p_args[1]);
} break; } break;
case MATH_POSMOD: {
VALIDATE_ARG_COUNT(2);
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
r_ret = Math::posmod((int)*p_args[0], (int)*p_args[1]);
} break;
case MATH_FLOOR: { case MATH_FLOOR: {
VALIDATE_ARG_COUNT(1); VALIDATE_ARG_COUNT(1);
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
@ -1456,6 +1463,7 @@ bool GDScriptFunctions::is_deterministic(Function p_func) {
case MATH_SQRT: case MATH_SQRT:
case MATH_FMOD: case MATH_FMOD:
case MATH_FPOSMOD: case MATH_FPOSMOD:
case MATH_POSMOD:
case MATH_FLOOR: case MATH_FLOOR:
case MATH_CEIL: case MATH_CEIL:
case MATH_ROUND: case MATH_ROUND:
@ -1568,15 +1576,20 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
return mi; return mi;
} break; } break;
case MATH_FMOD: { case MATH_FMOD: {
MethodInfo mi("fmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y")); MethodInfo mi("fmod", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
mi.return_val.type = Variant::REAL; mi.return_val.type = Variant::REAL;
return mi; return mi;
} break; } break;
case MATH_FPOSMOD: { case MATH_FPOSMOD: {
MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y")); MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
mi.return_val.type = Variant::REAL; mi.return_val.type = Variant::REAL;
return mi; return mi;
} break; } break;
case MATH_POSMOD: {
MethodInfo mi("posmod", PropertyInfo(Variant::INT, "a"), PropertyInfo(Variant::INT, "b"));
mi.return_val.type = Variant::INT;
return mi;
} break;
case MATH_FLOOR: { case MATH_FLOOR: {
MethodInfo mi("floor", PropertyInfo(Variant::REAL, "s")); MethodInfo mi("floor", PropertyInfo(Variant::REAL, "s"));
mi.return_val.type = Variant::REAL; mi.return_val.type = Variant::REAL;
@ -1603,7 +1616,7 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
return mi; return mi;
} break; } break;
case MATH_POW: { case MATH_POW: {
MethodInfo mi("pow", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y")); MethodInfo mi("pow", PropertyInfo(Variant::REAL, "base"), PropertyInfo(Variant::REAL, "exp"));
mi.return_val.type = Variant::REAL; mi.return_val.type = Variant::REAL;
return mi; return mi;
} break; } break;

View File

@ -49,6 +49,7 @@ public:
MATH_SQRT, MATH_SQRT,
MATH_FMOD, MATH_FMOD,
MATH_FPOSMOD, MATH_FPOSMOD,
MATH_POSMOD,
MATH_FLOOR, MATH_FLOOR,
MATH_CEIL, MATH_CEIL,
MATH_ROUND, MATH_ROUND,

View File

@ -104,6 +104,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX
"bytes2var", "bytes2var",
"color_named", "color_named",
"smoothstep", "smoothstep",
"posmod",
}; };
VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::find_function(const String &p_string) { VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::find_function(const String &p_string) {
@ -192,6 +193,7 @@ int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
case MATH_ATAN2: case MATH_ATAN2:
case MATH_FMOD: case MATH_FMOD:
case MATH_FPOSMOD: case MATH_FPOSMOD:
case MATH_POSMOD:
case MATH_POW: case MATH_POW:
case MATH_EASE: case MATH_EASE:
case MATH_STEPIFY: case MATH_STEPIFY:
@ -261,7 +263,16 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
case MATH_ASIN: case MATH_ASIN:
case MATH_ACOS: case MATH_ACOS:
case MATH_ATAN: case MATH_ATAN:
case MATH_SQRT: { case MATH_SQRT:
case MATH_FLOOR:
case MATH_CEIL:
case MATH_ROUND:
case MATH_ABS:
case MATH_SIGN:
case MATH_LOG:
case MATH_EXP:
case MATH_ISNAN:
case MATH_ISINF: {
return PropertyInfo(Variant::REAL, "s"); return PropertyInfo(Variant::REAL, "s");
} break; } break;
case MATH_ATAN2: { case MATH_ATAN2: {
@ -271,32 +282,25 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
return PropertyInfo(Variant::REAL, "x"); return PropertyInfo(Variant::REAL, "x");
} break; } break;
case MATH_FMOD: case MATH_FMOD:
case MATH_FPOSMOD: { case MATH_FPOSMOD:
case LOGIC_MAX:
case LOGIC_MIN: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::REAL, "x"); return PropertyInfo(Variant::REAL, "a");
else else
return PropertyInfo(Variant::REAL, "y"); return PropertyInfo(Variant::REAL, "b");
} break; } break;
case MATH_FLOOR: case MATH_POSMOD: {
case MATH_CEIL: if (p_idx == 0)
case MATH_ROUND: return PropertyInfo(Variant::INT, "a");
case MATH_ABS: else
case MATH_SIGN: { return PropertyInfo(Variant::INT, "b");
return PropertyInfo(Variant::REAL, "s");
} break; } break;
case MATH_POW: { case MATH_POW: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::REAL, "x"); return PropertyInfo(Variant::REAL, "base");
else else
return PropertyInfo(Variant::REAL, "y"); return PropertyInfo(Variant::REAL, "exp");
} break;
case MATH_LOG:
case MATH_EXP:
case MATH_ISNAN:
case MATH_ISINF: {
return PropertyInfo(Variant::REAL, "s");
} break; } break;
case MATH_EASE: { case MATH_EASE: {
if (p_idx == 0) if (p_idx == 0)
@ -313,14 +317,7 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
else else
return PropertyInfo(Variant::REAL, "steps"); return PropertyInfo(Variant::REAL, "steps");
} break; } break;
case MATH_LERP: { case MATH_LERP:
if (p_idx == 0)
return PropertyInfo(Variant::REAL, "from");
else if (p_idx == 1)
return PropertyInfo(Variant::REAL, "to");
else
return PropertyInfo(Variant::REAL, "weight");
} break;
case MATH_INVERSE_LERP: { case MATH_INVERSE_LERP: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::REAL, "from"); return PropertyInfo(Variant::REAL, "from");
@ -365,12 +362,8 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
else else
return PropertyInfo(Variant::REAL, "step"); return PropertyInfo(Variant::REAL, "step");
} break; } break;
case MATH_RANDOMIZE: { case MATH_RANDOMIZE:
case MATH_RAND:
} break;
case MATH_RAND: {
} break;
case MATH_RANDF: { case MATH_RANDF: {
} break; } break;
@ -380,9 +373,7 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
else else
return PropertyInfo(Variant::REAL, "to"); return PropertyInfo(Variant::REAL, "to");
} break; } break;
case MATH_SEED: { case MATH_SEED:
return PropertyInfo(Variant::INT, "seed");
} break;
case MATH_RANDSEED: { case MATH_RANDSEED: {
return PropertyInfo(Variant::INT, "seed"); return PropertyInfo(Variant::INT, "seed");
} break; } break;
@ -418,26 +409,7 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
else else
return PropertyInfo(Variant::INT, "max"); return PropertyInfo(Variant::INT, "max");
} break; } break;
case MATH_WRAPF: { case MATH_WRAPF:
if (p_idx == 0)
return PropertyInfo(Variant::REAL, "value");
else if (p_idx == 1)
return PropertyInfo(Variant::REAL, "min");
else
return PropertyInfo(Variant::REAL, "max");
} break;
case LOGIC_MAX: {
if (p_idx == 0)
return PropertyInfo(Variant::REAL, "a");
else
return PropertyInfo(Variant::REAL, "b");
} break;
case LOGIC_MIN: {
if (p_idx == 0)
return PropertyInfo(Variant::REAL, "a");
else
return PropertyInfo(Variant::REAL, "b");
} break;
case LOGIC_CLAMP: { case LOGIC_CLAMP: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::REAL, "value"); return PropertyInfo(Variant::REAL, "value");
@ -450,20 +422,15 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
return PropertyInfo(Variant::INT, "value"); return PropertyInfo(Variant::INT, "value");
} break; } break;
case OBJ_WEAKREF: { case OBJ_WEAKREF: {
return PropertyInfo(Variant::OBJECT, "source"); return PropertyInfo(Variant::OBJECT, "source");
} break; } break;
case FUNC_FUNCREF: { case FUNC_FUNCREF: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::OBJECT, "instance"); return PropertyInfo(Variant::OBJECT, "instance");
else else
return PropertyInfo(Variant::STRING, "funcname"); return PropertyInfo(Variant::STRING, "funcname");
} break; } break;
case TYPE_CONVERT: { case TYPE_CONVERT: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::NIL, "what"); return PropertyInfo(Variant::NIL, "what");
else else
@ -471,45 +438,24 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
} break; } break;
case TYPE_OF: { case TYPE_OF: {
return PropertyInfo(Variant::NIL, "what"); return PropertyInfo(Variant::NIL, "what");
} break; } break;
case TYPE_EXISTS: { case TYPE_EXISTS: {
return PropertyInfo(Variant::STRING, "type"); return PropertyInfo(Variant::STRING, "type");
} break; } break;
case TEXT_CHAR: { case TEXT_CHAR: {
return PropertyInfo(Variant::INT, "ascii"); return PropertyInfo(Variant::INT, "ascii");
} break;
case TEXT_STR: {
return PropertyInfo(Variant::NIL, "value");
} break;
case TEXT_PRINT: {
return PropertyInfo(Variant::NIL, "value");
} break;
case TEXT_PRINTERR: {
return PropertyInfo(Variant::NIL, "value");
} break; } break;
case TEXT_STR:
case TEXT_PRINT:
case TEXT_PRINTERR:
case TEXT_PRINTRAW: { case TEXT_PRINTRAW: {
return PropertyInfo(Variant::NIL, "value"); return PropertyInfo(Variant::NIL, "value");
} break;
case VAR_TO_STR: {
return PropertyInfo(Variant::NIL, "var");
} break; } break;
case STR_TO_VAR: { case STR_TO_VAR: {
return PropertyInfo(Variant::STRING, "string"); return PropertyInfo(Variant::STRING, "string");
} break; } break;
case VAR_TO_STR:
case VAR_TO_BYTES: { case VAR_TO_BYTES: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::NIL, "var"); return PropertyInfo(Variant::NIL, "var");
@ -525,12 +471,10 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
return PropertyInfo(Variant::BOOL, "allow_objects"); return PropertyInfo(Variant::BOOL, "allow_objects");
} break; } break;
case COLORN: { case COLORN: {
if (p_idx == 0) if (p_idx == 0)
return PropertyInfo(Variant::STRING, "name"); return PropertyInfo(Variant::STRING, "name");
else else
return PropertyInfo(Variant::REAL, "alpha"); return PropertyInfo(Variant::REAL, "alpha");
} break; } break;
case FUNC_MAX: { case FUNC_MAX: {
} }
@ -561,6 +505,7 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons
case MATH_CEIL: { case MATH_CEIL: {
t = Variant::REAL; t = Variant::REAL;
} break; } break;
case MATH_POSMOD:
case MATH_ROUND: { case MATH_ROUND: {
t = Variant::INT; t = Variant::INT;
} break; } break;
@ -806,6 +751,12 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in
VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(1);
*r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]); *r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]);
} break; } break;
case VisualScriptBuiltinFunc::MATH_POSMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_FLOOR: { case VisualScriptBuiltinFunc::MATH_FLOOR: {
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
@ -1365,6 +1316,7 @@ void VisualScriptBuiltinFunc::_bind_methods() {
BIND_ENUM_CONSTANT(MATH_SQRT); BIND_ENUM_CONSTANT(MATH_SQRT);
BIND_ENUM_CONSTANT(MATH_FMOD); BIND_ENUM_CONSTANT(MATH_FMOD);
BIND_ENUM_CONSTANT(MATH_FPOSMOD); BIND_ENUM_CONSTANT(MATH_FPOSMOD);
BIND_ENUM_CONSTANT(MATH_POSMOD);
BIND_ENUM_CONSTANT(MATH_FLOOR); BIND_ENUM_CONSTANT(MATH_FLOOR);
BIND_ENUM_CONSTANT(MATH_CEIL); BIND_ENUM_CONSTANT(MATH_CEIL);
BIND_ENUM_CONSTANT(MATH_ROUND); BIND_ENUM_CONSTANT(MATH_ROUND);
@ -1454,6 +1406,7 @@ void register_visual_script_builtin_func_node() {
VisualScriptLanguage::singleton->add_register_func("functions/built_in/sqrt", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SQRT>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/sqrt", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SQRT>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/fmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FMOD>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/fmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FMOD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/fposmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FPOSMOD>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/fposmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FPOSMOD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/posmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POSMOD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/floor", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FLOOR>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/floor", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FLOOR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/ceil", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CEIL>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/ceil", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CEIL>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/round", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ROUND>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/round", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ROUND>);

View File

@ -104,6 +104,7 @@ public:
BYTES_TO_VAR, BYTES_TO_VAR,
COLORN, COLORN,
MATH_SMOOTHSTEP, MATH_SMOOTHSTEP,
MATH_POSMOD,
FUNC_MAX FUNC_MAX
}; };