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