]> git.lizzy.rs Git - minetest.git/blob - src/scriptapi_types.cpp
Fix nearly all warnings
[minetest.git] / src / scriptapi_types.cpp
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 #include "scriptapi.h"
21 #include "scriptapi_types.h"
22
23 extern "C" {
24 #include <lauxlib.h>
25 }
26
27 #include "util/numeric.h"
28 #include "nodedef.h"
29
30 /*
31         C struct <-> Lua table converter functions
32 */
33
34 void push_v3f(lua_State *L, v3f p)
35 {
36         lua_newtable(L);
37         lua_pushnumber(L, p.X);
38         lua_setfield(L, -2, "x");
39         lua_pushnumber(L, p.Y);
40         lua_setfield(L, -2, "y");
41         lua_pushnumber(L, p.Z);
42         lua_setfield(L, -2, "z");
43 }
44
45 void push_v2f(lua_State *L, v2f p)
46 {
47         lua_newtable(L);
48         lua_pushnumber(L, p.X);
49         lua_setfield(L, -2, "x");
50         lua_pushnumber(L, p.Y);
51         lua_setfield(L, -2, "y");
52 }
53
54 v2s16 read_v2s16(lua_State *L, int index)
55 {
56         v2s16 p;
57         luaL_checktype(L, index, LUA_TTABLE);
58         lua_getfield(L, index, "x");
59         p.X = lua_tonumber(L, -1);
60         lua_pop(L, 1);
61         lua_getfield(L, index, "y");
62         p.Y = lua_tonumber(L, -1);
63         lua_pop(L, 1);
64         return p;
65 }
66
67 v2f read_v2f(lua_State *L, int index)
68 {
69         v2f p;
70         luaL_checktype(L, index, LUA_TTABLE);
71         lua_getfield(L, index, "x");
72         p.X = lua_tonumber(L, -1);
73         lua_pop(L, 1);
74         lua_getfield(L, index, "y");
75         p.Y = lua_tonumber(L, -1);
76         lua_pop(L, 1);
77         return p;
78 }
79
80 v3f read_v3f(lua_State *L, int index)
81 {
82         v3f pos;
83         luaL_checktype(L, index, LUA_TTABLE);
84         lua_getfield(L, index, "x");
85         pos.X = lua_tonumber(L, -1);
86         lua_pop(L, 1);
87         lua_getfield(L, index, "y");
88         pos.Y = lua_tonumber(L, -1);
89         lua_pop(L, 1);
90         lua_getfield(L, index, "z");
91         pos.Z = lua_tonumber(L, -1);
92         lua_pop(L, 1);
93         return pos;
94 }
95
96 v3f check_v3f(lua_State *L, int index)
97 {
98         v3f pos;
99         luaL_checktype(L, index, LUA_TTABLE);
100         lua_getfield(L, index, "x");
101         pos.X = luaL_checknumber(L, -1);
102         lua_pop(L, 1);
103         lua_getfield(L, index, "y");
104         pos.Y = luaL_checknumber(L, -1);
105         lua_pop(L, 1);
106         lua_getfield(L, index, "z");
107         pos.Z = luaL_checknumber(L, -1);
108         lua_pop(L, 1);
109         return pos;
110 }
111
112 void pushFloatPos(lua_State *L, v3f p)
113 {
114         p /= BS;
115         push_v3f(L, p);
116 }
117
118 v3f checkFloatPos(lua_State *L, int index)
119 {
120         return check_v3f(L, index) * BS;
121 }
122
123 void push_v3s16(lua_State *L, v3s16 p)
124 {
125         lua_newtable(L);
126         lua_pushnumber(L, p.X);
127         lua_setfield(L, -2, "x");
128         lua_pushnumber(L, p.Y);
129         lua_setfield(L, -2, "y");
130         lua_pushnumber(L, p.Z);
131         lua_setfield(L, -2, "z");
132 }
133
134 v3s16 read_v3s16(lua_State *L, int index)
135 {
136         // Correct rounding at <0
137         v3f pf = read_v3f(L, index);
138         return floatToInt(pf, 1.0);
139 }
140
141 v3s16 check_v3s16(lua_State *L, int index)
142 {
143         // Correct rounding at <0
144         v3f pf = check_v3f(L, index);
145         return floatToInt(pf, 1.0);
146 }
147
148 video::SColor readARGB8(lua_State *L, int index)
149 {
150         video::SColor color(0);
151         luaL_checktype(L, index, LUA_TTABLE);
152         lua_getfield(L, index, "a");
153         if(lua_isnumber(L, -1))
154                 color.setAlpha(lua_tonumber(L, -1));
155         lua_pop(L, 1);
156         lua_getfield(L, index, "r");
157         color.setRed(lua_tonumber(L, -1));
158         lua_pop(L, 1);
159         lua_getfield(L, index, "g");
160         color.setGreen(lua_tonumber(L, -1));
161         lua_pop(L, 1);
162         lua_getfield(L, index, "b");
163         color.setBlue(lua_tonumber(L, -1));
164         lua_pop(L, 1);
165         return color;
166 }
167
168 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
169 {
170         aabb3f box;
171         if(lua_istable(L, index)){
172                 lua_rawgeti(L, index, 1);
173                 box.MinEdge.X = lua_tonumber(L, -1) * scale;
174                 lua_pop(L, 1);
175                 lua_rawgeti(L, index, 2);
176                 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
177                 lua_pop(L, 1);
178                 lua_rawgeti(L, index, 3);
179                 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
180                 lua_pop(L, 1);
181                 lua_rawgeti(L, index, 4);
182                 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
183                 lua_pop(L, 1);
184                 lua_rawgeti(L, index, 5);
185                 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
186                 lua_pop(L, 1);
187                 lua_rawgeti(L, index, 6);
188                 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
189                 lua_pop(L, 1);
190         }
191         return box;
192 }
193
194 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
195 {
196         std::vector<aabb3f> boxes;
197         if(lua_istable(L, index)){
198                 int n = lua_objlen(L, index);
199                 // Check if it's a single box or a list of boxes
200                 bool possibly_single_box = (n == 6);
201                 for(int i = 1; i <= n && possibly_single_box; i++){
202                         lua_rawgeti(L, index, i);
203                         if(!lua_isnumber(L, -1))
204                                 possibly_single_box = false;
205                         lua_pop(L, 1);
206                 }
207                 if(possibly_single_box){
208                         // Read a single box
209                         boxes.push_back(read_aabb3f(L, index, scale));
210                 } else {
211                         // Read a list of boxes
212                         for(int i = 1; i <= n; i++){
213                                 lua_rawgeti(L, index, i);
214                                 boxes.push_back(read_aabb3f(L, -1, scale));
215                                 lua_pop(L, 1);
216                         }
217                 }
218         }
219         return boxes;
220 }
221
222 /*
223         Table field getters
224 */
225
226 bool getstringfield(lua_State *L, int table,
227                 const char *fieldname, std::string &result)
228 {
229         lua_getfield(L, table, fieldname);
230         bool got = false;
231         if(lua_isstring(L, -1)){
232                 size_t len = 0;
233                 const char *ptr = lua_tolstring(L, -1, &len);
234                 result.assign(ptr, len);
235                 got = true;
236         }
237         lua_pop(L, 1);
238         return got;
239 }
240
241 bool getintfield(lua_State *L, int table,
242                 const char *fieldname, int &result)
243 {
244         lua_getfield(L, table, fieldname);
245         bool got = false;
246         if(lua_isnumber(L, -1)){
247                 result = lua_tonumber(L, -1);
248                 got = true;
249         }
250         lua_pop(L, 1);
251         return got;
252 }
253
254 bool getfloatfield(lua_State *L, int table,
255                 const char *fieldname, float &result)
256 {
257         lua_getfield(L, table, fieldname);
258         bool got = false;
259         if(lua_isnumber(L, -1)){
260                 result = lua_tonumber(L, -1);
261                 got = true;
262         }
263         lua_pop(L, 1);
264         return got;
265 }
266
267 bool getboolfield(lua_State *L, int table,
268                 const char *fieldname, bool &result)
269 {
270         lua_getfield(L, table, fieldname);
271         bool got = false;
272         if(lua_isboolean(L, -1)){
273                 result = lua_toboolean(L, -1);
274                 got = true;
275         }
276         lua_pop(L, 1);
277         return got;
278 }
279
280 std::string checkstringfield(lua_State *L, int table,
281                 const char *fieldname)
282 {
283         lua_getfield(L, table, fieldname);
284         std::string s = luaL_checkstring(L, -1);
285         lua_pop(L, 1);
286         return s;
287 }
288
289 std::string getstringfield_default(lua_State *L, int table,
290                 const char *fieldname, const std::string &default_)
291 {
292         std::string result = default_;
293         getstringfield(L, table, fieldname, result);
294         return result;
295 }
296
297 int getintfield_default(lua_State *L, int table,
298                 const char *fieldname, int default_)
299 {
300         int result = default_;
301         getintfield(L, table, fieldname, result);
302         return result;
303 }
304
305 float getfloatfield_default(lua_State *L, int table,
306                 const char *fieldname, float default_)
307 {
308         float result = default_;
309         getfloatfield(L, table, fieldname, result);
310         return result;
311 }
312
313 bool getboolfield_default(lua_State *L, int table,
314                 const char *fieldname, bool default_)
315 {
316         bool result = default_;
317         getboolfield(L, table, fieldname, result);
318         return result;
319 }
320
321 void setintfield(lua_State *L, int table,
322                 const char *fieldname, int value)
323 {
324         lua_pushinteger(L, value);
325         if(table < 0)
326                 table -= 1;
327         lua_setfield(L, table, fieldname);
328 }
329
330 void setfloatfield(lua_State *L, int table,
331                 const char *fieldname, float value)
332 {
333         lua_pushnumber(L, value);
334         if(table < 0)
335                 table -= 1;
336         lua_setfield(L, table, fieldname);
337 }
338
339 void setboolfield(lua_State *L, int table,
340                 const char *fieldname, bool value)
341 {
342         lua_pushboolean(L, value);
343         if(table < 0)
344                 table -= 1;
345         lua_setfield(L, table, fieldname);
346 }
347
348 u32 getflagsfield(lua_State *L, int table,
349                 const char *fieldname, FlagDesc *flagdesc) {
350         std::string flagstring;
351         
352         flagstring = getstringfield_default(L, table, fieldname, "");
353         return readFlagString(flagstring, flagdesc);
354 }
355
356 /* minetest specific types */
357 MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
358 {
359         lua_getfield(L, index, "name");
360         const char *name = luaL_checkstring(L, -1);
361         lua_pop(L, 1);
362         u8 param1;
363         lua_getfield(L, index, "param1");
364         if(lua_isnil(L, -1))
365                 param1 = 0;
366         else
367                 param1 = lua_tonumber(L, -1);
368         lua_pop(L, 1);
369         u8 param2;
370         lua_getfield(L, index, "param2");
371         if(lua_isnil(L, -1))
372                 param2 = 0;
373         else
374                 param2 = lua_tonumber(L, -1);
375         lua_pop(L, 1);
376         return MapNode(ndef, name, param1, param2);
377 }
378
379 void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
380 {
381         lua_newtable(L);
382         lua_pushstring(L, ndef->get(n).name.c_str());
383         lua_setfield(L, -2, "name");
384         lua_pushnumber(L, n.getParam1());
385         lua_setfield(L, -2, "param1");
386         lua_pushnumber(L, n.getParam2());
387         lua_setfield(L, -2, "param2");
388 }