From 9eaf93d41d6745b877f8f52cf54b21050abefda1 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 25 Jul 2012 02:36:54 +0300 Subject: Detached inventory callbacks and reworked node metadata callbacks --- src/guiFormSpecMenu.cpp | 3 +- src/inventorymanager.cpp | 365 ++++++++++++++++++++++++++++------------- src/scriptapi.cpp | 410 +++++++++++++++++++++++++++++++++++++++++++++-- src/scriptapi.h | 61 ++++++- 4 files changed, 707 insertions(+), 132 deletions(-) (limited to 'src') diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 3eb056625..7ded2b37e 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -215,10 +215,9 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) } else{ invsize.Y = stof(f.next(";")); - errorstream<<"WARNING: invsize is deprecated, use size"<getItem(from_i).count; + + int src_can_take_count = 0xffff; + int dst_can_put_count = 0xffff; - // Handle node metadata move - if(from_inv.type == InventoryLocation::NODEMETA && - to_inv.type == InventoryLocation::NODEMETA && - from_inv.p != to_inv.p) + /* Query detached inventories */ + + // Move occurs in the same detached inventory + if(from_inv.type == InventoryLocation::DETACHED && + to_inv.type == InventoryLocation::DETACHED && + from_inv.name == to_inv.name) { - errorstream<<"Directly moving items between two nodes is " - <<"disallowed."<getEnv()->getLua(); + src_can_take_count = scriptapi_detached_inventory_allow_move( + L, from_inv.name, from_list, from_i, + to_list, to_i, try_take_count, player); + dst_can_put_count = src_can_take_count; + } + else + { + // Destination is detached + if(to_inv.type == InventoryLocation::DETACHED) + { + lua_State *L = player->getEnv()->getLua(); + ItemStack src_item = list_from->getItem(from_i); + src_item.count = try_take_count; + dst_can_put_count = scriptapi_detached_inventory_allow_put( + L, to_inv.name, to_list, to_i, src_item, player); + } + // Source is detached + if(from_inv.type == InventoryLocation::DETACHED) + { + lua_State *L = player->getEnv()->getLua(); + src_can_take_count = scriptapi_detached_inventory_allow_take( + L, from_inv.name, from_list, from_i, try_take_count, player); + } } - else if(from_inv.type == InventoryLocation::NODEMETA && + + /* Query node metadata inventories */ + + // Both endpoints are nodemeta + // Move occurs in the same nodemeta inventory + if(from_inv.type == InventoryLocation::NODEMETA && to_inv.type == InventoryLocation::NODEMETA && from_inv.p == to_inv.p) { lua_State *L = player->getEnv()->getLua(); - int count0 = count; - if(count0 == 0) - count0 = list_from->getItem(from_i).count; - infostream<getDescription()<<" moving "<getEnv()->getLua(); - int count0 = count; - if(count0 == 0) - count0 = list_from->getItem(from_i).count; - infostream<getDescription()<<" taking "<addItem(to_i, return_stack); - list_to->addItem(return_stack); // Force return of everything - } - // Handle node metadata offer - else if(to_inv.type == InventoryLocation::NODEMETA) - { - lua_State *L = player->getEnv()->getLua(); - int count0 = count; - if(count0 == 0) - count0 = list_from->getItem(from_i).count; - ItemStack offer_stack = list_from->takeItem(from_i, count0); - infostream<getDescription()<<" offering " - <addItem(from_i, reject_stack); - list_from->addItem(reject_stack); // Force return of everything - } - // Handle regular move + src_can_take_count = scriptapi_nodemeta_inventory_allow_move( + L, from_inv.p, from_list, from_i, + to_list, to_i, try_take_count, player); + dst_can_put_count = src_can_take_count; + } else { - /* - This performs the actual movement - - If something is wrong (source item is empty, destination is the - same as source), nothing happens - */ - list_from->moveItem(from_i, list_to, to_i, count); - - infostream<<"IMoveAction::apply(): moved " + // Destination is nodemeta + if(to_inv.type == InventoryLocation::NODEMETA) + { + lua_State *L = player->getEnv()->getLua(); + ItemStack src_item = list_from->getItem(from_i); + src_item.count = try_take_count; + dst_can_put_count = scriptapi_nodemeta_inventory_allow_put( + L, to_inv.p, to_list, to_i, src_item, player); + } + // Source is nodemeta + if(from_inv.type == InventoryLocation::NODEMETA) + { + lua_State *L = player->getEnv()->getLua(); + src_can_take_count = scriptapi_nodemeta_inventory_allow_take( + L, from_inv.p, from_list, from_i, try_take_count, player); + } + } + + /* Modify count according to collected data */ + int new_count = try_take_count; + if(new_count > src_can_take_count) + new_count = src_can_take_count; + if(new_count > dst_can_put_count) + new_count = dst_can_put_count; + + /* If no items will be moved, don't go further */ + if(new_count == 0) + { + infostream<<"IMoveAction::apply(): move was completely disallowed: " <<" count="<changeItem(from_i, ItemStack()); - else - item1 = list_from->takeItem(from_i, count); + lua_State *L = player->getEnv()->getLua(); + src_can_take_count = scriptapi_nodemeta_inventory_allow_take( + L, from_inv.p, from_list, from_i, take_count, player); } - // Drop the item and apply the returned ItemStack - ItemStack item2 = item1; - if(scriptapi_item_on_drop(player->getEnv()->getLua(), item2, player, + if(src_can_take_count < take_count) + take_count = src_can_take_count; + + int actually_dropped_count = 0; + + // Drop the item + ItemStack item1 = list_from->getItem(from_i); + if(scriptapi_item_on_drop(player->getEnv()->getLua(), item1, player, player->getBasePosition() + v3f(0,1,0))) { - if(g_settings->getBool("creative_mode") == true - && from_inv.type == InventoryLocation::PLAYER) - item2 = item1; // creative mode + actually_dropped_count = take_count - item1.count; + + if(actually_dropped_count == 0){ + infostream<<"Actually dropped no items"<addItem(from_i, item2); + // Don't remove from inventory in creative mode + if(g_settings->getBool("creative_mode") == true + && from_inv.type == InventoryLocation::PLAYER){ + } + else{ + // Take item from source list + ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count); - // Unless we have put the same amount back as we took in the first place, - // set inventory modified flag - if(item2.count != item1.count) + if(item2.count != actually_dropped_count) + errorstream<<"Could not take dropped count of items"<setInventoryModified(from_inv); + } } infostream<<"IDropAction::apply(): dropped " @@ -409,6 +534,26 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame <<" list=\""<ndef(); + + // If node doesn't exist, we don't know what callback to call + MapNode node = get_env(L)->getMap().getNodeNoEx(p); + if(node.getContent() == CONTENT_IGNORE) + return 0; + + // Push callback function on stack + if(!get_item_callback(L, ndef->get(node).name.c_str(), + "allow_metadata_inventory_move")) + return count; + + // function(pos, from_list, from_index, to_list, to_index, count, player) + // pos + push_v3s16(L, p); + // from_list + lua_pushstring(L, from_list.c_str()); + // from_index + lua_pushinteger(L, from_index + 1); + // to_list + lua_pushstring(L, to_list.c_str()); + // to_index + lua_pushinteger(L, to_index + 1); + // count + lua_pushinteger(L, count); + // player + objectref_get_or_create(L, player); + if(lua_pcall(L, 7, 1, 0)) + script_error(L, "error: %s", lua_tostring(L, -1)); + return luaL_checkinteger(L, -1); +} + +// Return number of accepted items to be put +int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p, + const std::string &listname, int index, ItemStack &stack, + ServerActiveObject *player) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + StackUnroller stack_unroller(L); + + INodeDefManager *ndef = get_server(L)->ndef(); + + // If node doesn't exist, we don't know what callback to call + MapNode node = get_env(L)->getMap().getNodeNoEx(p); + if(node.getContent() == CONTENT_IGNORE) + return 0; + + // Push callback function on stack + if(!get_item_callback(L, ndef->get(node).name.c_str(), + "allow_metadata_inventory_put")) + return stack.count; + + // Call function(pos, listname, index, stack, player) + // pos + push_v3s16(L, p); + // listname + lua_pushstring(L, listname.c_str()); + // index + lua_pushinteger(L, index + 1); + // stack + LuaItemStack::create(L, stack); + // player + objectref_get_or_create(L, player); + if(lua_pcall(L, 5, 1, 0)) + script_error(L, "error: %s", lua_tostring(L, -1)); + return luaL_checkinteger(L, -1); +} + +// Return number of accepted items to be taken +int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p, + const std::string &listname, int index, int count, + ServerActiveObject *player) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + StackUnroller stack_unroller(L); + + INodeDefManager *ndef = get_server(L)->ndef(); + + // If node doesn't exist, we don't know what callback to call + MapNode node = get_env(L)->getMap().getNodeNoEx(p); + if(node.getContent() == CONTENT_IGNORE) + return 0; + + // Push callback function on stack + if(!get_item_callback(L, ndef->get(node).name.c_str(), + "allow_metadata_inventory_take")) + return count; + + // Call function(pos, listname, index, count, player) + // pos + push_v3s16(L, p); + // listname + lua_pushstring(L, listname.c_str()); + // index + lua_pushinteger(L, index + 1); + // count + lua_pushinteger(L, count); + // player + objectref_get_or_create(L, player); + if(lua_pcall(L, 5, 1, 0)) + script_error(L, "error: %s", lua_tostring(L, -1)); + return luaL_checkinteger(L, -1); +} + +// Report moved items +void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p, const std::string &from_list, int from_index, const std::string &to_list, int to_index, int count, ServerActiveObject *player) @@ -5756,18 +5877,26 @@ void scriptapi_node_on_metadata_inventory_move(lua_State *L, v3s16 p, return; // function(pos, from_list, from_index, to_list, to_index, count, player) + // pos push_v3s16(L, p); + // from_list lua_pushstring(L, from_list.c_str()); + // from_index lua_pushinteger(L, from_index + 1); + // to_list lua_pushstring(L, to_list.c_str()); + // to_index lua_pushinteger(L, to_index + 1); + // count lua_pushinteger(L, count); + // player objectref_get_or_create(L, player); if(lua_pcall(L, 7, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); } -ItemStack scriptapi_node_on_metadata_inventory_offer(lua_State *L, v3s16 p, +// Report put items +void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p, const std::string &listname, int index, ItemStack &stack, ServerActiveObject *player) { @@ -5780,25 +5909,30 @@ ItemStack scriptapi_node_on_metadata_inventory_offer(lua_State *L, v3s16 p, // If node doesn't exist, we don't know what callback to call MapNode node = get_env(L)->getMap().getNodeNoEx(p); if(node.getContent() == CONTENT_IGNORE) - return stack; + return; // Push callback function on stack if(!get_item_callback(L, ndef->get(node).name.c_str(), - "on_metadata_inventory_offer")) - return stack; + "on_metadata_inventory_put")) + return; // Call function(pos, listname, index, stack, player) + // pos push_v3s16(L, p); + // listname lua_pushstring(L, listname.c_str()); + // index lua_pushinteger(L, index + 1); + // stack LuaItemStack::create(L, stack); + // player objectref_get_or_create(L, player); - if(lua_pcall(L, 5, 1, 0)) + if(lua_pcall(L, 5, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); - return read_item(L, -1); } -ItemStack scriptapi_node_on_metadata_inventory_take(lua_State *L, v3s16 p, +// Report taken items +void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p, const std::string &listname, int index, int count, ServerActiveObject *player) { @@ -5811,22 +5945,270 @@ ItemStack scriptapi_node_on_metadata_inventory_take(lua_State *L, v3s16 p, // If node doesn't exist, we don't know what callback to call MapNode node = get_env(L)->getMap().getNodeNoEx(p); if(node.getContent() == CONTENT_IGNORE) - return ItemStack(); + return; // Push callback function on stack if(!get_item_callback(L, ndef->get(node).name.c_str(), "on_metadata_inventory_take")) - return ItemStack(); + return; // Call function(pos, listname, index, count, player) + // pos push_v3s16(L, p); + // listname lua_pushstring(L, listname.c_str()); + // index lua_pushinteger(L, index + 1); + // count lua_pushinteger(L, count); + // player + objectref_get_or_create(L, player); + if(lua_pcall(L, 5, 0, 0)) + script_error(L, "error: %s", lua_tostring(L, -1)); +} + +/* + Detached inventory callbacks +*/ + +// Retrieves minetest.detached_inventories[name][callbackname] +// If that is nil or on error, return false and stack is unchanged +// If that is a function, returns true and pushes the +// function onto the stack +static bool get_detached_inventory_callback(lua_State *L, + const std::string &name, const char *callbackname) +{ + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "detached_inventories"); + lua_remove(L, -2); + luaL_checktype(L, -1, LUA_TTABLE); + lua_getfield(L, -1, name.c_str()); + lua_remove(L, -2); + // Should be a table + if(lua_type(L, -1) != LUA_TTABLE) + { + errorstream<<"Item \""< &fields, ServerActiveObject *sender); -// Moves items -void scriptapi_node_on_metadata_inventory_move(lua_State *L, v3s16 p, + +/* Node metadata inventory callbacks */ +// Return number of accepted items to be moved +int scriptapi_nodemeta_inventory_allow_move(lua_State *L, v3s16 p, + const std::string &from_list, int from_index, + const std::string &to_list, int to_index, + int count, ServerActiveObject *player); +// Return number of accepted items to be put +int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p, + const std::string &listname, int index, ItemStack &stack, + ServerActiveObject *player); +// Return number of accepted items to be taken +int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p, + const std::string &listname, int index, int count, + ServerActiveObject *player); +// Report moved items +void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p, + const std::string &from_list, int from_index, + const std::string &to_list, int to_index, + int count, ServerActiveObject *player); +// Report put items +void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p, + const std::string &listname, int index, ItemStack &stack, + ServerActiveObject *player); +// Report taken items +void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p, + const std::string &listname, int index, int count, + ServerActiveObject *player); + +/* Detached inventory callbacks */ +// Return number of accepted items to be moved +int scriptapi_detached_inventory_allow_move(lua_State *L, + const std::string &name, + const std::string &from_list, int from_index, + const std::string &to_list, int to_index, + int count, ServerActiveObject *player); +// Return number of accepted items to be put +int scriptapi_detached_inventory_allow_put(lua_State *L, + const std::string &name, + const std::string &listname, int index, ItemStack &stack, + ServerActiveObject *player); +// Return number of accepted items to be taken +int scriptapi_detached_inventory_allow_take(lua_State *L, + const std::string &name, + const std::string &listname, int index, int count, + ServerActiveObject *player); +// Report moved items +void scriptapi_detached_inventory_on_move(lua_State *L, + const std::string &name, const std::string &from_list, int from_index, const std::string &to_list, int to_index, int count, ServerActiveObject *player); -// Inserts items, returns rejected items -ItemStack scriptapi_node_on_metadata_inventory_offer(lua_State *L, v3s16 p, +// Report put items +void scriptapi_detached_inventory_on_put(lua_State *L, + const std::string &name, const std::string &listname, int index, ItemStack &stack, ServerActiveObject *player); -// Takes items, returns taken items -ItemStack scriptapi_node_on_metadata_inventory_take(lua_State *L, v3s16 p, +// Report taken items +void scriptapi_detached_inventory_on_take(lua_State *L, + const std::string &name, const std::string &listname, int index, int count, ServerActiveObject *player); -- cgit v1.2.3