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 struct EnumString ScriptApiNode::es_DrawType[] =
32 {NDT_NORMAL, "normal"},
33 {NDT_AIRLIKE, "airlike"},
34 {NDT_LIQUID, "liquid"},
35 {NDT_FLOWINGLIQUID, "flowingliquid"},
36 {NDT_GLASSLIKE, "glasslike"},
37 {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
38 {NDT_GLASSLIKE_FRAMED_OPTIONAL, "glasslike_framed_optional"},
39 {NDT_ALLFACES, "allfaces"},
40 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
41 {NDT_TORCHLIKE, "torchlike"},
42 {NDT_SIGNLIKE, "signlike"},
43 {NDT_PLANTLIKE, "plantlike"},
44 {NDT_FIRELIKE, "firelike"},
45 {NDT_FENCELIKE, "fencelike"},
46 {NDT_RAILLIKE, "raillike"},
47 {NDT_NODEBOX, "nodebox"},
52 struct EnumString ScriptApiNode::es_ContentParamType2[] =
56 {CPT2_FLOWINGLIQUID, "flowingliquid"},
57 {CPT2_FACEDIR, "facedir"},
58 {CPT2_WALLMOUNTED, "wallmounted"},
59 {CPT2_LEVELED, "leveled"},
60 {CPT2_DEGROTATE, "degrotate"},
61 {CPT2_MESHOPTIONS, "meshoptions"},
62 {CPT2_COLOR, "color"},
63 {CPT2_COLORED_FACEDIR, "colorfacedir"},
64 {CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
65 {CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
69 struct EnumString ScriptApiNode::es_LiquidType[] =
71 {LIQUID_NONE, "none"},
72 {LIQUID_FLOWING, "flowing"},
73 {LIQUID_SOURCE, "source"},
77 struct EnumString ScriptApiNode::es_ContentParamType[] =
84 struct EnumString ScriptApiNode::es_NodeBoxType[] =
86 {NODEBOX_REGULAR, "regular"},
87 {NODEBOX_FIXED, "fixed"},
88 {NODEBOX_WALLMOUNTED, "wallmounted"},
89 {NODEBOX_LEVELED, "leveled"},
90 {NODEBOX_CONNECTED, "connected"},
94 ScriptApiNode::ScriptApiNode() {
97 ScriptApiNode::~ScriptApiNode() {
100 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
101 ServerActiveObject *puncher, PointedThing pointed)
103 SCRIPTAPI_PRECHECKHEADER
105 int error_handler = PUSH_ERROR_HANDLER(L);
107 INodeDefManager *ndef = getServer()->ndef();
109 // Push callback function on stack
110 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
115 pushnode(L, node, ndef);
116 objectrefGetOrCreate(L, puncher);
117 pushPointedThing(pointed);
118 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
119 lua_pop(L, 1); // Pop error handler
123 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
124 ServerActiveObject *digger)
126 SCRIPTAPI_PRECHECKHEADER
128 int error_handler = PUSH_ERROR_HANDLER(L);
130 INodeDefManager *ndef = getServer()->ndef();
132 // Push callback function on stack
133 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
138 pushnode(L, node, ndef);
139 objectrefGetOrCreate(L, digger);
140 PCALL_RES(lua_pcall(L, 3, 0, error_handler));
141 lua_pop(L, 1); // Pop error handler
145 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
147 SCRIPTAPI_PRECHECKHEADER
149 int error_handler = PUSH_ERROR_HANDLER(L);
151 INodeDefManager *ndef = getServer()->ndef();
153 // Push callback function on stack
154 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
159 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
160 lua_pop(L, 1); // Pop error handler
163 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
165 SCRIPTAPI_PRECHECKHEADER
167 int error_handler = PUSH_ERROR_HANDLER(L);
169 INodeDefManager *ndef = getServer()->ndef();
171 // Push callback function on stack
172 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
177 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
178 lua_pop(L, 1); // Pop error handler
181 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
183 SCRIPTAPI_PRECHECKHEADER
185 int error_handler = PUSH_ERROR_HANDLER(L);
187 INodeDefManager *ndef = getServer()->ndef();
189 // Push callback function on stack
190 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
195 pushnode(L, node, ndef);
196 PCALL_RES(lua_pcall(L, 2, 0, error_handler));
197 lua_pop(L, 1); // Pop error handler
200 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
202 SCRIPTAPI_PRECHECKHEADER
204 int error_handler = PUSH_ERROR_HANDLER(L);
206 INodeDefManager *ndef = getServer()->ndef();
208 // Push callback function on stack
209 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
214 lua_pushnumber(L,dtime);
215 PCALL_RES(lua_pcall(L, 2, 1, error_handler));
216 lua_remove(L, error_handler);
217 return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
220 void ScriptApiNode::node_on_receive_fields(v3s16 p,
221 const std::string &formname,
222 const StringMap &fields,
223 ServerActiveObject *sender)
225 SCRIPTAPI_PRECHECKHEADER
227 int error_handler = PUSH_ERROR_HANDLER(L);
229 INodeDefManager *ndef = getServer()->ndef();
231 // If node doesn't exist, we don't know what callback to call
232 MapNode node = getEnv()->getMap().getNodeNoEx(p);
233 if (node.getContent() == CONTENT_IGNORE)
236 // Push callback function on stack
237 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
241 push_v3s16(L, p); // pos
242 lua_pushstring(L, formname.c_str()); // formname
243 lua_newtable(L); // fields
244 StringMap::const_iterator it;
245 for (it = fields.begin(); it != fields.end(); it++) {
246 const std::string &name = it->first;
247 const std::string &value = it->second;
248 lua_pushstring(L, name.c_str());
249 lua_pushlstring(L, value.c_str(), value.size());
252 objectrefGetOrCreate(L, sender); // player
253 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
254 lua_pop(L, 1); // Pop error handler
257 void ScriptApiNode::node_falling_update(v3s16 p)
259 SCRIPTAPI_PRECHECKHEADER
261 int error_handler = PUSH_ERROR_HANDLER(L);
263 lua_getglobal(L, "nodeupdate");
265 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
266 lua_pop(L, 1); // Pop error handler
269 void ScriptApiNode::node_falling_update_single(v3s16 p)
271 SCRIPTAPI_PRECHECKHEADER
273 int error_handler = PUSH_ERROR_HANDLER(L);
275 lua_getglobal(L, "nodeupdate_single");
277 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
278 lua_pop(L, 1); // Pop error handler