]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_inventory.h
Fix my name.
[dragonfireclient.git] / src / script / lua_api / l_inventory.h
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 #ifndef L_INVENTORY_H_
21 #define L_INVENTORY_H_
22
23 extern "C" {
24 #include <lua.h>
25 #include <lauxlib.h>
26 }
27
28 #include "inventorymanager.h"
29 #include "player.h"
30 #include "serverobject.h"
31 #include "inventory.h"
32
33 #include "lua_api/l_base.h"
34 /*
35         InvRef
36 */
37
38 class InvRef
39 {
40 private:
41         InventoryLocation m_loc;
42
43         static const char className[];
44         static const luaL_reg methods[];
45
46         static InvRef *checkobject(lua_State *L, int narg);
47
48         static Inventory* getinv(lua_State *L, InvRef *ref);
49
50         static InventoryList* getlist(lua_State *L, InvRef *ref,
51                         const char *listname);
52
53         static void reportInventoryChange(lua_State *L, InvRef *ref);
54
55         // Exported functions
56
57         // garbage collector
58         static int gc_object(lua_State *L);
59
60         // is_empty(self, listname) -> true/false
61         static int l_is_empty(lua_State *L);
62
63         // get_size(self, listname)
64         static int l_get_size(lua_State *L);
65
66         // get_width(self, listname)
67         static int l_get_width(lua_State *L);
68
69         // set_size(self, listname, size)
70         static int l_set_size(lua_State *L);
71
72         // set_width(self, listname, size)
73         static int l_set_width(lua_State *L);
74
75         // get_stack(self, listname, i) -> itemstack
76         static int l_get_stack(lua_State *L);
77
78         // set_stack(self, listname, i, stack) -> true/false
79         static int l_set_stack(lua_State *L);
80
81         // get_list(self, listname) -> list or nil
82         static int l_get_list(lua_State *L);
83
84         // set_list(self, listname, list)
85         static int l_set_list(lua_State *L);
86
87         // add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
88         // Returns the leftover stack
89         static int l_add_item(lua_State *L);
90
91         // room_for_item(self, listname, itemstack or itemstring or table or nil) -> true/false
92         // Returns true if the item completely fits into the list
93         static int l_room_for_item(lua_State *L);
94
95         // contains_item(self, listname, itemstack or itemstring or table or nil) -> true/false
96         // Returns true if the list contains the given count of the given item name
97         static int l_contains_item(lua_State *L);
98
99         // remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
100         // Returns the items that were actually removed
101         static int l_remove_item(lua_State *L);
102
103         // get_location() -> location (like minetest.get_inventory(location))
104         static int l_get_location(lua_State *L);
105
106 public:
107         InvRef(const InventoryLocation &loc);
108
109         ~InvRef();
110
111         // Creates an InvRef and leaves it on top of stack
112         // Not callable from Lua; all references are created on the C side.
113         static void create(lua_State *L, const InventoryLocation &loc);
114         static void createPlayer(lua_State *L, Player *player);
115         static void createNodeMeta(lua_State *L, v3s16 p);
116         static void Register(lua_State *L);
117 };
118
119 class ModApiInventory
120         : public ModApiBase
121 {
122 public:
123         ModApiInventory();
124
125         bool Initialize(lua_State *L, int top);
126
127         static int l_create_detached_inventory_raw(lua_State *L);
128         static int l_get_inventory(lua_State *L);
129 private:
130         static void inventory_set_list_from_lua(Inventory *inv, const char *name,
131                         lua_State *L, int tableindex, int forcesize);
132         static void inventory_get_list_to_lua(Inventory *inv, const char *name,
133                         lua_State *L);
134
135 };
136
137 #endif /* L_INVENTORY_H_ */