summaryrefslogtreecommitdiffstats
path: root/src/UI/SlotArea.h
blob: b150c47a1b35845d5dc0f3409e809117485fd0ff (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

// SlotArea.h

// Interfaces to the cSlotArea class representing a contiguous area of slots in a UI window




#pragma once

#include "../Inventory.h"
#include "Window.h"



class cWindow;
class cPlayer;
class cBeaconEntity;
class cChestEntity;
class cEnderChestEntity;
class cFurnaceEntity;
class cMinecartWithChest;
class cCraftingRecipe;
class cWorld;





class cSlotArea
{
public:
	cSlotArea(int a_NumSlots, cWindow & a_ParentWindow);
	virtual ~cSlotArea() {}  // force a virtual destructor in all subclasses
	
	int GetNumSlots(void) const { return m_NumSlots; }
	
	/** Called to retrieve an item in the specified slot for the specified player. Must return a valid cItem. */
	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const = 0;
	
	/** Called to set an item in the specified slot for the specified player */
	virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) = 0;
	
	/** Called when a player clicks in the window. Parameters taken from the click packet. */
	virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem);
	
	/** Called from Clicked when the action is a shiftclick (left or right) */
	virtual void ShiftClicked(cPlayer & a_Player, int a_SlotNum, const cItem & a_ClickedItem);

	/** Called from Clicked when the action is a caDblClick */
	virtual void DblClicked(cPlayer & a_Player, int a_SlotNum);

	/** Called from Clicked when the action is a middleclick */
	virtual void MiddleClicked(cPlayer & a_Player, int a_SlotNum);

	/** Called from Clicked when the action is a drop click. */
	virtual void DropClicked(cPlayer & a_Player, int a_SlotNum, bool a_DropStack);

	/** Called from Clicked when the action is a number click. */
	virtual void NumberClicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction);

	/** Called when a new player opens the same parent window. The window already tracks the player. CS-locked. */
	virtual void OnPlayerAdded(cPlayer & a_Player);
	
	/** Called when one of the players closes the parent window. The window already doesn't track the player. CS-locked. */
	virtual void OnPlayerRemoved(cPlayer & a_Player);
	
	/** Called to store as much of a_ItemStack in the area as possible. a_ItemStack is modified to reflect the change.
	The default implementation searches each slot for available space and distributes the stack there.
	if a_ShouldApply is true, the changes are written into the slots;
	if a_ShouldApply is false, only a_ItemStack is modified to reflect the number of fits (for fit-testing purposes)
	If a_KeepEmptySlots is true, empty slots will be skipped and won't be filled */
	virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill);
	
	/** Called on DblClicking to collect all stackable items into hand.
	The items are accumulated in a_Dragging and removed from the slots immediately.
	If a_CollectFullStacks is false, slots with full stacks are skipped while collecting.
	Returns true if full stack has been collected in a_Dragging, false if there's space remaining to fill. */
	virtual bool CollectItemsToHand(cItem & a_Dragging, cPlayer & a_Player, bool a_CollectFullStacks);

protected:
	int       m_NumSlots;
	cWindow & m_ParentWindow;
} ;





/** Handles any part of the inventory, using parameters in constructor to distinguish between the parts */
class cSlotAreaInventoryBase :
	public cSlotArea
{
	typedef cSlotArea super;
	
public:
	cSlotAreaInventoryBase(int a_NumSlots, int a_SlotOffset, cWindow & a_ParentWindow);
	
	// Creative inventory's click handling is somewhat different from survival inventory's, handle that here:
	virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;

	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
	
protected:
	int m_SlotOffset;  // Index that this area's slot 0 has in the underlying cInventory
} ;





/** Handles the main inventory of each player, excluding the armor and hotbar */
class cSlotAreaInventory :
	public cSlotAreaInventoryBase
{
	typedef cSlotAreaInventoryBase super;
	
public:
	cSlotAreaInventory(cWindow & a_ParentWindow) :
		cSlotAreaInventoryBase(cInventory::invInventoryCount, cInventory::invInventoryOffset, a_ParentWindow)
	{
	}
} ;





