]> git.lizzy.rs Git - minetest.git/blob - src/script/common/helper.cpp
Animated particlespawners and more (#11545)
[minetest.git] / src / script / common / helper.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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 "helper.h"
21 #include <cmath>
22 #include <sstream>
23 #include <irr_v2d.h>
24 #include <irr_v3d.h>
25 #include "c_types.h"
26 #include "c_internal.h"
27
28 // imported from c_converter.cpp with pure C++ style
29 static inline void check_lua_type(lua_State *L, int index, const char *name, int type)
30 {
31         int t = lua_type(L, index);
32         if (t != type) {
33                 std::string traceback = script_get_backtrace(L);
34                 throw LuaError(std::string("Invalid ") + (name) + " (expected " +
35                                 lua_typename(L, (type)) + " got " + lua_typename(L, t) +
36                                 ").\n" + traceback);
37         }
38 }
39
40 // imported from c_converter.cpp
41 #define CHECK_POS_COORD(name)                                                            \
42         check_lua_type(L, -1, "position coordinate '" name "'", LUA_TNUMBER)
43 #define CHECK_POS_TAB(index) check_lua_type(L, index, "position", LUA_TTABLE)
44
45 bool LuaHelper::isNaN(lua_State *L, int idx)
46 {
47         return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
48 }
49
50 /*
51  * Read template functions
52  */
53 template <>
54 bool LuaHelper::readParam(lua_State *L, int index)
55 {
56         return lua_toboolean(L, index) != 0;
57 }
58
59 template <>
60 s16 LuaHelper::readParam(lua_State *L, int index)
61 {
62         return lua_tonumber(L, index);
63 }
64
65 template <>
66 int LuaHelper::readParam(lua_State *L, int index)
67 {
68         return luaL_checkint(L, index);
69 }
70
71 template <>
72 float LuaHelper::readParam(lua_State *L, int index)
73 {
74         if (isNaN(L, index))
75                 throw LuaError("NaN value is not allowed.");
76
77         return (float)luaL_checknumber(L, index);
78 }
79
80 template <>
81 v2s16 LuaHelper::readParam(lua_State *L, int index)
82 {
83         v2s16 p;
84         CHECK_POS_TAB(index);
85         lua_getfield(L, index, "x");
86         CHECK_POS_COORD("x");
87         p.X = readParam<s16>(L, -1);
88         lua_pop(L, 1);
89         lua_getfield(L, index, "y");
90         CHECK_POS_COORD("y");
91         p.Y = readParam<s16>(L, -1);
92         lua_pop(L, 1);
93         return p;
94 }
95
96 template <>
97 v2f LuaHelper::readParam(lua_State *L, int index)
98 {
99         v2f p;
100         CHECK_POS_TAB(index);
101         lua_getfield(L, index, "x");
102         CHECK_POS_COORD("x");
103         p.X = readParam<float>(L, -1);
104         lua_pop(L, 1);
105         lua_getfield(L, index, "y");
106         CHECK_POS_COORD("y");
107         p.Y = readParam<float>(L, -1);
108         lua_pop(L, 1);
109         return p;
110 }
111
112 template <>
113 v3f LuaHelper::readParam(lua_State *L, int index)
114 {
115         v3f p;
116         CHECK_POS_TAB(index);
117         lua_getfield(L, index, "x");
118         CHECK_POS_COORD("x");
119         p.X = readParam<float>(L, -1);
120         lua_pop(L, 1);
121         lua_getfield(L, index, "y");
122         CHECK_POS_COORD("y");
123         p.Y = readParam<float>(L, -1);
124         lua_pop(L, 1);
125         lua_getfield(L, index, "z");
126         CHECK_POS_COORD("z");
127         p.Z = readParam<float>(L, -1);
128         lua_pop(L, 1);
129         return p;
130 }
131
132 template <>
133 std::string LuaHelper::readParam(lua_State *L, int index)
134 {
135         size_t length;
136         std::string result;
137         const char *str = luaL_checklstring(L, index, &length);
138         result.assign(str, length);
139         return result;
140 }