]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_nodemeta.cpp
Clean up threading
[dragonfireclient.git] / src / script / cpp_api / s_nodemeta.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_nodemeta.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "nodedef.h"
24 #include "mapnode.h"
25 #include "server.h"
26 #include "environment.h"
27 #include "lua_api/l_item.h"
28
29 // Return number of accepted items to be moved
30 int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
31                 const std::string &from_list, int from_index,
32                 const std::string &to_list, int to_index,
33                 int count, ServerActiveObject *player)
34 {
35         SCRIPTAPI_PRECHECKHEADER
36
37         INodeDefManager *ndef = getServer()->ndef();
38
39         // If node doesn't exist, we don't know what callback to call
40         MapNode node = getEnv()->getMap().getNodeNoEx(p);
41         if (node.getContent() == CONTENT_IGNORE)
42                 return 0;
43
44         // Push callback function on stack
45         std::string nodename = ndef->get(node).name;
46         if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move"))
47                 return count;
48
49         // function(pos, from_list, from_index, to_list, to_index, count, player)
50         push_v3s16(L, p);                     // pos
51         lua_pushstring(L, from_list.c_str()); // from_list
52         lua_pushinteger(L, from_index + 1);   // from_index
53         lua_pushstring(L, to_list.c_str());   // to_list
54         lua_pushinteger(L, to_index + 1);     // to_index
55         lua_pushinteger(L, count);            // count
56         objectrefGetOrCreate(L, player);      // player
57         PCALL_RES(lua_pcall(L, 7, 1, m_errorhandler));
58         if (!lua_isnumber(L, -1))
59                 throw LuaError("allow_metadata_inventory_move should"
60                                 " return a number, guilty node: " + nodename);
61         int num = luaL_checkinteger(L, -1);
62         lua_pop(L, 1); // Pop integer
63         return num;
64 }
65
66 // Return number of accepted items to be put
67 int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
68                 const std::string &listname, int index, ItemStack &stack,
69                 ServerActiveObject *player)
70 {
71         SCRIPTAPI_PRECHECKHEADER
72
73         INodeDefManager *ndef = getServer()->ndef();
74
75         // If node doesn't exist, we don't know what callback to call
76         MapNode node = getEnv()->getMap().getNodeNoEx(p);
77         if (node.getContent() == CONTENT_IGNORE)
78                 return 0;
79
80         // Push callback function on stack
81         std::string nodename = ndef->get(node).name;
82         if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put"))
83                 return stack.count;
84
85         // Call function(pos, listname, index, stack, player)
86         push_v3s16(L, p);                    // pos
87         lua_pushstring(L, listname.c_str()); // listname
88         lua_pushinteger(L, index + 1);       // index
89         LuaItemStack::create(L, stack);      // stack
90         objectrefGetOrCreate(L, player);     // player
91         PCALL_RES(lua_pcall(L, 5, 1, m_errorhandler));
92         if(!lua_isnumber(L, -1))
93                 throw LuaError("allow_metadata_inventory_put should"
94                                 " return a number, guilty node: " + nodename);
95         int num = luaL_checkinteger(L, -1);
96         lua_pop(L, 1); // Pop integer
97         return num;
98 }
99
100 // Return number of accepted items to be taken
101 int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
102                 const std::string &listname, int index, ItemStack &stack,
103                 ServerActiveObject *player)
104 {
105         SCRIPTAPI_PRECHECKHEADER
106
107         INodeDefManager *ndef = getServer()->ndef();
108
109         // If node doesn't exist, we don't know what callback to call
110         MapNode node = getEnv()->getMap().getNodeNoEx(p);
111         if (node.getContent() == CONTENT_IGNORE)
112                 return 0;
113
114         // Push callback function on stack
115         std::string nodename = ndef->get(node).name;
116         if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take"))
117                 return stack.count;
118
119         // Call function(pos, listname, index, count, player)
120         push_v3s16(L, p);                    // pos
121         lua_pushstring(L, listname.c_str()); // listname
122         lua_pushinteger(L, index + 1);       // index
123         LuaItemStack::create(L, stack);      // stack
124         objectrefGetOrCreate(L, player);     // player
125         PCALL_RES(lua_pcall(L, 5, 1, m_errorhandler));
126         if (!lua_isnumber(L, -1))
127                 throw LuaError("allow_metadata_inventory_take should"
128                                 " return a number, guilty node: " + nodename);
129         int num = luaL_checkinteger(L, -1);
130         lua_pop(L, 1); // Pop integer
131         return num;
132 }
133
134 // Report moved items
135 void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
136                 const std::string &from_list, int from_index,
137                 const std::string &to_list, int to_index,
138                 int count, ServerActiveObject *player)
139 {
140         SCRIPTAPI_PRECHECKHEADER
141
142         INodeDefManager *ndef = getServer()->ndef();
143
144         // If node doesn't exist, we don't know what callback to call
145         MapNode node = getEnv()->getMap().getNodeNoEx(p);
146         if (node.getContent() == CONTENT_IGNORE)
147                 return;
148
149         // Push callback function on stack
150         std::string nodename = ndef->get(node).name;
151         if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_move"))
152                 return;
153
154         // function(pos, from_list, from_index, to_list, to_index, count, player)
155         push_v3s16(L, p);                     // pos
156         lua_pushstring(L, from_list.c_str()); // from_list
157         lua_pushinteger(L, from_index + 1);   // from_index
158         lua_pushstring(L, to_list.c_str());   // to_list
159         lua_pushinteger(L, to_index + 1);     // to_index
160         lua_pushinteger(L, count);            // count
161         objectrefGetOrCreate(L, player);      // player
162         PCALL_RES(lua_pcall(L, 7, 0, m_errorhandler));
163 }
164
165 // Report put items
166 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
167                 const std::string &listname, int index, ItemStack &stack,
168                 ServerActiveObject *player)
169 {
170         SCRIPTAPI_PRECHECKHEADER
171
172         INodeDefManager *ndef = getServer()->ndef();
173
174         // If node doesn't exist, we don't know what callback to call
175         MapNode node = getEnv()->getMap().getNodeNoEx(p);
176         if (node.getContent() == CONTENT_IGNORE)
177                 return;
178
179         // Push callback function on stack
180         std::string nodename = ndef->get(node).name;
181         if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_put"))
182                 return;
183
184         // Call function(pos, listname, index, stack, player)
185         push_v3s16(L, p);                    // pos
186         lua_pushstring(L, listname.c_str()); // listname
187         lua_pushinteger(L, index + 1);       // index
188         LuaItemStack::create(L, stack);      // stack
189         objectrefGetOrCreate(L, player);     // player
190         PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
191 }
192
193 // Report taken items
194 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
195                 const std::string &listname, int index, ItemStack &stack,
196                 ServerActiveObject *player)
197 {
198         SCRIPTAPI_PRECHECKHEADER
199
200         INodeDefManager *ndef = getServer()->ndef();
201
202         // If node doesn't exist, we don't know what callback to call
203         MapNode node = getEnv()->getMap().getNodeNoEx(p);
204         if (node.getContent() == CONTENT_IGNORE)
205                 return;
206
207         // Push callback function on stack
208         std::string nodename = ndef->get(node).name;
209         if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_take"))
210                 return;
211
212         // Call function(pos, listname, index, stack, player)
213         push_v3s16(L, p);                    // pos
214         lua_pushstring(L, listname.c_str()); // listname
215         lua_pushinteger(L, index + 1);       // index
216         LuaItemStack::create(L, stack);      // stack
217         objectrefGetOrCreate(L, player);     // player
218         PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
219 }
220
221 ScriptApiNodemeta::ScriptApiNodemeta()
222 {
223 }
224
225 ScriptApiNodemeta::~ScriptApiNodemeta()
226 {
227 }
228