1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Exposed randi_range to global funcs + renamed rand_range to randf_range

This commit is contained in:
Yuri Roubinsky
2020-07-26 13:52:24 +03:00
parent df2abc55be
commit 38fb26794b
16 changed files with 191 additions and 91 deletions

View File

@@ -76,7 +76,8 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"randomize", "randomize",
"randi", "randi",
"randf", "randf",
"rand_range", "randf_range",
"randi_range",
"seed", "seed",
"rand_seed", "rand_seed",
"deg2rad", "deg2rad",
@@ -127,7 +128,7 @@ String Expression::get_func_name(BuiltinFunc p_func) {
int Expression::get_func_argument_count(BuiltinFunc p_func) { int Expression::get_func_argument_count(BuiltinFunc p_func) {
switch (p_func) { switch (p_func) {
case MATH_RANDOMIZE: case MATH_RANDOMIZE:
case MATH_RAND: case MATH_RANDI:
case MATH_RANDF: case MATH_RANDF:
return 0; return 0;
case MATH_SIN: case MATH_SIN:
@@ -178,7 +179,8 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case MATH_POW: case MATH_POW:
case MATH_EASE: case MATH_EASE:
case MATH_STEPIFY: case MATH_STEPIFY:
case MATH_RANDOM: case MATH_RANDF_RANGE:
case MATH_RANDI_RANGE:
case MATH_POLAR2CARTESIAN: case MATH_POLAR2CARTESIAN:
case MATH_CARTESIAN2POLAR: case MATH_CARTESIAN2POLAR:
case LOGIC_MAX: case LOGIC_MAX:
@@ -397,17 +399,22 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
Math::randomize(); Math::randomize();
} break; } break;
case MATH_RAND: { case MATH_RANDI: {
*r_return = Math::rand(); *r_return = Math::rand();
} break; } break;
case MATH_RANDF: { case MATH_RANDF: {
*r_return = Math::randf(); *r_return = Math::randf();
} break; } break;
case MATH_RANDOM: { case MATH_RANDF_RANGE: {
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(1);
*r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]); *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]);
} break; } break;
case MATH_RANDI_RANGE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::random((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case MATH_SEED: { case MATH_SEED: {
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
uint64_t seed = *p_inputs[0]; uint64_t seed = *p_inputs[0];

View File

@@ -73,9 +73,10 @@ public:
MATH_MOVE_TOWARD, MATH_MOVE_TOWARD,
MATH_DECTIME, MATH_DECTIME,
MATH_RANDOMIZE, MATH_RANDOMIZE,
MATH_RAND, MATH_RANDI,
MATH_RANDF, MATH_RANDF,
MATH_RANDOM, MATH_RANDF_RANGE,
MATH_RANDI_RANGE,
MATH_SEED, MATH_SEED,
MATH_RANDSEED, MATH_RANDSEED,
MATH_DEG2RAD, MATH_DEG2RAD,

View File

@@ -181,3 +181,7 @@ double Math::random(double from, double to) {
float Math::random(float from, float to) { float Math::random(float from, float to) {
return default_rand.random(from, to); return default_rand.random(from, to);
} }
int Math::random(int from, int to) {
return default_rand.random(from, to);
}

View File

@@ -289,7 +289,7 @@ public:
static double random(double from, double to); static double random(double from, double to);
static float random(float from, float to); static float random(float from, float to);
static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); } static int random(int from, int to);
static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) {
// Check for exact equality first, required to handle "infinity" values. // Check for exact equality first, required to handle "infinity" values.

View File

@@ -43,7 +43,7 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
_FORCE_INLINE_ void set_seed(uint64_t seed) { randbase.seed(seed); } _FORCE_INLINE_ void set_seed(uint64_t p_seed) { randbase.seed(p_seed); }
_FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); } _FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); }
@@ -53,24 +53,11 @@ public:
_FORCE_INLINE_ real_t randf() { return randbase.randf(); } _FORCE_INLINE_ real_t randf() { return randbase.randf(); }
_FORCE_INLINE_ real_t randf_range(real_t from, real_t to) { return randbase.random(from, to); } _FORCE_INLINE_ real_t randf_range(real_t p_from, real_t p_to) { return randbase.random(p_from, p_to); }
_FORCE_INLINE_ real_t randfn(real_t mean = 0.0, real_t deviation = 1.0) { return randbase.randfn(mean, deviation); } _FORCE_INLINE_ real_t randfn(real_t p_mean = 0.0, real_t p_deviation = 1.0) { return randbase.randfn(p_mean, p_deviation); }
_FORCE_INLINE_ int randi_range(int from, int to) { _FORCE_INLINE_ int randi_range(int p_from, int p_to) { return randbase.random(p_from, p_to); }
int range;
int min;
if (to > from) {
range = to - from + 1;
min = from;
} else if (to < from) {
range = from - to + 1;
min = to;
} else { // from == to
return from;
}
return randbase.rand(range) + min;
}
RandomNumberGenerator() {} RandomNumberGenerator() {}
}; };

