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