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"
29 struct EnumString ScriptApiNode::es_DrawType[] =
31 {NDT_NORMAL, "normal"},
32 {NDT_AIRLIKE, "airlike"},
33 {NDT_LIQUID, "liquid"},
34 {NDT_FLOWINGLIQUID, "flowingliquid"},
35 {NDT_GLASSLIKE, "glasslike"},
36 {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
37 {NDT_ALLFACES, "allfaces"},
38 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
39 {NDT_TORCHLIKE, "torchlike"},
40 {NDT_SIGNLIKE, "signlike"},
41 {NDT_PLANTLIKE, "plantlike"},
42 {NDT_FENCELIKE, "fencelike"},
43 {NDT_RAILLIKE, "raillike"},
44 {NDT_NODEBOX, "nodebox"},
48 struct EnumString ScriptApiNode::es_ContentParamType2[] =
52 {CPT2_FLOWINGLIQUID, "flowingliquid"},
53 {CPT2_FACEDIR, "facedir"},
54 {CPT2_WALLMOUNTED, "wallmounted"},
55 {CPT2_LEVELED, "leveled"},
59 struct EnumString ScriptApiNode::es_LiquidType[] =
61 {LIQUID_NONE, "none"},
62 {LIQUID_FLOWING, "flowing"},
63 {LIQUID_SOURCE, "source"},
67 struct EnumString ScriptApiNode::es_ContentParamType[] =
74 struct EnumString ScriptApiNode::es_NodeBoxType[] =
76 {NODEBOX_REGULAR, "regular"},
77 {NODEBOX_FIXED, "fixed"},
78 {NODEBOX_WALLMOUNTED, "wallmounted"},
79 {NODEBOX_LEVELED, "leveled"},
83 ScriptApiNode::ScriptApiNode() {
86 ScriptApiNode::~ScriptApiNode() {
89 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
90 ServerActiveObject *puncher)
92 SCRIPTAPI_PRECHECKHEADER
94 INodeDefManager *ndef = getServer()->ndef();
96 // Push callback function on stack
97 if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
102 pushnode(L, node, ndef);
103 objectrefGetOrCreate(puncher);
104 if(lua_pcall(L, 3, 0, 0))
105 scriptError("error: %s", lua_tostring(L, -1));
109 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
110 ServerActiveObject *digger)
112 SCRIPTAPI_PRECHECKHEADER
114 INodeDefManager *ndef = getServer()->ndef();
116 // Push callback function on stack
117 if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
122 pushnode(L, node, ndef);
123 objectrefGetOrCreate(digger);
124 if(lua_pcall(L, 3, 0, 0))
125 scriptError("error: %s", lua_tostring(L, -1));
129 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
131 SCRIPTAPI_PRECHECKHEADER
133 INodeDefManager *ndef = getServer()->ndef();
135 // Push callback function on stack
136 if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
141 if(lua_pcall(L, 1, 0, 0))
142 scriptError("error: %s", lua_tostring(L, -1));
145 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
147 SCRIPTAPI_PRECHECKHEADER
149 INodeDefManager *ndef = getServer()->ndef();
151 // Push callback function on stack
152 if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
157 if(lua_pcall(L, 1, 0, 0))
158 scriptError("error: %s", lua_tostring(L, -1));
161 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
163 SCRIPTAPI_PRECHECKHEADER
165 INodeDefManager *ndef = getServer()->ndef();
167 // Push callback function on stack
168 if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
173 pushnode(L, node, ndef);
174 if(lua_pcall(L, 2, 0, 0))
175 scriptError("error: %s", lua_tostring(L, -1));
178 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
180 SCRIPTAPI_PRECHECKHEADER
182 INodeDefManager *ndef = getServer()->ndef();
184 // Push callback function on stack
185 if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
190 lua_pushnumber(L,dtime);
191 if(lua_pcall(L, 2, 1, 0))
192 scriptError("error: %s", lua_tostring(L, -1));
193 if((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"))
221 lua_pushstring(L, formname.c_str());
224 for(std::map<std::string, std::string>::const_iterator
225 i = fields.begin(); i != fields.end(); i++){
226 const std::string &name = i->first;
227 const std::string &value = i->second;
228 lua_pushstring(L, name.c_str());
229 lua_pushlstring(L, value.c_str(), value.size());
233 objectrefGetOrCreate(sender);
234 if(lua_pcall(L, 4, 0, 0))
235 scriptError("error: %s", lua_tostring(L, -1));
238 void ScriptApiNode::node_falling_update(v3s16 p)
240 SCRIPTAPI_PRECHECKHEADER
241 lua_getglobal(L, "nodeupdate");
243 if(lua_pcall(L, 1, 0, 0))
244 scriptError("error: %s", lua_tostring(L, -1));
247 void ScriptApiNode::node_falling_update_single(v3s16 p)
249 SCRIPTAPI_PRECHECKHEADER
250 lua_getglobal(L, "nodeupdate_single");
252 if(lua_pcall(L, 1, 0, 0))
253 scriptError("error: %s", lua_tostring(L, -1));