1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
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.
9 
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.
14 
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.
18 */
19 
20 #pragma once
21 
22 #include "irr_v3d.h"
23 #include "lua_api/l_base.h"
24 #include "noise.h"
25 
26 /*
27 	LuaPerlinNoise
28 */
29 class LuaPerlinNoise : public ModApiBase
30 {
31 private:
32 	NoiseParams np;
33 	static const char className[];
34 	static luaL_Reg methods[];
35 
36 	// Exported functions
37 
38 	// garbage collector
39 	static int gc_object(lua_State *L);
40 
41 	static int l_get_2d(lua_State *L);
42 	static int l_get_3d(lua_State *L);
43 
44 public:
45 	LuaPerlinNoise(NoiseParams *params);
46 	~LuaPerlinNoise() = default;
47 
48 	// LuaPerlinNoise(seed, octaves, persistence, scale)
49 	// Creates an LuaPerlinNoise and leaves it on top of stack
50 	static int create_object(lua_State *L);
51 
52 	static LuaPerlinNoise *checkobject(lua_State *L, int narg);
53 
54 	static void Register(lua_State *L);
55 };
56 
57 /*
58 	LuaPerlinNoiseMap
59 */
60 class LuaPerlinNoiseMap : public ModApiBase
61 {
62 	NoiseParams np;
63 	Noise *noise;
64 	bool m_is3d;
65 	static const char className[];
66 	static luaL_Reg methods[];
67 
68 	// Exported functions
69 
70 	// garbage collector
71 	static int gc_object(lua_State *L);
72 
73 	static int l_get_2d_map(lua_State *L);
74 	static int l_get_2d_map_flat(lua_State *L);
75 	static int l_get_3d_map(lua_State *L);
76 	static int l_get_3d_map_flat(lua_State *L);
77 
78 	static int l_calc_2d_map(lua_State *L);
79 	static int l_calc_3d_map(lua_State *L);
80 	static int l_get_map_slice(lua_State *L);
81 
82 public:
83 	LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size);
84 
85 	~LuaPerlinNoiseMap();
86 
87 	// LuaPerlinNoiseMap(np, size)
88 	// Creates an LuaPerlinNoiseMap and leaves it on top of stack
89 	static int create_object(lua_State *L);
90 
91 	static LuaPerlinNoiseMap *checkobject(lua_State *L, int narg);
92 
93 	static void Register(lua_State *L);
94 };
95 
96 /*
97 	LuaPseudoRandom
98 */
99 class LuaPseudoRandom : public ModApiBase
100 {
101 private:
102 	PseudoRandom m_pseudo;
103 
104 	static const char className[];
105 	static const luaL_Reg methods[];
106 
107 	// Exported functions
108 
109 	// garbage collector
110 	static int gc_object(lua_State *L);
111 
112 	// next(self, min=0, max=32767) -> get next value
113 	static int l_next(lua_State *L);
114 
115 public:
LuaPseudoRandom(s32 seed)116 	LuaPseudoRandom(s32 seed) : m_pseudo(seed) {}
117 
118 	// LuaPseudoRandom(seed)
119 	// Creates an LuaPseudoRandom and leaves it on top of stack
120 	static int create_object(lua_State *L);
121 
122 	static LuaPseudoRandom *checkobject(lua_State *L, int narg);
123 
124 	static void Register(lua_State *L);
125 };
126 
127 /*
128 	LuaPcgRandom
129 */
130 class LuaPcgRandom : public ModApiBase
131 {
132 private:
133 	PcgRandom m_rnd;
134 
135 	static const char className[];
136 	static const luaL_Reg methods[];
137 
138 	// Exported functions
139 
140 	// garbage collector
141 	static int gc_object(lua_State *L);
142 
143 	// next(self, min=-2147483648, max=2147483647) -> get next value
144 	static int l_next(lua_State *L);
145 
146 	// rand_normal_dist(self, min=-2147483648, max=2147483647, num_trials=6) ->
147 	// get next normally distributed random value
148 	static int l_rand_normal_dist(lua_State *L);
149 
150 public:
LuaPcgRandom(u64 seed)151 	LuaPcgRandom(u64 seed) : m_rnd(seed) {}
LuaPcgRandom(u64 seed,u64 seq)152 	LuaPcgRandom(u64 seed, u64 seq) : m_rnd(seed, seq) {}
153 
154 	// LuaPcgRandom(seed)
155 	// Creates an LuaPcgRandom and leaves it on top of stack
156 	static int create_object(lua_State *L);
157 
158 	static LuaPcgRandom *checkobject(lua_State *L, int narg);
159 
160 	static void Register(lua_State *L);
161 };
162 
163 /*
164 	LuaSecureRandom
165 */
166 class LuaSecureRandom : public ModApiBase
167 {
168 private:
169 	static const size_t RAND_BUF_SIZE = 2048;
170 	static const char className[];
171 	static const luaL_Reg methods[];
172 
173 	u32 m_rand_idx;
174 	char m_rand_buf[RAND_BUF_SIZE];
175 
176 	// Exported functions
177 
178 	// garbage collector
179 	static int gc_object(lua_State *L);
180 
181 	// next_bytes(self, count) -> get count many bytes
182 	static int l_next_bytes(lua_State *L);
183 
184 public:
185 	bool fillRandBuf();
186 
187 	// LuaSecureRandom()
188 	// Creates an LuaSecureRandom and leaves it on top of stack
189 	static int create_object(lua_State *L);
190 
191 	static LuaSecureRandom *checkobject(lua_State *L, int narg);
192 
193 	static void Register(lua_State *L);
194 };
195