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.
25 #include "util/numeric.h"
26 #include "common/c_converter.h"
27 #include "constants.h"
29 void push_v3f(lua_State *L, v3f p)
32 lua_pushnumber(L, p.X);
33 lua_setfield(L, -2, "x");
34 lua_pushnumber(L, p.Y);
35 lua_setfield(L, -2, "y");
36 lua_pushnumber(L, p.Z);
37 lua_setfield(L, -2, "z");
40 void push_v2f(lua_State *L, v2f p)
43 lua_pushnumber(L, p.X);
44 lua_setfield(L, -2, "x");
45 lua_pushnumber(L, p.Y);
46 lua_setfield(L, -2, "y");
49 v2s16 read_v2s16(lua_State *L, int index)
52 luaL_checktype(L, index, LUA_TTABLE);
53 lua_getfield(L, index, "x");
54 p.X = lua_tonumber(L, -1);
56 lua_getfield(L, index, "y");
57 p.Y = lua_tonumber(L, -1);
62 v2s32 read_v2s32(lua_State *L, int index)
65 luaL_checktype(L, index, LUA_TTABLE);
66 lua_getfield(L, index, "x");
67 p.X = lua_tonumber(L, -1);
69 lua_getfield(L, index, "y");
70 p.Y = lua_tonumber(L, -1);
75 v2f read_v2f(lua_State *L, int index)
78 luaL_checktype(L, index, LUA_TTABLE);
79 lua_getfield(L, index, "x");
80 p.X = lua_tonumber(L, -1);
82 lua_getfield(L, index, "y");
83 p.Y = lua_tonumber(L, -1);
88 v3f read_v3f(lua_State *L, int index)
91 luaL_checktype(L, index, LUA_TTABLE);
92 lua_getfield(L, index, "x");
93 pos.X = lua_tonumber(L, -1);
95 lua_getfield(L, index, "y");
96 pos.Y = lua_tonumber(L, -1);
98 lua_getfield(L, index, "z");
99 pos.Z = lua_tonumber(L, -1);
104 v3f check_v3f(lua_State *L, int index)
107 luaL_checktype(L, index, LUA_TTABLE);
108 lua_getfield(L, index, "x");
109 pos.X = luaL_checknumber(L, -1);
111 lua_getfield(L, index, "y");
112 pos.Y = luaL_checknumber(L, -1);
114 lua_getfield(L, index, "z");
115 pos.Z = luaL_checknumber(L, -1);
120 void pushFloatPos(lua_State *L, v3f p)
126 v3f checkFloatPos(lua_State *L, int index)
128 return check_v3f(L, index) * BS;
131 void push_v3s16(lua_State *L, v3s16 p)
134 lua_pushnumber(L, p.X);
135 lua_setfield(L, -2, "x");
136 lua_pushnumber(L, p.Y);
137 lua_setfield(L, -2, "y");
138 lua_pushnumber(L, p.Z);
139 lua_setfield(L, -2, "z");
142 v3s16 read_v3s16(lua_State *L, int index)
144 // Correct rounding at <0
145 v3f pf = read_v3f(L, index);
146 return floatToInt(pf, 1.0);
149 v3s16 check_v3s16(lua_State *L, int index)
151 // Correct rounding at <0
152 v3f pf = check_v3f(L, index);
153 return floatToInt(pf, 1.0);
156 video::SColor readARGB8(lua_State *L, int index)
158 video::SColor color(0);
159 luaL_checktype(L, index, LUA_TTABLE);
160 lua_getfield(L, index, "a");
161 if(lua_isnumber(L, -1))
162 color.setAlpha(lua_tonumber(L, -1));
164 lua_getfield(L, index, "r");
165 color.setRed(lua_tonumber(L, -1));
167 lua_getfield(L, index, "g");
168 color.setGreen(lua_tonumber(L, -1));
170 lua_getfield(L, index, "b");
171 color.setBlue(lua_tonumber(L, -1));
176 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
179 if(lua_istable(L, index)){
180 lua_rawgeti(L, index, 1);
181 box.MinEdge.X = lua_tonumber(L, -1) * scale;
183 lua_rawgeti(L, index, 2);
184 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
186 lua_rawgeti(L, index, 3);
187 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
189 lua_rawgeti(L, index, 4);
190 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
192 lua_rawgeti(L, index, 5);
193 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
195 lua_rawgeti(L, index, 6);
196 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
202 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
204 std::vector<aabb3f> boxes;
205 if(lua_istable(L, index)){
206 int n = lua_objlen(L, index);
207 // Check if it's a single box or a list of boxes
208 bool possibly_single_box = (n == 6);
209 for(int i = 1; i <= n && possibly_single_box; i++){
210 lua_rawgeti(L, index, i);
211 if(!lua_isnumber(L, -1))
212 possibly_single_box = false;
215 if(possibly_single_box){
217 boxes.push_back(read_aabb3f(L, index, scale));
219 // Read a list of boxes
220 for(int i = 1; i <= n; i++){
221 lua_rawgeti(L, index, i);
222 boxes.push_back(read_aabb3f(L, -1, scale));
230 size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result)
233 index = lua_gettop(L) + 1 + index;
235 size_t num_strings = 0;
237 if (lua_istable(L, index)) {
239 while (lua_next(L, index)) {
240 if (lua_isstring(L, -1)) {
241 result->push_back(lua_tostring(L, -1));
246 } else if (lua_isstring(L, index)) {
247 result->push_back(lua_tostring(L, index));
258 bool getstringfield(lua_State *L, int table,
259 const char *fieldname, std::string &result)
261 lua_getfield(L, table, fieldname);
263 if(lua_isstring(L, -1)){
265 const char *ptr = lua_tolstring(L, -1, &len);
267 result.assign(ptr, len);
275 bool getintfield(lua_State *L, int table,
276 const char *fieldname, int &result)
278 lua_getfield(L, table, fieldname);
280 if(lua_isnumber(L, -1)){
281 result = lua_tonumber(L, -1);
288 bool getfloatfield(lua_State *L, int table,
289 const char *fieldname, float &result)
291 lua_getfield(L, table, fieldname);
293 if(lua_isnumber(L, -1)){
294 result = lua_tonumber(L, -1);
301 bool getboolfield(lua_State *L, int table,
302 const char *fieldname, bool &result)
304 lua_getfield(L, table, fieldname);
306 if(lua_isboolean(L, -1)){
307 result = lua_toboolean(L, -1);
314 size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
315 std::vector<std::string> *result)
317 lua_getfield(L, table, fieldname);
319 size_t num_strings_read = read_stringlist(L, -1, result);
322 return num_strings_read;
325 std::string checkstringfield(lua_State *L, int table,
326 const char *fieldname)
328 lua_getfield(L, table, fieldname);
329 std::string s = luaL_checkstring(L, -1);
334 std::string getstringfield_default(lua_State *L, int table,
335 const char *fieldname, const std::string &default_)
337 std::string result = default_;
338 getstringfield(L, table, fieldname, result);
342 int getintfield_default(lua_State *L, int table,
343 const char *fieldname, int default_)
345 int result = default_;
346 getintfield(L, table, fieldname, result);
350 float getfloatfield_default(lua_State *L, int table,
351 const char *fieldname, float default_)
353 float result = default_;
354 getfloatfield(L, table, fieldname, result);
358 bool getboolfield_default(lua_State *L, int table,
359 const char *fieldname, bool default_)
361 bool result = default_;
362 getboolfield(L, table, fieldname, result);
366 void setintfield(lua_State *L, int table,
367 const char *fieldname, int value)
369 lua_pushinteger(L, value);
372 lua_setfield(L, table, fieldname);
375 void setfloatfield(lua_State *L, int table,
376 const char *fieldname, float value)
378 lua_pushnumber(L, value);
381 lua_setfield(L, table, fieldname);
384 void setboolfield(lua_State *L, int table,
385 const char *fieldname, bool value)
387 lua_pushboolean(L, value);
390 lua_setfield(L, table, fieldname);