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