]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_nodemeta.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / script / cpp_api / s_nodemeta.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_nodemeta.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "nodedef.h"
24 #include "mapnode.h"
25 #include "server.h"
26 #include "environment.h"
27 #include "lua_api/l_item.h"
28
29 // Return number of accepted items to be moved
30 int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
31                 const std::string &from_list, int from_index,
32                 const std::string &to_list, int to_index,
33                 int count, ServerActiveObject *player)
34 {
35         SCRIPTAPI_PRECHECKHEADER
36
37         INodeDefManager *ndef = getServer()->ndef();
38
39         // If node doesn't exist, we don't know what callback to call
40         MapNode node = getEnv()->getMap().getNodeNoEx(p);
41         if(node.getContent() == CONTENT_IGNORE)
42                 return 0;
43
44         // Push callback function on stack
45         if(!getItemCallback(ndef->get(node).name.c_str(),
46                         "allow_metadata_inventory_move"))
47                 return count;
48
49         // function(pos, from_list, from_index, to_list, to_index, count, player)
50         // pos
51         push_v3s16(L, p);
52         // from_list
53         lua_pushstring(L, from_list.c_str());
54         // from_index
55         lua_pushinteger(L, from_index + 1);
56         // to_list
57         lua_pushstring(L, to_list.c_str());
58         // to_index
59         lua_pushinteger(L, to_index + 1);
60         // count
61         lua_pushinteger(L, count);
62         // player
63         objectrefGetOrCreate(player);
64         if(lua_pcall(L, 7, 1, 0))
65                 scriptError("error: %s", lua_tostring(L, -1));
66         if(!lua_isnumber(L, -1))
67                 throw LuaError(L, "allow_metadata_inventory_move should return a number");
68         return luaL_checkinteger(L, -1);
69 }
70
71 // Return number of accepted items to be put
72 int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
73                 const std::string &listname, int index, ItemStack &stack,
74                 ServerActiveObject *player)
75 {
76         SCRIPTAPI_PRECHECKHEADER
77
78         INodeDefManager *ndef = getServer()->ndef();
79
80         // If node doesn't exist, we don't know what callback to call
81         MapNode node = getEnv()->getMap().getNodeNoEx(p);
82         if(node.getContent() == CONTENT_IGNORE)
83                 return 0;
84
85         // Push callback function on stack
86         if(!getItemCallback(ndef->get(node).name.c_str(),
87                         "allow_metadata_inventory_put"))
88                 return stack.count;
89
90         // Call function(pos, listname, index, stack, player)
91         // pos
92         push_v3s16(L, p);
93         // listname
94         lua_pushstring(L, listname.c_str());
95         // index
96         lua_pushinteger(L, index + 1);
97         // stack
98         LuaItemStack::create(L, stack);
99         // player
100         objectrefGetOrCreate(player);
101         if(lua_pcall(L, 5, 1, 0))
102                 scriptError("error: %s", lua_tostring(L, -1));
103         if(!lua_isnumber(L, -1))
104                 throw LuaError(L, "allow_metadata_inventory_put should return a number");
105         return luaL_checkinteger(L, -1);
106 }
107
108 // Return number of accepted items to be taken
109 int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
110                 const std::string &listname, int index, ItemStack &stack,
111                 ServerActiveObject *player)
112 {
113         SCRIPTAPI_PRECHECKHEADER
114
115         INodeDefManager *ndef = getServer()->ndef();
116
117         // If node doesn't exist, we don't know what callback to call
118         MapNode node = getEnv()->getMap().getNodeNoEx(p);
119         if(node.getContent() == CONTENT_IGNORE)
120                 return 0;
121
122         // Push callback function on stack
123         if(!getItemCallback(ndef->get(node).name.c_str(),
124                         "allow_metadata_inventory_take"))
125                 return stack.count;
126
127         // Call function(pos, listname, index, count, player)
128         // pos
129         push_v3s16(L, p);
130         // listname
131         lua_pushstring(L, listname.c_str());
132         // index
133         lua_pushinteger(L, index + 1);
134         // stack
135         LuaItemStack::create(L, stack);
136         // player
137         objectrefGetOrCreate(player);
138         if(lua_pcall(L, 5, 1, 0))
139                 scriptError("error: %s", lua_tostring(L, -1));
140         if(!lua_isnumber(L, -1))
141                 throw LuaError(L, "allow_metadata_inventory_take should return a number");
142         return luaL_checkinteger(L, -1);
143 }
144
145 // Report moved items
146 void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
147                 const std::string &from_list, int from_index,
148                 const std::string &to_list, int to_index,
149                 int count, ServerActiveObject *player)
150 {
151         SCRIPTAPI_PRECHECKHEADER
152
153         INodeDefManager *ndef = getServer()->ndef();
154
155         // If node doesn't exist, we don't know what callback to call
156         MapNode node = getEnv()->getMap().getNodeNoEx(p);
157         if(node.getContent() == CONTENT_IGNORE)
158                 return;
159
160         // Push callback function on stack
161         if(!getItemCallback(ndef->get(node).name.c_str(),
162                         "on_metadata_inventory_move"))
163                 return;
164
165         // function(pos, from_list, from_index, to_list, to_index, count, player)
166         // pos
167         push_v3s16(L, p);
168         // from_list
169         lua_pushstring(L, from_list.c_str());
170         // from_index
171         lua_pushinteger(L, from_index + 1);
172         // to_list
173         lua_pushstring(L, to_list.c_str());
174         // to_index
175         lua_pushinteger(L, to_index + 1);
176         // count
177         lua_pushinteger(L, count);
178         // player
179         objectrefGetOrCreate(player);
180         if(lua_pcall(L, 7, 0, 0))
181                 scriptError("error: %s", lua_tostring(L, -1));
182 }
183
184 // Report put items
185 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
186                 const std::string &listname, int index, ItemStack &stack,
187                 ServerActiveObject *player)
188 {
189         SCRIPTAPI_PRECHECKHEADER
190
191         INodeDefManager *ndef = getServer()->ndef();
192
193         // If node doesn't exist, we don't know what callback to call
194         MapNode node = getEnv()->getMap().getNodeNoEx(p);
195         if(node.getContent() == CONTENT_IGNORE)
196                 return;
197
198         // Push callback function on stack
199         if(!getItemCallback(ndef->get(node).name.c_str(),
200                         "on_metadata_inventory_put"))
201                 return;
202
203         // Call function(pos, listname, index, stack, player)
204         // pos
205         push_v3s16(L, p);
206         // listname
207         lua_pushstring(L, listname.c_str());
208         // index
209         lua_pushinteger(L, index + 1);
210         // stack
211         LuaItemStack::create(L, stack);
212         // player
213         objectrefGetOrCreate(player);
214         if(lua_pcall(L, 5, 0, 0))
215                 scriptError("error: %s", lua_tostring(L, -1));
216 }
217
218 // Report taken items
219 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
220                 const std::string &listname, int index, ItemStack &stack,
221                 ServerActiveObject *player)
222 {
223         SCRIPTAPI_PRECHECKHEADER
224
225         INodeDefManager *ndef = getServer()->ndef();
226
227         // If node doesn't exist, we don't know what callback to call
228         MapNode node = getEnv()->getMap().getNodeNoEx(p);
229         if(node.getContent() == CONTENT_IGNORE)
230                 return;
231
232         // Push callback function on stack
233         if(!getItemCallback(ndef->get(node).name.c_str(),
234                         "on_metadata_inventory_take"))
235                 return;
236
237         // Call function(pos, listname, index, stack, player)
238         // pos
239         push_v3s16(L, p);
240         // listname
241         lua_pushstring(L, listname.c_str());
242         // index
243         lua_pushinteger(L, index + 1);
244         // stack
245         LuaItemStack::create(L, stack);
246         // player
247         objectrefGetOrCreate(player);
248         if(lua_pcall(L, 5, 0, 0))
249                 scriptError("error: %s", lua_tostring(L, -1));
250 }
251
252 ScriptApiNodemeta::ScriptApiNodemeta() {
253 }
254
255 ScriptApiNodemeta::~ScriptApiNodemeta() {
256 }
257
258
259