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 "common/c_converter.h"
22 #include "common/c_content.h"
27 struct EnumString ScriptApiNode::es_DrawType[] =
29 {NDT_NORMAL, "normal"},
30 {NDT_AIRLIKE, "airlike"},
31 {NDT_LIQUID, "liquid"},
32 {NDT_FLOWINGLIQUID, "flowingliquid"},
33 {NDT_GLASSLIKE, "glasslike"},
34 {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
35 {NDT_ALLFACES, "allfaces"},
36 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
37 {NDT_TORCHLIKE, "torchlike"},
38 {NDT_SIGNLIKE, "signlike"},
39 {NDT_PLANTLIKE, "plantlike"},
40 {NDT_FENCELIKE, "fencelike"},
41 {NDT_RAILLIKE, "raillike"},
42 {NDT_NODEBOX, "nodebox"},
46 struct EnumString ScriptApiNode::es_ContentParamType2[] =
50 {CPT2_FLOWINGLIQUID, "flowingliquid"},
51 {CPT2_FACEDIR, "facedir"},
52 {CPT2_WALLMOUNTED, "wallmounted"},
56 struct EnumString ScriptApiNode::es_LiquidType[] =
58 {LIQUID_NONE, "none"},
59 {LIQUID_FLOWING, "flowing"},
60 {LIQUID_SOURCE, "source"},
64 struct EnumString ScriptApiNode::es_ContentParamType[] =
71 struct EnumString ScriptApiNode::es_NodeBoxType[] =
73 {NODEBOX_REGULAR, "regular"},
74 {NODEBOX_FIXED, "fixed"},
75 {NODEBOX_WALLMOUNTED, "wallmounted"},
79 ScriptApiNode::ScriptApiNode() {
82 ScriptApiNode::~ScriptApiNode() {
85 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
86 ServerActiveObject *puncher)
88 SCRIPTAPI_PRECHECKHEADER
90 INodeDefManager *ndef = getServer()->ndef();
92 // Push callback function on stack
93 if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
98 pushnode(L, node, ndef);
99 objectrefGetOrCreate(puncher);
100 if(lua_pcall(L, 3, 0, 0))
101 scriptError("error: %s", lua_tostring(L, -1));
105 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
106 ServerActiveObject *digger)
108 SCRIPTAPI_PRECHECKHEADER
110 INodeDefManager *ndef = getServer()->ndef();
112 // Push callback function on stack
113 if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
118 pushnode(L, node, ndef);
119 objectrefGetOrCreate(digger);
120 if(lua_pcall(L, 3, 0, 0))
121 scriptError("error: %s", lua_tostring(L, -1));
125 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
127 SCRIPTAPI_PRECHECKHEADER
129 INodeDefManager *ndef = getServer()->ndef();
131 // Push callback function on stack
132 if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
137 if(lua_pcall(L, 1, 0, 0))
138 scriptError("error: %s", lua_tostring(L, -1));
141 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
143 SCRIPTAPI_PRECHECKHEADER
145 INodeDefManager *ndef = getServer()->ndef();
147 // Push callback function on stack
148 if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
153 if(lua_pcall(L, 1, 0, 0))
154 scriptError("error: %s", lua_tostring(L, -1));
157 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
159 SCRIPTAPI_PRECHECKHEADER
161 INodeDefManager *ndef = getServer()->ndef();
163 // Push callback function on stack
164 if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
169 pushnode(L, node, ndef);
170 if(lua_pcall(L, 2, 0, 0))
171 scriptError("error: %s", lua_tostring(L, -1));
174 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
176 SCRIPTAPI_PRECHECKHEADER
178 INodeDefManager *ndef = getServer()->ndef();
180 // Push callback function on stack
181 if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
186 lua_pushnumber(L,dtime);
187 if(lua_pcall(L, 2, 1, 0))
188 scriptError("error: %s", lua_tostring(L, -1));
189 if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
195 void ScriptApiNode::node_on_receive_fields(v3s16 p,
196 const std::string &formname,
197 const std::map<std::string, std::string> &fields,
198 ServerActiveObject *sender)
200 SCRIPTAPI_PRECHECKHEADER
202 INodeDefManager *ndef = getServer()->ndef();
204 // If node doesn't exist, we don't know what callback to call
205 MapNode node = getEnv()->getMap().getNodeNoEx(p);
206 if(node.getContent() == CONTENT_IGNORE)
209 // Push callback function on stack
210 if(!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
217 lua_pushstring(L, formname.c_str());
220 for(std::map<std::string, std::string>::const_iterator
221 i = fields.begin(); i != fields.end(); i++){
222 const std::string &name = i->first;
223 const std::string &value = i->second;
224 lua_pushstring(L, name.c_str());
225 lua_pushlstring(L, value.c_str(), value.size());
229 objectrefGetOrCreate(sender);
230 if(lua_pcall(L, 4, 0, 0))
231 scriptError("error: %s", lua_tostring(L, -1));