3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
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"
26 #include "environment.h"
27 #include "util/pointedthing.h"
30 // Should be ordered exactly like enum NodeDrawType in nodedef.h
31 struct EnumString ScriptApiNode::es_DrawType[] =
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"},
53 struct EnumString ScriptApiNode::es_ContentParamType2[] =
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"},
70 struct EnumString ScriptApiNode::es_LiquidType[] =
72 {LIQUID_NONE, "none"},
73 {LIQUID_FLOWING, "flowing"},
74 {LIQUID_SOURCE, "source"},
78 struct EnumString ScriptApiNode::es_ContentParamType[] =
85 struct EnumString ScriptApiNode::es_NodeBoxType[] =
87 {NODEBOX_REGULAR, "regular"},
88 {NODEBOX_FIXED, "fixed"},
89 {NODEBOX_WALLMOUNTED, "wallmounted"},
90 {NODEBOX_LEVELED, "leveled"},
91 {NODEBOX_CONNECTED, "connected"},
95 ScriptApiNode::ScriptApiNode() {
98 ScriptApiNode::~ScriptApiNode() {
101 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
102 ServerActiveObject *puncher, PointedThing pointed)
104 SCRIPTAPI_PRECHECKHEADER
106 int error_handler = PUSH_ERROR_HANDLER(L);
108 INodeDefManager *ndef = getServer()->ndef();
110 // Push callback function on stack
111 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
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
124 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
125 ServerActiveObject *digger)
127 SCRIPTAPI_PRECHECKHEADER
129 int error_handler = PUSH_ERROR_HANDLER(L);
131 INodeDefManager *ndef = getServer()->ndef();
133 // Push callback function on stack
134 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
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
146 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
148 SCRIPTAPI_PRECHECKHEADER
150 int error_handler = PUSH_ERROR_HANDLER(L);
152 INodeDefManager *ndef = getServer()->ndef();
154 // Push callback function on stack
155 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
160 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
161 lua_pop(L, 1); // Pop error handler
164 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
166 SCRIPTAPI_PRECHECKHEADER
168 int error_handler = PUSH_ERROR_HANDLER(L);
170 INodeDefManager *ndef = getServer()->ndef();
172 // Push callback function on stack
173 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
178 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
179 lua_pop(L, 1); // Pop error handler
182 bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
184 SCRIPTAPI_PRECHECKHEADER
186 int error_handler = PUSH_ERROR_HANDLER(L);
188 INodeDefManager *ndef = getServer()->ndef();
190 // Push callback function on stack
191 if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood"))
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;
203 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
205 SCRIPTAPI_PRECHECKHEADER
207 int error_handler = PUSH_ERROR_HANDLER(L);
209 INodeDefManager *ndef = getServer()->ndef();
211 // Push callback function on stack
212 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
217 pushnode(L, node, ndef);
218 PCALL_RES(lua_pcall(L, 2, 0, error_handler));
219 lua_pop(L, 1); // Pop error handler
222 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
224 SCRIPTAPI_PRECHECKHEADER
226 int error_handler = PUSH_ERROR_HANDLER(L);
228 INodeDefManager *ndef = getServer()->ndef();
230 // Push callback function on stack
231 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
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;
242 void ScriptApiNode::node_on_receive_fields(v3s16 p,
243 const std::string &formname,
244 const StringMap &fields,
245 ServerActiveObject *sender)
247 SCRIPTAPI_PRECHECKHEADER
249 int error_handler = PUSH_ERROR_HANDLER(L);
251 INodeDefManager *ndef = getServer()->ndef();
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)
258 // Push callback function on stack
259 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
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());
274 objectrefGetOrCreate(L, sender); // player
275 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
276 lua_pop(L, 1); // Pop error handler
279 void ScriptApiNode::node_falling_update(v3s16 p)
281 SCRIPTAPI_PRECHECKHEADER
283 int error_handler = PUSH_ERROR_HANDLER(L);
285 lua_getglobal(L, "nodeupdate");
287 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
288 lua_pop(L, 1); // Pop error handler
291 void ScriptApiNode::node_falling_update_single(v3s16 p)
293 SCRIPTAPI_PRECHECKHEADER
295 int error_handler = PUSH_ERROR_HANDLER(L);
297 lua_getglobal(L, "nodeupdate_single");
299 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
300 lua_pop(L, 1); // Pop error handler