]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
17f0f0dac3be395ba7b0cd049351534ad2dcaf17
[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                 {0, NULL},
62         };
63
64 struct EnumString ScriptApiNode::es_LiquidType[] =
65         {
66                 {LIQUID_NONE, "none"},
67                 {LIQUID_FLOWING, "flowing"},
68                 {LIQUID_SOURCE, "source"},
69                 {0, NULL},
70         };
71
72 struct EnumString ScriptApiNode::es_ContentParamType[] =
73         {
74                 {CPT_NONE, "none"},
75                 {CPT_LIGHT, "light"},
76                 {0, NULL},
77         };
78
79 struct EnumString ScriptApiNode::es_NodeBoxType[] =
80         {
81                 {NODEBOX_REGULAR, "regular"},
82                 {NODEBOX_FIXED, "fixed"},
83                 {NODEBOX_WALLMOUNTED, "wallmounted"},
84                 {NODEBOX_LEVELED, "leveled"},
85                 {NODEBOX_CONNECTED, "connected"},
86                 {0, NULL},
87         };
88
89 ScriptApiNode::ScriptApiNode() {
90 }
91
92 ScriptApiNode::~ScriptApiNode() {
93 }
94
95 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
96                 ServerActiveObject *puncher, PointedThing pointed)
97 {
98         SCRIPTAPI_PRECHECKHEADER
99
100         int error_handler = PUSH_ERROR_HANDLER(L);
101
102         INodeDefManager *ndef = getServer()->ndef();
103
104         // Push callback function on stack
105         if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
106                 return false;
107
108         // Call function
109         push_v3s16(L, p);
110         pushnode(L, node, ndef);
111         objectrefGetOrCreate(L, puncher);
112         pushPointedThing(pointed);
113         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
114         lua_pop(L, 1);  // Pop error handler
115         return true;
116 }
117
118 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
119                 ServerActiveObject *digger)
120 {
121         SCRIPTAPI_PRECHECKHEADER
122
123         int error_handler = PUSH_ERROR_HANDLER(L);
124
125         INodeDefManager *ndef = getServer()->ndef();
126
127         // Push callback function on stack
128         if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
129                 return false;
130
131         // Call function
132         push_v3s16(L, p);
133         pushnode(L, node, ndef);
134         objectrefGetOrCreate(L, digger);
135         PCALL_RES(lua_pcall(L, 3, 0, error_handler));
136         lua_pop(L, 1);  // Pop error handler
137         return true;
138 }
139
140 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
141 {
142         SCRIPTAPI_PRECHECKHEADER
143
144         int error_handler = PUSH_ERROR_HANDLER(L);
145
146         INodeDefManager *ndef = getServer()->ndef();
147
148         // Push callback function on stack
149         if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
150                 return;
151
152         // Call function
153         push_v3s16(L, p);
154         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
155         lua_pop(L, 1);  // Pop error handler
156 }
157
158 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
159 {
160         SCRIPTAPI_PRECHECKHEADER
161
162         int error_handler = PUSH_ERROR_HANDLER(L);
163
164         INodeDefManager *ndef = getServer()->ndef();
165
166         // Push callback function on stack
167         if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
168                 return;
169
170         // Call function
171         push_v3s16(L, p);
172         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
173         lua_pop(L, 1);  // Pop error handler
174 }
175
176 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
177 {
178         SCRIPTAPI_PRECHECKHEADER
179
180         int error_handler = PUSH_ERROR_HANDLER(L);
181
182         INodeDefManager *ndef = getServer()->ndef();
183
184         // Push callback function on stack
185         if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
186                 return;
187
188         // Call function
189         push_v3s16(L, p);
190         pushnode(L, node, ndef);
191         PCALL_RES(lua_pcall(L, 2, 0, error_handler));
192         lua_pop(L, 1);  // Pop error handler
193 }
194
195 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
196 {
197         SCRIPTAPI_PRECHECKHEADER
198
199         int error_handler = PUSH_ERROR_HANDLER(L);
200
201         INodeDefManager *ndef = getServer()->ndef();
202
203         // Push callback function on stack
204         if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
205                 return false;
206
207         // Call function
208         push_v3s16(L, p);
209         lua_pushnumber(L,dtime);
210         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
211         lua_remove(L, error_handler);
212         return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
213 }
214
215 void ScriptApiNode::node_on_receive_fields(v3s16 p,
216                 const std::string &formname,
217                 const StringMap &fields,
218                 ServerActiveObject *sender)
219 {
220         SCRIPTAPI_PRECHECKHEADER
221
222         int error_handler = PUSH_ERROR_HANDLER(L);
223
224         INodeDefManager *ndef = getServer()->ndef();
225
226         // If node doesn't exist, we don't know what callback to call
227         MapNode node = getEnv()->getMap().getNodeNoEx(p);
228         if (node.getContent() == CONTENT_IGNORE)
229                 return;
230
231         // Push callback function on stack
232         if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
233                 return;
234
235         // Call function
236         push_v3s16(L, p);                    // pos
237         lua_pushstring(L, formname.c_str()); // formname
238         lua_newtable(L);                     // fields
239         StringMap::const_iterator it;
240         for (it = fields.begin(); it != fields.end(); it++) {
241                 const std::string &name = it->first;
242                 const std::string &value = it->second;
243                 lua_pushstring(L, name.c_str());
244                 lua_pushlstring(L, value.c_str(), value.size());
245                 lua_settable(L, -3);
246         }
247         objectrefGetOrCreate(L, sender);        // player
248         PCALL_RES(lua_pcall(L, 4, 0, error_handler));
249         lua_pop(L, 1);  // Pop error handler
250 }
251
252 void ScriptApiNode::node_falling_update(v3s16 p)
253 {
254         SCRIPTAPI_PRECHECKHEADER
255
256         int error_handler = PUSH_ERROR_HANDLER(L);
257
258         lua_getglobal(L, "nodeupdate");
259         push_v3s16(L, p);
260         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
261         lua_pop(L, 1);  // Pop error handler
262 }
263
264 void ScriptApiNode::node_falling_update_single(v3s16 p)
265 {
266         SCRIPTAPI_PRECHECKHEADER
267
268         int error_handler = PUSH_ERROR_HANDLER(L);
269
270         lua_getglobal(L, "nodeupdate_single");
271         push_v3s16(L, p);
272         PCALL_RES(lua_pcall(L, 1, 0, error_handler));
273         lua_pop(L, 1);  // Pop error handler
274 }