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