]> git.lizzy.rs Git - dragonfireclient.git/blob - src/scriptapi_node.cpp
split scriptapi.cpp
[dragonfireclient.git] / src / scriptapi_node.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "scriptapi.h"
21 #include "scriptapi_node.h"
22 #include "util/pointedthing.h"
23 #include "script.h"
24 #include "scriptapi_common.h"
25 #include "scriptapi_types.h"
26 #include "scriptapi_item.h"
27 #include "scriptapi_object.h"
28
29
30 struct EnumString es_DrawType[] =
31 {
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"},
45         {0, NULL},
46 };
47
48 struct EnumString es_ContentParamType[] =
49 {
50         {CPT_NONE, "none"},
51         {CPT_LIGHT, "light"},
52         {0, NULL},
53 };
54
55 struct EnumString es_ContentParamType2[] =
56 {
57         {CPT2_NONE, "none"},
58         {CPT2_FULL, "full"},
59         {CPT2_FLOWINGLIQUID, "flowingliquid"},
60         {CPT2_FACEDIR, "facedir"},
61         {CPT2_WALLMOUNTED, "wallmounted"},
62         {0, NULL},
63 };
64
65 struct EnumString es_LiquidType[] =
66 {
67         {LIQUID_NONE, "none"},
68         {LIQUID_FLOWING, "flowing"},
69         {LIQUID_SOURCE, "source"},
70         {0, NULL},
71 };
72
73 struct EnumString es_NodeBoxType[] =
74 {
75         {NODEBOX_REGULAR, "regular"},
76         {NODEBOX_FIXED, "fixed"},
77         {NODEBOX_WALLMOUNTED, "wallmounted"},
78         {0, NULL},
79 };
80
81
82 bool scriptapi_node_on_punch(lua_State *L, v3s16 p, MapNode node,
83                 ServerActiveObject *puncher)
84 {
85         realitycheck(L);
86         assert(lua_checkstack(L, 20));
87         StackUnroller stack_unroller(L);
88
89         INodeDefManager *ndef = get_server(L)->ndef();
90
91         // Push callback function on stack
92         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_punch"))
93                 return false;
94
95         // Call function
96         push_v3s16(L, p);
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));
101         return true;
102 }
103
104 bool scriptapi_node_on_dig(lua_State *L, v3s16 p, MapNode node,
105                 ServerActiveObject *digger)
106 {
107         realitycheck(L);
108         assert(lua_checkstack(L, 20));
109         StackUnroller stack_unroller(L);
110
111         INodeDefManager *ndef = get_server(L)->ndef();
112
113         // Push callback function on stack
114         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_dig"))
115                 return false;
116
117         // Call function
118         push_v3s16(L, p);
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));
123         return true;
124 }
125
126 void scriptapi_node_on_construct(lua_State *L, v3s16 p, MapNode node)
127 {
128         realitycheck(L);
129         assert(lua_checkstack(L, 20));
130         StackUnroller stack_unroller(L);
131
132         INodeDefManager *ndef = get_server(L)->ndef();
133
134         // Push callback function on stack
135         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_construct"))
136                 return;
137
138         // Call function
139         push_v3s16(L, p);
140         if(lua_pcall(L, 1, 0, 0))
141                 script_error(L, "error: %s", lua_tostring(L, -1));
142 }
143
144 void scriptapi_node_on_destruct(lua_State *L, v3s16 p, MapNode node)
145 {
146         realitycheck(L);
147         assert(lua_checkstack(L, 20));
148         StackUnroller stack_unroller(L);
149
150         INodeDefManager *ndef = get_server(L)->ndef();
151
152         // Push callback function on stack
153         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_destruct"))
154                 return;
155
156         // Call function
157         push_v3s16(L, p);
158         if(lua_pcall(L, 1, 0, 0))
159                 script_error(L, "error: %s", lua_tostring(L, -1));
160 }
161
162 void scriptapi_node_after_destruct(lua_State *L, v3s16 p, MapNode node)
163 {
164         realitycheck(L);
165         assert(lua_checkstack(L, 20));
166         StackUnroller stack_unroller(L);
167
168         INodeDefManager *ndef = get_server(L)->ndef();
169
170         // Push callback function on stack
171         if(!get_item_callback(L, ndef->get(node).name.c_str(), "after_destruct"))
172                 return;
173
174         // Call function
175         push_v3s16(L, p);
176         pushnode(L, node, ndef);
177         if(lua_pcall(L, 2, 0, 0))
178                 script_error(L, "error: %s", lua_tostring(L, -1));
179 }
180
181 bool scriptapi_node_on_timer(lua_State *L, v3s16 p, MapNode node, f32 dtime)
182 {
183         realitycheck(L);
184         assert(lua_checkstack(L, 20));
185         StackUnroller stack_unroller(L);
186
187         INodeDefManager *ndef = get_server(L)->ndef();
188
189         // Push callback function on stack
190         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_timer"))
191                 return false;
192
193         // Call function
194         push_v3s16(L, p);
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)
199                 return true;
200
201         return false;
202 }
203
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)
208 {
209         realitycheck(L);
210         assert(lua_checkstack(L, 20));
211         StackUnroller stack_unroller(L);
212
213         INodeDefManager *ndef = get_server(L)->ndef();
214
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)
218                 return;
219
220         // Push callback function on stack
221         if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_receive_fields"))
222                 return;
223
224         // Call function
225         // param 1
226         push_v3s16(L, p);
227         // param 2
228         lua_pushstring(L, formname.c_str());
229         // param 3
230         lua_newtable(L);
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());
237                 lua_settable(L, -3);
238         }
239         // param 4
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));
243 }