]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_node.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[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
28
29 struct EnumString ScriptApiNode::es_DrawType[] =
30         {
31                 {NDT_NORMAL, "normal"},
32                 {NDT_AIRLIKE, "airlike"},
33                 {NDT_LIQUID, "liquid"},
34                 {NDT_FLOWINGLIQUID, "flowingliquid"},
35                 {NDT_GLASSLIKE, "glasslike"},
36                 {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
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 ScriptApiNode::es_ContentParamType2[] =
49         {
50                 {CPT2_NONE, "none"},
51                 {CPT2_FULL, "full"},
52                 {CPT2_FLOWINGLIQUID, "flowingliquid"},
53                 {CPT2_FACEDIR, "facedir"},
54                 {CPT2_WALLMOUNTED, "wallmounted"},
55                 {CPT2_LEVELED, "leveled"},
56                 {0, NULL},
57         };
58
59 struct EnumString ScriptApiNode::es_LiquidType[] =
60         {
61                 {LIQUID_NONE, "none"},
62                 {LIQUID_FLOWING, "flowing"},
63                 {LIQUID_SOURCE, "source"},
64                 {0, NULL},
65         };
66
67 struct EnumString ScriptApiNode::es_ContentParamType[] =
68         {
69                 {CPT_NONE, "none"},
70                 {CPT_LIGHT, "light"},
71                 {0, NULL},
72         };
73
74 struct EnumString ScriptApiNode::es_NodeBoxType[] =
75         {
76                 {NODEBOX_REGULAR, "regular"},
77                 {NODEBOX_FIXED, "fixed"},
78                 {NODEBOX_WALLMOUNTED, "wallmounted"},
79                 {NODEBOX_LEVELED, "leveled"},
80                 {0, NULL},
81         };
82
83 ScriptApiNode::ScriptApiNode() {
84 }
85
86 ScriptApiNode::~ScriptApiNode() {
87 }
88
89 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
90                 ServerActiveObject *puncher)
91 {
92         SCRIPTAPI_PRECHECKHEADER
93
94         INodeDefManager *ndef = getServer()->ndef();
95
96         // Push callback function on stack
97         if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
98                 return false;
99
100         // Call function
101         push_v3s16(L, p);
102         pushnode(L, node, ndef);
103         objectrefGetOrCreate(puncher);
104         if(lua_pcall(L, 3, 0, 0))
105                 scriptError("error: %s", lua_tostring(L, -1));
106         return true;
107 }
108
109 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
110                 ServerActiveObject *digger)
111 {
112         SCRIPTAPI_PRECHECKHEADER
113
114         INodeDefManager *ndef = getServer()->ndef();
115
116         // Push callback function on stack
117         if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
118                 return false;
119
120         // Call function
121         push_v3s16(L, p);
122         pushnode(L, node, ndef);
123         objectrefGetOrCreate(digger);
124         if(lua_pcall(L, 3, 0, 0))
125                 scriptError("error: %s", lua_tostring(L, -1));
126         return true;
127 }
128
129 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
130 {
131         SCRIPTAPI_PRECHECKHEADER
132
133         INodeDefManager *ndef = getServer()->ndef();
134
135         // Push callback function on stack
136         if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
137                 return;
138
139         // Call function
140         push_v3s16(L, p);
141         if(lua_pcall(L, 1, 0, 0))
142                 scriptError("error: %s", lua_tostring(L, -1));
143 }
144
145 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
146 {
147         SCRIPTAPI_PRECHECKHEADER
148
149         INodeDefManager *ndef = getServer()->ndef();
150
151         // Push callback function on stack
152         if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
153                 return;
154
155         // Call function
156         push_v3s16(L, p);
157         if(lua_pcall(L, 1, 0, 0))
158                 scriptError("error: %s", lua_tostring(L, -1));
159 }
160
161 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
162 {
163         SCRIPTAPI_PRECHECKHEADER
164
165         INodeDefManager *ndef = getServer()->ndef();
166
167         // Push callback function on stack
168         if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
169                 return;
170
171         // Call function
172         push_v3s16(L, p);
173         pushnode(L, node, ndef);
174         if(lua_pcall(L, 2, 0, 0))
175                 scriptError("error: %s", lua_tostring(L, -1));
176 }
177
178 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
179 {
180         SCRIPTAPI_PRECHECKHEADER
181
182         INodeDefManager *ndef = getServer()->ndef();
183
184         // Push callback function on stack
185         if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
186                 return false;
187
188         // Call function
189         push_v3s16(L, p);
190         lua_pushnumber(L,dtime);
191         if(lua_pcall(L, 2, 1, 0))
192                 scriptError("error: %s", lua_tostring(L, -1));
193         if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
194                 return true;
195
196         return false;
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         // param 1
219         push_v3s16(L, p);
220         // param 2
221         lua_pushstring(L, formname.c_str());
222         // param 3
223         lua_newtable(L);
224         for(std::map<std::string, std::string>::const_iterator
225                         i = fields.begin(); i != fields.end(); i++){
226                 const std::string &name = i->first;
227                 const std::string &value = i->second;
228                 lua_pushstring(L, name.c_str());
229                 lua_pushlstring(L, value.c_str(), value.size());
230                 lua_settable(L, -3);
231         }
232         // param 4
233         objectrefGetOrCreate(sender);
234         if(lua_pcall(L, 4, 0, 0))
235                 scriptError("error: %s", lua_tostring(L, -1));
236 }
237
238 void ScriptApiNode::node_falling_update(v3s16 p)
239 {
240         SCRIPTAPI_PRECHECKHEADER
241         lua_getglobal(L, "nodeupdate");
242         push_v3s16(L, p);
243         if(lua_pcall(L, 1, 0, 0))
244                 scriptError("error: %s", lua_tostring(L, -1));
245 }
246
247 void ScriptApiNode::node_falling_update_single(v3s16 p)
248 {
249         SCRIPTAPI_PRECHECKHEADER
250         lua_getglobal(L, "nodeupdate_single");
251         push_v3s16(L, p);
252         if(lua_pcall(L, 1, 0, 0))
253                 scriptError("error: %s", lua_tostring(L, -1));
254 }