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));
234 bool getstringfield(lua_State *L, int table,
235 const char *fieldname, std::string &result)
237 lua_getfield(L, table, fieldname);
239 if(lua_isstring(L, -1)){
241 const char *ptr = lua_tolstring(L, -1, &len);
243 result.assign(ptr, len);
251 bool getintfield(lua_State *L, int table,
252 const char *fieldname, int &result)
254 lua_getfield(L, table, fieldname);
256 if(lua_isnumber(L, -1)){
257 result = lua_tonumber(L, -1);
264 bool getfloatfield(lua_State *L, int table,
265 const char *fieldname, float &result)
267 lua_getfield(L, table, fieldname);
269 if(lua_isnumber(L, -1)){
270 result = lua_tonumber(L, -1);
277 bool getboolfield(lua_State *L, int table,
278 const char *fieldname, bool &result)
280 lua_getfield(L, table, fieldname);
282 if(lua_isboolean(L, -1)){
283 result = lua_toboolean(L, -1);
290 std::string checkstringfield(lua_State *L, int table,
291 const char *fieldname)
293 lua_getfield(L, table, fieldname);
294 std::string s = luaL_checkstring(L, -1);
299 std::string getstringfield_default(lua_State *L, int table,
300 const char *fieldname, const std::string &default_)
302 std::string result = default_;
303 getstringfield(L, table, fieldname, result);
307 int getintfield_default(lua_State *L, int table,
308 const char *fieldname, int default_)
310 int result = default_;
311 getintfield(L, table, fieldname, result);
315 float getfloatfield_default(lua_State *L, int table,
316 const char *fieldname, float default_)
318 float result = default_;
319 getfloatfield(L, table, fieldname, result);
323 bool getboolfield_default(lua_State *L, int table,
324 const char *fieldname, bool default_)
326 bool result = default_;
327 getboolfield(L, table, fieldname, result);
331 void setintfield(lua_State *L, int table,
332 const char *fieldname, int value)
334 lua_pushinteger(L, value);
337 lua_setfield(L, table, fieldname);
340 void setfloatfield(lua_State *L, int table,
341 const char *fieldname, float value)
343 lua_pushnumber(L, value);
346 lua_setfield(L, table, fieldname);
349 void setboolfield(lua_State *L, int table,
350 const char *fieldname, bool value)
352 lua_pushboolean(L, value);
355 lua_setfield(L, table, fieldname);