/** Handles the hotbar of each player */
class cSlotAreaHotBar :
	public cSlotAreaInventoryBase
{
	typedef cSlotAreaInventoryBase super;
	
public:
	cSlotAreaHotBar(cWindow & a_ParentWindow) :
		cSlotAreaInventoryBase(cInventory::invHotbarCount, cInventory::invHotbarOffset, a_ParentWindow)
	{
	}
} ;





/** Handles the armor area of the player's inventory */
class cSlotAreaArmor :
	public cSlotAreaInventoryBase
{
public:
	cSlotAreaArmor(cWindow & a_ParentWindow) :
		cSlotAreaInventoryBase(cInventory::invArmorCount, cInventory::invArmorOffset, a_ParentWindow)
	{
	}

	/** Distributing the stack is allowed only for compatible items (helmets into helmet slot etc.) */
	virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override;

	/** Called when a player clicks in the window. Parameters taken from the click packet. */
	virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;

	static bool CanPlaceArmorInSlot(int a_SlotNum, const cItem & a_Item);
} ;





/** Handles any slot area that is representing a cItemGrid; same items for all the players */
class cSlotAreaItemGrid :
	public cSlotArea,
	public cItemGrid::cListener
{
	typedef cSlotArea super;
	
public:
	cSlotAreaItemGrid(cItemGrid & a_ItemGrid, cWindow & a_ParentWindow);
	
	virtual ~cSlotAreaItemGrid();
	
	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;

protected:
	cItemGrid & m_ItemGrid;
	
	// cItemGrid::cListener overrides:
	virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
} ;





