3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
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"
27 // Return number of accepted items to be moved
28 int ScriptApiDetached::detached_inventory_AllowMove(
29 const MoveAction &ma, int count,
30 ServerActiveObject *player)
32 SCRIPTAPI_PRECHECKHEADER
34 int error_handler = PUSH_ERROR_HANDLER(L);
36 // Push callback function on stack
37 if (!getDetachedInventoryCallback(ma.from_inv.name, "allow_move"))
40 // function(inv, from_list, from_index, to_list, to_index, count, player)
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
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)
62 SCRIPTAPI_PRECHECKHEADER
64 int error_handler = PUSH_ERROR_HANDLER(L);
66 // Push callback function on stack
67 if (!getDetachedInventoryCallback(ma.to_inv.name, "allow_put"))
68 return stack.count; // All will be accepted
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
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)
89 SCRIPTAPI_PRECHECKHEADER
91 int error_handler = PUSH_ERROR_HANDLER(L);
93 // Push callback function on stack
94 if (!getDetachedInventoryCallback(ma.from_inv.name, "allow_take"))
95 return stack.count; // All will be accepted
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
111 // Report moved items
112 void ScriptApiDetached::detached_inventory_OnMove(
113 const MoveAction &ma, int count,
114 ServerActiveObject *player)
116 SCRIPTAPI_PRECHECKHEADER
118 int error_handler = PUSH_ERROR_HANDLER(L);
120 // Push callback function on stack
121 if (!getDetachedInventoryCallback(ma.from_inv.name, "on_move"))
124 // function(inv, from_list, from_index, to_list, to_index, count, player)
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
138 void ScriptApiDetached::detached_inventory_OnPut(
139 const MoveAction &ma, const ItemStack &stack,
140 ServerActiveObject *player)
142 SCRIPTAPI_PRECHECKHEADER
144 int error_handler = PUSH_ERROR_HANDLER(L);
146 // Push callback function on stack
147 if (!getDetachedInventoryCallback(ma.to_inv.name, "on_put"))
150 // Call function(inv, listname, index, stack, player)
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
161 // Report taken items
162 void ScriptApiDetached::detached_inventory_OnTake(
163 const MoveAction &ma, const ItemStack &stack,
164 ServerActiveObject *player)
166 SCRIPTAPI_PRECHECKHEADER
168 int error_handler = PUSH_ERROR_HANDLER(L);
170 // Push callback function on stack
171 if (!getDetachedInventoryCallback(ma.from_inv.name, "on_take"))
174 // Call function(inv, listname, index, stack, player)
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
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)
192 lua_State *L = getStack();
194 lua_getglobal(L, "core");
195 lua_getfield(L, -1, "detached_inventories");
197 luaL_checktype(L, -1, LUA_TTABLE);
198 lua_getfield(L, -1, name.c_str());
201 if (lua_type(L, -1) != LUA_TTABLE) {
202 errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
207 setOriginFromTable(-1);
209 lua_getfield(L, -1, callbackname);
211 // Should be a function or nil
212 if (lua_type(L, -1) == LUA_TFUNCTION) {
216 if (lua_isnil(L, -1)) {
221 errorstream << "Detached inventory \"" << name << "\" callback \""
222 << callbackname << "\" is not a function" << std::endl;