]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
code style fix on src/script/cpp_api/s_client.h
[dragonfireclient.git] / src / script / cpp_api / s_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 "cpp_api/s_node.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "nodedef.h"
25 #include "server.h"
26 #include "environment.h"
27 #include "util/pointedthing.h"
28
29
30 struct EnumString ScriptApiNode::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_GLASSLIKE_FRAMED, "glasslike_framed"},
38                 {NDT_GLASSLIKE_FRAMED_OPTIONAL, "glasslike_framed_optional"},
39                 {NDT_ALLFACES, "allfaces"},
40                 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
41                 {NDT_TORCHLIKE, "torchlike"},
42                 {NDT_SIGNLIKE, "signlike"},
43                 {NDT_PLANTLIKE, "plantlike"},
44                 {NDT_FIRELIKE, "firelike"},
45                 {NDT_FENCELIKE, "fencelike"},
46                 {NDT_RAILLIKE, "raillike"},
47                 {NDT_NODEBOX, "nodebox"},
48                 {NDT_MESH, "mesh"},
49                 {0, NULL},
50         };
51
52 struct EnumString ScriptApiNode::es_ContentParamType2[] =
53         {
54                 {CPT2_NONE, "none"},
55                 {CPT2_FULL, "full"},
56                 {CPT2_FLOWINGLIQUID, "flowingliquid"},
57                 {CPT2_FACEDIR, "facedir"},
58                 {CPT2_WALLMOUNTED, "wallmounted"},
59                 {CPT2_LEVELED, "leveled"},
60                 {CPT2_DEGROTATE, "degrotate"},
61                 {CPT2_MESHOPTIONS, "meshoptions"},
62                 {CPT2_COLOR, "color"},
63                 {CPT2_COLORED_FACEDIR, "colorfacedir"},
64                 {CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
65                 {CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
66                 {0, NULL},
67         };
68
69 struct EnumString ScriptApiNode::es_LiquidType[] =
70         {
71                 {LIQUID_NONE, "none"},
72                 {LIQUID_FLOWING, "flowing"},
73                 {LIQUID_SOURCE, "source"},
74                 {0, NULL},
75         };
76
77 struct EnumString ScriptApiNode::es_ContentParamType[] =
78         {
79                 {CPT_NONE, "none"},
80                 {CPT_LIGHT, "light"},
81                 {0, NULL},
82         };
83
84 struct EnumString ScriptApiNode::es_NodeBoxType[] =
85         {
86                 {NODEBOX_REGULAR, "regular"},
87                 {NODEBOX_FIXED, "fixed"},
88                 {NODEBOX_WALLMOUNTED, "wallmounted"},
89                 {NODEBOX_LEVELED, "leveled"},
90                 {NODEBOX_CONNECTED, "connected"},
91                 {0, NULL},
92         };
93
94 ScriptApiNode::ScriptApiNode() {
95 }
96
97 ScriptApiNode::~ScriptApiNode() {
98 }
99
100 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
101                 ServerActiveObject *puncher, PointedThing pointed)
102 {
103         SCRIPTAPI_PRECHECKHEADER
104
105         int error_handler = PUSH_ERROR_HANDLER(L);
106
107         INodeDefManager *ndef = getServer()->ndef();
108
109         // Push callback function on stack
110         if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
111                 return false;
112
113         // Call function
114         push_v3s16(L, p);
115         pushnode(L, node, ndef);
116         objectrefGetOrCreate(L, puncher);
117         pushPointedThing(pointed);
118         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
119         lua_pop(L, 1);  // Pop error handler
120         return true;
121 }
122
123 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
124                 ServerActiveObject *digger)
125 {
126         SCRIPTAPI_PRECHECKHEADER
127
128         int error_handler = PUSH_ERROR_HANDLER(L);
129
130         INodeDefManager *ndef = getServer()->ndef();
131
132         // Push callback function on stack
133         if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
134                 return false;
135
136         // Call function
137         push_v3s16(L, p);
138         pushnode(L, node, ndef);
139         objectrefGetOrCreate(L, digger);
140         PCALL_RES(lua_pcall(L, 3, 0, error_handler));
141         lua_pop(L, 1);  // Pop error handler
142         return true;
143 }
144
145 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
146 {
147         SCRIPTAPI_PRECHECKHEADER
148
149         int error_handler = PUSH_ERROR_HANDLER(L);
150
151         INodeDefManager *ndef = getServer()->ndef();
152
153         // Push callback function on stack
154         if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
155                 return;
156
157         // Call function
158         push_v3s16(L, p);
159         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
160         lua_pop(L, 1);  // Pop error handler
161 }
162
163 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
164 {
165         SCRIPTAPI_PRECHECKHEADER
166
167         int error_handler = PUSH_ERROR_HANDLER(L);
168
169         INodeDefManager *ndef = getServer()->ndef();
170
171         // Push callback function on stack
172         if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
173                 return;
174
175         // Call function
176         push_v3s16(L, p);
177         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
178         lua_pop(L, 1);  // Pop error handler
179 }
180
181 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
182 {
183         SCRIPTAPI_PRECHECKHEADER
184
185         int error_handler = PUSH_ERROR_HANDLER(L);
186
187         INodeDefManager *ndef = getServer()->ndef();
188
189         // Push callback function on stack
190         if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
191                 return;
192
193         // Call function
194         push_v3s16(L, p);
195         pushnode(L, node, ndef);
196         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
197         lua_pop(L, 1);  // Pop error handler
198 }
199
200 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
201 {
202         SCRIPTAPI_PRECHECKHEADER
203
204         int error_handler = PUSH_ERROR_HANDLER(L);
205
206         INodeDefManager *ndef = getServer()->ndef();
207
208         // Push callback function on stack
209         if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
210                 return false;
211
212         // Call function
213         push_v3s16(L, p);
214         lua_pushnumber(L,dtime);
215         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
216         lua_remove(L, error_handler);
217         return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
218 }
219
220 void ScriptApiNode::node_on_receive_fields(v3s16 p,
221                 const std::string &formname,
222                 const StringMap &fields,
223                 ServerActiveObject *sender)
224 {
225         SCRIPTAPI_PRECHECKHEADER
226
227         int error_handler = PUSH_ERROR_HANDLER(L);
228
229         INodeDefManager *ndef = getServer()->ndef();
230
231         // If node doesn't exist, we don't know what callback to call
232         MapNode node = getEnv()->getMap().getNodeNoEx(p);
233         if (node.getContent() == CONTENT_IGNORE)
234                 return;
235
236         // Push callback function on stack
237         if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
238                 return;
239
240         // Call function
241         push_v3s16(L, p);                    // pos
242         lua_pushstring(L, formname.c_str()); // formname
243         lua_newtable(L);                     // fields
244         StringMap::const_iterator it;
245         for (it = fields.begin(); it != fields.end(); it++) {
246                 const std::string &name = it->first;
247                 const std::string &value = it->second;
248                 lua_pushstring(L, name.c_str());
249                 lua_pushlstring(L, value.c_str(), value.size());
250                 lua_settable(L, -3);
251         }
252         objectrefGetOrCreate(L, sender);        // player
253         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
254         lua_pop(L, 1);  // Pop error handler
255 }
256
257 void ScriptApiNode::node_falling_update(v3s16 p)
258 {
259         SCRIPTAPI_PRECHECKHEADER
260
261         int error_handler = PUSH_ERROR_HANDLER(L);
262
263         lua_getglobal(L, "nodeupdate");
264         push_v3s16(L, p);
265         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
266         lua_pop(L, 1);  // Pop error handler
267 }
268
269 void ScriptApiNode::node_falling_update_single(v3s16 p)
270 {
271         SCRIPTAPI_PRECHECKHEADER
272
273         int error_handler = PUSH_ERROR_HANDLER(L);
274
275         lua_getglobal(L, "nodeupdate_single");
276         push_v3s16(L, p);
277         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
278         lua_pop(L, 1);  // Pop error handler
279 }