| 1091 | } |
| 1092 | |
| 1093 | static int dfhack_random_init(lua_State *L) |
| 1094 | { |
| 1095 | lua_settop(L, 3); |
| 1096 | |
| 1097 | MersenneRNG *prng = check_random_native(L, 1); |
| 1098 | |
| 1099 | if (lua_isnil(L, 2)) |
| 1100 | prng->init(); |
| 1101 | else |
| 1102 | { |
| 1103 | vector<uint32_t> data; |
| 1104 | int tcnt = luaL_optint(L, 3, 1); |
| 1105 | |
| 1106 | if (lua_isnumber(L, 2)) |
| 1107 | data.push_back(lua_tounsigned(L, 2)); |
| 1108 | else if (lua_istable(L, 2)) |
| 1109 | { |
| 1110 | int cnt = lua_rawlen(L, 2); |
| 1111 | if (cnt <= 0) |
| 1112 | luaL_argerror(L, 2, "empty list in dfhack.random.init"); |
| 1113 | |
| 1114 | for (int i = 1; i <= cnt; i++) |
| 1115 | { |
| 1116 | lua_rawgeti(L, 2, i); |
| 1117 | if (!lua_isnumber(L, -1)) |
| 1118 | luaL_argerror(L, 2, "not a number in dfhack.random.init argument"); |
| 1119 | |
| 1120 | data.push_back(lua_tounsigned(L, -1)); |
| 1121 | lua_pop(L, 1); |
| 1122 | } |
| 1123 | } |
| 1124 | else |
| 1125 | luaL_argerror(L, 2, "dfhack.random.init argument not number or table"); |
| 1126 | |
| 1127 | prng->init(data.data(), data.size(), tcnt); |
| 1128 | } |
| 1129 | |
| 1130 | lua_settop(L, 1); |
| 1131 | return 1; |
| 1132 | } |
| 1133 | |
| 1134 | static int dfhack_random_new(lua_State *L) |
| 1135 | { |
no test coverage detected