summaryrefslogtreecommitdiffstats
path: root/source/cSurvivalInventory.cpp
blob: 62808a17d9e51b4e69e96d6015c495fa0c40e89d (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

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





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





cSurvivalInventory::~cSurvivalInventory()
{
}





void cSurvivalInventory::Clicked( cPacket* a_ClickPacket )
{
	cPacket_WindowClick * Packet = reinterpret_cast<cPacket_WindowClick *>(a_ClickPacket);
	
	if (
		Packet->m_IsShiftPressed &&   // Shift pressed
		(GetWindow() != NULL) &&      // Window is valid
		(GetWindow()->GetDraggingItem()->IsEmpty())  // Not dragging anything
	)
	{
		ShiftClicked(Packet);
		return;
	}
	
	bool bDontCook = false;
	if (GetWindow())
	{
		// Override for craft result slot
		if (Packet->m_SlotNum == (short)c_CraftOffset)
		{
			LOGD("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;
			}
			LOGD("Dragging Dish %i", DraggingItem->m_ItemCount );
		}
		else
		{
			GetWindow()->Clicked( Packet, *m_Owner );
		}
	}
	else
	{
		ASSERT(!"No inventory window! WTF?");
		LOG("No Inventory window! WTF");
	}

	if ((Packet->m_SlotNum >= (short)c_CraftOffset) && (Packet->m_SlotNum < (short)(c_CraftOffset + c_CraftSlots + 1)))
	{
		cCraftingGrid   Grid(m_Slots + c_CraftOffset + 1, 2, 2);
		cCraftingRecipe Recipe(Grid);
		
		cRoot::Get()->GetCraftingRecipes()->GetRecipe(m_Owner, Grid, Recipe);
		
		if ((Packet->m_SlotNum == 0) && !bDontCook)
		{
			// 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();
		LOGD("%s cooked: %i x %i !!", m_Owner->GetName().c_str(), m_Slots[c_CraftOffset].m_ItemID, m_Slots[c_CraftOffset].m_ItemCount );
		SendWholeInventory( m_Owner->GetClientHandle() );
	}
	SendSlot(0);
}





void cSurvivalInventory::ShiftClicked(cPacket_WindowClick * a_ClickPacket)
{
	ASSERT((GetWindow() == NULL) || (GetWindow()->GetDraggingItem()->IsEmpty()));  // Cannot handle shift-click if dragging something
	
	short Slot = a_ClickPacket->m_SlotNum;
	if (Slot == SLOT_CRAFTING_RESULT)
	{
		ShiftClickedCraftingResult(Slot);
	}
	else if ((Slot >= SLOT_CRAFTING_MIN) && (Slot <= SLOT_CRAFTING_MAX))
	{
		ShiftClickedCraftingGrid(Slot);
	}
	else if ((Slot >= SLOT_ARMOR_MIN) && (Slot <= SLOT_ARMOR_MAX))
	{
		ShiftClickedArmor(Slot);
	}
	else if ((Slot >= SLOT_HOTBAR_MIN) && (Slot <= SLOT_HOTBAR_MAX))
	{
		ShiftClickedHotbar(Slot);
	}
	else
	{
		ShiftClickedInventory(Slot);
	}
	// Because the client tries to guess our actions, not always right, send the whole inventory:
	SendWholeInventoryToAll();
}





void cSurvivalInventory::ShiftClickedCraftingResult(short a_Slot)
{
	// TODO
}





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;
	}
	Item->m_ItemCount -= MoveItem(Item->m_ItemID, Item->m_ItemHealth, Item->m_ItemCount, SLOT_INVENTORY_MIN, SLOT_INVENTORY_MAX);
	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);
}