]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
Order es_DrawType exactly like enum NodeDrawType in nodedef.h (#5946)
[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                 {0, NULL},
51         };
52
53 struct EnumString ScriptApiNode::es_ContentParamType2[] =
54         {
55                 {CPT2_NONE, "none"},
56                 {CPT2_FULL, "full"},
57                 {CPT2_FLOWINGLIQUID, "flowingliquid"},
58                 {CPT2_FACEDIR, "facedir"},
59                 {CPT2_WALLMOUNTED, "wallmounted"},
60                 {CPT2_LEVELED, "leveled"},
61                 {CPT2_DEGROTATE, "degrotate"},
62                 {CPT2_MESHOPTIONS, "meshoptions"},
63                 {CPT2_COLOR, "color"},
64                 {CPT2_COLORED_FACEDIR, "colorfacedir"},
65                 {CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
66                 {CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
67                 {0, NULL},
68         };
69
70 struct EnumString ScriptApiNode::es_LiquidType[] =
71         {
72                 {LIQUID_NONE, "none"},
73                 {LIQUID_FLOWING, "flowing"},
74                 {LIQUID_SOURCE, "source"},
75                 {0, NULL},
76         };
77
78 struct EnumString ScriptApiNode::es_ContentParamType[] =
79         {
80                 {CPT_NONE, "none"},
81                 {CPT_LIGHT, "light"},
82                 {0, NULL},
83         };
84
85 struct EnumString ScriptApiNode::es_NodeBoxType[] =
86         {
87                 {NODEBOX_REGULAR, "regular"},
88                 {NODEBOX_FIXED, "fixed"},
89                 {NODEBOX_WALLMOUNTED, "wallmounted"},
90                 {NODEBOX_LEVELED, "leveled"},
91                 {NODEBOX_CONNECTED, "connected"},
92                 {0, NULL},
93         };
94
95 ScriptApiNode::ScriptApiNode() {
96 }
97
98 ScriptApiNode::~ScriptApiNode() {
99 }
100
101 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
102                 ServerActiveObject *puncher, PointedThing pointed)
103 {
104         SCRIPTAPI_PRECHECKHEADER
105
106         int error_handler = PUSH_ERROR_HANDLER(L);
107
108         INodeDefManager *ndef = getServer()->ndef();
109
110         // Push callback function on stack
111         if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
112                 return false;
113
114         // Call function
115         push_v3s16(L, p);
116         pushnode(L, node, ndef);
117         objectrefGetOrCreate(L, puncher);
118         pushPointedThing(pointed);
119         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
120         lua_pop(L, 1);  // Pop error handler
121         return true;
122 }
123
124 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
125                 ServerActiveObject *digger)
126 {
127         SCRIPTAPI_PRECHECKHEADER
128
129         int error_handler = PUSH_ERROR_HANDLER(L);
130
131         INodeDefManager *ndef = getServer()->ndef();
132
133         // Push callback function on stack
134         if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
135                 return false;
136
137         // Call function
138         push_v3s16(L, p);
139         pushnode(L, node, ndef);
140         objectrefGetOrCreate(L, digger);
141         PCALL_RES(lua_pcall(L, 3, 0, error_handler));
142         lua_pop(L, 1);  // Pop error handler
143         return true;
144 }
145
146 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
147 {
148         SCRIPTAPI_PRECHECKHEADER
149
150         int error_handler = PUSH_ERROR_HANDLER(L);
151
152         INodeDefManager *ndef = getServer()->ndef();
153
154         // Push callback function on stack
155         if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
156                 return;
157
158         // Call function
159         push_v3s16(L, p);
160         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
161         lua_pop(L, 1);  // Pop error handler
162 }
163
164 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
165 {
166         SCRIPTAPI_PRECHECKHEADER
167
168         int error_handler = PUSH_ERROR_HANDLER(L);
169
170         INodeDefManager *ndef = getServer()->ndef();
171
172         // Push callback function on stack
173         if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
174                 return;
175
176         // Call function
177         push_v3s16(L, p);
178         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
179         lua_pop(L, 1);  // Pop error handler
180 }
181
182 bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
183 {
184         SCRIPTAPI_PRECHECKHEADER
185
186         int error_handler = PUSH_ERROR_HANDLER(L);
187
188         INodeDefManager *ndef = getServer()->ndef();
189
190         // Push callback function on stack
191         if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood"))
192                 return false;
193
194         // Call function
195         push_v3s16(L, p);
196         pushnode(L, node, ndef);
197         pushnode(L, newnode, ndef);
198         PCALL_RES(lua_pcall(L, 3, 1, error_handler));
199         lua_remove(L, error_handler);
200         return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
201 }
202
203 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
204 {
205         SCRIPTAPI_PRECHECKHEADER
206
207         int error_handler = PUSH_ERROR_HANDLER(L);
208
209         INodeDefManager *ndef = getServer()->ndef();
210
211         // Push callback function on stack
212         if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
213                 return;
214
215         // Call function
216         push_v3s16(L, p);
217         pushnode(L, node, ndef);
218         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
219         lua_pop(L, 1);  // Pop error handler
220 }
221
222 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
223 {
224         SCRIPTAPI_PRECHECKHEADER
225
226         int error_handler = PUSH_ERROR_HANDLER(L);
227
228         INodeDefManager *ndef = getServer()->ndef();
229
230         // Push callback function on stack
231         if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
232                 return false;
233
234         // Call function
235         push_v3s16(L, p);
236         lua_pushnumber(L,dtime);
237         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
238         lua_remove(L, error_handler);
239         return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
240 }
241
242 void ScriptApiNode::node_on_receive_fields(v3s16 p,
243                 const std::string &formname,
244                 const StringMap &fields,
245                 ServerActiveObject *sender)
246 {
247         SCRIPTAPI_PRECHECKHEADER
248
249         int error_handler = PUSH_ERROR_HANDLER(L);
250
251         INodeDefManager *ndef = getServer()->ndef();
252
253         // If node doesn't exist, we don't know what callback to call
254         MapNode node = getEnv()->getMap().getNodeNoEx(p);
255         if (node.getContent() == CONTENT_IGNORE)
256                 return;
257
258         // Push callback function on stack
259         if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
260                 return;
261
262         // Call function
263         push_v3s16(L, p);                    // pos
264         lua_pushstring(L, formname.c_str()); // formname
265         lua_newtable(L);                     // fields
266         StringMap::const_iterator it;
267         for (it = fields.begin(); it != fields.end(); ++it) {
268                 const std::string &name = it->first;
269                 const std::string &value = it->second;
270                 lua_pushstring(L, name.c_str());
271                 lua_pushlstring(L, value.c_str(), value.size());
272                 lua_settable(L, -3);
273         }
274         objectrefGetOrCreate(L, sender);        // player
275         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
276         lua_pop(L, 1);  // Pop error handler
277 }
278
279 void ScriptApiNode::node_falling_update(v3s16 p)
280 {
281         SCRIPTAPI_PRECHECKHEADER
282
283         int error_handler = PUSH_ERROR_HANDLER(L);
284
285         lua_getglobal(L, "nodeupdate");
286         push_v3s16(L, p);
287         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
288         lua_pop(L, 1);  // Pop error handler
289 }
290
291 void ScriptApiNode::node_falling_update_single(v3s16 p)
292 {
293         SCRIPTAPI_PRECHECKHEADER
294
295         int error_handler = PUSH_ERROR_HANDLER(L);
296
297         lua_getglobal(L, "nodeupdate_single");
298         push_v3s16(L, p);
299         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
300         lua_pop(L, 1);  // Pop error handler
301 }