From 004401a70aafe3526a48d50cae94bea41c1d5176 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 14 Aug 2012 14:41:57 +0000 Subject: Crafting window crafting result shift-click handling git-svn-id: http://mc-server.googlecode.com/svn/trunk@736 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cCraftingWindow.cpp | 62 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/source/cCraftingWindow.cpp b/source/cCraftingWindow.cpp index a41eede70..40e41f2c4 100644 --- a/source/cCraftingWindow.cpp +++ b/source/cCraftingWindow.cpp @@ -18,6 +18,21 @@ +/* These numbers are valid for the underlying cInventory object, because that's where we're sending the +MoveItem() and HowManyCanFit() function calls +*/ +enum +{ + SLOT_INVENTORY_MIN = 9, + SLOT_INVENTORY_MAX = 35, + SLOT_HOTBAR_MIN = 36, + SLOT_HOTBAR_MAX = 44 +} ; + + + + + cCraftingWindow::cCraftingWindow( cWindowOwner* a_Owner, bool a_bInventoryVisible ) : cWindow(a_Owner, a_bInventoryVisible, cWindow::Workbench, 1) { @@ -162,7 +177,44 @@ void cCraftingWindow::ShiftClicked(cPacket_WindowClick * a_ClickPacket, cPlayer void cCraftingWindow::ShiftClickedCraftingResult(short a_Slot, cPlayer & a_Player) { - // TODO + // Craft until either the recipe changes (due to ingredients) or there's not enough storage for the result + cInventory & Inventory = a_Player.GetInventory(); + cItem * CraftingResult = GetSlot(SLOT_CRAFTING_RESULT); + if ((CraftingResult == NULL) || CraftingResult->IsEmpty()) + { + return; + } + cItem ResultCopy = *CraftingResult; + int HowManyItemsWillFit = Inventory.HowManyCanFit(CraftingResult->m_ItemID, CraftingResult->m_ItemHealth, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX); + HowManyItemsWillFit += Inventory.HowManyCanFit(CraftingResult->m_ItemID, CraftingResult->m_ItemHealth, SLOT_HOTBAR_MIN, SLOT_HOTBAR_MAX); + int HowManyPassesWillFit = HowManyItemsWillFit / CraftingResult->m_ItemCount; + for (int i = 0; i < HowManyPassesWillFit; i++) + { + // First try moving into the hotbar: + int NumMoved = Inventory.MoveItem(CraftingResult->m_ItemID, CraftingResult->m_ItemHealth, CraftingResult->m_ItemCount, SLOT_HOTBAR_MIN, SLOT_HOTBAR_MAX); + + // If something didn't fit, move into main inventory: + if (NumMoved < CraftingResult->m_ItemCount) + { + NumMoved += Inventory.MoveItem(CraftingResult->m_ItemID, CraftingResult->m_ItemHealth, CraftingResult->m_ItemCount - NumMoved, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX); + ASSERT(NumMoved == CraftingResult->m_ItemCount); // We checked earlier that we can fit this many items + } + + // "Use" the crafting recipe once: + cCraftingGrid Grid(GetSlots() + 1, 3, 3); + cCraftingRecipe Recipe(Grid); + cRoot::Get()->GetCraftingRecipes()->GetRecipe(&a_Player, Grid, Recipe); + Recipe.ConsumeIngredients(Grid); + Grid.CopyToItems(GetSlots() + 1); + cRoot::Get()->GetCraftingRecipes()->GetRecipe(&a_Player, Grid, Recipe); + GetSlots()[SLOT_CRAFTING_RESULT] = Recipe.GetResult(); + + // If the recipe changed, abort: + if (!Recipe.GetResult().Equals(ResultCopy)) + { + break; + } + } } @@ -171,14 +223,6 @@ void cCraftingWindow::ShiftClickedCraftingResult(short a_Slot, cPlayer & a_Playe void cCraftingWindow::ShiftClickedCraftingGrid(short a_Slot, cPlayer & a_Player) { - enum - { - SLOT_INVENTORY_MIN = 9, - SLOT_INVENTORY_MAX = 35, - SLOT_HOTBAR_MIN = 36, - SLOT_HOTBAR_MAX = 44 - } ; - cInventory & Inventory = a_Player.GetInventory(); cItem * Item = GetSlot(a_Slot); if ((Item == NULL) || Item->IsEmpty()) -- cgit v1.2.3