View File

@@ -49,3 +49,18 @@ double RandomPCG::random(double p_from, double p_to) {
float RandomPCG::random(float p_from, float p_to) { float RandomPCG::random(float p_from, float p_to) {
return randf() * (p_to - p_from) + p_from; return randf() * (p_to - p_from) + p_from;
} }
int RandomPCG::random(int p_from, int p_to) {
int range;
int min;
if (p_to > p_from) {
range = p_to - p_from + 1;
min = p_from;
} else if (p_to < p_from) {
range = p_from - p_to + 1;
min = p_to;
} else { // from == to
return p_from;
}
return rand(range) + min;
}

View File

@@ -133,7 +133,7 @@ public:
double random(double p_from, double p_to); double random(double p_from, double p_to);
float random(float p_from, float p_to); float random(float p_from, float p_to);
real_t random(int p_from, int p_to) { return (real_t)random((real_t)p_from, (real_t)p_to); } int random(int p_from, int p_to);
}; };
#endif // RANDOM_PCG_H #endif // RANDOM_PCG_H

View File

@@ -921,7 +921,7 @@
[/codeblock] [/codeblock]
</description> </description>
</method> </method>
<method name="rand_range"> <method name="randf_range">
<return type="float"> <return type="float">
</return> </return>
<argument index="0" name="from" type="float"> <argument index="0" name="from" type="float">
@@ -931,7 +931,22 @@
<description> <description>
Random range, any floating point value between [code]from[/code] and [code]to[/code]. Random range, any floating point value between [code]from[/code] and [code]to[/code].
[codeblock] [codeblock]
prints(rand_range(0, 1), rand_range(0, 1)) # Prints e.g. 0.135591 0.405263 prints(randf_range(-10, 10), randf_range(-10, 10)) # Prints e.g. -3.844535 7.45315
[/codeblock]
</description>
</method>
<method name="randi_range">
<return type="int">
</return>
<argument index="0" name="from" type="int">
</argument>
<argument index="1" name="to" type="int">
</argument>
<description>
Random range, any 32-bit integer value between [code]from[/code] and [code]to[/code] (inclusive). If [code]to[/code] is lesser than [code]from[/code] they are swapped.
[codeblock]
print(randi_range(0, 1)) # Prints 0 or 1
print(randi_range(-10, 1000)) # Prints any number from -10 to 1000
[/codeblock] [/codeblock]
</description> </description>
</method> </method>

View File

