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