]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
Add on_deactivate callback for luaentities (#10723)
[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 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
97                 ServerActiveObject *puncher, const PointedThing &pointed)
98 {
99         SCRIPTAPI_PRECHECKHEADER
100
101         int error_handler = PUSH_ERROR_HANDLER(L);
102
103         const NodeDefManager *ndef = getServer()->ndef();
104
105         // Push callback function on stack
106         if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch", &p))
107                 return false;
108
109         // Call function
110         push_v3s16(L, p);
111         pushnode(L, node, ndef);
112         objectrefGetOrCreate(L, puncher);
113         pushPointedThing(pointed);
114         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
115         lua_pop(L, 1);  // Pop error handler
116         return true;
117 }
118
119 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
120                 ServerActiveObject *digger)
121 {
122         SCRIPTAPI_PRECHECKHEADER
123
124         int error_handler = PUSH_ERROR_HANDLER(L);
125
126         const NodeDefManager *ndef = getServer()->ndef();
127
128         // Push callback function on stack
129         if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig", &p))
130                 return false;
131
132         // Call function
133         push_v3s16(L, p);
134         pushnode(L, node, ndef);
135         objectrefGetOrCreate(L, digger);
136         PCALL_RES(lua_pcall(L, 3, 0, error_handler));
137         lua_pop(L, 1);  // Pop error handler
138         return true;
139 }
140
141 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
142 {
143         SCRIPTAPI_PRECHECKHEADER
144
145         int error_handler = PUSH_ERROR_HANDLER(L);
146
147         const NodeDefManager *ndef = getServer()->ndef();
148
149         // Push callback function on stack
150         if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct", &p))
151                 return;
152
153         // Call function
154         push_v3s16(L, p);
155         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
156         lua_pop(L, 1);  // Pop error handler
157 }
158
159 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
160 {
161         SCRIPTAPI_PRECHECKHEADER
162
163         int error_handler = PUSH_ERROR_HANDLER(L);
164
165         const NodeDefManager *ndef = getServer()->ndef();
166
167         // Push callback function on stack
168         if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct", &p))
169                 return;
170
171         // Call function
172         push_v3s16(L, p);
173         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
174         lua_pop(L, 1);  // Pop error handler
175 }
176
177 bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
178 {
179         SCRIPTAPI_PRECHECKHEADER
180
181         int error_handler = PUSH_ERROR_HANDLER(L);
182
183         const NodeDefManager *ndef = getServer()->ndef();
184
185         // Push callback function on stack
186         if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood", &p))
187                 return false;
188
189         // Call function
190         push_v3s16(L, p);
191         pushnode(L, node, ndef);
192         pushnode(L, newnode, ndef);
193         PCALL_RES(lua_pcall(L, 3, 1, error_handler));
194         lua_remove(L, error_handler);
195         return readParam<bool>(L, -1, false);
196 }
197
198 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
199 {
200         SCRIPTAPI_PRECHECKHEADER
201
202         int error_handler = PUSH_ERROR_HANDLER(L);
203
204         const NodeDefManager *ndef = getServer()->ndef();
205
206         // Push callback function on stack
207         if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct", &p))
208                 return;
209
210         // Call function
211         push_v3s16(L, p);
212         pushnode(L, node, ndef);
213         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
214         lua_pop(L, 1);  // Pop error handler
215 }
216
217 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
218 {
219         SCRIPTAPI_PRECHECKHEADER
220
221         int error_handler = PUSH_ERROR_HANDLER(L);
222
223         const NodeDefManager *ndef = getServer()->ndef();
224
225         // Push callback function on stack
226         if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer", &p))
227                 return false;
228
229         // Call function
230         push_v3s16(L, p);
231         lua_pushnumber(L,dtime);
232         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
233         lua_remove(L, error_handler);
234         return readParam<bool>(L, -1, false);
235 }
236
237 void ScriptApiNode::node_on_receive_fields(v3s16 p,
238                 const std::string &formname,
239                 const StringMap &fields,
240                 ServerActiveObject *sender)
241 {
242         SCRIPTAPI_PRECHECKHEADER
243
244         int error_handler = PUSH_ERROR_HANDLER(L);
245
246         const NodeDefManager *ndef = getServer()->ndef();
247
248         // If node doesn't exist, we don't know what callback to call
249         MapNode node = getEnv()->getMap().getNode(p);
250         if (node.getContent() == CONTENT_IGNORE)
251                 return;
252
253         // Push callback function on stack
254         if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields", &p))
255                 return;
256
257         // Call function
258         push_v3s16(L, p);                    // pos
259         lua_pushstring(L, formname.c_str()); // formname
260         lua_newtable(L);                     // fields
261         StringMap::const_iterator it;
262         for (it = fields.begin(); it != fields.end(); ++it) {
263                 const std::string &name = it->first;
264                 const std::string &value = it->second;
265                 lua_pushstring(L, name.c_str());
266                 lua_pushlstring(L, value.c_str(), value.size());
267                 lua_settable(L, -3);
268         }
269         objectrefGetOrCreate(L, sender);        // player
270         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
271         lua_pop(L, 1);  // Pop error handler
272 }