wrap_RandomGenerator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_RandomGenerator.h"
  21. #include <cmath>
  22. #include <algorithm>
  23. namespace love
  24. {
  25. namespace math
  26. {
  27. template <typename T>
  28. static T checkrandomseed_part(lua_State *L, int idx)
  29. {
  30. double num = luaL_checknumber(L, idx);
  31. double inf = std::numeric_limits<double>::infinity();
  32. // Disallow conversions from infinity and NaN.
  33. if (num == inf || num == -inf || num != num)
  34. luaL_argerror(L, idx, "invalid random seed");
  35. return (T) num;
  36. }
  37. RandomGenerator::Seed luax_checkrandomseed(lua_State *L, int idx)
  38. {
  39. RandomGenerator::Seed s;
  40. if (!lua_isnoneornil(L, idx + 1))
  41. {
  42. s.b32.low = checkrandomseed_part<uint32>(L, idx);
  43. s.b32.high = checkrandomseed_part<uint32>(L, idx + 1);
  44. }
  45. else
  46. s.b64 = checkrandomseed_part<uint64>(L, idx);
  47. return s;
  48. }
  49. int luax_getrandom(lua_State *L, int startidx, double r)
  50. {
  51. int l, u;
  52. // from lua 5.1.4 source code: lmathlib.c:185 ff.
  53. switch (lua_gettop(L) - (startidx - 1))
  54. {
  55. case 0:
  56. lua_pushnumber(L, r);
  57. break;
  58. case 1:
  59. u = luaL_checkint(L, startidx);
  60. luaL_argcheck(L, 1 <= u, startidx, "interval is empty");
  61. lua_pushnumber(L, floor(r * u) + 1);
  62. break;
  63. case 2:
  64. l = luaL_checkint(L, startidx);
  65. u = luaL_checkint(L, startidx + 1);
  66. luaL_argcheck(L, l <= u, startidx + 1, "interval is empty");
  67. lua_pushnumber(L, floor(r * (u - l + 1)) + l);
  68. break;
  69. default:
  70. return luaL_error(L, "wrong number of arguments");
  71. }
  72. return 1;
  73. }
  74. RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx)
  75. {
  76. return luax_checktype<RandomGenerator>(L, idx, "RandomGenerator", MATH_RANDOM_GENERATOR_T);
  77. }
  78. int w_RandomGenerator_random(lua_State *L)
  79. {
  80. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  81. return luax_getrandom(L, 2, rng->random());
  82. }
  83. int w_RandomGenerator_randomNormal(lua_State *L)
  84. {
  85. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  86. double stddev = luaL_optnumber(L, 2, 1.0);
  87. double mean = luaL_optnumber(L, 3, 0.0);
  88. double r = rng->randomNormal(stddev);
  89. lua_pushnumber(L, r + mean);
  90. return 1;
  91. }
  92. int w_RandomGenerator_setSeed(lua_State *L)
  93. {
  94. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  95. EXCEPT_GUARD(rng->setSeed(luax_checkrandomseed(L, 2));)
  96. return 0;
  97. }
  98. int w_RandomGenerator_getSeed(lua_State *L)
  99. {
  100. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  101. RandomGenerator::Seed s = rng->getSeed();
  102. lua_pushnumber(L, (lua_Number) s.b32.low);
  103. lua_pushnumber(L, (lua_Number) s.b32.high);
  104. return 2;
  105. }
  106. int w_RandomGenerator_setState(lua_State *L)
  107. {
  108. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  109. EXCEPT_GUARD(rng->setState(luax_checkstring(L, 2));)
  110. return 0;
  111. }
  112. int w_RandomGenerator_getState(lua_State *L)
  113. {
  114. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  115. luax_pushstring(L, rng->getState());
  116. return 1;
  117. }
  118. static const luaL_Reg functions[] =
  119. {
  120. { "random", w_RandomGenerator_random },
  121. { "randomNormal", w_RandomGenerator_randomNormal },
  122. { "setSeed", w_RandomGenerator_setSeed },
  123. { "getSeed", w_RandomGenerator_getSeed },
  124. { "setState", w_RandomGenerator_setState },
  125. { "getState", w_RandomGenerator_getState },
  126. { 0, 0 }
  127. };
  128. extern "C" int luaopen_randomgenerator(lua_State *L)
  129. {
  130. return luax_register_type(L, "RandomGenerator", functions);
  131. }
  132. } // math
  133. } // love