]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_inventory.cpp
Order es_DrawType exactly like enum NodeDrawType in nodedef.h (#5946)
[dragonfireclient.git] / src / script / cpp_api / s_inventory.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_inventory.h"
21 #include "cpp_api/s_internal.h"
22 #include "inventorymanager.h"
23 #include "lua_api/l_inventory.h"
24 #include "lua_api/l_item.h"
25 #include "log.h"
26
27 // Return number of accepted items to be moved
28 int ScriptApiDetached::detached_inventory_AllowMove(
29                 const std::string &name,
30                 const std::string &from_list, int from_index,
31                 const std::string &to_list, int to_index,
32                 int count, ServerActiveObject *player)
33 {
34         SCRIPTAPI_PRECHECKHEADER
35
36         int error_handler = PUSH_ERROR_HANDLER(L);
37
38         // Push callback function on stack
39         if (!getDetachedInventoryCallback(name, "allow_move"))
40                 return count;
41
42         // function(inv, from_list, from_index, to_list, to_index, count, player)
43         // inv
44         InventoryLocation loc;
45         loc.setDetached(name);
46         InvRef::create(L, loc);
47         lua_pushstring(L, from_list.c_str()); // from_list
48         lua_pushinteger(L, from_index + 1);   // from_index
49         lua_pushstring(L, to_list.c_str());   // to_list
50         lua_pushinteger(L, to_index + 1);     // to_index
51         lua_pushinteger(L, count);            // count
52         objectrefGetOrCreate(L, player);      // player
53         PCALL_RES(lua_pcall(L, 7, 1, error_handler));
54         if(!lua_isnumber(L, -1))
55                 throw LuaError("allow_move should return a number. name=" + name);
56         int ret = luaL_checkinteger(L, -1);
57         lua_pop(L, 2); // Pop integer and error handler
58         return ret;
59 }
60
61 // Return number of accepted items to be put
62 int ScriptApiDetached::detached_inventory_AllowPut(
63                 const std::string &name,
64                 const std::string &listname, int index, ItemStack &stack,
65                 ServerActiveObject *player)
66 {
67         SCRIPTAPI_PRECHECKHEADER
68
69         int error_handler = PUSH_ERROR_HANDLER(L);
70
71         // Push callback function on stack
72         if (!getDetachedInventoryCallback(name, "allow_put"))
73                 return stack.count; // All will be accepted
74
75         // Call function(inv, listname, index, stack, player)
76         InventoryLocation loc;
77         loc.setDetached(name);
78         InvRef::create(L, loc);              // inv
79         lua_pushstring(L, listname.c_str()); // listname
80         lua_pushinteger(L, index + 1);       // index
81         LuaItemStack::create(L, stack);      // stack
82         objectrefGetOrCreate(L, player);     // player
83         PCALL_RES(lua_pcall(L, 5, 1, error_handler));
84         if (!lua_isnumber(L, -1))
85                 throw LuaError("allow_put should return a number. name=" + name);
86         int ret = luaL_checkinteger(L, -1);
87         lua_pop(L, 2); // Pop integer and error handler
88         return ret;
89 }
90
91 // Return number of accepted items to be taken
92 int ScriptApiDetached::detached_inventory_AllowTake(
93                 const std::string &name,
94                 const std::string &listname, int index, ItemStack &stack,
95                 ServerActiveObject *player)
96 {
97         SCRIPTAPI_PRECHECKHEADER
98
99         int error_handler = PUSH_ERROR_HANDLER(L);
100
101         // Push callback function on stack
102         if (!getDetachedInventoryCallback(name, "allow_take"))
103                 return stack.count; // All will be accepted
104
105         // Call function(inv, listname, index, stack, player)
106         InventoryLocation loc;
107         loc.setDetached(name);
108         InvRef::create(L, loc);              // inv
109         lua_pushstring(L, listname.c_str()); // listname
110         lua_pushinteger(L, index + 1);       // index
111         LuaItemStack::create(L, stack);      // stack
112         objectrefGetOrCreate(L, player);     // player
113         PCALL_RES(lua_pcall(L, 5, 1, error_handler));
114         if (!lua_isnumber(L, -1))
115                 throw LuaError("allow_take should return a number. name=" + name);
116         int ret = luaL_checkinteger(L, -1);
117         lua_pop(L, 2); // Pop integer and error handler
118         return ret;
119 }
120
121 // Report moved items
122 void ScriptApiDetached::detached_inventory_OnMove(
123                 const std::string &name,
124                 const std::string &from_list, int from_index,
125                 const std::string &to_list, int to_index,
126                 int count, ServerActiveObject *player)
127 {
128         SCRIPTAPI_PRECHECKHEADER
129
130         int error_handler = PUSH_ERROR_HANDLER(L);
131
132         // Push callback function on stack
133         if (!getDetachedInventoryCallback(name, "on_move"))
134                 return;
135
136         // function(inv, from_list, from_index, to_list, to_index, count, player)
137         // inv
138         InventoryLocation loc;
139         loc.setDetached(name);
140         InvRef::create(L, loc);
141         lua_pushstring(L, from_list.c_str()); // from_list
142         lua_pushinteger(L, from_index + 1);   // from_index
143         lua_pushstring(L, to_list.c_str());   // to_list
144         lua_pushinteger(L, to_index + 1);     // to_index
145         lua_pushinteger(L, count);            // count
146         objectrefGetOrCreate(L, player);      // player
147         PCALL_RES(lua_pcall(L, 7, 0, error_handler));
148         lua_pop(L, 1);  // Pop error handler
149 }
150
151 // Report put items
152 void ScriptApiDetached::detached_inventory_OnPut(
153                 const std::string &name,
154                 const std::string &listname, int index, ItemStack &stack,
155                 ServerActiveObject *player)
156 {
157         SCRIPTAPI_PRECHECKHEADER
158
159         int error_handler = PUSH_ERROR_HANDLER(L);
160
161         // Push callback function on stack
162         if (!getDetachedInventoryCallback(name, "on_put"))
163                 return;
164
165         // Call function(inv, listname, index, stack, player)
166         // inv
167         InventoryLocation loc;
168         loc.setDetached(name);
169         InvRef::create(L, loc);
170         lua_pushstring(L, listname.c_str()); // listname
171         lua_pushinteger(L, index + 1);       // index
172         LuaItemStack::create(L, stack);      // stack
173         objectrefGetOrCreate(L, player);     // player
174         PCALL_RES(lua_pcall(L, 5, 0, error_handler));
175         lua_pop(L, 1);  // Pop error handler
176 }
177
178 // Report taken items
179 void ScriptApiDetached::detached_inventory_OnTake(
180                 const std::string &name,
181                 const std::string &listname, int index, ItemStack &stack,
182                 ServerActiveObject *player)
183 {
184         SCRIPTAPI_PRECHECKHEADER
185
186         int error_handler = PUSH_ERROR_HANDLER(L);
187
188         // Push callback function on stack
189         if (!getDetachedInventoryCallback(name, "on_take"))
190                 return;
191
192         // Call function(inv, listname, index, stack, player)
193         // inv
194         InventoryLocation loc;
195         loc.setDetached(name);
196         InvRef::create(L, loc);
197         lua_pushstring(L, listname.c_str()); // listname
198         lua_pushinteger(L, index + 1);       // index
199         LuaItemStack::create(L, stack);      // stack
200         objectrefGetOrCreate(L, player);     // player
201         PCALL_RES(lua_pcall(L, 5, 0, error_handler));
202         lua_pop(L, 1);  // Pop error handler
203 }
204
205 // Retrieves core.detached_inventories[name][callbackname]
206 // If that is nil or on error, return false and stack is unchanged
207 // If that is a function, returns true and pushes the
208 // function onto the stack
209 bool ScriptApiDetached::getDetachedInventoryCallback(
210                 const std::string &name, const char *callbackname)
211 {
212         lua_State *L = getStack();
213
214         lua_getglobal(L, "core");
215         lua_getfield(L, -1, "detached_inventories");
216         lua_remove(L, -2);
217         luaL_checktype(L, -1, LUA_TTABLE);
218         lua_getfield(L, -1, name.c_str());
219         lua_remove(L, -2);
220         // Should be a table
221         if(lua_type(L, -1) != LUA_TTABLE)
222         {
223                 errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
224                 lua_pop(L, 1);
225                 return false;
226         }
227
228         setOriginFromTable(-1);
229
230         lua_getfield(L, -1, callbackname);
231         lua_remove(L, -2);
232         // Should be a function or nil
233         if(lua_type(L, -1) == LUA_TFUNCTION)
234         {
235                 return true;
236         }
237         else if(lua_isnil(L, -1))
238         {
239                 lua_pop(L, 1);
240                 return false;
241         }
242         else
243         {
244                 errorstream<<"Detached inventory \""<<name<<"\" callback \""
245                         <<callbackname<<"\" is not a function"<<std::endl;
246                 lua_pop(L, 1);
247                 return false;
248         }
249 }