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"},
85 {NODEBOX_CONNECTED, "connected"},
89 ScriptApiNode::ScriptApiNode() {
92 ScriptApiNode::~ScriptApiNode() {
95 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
96 ServerActiveObject *puncher, PointedThing pointed)
98 SCRIPTAPI_PRECHECKHEADER
100 int error_handler = PUSH_ERROR_HANDLER(L);
102 INodeDefManager *ndef = getServer()->ndef();
104 // Push callback function on stack
105 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
110 pushnode(L, node, ndef);
111 objectrefGetOrCreate(L, puncher);
112 pushPointedThing(pointed);
113 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
114 lua_pop(L, 1); // Pop error handler
118 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
119 ServerActiveObject *digger)
121 SCRIPTAPI_PRECHECKHEADER
123 int error_handler = PUSH_ERROR_HANDLER(L);
125 INodeDefManager *ndef = getServer()->ndef();
127 // Push callback function on stack
128 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
133 pushnode(L, node, ndef);
134 objectrefGetOrCreate(L, digger);
135 PCALL_RES(lua_pcall(L, 3, 0, error_handler));
136 lua_pop(L, 1); // Pop error handler
140 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
142 SCRIPTAPI_PRECHECKHEADER
144 int error_handler = PUSH_ERROR_HANDLER(L);
146 INodeDefManager *ndef = getServer()->ndef();
148 // Push callback function on stack
149 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
154 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
155 lua_pop(L, 1); // Pop error handler
158 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
160 SCRIPTAPI_PRECHECKHEADER
162 int error_handler = PUSH_ERROR_HANDLER(L);
164 INodeDefManager *ndef = getServer()->ndef();
166 // Push callback function on stack
167 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
172 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
173 lua_pop(L, 1); // Pop error handler
176 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
178 SCRIPTAPI_PRECHECKHEADER
180 int error_handler = PUSH_ERROR_HANDLER(L);
182 INodeDefManager *ndef = getServer()->ndef();
184 // Push callback function on stack
185 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
190 pushnode(L, node, ndef);
191 PCALL_RES(lua_pcall(L, 2, 0, error_handler));
192 lua_pop(L, 1); // Pop error handler
195 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
197 SCRIPTAPI_PRECHECKHEADER
199 int error_handler = PUSH_ERROR_HANDLER(L);
201 INodeDefManager *ndef = getServer()->ndef();
203 // Push callback function on stack
204 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
209 lua_pushnumber(L,dtime);
210 PCALL_RES(lua_pcall(L, 2, 1, error_handler));
211 lua_remove(L, error_handler);
212 return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
215 void ScriptApiNode::node_on_receive_fields(v3s16 p,
216 const std::string &formname,
217 const StringMap &fields,
218 ServerActiveObject *sender)
220 SCRIPTAPI_PRECHECKHEADER
222 int error_handler = PUSH_ERROR_HANDLER(L);
224 INodeDefManager *ndef = getServer()->ndef();
226 // If node doesn't exist, we don't know what callback to call
227 MapNode node = getEnv()->getMap().getNodeNoEx(p);
228 if (node.getContent() == CONTENT_IGNORE)
231 // Push callback function on stack
232 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
236 push_v3s16(L, p); // pos
237 lua_pushstring(L, formname.c_str()); // formname
238 lua_newtable(L); // fields
239 StringMap::const_iterator it;
240 for (it = fields.begin(); it != fields.end(); it++) {
241 const std::string &name = it->first;
242 const std::string &value = it->second;
243 lua_pushstring(L, name.c_str());
244 lua_pushlstring(L, value.c_str(), value.size());
247 objectrefGetOrCreate(L, sender); // player
248 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
249 lua_pop(L, 1); // Pop error handler
252 void ScriptApiNode::node_falling_update(v3s16 p)
254 SCRIPTAPI_PRECHECKHEADER
256 int error_handler = PUSH_ERROR_HANDLER(L);
258 lua_getglobal(L, "nodeupdate");
260 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
261 lua_pop(L, 1); // Pop error handler
264 void ScriptApiNode::node_falling_update_single(v3s16 p)
266 SCRIPTAPI_PRECHECKHEADER
268 int error_handler = PUSH_ERROR_HANDLER(L);
270 lua_getglobal(L, "nodeupdate_single");
272 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
273 lua_pop(L, 1); // Pop error handler