]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_node.cpp
Move scriptapi to separate folder (by sapier)
[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 "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "nodedef.h"
24 #include "server.h"
25
26
27 struct EnumString ScriptApiNode::es_DrawType[] =
28         {
29                 {NDT_NORMAL, "normal"},
30                 {NDT_AIRLIKE, "airlike"},
31                 {NDT_LIQUID, "liquid"},
32                 {NDT_FLOWINGLIQUID, "flowingliquid"},
33                 {NDT_GLASSLIKE, "glasslike"},
34                 {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
35                 {NDT_ALLFACES, "allfaces"},
36                 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
37                 {NDT_TORCHLIKE, "torchlike"},
38                 {NDT_SIGNLIKE, "signlike"},
39                 {NDT_PLANTLIKE, "plantlike"},
40                 {NDT_FENCELIKE, "fencelike"},
41                 {NDT_RAILLIKE, "raillike"},
42                 {NDT_NODEBOX, "nodebox"},
43                 {0, NULL},
44         };
45
46 struct EnumString ScriptApiNode::es_ContentParamType2[] =
47         {
48                 {CPT2_NONE, "none"},
49                 {CPT2_FULL, "full"},
50                 {CPT2_FLOWINGLIQUID, "flowingliquid"},
51                 {CPT2_FACEDIR, "facedir"},
52                 {CPT2_WALLMOUNTED, "wallmounted"},
53                 {0, NULL},
54         };
55
56 struct EnumString ScriptApiNode::es_LiquidType[] =
57         {
58                 {LIQUID_NONE, "none"},
59                 {LIQUID_FLOWING, "flowing"},
60                 {LIQUID_SOURCE, "source"},
61                 {0, NULL},
62         };
63
64 struct EnumString ScriptApiNode::es_ContentParamType[] =
65         {
66                 {CPT_NONE, "none"},
67                 {CPT_LIGHT, "light"},
68                 {0, NULL},
69         };
70
71 struct EnumString ScriptApiNode::es_NodeBoxType[] =
72         {
73                 {NODEBOX_REGULAR, "regular"},
74                 {NODEBOX_FIXED, "fixed"},
75                 {NODEBOX_WALLMOUNTED, "wallmounted"},
76                 {0, NULL},
77         };
78
79 ScriptApiNode::ScriptApiNode() {
80 }
81
82 ScriptApiNode::~ScriptApiNode() {
83 }
84
85 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
86                 ServerActiveObject *puncher)
87 {
88         SCRIPTAPI_PRECHECKHEADER
89
90         INodeDefManager *ndef = getServer()->ndef();
91
92         // Push callback function on stack
93         if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
94                 return false;
95
96         // Call function
97         push_v3s16(L, p);
98         pushnode(L, node, ndef);
99         objectrefGetOrCreate(puncher);
100         if(lua_pcall(L, 3, 0, 0))
101                 scriptError("error: %s", lua_tostring(L, -1));
102         return true;
103 }
104
105 bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
106                 ServerActiveObject *digger)
107 {
108         SCRIPTAPI_PRECHECKHEADER
109
110         INodeDefManager *ndef = getServer()->ndef();
111
112         // Push callback function on stack
113         if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
114                 return false;
115
116         // Call function
117         push_v3s16(L, p);
118         pushnode(L, node, ndef);
119         objectrefGetOrCreate(digger);
120         if(lua_pcall(L, 3, 0, 0))
121                 scriptError("error: %s", lua_tostring(L, -1));
122         return true;
123 }
124
125 void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
126 {
127         SCRIPTAPI_PRECHECKHEADER
128
129         INodeDefManager *ndef = getServer()->ndef();
130
131         // Push callback function on stack
132         if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
133                 return;
134
135         // Call function
136         push_v3s16(L, p);
137         if(lua_pcall(L, 1, 0, 0))
138                 scriptError("error: %s", lua_tostring(L, -1));
139 }
140
141 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
142 {
143         SCRIPTAPI_PRECHECKHEADER
144
145         INodeDefManager *ndef = getServer()->ndef();
146
147         // Push callback function on stack
148         if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
149                 return;
150
151         // Call function
152         push_v3s16(L, p);
153         if(lua_pcall(L, 1, 0, 0))
154                 scriptError("error: %s", lua_tostring(L, -1));
155 }
156
157 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
158 {
159         SCRIPTAPI_PRECHECKHEADER
160
161         INodeDefManager *ndef = getServer()->ndef();
162
163         // Push callback function on stack
164         if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
165                 return;
166
167         // Call function
168         push_v3s16(L, p);
169         pushnode(L, node, ndef);
170         if(lua_pcall(L, 2, 0, 0))
171                 scriptError("error: %s", lua_tostring(L, -1));
172 }
173
174 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
175 {
176         SCRIPTAPI_PRECHECKHEADER
177
178         INodeDefManager *ndef = getServer()->ndef();
179
180         // Push callback function on stack
181         if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
182                 return false;
183
184         // Call function
185         push_v3s16(L, p);
186         lua_pushnumber(L,dtime);
187         if(lua_pcall(L, 2, 1, 0))
188                 scriptError("error: %s", lua_tostring(L, -1));
189         if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
190                 return true;
191
192         return false;
193 }
194
195 void ScriptApiNode::node_on_receive_fields(v3s16 p,
196                 const std::string &formname,
197                 const std::map<std::string, std::string> &fields,
198                 ServerActiveObject *sender)
199 {
200         SCRIPTAPI_PRECHECKHEADER
201
202         INodeDefManager *ndef = getServer()->ndef();
203
204         // If node doesn't exist, we don't know what callback to call
205         MapNode node = getEnv()->getMap().getNodeNoEx(p);
206         if(node.getContent() == CONTENT_IGNORE)
207                 return;
208
209         // Push callback function on stack
210         if(!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
211                 return;
212
213         // Call function
214         // param 1
215         push_v3s16(L, p);
216         // param 2
217         lua_pushstring(L, formname.c_str());
218         // param 3
219         lua_newtable(L);
220         for(std::map<std::string, std::string>::const_iterator
221                         i = fields.begin(); i != fields.end(); i++){
222                 const std::string &name = i->first;
223                 const std::string &value = i->second;
224                 lua_pushstring(L, name.c_str());
225                 lua_pushlstring(L, value.c_str(), value.size());
226                 lua_settable(L, -3);
227         }
228         // param 4
229         objectrefGetOrCreate(sender);
230         if(lua_pcall(L, 4, 0, 0))
231                 scriptError("error: %s", lua_tostring(L, -1));
232 }
233