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/scriptapi.h"
21 #include "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "lua_api/l_inventory.h"
24 #include "lua_api/l_item.h"
25 #include "common/c_internal.h"
28 #include "inventorymanager.h"
33 InvRef* InvRef::checkobject(lua_State *L, int narg)
35 luaL_checktype(L, narg, LUA_TUSERDATA);
36 void *ud = luaL_checkudata(L, narg, className);
37 if(!ud) luaL_typerror(L, narg, className);
38 return *(InvRef**)ud; // unbox pointer
41 Inventory* InvRef::getinv(lua_State *L, InvRef *ref)
43 return STACK_TO_SERVER(L)->getInventory(ref->m_loc);
46 InventoryList* InvRef::getlist(lua_State *L, InvRef *ref,
50 Inventory *inv = getinv(L, ref);
53 return inv->getList(listname);
56 void InvRef::reportInventoryChange(lua_State *L, InvRef *ref)
58 // Inform other things that the inventory has changed
59 STACK_TO_SERVER(L)->setInventoryModified(ref->m_loc);
65 int InvRef::gc_object(lua_State *L) {
66 InvRef *o = *(InvRef **)(lua_touserdata(L, 1));
71 // is_empty(self, listname) -> true/false
72 int InvRef::l_is_empty(lua_State *L)
75 InvRef *ref = checkobject(L, 1);
76 const char *listname = luaL_checkstring(L, 2);
77 InventoryList *list = getlist(L, ref, listname);
78 if(list && list->getUsedSlots() > 0){
79 lua_pushboolean(L, false);
81 lua_pushboolean(L, true);
86 // get_size(self, listname)
87 int InvRef::l_get_size(lua_State *L)
90 InvRef *ref = checkobject(L, 1);
91 const char *listname = luaL_checkstring(L, 2);
92 InventoryList *list = getlist(L, ref, listname);
94 lua_pushinteger(L, list->getSize());
96 lua_pushinteger(L, 0);
101 // get_width(self, listname)
102 int InvRef::l_get_width(lua_State *L)
104 NO_MAP_LOCK_REQUIRED;
105 InvRef *ref = checkobject(L, 1);
106 const char *listname = luaL_checkstring(L, 2);
107 InventoryList *list = getlist(L, ref, listname);
109 lua_pushinteger(L, list->getWidth());
111 lua_pushinteger(L, 0);
116 // set_size(self, listname, size)
117 int InvRef::l_set_size(lua_State *L)
119 NO_MAP_LOCK_REQUIRED;
120 InvRef *ref = checkobject(L, 1);
121 const char *listname = luaL_checkstring(L, 2);
122 int newsize = luaL_checknumber(L, 3);
123 Inventory *inv = getinv(L, ref);
125 inv->deleteList(listname);
126 reportInventoryChange(L, ref);
129 InventoryList *list = inv->getList(listname);
131 list->setSize(newsize);
133 list = inv->addList(listname, newsize);
135 reportInventoryChange(L, ref);
139 // set_width(self, listname, size)
140 int InvRef::l_set_width(lua_State *L)
142 NO_MAP_LOCK_REQUIRED;
143 InvRef *ref = checkobject(L, 1);
144 const char *listname = luaL_checkstring(L, 2);
145 int newwidth = luaL_checknumber(L, 3);
146 Inventory *inv = getinv(L, ref);
147 InventoryList *list = inv->getList(listname);
149 list->setWidth(newwidth);
153 reportInventoryChange(L, ref);
157 // get_stack(self, listname, i) -> itemstack
158 int InvRef::l_get_stack(lua_State *L)
160 NO_MAP_LOCK_REQUIRED;
161 InvRef *ref = checkobject(L, 1);
162 const char *listname = luaL_checkstring(L, 2);
163 int i = luaL_checknumber(L, 3) - 1;
164 InventoryList *list = getlist(L, ref, listname);
166 if(list != NULL && i >= 0 && i < (int) list->getSize())
167 item = list->getItem(i);
168 LuaItemStack::create(L, item);
172 // set_stack(self, listname, i, stack) -> true/false
173 int InvRef::l_set_stack(lua_State *L)
175 NO_MAP_LOCK_REQUIRED;
176 InvRef *ref = checkobject(L, 1);
177 const char *listname = luaL_checkstring(L, 2);
178 int i = luaL_checknumber(L, 3) - 1;
179 ItemStack newitem = read_item(L, 4,STACK_TO_SERVER(L));
180 InventoryList *list = getlist(L, ref, listname);
181 if(list != NULL && i >= 0 && i < (int) list->getSize()){
182 list->changeItem(i, newitem);
183 reportInventoryChange(L, ref);
184 lua_pushboolean(L, true);
186 lua_pushboolean(L, false);
191 // get_list(self, listname) -> list or nil
192 int InvRef::l_get_list(lua_State *L)
194 NO_MAP_LOCK_REQUIRED;
195 InvRef *ref = checkobject(L, 1);
196 const char *listname = luaL_checkstring(L, 2);
197 Inventory *inv = getinv(L, ref);
198 push_inventory_list(inv, listname, L);
202 // set_list(self, listname, list)
203 int InvRef::l_set_list(lua_State *L)
205 NO_MAP_LOCK_REQUIRED;
206 InvRef *ref = checkobject(L, 1);
207 const char *listname = luaL_checkstring(L, 2);
208 Inventory *inv = getinv(L, ref);
209 InventoryList *list = inv->getList(listname);
211 read_inventory_list(inv, listname, L, 3,
212 STACK_TO_SERVER(L),list->getSize());
214 read_inventory_list(inv, listname, L, 3,STACK_TO_SERVER(L));
215 reportInventoryChange(L, ref);
219 // add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
220 // Returns the leftover stack
221 int InvRef::l_add_item(lua_State *L)
223 NO_MAP_LOCK_REQUIRED;
224 InvRef *ref = checkobject(L, 1);
225 const char *listname = luaL_checkstring(L, 2);
226 ItemStack item = read_item(L, 3,STACK_TO_SERVER(L));
227 InventoryList *list = getlist(L, ref, listname);
229 ItemStack leftover = list->addItem(item);
230 if(leftover.count != item.count)
231 reportInventoryChange(L, ref);
232 LuaItemStack::create(L, leftover);
234 LuaItemStack::create(L, item);
239 // room_for_item(self, listname, itemstack or itemstring or table or nil) -> true/false
240 // Returns true if the item completely fits into the list
241 int InvRef::l_room_for_item(lua_State *L)
243 NO_MAP_LOCK_REQUIRED;
244 InvRef *ref = checkobject(L, 1);
245 const char *listname = luaL_checkstring(L, 2);
246 ItemStack item = read_item(L, 3,STACK_TO_SERVER(L));
247 InventoryList *list = getlist(L, ref, listname);
249 lua_pushboolean(L, list->roomForItem(item));
251 lua_pushboolean(L, false);
256 // contains_item(self, listname, itemstack or itemstring or table or nil) -> true/false
257 // Returns true if the list contains the given count of the given item name
258 int InvRef::l_contains_item(lua_State *L)
260 NO_MAP_LOCK_REQUIRED;
261 InvRef *ref = checkobject(L, 1);
262 const char *listname = luaL_checkstring(L, 2);
263 ItemStack item = read_item(L, 3, STACK_TO_SERVER(L));
264 InventoryList *list = getlist(L, ref, listname);
266 lua_pushboolean(L, list->containsItem(item));
268 lua_pushboolean(L, false);
273 // remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
274 // Returns the items that were actually removed
275 int InvRef::l_remove_item(lua_State *L)
277 NO_MAP_LOCK_REQUIRED;
278 InvRef *ref = checkobject(L, 1);
279 const char *listname = luaL_checkstring(L, 2);
280 ItemStack item = read_item(L, 3,STACK_TO_SERVER(L));
281 InventoryList *list = getlist(L, ref, listname);
283 ItemStack removed = list->removeItem(item);
285 reportInventoryChange(L, ref);
286 LuaItemStack::create(L, removed);
288 LuaItemStack::create(L, ItemStack());
293 // get_location() -> location (like minetest.get_inventory(location))
294 int InvRef::l_get_location(lua_State *L)
296 NO_MAP_LOCK_REQUIRED;
297 InvRef *ref = checkobject(L, 1);
298 const InventoryLocation &loc = ref->m_loc;
300 case InventoryLocation::PLAYER:
302 lua_pushstring(L, "player");
303 lua_setfield(L, -2, "type");
304 lua_pushstring(L, loc.name.c_str());
305 lua_setfield(L, -2, "name");
307 case InventoryLocation::NODEMETA:
309 lua_pushstring(L, "nodemeta");
310 lua_setfield(L, -2, "type");
311 push_v3s16(L, loc.p);
312 lua_setfield(L, -2, "name");
314 case InventoryLocation::DETACHED:
316 lua_pushstring(L, "detached");
317 lua_setfield(L, -2, "type");
318 lua_pushstring(L, loc.name.c_str());
319 lua_setfield(L, -2, "name");
321 case InventoryLocation::UNDEFINED:
322 case InventoryLocation::CURRENT_PLAYER:
326 lua_pushstring(L, "undefined");
327 lua_setfield(L, -2, "type");
332 InvRef::InvRef(const InventoryLocation &loc):
341 // Creates an InvRef and leaves it on top of stack
342 // Not callable from Lua; all references are created on the C side.
343 void InvRef::create(lua_State *L, const InventoryLocation &loc)
345 NO_MAP_LOCK_REQUIRED;
346 InvRef *o = new InvRef(loc);
347 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
348 luaL_getmetatable(L, className);
349 lua_setmetatable(L, -2);
351 void InvRef::createPlayer(lua_State *L, Player *player)
353 NO_MAP_LOCK_REQUIRED;
354 InventoryLocation loc;
355 loc.setPlayer(player->getName());
358 void InvRef::createNodeMeta(lua_State *L, v3s16 p)
360 InventoryLocation loc;
365 void InvRef::Register(lua_State *L)
368 int methodtable = lua_gettop(L);
369 luaL_newmetatable(L, className);
370 int metatable = lua_gettop(L);
372 lua_pushliteral(L, "__metatable");
373 lua_pushvalue(L, methodtable);
374 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
376 lua_pushliteral(L, "__index");
377 lua_pushvalue(L, methodtable);
378 lua_settable(L, metatable);
380 lua_pushliteral(L, "__gc");
381 lua_pushcfunction(L, gc_object);
382 lua_settable(L, metatable);
384 lua_pop(L, 1); // drop metatable
386 luaL_openlib(L, 0, methods, 0); // fill methodtable
387 lua_pop(L, 1); // drop methodtable
389 // Cannot be created from Lua
390 //lua_register(L, className, create_object);
393 const char InvRef::className[] = "InvRef";
394 const luaL_reg InvRef::methods[] = {
395 luamethod(InvRef, is_empty),
396 luamethod(InvRef, get_size),
397 luamethod(InvRef, set_size),
398 luamethod(InvRef, get_width),
399 luamethod(InvRef, set_width),
400 luamethod(InvRef, get_stack),
401 luamethod(InvRef, set_stack),
402 luamethod(InvRef, get_list),
403 luamethod(InvRef, set_list),
404 luamethod(InvRef, add_item),
405 luamethod(InvRef, room_for_item),
406 luamethod(InvRef, contains_item),
407 luamethod(InvRef, remove_item),
408 luamethod(InvRef, get_location),
412 // get_inventory(location)
413 int ModApiInventory::l_get_inventory(lua_State *L)
415 InventoryLocation loc;
417 std::string type = checkstringfield(L, 1, "type");
420 NO_MAP_LOCK_REQUIRED;
422 if(type == "player"){
423 std::string name = checkstringfield(L, 1, "name");
425 } else if(type == "detached"){
426 std::string name = checkstringfield(L, 1, "name");
427 loc.setDetached(name);
430 if(getServer(L)->getInventory(loc) != NULL)
431 InvRef::create(L, loc);
439 lua_getfield(L, 1, "pos");
440 v3s16 pos = check_v3s16(L, -1);
441 loc.setNodeMeta(pos);
443 if(getServer(L)->getInventory(loc) != NULL)
444 InvRef::create(L, loc);
452 // create_detached_inventory_raw(name)
453 int ModApiInventory::l_create_detached_inventory_raw(lua_State *L)
455 NO_MAP_LOCK_REQUIRED;
456 const char *name = luaL_checkstring(L, 1);
457 if(getServer(L)->createDetachedInventory(name) != NULL){
458 InventoryLocation loc;
459 loc.setDetached(name);
460 InvRef::create(L, loc);
467 bool ModApiInventory::Initialize(lua_State *L, int top) {
470 retval &= API_FCT(create_detached_inventory_raw);
471 retval &= API_FCT(get_inventory);
478 ModApiInventory::ModApiInventory()
483 ModApiInventory modapiinventory_prototype;