1 | /** |
2 | * Copyright (c) 2006-2023 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 | |
21 | #include "wrap_RandomGenerator.h" |
22 | |
23 | #include <cmath> |
24 | #include <algorithm> |
25 | |
26 | // Put the Lua code directly into a raw string literal. |
27 | static const char randomgenerator_lua[] = |
28 | #include "wrap_RandomGenerator.lua" |
29 | ; |
30 | |
31 | namespace love |
32 | { |
33 | namespace math |
34 | { |
35 | |
36 | template <typename T> |
37 | static T checkrandomseed_part(lua_State *L, int idx) |
38 | { |
39 | double num = luaL_checknumber(L, idx); |
40 | double inf = std::numeric_limits<double>::infinity(); |
41 | |
42 | // Disallow conversions from infinity and NaN. |
43 | if (num == inf || num == -inf || num != num) |
44 | luaL_argerror(L, idx, "invalid random seed" ); |
45 | |
46 | return (T) num; |
47 | } |
48 | |
49 | RandomGenerator::Seed luax_checkrandomseed(lua_State *L, int idx) |
50 | { |
51 | RandomGenerator::Seed s; |
52 | |
53 | if (!lua_isnoneornil(L, idx + 1)) |
54 | { |
55 | s.b32.low = checkrandomseed_part<uint32>(L, idx); |
56 | s.b32.high = checkrandomseed_part<uint32>(L, idx + 1); |
57 | } |
58 | else |
59 | s.b64 = checkrandomseed_part<uint64>(L, idx); |
60 | |
61 | return s; |
62 | } |
63 | |
64 | RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx) |
65 | { |
66 | return luax_checktype<RandomGenerator>(L, idx); |
67 | } |
68 | |
69 | int w_RandomGenerator__random(lua_State *L) |
70 | { |
71 | RandomGenerator *rng = luax_checkrandomgenerator(L, 1); |
72 | lua_pushnumber(L, rng->random()); |
73 | return 1; |
74 | } |
75 | |
76 | int w_RandomGenerator_randomNormal(lua_State *L) |
77 | { |
78 | RandomGenerator *rng = luax_checkrandomgenerator(L, 1); |
79 | |
80 | double stddev = luaL_optnumber(L, 2, 1.0); |
81 | double mean = luaL_optnumber(L, 3, 0.0); |
82 | double r = rng->randomNormal(stddev); |
83 | |
84 | lua_pushnumber(L, r + mean); |
85 | return 1; |
86 | } |
87 | |
88 | int w_RandomGenerator_setSeed(lua_State *L) |
89 | { |
90 | RandomGenerator *rng = luax_checkrandomgenerator(L, 1); |
91 | luax_catchexcept(L, [&](){ rng->setSeed(luax_checkrandomseed(L, 2)); }); |
92 | return 0; |
93 | } |
94 | |
95 | int w_RandomGenerator_getSeed(lua_State *L) |
96 | { |
97 | RandomGenerator *rng = luax_checkrandomgenerator(L, 1); |
98 | RandomGenerator::Seed s = rng->getSeed(); |
99 | lua_pushnumber(L, (lua_Number) s.b32.low); |
100 | lua_pushnumber(L, (lua_Number) s.b32.high); |
101 | return 2; |
102 | } |
103 | |
104 | int w_RandomGenerator_setState(lua_State *L) |
105 | { |
106 | RandomGenerator *rng = luax_checkrandomgenerator(L, 1); |
107 | luax_catchexcept(L, [&](){ rng->setState(luax_checkstring(L, 2)); }); |
108 | return 0; |
109 | } |
110 | |
111 | int w_RandomGenerator_getState(lua_State *L) |
112 | { |
113 | RandomGenerator *rng = luax_checkrandomgenerator(L, 1); |
114 | luax_pushstring(L, rng->getState()); |
115 | return 1; |
116 | } |
117 | |
118 | // C functions in a struct, necessary for the FFI versions of RandomGenerator functions. |
119 | struct FFI_RandomGenerator |
120 | { |
121 | double (*random)(Proxy *p); |
122 | double (*randomNormal)(Proxy *p, double stddev, double mean); |
123 | }; |
124 | |
125 | static FFI_RandomGenerator ffifuncs = |
126 | { |
127 | [](Proxy *p) -> double // random() |
128 | { |
129 | auto rng = luax_ffi_checktype<RandomGenerator>(p); |
130 | return rng != nullptr ? rng->random() : 0.0; |
131 | }, |
132 | |
133 | [](Proxy *p, double stdddev, double mean) -> double // randomNormal |
134 | { |
135 | auto rng = luax_ffi_checktype<RandomGenerator>(p); |
136 | return rng != nullptr ? (rng->randomNormal(stdddev) + mean) : 0.0; |
137 | } |
138 | }; |
139 | |
140 | static const luaL_Reg w_RandomGenerator_functions[] = |
141 | { |
142 | { "_random" , w_RandomGenerator__random }, // random() is defined in wrap_RandomGenerator.lua. |
143 | { "randomNormal" , w_RandomGenerator_randomNormal }, |
144 | { "setSeed" , w_RandomGenerator_setSeed }, |
145 | { "getSeed" , w_RandomGenerator_getSeed }, |
146 | { "setState" , w_RandomGenerator_setState }, |
147 | { "getState" , w_RandomGenerator_getState }, |
148 | { 0, 0 } |
149 | }; |
150 | |
151 | extern "C" int luaopen_randomgenerator(lua_State *L) |
152 | { |
153 | int n = luax_register_type(L, &RandomGenerator::type, w_RandomGenerator_functions, nullptr); |
154 | |
155 | luax_runwrapper(L, randomgenerator_lua, sizeof(randomgenerator_lua), "RandomGenerator.lua" , RandomGenerator::type, &ffifuncs); |
156 | |
157 | return n; |
158 | } |
159 | |
160 | } // math |
161 | } // love |
162 | |