/** A cSlotArea with items layout that is private to each player and is temporary, such as
a crafting grid or an enchantment table.
This common ancestor stores the items in a per-player map. It also implements tossing items from the map. */
class cSlotAreaTemporary :
	public cSlotArea
{
	typedef cSlotArea super;
	
public:
	cSlotAreaTemporary(int a_NumSlots, cWindow & a_ParentWindow);
	
	// cSlotArea overrides:
	virtual const cItem * GetSlot        (int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot        (int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
	virtual void          OnPlayerAdded  (cPlayer & a_Player) override;
	virtual void          OnPlayerRemoved(cPlayer & a_Player) override;
	
	/** Tosses the player's items in slots [a_Begin, a_End) (ie. incl. a_Begin, but excl. a_End) */
	void TossItems(cPlayer & a_Player, int a_Begin, int a_End);
	
protected:
	typedef std::map<UInt32, std::vector<cItem> > cItemMap;  // Maps EntityID -> items
	
	cItemMap m_Items;
	
	/** Returns the pointer to the slot array for the player specified. */
	cItem * GetPlayerSlots(cPlayer & a_Player);
} ;





class cSlotAreaCrafting :
	public cSlotAreaTemporary
{
	typedef cSlotAreaTemporary super;
	
public:
	/** a_GridSize is allowed to be only 2 or 3 */
	cSlotAreaCrafting(int a_GridSize, cWindow & a_ParentWindow);

	// cSlotAreaTemporary overrides:
	virtual void Clicked        (cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
	virtual void DblClicked     (cPlayer & a_Player, int a_SlotNum) override;
	virtual void OnPlayerRemoved(cPlayer & a_Player) override;
	virtual void SetSlot        (int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
	
	// Distributing items into this area is completely disabled
	virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override;


protected:
	/** Maps player's EntityID -> current recipe.
	Not a std::map because cCraftingGrid needs proper constructor params. */
	typedef std::list<std::pair<UInt32, cCraftingRecipe> > cRecipeMap;
	
	int        m_GridSize;
	cRecipeMap m_Recipes;
	
	/** Handles a click in the result slot.
	Crafts using the current recipe, if possible. */
	void ClickedResult(cPlayer & a_Player);
	
	/** Handles a shift-click in the result slot.
	Crafts using the current recipe until it changes or no more space for result. */
	void ShiftClickedResult(cPlayer & a_Player);

	/** Handles a drop-click in the result slot. */
	void DropClickedResult(cPlayer & a_Player);

	/** Updates the current recipe and result slot based on the ingredients currently in the crafting grid of the specified player. */
	void UpdateRecipe(cPlayer & a_Player);
	
	/** Retrieves the recipe for the specified player from the map, or creates one if not found. */
	cCraftingRecipe & GetRecipeForPlayer(cPlayer & a_Player);

	/** Called after an item has been crafted to handle statistics e.t.c. */
	void HandleCraftItem(const cItem & a_Result, cPlayer & a_Player);
} ;





class cSlotAreaAnvil :
	public cSlotAreaTemporary
{
	typedef cSlotAreaTemporary super;

public:
	cSlotAreaAnvil(cWindow & a_ParentWindow);

	// cSlotArea overrides:
	virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
	virtual void ShiftClicked(cPlayer & a_Player, int a_SlotNum, const cItem & a_ClickedItem) override;
	virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override;

	// cSlotAreaTemporary overrides:
	virtual void OnPlayerRemoved(cPlayer & a_Player) override;

	/** Can the player take the item from the slot? */
	bool CanTakeResultItem(cPlayer & a_Player);

	/** This function will call, when the player take the item from the slot. */
	void OnTakeResult(cPlayer & a_Player);

	/** Handles a click in the item slot. */
	void UpdateResult(cPlayer & a_Player);

protected:
	/** The maximum cost of repairing / renaming in the anvil. */
	int m_MaximumCost;

	/** The stack size of the second item where was used for repair */
	char m_StackSizeToBeUsedInRepair;
} ;





class cSlotAreaBeacon :
	public cSlotArea,
	public cItemGrid::cListener
{
	typedef cSlotArea super;
	
public:
	cSlotAreaBeacon(cBeaconEntity * a_Beacon, cWindow & a_ParentWindow);
	virtual ~cSlotAreaBeacon();

	static bool IsPlaceableItem(short a_ItemType);
	
	virtual void          Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
	virtual void          DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override;
	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;

protected:
	cBeaconEntity * m_Beacon;

	// cItemGrid::cListener overrides:
	virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
} ;





class cSlotAreaEnchanting :
	public cSlotAreaTemporary
{
	typedef cSlotAreaTemporary super;

public:
	cSlotAreaEnchanting(cWindow & a_ParentWindow, int a_BlockX, int a_BlockY, int a_BlockZ);

	// cSlotArea overrides:
	virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
	virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override;
	virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;

	// cSlotAreaTemporary overrides:
	virtual void OnPlayerAdded  (cPlayer & a_Player) override;
	virtual void OnPlayerRemoved(cPlayer & a_Player) override;

	/* Get the count of bookshelves who stand in the near of the enchanting table */
	int GetBookshelvesCount(cWorld * a_World);

protected:
	/** Handles a click in the item slot. */
	void UpdateResult(cPlayer & a_Player);

	int m_BlockX, m_BlockY, m_BlockZ;
};





class cSlotAreaChest :
	public cSlotArea
{
public:
	cSlotAreaChest(cChestEntity * a_Chest, cWindow & a_ParentWindow);
	
	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
	
protected:
	cChestEntity * m_Chest;
} ;





class cSlotAreaDoubleChest :
	public cSlotArea
{
public:
	cSlotAreaDoubleChest(cChestEntity * a_TopChest, cChestEntity * a_BottomChest, cWindow & a_ParentWindow);
	
	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
	
protected:
	cChestEntity * m_TopChest;
	cChestEntity * m_BottomChest;
} ;





class cSlotAreaEnderChest :
	public cSlotArea
{
public:
	cSlotAreaEnderChest(cEnderChestEntity * a_EnderChest, cWindow & a_ParentWindow);

	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;

protected:
	cEnderChestEntity * m_EnderChest;
};





class cSlotAreaFurnace :
	public cSlotArea,
	public cItemGrid::cListener
{
	typedef cSlotArea super;
	
public:
	cSlotAreaFurnace(cFurnaceEntity * a_Furnace, cWindow & a_ParentWindow);
	
	virtual ~cSlotAreaFurnace();
	
	virtual void          Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
	virtual void          DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override;
	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
	
protected:
	cFurnaceEntity * m_Furnace;

	// cItemGrid::cListener overrides:
	virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;

	/** Called after an item has been smelted to handle statistics etc. */
	void HandleSmeltItem(const cItem & a_Result, cPlayer & a_Player);
} ;





class cSlotAreaMinecartWithChest :
	public cSlotArea
{
public:
	cSlotAreaMinecartWithChest(cMinecartWithChest * a_ChestCart, cWindow & a_ParentWindow);

	virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override;
	virtual void          SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;

protected:
	cMinecartWithChest * m_Chest;
};