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