@@ -83,7 +83,8 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"randomize", "randomize",
"randi", "randi",
"randf", "randf",
"rand_range", "randf_range",
"randi_range",
"seed", "seed",
"rand_seed", "rand_seed",
"deg2rad", "deg2rad",
@@ -419,7 +420,7 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
Math::randomize(); Math::randomize();
r_ret = Variant(); r_ret = Variant();
} break; } break;
case MATH_RAND: { case MATH_RANDI: {
VALIDATE_ARG_COUNT(0); VALIDATE_ARG_COUNT(0);
r_ret = Math::rand(); r_ret = Math::rand();
} break; } break;
@@ -427,12 +428,18 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
VALIDATE_ARG_COUNT(0); VALIDATE_ARG_COUNT(0);
r_ret = Math::randf(); r_ret = Math::randf();
} break; } break;
case MATH_RANDOM: { case MATH_RANDF_RANGE: {
VALIDATE_ARG_COUNT(2); VALIDATE_ARG_COUNT(2);
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(1);
r_ret = Math::random((double)*p_args[0], (double)*p_args[1]); r_ret = Math::random((double)*p_args[0], (double)*p_args[1]);
} break; } break;
case MATH_RANDI_RANGE: {
VALIDATE_ARG_COUNT(2);
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
r_ret = Math::random((int)*p_args[0], (int)*p_args[1]);
} break;
case MATH_SEED: { case MATH_SEED: {
VALIDATE_ARG_COUNT(1); VALIDATE_ARG_COUNT(1);
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
@@ -1655,7 +1662,7 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.return_val.type = Variant::NIL; mi.return_val.type = Variant::NIL;
return mi; return mi;
} break; } break;
case MATH_RAND: { case MATH_RANDI: {
MethodInfo mi("randi"); MethodInfo mi("randi");
mi.return_val.type = Variant::INT; mi.return_val.type = Variant::INT;
return mi; return mi;
@@ -1665,11 +1672,16 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.return_val.type = Variant::FLOAT; mi.return_val.type = Variant::FLOAT;
return mi; return mi;
} break; } break;
case MATH_RANDOM: { case MATH_RANDF_RANGE: {
MethodInfo mi("rand_range", PropertyInfo(Variant::FLOAT, "from"), PropertyInfo(Variant::FLOAT, "to")); MethodInfo mi("randf_range", PropertyInfo(Variant::FLOAT, "from"), PropertyInfo(Variant::FLOAT, "to"));
mi.return_val.type = Variant::FLOAT; mi.return_val.type = Variant::FLOAT;
return mi; return mi;
} break; } break;
case MATH_RANDI_RANGE: {
MethodInfo mi("randi_range", PropertyInfo(Variant::INT, "from"), PropertyInfo(Variant::INT, "to"));
mi.return_val.type = Variant::INT;
return mi;
} break;
case MATH_SEED: { case MATH_SEED: {
MethodInfo mi("seed", PropertyInfo(Variant::INT, "seed")); MethodInfo mi("seed", PropertyInfo(Variant::INT, "seed"));
mi.return_val.type = Variant::NIL; mi.return_val.type = Variant::NIL;

View File

@@ -73,9 +73,10 @@ public:
MATH_MOVE_TOWARD, MATH_MOVE_TOWARD,
MATH_DECTIME, MATH_DECTIME,
MATH_RANDOMIZE, MATH_RANDOMIZE,
MATH_RAND, MATH_RANDI,
MATH_RANDF, MATH_RANDF,
MATH_RANDOM, MATH_RANDF_RANGE,
MATH_RANDI_RANGE,
MATH_SEED, MATH_SEED,
MATH_RANDSEED, MATH_RANDSEED,
MATH_DEG2RAD, MATH_DEG2RAD,

View File

@@ -129,7 +129,12 @@ namespace Godot
public static double RandRange(double from, double to) public static double RandRange(double from, double to)
{ {
return godot_icall_GD_rand_range(from, to); return godot_icall_GD_randf_range(from, to);
}
public static int RandRange(int from, int to)
{
return godot_icall_GD_randi_range(from, to);
} }
public static uint RandSeed(ulong seed, out ulong newSeed) public static uint RandSeed(ulong seed, out ulong newSeed)
@@ -238,9 +243,11 @@ namespace Godot
[MethodImpl(MethodImplOptions.InternalCall)] [MethodImpl(MethodImplOptions.InternalCall)]
internal extern static void godot_icall_GD_randomize(); internal extern static void godot_icall_GD_randomize();
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static double godot_icall_GD_randf_range(double from, double to);
[MethodImpl(MethodImplOptions.InternalCall)] [MethodImpl(MethodImplOptions.InternalCall)]
internal extern static double godot_icall_GD_rand_range(double from, double to); internal extern static int godot_icall_GD_randi_range(int from, int to);
[MethodImpl(MethodImplOptions.InternalCall)] [MethodImpl(MethodImplOptions.InternalCall)]
internal extern static uint godot_icall_GD_rand_seed(ulong seed, out ulong newSeed); internal extern static uint godot_icall_GD_rand_seed(ulong seed, out ulong newSeed);

View File

@@ -193,7 +193,11 @@ void godot_icall_GD_randomize() {
Math::randomize(); Math::randomize();
} }
double godot_icall_GD_rand_range(double from, double to) { double godot_icall_GD_randf_range(double from, double to) {
return Math::random(from, to);
}
int32_t godot_icall_GD_randi_range(int32_t from, int32_t to) {
return Math::random(from, to); return Math::random(from, to);
} }
@@ -298,7 +302,8 @@ void godot_register_gd_icalls() {
mono_add_internal_call("Godot.GD::godot_icall_GD_randf", (void *)godot_icall_GD_randf); mono_add_internal_call("Godot.GD::godot_icall_GD_randf", (void *)godot_icall_GD_randf);
mono_add_internal_call("Godot.GD::godot_icall_GD_randi", (void *)godot_icall_GD_randi); mono_add_internal_call("Godot.GD::godot_icall_GD_randi", (void *)godot_icall_GD_randi);
mono_add_internal_call("Godot.GD::godot_icall_GD_randomize", (void *)godot_icall_GD_randomize); mono_add_internal_call("Godot.GD::godot_icall_GD_randomize", (void *)godot_icall_GD_randomize);
mono_add_internal_call("Godot.GD::godot_icall_GD_rand_range", (void *)godot_icall_GD_rand_range); mono_add_internal_call("Godot.GD::godot_icall_GD_randf_range", (void *)godot_icall_GD_randf_range);
mono_add_internal_call("Godot.GD::godot_icall_GD_randi_range", (void *)godot_icall_GD_randi_range);
mono_add_internal_call("Godot.GD::godot_icall_GD_rand_seed", (void *)godot_icall_GD_rand_seed); mono_add_internal_call("Godot.GD::godot_icall_GD_rand_seed", (void *)godot_icall_GD_rand_seed);
mono_add_internal_call("Godot.GD::godot_icall_GD_seed", (void *)godot_icall_GD_seed); mono_add_internal_call("Godot.GD::godot_icall_GD_seed", (void *)godot_icall_GD_seed);
mono_add_internal_call("Godot.GD::godot_icall_GD_str", (void *)godot_icall_GD_str); mono_add_internal_call("Godot.GD::godot_icall_GD_str", (void *)godot_icall_GD_str);

View File

@@ -111,115 +111,118 @@
<constant name="MATH_RANDOMIZE" value="31" enum="BuiltinFunc"> <constant name="MATH_RANDOMIZE" value="31" enum="BuiltinFunc">
Randomize the seed (or the internal state) of the random number generator. Current implementation reseeds using a number based on time. Randomize the seed (or the internal state) of the random number generator. Current implementation reseeds using a number based on time.
</constant> </constant>
<constant name="MATH_RAND" value="32" enum="BuiltinFunc"> <constant name="MATH_RANDI" value="32" enum="BuiltinFunc">
Return a random 32 bits integer value. To obtain a random value between 0 to N (where N is smaller than 2^32 - 1), you can use it with the remainder function. Return a random 32 bits integer value. To obtain a random value between 0 to N (where N is smaller than 2^32 - 1), you can use it with the remainder function.
</constant> </constant>
<constant name="MATH_RANDF" value="33" enum="BuiltinFunc"> <constant name="MATH_RANDF" value="33" enum="BuiltinFunc">
Return a random floating-point value between 0 and 1. To obtain a random value between 0 to N, you can use it with multiplication. Return a random floating-point value between 0 and 1. To obtain a random value between 0 to N, you can use it with multiplication.
</constant> </constant>
<constant name="MATH_RANDOM" value="34" enum="BuiltinFunc"> <constant name="MATH_RANDF_RANGE" value="34" enum="BuiltinFunc">
Return a random floating-point value between the two inputs. Return a random floating-point value between the two inputs.
</constant> </constant>
<constant name="MATH_SEED" value="35" enum="BuiltinFunc"> <constant name="MATH_RANDI_RANGE" value="35" enum="BuiltinFunc">
Return a random 32-bit integer value between the two inputs.
</constant>
<constant name="MATH_SEED" value="36" enum="BuiltinFunc">
Set the seed for the random number generator. Set the seed for the random number generator.
</constant> </constant>
<constant name="MATH_RANDSEED" value="36" enum="BuiltinFunc"> <constant name="MATH_RANDSEED" value="37" enum="BuiltinFunc">
Return a random value from the given seed, along with the new seed. Return a random value from the given seed, along with the new seed.
</constant> </constant>
<constant name="MATH_DEG2RAD" value="37" enum="BuiltinFunc"> <constant name="MATH_DEG2RAD" value="38" enum="BuiltinFunc">
Convert the input from degrees to radians. Convert the input from degrees to radians.
</constant> </constant>
<constant name="MATH_RAD2DEG" value="38" enum="BuiltinFunc"> <constant name="MATH_RAD2DEG" value="39" enum="BuiltinFunc">
Convert the input from radians to degrees. Convert the input from radians to degrees.
</constant> </constant>
<constant name="MATH_LINEAR2DB" value="39" enum="BuiltinFunc"> <constant name="MATH_LINEAR2DB" value="40" enum="BuiltinFunc">
Convert the input from linear volume to decibel volume. Convert the input from linear volume to decibel volume.
</constant> </constant>
<constant name="MATH_DB2LINEAR" value="40" enum="BuiltinFunc"> <constant name="MATH_DB2LINEAR" value="41" enum="BuiltinFunc">
Convert the input from decibel volume to linear volume. Convert the input from decibel volume to linear volume.
</constant> </constant>
<constant name="MATH_POLAR2CARTESIAN" value="41" enum="BuiltinFunc"> <constant name="MATH_POLAR2CARTESIAN" value="42" enum="BuiltinFunc">
Converts a 2D point expressed in the polar coordinate system (a distance from the origin [code]r[/code] and an angle [code]th[/code]) to the cartesian coordinate system (X and Y axis). Converts a 2D point expressed in the polar coordinate system (a distance from the origin [code]r[/code] and an angle [code]th[/code]) to the cartesian coordinate system (X and Y axis).
</constant> </constant>
<constant name="MATH_CARTESIAN2POLAR" value="42" enum="BuiltinFunc"> <constant name="MATH_CARTESIAN2POLAR" value="43" enum="BuiltinFunc">
Converts a 2D point expressed in the cartesian coordinate system (X and Y axis) to the polar coordinate system (a distance from the origin and an angle). Converts a 2D point expressed in the cartesian coordinate system (X and Y axis) to the polar coordinate system (a distance from the origin and an angle).
</constant> </constant>
<constant name="MATH_WRAP" value="43" enum="BuiltinFunc"> <constant name="MATH_WRAP" value="44" enum="BuiltinFunc">
</constant> </constant>
<constant name="MATH_WRAPF" value="44" enum="BuiltinFunc"> <constant name="MATH_WRAPF" value="45" enum="BuiltinFunc">
</constant> </constant>
<constant name="LOGIC_MAX" value="45" enum="BuiltinFunc"> <constant name="LOGIC_MAX" value="46" enum="BuiltinFunc">
Return the greater of the two numbers, also known as their maximum. Return the greater of the two numbers, also known as their maximum.
</constant> </constant>
<constant name="LOGIC_MIN" value="46" enum="BuiltinFunc"> <constant name="LOGIC_MIN" value="47" enum="BuiltinFunc">
Return the lesser of the two numbers, also known as their minimum. Return the lesser of the two numbers, also known as their minimum.
</constant> </constant>
<constant name="LOGIC_CLAMP" value="47" enum="BuiltinFunc"> <constant name="LOGIC_CLAMP" value="48" enum="BuiltinFunc">
Return the input clamped inside the given range, ensuring the result is never outside it. Equivalent to [code]min(max(input, range_low), range_high)[/code]. Return the input clamped inside the given range, ensuring the result is never outside it. Equivalent to [code]min(max(input, range_low), range_high)[/code].
</constant> </constant>
<constant name="LOGIC_NEAREST_PO2" value="48" enum="BuiltinFunc"> <constant name="LOGIC_NEAREST_PO2" value="49" enum="BuiltinFunc">
Return the nearest power of 2 to the input. Return the nearest power of 2 to the input.
</constant> </constant>
<constant name="OBJ_WEAKREF" value="49" enum="BuiltinFunc"> <constant name="OBJ_WEAKREF" value="50" enum="BuiltinFunc">
Create a [WeakRef] from the input. Create a [WeakRef] from the input.
</constant> </constant>
<constant name="FUNC_FUNCREF" value="50" enum="BuiltinFunc"> <constant name="FUNC_FUNCREF" value="51" enum="BuiltinFunc">
Create a [FuncRef] from the input. Create a [FuncRef] from the input.
</constant> </constant>
<constant name="TYPE_CONVERT" value="51" enum="BuiltinFunc"> <constant name="TYPE_CONVERT" value="52" enum="BuiltinFunc">
Convert between types. Convert between types.
</constant> </constant>
<constant name="TYPE_OF" value="52" enum="BuiltinFunc"> <constant name="TYPE_OF" value="53" enum="BuiltinFunc">
Return the type of the input as an integer. Check [enum Variant.Type] for the integers that might be returned. Return the type of the input as an integer. Check [enum Variant.Type] for the integers that might be returned.
</constant> </constant>
<constant name="TYPE_EXISTS" value="53" enum="BuiltinFunc"> <constant name="TYPE_EXISTS" value="54" enum="BuiltinFunc">
Checks if a type is registered in the [ClassDB]. Checks if a type is registered in the [ClassDB].
</constant> </constant>
<constant name="TEXT_CHAR" value="54" enum="BuiltinFunc"> <constant name="TEXT_CHAR" value="55" enum="BuiltinFunc">
Return a character with the given ascii value. Return a character with the given ascii value.
</constant> </constant>
<constant name="TEXT_STR" value="55" enum="BuiltinFunc"> <constant name="TEXT_STR" value="56" enum="BuiltinFunc">
Convert the input to a string. Convert the input to a string.
</constant> </constant>
<constant name="TEXT_PRINT" value="56" enum="BuiltinFunc"> <constant name="TEXT_PRINT" value="57" enum="BuiltinFunc">
Print the given string to the output window. Print the given string to the output window.
</constant> </constant>
<constant name="TEXT_PRINTERR" value="57" enum="BuiltinFunc"> <constant name="TEXT_PRINTERR" value="58" enum="BuiltinFunc">
Print the given string to the standard error output. Print the given string to the standard error output.
</constant> </constant>
<constant name="TEXT_PRINTRAW" value="58" enum="BuiltinFunc"> <constant name="TEXT_PRINTRAW" value="59" enum="BuiltinFunc">
Print the given string to the standard output, without adding a newline. Print the given string to the standard output, without adding a newline.
</constant> </constant>
<constant name="VAR_TO_STR" value="59" enum="BuiltinFunc"> <constant name="VAR_TO_STR" value="60" enum="BuiltinFunc">
Serialize a [Variant] to a string. Serialize a [Variant] to a string.
</constant> </constant>
<constant name="STR_TO_VAR" value="60" enum="BuiltinFunc"> <constant name="STR_TO_VAR" value="61" enum="BuiltinFunc">
Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR]. Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR].
</constant> </constant>
<constant name="VAR_TO_BYTES" value="61" enum="BuiltinFunc"> <constant name="VAR_TO_BYTES" value="62" enum="BuiltinFunc">
Serialize a [Variant] to a [PackedByteArray]. Serialize a [Variant] to a [PackedByteArray].
</constant> </constant>
<constant name="BYTES_TO_VAR" value="62" enum="BuiltinFunc"> <constant name="BYTES_TO_VAR" value="63" enum="BuiltinFunc">
Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES]. Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES].
</constant> </constant>
<constant name="COLORN" value="63" enum="BuiltinFunc"> <constant name="COLORN" value="64" enum="BuiltinFunc">
Return the [Color] with the given name and alpha ranging from 0 to 1. Return the [Color] with the given name and alpha ranging from 0 to 1.
[b]Note:[/b] Names are defined in [code]color_names.inc[/code]. [b]Note:[/b] Names are defined in [code]color_names.inc[/code].
</constant> </constant>
<constant name="MATH_SMOOTHSTEP" value="64" enum="BuiltinFunc"> <constant name="MATH_SMOOTHSTEP" value="65" enum="BuiltinFunc">
Return a number smoothly interpolated between the first two inputs, based on the third input. Similar to [constant MATH_LERP], but interpolates faster at the beginning and slower at the end. Using Hermite interpolation formula: Return a number smoothly interpolated between the first two inputs, based on the third input. Similar to [constant MATH_LERP], but interpolates faster at the beginning and slower at the end. Using Hermite interpolation formula:
[codeblock] [codeblock]
var t = clamp((weight - from) / (to - from), 0.0, 1.0) var t = clamp((weight - from) / (to - from), 0.0, 1.0)
return t * t * (3.0 - 2.0 * t) return t * t * (3.0 - 2.0 * t)
[/codeblock] [/codeblock]
</constant> </constant>
<constant name="MATH_POSMOD" value="65" enum="BuiltinFunc"> <constant name="MATH_POSMOD" value="66" enum="BuiltinFunc">
</constant> </constant>
<constant name="MATH_LERP_ANGLE" value="66" enum="BuiltinFunc"> <constant name="MATH_LERP_ANGLE" value="67" enum="BuiltinFunc">
</constant> </constant>
<constant name="TEXT_ORD" value="67" enum="BuiltinFunc"> <constant name="TEXT_ORD" value="68" enum="BuiltinFunc">
</constant> </constant>
<constant name="FUNC_MAX" value="68" enum="BuiltinFunc"> <constant name="FUNC_MAX" value="69" enum="BuiltinFunc">
Represents the size of the [enum BuiltinFunc] enum. Represents the size of the [enum BuiltinFunc] enum.
</constant> </constant>
</constants> </constants>

