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"},
53 {CPT2_LEVELED, "leveled"},
57 struct EnumString ScriptApiNode::es_LiquidType[] =
59 {LIQUID_NONE, "none"},
60 {LIQUID_FLOWING, "flowing"},
61 {LIQUID_SOURCE, "source"},
65 struct EnumString ScriptApiNode::es_ContentParamType[] =
72 struct EnumString ScriptApiNode::es_NodeBoxType[] =
74 {NODEBOX_REGULAR, "regular"},
75 {NODEBOX_FIXED, "fixed"},
76 {NODEBOX_WALLMOUNTED, "wallmounted"},
77 {NODEBOX_LEVELED, "leveled"},
81 ScriptApiNode::ScriptApiNode() {
84 ScriptApiNode::~ScriptApiNode() {
87 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
88 ServerActiveObject *puncher)
90 SCRIPTAPI_PRECHECKHEADER
92 INodeDefManager *ndef = getServer()->ndef();
94 // Push callback function on stack
95 if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
100 pushnode(L, node, ndef);
101 objectrefGetOrCreate(puncher);
102 if(lua_pcall(L, 3, 0, 0))
103 scriptError("error: %s", lua_tostring(L, -1));
107 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
108 ServerActiveObject *digger)
110 SCRIPTAPI_PRECHECKHEADER
112 INodeDefManager *ndef = getServer()->ndef();
114 // Push callback function on stack
115 if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
120 pushnode(L, node, ndef);
121 objectrefGetOrCreate(digger);
122 if(lua_pcall(L, 3, 0, 0))
123 scriptError("error: %s", lua_tostring(L, -1));
127 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
129 SCRIPTAPI_PRECHECKHEADER
131 INodeDefManager *ndef = getServer()->ndef();
133 // Push callback function on stack
134 if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
139 if(lua_pcall(L, 1, 0, 0))
140 scriptError("error: %s", lua_tostring(L, -1));
143 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
145 SCRIPTAPI_PRECHECKHEADER
147 INodeDefManager *ndef = getServer()->ndef();
149 // Push callback function on stack
150 if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
155 if(lua_pcall(L, 1, 0, 0))
156 scriptError("error: %s", lua_tostring(L, -1));
159 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
161 SCRIPTAPI_PRECHECKHEADER
163 INodeDefManager *ndef = getServer()->ndef();
165 // Push callback function on stack
166 if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
171 pushnode(L, node, ndef);
172 if(lua_pcall(L, 2, 0, 0))
173 scriptError("error: %s", lua_tostring(L, -1));
176 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
178 SCRIPTAPI_PRECHECKHEADER
180 INodeDefManager *ndef = getServer()->ndef();
182 // Push callback function on stack
183 if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
188 lua_pushnumber(L,dtime);
189 if(lua_pcall(L, 2, 1, 0))
190 scriptError("error: %s", lua_tostring(L, -1));
191 if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
197 void ScriptApiNode::node_on_receive_fields(v3s16 p,
198 const std::string &formname,
199 const std::map<std::string, std::string> &fields,
200 ServerActiveObject *sender)
202 SCRIPTAPI_PRECHECKHEADER
204 INodeDefManager *ndef = getServer()->ndef();
206 // If node doesn't exist, we don't know what callback to call
207 MapNode node = getEnv()->getMap().getNodeNoEx(p);
208 if(node.getContent() == CONTENT_IGNORE)
211 // Push callback function on stack
212 if(!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
219 lua_pushstring(L, formname.c_str());
222 for(std::map<std::string, std::string>::const_iterator
223 i = fields.begin(); i != fields.end(); i++){
224 const std::string &name = i->first;
225 const std::string &value = i->second;
226 lua_pushstring(L, name.c_str());
227 lua_pushlstring(L, value.c_str(), value.size());
231 objectrefGetOrCreate(sender);
232 if(lua_pcall(L, 4, 0, 0))
233 scriptError("error: %s", lua_tostring(L, -1));