3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "scriptapi.h"
21 #include "scriptapi_nodemeta.h"
24 #include "scriptapi_types.h"
25 #include "scriptapi_inventory.h"
26 #include "scriptapi_common.h"
27 #include "scriptapi_item.h"
28 #include "scriptapi_object.h"
34 NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg)
36 luaL_checktype(L, narg, LUA_TUSERDATA);
37 void *ud = luaL_checkudata(L, narg, className);
38 if(!ud) luaL_typerror(L, narg, className);
39 return *(NodeMetaRef**)ud; // unbox pointer
42 NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
44 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
45 if(meta == NULL && auto_create)
47 meta = new NodeMetadata(ref->m_env->getGameDef());
48 ref->m_env->getMap().setNodeMetadata(ref->m_p, meta);
53 void NodeMetaRef::reportMetadataChange(NodeMetaRef *ref)
55 // NOTE: This same code is in rollback_interface.cpp
56 // Inform other things that the metadata has changed
57 v3s16 blockpos = getNodeBlockPos(ref->m_p);
59 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
61 ref->m_env->getMap().dispatchEvent(&event);
62 // Set the block to be saved
63 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
65 block->raiseModified(MOD_STATE_WRITE_NEEDED,
66 "NodeMetaRef::reportMetadataChange");
72 int NodeMetaRef::gc_object(lua_State *L) {
73 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
78 // get_string(self, name)
79 int NodeMetaRef::l_get_string(lua_State *L)
81 NodeMetaRef *ref = checkobject(L, 1);
82 std::string name = luaL_checkstring(L, 2);
84 NodeMetadata *meta = getmeta(ref, false);
86 lua_pushlstring(L, "", 0);
89 std::string str = meta->getString(name);
90 lua_pushlstring(L, str.c_str(), str.size());
94 // set_string(self, name, var)
95 int NodeMetaRef::l_set_string(lua_State *L)
97 NodeMetaRef *ref = checkobject(L, 1);
98 std::string name = luaL_checkstring(L, 2);
100 const char *s = lua_tolstring(L, 3, &len);
101 std::string str(s, len);
103 NodeMetadata *meta = getmeta(ref, !str.empty());
104 if(meta == NULL || str == meta->getString(name))
106 meta->setString(name, str);
107 reportMetadataChange(ref);
111 // get_int(self, name)
112 int NodeMetaRef::l_get_int(lua_State *L)
114 NodeMetaRef *ref = checkobject(L, 1);
115 std::string name = lua_tostring(L, 2);
117 NodeMetadata *meta = getmeta(ref, false);
119 lua_pushnumber(L, 0);
122 std::string str = meta->getString(name);
123 lua_pushnumber(L, stoi(str));
127 // set_int(self, name, var)
128 int NodeMetaRef::l_set_int(lua_State *L)
130 NodeMetaRef *ref = checkobject(L, 1);
131 std::string name = lua_tostring(L, 2);
132 int a = lua_tointeger(L, 3);
133 std::string str = itos(a);
135 NodeMetadata *meta = getmeta(ref, true);
136 if(meta == NULL || str == meta->getString(name))
138 meta->setString(name, str);
139 reportMetadataChange(ref);
143 // get_float(self, name)
144 int NodeMetaRef::l_get_float(lua_State *L)
146 NodeMetaRef *ref = checkobject(L, 1);
147 std::string name = lua_tostring(L, 2);
149 NodeMetadata *meta = getmeta(ref, false);
151 lua_pushnumber(L, 0);
154 std::string str = meta->getString(name);
155 lua_pushnumber(L, stof(str));
159 // set_float(self, name, var)
160 int NodeMetaRef::l_set_float(lua_State *L)
162 NodeMetaRef *ref = checkobject(L, 1);
163 std::string name = lua_tostring(L, 2);
164 float a = lua_tonumber(L, 3);
165 std::string str = ftos(a);
167 NodeMetadata *meta = getmeta(ref, true);
168 if(meta == NULL || str == meta->getString(name))
170 meta->setString(name, str);
171 reportMetadataChange(ref);
175 // get_inventory(self)
176 int NodeMetaRef::l_get_inventory(lua_State *L)
178 NodeMetaRef *ref = checkobject(L, 1);
179 getmeta(ref, true); // try to ensure the metadata exists
180 InvRef::createNodeMeta(L, ref->m_p);
185 int NodeMetaRef::l_to_table(lua_State *L)
187 NodeMetaRef *ref = checkobject(L, 1);
189 NodeMetadata *meta = getmeta(ref, true);
198 std::map<std::string, std::string> fields = meta->getStrings();
199 for(std::map<std::string, std::string>::const_iterator
200 i = fields.begin(); i != fields.end(); i++){
201 const std::string &name = i->first;
202 const std::string &value = i->second;
203 lua_pushlstring(L, name.c_str(), name.size());
204 lua_pushlstring(L, value.c_str(), value.size());
208 lua_setfield(L, -2, "fields");
211 Inventory *inv = meta->getInventory();
213 std::vector<const InventoryList*> lists = inv->getLists();
214 for(std::vector<const InventoryList*>::const_iterator
215 i = lists.begin(); i != lists.end(); i++){
216 inventory_get_list_to_lua(inv, (*i)->getName().c_str(), L);
217 lua_setfield(L, -2, (*i)->getName().c_str());
220 lua_setfield(L, -2, "inventory");
224 // from_table(self, table)
225 int NodeMetaRef::l_from_table(lua_State *L)
227 NodeMetaRef *ref = checkobject(L, 1);
230 if(lua_isnil(L, base)){
232 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
233 lua_pushboolean(L, true);
237 // Has metadata; clear old one first
238 ref->m_env->getMap().removeNodeMetadata(ref->m_p);
239 // Create new metadata
240 NodeMetadata *meta = getmeta(ref, true);
242 lua_getfield(L, base, "fields");
243 int fieldstable = lua_gettop(L);
245 while(lua_next(L, fieldstable) != 0){
246 // key at index -2 and value at index -1
247 std::string name = lua_tostring(L, -2);
249 const char *cs = lua_tolstring(L, -1, &cl);
250 std::string value(cs, cl);
251 meta->setString(name, value);
252 lua_pop(L, 1); // removes value, keeps key for next iteration
255 Inventory *inv = meta->getInventory();
256 lua_getfield(L, base, "inventory");
257 int inventorytable = lua_gettop(L);
259 while(lua_next(L, inventorytable) != 0){
260 // key at index -2 and value at index -1
261 std::string name = lua_tostring(L, -2);
262 inventory_set_list_from_lua(inv, name.c_str(), L, -1);
263 lua_pop(L, 1); // removes value, keeps key for next iteration
265 reportMetadataChange(ref);
266 lua_pushboolean(L, true);
271 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
277 NodeMetaRef::~NodeMetaRef()
281 // Creates an NodeMetaRef and leaves it on top of stack
282 // Not callable from Lua; all references are created on the C side.
283 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
285 NodeMetaRef *o = new NodeMetaRef(p, env);
286 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
287 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
288 luaL_getmetatable(L, className);
289 lua_setmetatable(L, -2);
292 void NodeMetaRef::Register(lua_State *L)
295 int methodtable = lua_gettop(L);
296 luaL_newmetatable(L, className);
297 int metatable = lua_gettop(L);
299 lua_pushliteral(L, "__metatable");
300 lua_pushvalue(L, methodtable);
301 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
303 lua_pushliteral(L, "__index");
304 lua_pushvalue(L, methodtable);
305 lua_settable(L, metatable);
307 lua_pushliteral(L, "__gc");
308 lua_pushcfunction(L, gc_object);
309 lua_settable(L, metatable);
311 lua_pop(L, 1); // drop metatable
313 luaL_openlib(L, 0, methods, 0); // fill methodtable
314 lua_pop(L, 1); // drop methodtable
316 // Cannot be created from Lua
317 //lua_register(L, className, create_object);
320 const char NodeMetaRef::className[] = "NodeMetaRef";
321 const luaL_reg NodeMetaRef::methods[] = {
322 luamethod(NodeMetaRef, get_string),
323 luamethod(NodeMetaRef, set_string),
324 luamethod(NodeMetaRef, get_int),
325 luamethod(NodeMetaRef, set_int),
326 luamethod(NodeMetaRef, get_float),
327 luamethod(NodeMetaRef, set_float),
328 luamethod(NodeMetaRef, get_inventory),
329 luamethod(NodeMetaRef, to_table),
330 luamethod(NodeMetaRef, from_table),
335 Node metadata inventory callbacks
338 // Return number of accepted items to be moved
339 int scriptapi_nodemeta_inventory_allow_move(lua_State *L, v3s16 p,
340 const std::string &from_list, int from_index,
341 const std::string &to_list, int to_index,
342 int count, ServerActiveObject *player)
345 assert(lua_checkstack(L, 20));
346 StackUnroller stack_unroller(L);
348 INodeDefManager *ndef = get_server(L)->ndef();
350 // If node doesn't exist, we don't know what callback to call
351 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
352 if(node.getContent() == CONTENT_IGNORE)
355 // Push callback function on stack
356 if(!get_item_callback(L, ndef->get(node).name.c_str(),
357 "allow_metadata_inventory_move"))
360 // function(pos, from_list, from_index, to_list, to_index, count, player)
364 lua_pushstring(L, from_list.c_str());
366 lua_pushinteger(L, from_index + 1);
368 lua_pushstring(L, to_list.c_str());
370 lua_pushinteger(L, to_index + 1);
372 lua_pushinteger(L, count);
374 objectref_get_or_create(L, player);
375 if(lua_pcall(L, 7, 1, 0))
376 script_error(L, "error: %s", lua_tostring(L, -1));
377 if(!lua_isnumber(L, -1))
378 throw LuaError(L, "allow_metadata_inventory_move should return a number");
379 return luaL_checkinteger(L, -1);
382 // Return number of accepted items to be put
383 int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p,
384 const std::string &listname, int index, ItemStack &stack,
385 ServerActiveObject *player)
388 assert(lua_checkstack(L, 20));
389 StackUnroller stack_unroller(L);
391 INodeDefManager *ndef = get_server(L)->ndef();
393 // If node doesn't exist, we don't know what callback to call
394 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
395 if(node.getContent() == CONTENT_IGNORE)
398 // Push callback function on stack
399 if(!get_item_callback(L, ndef->get(node).name.c_str(),
400 "allow_metadata_inventory_put"))
403 // Call function(pos, listname, index, stack, player)
407 lua_pushstring(L, listname.c_str());
409 lua_pushinteger(L, index + 1);
411 LuaItemStack::create(L, stack);
413 objectref_get_or_create(L, player);
414 if(lua_pcall(L, 5, 1, 0))
415 script_error(L, "error: %s", lua_tostring(L, -1));
416 if(!lua_isnumber(L, -1))
417 throw LuaError(L, "allow_metadata_inventory_put should return a number");
418 return luaL_checkinteger(L, -1);
421 // Return number of accepted items to be taken
422 int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
423 const std::string &listname, int index, ItemStack &stack,
424 ServerActiveObject *player)
427 assert(lua_checkstack(L, 20));
428 StackUnroller stack_unroller(L);
430 INodeDefManager *ndef = get_server(L)->ndef();
432 // If node doesn't exist, we don't know what callback to call
433 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
434 if(node.getContent() == CONTENT_IGNORE)
437 // Push callback function on stack
438 if(!get_item_callback(L, ndef->get(node).name.c_str(),
439 "allow_metadata_inventory_take"))
442 // Call function(pos, listname, index, count, player)
446 lua_pushstring(L, listname.c_str());
448 lua_pushinteger(L, index + 1);
450 LuaItemStack::create(L, stack);
452 objectref_get_or_create(L, player);
453 if(lua_pcall(L, 5, 1, 0))
454 script_error(L, "error: %s", lua_tostring(L, -1));
455 if(!lua_isnumber(L, -1))
456 throw LuaError(L, "allow_metadata_inventory_take should return a number");
457 return luaL_checkinteger(L, -1);
460 // Report moved items
461 void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p,
462 const std::string &from_list, int from_index,
463 const std::string &to_list, int to_index,
464 int count, ServerActiveObject *player)
467 assert(lua_checkstack(L, 20));
468 StackUnroller stack_unroller(L);
470 INodeDefManager *ndef = get_server(L)->ndef();
472 // If node doesn't exist, we don't know what callback to call
473 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
474 if(node.getContent() == CONTENT_IGNORE)
477 // Push callback function on stack
478 if(!get_item_callback(L, ndef->get(node).name.c_str(),
479 "on_metadata_inventory_move"))
482 // function(pos, from_list, from_index, to_list, to_index, count, player)
486 lua_pushstring(L, from_list.c_str());
488 lua_pushinteger(L, from_index + 1);
490 lua_pushstring(L, to_list.c_str());
492 lua_pushinteger(L, to_index + 1);
494 lua_pushinteger(L, count);
496 objectref_get_or_create(L, player);
497 if(lua_pcall(L, 7, 0, 0))
498 script_error(L, "error: %s", lua_tostring(L, -1));
502 void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p,
503 const std::string &listname, int index, ItemStack &stack,
504 ServerActiveObject *player)
507 assert(lua_checkstack(L, 20));
508 StackUnroller stack_unroller(L);
510 INodeDefManager *ndef = get_server(L)->ndef();
512 // If node doesn't exist, we don't know what callback to call
513 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
514 if(node.getContent() == CONTENT_IGNORE)
517 // Push callback function on stack
518 if(!get_item_callback(L, ndef->get(node).name.c_str(),
519 "on_metadata_inventory_put"))
522 // Call function(pos, listname, index, stack, player)
526 lua_pushstring(L, listname.c_str());
528 lua_pushinteger(L, index + 1);
530 LuaItemStack::create(L, stack);
532 objectref_get_or_create(L, player);
533 if(lua_pcall(L, 5, 0, 0))
534 script_error(L, "error: %s", lua_tostring(L, -1));
537 // Report taken items
538 void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
539 const std::string &listname, int index, ItemStack &stack,
540 ServerActiveObject *player)
543 assert(lua_checkstack(L, 20));
544 StackUnroller stack_unroller(L);
546 INodeDefManager *ndef = get_server(L)->ndef();
548 // If node doesn't exist, we don't know what callback to call
549 MapNode node = get_env(L)->getMap().getNodeNoEx(p);
550 if(node.getContent() == CONTENT_IGNORE)
553 // Push callback function on stack
554 if(!get_item_callback(L, ndef->get(node).name.c_str(),
555 "on_metadata_inventory_take"))
558 // Call function(pos, listname, index, stack, player)
562 lua_pushstring(L, listname.c_str());
564 lua_pushinteger(L, index + 1);
566 LuaItemStack::create(L, stack);
568 objectref_get_or_create(L, player);
569 if(lua_pcall(L, 5, 0, 0))
570 script_error(L, "error: %s", lua_tostring(L, -1));