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 // Push callback function on stack
37 if (!getDetachedInventoryCallback(name, "allow_move"))
40 // function(inv, from_list, from_index, to_list, to_index, count, player)
42 InventoryLocation loc;
43 loc.setDetached(name);
44 InvRef::create(L, loc);
45 lua_pushstring(L, from_list.c_str()); // from_list
46 lua_pushinteger(L, from_index + 1); // from_index
47 lua_pushstring(L, to_list.c_str()); // to_list
48 lua_pushinteger(L, to_index + 1); // to_index
49 lua_pushinteger(L, count); // count
50 objectrefGetOrCreate(player); // player
51 if (lua_pcall(L, 7, 1, m_errorhandler))
53 if(!lua_isnumber(L, -1))
54 throw LuaError("allow_move should return a number. name=" + name);
55 int ret = luaL_checkinteger(L, -1);
56 lua_pop(L, 1); // Pop integer
60 // Return number of accepted items to be put
61 int ScriptApiDetached::detached_inventory_AllowPut(
62 const std::string &name,
63 const std::string &listname, int index, ItemStack &stack,
64 ServerActiveObject *player)
66 SCRIPTAPI_PRECHECKHEADER
68 // Push callback function on stack
69 if (!getDetachedInventoryCallback(name, "allow_put"))
70 return stack.count; // All will be accepted
72 // Call function(inv, listname, index, stack, player)
73 InventoryLocation loc;
74 loc.setDetached(name);
75 InvRef::create(L, loc); // inv
76 lua_pushstring(L, listname.c_str()); // listname
77 lua_pushinteger(L, index + 1); // index
78 LuaItemStack::create(L, stack); // stack
79 objectrefGetOrCreate(player); // player
80 if (lua_pcall(L, 5, 1, m_errorhandler))
82 if (!lua_isnumber(L, -1))
83 throw LuaError("allow_put should return a number. name=" + name);
84 int ret = luaL_checkinteger(L, -1);
85 lua_pop(L, 1); // Pop integer
89 // Return number of accepted items to be taken
90 int ScriptApiDetached::detached_inventory_AllowTake(
91 const std::string &name,
92 const std::string &listname, int index, ItemStack &stack,
93 ServerActiveObject *player)
95 SCRIPTAPI_PRECHECKHEADER
97 // Push callback function on stack
98 if (!getDetachedInventoryCallback(name, "allow_take"))
99 return stack.count; // All will be accepted
101 // Call function(inv, listname, index, stack, player)
102 InventoryLocation loc;
103 loc.setDetached(name);
104 InvRef::create(L, loc); // inv
105 lua_pushstring(L, listname.c_str()); // listname
106 lua_pushinteger(L, index + 1); // index
107 LuaItemStack::create(L, stack); // stack
108 objectrefGetOrCreate(player); // player
109 if (lua_pcall(L, 5, 1, m_errorhandler))
111 if (!lua_isnumber(L, -1))
112 throw LuaError("allow_take should return a number. name=" + name);
113 int ret = luaL_checkinteger(L, -1);
114 lua_pop(L, 1); // Pop integer
118 // Report moved items
119 void ScriptApiDetached::detached_inventory_OnMove(
120 const std::string &name,
121 const std::string &from_list, int from_index,
122 const std::string &to_list, int to_index,
123 int count, ServerActiveObject *player)
125 SCRIPTAPI_PRECHECKHEADER
127 // Push callback function on stack
128 if (!getDetachedInventoryCallback(name, "on_move"))
131 // function(inv, from_list, from_index, to_list, to_index, count, player)
133 InventoryLocation loc;
134 loc.setDetached(name);
135 InvRef::create(L, loc);
136 lua_pushstring(L, from_list.c_str()); // from_list
137 lua_pushinteger(L, from_index + 1); // from_index
138 lua_pushstring(L, to_list.c_str()); // to_list
139 lua_pushinteger(L, to_index + 1); // to_index
140 lua_pushinteger(L, count); // count
141 objectrefGetOrCreate(player); // player
142 if (lua_pcall(L, 7, 0, m_errorhandler))
147 void ScriptApiDetached::detached_inventory_OnPut(
148 const std::string &name,
149 const std::string &listname, int index, ItemStack &stack,
150 ServerActiveObject *player)
152 SCRIPTAPI_PRECHECKHEADER
154 // Push callback function on stack
155 if (!getDetachedInventoryCallback(name, "on_put"))
158 // Call function(inv, listname, index, stack, player)
160 InventoryLocation loc;
161 loc.setDetached(name);
162 InvRef::create(L, loc);
163 lua_pushstring(L, listname.c_str()); // listname
164 lua_pushinteger(L, index + 1); // index
165 LuaItemStack::create(L, stack); // stack
166 objectrefGetOrCreate(player); // player
167 if (lua_pcall(L, 5, 0, m_errorhandler))
171 // Report taken items
172 void ScriptApiDetached::detached_inventory_OnTake(
173 const std::string &name,
174 const std::string &listname, int index, ItemStack &stack,
175 ServerActiveObject *player)
177 SCRIPTAPI_PRECHECKHEADER
179 // Push callback function on stack
180 if (!getDetachedInventoryCallback(name, "on_take"))
183 // Call function(inv, listname, index, stack, player)
185 InventoryLocation loc;
186 loc.setDetached(name);
187 InvRef::create(L, loc);
188 lua_pushstring(L, listname.c_str()); // listname
189 lua_pushinteger(L, index + 1); // index
190 LuaItemStack::create(L, stack); // stack
191 objectrefGetOrCreate(player); // player
192 if (lua_pcall(L, 5, 0, m_errorhandler))
196 // Retrieves core.detached_inventories[name][callbackname]
197 // If that is nil or on error, return false and stack is unchanged
198 // If that is a function, returns true and pushes the
199 // function onto the stack
200 bool ScriptApiDetached::getDetachedInventoryCallback(
201 const std::string &name, const char *callbackname)
203 lua_State *L = getStack();
205 lua_getglobal(L, "core");
206 lua_getfield(L, -1, "detached_inventories");
208 luaL_checktype(L, -1, LUA_TTABLE);
209 lua_getfield(L, -1, name.c_str());
212 if(lua_type(L, -1) != LUA_TTABLE)
214 errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
218 lua_getfield(L, -1, callbackname);
220 // Should be a function or nil
221 if(lua_type(L, -1) == LUA_TFUNCTION)
225 else if(lua_isnil(L, -1))
232 errorstream<<"Detached inventory \""<<name<<"\" callback \""
233 <<callbackname<<"\" is not a function"<<std::endl;