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 v2s16 read_v2s16(lua_State *L, int index)
48 luaL_checktype(L, index, LUA_TTABLE);
49 lua_getfield(L, index, "x");
50 p.X = lua_tonumber(L, -1);
52 lua_getfield(L, index, "y");
53 p.Y = lua_tonumber(L, -1);
58 v2f read_v2f(lua_State *L, int index)
61 luaL_checktype(L, index, LUA_TTABLE);
62 lua_getfield(L, index, "x");
63 p.X = lua_tonumber(L, -1);
65 lua_getfield(L, index, "y");
66 p.Y = lua_tonumber(L, -1);
71 v3f read_v3f(lua_State *L, int index)
74 luaL_checktype(L, index, LUA_TTABLE);
75 lua_getfield(L, index, "x");
76 pos.X = lua_tonumber(L, -1);
78 lua_getfield(L, index, "y");
79 pos.Y = lua_tonumber(L, -1);
81 lua_getfield(L, index, "z");
82 pos.Z = lua_tonumber(L, -1);
87 v3f check_v3f(lua_State *L, int index)
90 luaL_checktype(L, index, LUA_TTABLE);
91 lua_getfield(L, index, "x");
92 pos.X = luaL_checknumber(L, -1);
94 lua_getfield(L, index, "y");
95 pos.Y = luaL_checknumber(L, -1);
97 lua_getfield(L, index, "z");
98 pos.Z = luaL_checknumber(L, -1);
103 void pushFloatPos(lua_State *L, v3f p)
109 v3f checkFloatPos(lua_State *L, int index)
111 return check_v3f(L, index) * BS;
114 void push_v3s16(lua_State *L, v3s16 p)
117 lua_pushnumber(L, p.X);
118 lua_setfield(L, -2, "x");
119 lua_pushnumber(L, p.Y);
120 lua_setfield(L, -2, "y");
121 lua_pushnumber(L, p.Z);
122 lua_setfield(L, -2, "z");
125 v3s16 read_v3s16(lua_State *L, int index)
127 // Correct rounding at <0
128 v3f pf = read_v3f(L, index);
129 return floatToInt(pf, 1.0);
132 v3s16 check_v3s16(lua_State *L, int index)
134 // Correct rounding at <0
135 v3f pf = check_v3f(L, index);
136 return floatToInt(pf, 1.0);
139 video::SColor readARGB8(lua_State *L, int index)
142 luaL_checktype(L, index, LUA_TTABLE);
143 lua_getfield(L, index, "a");
144 if(lua_isnumber(L, -1))
145 color.setAlpha(lua_tonumber(L, -1));
147 lua_getfield(L, index, "r");
148 color.setRed(lua_tonumber(L, -1));
150 lua_getfield(L, index, "g");
151 color.setGreen(lua_tonumber(L, -1));
153 lua_getfield(L, index, "b");
154 color.setBlue(lua_tonumber(L, -1));
159 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
162 if(lua_istable(L, index)){
163 lua_rawgeti(L, index, 1);
164 box.MinEdge.X = lua_tonumber(L, -1) * scale;
166 lua_rawgeti(L, index, 2);
167 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
169 lua_rawgeti(L, index, 3);
170 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
172 lua_rawgeti(L, index, 4);
173 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
175 lua_rawgeti(L, index, 5);
176 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
178 lua_rawgeti(L, index, 6);
179 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
185 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
187 std::vector<aabb3f> boxes;
188 if(lua_istable(L, index)){
189 int n = lua_objlen(L, index);
190 // Check if it's a single box or a list of boxes
191 bool possibly_single_box = (n == 6);
192 for(int i = 1; i <= n && possibly_single_box; i++){
193 lua_rawgeti(L, index, i);
194 if(!lua_isnumber(L, -1))
195 possibly_single_box = false;
198 if(possibly_single_box){
200 boxes.push_back(read_aabb3f(L, index, scale));
202 // Read a list of boxes
203 for(int i = 1; i <= n; i++){
204 lua_rawgeti(L, index, i);
205 boxes.push_back(read_aabb3f(L, -1, scale));
217 bool getstringfield(lua_State *L, int table,
218 const char *fieldname, std::string &result)
220 lua_getfield(L, table, fieldname);
222 if(lua_isstring(L, -1)){
224 const char *ptr = lua_tolstring(L, -1, &len);
225 result.assign(ptr, len);
232 bool getintfield(lua_State *L, int table,
233 const char *fieldname, int &result)
235 lua_getfield(L, table, fieldname);
237 if(lua_isnumber(L, -1)){
238 result = lua_tonumber(L, -1);
245 bool getfloatfield(lua_State *L, int table,
246 const char *fieldname, float &result)
248 lua_getfield(L, table, fieldname);
250 if(lua_isnumber(L, -1)){
251 result = lua_tonumber(L, -1);
258 bool getboolfield(lua_State *L, int table,
259 const char *fieldname, bool &result)
261 lua_getfield(L, table, fieldname);
263 if(lua_isboolean(L, -1)){
264 result = lua_toboolean(L, -1);
271 std::string checkstringfield(lua_State *L, int table,
272 const char *fieldname)
274 lua_getfield(L, table, fieldname);
275 std::string s = luaL_checkstring(L, -1);
280 std::string getstringfield_default(lua_State *L, int table,
281 const char *fieldname, const std::string &default_)
283 std::string result = default_;
284 getstringfield(L, table, fieldname, result);
288 int getintfield_default(lua_State *L, int table,
289 const char *fieldname, int default_)
291 int result = default_;
292 getintfield(L, table, fieldname, result);
296 float getfloatfield_default(lua_State *L, int table,
297 const char *fieldname, float default_)
299 float result = default_;
300 getfloatfield(L, table, fieldname, result);
304 bool getboolfield_default(lua_State *L, int table,
305 const char *fieldname, bool default_)
307 bool result = default_;
308 getboolfield(L, table, fieldname, result);
312 void setintfield(lua_State *L, int table,
313 const char *fieldname, int value)
315 lua_pushinteger(L, value);
318 lua_setfield(L, table, fieldname);
321 void setfloatfield(lua_State *L, int table,
322 const char *fieldname, float value)
324 lua_pushnumber(L, value);
327 lua_setfield(L, table, fieldname);
330 void setboolfield(lua_State *L, int table,
331 const char *fieldname, bool value)
333 lua_pushboolean(L, value);
336 lua_setfield(L, table, fieldname);
340 /* minetest specific types */
341 MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
343 lua_getfield(L, index, "name");
344 const char *name = luaL_checkstring(L, -1);
347 lua_getfield(L, index, "param1");
351 param1 = lua_tonumber(L, -1);
354 lua_getfield(L, index, "param2");
358 param2 = lua_tonumber(L, -1);
360 return MapNode(ndef, name, param1, param2);
363 void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
366 lua_pushstring(L, ndef->get(n).name.c_str());
367 lua_setfield(L, -2, "name");
368 lua_pushnumber(L, n.getParam1());
369 lua_setfield(L, -2, "param1");
370 lua_pushnumber(L, n.getParam2());
371 lua_setfield(L, -2, "param2");