]> git.lizzy.rs Git - minetest.git/blob - src/scriptapi_inventory.h
split scriptapi.cpp
[minetest.git] / src / scriptapi_inventory.h
1 /*
2 Minetest-c55
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 LUA_INVENTORY_H_
21 #define LUA_INVENTORY_H_
22
23 #include <irr_v3d.h>
24
25 extern "C" {
26 #include <lua.h>
27 #include <lauxlib.h>
28 }
29
30 #include "inventorymanager.h"
31 #include "player.h"
32 #include "serverobject.h"
33 #include "inventory.h"
34
35 /*
36         InvRef
37 */
38
39 class InvRef
40 {
41 private:
42         InventoryLocation m_loc;
43
44         static const char className[];
45         static const luaL_reg methods[];
46
47         static InvRef *checkobject(lua_State *L, int narg);
48
49         static Inventory* getinv(lua_State *L, InvRef *ref);
50
51         static InventoryList* getlist(lua_State *L, InvRef *ref,
52                         const char *listname);
53
54         static void reportInventoryChange(lua_State *L, InvRef *ref);
55
56         // Exported functions
57
58         // garbage collector
59         static int gc_object(lua_State *L);
60
61         // is_empty(self, listname) -> true/false
62         static int l_is_empty(lua_State *L);
63
64         // get_size(self, listname)
65         static int l_get_size(lua_State *L);
66
67         // get_width(self, listname)
68         static int l_get_width(lua_State *L);
69
70         // set_size(self, listname, size)
71         static int l_set_size(lua_State *L);
72
73         // set_width(self, listname, size)
74         static int l_set_width(lua_State *L);
75
76         // get_stack(self, listname, i) -> itemstack
77         static int l_get_stack(lua_State *L);
78
79         // set_stack(self, listname, i, stack) -> true/false
80         static int l_set_stack(lua_State *L);
81
82         // get_list(self, listname) -> list or nil
83         static int l_get_list(lua_State *L);
84
85         // set_list(self, listname, list)
86         static int l_set_list(lua_State *L);
87
88         // add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
89         // Returns the leftover stack
90         static int l_add_item(lua_State *L);
91
92         // room_for_item(self, listname, itemstack or itemstring or table or nil) -> true/false
93         // Returns true if the item completely fits into the list
94         static int l_room_for_item(lua_State *L);
95
96         // contains_item(self, listname, itemstack or itemstring or table or nil) -> true/false
97         // Returns true if the list contains the given count of the given item name
98         static int l_contains_item(lua_State *L);
99
100         // remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
101         // Returns the items that were actually removed
102         static int l_remove_item(lua_State *L);
103
104         // get_location() -> location (like minetest.get_inventory(location))
105         static int l_get_location(lua_State *L);
106
107 public:
108         InvRef(const InventoryLocation &loc);
109
110         ~InvRef();
111
112         // Creates an InvRef and leaves it on top of stack
113         // Not callable from Lua; all references are created on the C side.
114         static void create(lua_State *L, const InventoryLocation &loc);
115         static void createPlayer(lua_State *L, Player *player);
116         static void createNodeMeta(lua_State *L, v3s16 p);
117         static void Register(lua_State *L);
118 };
119
120 void inventory_get_list_to_lua(Inventory *inv, const char *name,lua_State *L);
121 void inventory_set_list_from_lua(Inventory *inv, const char *name,
122                                                                 lua_State *L, int tableindex, int forcesize=-1);
123
124 /*****************************************************************************/
125 /* Minetest interface                                                        */
126 /*****************************************************************************/
127 /* Detached inventory callbacks */
128 // Return number of accepted items to be moved
129 int scriptapi_detached_inventory_allow_move(lua_State *L,
130                 const std::string &name,
131                 const std::string &from_list, int from_index,
132                 const std::string &to_list, int to_index,
133                 int count, ServerActiveObject *player);
134 // Return number of accepted items to be put
135 int scriptapi_detached_inventory_allow_put(lua_State *L,
136                 const std::string &name,
137                 const std::string &listname, int index, ItemStack &stack,
138                 ServerActiveObject *player);
139 // Return number of accepted items to be taken
140 int scriptapi_detached_inventory_allow_take(lua_State *L,
141                 const std::string &name,
142                 const std::string &listname, int index, ItemStack &stack,
143                 ServerActiveObject *player);
144 // Report moved items
145 void scriptapi_detached_inventory_on_move(lua_State *L,
146                 const std::string &name,
147                 const std::string &from_list, int from_index,
148                 const std::string &to_list, int to_index,
149                 int count, ServerActiveObject *player);
150 // Report put items
151 void scriptapi_detached_inventory_on_put(lua_State *L,
152                 const std::string &name,
153                 const std::string &listname, int index, ItemStack &stack,
154                 ServerActiveObject *player);
155 // Report taken items
156 void scriptapi_detached_inventory_on_take(lua_State *L,
157                 const std::string &name,
158                 const std::string &listname, int index, ItemStack &stack,
159                 ServerActiveObject *player);
160
161 /*****************************************************************************/
162 /* Mod API                                                                   */
163 /*****************************************************************************/
164 int l_create_detached_inventory_raw(lua_State *L);
165 int l_get_inventory(lua_State *L);
166
167 #endif /* LUA_INVENTORY_H_ */