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_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"},
49 struct EnumString ScriptApiNode::es_ContentParamType2[] =
53 {CPT2_FLOWINGLIQUID, "flowingliquid"},
54 {CPT2_FACEDIR, "facedir"},
55 {CPT2_WALLMOUNTED, "wallmounted"},
56 {CPT2_LEVELED, "leveled"},
60 struct EnumString ScriptApiNode::es_LiquidType[] =
62 {LIQUID_NONE, "none"},
63 {LIQUID_FLOWING, "flowing"},
64 {LIQUID_SOURCE, "source"},
68 struct EnumString ScriptApiNode::es_ContentParamType[] =
75 struct EnumString ScriptApiNode::es_NodeBoxType[] =
77 {NODEBOX_REGULAR, "regular"},
78 {NODEBOX_FIXED, "fixed"},
79 {NODEBOX_WALLMOUNTED, "wallmounted"},
80 {NODEBOX_LEVELED, "leveled"},
84 ScriptApiNode::ScriptApiNode() {
87 ScriptApiNode::~ScriptApiNode() {
90 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
91 ServerActiveObject *puncher, PointedThing pointed)
93 SCRIPTAPI_PRECHECKHEADER
95 INodeDefManager *ndef = getServer()->ndef();
97 // Push callback function on stack
98 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
103 pushnode(L, node, ndef);
104 objectrefGetOrCreate(puncher);
105 pushPointedThing(pointed);
106 if (lua_pcall(L, 4, 0, m_errorhandler))
111 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
112 ServerActiveObject *digger)
114 SCRIPTAPI_PRECHECKHEADER
116 INodeDefManager *ndef = getServer()->ndef();
118 // Push callback function on stack
119 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
124 pushnode(L, node, ndef);
125 objectrefGetOrCreate(digger);
126 if (lua_pcall(L, 3, 0, m_errorhandler))
131 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
133 SCRIPTAPI_PRECHECKHEADER
135 INodeDefManager *ndef = getServer()->ndef();
137 // Push callback function on stack
138 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
143 if (lua_pcall(L, 1, 0, m_errorhandler))
147 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
149 SCRIPTAPI_PRECHECKHEADER
151 INodeDefManager *ndef = getServer()->ndef();
153 // Push callback function on stack
154 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
159 if (lua_pcall(L, 1, 0, m_errorhandler))
163 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
165 SCRIPTAPI_PRECHECKHEADER
167 INodeDefManager *ndef = getServer()->ndef();
169 // Push callback function on stack
170 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
175 pushnode(L, node, ndef);
176 if (lua_pcall(L, 2, 0, m_errorhandler))
180 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
182 SCRIPTAPI_PRECHECKHEADER
184 INodeDefManager *ndef = getServer()->ndef();
186 // Push callback function on stack
187 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
192 lua_pushnumber(L,dtime);
193 if (lua_pcall(L, 2, 1, m_errorhandler))
195 return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
198 void ScriptApiNode::node_on_receive_fields(v3s16 p,
199 const std::string &formname,
200 const std::map<std::string, std::string> &fields,
201 ServerActiveObject *sender)
203 SCRIPTAPI_PRECHECKHEADER
205 INodeDefManager *ndef = getServer()->ndef();
207 // If node doesn't exist, we don't know what callback to call
208 MapNode node = getEnv()->getMap().getNodeNoEx(p);
209 if (node.getContent() == CONTENT_IGNORE)
212 // Push callback function on stack
213 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
217 push_v3s16(L, p); // pos
218 lua_pushstring(L, formname.c_str()); // formname
219 lua_newtable(L); // fields
220 std::map<std::string, std::string>::const_iterator it;
221 for (it = fields.begin(); it != fields.end(); it++){
222 const std::string &name = it->first;
223 const std::string &value = it->second;
224 lua_pushstring(L, name.c_str());
225 lua_pushlstring(L, value.c_str(), value.size());
228 objectrefGetOrCreate(sender); // player
229 if (lua_pcall(L, 4, 0, m_errorhandler))
233 void ScriptApiNode::node_falling_update(v3s16 p)
235 SCRIPTAPI_PRECHECKHEADER
237 lua_getglobal(L, "nodeupdate");
239 if (lua_pcall(L, 1, 0, m_errorhandler))
243 void ScriptApiNode::node_falling_update_single(v3s16 p)
245 SCRIPTAPI_PRECHECKHEADER
247 lua_getglobal(L, "nodeupdate_single");
249 if (lua_pcall(L, 1, 0, m_errorhandler))