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"},
63 struct EnumString ScriptApiNode::es_LiquidType[] =
65 {LIQUID_NONE, "none"},
66 {LIQUID_FLOWING, "flowing"},
67 {LIQUID_SOURCE, "source"},
71 struct EnumString ScriptApiNode::es_ContentParamType[] =
78 struct EnumString ScriptApiNode::es_NodeBoxType[] =
80 {NODEBOX_REGULAR, "regular"},
81 {NODEBOX_FIXED, "fixed"},
82 {NODEBOX_WALLMOUNTED, "wallmounted"},
83 {NODEBOX_LEVELED, "leveled"},
87 ScriptApiNode::ScriptApiNode() {
90 ScriptApiNode::~ScriptApiNode() {
93 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
94 ServerActiveObject *puncher, PointedThing pointed)
96 SCRIPTAPI_PRECHECKHEADER
98 INodeDefManager *ndef = getServer()->ndef();
100 // Push callback function on stack
101 if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
106 pushnode(L, node, ndef);
107 objectrefGetOrCreate(L, puncher);
108 pushPointedThing(pointed);
109 if (lua_pcall(L, 4, 0, m_errorhandler))
114 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
115 ServerActiveObject *digger)
117 SCRIPTAPI_PRECHECKHEADER
119 INodeDefManager *ndef = getServer()->ndef();
121 // Push callback function on stack
122 if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
127 pushnode(L, node, ndef);
128 objectrefGetOrCreate(L, digger);
129 if (lua_pcall(L, 3, 0, m_errorhandler))
134 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
136 SCRIPTAPI_PRECHECKHEADER
138 INodeDefManager *ndef = getServer()->ndef();
140 // Push callback function on stack
141 if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
146 if (lua_pcall(L, 1, 0, m_errorhandler))
150 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
152 SCRIPTAPI_PRECHECKHEADER
154 INodeDefManager *ndef = getServer()->ndef();
156 // Push callback function on stack
157 if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
162 if (lua_pcall(L, 1, 0, m_errorhandler))
166 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
168 SCRIPTAPI_PRECHECKHEADER
170 INodeDefManager *ndef = getServer()->ndef();
172 // Push callback function on stack
173 if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
178 pushnode(L, node, ndef);
179 if (lua_pcall(L, 2, 0, m_errorhandler))
183 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
185 SCRIPTAPI_PRECHECKHEADER
187 INodeDefManager *ndef = getServer()->ndef();
189 // Push callback function on stack
190 if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
195 lua_pushnumber(L,dtime);
196 if (lua_pcall(L, 2, 1, m_errorhandler))
198 return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
201 void ScriptApiNode::node_on_receive_fields(v3s16 p,
202 const std::string &formname,
203 const StringMap &fields,
204 ServerActiveObject *sender)
206 SCRIPTAPI_PRECHECKHEADER
208 INodeDefManager *ndef = getServer()->ndef();
210 // If node doesn't exist, we don't know what callback to call
211 MapNode node = getEnv()->getMap().getNodeNoEx(p);
212 if (node.getContent() == CONTENT_IGNORE)
215 // Push callback function on stack
216 if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
220 push_v3s16(L, p); // pos
221 lua_pushstring(L, formname.c_str()); // formname
222 lua_newtable(L); // fields
223 StringMap::const_iterator it;
224 for (it = fields.begin(); it != fields.end(); it++) {
225 const std::string &name = it->first;
226 const std::string &value = it->second;
227 lua_pushstring(L, name.c_str());
228 lua_pushlstring(L, value.c_str(), value.size());
231 objectrefGetOrCreate(L, sender); // player
232 if (lua_pcall(L, 4, 0, m_errorhandler))
236 void ScriptApiNode::node_falling_update(v3s16 p)
238 SCRIPTAPI_PRECHECKHEADER
240 lua_getglobal(L, "nodeupdate");
242 if (lua_pcall(L, 1, 0, m_errorhandler))
246 void ScriptApiNode::node_falling_update_single(v3s16 p)
248 SCRIPTAPI_PRECHECKHEADER
250 lua_getglobal(L, "nodeupdate_single");
252 if (lua_pcall(L, 1, 0, m_errorhandler))