summaryrefslogtreecommitdiffstats
path: root/source/cSurvivalInventory.cpp
blob: a39cc6b5946743ba3421ab30fd9cede6045bc744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#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 "cRecipeChecker.h"
#include "CraftingRecipes.h"
#include "cRoot.h"
#include "packets/cPacket_WindowClick.h"





cSurvivalInventory::~cSurvivalInventory()
{
	
}

cSurvivalInventory::cSurvivalInventory(cPlayer* a_Owner)
	: cInventory(a_Owner)
{
	
}

void cSurvivalInventory::Clicked( cPacket* a_ClickPacket )
{
	cPacket_WindowClick *Packet = reinterpret_cast<cPacket_WindowClick *>(a_ClickPacket);
	bool bDontCook = false;
	if( GetWindow() )
	{
		// Override for craft result slot
		if( Packet->m_SlotNum == (short)c_CraftOffset )
		{
			LOG("In craft slot: %i x %i !!", m_Slots[c_CraftOffset].m_ItemID, m_Slots[c_CraftOffset].m_ItemCount );
			cItem* DraggingItem = GetWindow()->GetDraggingItem();
			if( DraggingItem->IsEmpty() )
			{
				*DraggingItem = m_Slots[c_CraftOffset];
				m_Slots[c_CraftOffset].Empty();
			}
			else if( DraggingItem->Equals( m_Slots[c_CraftOffset] ) )
			{
				if( DraggingItem->m_ItemCount + m_Slots[c_CraftOffset].m_ItemCount <= 64 )
				{
					DraggingItem->m_ItemCount += m_Slots[c_CraftOffset].m_ItemCount;
					m_Slots[0].Empty();
				}
				else
				{
					bDontCook = true;
				}
			}
			else
			{
				bDontCook = true;
			}
			LOG("Dragging Dish %i", DraggingItem->m_ItemCount );
		}
		else
		{
			GetWindow()->Clicked( Packet, *m_Owner );
		}
	}
	else
	{
		LOG("No Inventory window! WTF");
	}

	if ((Packet->m_SlotNum >= (short)c_CraftOffset) && (Packet->m_SlotNum < (short)(c_CraftOffset + c_CraftSlots + 1)))
	{
		cItem CookedItem;
		if ((Packet->m_SlotNum == 0) && !bDontCook)
		{
			// Consume the items from the crafting grid:
			CookedItem = cRoot::Get()->GetCraftingRecipes()->Craft(m_Slots + c_CraftOffset + 1, 2, 2);
			// Upgrade the crafting result from the new crafting grid contents:
			CookedItem = cRoot::Get()->GetCraftingRecipes()->Offer(m_Slots + c_CraftOffset + 1, 2, 2);
			if (CookedItem.IsEmpty())
			{
				// Fallback to the old recipes:
				CookedItem = cRoot::Get()->GetRecipeChecker()->CookIngredients( m_Slots+c_CraftOffset+1, 2, 2, true );
			}
		}
		else
		{
			CookedItem = cRoot::Get()->GetCraftingRecipes()->Offer(m_Slots + c_CraftOffset + 1, 2, 2);
			if (CookedItem.IsEmpty())
			{
				// Fallback to the old recipes:
				CookedItem = cRoot::Get()->GetRecipeChecker()->CookIngredients( m_Slots+c_CraftOffset+1, 2, 2 );
			}
		}
		m_Slots[c_CraftOffset] = CookedItem;
		LOG("You cooked: %i x %i !!", m_Slots[c_CraftOffset].m_ItemID, m_Slots[c_CraftOffset].m_ItemCount );
		SendWholeInventory( m_Owner->GetClientHandle() );
	}
	SendSlot( 0 );
}