6937ebbebf21529c8c57e98fe6bc167228e1d9c4
[oweals/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 "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "lua_api/l_item.h"
24 #include "server.h"
25
26 bool ScriptApiItem::item_OnDrop(ItemStack &item,
27                 ServerActiveObject *dropper, v3f pos)
28 {
29         SCRIPTAPI_PRECHECKHEADER
30
31         // Push callback function on stack
32         if(!getItemCallback(item.name.c_str(), "on_drop"))
33                 return false;
34
35         // Call function
36         LuaItemStack::create(L, item);
37         objectrefGetOrCreate(dropper);
38         pushFloatPos(L, pos);
39         if(lua_pcall(L, 3, 1, 0))
40                 scriptError("error: %s", lua_tostring(L, -1));
41         if(!lua_isnil(L, -1))
42                 item = read_item(L,-1, getServer());
43         return true;
44 }
45
46 bool ScriptApiItem::item_OnPlace(ItemStack &item,
47                 ServerActiveObject *placer, const PointedThing &pointed)
48 {
49         SCRIPTAPI_PRECHECKHEADER
50
51         // Push callback function on stack
52         if(!getItemCallback(item.name.c_str(), "on_place"))
53                 return false;
54
55         // Call function
56         LuaItemStack::create(L, item);
57         objectrefGetOrCreate(placer);
58         pushPointedThing(pointed);
59         if(lua_pcall(L, 3, 1, 0))
60                 scriptError("error: %s", lua_tostring(L, -1));
61         if(!lua_isnil(L, -1))
62                 item = read_item(L,-1, getServer());
63         return true;
64 }
65
66 bool ScriptApiItem::item_OnUse(ItemStack &item,
67                 ServerActiveObject *user, const PointedThing &pointed)
68 {
69         SCRIPTAPI_PRECHECKHEADER
70
71         // Push callback function on stack
72         if(!getItemCallback(item.name.c_str(), "on_use"))
73                 return false;
74
75         // Call function
76         LuaItemStack::create(L, item);
77         objectrefGetOrCreate(user);
78         pushPointedThing(pointed);
79         if(lua_pcall(L, 3, 1, 0))
80                 scriptError("error: %s", lua_tostring(L, -1));
81         if(!lua_isnil(L, -1))
82                 item = read_item(L,-1, getServer());
83         return true;
84 }
85
86 // Retrieves minetest.registered_items[name][callbackname]
87 // If that is nil or on error, return false and stack is unchanged
88 // If that is a function, returns true and pushes the
89 // function onto the stack
90 // If minetest.registered_items[name] doesn't exist, minetest.nodedef_default
91 // is tried instead so unknown items can still be manipulated to some degree
92 bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
93 {
94         lua_State* L = getStack();
95
96         lua_getglobal(L, "minetest");
97         lua_getfield(L, -1, "registered_items");
98         lua_remove(L, -2);
99         luaL_checktype(L, -1, LUA_TTABLE);
100         lua_getfield(L, -1, name);
101         lua_remove(L, -2);
102         // Should be a table
103         if(lua_type(L, -1) != LUA_TTABLE)
104         {
105                 // Report error and clean up
106                 errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
107                 lua_pop(L, 1);
108
109                 // Try minetest.nodedef_default instead
110                 lua_getglobal(L, "minetest");
111                 lua_getfield(L, -1, "nodedef_default");
112                 lua_remove(L, -2);
113                 luaL_checktype(L, -1, LUA_TTABLE);
114         }
115         lua_getfield(L, -1, callbackname);
116         lua_remove(L, -2);
117         // Should be a function or nil
118         if(lua_type(L, -1) == LUA_TFUNCTION)
119         {
120                 return true;
121         }
122         else if(lua_isnil(L, -1))
123         {
124                 lua_pop(L, 1);
125                 return false;
126         }
127         else
128         {
129                 errorstream<<"Item \""<<name<<"\" callback \""
130                         <<callbackname<<" is not a function"<<std::endl;
131                 lua_pop(L, 1);
132                 return false;
133         }
134 }
135
136 void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
137 {
138         lua_State* L = getStack();
139
140         lua_newtable(L);
141         if(pointed.type == POINTEDTHING_NODE)
142         {
143                 lua_pushstring(L, "node");
144                 lua_setfield(L, -2, "type");
145                 push_v3s16(L, pointed.node_undersurface);
146                 lua_setfield(L, -2, "under");
147                 push_v3s16(L, pointed.node_abovesurface);
148                 lua_setfield(L, -2, "above");
149         }
150         else if(pointed.type == POINTEDTHING_OBJECT)
151         {
152                 lua_pushstring(L, "object");
153                 lua_setfield(L, -2, "type");
154                 objectrefGet(pointed.object_id);
155                 lua_setfield(L, -2, "ref");
156         }
157         else
158         {
159                 lua_pushstring(L, "nothing");
160                 lua_setfield(L, -2, "type");
161         }
162 }
163
164