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 "scriptapi.h"
21 #include "scriptapi_node.h"
22 #include "util/pointedthing.h"
24 #include "scriptapi_common.h"
25 #include "scriptapi_types.h"
26 #include "scriptapi_item.h"
27 #include "scriptapi_object.h"
30 struct EnumString es_DrawType[] =
32 {NDT_NORMAL, "normal"},
33 {NDT_AIRLIKE, "airlike"},
34 {NDT_LIQUID, "liquid"},
35 {NDT_FLOWINGLIQUID, "flowingliquid"},
36 {NDT_GLASSLIKE, "glasslike"},
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 es_ContentParamType[] =
55 struct EnumString es_ContentParamType2[] =
59 {CPT2_FLOWINGLIQUID, "flowingliquid"},
60 {CPT2_FACEDIR, "facedir"},
61 {CPT2_WALLMOUNTED, "wallmounted"},
65 struct EnumString es_LiquidType[] =
67 {LIQUID_NONE, "none"},
68 {LIQUID_FLOWING, "flowing"},
69 {LIQUID_SOURCE, "source"},
73 struct EnumString es_NodeBoxType[] =
75 {NODEBOX_REGULAR, "regular"},
76 {NODEBOX_FIXED, "fixed"},
77 {NODEBOX_WALLMOUNTED, "wallmounted"},
82 bool scriptapi_node_on_punch(lua_State *L, v3s16 p, MapNode node,
83 ServerActiveObject *puncher)
86 assert(lua_checkstack(L, 20));
87 StackUnroller stack_unroller(L);
89 INodeDefManager *ndef = get_server(L)->ndef();
91 // Push callback function on stack
92 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch"))
97 pushnode(L, node, ndef);
98 objectref_get_or_create(L, puncher);
99 if(lua_pcall(L, 3, 0, 0))
100 script_error(L, "error: %s", lua_tostring(L, -1));
104 bool scriptapi_node_on_dig(lua_State *L, v3s16 p, MapNode node,
105 ServerActiveObject *digger)
108 assert(lua_checkstack(L, 20));
109 StackUnroller stack_unroller(L);
111 INodeDefManager *ndef = get_server(L)->ndef();
113 // Push callback function on stack
114 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_dig"))
119 pushnode(L, node, ndef);
120 objectref_get_or_create(L, digger);
121 if(lua_pcall(L, 3, 0, 0))
122 script_error(L, "error: %s", lua_tostring(L, -1));
126 void scriptapi_node_on_construct(lua_State *L, v3s16 p, MapNode node)
129 assert(lua_checkstack(L, 20));
130 StackUnroller stack_unroller(L);
132 INodeDefManager *ndef = get_server(L)->ndef();
134 // Push callback function on stack
135 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_construct"))
140 if(lua_pcall(L, 1, 0, 0))
141 script_error(L, "error: %s", lua_tostring(L, -1));
144 void scriptapi_node_on_destruct(lua_State *L, v3s16 p, MapNode node)
147 assert(lua_checkstack(L, 20));
148 StackUnroller stack_unroller(L);
150 INodeDefManager *ndef = get_server(L)->ndef();
152 // Push callback function on stack
153 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_destruct"))
158 if(lua_pcall(L, 1, 0, 0))
159 script_error(L, "error: %s", lua_tostring(L, -1));
162 void scriptapi_node_after_destruct(lua_State *L, v3s16 p, MapNode node)
165 assert(lua_checkstack(L, 20));
166 StackUnroller stack_unroller(L);
168 INodeDefManager *ndef = get_server(L)->ndef();
170 // Push callback function on stack
171 if(!get_item_callback(L, ndef->get(node).name.c_str(), "after_destruct"))
176 pushnode(L, node, ndef);
177 if(lua_pcall(L, 2, 0, 0))
178 script_error(L, "error: %s", lua_tostring(L, -1));
181 bool scriptapi_node_on_timer(lua_State *L, v3s16 p, MapNode node, f32 dtime)
184 assert(lua_checkstack(L, 20));
185 StackUnroller stack_unroller(L);
187 INodeDefManager *ndef = get_server(L)->ndef();
189 // Push callback function on stack
190 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_timer"))
195 lua_pushnumber(L,dtime);
196 if(lua_pcall(L, 2, 1, 0))
197 script_error(L, "error: %s", lua_tostring(L, -1));
198 if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
204 void scriptapi_node_on_receive_fields(lua_State *L, v3s16 p,
205 const std::string &formname,
206 const std::map<std::string, std::string> &fields,
207 ServerActiveObject *sender)
210 assert(lua_checkstack(L, 20));
211 StackUnroller stack_unroller(L);
213 INodeDefManager *ndef = get_server(L)->ndef();
215 // If node doesn't exist, we don't know what callback to call
216 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
217 if(node.getContent() == CONTENT_IGNORE)
220 // Push callback function on stack
221 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_receive_fields"))
228 lua_pushstring(L, formname.c_str());
231 for(std::map<std::string, std::string>::const_iterator
232 i = fields.begin(); i != fields.end(); i++){
233 const std::string &name = i->first;
234 const std::string &value = i->second;
235 lua_pushstring(L, name.c_str());
236 lua_pushlstring(L, value.c_str(), value.size());
240 objectref_get_or_create(L, sender);
241 if(lua_pcall(L, 4, 0, 0))
242 script_error(L, "error: %s", lua_tostring(L, -1));