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