Fix itemdef drop on NULL texture
[oweals/minetest.git] / src / scriptapi_item.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_ITEM_H_
21 #define LUA_ITEM_H_
22
23 extern "C" {
24 #include <lua.h>
25 #include <lauxlib.h>
26 }
27
28 #include <vector>
29
30 #include "itemdef.h"
31 #include "content_sao.h"
32 #include "util/pointedthing.h"
33 #include "inventory.h"
34
35 #include "inventory.h"
36
37 ItemStack read_item(lua_State *L, int index);
38 std::vector<ItemStack> read_items(lua_State *L, int index);
39 void push_items(lua_State *L, const std::vector<ItemStack> &items);
40
41 class LuaItemStack
42 {
43 private:
44         ItemStack m_stack;
45
46         static const char className[];
47         static const luaL_reg methods[];
48
49         // Exported functions
50
51         // garbage collector
52         static int gc_object(lua_State *L);
53
54         // is_empty(self) -> true/false
55         static int l_is_empty(lua_State *L);
56
57         // get_name(self) -> string
58         static int l_get_name(lua_State *L);
59
60         // get_count(self) -> number
61         static int l_get_count(lua_State *L);
62
63         // get_wear(self) -> number
64         static int l_get_wear(lua_State *L);
65
66         // get_metadata(self) -> string
67         static int l_get_metadata(lua_State *L);
68
69         // clear(self) -> true
70         static int l_clear(lua_State *L);
71
72         // replace(self, itemstack or itemstring or table or nil) -> true
73         static int l_replace(lua_State *L);
74
75         // to_string(self) -> string
76         static int l_to_string(lua_State *L);
77
78         // to_table(self) -> table or nil
79         static int l_to_table(lua_State *L);
80
81         // get_stack_max(self) -> number
82         static int l_get_stack_max(lua_State *L);
83
84         // get_free_space(self) -> number
85         static int l_get_free_space(lua_State *L);
86
87         // is_known(self) -> true/false
88         // Checks if the item is defined.
89         static int l_is_known(lua_State *L);
90
91         // get_definition(self) -> table
92         // Returns the item definition table from minetest.registered_items,
93         // or a fallback one (name="unknown")
94         static int l_get_definition(lua_State *L);
95
96         // get_tool_capabilities(self) -> table
97         // Returns the effective tool digging properties.
98         // Returns those of the hand ("") if this item has none associated.
99         static int l_get_tool_capabilities(lua_State *L);
100
101         // add_wear(self, amount) -> true/false
102         // The range for "amount" is [0,65535]. Wear is only added if the item
103         // is a tool. Adding wear might destroy the item.
104         // Returns true if the item is (or was) a tool.
105         static int l_add_wear(lua_State *L);
106
107         // add_item(self, itemstack or itemstring or table or nil) -> itemstack
108         // Returns leftover item stack
109         static int l_add_item(lua_State *L);
110
111         // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
112         // First return value is true iff the new item fits fully into the stack
113         // Second return value is the would-be-left-over item stack
114         static int l_item_fits(lua_State *L);
115
116         // take_item(self, takecount=1) -> itemstack
117         static int l_take_item(lua_State *L);
118
119         // peek_item(self, peekcount=1) -> itemstack
120         static int l_peek_item(lua_State *L);
121
122 public:
123         LuaItemStack(const ItemStack &item);
124         ~LuaItemStack();
125
126         const ItemStack& getItem() const;
127         ItemStack& getItem();
128
129         // LuaItemStack(itemstack or itemstring or table or nil)
130         // Creates an LuaItemStack and leaves it on top of stack
131         static int create_object(lua_State *L);
132         // Not callable from Lua
133         static int create(lua_State *L, const ItemStack &item);
134         static LuaItemStack* checkobject(lua_State *L, int narg);
135         static void Register(lua_State *L);
136
137 };
138
139 /*****************************************************************************/
140 /* Mod API                                                                   */
141 /*****************************************************************************/
142 int l_register_item_raw(lua_State *L);
143 int l_register_alias_raw(lua_State *L);
144
145 /*****************************************************************************/
146 /* scriptapi internal                                                        */
147 /*****************************************************************************/
148 bool get_item_callback(lua_State *L,
149                 const char *name, const char *callbackname);
150 ItemDefinition read_item_definition(lua_State *L, int index,
151                 ItemDefinition default_def = ItemDefinition());
152
153 extern struct EnumString es_ItemType[];
154
155 /*****************************************************************************/
156 /* Minetest interface                                                        */
157 /*****************************************************************************/
158 bool scriptapi_item_on_drop(lua_State *L, ItemStack &item,
159                 ServerActiveObject *dropper, v3f pos);
160 bool scriptapi_item_on_place(lua_State *L, ItemStack &item,
161                 ServerActiveObject *placer, const PointedThing &pointed);
162 bool scriptapi_item_on_use(lua_State *L, ItemStack &item,
163                 ServerActiveObject *user, const PointedThing &pointed);
164
165
166 #endif /* LUA_ITEM_H_ */