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 "scriptapi.h"
21 #include "scriptapi_types.h"
27 #include "util/numeric.h"
31 C struct <-> Lua table converter functions
34 void push_v3f(lua_State *L, v3f p)
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");
45 void push_v2f(lua_State *L, v2f p)
48 lua_pushnumber(L, p.X);
49 lua_setfield(L, -2, "x");
50 lua_pushnumber(L, p.Y);
51 lua_setfield(L, -2, "y");
54 v2s16 read_v2s16(lua_State *L, int index)
57 luaL_checktype(L, index, LUA_TTABLE);
58 lua_getfield(L, index, "x");
59 p.X = lua_tonumber(L, -1);
61 lua_getfield(L, index, "y");
62 p.Y = lua_tonumber(L, -1);
67 v2f read_v2f(lua_State *L, int index)
70 luaL_checktype(L, index, LUA_TTABLE);
71 lua_getfield(L, index, "x");
72 p.X = lua_tonumber(L, -1);
74 lua_getfield(L, index, "y");
75 p.Y = lua_tonumber(L, -1);
80 v3f read_v3f(lua_State *L, int index)
83 luaL_checktype(L, index, LUA_TTABLE);
84 lua_getfield(L, index, "x");
85 pos.X = lua_tonumber(L, -1);
87 lua_getfield(L, index, "y");
88 pos.Y = lua_tonumber(L, -1);
90 lua_getfield(L, index, "z");
91 pos.Z = lua_tonumber(L, -1);
96 v3f check_v3f(lua_State *L, int index)
99 luaL_checktype(L, index, LUA_TTABLE);
100 lua_getfield(L, index, "x");
101 pos.X = luaL_checknumber(L, -1);
103 lua_getfield(L, index, "y");
104 pos.Y = luaL_checknumber(L, -1);
106 lua_getfield(L, index, "z");
107 pos.Z = luaL_checknumber(L, -1);
112 void pushFloatPos(lua_State *L, v3f p)
118 v3f checkFloatPos(lua_State *L, int index)
120 return check_v3f(L, index) * BS;
123 void push_v3s16(lua_State *L, v3s16 p)
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");
134 v3s16 read_v3s16(lua_State *L, int index)
136 // Correct rounding at <0
137 v3f pf = read_v3f(L, index);
138 return floatToInt(pf, 1.0);
141 v3s16 check_v3s16(lua_State *L, int index)
143 // Correct rounding at <0
144 v3f pf = check_v3f(L, index);
145 return floatToInt(pf, 1.0);
148 video::SColor readARGB8(lua_State *L, int index)
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));
156 lua_getfield(L, index, "r");
157 color.setRed(lua_tonumber(L, -1));
159 lua_getfield(L, index, "g");
160 color.setGreen(lua_tonumber(L, -1));
162 lua_getfield(L, index, "b");
163 color.setBlue(lua_tonumber(L, -1));
168 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
171 if(lua_istable(L, index)){
172 lua_rawgeti(L, index, 1);
173 box.MinEdge.X = lua_tonumber(L, -1) * scale;
175 lua_rawgeti(L, index, 2);
176 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
178 lua_rawgeti(L, index, 3);
179 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
181 lua_rawgeti(L, index, 4);
182 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
184 lua_rawgeti(L, index, 5);
185 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
187 lua_rawgeti(L, index, 6);
188 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
194 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
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;
207 if(possibly_single_box){
209 boxes.push_back(read_aabb3f(L, index, scale));
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));
226 bool getstringfield(lua_State *L, int table,
227 const char *fieldname, std::string &result)
229 lua_getfield(L, table, fieldname);
231 if(lua_isstring(L, -1)){
233 const char *ptr = lua_tolstring(L, -1, &len);
234 result.assign(ptr, len);
241 bool getintfield(lua_State *L, int table,
242 const char *fieldname, int &result)
244 lua_getfield(L, table, fieldname);
246 if(lua_isnumber(L, -1)){
247 result = lua_tonumber(L, -1);
254 bool getfloatfield(lua_State *L, int table,
255 const char *fieldname, float &result)
257 lua_getfield(L, table, fieldname);
259 if(lua_isnumber(L, -1)){
260 result = lua_tonumber(L, -1);
267 bool getboolfield(lua_State *L, int table,
268 const char *fieldname, bool &result)
270 lua_getfield(L, table, fieldname);
272 if(lua_isboolean(L, -1)){
273 result = lua_toboolean(L, -1);
280 std::string checkstringfield(lua_State *L, int table,
281 const char *fieldname)
283 lua_getfield(L, table, fieldname);
284 std::string s = luaL_checkstring(L, -1);
289 std::string getstringfield_default(lua_State *L, int table,
290 const char *fieldname, const std::string &default_)
292 std::string result = default_;
293 getstringfield(L, table, fieldname, result);
297 int getintfield_default(lua_State *L, int table,
298 const char *fieldname, int default_)
300 int result = default_;
301 getintfield(L, table, fieldname, result);
305 float getfloatfield_default(lua_State *L, int table,
306 const char *fieldname, float default_)
308 float result = default_;
309 getfloatfield(L, table, fieldname, result);
313 bool getboolfield_default(lua_State *L, int table,
314 const char *fieldname, bool default_)
316 bool result = default_;
317 getboolfield(L, table, fieldname, result);
321 void setintfield(lua_State *L, int table,
322 const char *fieldname, int value)
324 lua_pushinteger(L, value);
327 lua_setfield(L, table, fieldname);
330 void setfloatfield(lua_State *L, int table,
331 const char *fieldname, float value)
333 lua_pushnumber(L, value);
336 lua_setfield(L, table, fieldname);
339 void setboolfield(lua_State *L, int table,
340 const char *fieldname, bool value)
342 lua_pushboolean(L, value);
345 lua_setfield(L, table, fieldname);
348 u32 getflagsfield(lua_State *L, int table,
349 const char *fieldname, FlagDesc *flagdesc) {
350 std::string flagstring;
352 flagstring = getstringfield_default(L, table, fieldname, "");
353 return readFlagString(flagstring, flagdesc);
356 /* minetest specific types */
357 MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
359 lua_getfield(L, index, "name");
360 const char *name = luaL_checkstring(L, -1);
363 lua_getfield(L, index, "param1");
367 param1 = lua_tonumber(L, -1);
370 lua_getfield(L, index, "param2");
374 param2 = lua_tonumber(L, -1);
376 return MapNode(ndef, name, param1, param2);
379 void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
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");