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_GLASSLIKE_FRAMED, "glasslike_framed"},
38 {NDT_ALLFACES, "allfaces"},
39 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
40 {NDT_TORCHLIKE, "torchlike"},
41 {NDT_SIGNLIKE, "signlike"},
42 {NDT_PLANTLIKE, "plantlike"},
43 {NDT_FENCELIKE, "fencelike"},
44 {NDT_RAILLIKE, "raillike"},
45 {NDT_NODEBOX, "nodebox"},
49 struct EnumString es_ContentParamType[] =
56 struct EnumString es_ContentParamType2[] =
60 {CPT2_FLOWINGLIQUID, "flowingliquid"},
61 {CPT2_FACEDIR, "facedir"},
62 {CPT2_WALLMOUNTED, "wallmounted"},
66 struct EnumString es_LiquidType[] =
68 {LIQUID_NONE, "none"},
69 {LIQUID_FLOWING, "flowing"},
70 {LIQUID_SOURCE, "source"},
74 struct EnumString es_NodeBoxType[] =
76 {NODEBOX_REGULAR, "regular"},
77 {NODEBOX_FIXED, "fixed"},
78 {NODEBOX_WALLMOUNTED, "wallmounted"},
83 bool scriptapi_node_on_punch(lua_State *L, v3s16 p, MapNode node,
84 ServerActiveObject *puncher)
87 assert(lua_checkstack(L, 20));
88 StackUnroller stack_unroller(L);
90 INodeDefManager *ndef = get_server(L)->ndef();
92 // Push callback function on stack
93 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch"))
98 pushnode(L, node, ndef);
99 objectref_get_or_create(L, puncher);
100 if(lua_pcall(L, 3, 0, 0))
101 script_error(L, "error: %s", lua_tostring(L, -1));
105 bool scriptapi_node_on_dig(lua_State *L, v3s16 p, MapNode node,
106 ServerActiveObject *digger)
109 assert(lua_checkstack(L, 20));
110 StackUnroller stack_unroller(L);
112 INodeDefManager *ndef = get_server(L)->ndef();
114 // Push callback function on stack
115 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_dig"))
120 pushnode(L, node, ndef);
121 objectref_get_or_create(L, digger);
122 if(lua_pcall(L, 3, 0, 0))
123 script_error(L, "error: %s", lua_tostring(L, -1));
127 void scriptapi_node_on_construct(lua_State *L, v3s16 p, MapNode node)
130 assert(lua_checkstack(L, 20));
131 StackUnroller stack_unroller(L);
133 INodeDefManager *ndef = get_server(L)->ndef();
135 // Push callback function on stack
136 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_construct"))
141 if(lua_pcall(L, 1, 0, 0))
142 script_error(L, "error: %s", lua_tostring(L, -1));
145 void scriptapi_node_on_destruct(lua_State *L, v3s16 p, MapNode node)
148 assert(lua_checkstack(L, 20));
149 StackUnroller stack_unroller(L);
151 INodeDefManager *ndef = get_server(L)->ndef();
153 // Push callback function on stack
154 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_destruct"))
159 if(lua_pcall(L, 1, 0, 0))
160 script_error(L, "error: %s", lua_tostring(L, -1));
163 void scriptapi_node_after_destruct(lua_State *L, v3s16 p, MapNode node)
166 assert(lua_checkstack(L, 20));
167 StackUnroller stack_unroller(L);
169 INodeDefManager *ndef = get_server(L)->ndef();
171 // Push callback function on stack
172 if(!get_item_callback(L, ndef->get(node).name.c_str(), "after_destruct"))
177 pushnode(L, node, ndef);
178 if(lua_pcall(L, 2, 0, 0))
179 script_error(L, "error: %s", lua_tostring(L, -1));
182 bool scriptapi_node_on_timer(lua_State *L, v3s16 p, MapNode node, f32 dtime)
185 assert(lua_checkstack(L, 20));
186 StackUnroller stack_unroller(L);
188 INodeDefManager *ndef = get_server(L)->ndef();
190 // Push callback function on stack
191 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_timer"))
196 lua_pushnumber(L,dtime);
197 if(lua_pcall(L, 2, 1, 0))
198 script_error(L, "error: %s", lua_tostring(L, -1));
199 if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
205 void scriptapi_node_on_receive_fields(lua_State *L, v3s16 p,
206 const std::string &formname,
207 const std::map<std::string, std::string> &fields,
208 ServerActiveObject *sender)
211 assert(lua_checkstack(L, 20));
212 StackUnroller stack_unroller(L);
214 INodeDefManager *ndef = get_server(L)->ndef();
216 // If node doesn't exist, we don't know what callback to call
217 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
218 if(node.getContent() == CONTENT_IGNORE)
221 // Push callback function on stack
222 if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_receive_fields"))
229 lua_pushstring(L, formname.c_str());
232 for(std::map<std::string, std::string>::const_iterator
233 i = fields.begin(); i != fields.end(); i++){
234 const std::string &name = i->first;
235 const std::string &value = i->second;
236 lua_pushstring(L, name.c_str());
237 lua_pushlstring(L, value.c_str(), value.size());
241 objectref_get_or_create(L, sender);
242 if(lua_pcall(L, 4, 0, 0))
243 script_error(L, "error: %s", lua_tostring(L, -1));