2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "cSurvivalInventory.h"
|
|
|
|
#include "cPlayer.h"
|
|
|
|
#include "cClientHandle.h"
|
|
|
|
#include "cWindow.h"
|
|
|
|
#include "cItem.h"
|
|
|
|
#include "CraftingRecipes.h"
|
|
|
|
#include "cRoot.h"
|
2012-08-18 05:56:28 -04:00
|
|
|
#include "items/Item.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-11 15:54:57 -04:00
|
|
|
cSurvivalInventory::cSurvivalInventory(cPlayer* a_Owner)
|
|
|
|
: cInventory(a_Owner)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-11 15:54:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cSurvivalInventory::~cSurvivalInventory()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-11 15:54:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cSurvivalInventory::Clicked(
|
|
|
|
short a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed,
|
|
|
|
const cItem & a_HeldItem
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
cWindow * Window = GetWindow();
|
2012-08-11 15:54:57 -04:00
|
|
|
if (
|
2012-08-18 05:56:28 -04:00
|
|
|
a_IsShiftPressed && // Shift pressed
|
|
|
|
(Window != NULL) && // Window is valid
|
|
|
|
(Window->GetDraggingItem()->IsEmpty()) // Not dragging anything
|
2012-08-11 15:54:57 -04:00
|
|
|
)
|
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ShiftClicked(a_SlotNum);
|
2012-08-11 15:54:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
bool bDontCook = false;
|
2012-08-18 05:56:28 -04:00
|
|
|
if (Window != NULL)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Override for craft result slot
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_SlotNum == (short)c_CraftOffset)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGD("Clicked in craft result slot, item there: %d:%d (%d times) !!", m_Slots[c_CraftOffset].m_ItemID, m_Slots[c_CraftOffset].m_ItemHealth, m_Slots[c_CraftOffset].m_ItemCount);
|
|
|
|
cItem * DraggingItem = Window->GetDraggingItem();
|
2012-08-11 15:54:57 -04:00
|
|
|
if (DraggingItem->IsEmpty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
*DraggingItem = m_Slots[c_CraftOffset];
|
|
|
|
m_Slots[c_CraftOffset].Empty();
|
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
else if (DraggingItem->IsEqual(m_Slots[c_CraftOffset]))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
cItemHandler * Handler = ItemHandler(m_Slots[c_CraftOffset].m_ItemID);
|
|
|
|
if (DraggingItem->m_ItemCount + m_Slots[c_CraftOffset].m_ItemCount <= Handler->GetMaxStackSize())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
DraggingItem->m_ItemCount += m_Slots[c_CraftOffset].m_ItemCount;
|
2012-08-18 05:56:28 -04:00
|
|
|
m_Slots[c_CraftOffset].Empty();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bDontCook = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bDontCook = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
Window->Clicked(*m_Owner, 0, a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_HeldItem);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-11 15:54:57 -04:00
|
|
|
ASSERT(!"No inventory window! WTF?");
|
2012-06-14 09:06:06 -04:00
|
|
|
LOG("No Inventory window! WTF");
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if ((a_SlotNum >= (short)c_CraftOffset) && (a_SlotNum < (short)(c_CraftOffset + c_CraftSlots + 1)))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cCraftingGrid Grid(m_Slots + c_CraftOffset + 1, 2, 2);
|
|
|
|
cCraftingRecipe Recipe(Grid);
|
|
|
|
|
|
|
|
cRoot::Get()->GetCraftingRecipes()->GetRecipe(m_Owner, Grid, Recipe);
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if ((a_SlotNum == 0) && !bDontCook)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Consume the items from the crafting grid:
|
|
|
|
Recipe.ConsumeIngredients(Grid);
|
|
|
|
|
|
|
|
// Propagate grid back to m_Slots:
|
|
|
|
Grid.CopyToItems(m_Slots + c_CraftOffset + 1);
|
|
|
|
|
|
|
|
// Get the recipe for the new grid contents:
|
|
|
|
cRoot::Get()->GetCraftingRecipes()->GetRecipe(m_Owner, Grid, Recipe);
|
|
|
|
}
|
|
|
|
m_Slots[c_CraftOffset] = Recipe.GetResult();
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGD("%s cooked: %d:%d (%d times) !!", m_Owner->GetName().c_str(), m_Slots[c_CraftOffset].m_ItemID, m_Slots[c_CraftOffset].m_ItemHealth, m_Slots[c_CraftOffset].m_ItemCount );
|
|
|
|
SendWholeInventory(m_Owner->GetClientHandle());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-08-11 15:54:57 -04:00
|
|
|
SendSlot(0);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-08-11 15:54:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cSurvivalInventory::ShiftClicked(short a_SlotNum)
|
2012-08-11 15:54:57 -04:00
|
|
|
{
|
|
|
|
ASSERT((GetWindow() == NULL) || (GetWindow()->GetDraggingItem()->IsEmpty())); // Cannot handle shift-click if dragging something
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_SlotNum == SLOT_CRAFTING_RESULT)
|
2012-08-11 15:54:57 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ShiftClickedCraftingResult(a_SlotNum);
|
2012-08-11 15:54:57 -04:00
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
else if ((a_SlotNum >= SLOT_CRAFTING_MIN) && (a_SlotNum <= SLOT_CRAFTING_MAX))
|
2012-08-11 15:54:57 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ShiftClickedCraftingGrid(a_SlotNum);
|
2012-08-11 15:54:57 -04:00
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
else if ((a_SlotNum >= SLOT_ARMOR_MIN) && (a_SlotNum <= SLOT_ARMOR_MAX))
|
2012-08-11 15:54:57 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ShiftClickedArmor(a_SlotNum);
|
2012-08-11 15:54:57 -04:00
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
else if ((a_SlotNum >= SLOT_HOTBAR_MIN) && (a_SlotNum <= SLOT_HOTBAR_MAX))
|
2012-08-11 15:54:57 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ShiftClickedHotbar(a_SlotNum);
|
2012-08-11 15:54:57 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ShiftClickedInventory(a_SlotNum);
|
2012-08-11 15:54:57 -04:00
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
// Because the client tries to guess our actions and is not always right, send the whole inventory:
|
2012-08-11 15:54:57 -04:00
|
|
|
SendWholeInventoryToAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSurvivalInventory::ShiftClickedCraftingResult(short a_Slot)
|
|
|
|
{
|
2012-08-14 04:33:09 -04:00
|
|
|
// Craft until either the recipe changes (due to ingredients) or there's not enough storage for the result
|
|
|
|
cItem * CraftingResult = GetSlot(SLOT_CRAFTING_RESULT);
|
|
|
|
if ((CraftingResult == NULL) || CraftingResult->IsEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cItem ResultCopy = *CraftingResult;
|
|
|
|
int HowManyItemsWillFit = HowManyCanFit(CraftingResult->m_ItemID, CraftingResult->m_ItemHealth, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX);
|
|
|
|
HowManyItemsWillFit += 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 = 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)
|
|
|
|
{
|
|
|
|
MoveItem(CraftingResult->m_ItemID, CraftingResult->m_ItemHealth, CraftingResult->m_ItemCount - NumMoved, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "Use" the crafting recipe once:
|
|
|
|
cCraftingGrid Grid(m_Slots + SLOT_CRAFTING_MIN, 2, 2);
|
|
|
|
cCraftingRecipe Recipe(Grid);
|
|
|
|
cRoot::Get()->GetCraftingRecipes()->GetRecipe(m_Owner, Grid, Recipe);
|
|
|
|
Recipe.ConsumeIngredients(Grid);
|
|
|
|
Grid.CopyToItems(m_Slots + c_CraftOffset + 1);
|
|
|
|
cRoot::Get()->GetCraftingRecipes()->GetRecipe(m_Owner, Grid, Recipe);
|
|
|
|
m_Slots[SLOT_CRAFTING_RESULT] = Recipe.GetResult();
|
|
|
|
|
|
|
|
// If the recipe changed, abort:
|
2012-08-18 05:56:28 -04:00
|
|
|
if (!Recipe.GetResult().IsEqual(ResultCopy))
|
2012-08-14 04:33:09 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-11 15:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSurvivalInventory::ShiftClickedCraftingGrid(short a_Slot)
|
|
|
|
{
|
|
|
|
// Move the item from the crafting grid into the main inventory:
|
|
|
|
cItem * Item = GetSlot(a_Slot);
|
|
|
|
if ((Item == NULL) || Item->IsEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-14 05:45:38 -04:00
|
|
|
// First try the main inventory:
|
2012-08-11 15:54:57 -04:00
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX);
|
2012-08-14 05:45:38 -04:00
|
|
|
|
|
|
|
// If anything left, try the hotbar:
|
|
|
|
if (Item->m_ItemCount > 0)
|
|
|
|
{
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_HOTBAR_MIN, SLOT_HOTBAR_MAX);
|
|
|
|
}
|
2012-08-11 15:54:57 -04:00
|
|
|
SendSlot(a_Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSurvivalInventory::ShiftClickedArmor(short a_Slot)
|
|
|
|
{
|
|
|
|
// Move the item from the armor slot into the main inventory:
|
|
|
|
cItem * Item = GetSlot(a_Slot);
|
|
|
|
if ((Item == NULL) || Item->IsEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX);
|
|
|
|
SendSlot(a_Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSurvivalInventory::ShiftClickedHotbar(short a_Slot)
|
|
|
|
{
|
|
|
|
// Move the item from the hotbar into the main inventory:
|
|
|
|
cItem * Item = GetSlot(a_Slot);
|
|
|
|
if ((Item == NULL) || Item->IsEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX);
|
|
|
|
SendSlot(a_Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSurvivalInventory::ShiftClickedInventory(short a_Slot)
|
|
|
|
{
|
|
|
|
// Move the item from the main inventory into armor slot if it is armor, or the hotbar otherwise:
|
|
|
|
cItem * Item = GetSlot(a_Slot);
|
|
|
|
if ((Item == NULL) || Item->IsEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ItemCategory::IsHelmet(Item->m_ItemID))
|
|
|
|
{
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_ARMOR_HELMET, SLOT_ARMOR_HELMET);
|
|
|
|
}
|
|
|
|
else if (ItemCategory::IsChestPlate(Item->m_ItemID))
|
|
|
|
{
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_ARMOR_CHESTPLATE, SLOT_ARMOR_CHESTPLATE);
|
|
|
|
}
|
|
|
|
else if (ItemCategory::IsLeggings(Item->m_ItemID))
|
|
|
|
{
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_ARMOR_LEGGINGS, SLOT_ARMOR_LEGGINGS);
|
|
|
|
}
|
|
|
|
else if (ItemCategory::IsBoots(Item->m_ItemID))
|
|
|
|
{
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_ARMOR_BOOTS, SLOT_ARMOR_BOOTS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_HOTBAR_MIN, SLOT_HOTBAR_MAX);
|
|
|
|
}
|
|
|
|
SendSlot(a_Slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|