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.
23 #include "lua_api/l_base.h"
29 class LuaPerlinNoise : public ModApiBase
34 static const char className[];
35 static luaL_Reg methods[];
40 static int gc_object(lua_State *L);
42 static int l_get_2d(lua_State *L);
43 static int l_get_3d(lua_State *L);
46 LuaPerlinNoise(const NoiseParams *params);
47 ~LuaPerlinNoise() = default;
49 // LuaPerlinNoise(seed, octaves, persistence, scale)
50 // Creates an LuaPerlinNoise and leaves it on top of stack
51 static int create_object(lua_State *L);
53 static LuaPerlinNoise *checkobject(lua_State *L, int narg);
55 static void *packIn(lua_State *L, int idx);
56 static void packOut(lua_State *L, void *ptr);
58 static void Register(lua_State *L);
64 class LuaPerlinNoiseMap : public ModApiBase
68 static const char className[];
69 static luaL_Reg methods[];
74 static int gc_object(lua_State *L);
76 static int l_get_2d_map(lua_State *L);
77 static int l_get_2d_map_flat(lua_State *L);
78 static int l_get_3d_map(lua_State *L);
79 static int l_get_3d_map_flat(lua_State *L);
81 static int l_calc_2d_map(lua_State *L);
82 static int l_calc_3d_map(lua_State *L);
83 static int l_get_map_slice(lua_State *L);
86 LuaPerlinNoiseMap(const NoiseParams *np, s32 seed, v3s16 size);
89 inline bool is3D() const { return noise->sz > 1; }
91 // LuaPerlinNoiseMap(np, size)
92 // Creates an LuaPerlinNoiseMap and leaves it on top of stack
93 static int create_object(lua_State *L);
95 static LuaPerlinNoiseMap *checkobject(lua_State *L, int narg);
97 static void *packIn(lua_State *L, int idx);
98 static void packOut(lua_State *L, void *ptr);
100 static void Register(lua_State *L);
106 class LuaPseudoRandom : public ModApiBase
109 PseudoRandom m_pseudo;
111 static const char className[];
112 static const luaL_Reg methods[];
114 // Exported functions
117 static int gc_object(lua_State *L);
119 // next(self, min=0, max=32767) -> get next value
120 static int l_next(lua_State *L);
123 LuaPseudoRandom(s32 seed) : m_pseudo(seed) {}
125 // LuaPseudoRandom(seed)
126 // Creates an LuaPseudoRandom and leaves it on top of stack
127 static int create_object(lua_State *L);
129 static LuaPseudoRandom *checkobject(lua_State *L, int narg);
131 static void Register(lua_State *L);
137 class LuaPcgRandom : public ModApiBase
142 static const char className[];
143 static const luaL_Reg methods[];
145 // Exported functions
148 static int gc_object(lua_State *L);
150 // next(self, min=-2147483648, max=2147483647) -> get next value
151 static int l_next(lua_State *L);
153 // rand_normal_dist(self, min=-2147483648, max=2147483647, num_trials=6) ->
154 // get next normally distributed random value
155 static int l_rand_normal_dist(lua_State *L);
158 LuaPcgRandom(u64 seed) : m_rnd(seed) {}
159 LuaPcgRandom(u64 seed, u64 seq) : m_rnd(seed, seq) {}
161 // LuaPcgRandom(seed)
162 // Creates an LuaPcgRandom and leaves it on top of stack
163 static int create_object(lua_State *L);
165 static LuaPcgRandom *checkobject(lua_State *L, int narg);
167 static void Register(lua_State *L);
173 class LuaSecureRandom : public ModApiBase
176 static const size_t RAND_BUF_SIZE = 2048;
177 static const char className[];
178 static const luaL_Reg methods[];
181 char m_rand_buf[RAND_BUF_SIZE];
183 // Exported functions
186 static int gc_object(lua_State *L);
188 // next_bytes(self, count) -> get count many bytes
189 static int l_next_bytes(lua_State *L);
195 // Creates an LuaSecureRandom and leaves it on top of stack
196 static int create_object(lua_State *L);
198 static LuaSecureRandom *checkobject(lua_State *L, int narg);
200 static void Register(lua_State *L);