1 /*
2 script/lua_api/l_item.h
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 */
5 
6 /*
7 This file is part of Freeminer.
8 
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Freeminer  is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Freeminer.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef L_ITEM_H_
24 #define L_ITEM_H_
25 
26 #include "lua_api/l_base.h"
27 #include "inventory.h"  // ItemStack
28 
29 class LuaItemStack : public ModApiBase {
30 private:
31 	ItemStack m_stack;
32 
33 	static const char className[];
34 	static const luaL_reg methods[];
35 
36 	// Exported functions
37 
38 	// garbage collector
39 	static int gc_object(lua_State *L);
40 
41 	// is_empty(self) -> true/false
42 	static int l_is_empty(lua_State *L);
43 
44 	// get_name(self) -> string
45 	static int l_get_name(lua_State *L);
46 
47 	// set_name(self, name)
48 	static int l_set_name(lua_State *L);
49 
50 	// get_count(self) -> number
51 	static int l_get_count(lua_State *L);
52 
53 	// set_count(self, number)
54 	static int l_set_count(lua_State *L);
55 
56 	// get_wear(self) -> number
57 	static int l_get_wear(lua_State *L);
58 
59 	// set_wear(self, number)
60 	static int l_set_wear(lua_State *L);
61 
62 	// get_metadata(self) -> string
63 	static int l_get_metadata(lua_State *L);
64 
65 	// set_metadata(self, string)
66 	static int l_set_metadata(lua_State *L);
67 
68 	// clear(self) -> true
69 	static int l_clear(lua_State *L);
70 
71 	// replace(self, itemstack or itemstring or table or nil) -> true
72 	static int l_replace(lua_State *L);
73 
74 	// to_string(self) -> string
75 	static int l_to_string(lua_State *L);
76 
77 	// to_table(self) -> table or nil
78 	static int l_to_table(lua_State *L);
79 
80 	// get_stack_max(self) -> number
81 	static int l_get_stack_max(lua_State *L);
82 
83 	// get_free_space(self) -> number
84 	static int l_get_free_space(lua_State *L);
85 
86 	// is_known(self) -> true/false
87 	// Checks if the item is defined.
88 	static int l_is_known(lua_State *L);
89 
90 	// get_definition(self) -> table
91 	// Returns the item definition table from core.registered_items,
92 	// or a fallback one (name="unknown")
93 	static int l_get_definition(lua_State *L);
94 
95 	// get_tool_capabilities(self) -> table
96 	// Returns the effective tool digging properties.
97 	// Returns those of the hand ("") if this item has none associated.
98 	static int l_get_tool_capabilities(lua_State *L);
99 
100 	// add_wear(self, amount) -> true/false
101 	// The range for "amount" is [0,65535]. Wear is only added if the item
102 	// is a tool. Adding wear might destroy the item.
103 	// Returns true if the item is (or was) a tool.
104 	static int l_add_wear(lua_State *L);
105 
106 	// add_item(self, itemstack or itemstring or table or nil) -> itemstack
107 	// Returns leftover item stack
108 	static int l_add_item(lua_State *L);
109 
110 	// item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
111 	// First return value is true iff the new item fits fully into the stack
112 	// Second return value is the would-be-left-over item stack
113 	static int l_item_fits(lua_State *L);
114 
115 	// take_item(self, takecount=1) -> itemstack
116 	static int l_take_item(lua_State *L);
117 
118 	// peek_item(self, peekcount=1) -> itemstack
119 	static int l_peek_item(lua_State *L);
120 
121 public:
122 	LuaItemStack(const ItemStack &item);
123 	~LuaItemStack();
124 
125 	const ItemStack& getItem() const;
126 	ItemStack& getItem();
127 
128 	// LuaItemStack(itemstack or itemstring or table or nil)
129 	// Creates an LuaItemStack and leaves it on top of stack
130 	static int create_object(lua_State *L);
131 	// Not callable from Lua
132 	static int create(lua_State *L, const ItemStack &item);
133 	static LuaItemStack* checkobject(lua_State *L, int narg);
134 	static void Register(lua_State *L);
135 
136 };
137 
138 class ModApiItemMod : public ModApiBase {
139 private:
140 	static int l_register_item_raw(lua_State *L);
141 	static int l_register_alias_raw(lua_State *L);
142 	static int l_get_content_id(lua_State *L);
143 	static int l_get_name_from_content_id(lua_State *L);
144 public:
145 	static void Initialize(lua_State *L, int top);
146 };
147 
148 
149 
150 #endif /* L_ITEM_H_ */
151