]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
Add Lua bitop library (#9847)
[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                 {CPT2_COLORED_DEGROTATE, "colordegrotate"},
69                 {0, NULL},
70         };
71
72 struct EnumString ScriptApiNode::es_LiquidType[] =
73         {
74                 {LIQUID_NONE, "none"},
75                 {LIQUID_FLOWING, "flowing"},
76                 {LIQUID_SOURCE, "source"},
77                 {0, NULL},
78         };
79
80 struct EnumString ScriptApiNode::es_ContentParamType[] =
81         {
82                 {CPT_NONE, "none"},
83                 {CPT_LIGHT, "light"},
84                 {0, NULL},
85         };
86
87 struct EnumString ScriptApiNode::es_NodeBoxType[] =
88         {
89                 {NODEBOX_REGULAR, "regular"},
90                 {NODEBOX_FIXED, "fixed"},
91                 {NODEBOX_WALLMOUNTED, "wallmounted"},
92                 {NODEBOX_LEVELED, "leveled"},
93                 {NODEBOX_CONNECTED, "connected"},
94                 {0, NULL},
95         };
96
97 struct EnumString ScriptApiNode::es_TextureAlphaMode[] =
98         {
99                 {ALPHAMODE_OPAQUE, "opaque"},
100                 {ALPHAMODE_CLIP, "clip"},
101                 {ALPHAMODE_BLEND, "blend"},
102                 {0, NULL},
103         };
104
105 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
106                 ServerActiveObject *puncher, const PointedThing &pointed)
107 {
108         SCRIPTAPI_PRECHECKHEADER
109
110         int error_handler = PUSH_ERROR_HANDLER(L);
111
112         const NodeDefManager *ndef = getServer()->ndef();
113
114         // Push callback function on stack
115         if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch", &p))
116                 return false;
117
118         // Call function
119         push_v3s16(L, p);
120         pushnode(L, node, ndef);
121         objectrefGetOrCreate(L, puncher);
122         pushPointedThing(pointed);
123         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
124         lua_pop(L, 1);  // Pop error handler
125         return true;
126 }
127
128 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
129                 ServerActiveObject *digger)
130 {
131         SCRIPTAPI_PRECHECKHEADER
132
133         int error_handler = PUSH_ERROR_HANDLER(L);
134
135         const NodeDefManager *ndef = getServer()->ndef();
136
137         // Push callback function on stack
138         if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig", &p))
139                 return false;
140
141         // Call function
142         push_v3s16(L, p);
143         pushnode(L, node, ndef);
144         objectrefGetOrCreate(L, digger);
145         PCALL_RES(lua_pcall(L, 3, 1, error_handler));
146
147         // nil is treated as true for backwards compat
148         bool result = lua_isnil(L, -1) || lua_toboolean(L, -1);
149
150         lua_pop(L, 2);  // Pop error handler and result
151
152         return result;
153 }
154
155 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
156 {
157         SCRIPTAPI_PRECHECKHEADER
158
159         int error_handler = PUSH_ERROR_HANDLER(L);
160
161         const NodeDefManager *ndef = getServer()->ndef();
162
163         // Push callback function on stack
164         if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct", &p))
165                 return;
166
167         // Call function
168         push_v3s16(L, p);
169         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
170         lua_pop(L, 1);  // Pop error handler
171 }
172
173 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
174 {
175         SCRIPTAPI_PRECHECKHEADER
176
177         int error_handler = PUSH_ERROR_HANDLER(L);
178
179         const NodeDefManager *ndef = getServer()->ndef();
180
181         // Push callback function on stack
182         if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct", &p))
183                 return;
184
185         // Call function
186         push_v3s16(L, p);
187         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
188         lua_pop(L, 1);  // Pop error handler
189 }
190
191 bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
192 {
193         SCRIPTAPI_PRECHECKHEADER
194
195         int error_handler = PUSH_ERROR_HANDLER(L);
196
197         const NodeDefManager *ndef = getServer()->ndef();
198
199         // Push callback function on stack
200         if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood", &p))
201                 return false;
202
203         // Call function
204         push_v3s16(L, p);
205         pushnode(L, node, ndef);
206         pushnode(L, newnode, ndef);
207         PCALL_RES(lua_pcall(L, 3, 1, error_handler));
208         lua_remove(L, error_handler);
209         return readParam<bool>(L, -1, false);
210 }
211
212 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
213 {
214         SCRIPTAPI_PRECHECKHEADER
215
216         int error_handler = PUSH_ERROR_HANDLER(L);
217
218         const NodeDefManager *ndef = getServer()->ndef();
219
220         // Push callback function on stack
221         if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct", &p))
222                 return;
223
224         // Call function
225         push_v3s16(L, p);
226         pushnode(L, node, ndef);
227         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
228         lua_pop(L, 1);  // Pop error handler
229 }
230
231 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
232 {
233         SCRIPTAPI_PRECHECKHEADER
234
235         int error_handler = PUSH_ERROR_HANDLER(L);
236
237         const NodeDefManager *ndef = getServer()->ndef();
238
239         // Push callback function on stack
240         if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer", &p))
241                 return false;
242
243         // Call function
244         push_v3s16(L, p);
245         lua_pushnumber(L,dtime);
246         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
247         lua_remove(L, error_handler);
248         return readParam<bool>(L, -1, false);
249 }
250
251 void ScriptApiNode::node_on_receive_fields(v3s16 p,
252                 const std::string &formname,
253                 const StringMap &fields,
254                 ServerActiveObject *sender)
255 {
256         SCRIPTAPI_PRECHECKHEADER
257
258         int error_handler = PUSH_ERROR_HANDLER(L);
259
260         const NodeDefManager *ndef = getServer()->ndef();
261
262         // If node doesn't exist, we don't know what callback to call
263         MapNode node = getEnv()->getMap().getNode(p);
264         if (node.getContent() == CONTENT_IGNORE)
265                 return;
266
267         // Push callback function on stack
268         if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields", &p))
269                 return;
270
271         // Call function
272         push_v3s16(L, p);                    // pos
273         lua_pushstring(L, formname.c_str()); // formname
274         lua_newtable(L);                     // fields
275         StringMap::const_iterator it;
276         for (it = fields.begin(); it != fields.end(); ++it) {
277                 const std::string &name = it->first;
278                 const std::string &value = it->second;
279                 lua_pushstring(L, name.c_str());
280                 lua_pushlstring(L, value.c_str(), value.size());
281                 lua_settable(L, -3);
282         }
283         objectrefGetOrCreate(L, sender);        // player
284         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
285         lua_pop(L, 1);  // Pop error handler
286 }