]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_item.cpp
1ca06de7646af2a4a16549df7b2bce391c5b83da
[dragonfireclient.git] / src / script / cpp_api / s_item.cpp
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 #include "cpp_api/s_item.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "lua_api/l_item.h"
25 #include "lua_api/l_inventory.h"
26 #include "server.h"
27 #include "log.h"
28 #include "util/pointedthing.h"
29 #include "inventory.h"
30 #include "inventorymanager.h"
31
32 bool ScriptApiItem::item_OnDrop(ItemStack &item,
33                 ServerActiveObject *dropper, v3f pos)
34 {
35         SCRIPTAPI_PRECHECKHEADER
36
37         // Push callback function on stack
38         if (!getItemCallback(item.name.c_str(), "on_drop"))
39                 return false;
40
41         // Call function
42         LuaItemStack::create(L, item);
43         objectrefGetOrCreate(L, dropper);
44         pushFloatPos(L, pos);
45         PCALL_RES(lua_pcall(L, 3, 1, m_errorhandler));
46         if (!lua_isnil(L, -1)) {
47                 try {
48                         item = read_item(L,-1, getServer());
49                 } catch (LuaError &e) {
50                         throw LuaError(std::string(e.what()) + ". item=" + item.name);
51                 }
52         }
53         lua_pop(L, 1);  // Pop item
54         return true;
55 }
56
57 bool ScriptApiItem::item_OnPlace(ItemStack &item,
58                 ServerActiveObject *placer, const PointedThing &pointed)
59 {
60         SCRIPTAPI_PRECHECKHEADER
61
62         // Push callback function on stack
63         if (!getItemCallback(item.name.c_str(), "on_place"))
64                 return false;
65
66         // Call function
67         LuaItemStack::create(L, item);
68         objectrefGetOrCreate(L, placer);
69         pushPointedThing(pointed);
70         PCALL_RES(lua_pcall(L, 3, 1, m_errorhandler));
71         if (!lua_isnil(L, -1)) {
72                 try {
73                         item = read_item(L,-1, getServer());
74                 } catch (LuaError &e) {
75                         throw LuaError(std::string(e.what()) + ". item=" + item.name);
76                 }
77         }
78         lua_pop(L, 1);  // Pop item
79         return true;
80 }
81
82 bool ScriptApiItem::item_OnUse(ItemStack &item,
83                 ServerActiveObject *user, const PointedThing &pointed)
84 {
85         SCRIPTAPI_PRECHECKHEADER
86
87         // Push callback function on stack
88         if (!getItemCallback(item.name.c_str(), "on_use"))
89                 return false;
90
91         // Call function
92         LuaItemStack::create(L, item);
93         objectrefGetOrCreate(L, user);
94         pushPointedThing(pointed);
95         PCALL_RES(lua_pcall(L, 3, 1, m_errorhandler));
96         if(!lua_isnil(L, -1)) {
97                 try {
98                         item = read_item(L,-1, getServer());
99                 } catch (LuaError &e) {
100                         throw LuaError(std::string(e.what()) + ". item=" + item.name);
101                 }
102         }
103         lua_pop(L, 1);  // Pop item
104         return true;
105 }
106
107 bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
108                 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
109 {
110         SCRIPTAPI_PRECHECKHEADER
111
112         lua_getglobal(L, "core");
113         lua_getfield(L, -1, "on_craft");
114         LuaItemStack::create(L, item);
115         objectrefGetOrCreate(L, user);
116
117         // Push inventory list
118         std::vector<ItemStack> items;
119         for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
120                 items.push_back(old_craft_grid->getItem(i));
121         }
122         push_items(L, items);
123
124         InvRef::create(L, craft_inv);
125         PCALL_RES(lua_pcall(L, 4, 1, m_errorhandler));
126         if (!lua_isnil(L, -1)) {
127                 try {
128                         item = read_item(L,-1, getServer());
129                 } catch (LuaError &e) {
130                         throw LuaError(std::string(e.what()) + ". item=" + item.name);
131                 }
132         }
133         lua_pop(L, 1);  // Pop item
134         return true;
135 }
136
137 bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
138                 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
139 {
140         SCRIPTAPI_PRECHECKHEADER
141
142         lua_getglobal(L, "core");
143         lua_getfield(L, -1, "craft_predict");
144         LuaItemStack::create(L, item);
145         objectrefGetOrCreate(L, user);
146
147         //Push inventory list
148         std::vector<ItemStack> items;
149         for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
150                 items.push_back(old_craft_grid->getItem(i));
151         }
152         push_items(L, items);
153
154         InvRef::create(L, craft_inv);
155         PCALL_RES(lua_pcall(L, 4, 1, m_errorhandler));
156         if (!lua_isnil(L, -1)) {
157                 try {
158                         item = read_item(L,-1, getServer());
159                 } catch (LuaError &e) {
160                         throw LuaError(std::string(e.what()) + ". item=" + item.name);
161                 }
162         }
163         lua_pop(L, 1);  // Pop item
164         return true;
165 }
166
167 // Retrieves core.registered_items[name][callbackname]
168 // If that is nil or on error, return false and stack is unchanged
169 // If that is a function, returns true and pushes the
170 // function onto the stack
171 // If core.registered_items[name] doesn't exist, core.nodedef_default
172 // is tried instead so unknown items can still be manipulated to some degree
173 bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
174 {
175         lua_State* L = getStack();
176
177         lua_getglobal(L, "core");
178         lua_getfield(L, -1, "registered_items");
179         lua_remove(L, -2); // Remove core
180         luaL_checktype(L, -1, LUA_TTABLE);
181         lua_getfield(L, -1, name);
182         lua_remove(L, -2); // Remove registered_items
183         // Should be a table
184         if(lua_type(L, -1) != LUA_TTABLE)
185         {
186                 // Report error and clean up
187                 errorstream << "Item \"" << name << "\" not defined" << std::endl;
188                 lua_pop(L, 1);
189
190                 // Try core.nodedef_default instead
191                 lua_getglobal(L, "core");
192                 lua_getfield(L, -1, "nodedef_default");
193                 lua_remove(L, -2);
194                 luaL_checktype(L, -1, LUA_TTABLE);
195         }
196         lua_getfield(L, -1, callbackname);
197         lua_remove(L, -2); // Remove item def
198         // Should be a function or nil
199         if (lua_type(L, -1) == LUA_TFUNCTION) {
200                 return true;
201         } else if (!lua_isnil(L, -1)) {
202                 errorstream << "Item \"" << name << "\" callback \""
203                         << callbackname << "\" is not a function" << std::endl;
204         }
205         lua_pop(L, 1);
206         return false;
207 }
208
209 void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
210 {
211         lua_State* L = getStack();
212
213         lua_newtable(L);
214         if(pointed.type == POINTEDTHING_NODE)
215         {
216                 lua_pushstring(L, "node");
217                 lua_setfield(L, -2, "type");
218                 push_v3s16(L, pointed.node_undersurface);
219                 lua_setfield(L, -2, "under");
220                 push_v3s16(L, pointed.node_abovesurface);
221                 lua_setfield(L, -2, "above");
222         }
223         else if(pointed.type == POINTEDTHING_OBJECT)
224         {
225                 lua_pushstring(L, "object");
226                 lua_setfield(L, -2, "type");
227                 objectrefGet(L, pointed.object_id);
228                 lua_setfield(L, -2, "ref");
229         }
230         else
231         {
232                 lua_pushstring(L, "nothing");
233                 lua_setfield(L, -2, "type");
234         }
235 }
236