2
0
Эх сурвалжийг харах

Added Random() & RandomInt() script functions with min & max range parameters. Similar to the RandomInt(range) overload, in RandomInt(min, max) the max value is exclusive. Note that the corresponding C++ functions are simply called Random for both int and float versions. Closes #48.

Lasse Öörni 12 жил өмнө
parent
commit
a022430876

+ 2 - 0
Docs/AngelScriptAPI.h

@@ -8315,8 +8315,10 @@ float Floor(float);
 float Ceil(float);
 float Random();
 float Random(float);
+float Random(float, float);
 int RandomInt();
 int RandomInt(int);
+int RandomInt(int, int);
 void SetRandomSeed(uint);
 uint GetRandomSeed();
 String ToStringHex(int);

+ 3 - 1
Docs/LuaScriptAPI.dox

@@ -4803,7 +4803,9 @@ Properties:
 - bool Equals(float lhs, float rhs)
 - float Random()
 - float Random(float range)
-- int RandoRandomInt(int range)
+- float Random(float min, float max)
+- int RandomInt(int range)
+- int RandomInt(int min, int max)
 - void SetRandomSeed(unsigned seed)
 - unsigned GetRandomSeed()
 - int Rand()

+ 2 - 0
Docs/ScriptAPI.dox

@@ -7053,8 +7053,10 @@ Properties:
 - float Ceil(float)
 - float Random()
 - float Random(float)
+- float Random(float, float)
 - int RandomInt()
 - int RandomInt(int)
+- int RandomInt(int, int)
 - void SetRandomSeed(uint)
 - uint GetRandomSeed()
 - String ToStringHex(int)

+ 2 - 0
Source/Engine/LuaScript/pkgs/Math/MathDefs.pkg

@@ -31,4 +31,6 @@ bool Equals(float lhs, float rhs);
 
 float Random();
 float Random(float range);
+float Random(float min, float max);
 int Random @ RandomInt(int range);
+int Random @ RandomInt(int min, int max);

+ 1 - 1
Source/Engine/LuaScript/pkgs/pkgToDox.lua

@@ -176,7 +176,7 @@ end
 function writeGlobalFunctions(ofile)
     ofile:write("\\section LuaScriptAPI_GlobalFunctions Global functions\n")
     for _, line in ipairs(globalFunctions) do
-        line = line:gsub("%a @ ", "")
+        line = line:gsub("%w+ @ ", "")
         line = line:gsub(";", "")
         ofile:write("- " .. line .. "\n")
     end

+ 4 - 0
Source/Engine/Math/MathDefs.h

@@ -136,7 +136,11 @@ inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6
 inline float Random() { return Rand() / 32768.0f; }
 /// Return a random float between 0.0 and range, inclusive from both ends.
 inline float Random(float range) { return Rand() * range / 32767.0f; }
+/// Return a random float between min and max, inclusive from both ends.
+inline float Random(float min, float max) { return Rand() * (max - min) / 32767.0f + min; }
 /// Return a random integer between 0 and range - 1.
 inline int Random(int range) { return (Rand() * (range - 1) + 16384) / 32767; }
+/// Return a random integer between min and max - 1.
+inline int Random(int min, int max) { return (Rand() * (max - min - 1) + 16384) / 32767 + min; }
 
 }

+ 2 - 0
Source/Engine/Script/MathAPI.cpp

@@ -70,8 +70,10 @@ static void RegisterMathFunctions(asIScriptEngine* engine)
     engine->RegisterGlobalFunction("float Ceil(float)", asFUNCTION(ceilf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Random()", asFUNCTIONPR(Random, (), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Random(float)", asFUNCTIONPR(Random, (float), float), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Random(float, float)", asFUNCTIONPR(Random, (float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt()", asFUNCTION(Rand), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt(int)", asFUNCTIONPR(Random, (int), int), asCALL_CDECL);
+    engine->RegisterGlobalFunction("int RandomInt(int, int)", asFUNCTIONPR(Random, (int, int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("void SetRandomSeed(uint)", asFUNCTION(SetRandomSeed), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint GetRandomSeed()", asFUNCTION(GetRandomSeed), asCALL_CDECL);
 }