3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_noise.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
26 ///////////////////////////////////////
31 LuaPerlinNoise::LuaPerlinNoise(NoiseParams *params) :
37 LuaPerlinNoise::~LuaPerlinNoise()
42 int LuaPerlinNoise::l_get2d(lua_State *L)
45 LuaPerlinNoise *o = checkobject(L, 1);
46 v2f p = check_v2f(L, 2);
47 lua_Number val = NoisePerlin2D(&o->np, p.X, p.Y, 0);
48 lua_pushnumber(L, val);
53 int LuaPerlinNoise::l_get3d(lua_State *L)
56 LuaPerlinNoise *o = checkobject(L, 1);
57 v3f p = check_v3f(L, 2);
58 lua_Number val = NoisePerlin3D(&o->np, p.X, p.Y, p.Z, 0);
59 lua_pushnumber(L, val);
64 int LuaPerlinNoise::create_object(lua_State *L)
70 if (lua_istable(L, 1)) {
71 read_noiseparams(L, 1, ¶ms);
73 params.seed = luaL_checkint(L, 1);
74 params.octaves = luaL_checkint(L, 2);
75 params.persist = luaL_checknumber(L, 3);
76 params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4);
79 LuaPerlinNoise *o = new LuaPerlinNoise(¶ms);
81 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
82 luaL_getmetatable(L, className);
83 lua_setmetatable(L, -2);
88 int LuaPerlinNoise::gc_object(lua_State *L)
90 LuaPerlinNoise *o = *(LuaPerlinNoise **)(lua_touserdata(L, 1));
96 LuaPerlinNoise *LuaPerlinNoise::checkobject(lua_State *L, int narg)
99 luaL_checktype(L, narg, LUA_TUSERDATA);
100 void *ud = luaL_checkudata(L, narg, className);
102 luaL_typerror(L, narg, className);
103 return *(LuaPerlinNoise **)ud;
107 void LuaPerlinNoise::Register(lua_State *L)
110 int methodtable = lua_gettop(L);
111 luaL_newmetatable(L, className);
112 int metatable = lua_gettop(L);
114 lua_pushliteral(L, "__metatable");
115 lua_pushvalue(L, methodtable);
116 lua_settable(L, metatable);
118 lua_pushliteral(L, "__index");
119 lua_pushvalue(L, methodtable);
120 lua_settable(L, metatable);
122 lua_pushliteral(L, "__gc");
123 lua_pushcfunction(L, gc_object);
124 lua_settable(L, metatable);
128 luaL_openlib(L, 0, methods, 0);
131 lua_register(L, className, create_object);
135 const char LuaPerlinNoise::className[] = "PerlinNoise";
136 const luaL_reg LuaPerlinNoise::methods[] = {
137 luamethod(LuaPerlinNoise, get2d),
138 luamethod(LuaPerlinNoise, get3d),
142 ///////////////////////////////////////
147 LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, int seed, v3s16 size)
152 noise = new Noise(&np, seed, size.X, size.Y, size.Z);
153 } catch (InvalidNoiseParamsException &e) {
154 throw LuaError(e.what());
159 LuaPerlinNoiseMap::~LuaPerlinNoiseMap()
165 int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
167 NO_MAP_LOCK_REQUIRED;
170 LuaPerlinNoiseMap *o = checkobject(L, 1);
171 v2f p = check_v2f(L, 2);
174 n->perlinMap2D(p.X, p.Y);
177 for (int y = 0; y != n->sy; y++) {
179 for (int x = 0; x != n->sx; x++) {
180 lua_pushnumber(L, n->result[i++]);
181 lua_rawseti(L, -2, x + 1);
183 lua_rawseti(L, -2, y + 1);
189 int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
191 NO_MAP_LOCK_REQUIRED;
193 LuaPerlinNoiseMap *o = checkobject(L, 1);
194 v2f p = check_v2f(L, 2);
197 n->perlinMap2D(p.X, p.Y);
199 size_t maplen = n->sx * n->sy;
202 for (size_t i = 0; i != maplen; i++) {
203 lua_pushnumber(L, n->result[i]);
204 lua_rawseti(L, -2, i + 1);
210 int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
212 NO_MAP_LOCK_REQUIRED;
215 LuaPerlinNoiseMap *o = checkobject(L, 1);
216 v3f p = check_v3f(L, 2);
222 n->perlinMap3D(p.X, p.Y, p.Z);
225 for (int z = 0; z != n->sz; z++) {
227 for (int y = 0; y != n->sy; y++) {
229 for (int x = 0; x != n->sx; x++) {
230 lua_pushnumber(L, n->result[i++]);
231 lua_rawseti(L, -2, x + 1);
233 lua_rawseti(L, -2, y + 1);
235 lua_rawseti(L, -2, z + 1);
241 int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
243 NO_MAP_LOCK_REQUIRED;
245 LuaPerlinNoiseMap *o = checkobject(L, 1);
246 v3f p = check_v3f(L, 2);
252 n->perlinMap3D(p.X, p.Y, p.Z);
254 size_t maplen = n->sx * n->sy * n->sz;
257 for (size_t i = 0; i != maplen; i++) {
258 lua_pushnumber(L, n->result[i]);
259 lua_rawseti(L, -2, i + 1);
265 int LuaPerlinNoiseMap::create_object(lua_State *L)
268 if (!read_noiseparams(L, 1, &np))
270 v3s16 size = read_v3s16(L, 2);
272 LuaPerlinNoiseMap *o = new LuaPerlinNoiseMap(&np, 0, size);
273 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
274 luaL_getmetatable(L, className);
275 lua_setmetatable(L, -2);
280 int LuaPerlinNoiseMap::gc_object(lua_State *L)
282 LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)(lua_touserdata(L, 1));
288 LuaPerlinNoiseMap *LuaPerlinNoiseMap::checkobject(lua_State *L, int narg)
290 luaL_checktype(L, narg, LUA_TUSERDATA);
292 void *ud = luaL_checkudata(L, narg, className);
294 luaL_typerror(L, narg, className);
296 return *(LuaPerlinNoiseMap **)ud;
300 void LuaPerlinNoiseMap::Register(lua_State *L)
303 int methodtable = lua_gettop(L);
304 luaL_newmetatable(L, className);
305 int metatable = lua_gettop(L);
307 lua_pushliteral(L, "__metatable");
308 lua_pushvalue(L, methodtable);
309 lua_settable(L, metatable);
311 lua_pushliteral(L, "__index");
312 lua_pushvalue(L, methodtable);
313 lua_settable(L, metatable);
315 lua_pushliteral(L, "__gc");
316 lua_pushcfunction(L, gc_object);
317 lua_settable(L, metatable);
321 luaL_openlib(L, 0, methods, 0);
324 lua_register(L, className, create_object);
328 const char LuaPerlinNoiseMap::className[] = "PerlinNoiseMap";
329 const luaL_reg LuaPerlinNoiseMap::methods[] = {
330 luamethod(LuaPerlinNoiseMap, get2dMap),
331 luamethod(LuaPerlinNoiseMap, get2dMap_flat),
332 luamethod(LuaPerlinNoiseMap, get3dMap),
333 luamethod(LuaPerlinNoiseMap, get3dMap_flat),
337 ///////////////////////////////////////
342 int LuaPseudoRandom::l_next(lua_State *L)
344 NO_MAP_LOCK_REQUIRED;
345 LuaPseudoRandom *o = checkobject(L, 1);
349 if (lua_isnumber(L, 2))
350 min = luaL_checkinteger(L, 2);
351 if (lua_isnumber(L, 3))
352 max = luaL_checkinteger(L, 3);
354 errorstream<<"PseudoRandom.next(): max="<<max<<" min="<<min<<std::endl;
355 throw LuaError("PseudoRandom.next(): max < min");
357 if(max - min != 32767 && max - min > 32767/5)
358 throw LuaError("PseudoRandom.next() max-min is not 32767"
359 " and is > 32768/5. This is disallowed due to"
360 " the bad random distribution the"
361 " implementation would otherwise make.");
362 PseudoRandom &pseudo = o->m_pseudo;
363 int val = pseudo.next();
364 val = (val % (max-min+1)) + min;
365 lua_pushinteger(L, val);
370 int LuaPseudoRandom::create_object(lua_State *L)
372 int seed = luaL_checknumber(L, 1);
373 LuaPseudoRandom *o = new LuaPseudoRandom(seed);
374 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
375 luaL_getmetatable(L, className);
376 lua_setmetatable(L, -2);
381 int LuaPseudoRandom::gc_object(lua_State *L)
383 LuaPseudoRandom *o = *(LuaPseudoRandom **)(lua_touserdata(L, 1));
389 LuaPseudoRandom *LuaPseudoRandom::checkobject(lua_State *L, int narg)
391 luaL_checktype(L, narg, LUA_TUSERDATA);
392 void *ud = luaL_checkudata(L, narg, className);
394 luaL_typerror(L, narg, className);
395 return *(LuaPseudoRandom **)ud;
399 void LuaPseudoRandom::Register(lua_State *L)
402 int methodtable = lua_gettop(L);
403 luaL_newmetatable(L, className);
404 int metatable = lua_gettop(L);
406 lua_pushliteral(L, "__metatable");
407 lua_pushvalue(L, methodtable);
408 lua_settable(L, metatable);
410 lua_pushliteral(L, "__index");
411 lua_pushvalue(L, methodtable);
412 lua_settable(L, metatable);
414 lua_pushliteral(L, "__gc");
415 lua_pushcfunction(L, gc_object);
416 lua_settable(L, metatable);
420 luaL_openlib(L, 0, methods, 0);
423 lua_register(L, className, create_object);
427 const char LuaPseudoRandom::className[] = "PseudoRandom";
428 const luaL_reg LuaPseudoRandom::methods[] = {
429 luamethod(LuaPseudoRandom, next),
433 ///////////////////////////////////////
438 int LuaPcgRandom::l_next(lua_State *L)
440 NO_MAP_LOCK_REQUIRED;
442 LuaPcgRandom *o = checkobject(L, 1);
443 u32 min = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : o->m_rnd.RANDOM_MIN;
444 u32 max = lua_isnumber(L, 3) ? lua_tointeger(L, 3) : o->m_rnd.RANDOM_MAX;
446 lua_pushinteger(L, o->m_rnd.range(min, max));
451 int LuaPcgRandom::l_rand_normal_dist(lua_State *L)
453 NO_MAP_LOCK_REQUIRED;
455 LuaPcgRandom *o = checkobject(L, 1);
456 u32 min = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : o->m_rnd.RANDOM_MIN;
457 u32 max = lua_isnumber(L, 3) ? lua_tointeger(L, 3) : o->m_rnd.RANDOM_MAX;
458 int num_trials = lua_isnumber(L, 4) ? lua_tointeger(L, 4) : 6;
460 lua_pushinteger(L, o->m_rnd.randNormalDist(min, max, num_trials));
465 int LuaPcgRandom::create_object(lua_State *L)
467 lua_Integer seed = luaL_checknumber(L, 1);
468 LuaPcgRandom *o = lua_isnumber(L, 2) ?
469 new LuaPcgRandom(seed, lua_tointeger(L, 2)) :
470 new LuaPcgRandom(seed);
471 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
472 luaL_getmetatable(L, className);
473 lua_setmetatable(L, -2);
478 int LuaPcgRandom::gc_object(lua_State *L)
480 LuaPcgRandom *o = *(LuaPcgRandom **)(lua_touserdata(L, 1));
486 LuaPcgRandom *LuaPcgRandom::checkobject(lua_State *L, int narg)
488 luaL_checktype(L, narg, LUA_TUSERDATA);
489 void *ud = luaL_checkudata(L, narg, className);
491 luaL_typerror(L, narg, className);
492 return *(LuaPcgRandom **)ud;
496 void LuaPcgRandom::Register(lua_State *L)
499 int methodtable = lua_gettop(L);
500 luaL_newmetatable(L, className);
501 int metatable = lua_gettop(L);
503 lua_pushliteral(L, "__metatable");
504 lua_pushvalue(L, methodtable);
505 lua_settable(L, metatable);
507 lua_pushliteral(L, "__index");
508 lua_pushvalue(L, methodtable);
509 lua_settable(L, metatable);
511 lua_pushliteral(L, "__gc");
512 lua_pushcfunction(L, gc_object);
513 lua_settable(L, metatable);
517 luaL_openlib(L, 0, methods, 0);
520 lua_register(L, className, create_object);
524 const char LuaPcgRandom::className[] = "PcgRandom";
525 const luaL_reg LuaPcgRandom::methods[] = {
526 luamethod(LuaPcgRandom, next),
527 luamethod(LuaPcgRandom, rand_normal_dist),