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_FIRELIKE, "firelike"},
44 {NDT_FENCELIKE, "fencelike"},
45 {NDT_RAILLIKE, "raillike"},
46 {NDT_NODEBOX, "nodebox"},
50 struct EnumString ScriptApiNode::es_ContentParamType2[] =
54 {CPT2_FLOWINGLIQUID, "flowingliquid"},
55 {CPT2_FACEDIR, "facedir"},
56 {CPT2_WALLMOUNTED, "wallmounted"},
57 {CPT2_LEVELED, "leveled"},
61 struct EnumString ScriptApiNode::es_LiquidType[] =
63 {LIQUID_NONE, "none"},
64 {LIQUID_FLOWING, "flowing"},
65 {LIQUID_SOURCE, "source"},
69 struct EnumString ScriptApiNode::es_ContentParamType[] =
76 struct EnumString ScriptApiNode::es_NodeBoxType[] =
78 {NODEBOX_REGULAR, "regular"},
79 {NODEBOX_FIXED, "fixed"},
80 {NODEBOX_WALLMOUNTED, "wallmounted"},
81 {NODEBOX_LEVELED, "leveled"},
85 ScriptApiNode::ScriptApiNode() {
88 ScriptApiNode::~ScriptApiNode() {
91 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
92 ServerActiveObject *puncher, PointedThing pointed)
94 SCRIPTAPI_PRECHECKHEADER
96 INodeDefManager *ndef = getServer()->ndef();
98 // Push callback function on stack
99 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
104 pushnode(L, node, ndef);
105 objectrefGetOrCreate(puncher);
106 pushPointedThing(pointed);
107 if (lua_pcall(L, 4, 0, m_errorhandler))
112 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
113 ServerActiveObject *digger)
115 SCRIPTAPI_PRECHECKHEADER
117 INodeDefManager *ndef = getServer()->ndef();
119 // Push callback function on stack
120 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
125 pushnode(L, node, ndef);
126 objectrefGetOrCreate(digger);
127 if (lua_pcall(L, 3, 0, m_errorhandler))
132 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
134 SCRIPTAPI_PRECHECKHEADER
136 INodeDefManager *ndef = getServer()->ndef();
138 // Push callback function on stack
139 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
144 if (lua_pcall(L, 1, 0, m_errorhandler))
148 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
150 SCRIPTAPI_PRECHECKHEADER
152 INodeDefManager *ndef = getServer()->ndef();
154 // Push callback function on stack
155 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
160 if (lua_pcall(L, 1, 0, m_errorhandler))
164 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
166 SCRIPTAPI_PRECHECKHEADER
168 INodeDefManager *ndef = getServer()->ndef();
170 // Push callback function on stack
171 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
176 pushnode(L, node, ndef);
177 if (lua_pcall(L, 2, 0, m_errorhandler))
181 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
183 SCRIPTAPI_PRECHECKHEADER
185 INodeDefManager *ndef = getServer()->ndef();
187 // Push callback function on stack
188 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
193 lua_pushnumber(L,dtime);
194 if (lua_pcall(L, 2, 1, m_errorhandler))
196 return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
199 void ScriptApiNode::node_on_receive_fields(v3s16 p,
200 const std::string &formname,
201 const std::map<std::string, std::string> &fields,
202 ServerActiveObject *sender)
204 SCRIPTAPI_PRECHECKHEADER
206 INodeDefManager *ndef = getServer()->ndef();
208 // If node doesn't exist, we don't know what callback to call
209 MapNode node = getEnv()->getMap().getNodeNoEx(p);
210 if (node.getContent() == CONTENT_IGNORE)
213 // Push callback function on stack
214 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
218 push_v3s16(L, p); // pos
219 lua_pushstring(L, formname.c_str()); // formname
220 lua_newtable(L); // fields
221 std::map<std::string, std::string>::const_iterator it;
222 for (it = fields.begin(); it != fields.end(); it++){
223 const std::string &name = it->first;
224 const std::string &value = it->second;
225 lua_pushstring(L, name.c_str());
226 lua_pushlstring(L, value.c_str(), value.size());
229 objectrefGetOrCreate(sender); // player
230 if (lua_pcall(L, 4, 0, m_errorhandler))
234 void ScriptApiNode::node_falling_update(v3s16 p)
236 SCRIPTAPI_PRECHECKHEADER
238 lua_getglobal(L, "nodeupdate");
240 if (lua_pcall(L, 1, 0, m_errorhandler))
244 void ScriptApiNode::node_falling_update_single(v3s16 p)
246 SCRIPTAPI_PRECHECKHEADER
248 lua_getglobal(L, "nodeupdate_single");
250 if (lua_pcall(L, 1, 0, m_errorhandler))