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 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)
34 SCRIPTAPI_PRECHECKHEADER
36 int error_handler = PUSH_ERROR_HANDLER(L);
38 // Push callback function on stack
39 if (!getDetachedInventoryCallback(name, "allow_move"))
42 // function(inv, from_list, from_index, to_list, to_index, count, player)
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
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)
67 SCRIPTAPI_PRECHECKHEADER
69 int error_handler = PUSH_ERROR_HANDLER(L);
71 // Push callback function on stack
72 if (!getDetachedInventoryCallback(name, "allow_put"))
73 return stack.count; // All will be accepted
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
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)
97 SCRIPTAPI_PRECHECKHEADER
99 int error_handler = PUSH_ERROR_HANDLER(L);
101 // Push callback function on stack
102 if (!getDetachedInventoryCallback(name, "allow_take"))
103 return stack.count; // All will be accepted
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
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)
128 SCRIPTAPI_PRECHECKHEADER
130 int error_handler = PUSH_ERROR_HANDLER(L);
132 // Push callback function on stack
133 if (!getDetachedInventoryCallback(name, "on_move"))
136 // function(inv, from_list, from_index, to_list, to_index, count, player)
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
152 void ScriptApiDetached::detached_inventory_OnPut(
153 const std::string &name,
154 const std::string &listname, int index, ItemStack &stack,
155 ServerActiveObject *player)
157 SCRIPTAPI_PRECHECKHEADER
159 int error_handler = PUSH_ERROR_HANDLER(L);
161 // Push callback function on stack
162 if (!getDetachedInventoryCallback(name, "on_put"))
165 // Call function(inv, listname, index, stack, player)
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
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)
184 SCRIPTAPI_PRECHECKHEADER
186 int error_handler = PUSH_ERROR_HANDLER(L);
188 // Push callback function on stack
189 if (!getDetachedInventoryCallback(name, "on_take"))
192 // Call function(inv, listname, index, stack, player)
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
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)
212 lua_State *L = getStack();
214 lua_getglobal(L, "core");
215 lua_getfield(L, -1, "detached_inventories");
217 luaL_checktype(L, -1, LUA_TTABLE);
218 lua_getfield(L, -1, name.c_str());
221 if (lua_type(L, -1) != LUA_TTABLE) {
222 errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
227 setOriginFromTable(-1);
229 lua_getfield(L, -1, callbackname);
231 // Should be a function or nil
232 if (lua_type(L, -1) == LUA_TFUNCTION) {
236 if (lua_isnil(L, -1)) {
241 errorstream << "Detached inventory \"" << name << "\" callback \""
242 << callbackname << "\" is not a function" << std::endl;