wrap_RandomGenerator.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright (c) 2006-2013 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. namespace love
  23. {
  24. namespace math
  25. {
  26. RandomGenerator::State luax_checkrandomstate(lua_State *L, int idx)
  27. {
  28. RandomGenerator::State s;
  29. if (!lua_isnoneornil(L, idx + 1))
  30. {
  31. uint32 low = (uint32) luaL_checknumber(L, idx);
  32. uint32 high = (uint32) luaL_checknumber(L, idx + 1);
  33. #ifdef LOVE_BIG_ENDIAN
  34. s.b32.a = high;
  35. s.b32.b = low;
  36. #else
  37. s.b32.b = high;
  38. s.b32.a = low;
  39. #endif
  40. }
  41. else
  42. s.b64 = (uint64) luaL_checknumber(L, idx);
  43. return s;
  44. }
  45. int luax_getrandom(lua_State *L, int startidx, double r)
  46. {
  47. int l, u;
  48. // from lua 5.1.4 source code: lmathlib.c:185 ff.
  49. switch (lua_gettop(L) - (startidx - 1))
  50. {
  51. case 0:
  52. lua_pushnumber(L, r);
  53. break;
  54. case 1:
  55. u = luaL_checkint(L, startidx);
  56. luaL_argcheck(L, 1 <= u, startidx, "interval is empty");
  57. lua_pushnumber(L, floor(r * u) + 1);
  58. break;
  59. case 2:
  60. l = luaL_checkint(L, startidx);
  61. u = luaL_checkint(L, startidx + 1);
  62. luaL_argcheck(L, l <= u, startidx + 1, "interval is empty");
  63. lua_pushnumber(L, floor(r * (u - l + 1)) + l);
  64. break;
  65. default:
  66. return luaL_error(L, "wrong number of arguments");
  67. }
  68. return 1;
  69. }
  70. RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx)
  71. {
  72. return luax_checktype<RandomGenerator>(L, idx, "RandomGenerator", MATH_RANDOM_GENERATOR_T);
  73. }
  74. int w_RandomGenerator_setState(lua_State *L)
  75. {
  76. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  77. rng->setState(luax_checkrandomstate(L, 2));
  78. return 0;
  79. }
  80. int w_RandomGenerator_getState(lua_State *L)
  81. {
  82. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  83. uint32 low = 0, high = 0;
  84. rng->getState(low, high);
  85. lua_pushnumber(L, (lua_Number) low);
  86. lua_pushnumber(L, (lua_Number) high);
  87. return 2;
  88. }
  89. int w_RandomGenerator_random(lua_State *L)
  90. {
  91. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  92. return luax_getrandom(L, 2, rng->random());
  93. }
  94. int w_RandomGenerator_randomNormal(lua_State *L)
  95. {
  96. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  97. double stddev = luaL_optnumber(L, 2, 1.0);
  98. double mean = luaL_optnumber(L, 3, 0.0);
  99. double r = rng->randomNormal(stddev);
  100. lua_pushnumber(L, r + mean);
  101. return 1;
  102. }
  103. static const luaL_Reg functions[] =
  104. {
  105. { "setState", w_RandomGenerator_setState },
  106. { "getState", w_RandomGenerator_getState },
  107. { "random", w_RandomGenerator_random },
  108. { "randomNormal", w_RandomGenerator_randomNormal },
  109. { 0, 0 }
  110. };
  111. extern "C" int luaopen_randomgenerator(lua_State *L)
  112. {
  113. return luax_register_type(L, "RandomGenerator", functions);
  114. }
  115. } // math
  116. } // love