]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
Use LuaErrors in security check macros
[dragonfireclient.git] / src / script / cpp_api / s_node.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "cpp_api/s_node.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "nodedef.h"
25 #include "server.h"
26 #include "environment.h"
27 #include "util/pointedthing.h"
28
29
30 struct EnumString ScriptApiNode::es_DrawType[] =
31         {
32                 {NDT_NORMAL, "normal"},
33                 {NDT_AIRLIKE, "airlike"},
34                 {NDT_LIQUID, "liquid"},
35                 {NDT_FLOWINGLIQUID, "flowingliquid"},
36                 {NDT_GLASSLIKE, "glasslike"},
37                 {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
38                 {NDT_GLASSLIKE_FRAMED_OPTIONAL, "glasslike_framed_optional"},
39                 {NDT_ALLFACES, "allfaces"},
40                 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
41                 {NDT_TORCHLIKE, "torchlike"},
42                 {NDT_SIGNLIKE, "signlike"},
43                 {NDT_PLANTLIKE, "plantlike"},
44                 {NDT_FIRELIKE, "firelike"},
45                 {NDT_FENCELIKE, "fencelike"},
46                 {NDT_RAILLIKE, "raillike"},
47                 {NDT_NODEBOX, "nodebox"},
48                 {NDT_MESH, "mesh"},
49                 {0, NULL},
50         };
51
52 struct EnumString ScriptApiNode::es_ContentParamType2[] =
53         {
54                 {CPT2_NONE, "none"},
55                 {CPT2_FULL, "full"},
56                 {CPT2_FLOWINGLIQUID, "flowingliquid"},
57                 {CPT2_FACEDIR, "facedir"},
58                 {CPT2_WALLMOUNTED, "wallmounted"},
59                 {CPT2_LEVELED, "leveled"},
60                 {CPT2_DEGROTATE, "degrotate"},
61                 {0, NULL},
62         };
63
64 struct EnumString ScriptApiNode::es_LiquidType[] =
65         {
66                 {LIQUID_NONE, "none"},
67                 {LIQUID_FLOWING, "flowing"},
68                 {LIQUID_SOURCE, "source"},
69                 {0, NULL},
70         };
71
72 struct EnumString ScriptApiNode::es_ContentParamType[] =
73         {
74                 {CPT_NONE, "none"},
75                 {CPT_LIGHT, "light"},
76                 {0, NULL},
77         };
78
79 struct EnumString ScriptApiNode::es_NodeBoxType[] =
80         {
81                 {NODEBOX_REGULAR, "regular"},
82                 {NODEBOX_FIXED, "fixed"},
83                 {NODEBOX_WALLMOUNTED, "wallmounted"},
84                 {NODEBOX_LEVELED, "leveled"},
85                 {0, NULL},
86         };
87
88 ScriptApiNode::ScriptApiNode() {
89 }
90
91 ScriptApiNode::~ScriptApiNode() {
92 }
93
94 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
95                 ServerActiveObject *puncher, PointedThing pointed)
96 {
97         SCRIPTAPI_PRECHECKHEADER
98
99         int error_handler = PUSH_ERROR_HANDLER(L);
100
101         INodeDefManager *ndef = getServer()->ndef();
102
103         // Push callback function on stack
104         if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
105                 return false;
106
107         // Call function
108         push_v3s16(L, p);
109         pushnode(L, node, ndef);
110         objectrefGetOrCreate(L, puncher);
111         pushPointedThing(pointed);
112         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
113         lua_pop(L, 1);  // Pop error handler
114         return true;
115 }
116
117 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
118                 ServerActiveObject *digger)
119 {
120         SCRIPTAPI_PRECHECKHEADER
121
122         int error_handler = PUSH_ERROR_HANDLER(L);
123
124         INodeDefManager *ndef = getServer()->ndef();
125
126         // Push callback function on stack
127         if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
128                 return false;
129
130         // Call function
131         push_v3s16(L, p);
132         pushnode(L, node, ndef);
133         objectrefGetOrCreate(L, digger);
134         PCALL_RES(lua_pcall(L, 3, 0, error_handler));
135         lua_pop(L, 1);  // Pop error handler
136         return true;
137 }
138
139 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
140 {
141         SCRIPTAPI_PRECHECKHEADER
142
143         int error_handler = PUSH_ERROR_HANDLER(L);
144
145         INodeDefManager *ndef = getServer()->ndef();
146
147         // Push callback function on stack
148         if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
149                 return;
150
151         // Call function
152         push_v3s16(L, p);
153         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
154         lua_pop(L, 1);  // Pop error handler
155 }
156
157 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
158 {
159         SCRIPTAPI_PRECHECKHEADER
160
161         int error_handler = PUSH_ERROR_HANDLER(L);
162
163         INodeDefManager *ndef = getServer()->ndef();
164
165         // Push callback function on stack
166         if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
167                 return;
168
169         // Call function
170         push_v3s16(L, p);
171         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
172         lua_pop(L, 1);  // Pop error handler
173 }
174
175 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
176 {
177         SCRIPTAPI_PRECHECKHEADER
178
179         int error_handler = PUSH_ERROR_HANDLER(L);
180
181         INodeDefManager *ndef = getServer()->ndef();
182
183         // Push callback function on stack
184         if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
185                 return;
186
187         // Call function
188         push_v3s16(L, p);
189         pushnode(L, node, ndef);
190         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
191         lua_pop(L, 1);  // Pop error handler
192 }
193
194 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
195 {
196         SCRIPTAPI_PRECHECKHEADER
197
198         int error_handler = PUSH_ERROR_HANDLER(L);
199
200         INodeDefManager *ndef = getServer()->ndef();
201
202         // Push callback function on stack
203         if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
204                 return false;
205
206         // Call function
207         push_v3s16(L, p);
208         lua_pushnumber(L,dtime);
209         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
210         lua_remove(L, error_handler);
211         return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
212 }
213
214 void ScriptApiNode::node_on_receive_fields(v3s16 p,
215                 const std::string &formname,
216                 const StringMap &fields,
217                 ServerActiveObject *sender)
218 {
219         SCRIPTAPI_PRECHECKHEADER
220
221         int error_handler = PUSH_ERROR_HANDLER(L);
222
223         INodeDefManager *ndef = getServer()->ndef();
224
225         // If node doesn't exist, we don't know what callback to call
226         MapNode node = getEnv()->getMap().getNodeNoEx(p);
227         if (node.getContent() == CONTENT_IGNORE)
228                 return;
229
230         // Push callback function on stack
231         if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
232                 return;
233
234         // Call function
235         push_v3s16(L, p);                    // pos
236         lua_pushstring(L, formname.c_str()); // formname
237         lua_newtable(L);                     // fields
238         StringMap::const_iterator it;
239         for (it = fields.begin(); it != fields.end(); it++) {
240                 const std::string &name = it->first;
241                 const std::string &value = it->second;
242                 lua_pushstring(L, name.c_str());
243                 lua_pushlstring(L, value.c_str(), value.size());
244                 lua_settable(L, -3);
245         }
246         objectrefGetOrCreate(L, sender);        // player
247         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
248         lua_pop(L, 1);  // Pop error handler
249 }
250
251 void ScriptApiNode::node_falling_update(v3s16 p)
252 {
253         SCRIPTAPI_PRECHECKHEADER
254
255         int error_handler = PUSH_ERROR_HANDLER(L);
256
257         lua_getglobal(L, "nodeupdate");
258         push_v3s16(L, p);
259         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
260         lua_pop(L, 1);  // Pop error handler
261 }
262
263 void ScriptApiNode::node_falling_update_single(v3s16 p)
264 {
265         SCRIPTAPI_PRECHECKHEADER
266
267         int error_handler = PUSH_ERROR_HANDLER(L);
268
269         lua_getglobal(L, "nodeupdate_single");
270         push_v3s16(L, p);
271         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
272         lua_pop(L, 1);  // Pop error handler
273 }