From 1efdc36b22532807d21a0beac94524e3eacfe7bc Mon Sep 17 00:00:00 2001 From: Kahrl Date: Sat, 21 Jan 2012 21:21:41 +0100 Subject: Inventory menu (with dragging) improved. Crafting is now handled via a IACTION_CRAFT inventory action. --- src/inventorymanager.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 2 deletions(-) (limited to 'src/inventorymanager.cpp') diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp index eeb293cb1..b42a80673 100644 --- a/src/inventorymanager.cpp +++ b/src/inventorymanager.cpp @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "main.h" // for g_settings #include "settings.h" #include "utility.h" +#include "craftdef.h" /* InventoryLocation @@ -124,6 +125,10 @@ InventoryAction * InventoryAction::deSerialize(std::istream &is) { a = new IDropAction(is); } + else if(type == "Craft") + { + a = new ICraftAction(is); + } return a; } @@ -152,7 +157,7 @@ IMoveAction::IMoveAction(std::istream &is) to_i = stoi(ts); } -void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player) +void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef) { Inventory *inv_from = mgr->getInventory(from_inv); Inventory *inv_to = mgr->getInventory(to_inv); @@ -273,7 +278,7 @@ IDropAction::IDropAction(std::istream &is) from_i = stoi(ts); } -void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player) +void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef) { Inventory *inv_from = mgr->getInventory(from_inv); @@ -332,3 +337,120 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player) <<" i="<getInventory(craft_inv); + + if(!inv_craft){ + infostream<<"ICraftAction::apply(): FAIL: inventory not found: " + <<"craft_inv=\""<getList("craft"); + InventoryList *list_craftresult = inv_craft->getList("craftresult"); + + /* + If a list doesn't exist or the source item doesn't exist + */ + if(!list_craft){ + infostream<<"ICraftAction::apply(): FAIL: craft list not found: " + <<"craft_inv=\""<getSize() < 1){ + infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: " + <<"craft_inv=\""<itemFits(0, crafted)) + { + // Decrement input and add crafting output + getCraftingResult(inv_craft, crafted, true, gamedef); + list_craftresult->addItem(0, crafted); + mgr->setInventoryModified(craft_inv); + + actionstream<getDescription() + <<" crafts " + < 1) + count_remaining--; + + // Get next crafting result + found = getCraftingResult(inv_craft, crafted, false, gamedef); + } + + infostream<<"ICraftAction::apply(): crafted " + <<" craft_inv=\""<getList("craft"); + if(!clist || clist->getSize() != 9) + return false; + + // Mangle crafting grid to an another format + CraftInput ci; + ci.method = CRAFT_METHOD_NORMAL; + ci.width = 3; + for(u16 i=0; i<9; i++) + ci.items.push_back(clist->getItem(i)); + + // Find out what is crafted and add it to result item slot + CraftOutput co; + bool found = gamedef->getCraftDefManager()->getCraftResult( + ci, co, decrementInput, gamedef); + if(found) + result.deSerialize(co.item, gamedef->getItemDefManager()); + + if(found && decrementInput) + { + // CraftInput has been changed, apply changes in clist + for(u16 i=0; i<9; i++) + { + clist->changeItem(i, ci.items[i]); + } + } + + return found; +} + -- cgit v1.2.3