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