View File

@@ -73,7 +73,8 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX
"randomize", "randomize",
"randi", "randi",
"randf", "randf",
"rand_range", "randf_range",
"randi_range",
"seed", "seed",
"rand_seed", "rand_seed",
"deg2rad", "deg2rad",
@@ -143,7 +144,7 @@ bool VisualScriptBuiltinFunc::has_input_sequence_port() const {
int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) { int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
switch (p_func) { switch (p_func) {
case MATH_RANDOMIZE: case MATH_RANDOMIZE:
case MATH_RAND: case MATH_RANDI:
case MATH_RANDF: case MATH_RANDF:
return 0; return 0;
case MATH_SIN: case MATH_SIN:
@@ -194,7 +195,8 @@ int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
case MATH_POW: case MATH_POW:
case MATH_EASE: case MATH_EASE:
case MATH_STEPIFY: case MATH_STEPIFY:
case MATH_RANDOM: case MATH_RANDF_RANGE:
case MATH_RANDI_RANGE:
case MATH_POLAR2CARTESIAN: case MATH_POLAR2CARTESIAN:
case MATH_CARTESIAN2POLAR: case MATH_CARTESIAN2POLAR:
case LOGIC_MAX: case LOGIC_MAX:
@@ -361,16 +363,23 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
} }
} break; } break;
case MATH_RANDOMIZE: case MATH_RANDOMIZE:
case MATH_RAND: case MATH_RANDI:
case MATH_RANDF: { case MATH_RANDF: {
} break; } break;
case MATH_RANDOM: { case MATH_RANDF_RANGE: {
if (p_idx == 0) { if (p_idx == 0) {
return PropertyInfo(Variant::FLOAT, "from"); return PropertyInfo(Variant::FLOAT, "from");
} else { } else {
return PropertyInfo(Variant::FLOAT, "to"); return PropertyInfo(Variant::FLOAT, "to");
} }
} break; } break;
case MATH_RANDI_RANGE: {
if (p_idx == 0) {
return PropertyInfo(Variant::INT, "from");
} else {
return PropertyInfo(Variant::INT, "to");
}
} break;
case MATH_SEED: case MATH_SEED:
case MATH_RANDSEED: { case MATH_RANDSEED: {
return PropertyInfo(Variant::INT, "seed"); return PropertyInfo(Variant::INT, "seed");
@@ -551,13 +560,16 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons
} break; } break;
case MATH_RANDOMIZE: { case MATH_RANDOMIZE: {
} break; } break;
case MATH_RAND: { case MATH_RANDI: {
t = Variant::INT; t = Variant::INT;
} break; } break;
case MATH_RANDF: case MATH_RANDF:
case MATH_RANDOM: { case MATH_RANDF_RANGE: {
t = Variant::FLOAT; t = Variant::FLOAT;
} break; } break;
case MATH_RANDI_RANGE: {
t = Variant::INT;
} break;
case MATH_SEED: { case MATH_SEED: {
} break; } break;
case MATH_RANDSEED: { case MATH_RANDSEED: {
@@ -861,17 +873,22 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in
Math::randomize(); Math::randomize();
} break; } break;
case VisualScriptBuiltinFunc::MATH_RAND: { case VisualScriptBuiltinFunc::MATH_RANDI: {
*r_return = Math::rand(); *r_return = Math::rand();
} break; } break;
case VisualScriptBuiltinFunc::MATH_RANDF: { case VisualScriptBuiltinFunc::MATH_RANDF: {
*r_return = Math::randf(); *r_return = Math::randf();
} break; } break;
case VisualScriptBuiltinFunc::MATH_RANDOM: { case VisualScriptBuiltinFunc::MATH_RANDF_RANGE: {
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(1);
*r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]); *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]);
} break; } break;
case VisualScriptBuiltinFunc::MATH_RANDI_RANGE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::random((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_SEED: { case VisualScriptBuiltinFunc::MATH_SEED: {
VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(0);
uint64_t seed = *p_inputs[0]; uint64_t seed = *p_inputs[0];
@@ -1283,9 +1300,10 @@ void VisualScriptBuiltinFunc::_bind_methods() {
BIND_ENUM_CONSTANT(MATH_MOVE_TOWARD); BIND_ENUM_CONSTANT(MATH_MOVE_TOWARD);
BIND_ENUM_CONSTANT(MATH_DECTIME); BIND_ENUM_CONSTANT(MATH_DECTIME);
BIND_ENUM_CONSTANT(MATH_RANDOMIZE); BIND_ENUM_CONSTANT(MATH_RANDOMIZE);
BIND_ENUM_CONSTANT(MATH_RAND); BIND_ENUM_CONSTANT(MATH_RANDI);
BIND_ENUM_CONSTANT(MATH_RANDF); BIND_ENUM_CONSTANT(MATH_RANDF);
BIND_ENUM_CONSTANT(MATH_RANDOM); BIND_ENUM_CONSTANT(MATH_RANDF_RANGE);
BIND_ENUM_CONSTANT(MATH_RANDI_RANGE);
BIND_ENUM_CONSTANT(MATH_SEED); BIND_ENUM_CONSTANT(MATH_SEED);
BIND_ENUM_CONSTANT(MATH_RANDSEED); BIND_ENUM_CONSTANT(MATH_RANDSEED);
BIND_ENUM_CONSTANT(MATH_DEG2RAD); BIND_ENUM_CONSTANT(MATH_DEG2RAD);
@@ -1375,9 +1393,10 @@ void register_visual_script_builtin_func_node() {
VisualScriptLanguage::singleton->add_register_func("functions/built_in/move_toward", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_MOVE_TOWARD>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/move_toward", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_MOVE_TOWARD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/dectime", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECTIME>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/dectime", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECTIME>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randomize", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOMIZE>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/randomize", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOMIZE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/rand", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAND>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/randi", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDI>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDF>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/randf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDF>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/random", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOM>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/randf_range", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDF_RANGE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randi_range", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDI_RANGE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/seed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SEED>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/seed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SEED>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randseed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDSEED>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/randseed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDSEED>);

View File

@@ -70,9 +70,10 @@ public:
MATH_MOVE_TOWARD, MATH_MOVE_TOWARD,
MATH_DECTIME, MATH_DECTIME,
MATH_RANDOMIZE, MATH_RANDOMIZE,
MATH_RAND, MATH_RANDI,
MATH_RANDF, MATH_RANDF,
MATH_RANDOM, MATH_RANDF_RANGE,
MATH_RANDI_RANGE,
MATH_SEED, MATH_SEED,
MATH_RANDSEED, MATH_RANDSEED,
MATH_DEG2RAD, MATH_DEG2RAD,

View File

@@ -25,8 +25,31 @@ void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
} }
// Source from https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c // Source from https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c
// pcg32_boundedrand_r(rng, bound):
// Generate a uniformly distributed number, r, where 0 <= r < bound
uint32_t pcg32_boundedrand_r(pcg32_random_t *rng, uint32_t bound) { uint32_t pcg32_boundedrand_r(pcg32_random_t *rng, uint32_t bound) {
// To avoid bias, we need to make the range of the RNG a multiple of
// bound, which we do by dropping output less than a threshold.
// A naive scheme to calculate the threshold would be to do
//
// uint32_t threshold = 0x100000000ull % bound;
//
// but 64-bit div/mod is slower than 32-bit div/mod (especially on
// 32-bit platforms). In essence, we do
//
// uint32_t threshold = (0x100000000ull-bound) % bound;
//
// because this version will calculate the same modulus, but the LHS
// value is less than 2^32.
uint32_t threshold = -bound % bound; uint32_t threshold = -bound % bound;
// Uniformity guarantees that this loop will terminate. In practice, it
// should usually terminate quickly; on average (assuming all bounds are
// equally likely), 82.25% of the time, we can expect it to require just
// one iteration. In the worst case, someone passes a bound of 2^31 + 1
// (i.e., 2147483649), which invalidates almost 50% of the range. In
// practice, bounds are typically small and only a tiny amount of the range
// is eliminated.
for (;;) { for (;;) {
uint32_t r = pcg32_random_r(rng); uint32_t r = pcg32_random_r(rng);
if (r >= threshold) if (r >= threshold)