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"},
64 struct EnumString ScriptApiNode::es_LiquidType[] =
66 {LIQUID_NONE, "none"},
67 {LIQUID_FLOWING, "flowing"},
68 {LIQUID_SOURCE, "source"},
72 struct EnumString ScriptApiNode::es_ContentParamType[] =
79 struct EnumString ScriptApiNode::es_NodeBoxType[] =
81 {NODEBOX_REGULAR, "regular"},
82 {NODEBOX_FIXED, "fixed"},
83 {NODEBOX_WALLMOUNTED, "wallmounted"},
84 {NODEBOX_LEVELED, "leveled"},
88 ScriptApiNode::ScriptApiNode() {
91 ScriptApiNode::~ScriptApiNode() {
94 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
95 ServerActiveObject *puncher, PointedThing pointed)
97 SCRIPTAPI_PRECHECKHEADER
99 int error_handler = PUSH_ERROR_HANDLER(L);
101 INodeDefManager *ndef = getServer()->ndef();
103 // Push callback function on stack
104 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
109 pushnode(L, node, ndef);
110 objectrefGetOrCreate(L, puncher);
111 pushPointedThing(pointed);
112 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
113 lua_pop(L, 1); // Pop error handler
117 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
118 ServerActiveObject *digger)
120 SCRIPTAPI_PRECHECKHEADER
122 int error_handler = PUSH_ERROR_HANDLER(L);
124 INodeDefManager *ndef = getServer()->ndef();
126 // Push callback function on stack
127 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
132 pushnode(L, node, ndef);
133 objectrefGetOrCreate(L, digger);
134 PCALL_RES(lua_pcall(L, 3, 0, error_handler));
135 lua_pop(L, 1); // Pop error handler
139 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
141 SCRIPTAPI_PRECHECKHEADER
143 int error_handler = PUSH_ERROR_HANDLER(L);
145 INodeDefManager *ndef = getServer()->ndef();
147 // Push callback function on stack
148 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
153 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
154 lua_pop(L, 1); // Pop error handler
157 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
159 SCRIPTAPI_PRECHECKHEADER
161 int error_handler = PUSH_ERROR_HANDLER(L);
163 INodeDefManager *ndef = getServer()->ndef();
165 // Push callback function on stack
166 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
171 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
172 lua_pop(L, 1); // Pop error handler
175 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
177 SCRIPTAPI_PRECHECKHEADER
179 int error_handler = PUSH_ERROR_HANDLER(L);
181 INodeDefManager *ndef = getServer()->ndef();
183 // Push callback function on stack
184 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
189 pushnode(L, node, ndef);
190 PCALL_RES(lua_pcall(L, 2, 0, error_handler));
191 lua_pop(L, 1); // Pop error handler
194 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
196 SCRIPTAPI_PRECHECKHEADER
198 int error_handler = PUSH_ERROR_HANDLER(L);
200 INodeDefManager *ndef = getServer()->ndef();
202 // Push callback function on stack
203 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
208 lua_pushnumber(L,dtime);
209 PCALL_RES(lua_pcall(L, 2, 1, error_handler));
210 lua_remove(L, error_handler);
211 return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
214 void ScriptApiNode::node_on_receive_fields(v3s16 p,
215 const std::string &formname,
216 const StringMap &fields,
217 ServerActiveObject *sender)
219 SCRIPTAPI_PRECHECKHEADER
221 int error_handler = PUSH_ERROR_HANDLER(L);
223 INodeDefManager *ndef = getServer()->ndef();
225 // If node doesn't exist, we don't know what callback to call
226 MapNode node = getEnv()->getMap().getNodeNoEx(p);
227 if (node.getContent() == CONTENT_IGNORE)
230 // Push callback function on stack
231 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
235 push_v3s16(L, p); // pos
236 lua_pushstring(L, formname.c_str()); // formname
237 lua_newtable(L); // fields
238 StringMap::const_iterator it;
239 for (it = fields.begin(); it != fields.end(); it++) {
240 const std::string &name = it->first;
241 const std::string &value = it->second;
242 lua_pushstring(L, name.c_str());
243 lua_pushlstring(L, value.c_str(), value.size());
246 objectrefGetOrCreate(L, sender); // player
247 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
248 lua_pop(L, 1); // Pop error handler
251 void ScriptApiNode::node_falling_update(v3s16 p)
253 SCRIPTAPI_PRECHECKHEADER
255 int error_handler = PUSH_ERROR_HANDLER(L);
257 lua_getglobal(L, "nodeupdate");
259 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
260 lua_pop(L, 1); // Pop error handler
263 void ScriptApiNode::node_falling_update_single(v3s16 p)
265 SCRIPTAPI_PRECHECKHEADER
267 int error_handler = PUSH_ERROR_HANDLER(L);
269 lua_getglobal(L, "nodeupdate_single");
271 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
272 lua_pop(L, 1); // Pop error handler