/* Minetest-c55 Copyright (C) 2010-2011 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "inventorymanager.h" #include "serverremoteplayer.h" #include "log.h" #include "mapblock.h" // getNodeBlockPos /* InventoryManager */ // Wrapper for old code Inventory* InventoryManager::getInventory(InventoryContext *c, std::string id) { if(id == "current_player") { assert(c->current_player); InventoryLocation loc; loc.setPlayer(c->current_player->getName()); return getInventory(loc); } Strfnd fn(id); std::string id0 = fn.next(":"); if(id0 == "nodemeta") { v3s16 p; p.X = stoi(fn.next(",")); p.Y = stoi(fn.next(",")); p.Z = stoi(fn.next(",")); InventoryLocation loc; loc.setNodeMeta(p); return getInventory(loc); } errorstream<<__FUNCTION_NAME<<": unknown id "<current_player); InventoryLocation loc; loc.setPlayer(c->current_player->getName()); setInventoryModified(loc); return; } Strfnd fn(id); std::string id0 = fn.next(":"); if(id0 == "nodemeta") { v3s16 p; p.X = stoi(fn.next(",")); p.Y = stoi(fn.next(",")); p.Z = stoi(fn.next(",")); v3s16 blockpos = getNodeBlockPos(p); InventoryLocation loc; loc.setNodeMeta(p); setInventoryModified(loc); return; } errorstream<<__FUNCTION_NAME<<": unknown id "<current_player == NULL) return "current_player=NULL"; else return std::string("current_player=") + c->current_player->getName(); } IMoveAction::IMoveAction(std::istream &is) { std::string ts; std::getline(is, ts, ' '); count = stoi(ts); std::getline(is, from_inv, ' '); std::getline(is, from_list, ' '); std::getline(is, ts, ' '); from_i = stoi(ts); std::getline(is, to_inv, ' '); std::getline(is, to_list, ' '); std::getline(is, ts, ' '); to_i = stoi(ts); } void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr, ServerEnvironment *env) { Inventory *inv_from = mgr->getInventory(c, from_inv); Inventory *inv_to = mgr->getInventory(c, to_inv); if(!inv_from){ infostream<<"IMoveAction::apply(): FAIL: source inventory not found: " <<"context=["<getList(from_list); InventoryList *list_to = inv_to->getList(to_list); /* If a list doesn't exist or the source item doesn't exist */ if(!list_from){ infostream<<"IMoveAction::apply(): FAIL: source list not found: " <<"context=["<getItem(from_i) == NULL) { infostream<<"IMoveAction::apply(): FAIL: source item not found: " <<"context=["<( static_cast( c->current_player )); Inventory *inv_from = mgr->getInventory(c, from_inv); if(!inv_from){ infostream<<"IDropAction::apply(): FAIL: source inventory not found: " <<"context=["<getList(from_list); /* If a list doesn't exist or the source item doesn't exist */ if(!list_from){ infostream<<"IDropAction::apply(): FAIL: source list not found: " <<"context=["<getItem(from_i); if(item == NULL) { infostream<<"IDropAction::apply(): FAIL: source item not found: " <<"context=["<getMaterial() != num) return false; } else { if(mitem->getNodeName() != name) return false; } } else if(type == ITEM_CRAFT) { if(itemname != "CraftItem") return false; CraftItem *mitem = (CraftItem*)item; if(mitem->getSubName() != name) return false; } else if(type == ITEM_TOOL) { // Not supported yet assert(0); } else if(type == ITEM_MBO) { // Not supported yet assert(0); } else { // Not supported yet assert(0); } return true; } bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs) { u16 items_min_x = 100; u16 items_max_x = 100; u16 items_min_y = 100; u16 items_max_y = 100; for(u16 y=0; y<3; y++) for(u16 x=0; x<3; x++) { if(items[y*3 + x] == NULL) continue; if(items_min_x == 100 || x < items_min_x) items_min_x = x; if(items_min_y == 100 || y < items_min_y) items_min_y = y; if(items_max_x == 100 || x > items_max_x) items_max_x = x; if(items_max_y == 100 || y > items_max_y) items_max_y = y; } // No items at all, just return false if(items_min_x == 100) return false; u16 items_w = items_max_x - items_min_x + 1; u16 items_h = items_max_y - items_min_y + 1; u16 specs_min_x = 100; u16 specs_max_x = 100; u16 specs_min_y = 100; u16 specs_max_y = 100; for(u16 y=0; y<3; y++) for(u16 x=0; x<3; x++) { if(specs[y*3 + x].type == ITEM_NONE) continue; if(specs_min_x == 100 || x < specs_min_x) specs_min_x = x; if(specs_min_y == 100 || y < specs_min_y) specs_min_y = y; if(specs_max_x == 100 || x > specs_max_x) specs_max_x = x; if(specs_max_y == 100 || y > specs_max_y) specs_max_y = y; } // No specs at all, just return false if(specs_min_x == 100) return false; u16 specs_w = specs_max_x - specs_min_x + 1; u16 specs_h = specs_max_y - specs_min_y + 1; // Different sizes if(items_w != specs_w || items_h != specs_h) return false; for(u16 y=0; y items_max_x) items_max_x = x; if(items_max_y == 100 || y > items_max_y) items_max_y = y; } // No items at all, just return false if(items_min_x == 100) return false; u16 items_w = items_max_x - items_min_x + 1; u16 items_h = items_max_y - items_min_y + 1; u16 specs_min_x = 100; u16 specs_max_x = 100; u16 specs_min_y = 100; u16 specs_max_y = 100; for(u16 y=0; y<3; y++) for(u16 x=0; x<3; x++) { if(specs[y*3 + x] == NULL) continue; if(specs_min_x == 100 || x < specs_min_x) specs_min_x = x; if(specs_min_y == 100 || y < specs_min_y) specs_min_y = y; if(specs_max_x == 100 || x > specs_max_x) specs_max_x = x; if(specs_max_y == 100 || y > specs_max_y) specs_max_y = y; } // No specs at all, just return false if(specs_min_x == 100) return false; u16 specs_w = specs_max_x - specs_min_x + 1; u16 specs_h = specs_max_y - specs_min_y + 1; // Different sizes if(items_w != specs_w || items_h != specs_h) return false; for(u16 y=0; yisSubsetOf(item)) return false; } return true; }