Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemGrid.h
Go to the documentation of this file.
1 
2 // ItemGrid.h
3 
4 // Declares the cItemGrid class representing a storage for items in a XY grid (chests, dispensers, inventory etc.)
5 
6 
7 
8 
9 #pragma once
10 
11 #include "Item.h"
12 #include "LazyArray.h"
13 
14 
15 
16 
17 
18 // tolua_begin
19 class cItemGrid
20 {
21 public:
22  // tolua_end
23 
25  class cListener
26  {
27  public:
28  virtual ~cListener() {}
29 
31  virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) = 0;
32  } ;
33  typedef std::vector<cListener *> cListeners;
34 
35  cItemGrid(int a_Width, int a_Height);
36 
37  // tolua_begin
38  int GetWidth (void) const { return m_Width; }
39  int GetHeight (void) const { return m_Height; }
40  int GetNumSlots(void) const { return m_Slots.size(); }
41 
43  int GetSlotNum(int a_X, int a_Y) const;
44 
45  // tolua_end
46 
48  void GetSlotCoords(int a_SlotNum, int & a_X, int & a_Y) const;
49 
52  void CopyFrom(const cItemGrid & a_Src);
53 
54  // tolua_begin
55 
56  // Retrieve slots by coords or slot number; Logs warning and returns the first slot on invalid coords / slotnum
57  const cItem & GetSlot(int a_X, int a_Y) const;
58  const cItem & GetSlot(int a_SlotNum) const;
59 
60  // Set slot by coords or slot number; Logs warning and doesn't set on invalid coords / slotnum
61  void SetSlot(int a_X, int a_Y, const cItem & a_Item);
62  void SetSlot(int a_X, int a_Y, short a_ItemType, char a_ItemCount, short a_ItemDamage);
63  void SetSlot(int a_SlotNum, const cItem & a_Item);
64  void SetSlot(int a_SlotNum, short a_ItemType, char a_ItemCount, short a_ItemDamage);
65 
66  // Empty the specified slot; Logs warning and doesn't set on invalid coords / slotnum
67  void EmptySlot(int a_X, int a_Y);
68  void EmptySlot(int a_SlotNum);
69 
71  bool IsSlotEmpty(int a_SlotNum) const;
72 
74  bool IsSlotEmpty(int a_X, int a_Y) const;
75 
77  void Clear(void);
78 
80  int HowManyCanFit(const cItem & a_ItemStack, bool a_AllowNewStacks = true);
81 
89  int AddItem(cItem & a_ItemStack, bool a_AllowNewStacks = true, int a_PrioritySlot = -1);
90 
99  int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritySlot = -1);
100 
103  int RemoveItem(const cItem & a_ItemStack);
104 
109  int ChangeSlotCount(int a_SlotNum, int a_AddToCount);
110 
115  int ChangeSlotCount(int a_X, int a_Y, int a_AddToCount);
116 
120  cItem RemoveOneItem(int a_SlotNum);
121 
125  cItem RemoveOneItem(int a_X, int a_Y);
126 
128  int HowManyItems(const cItem & a_Item);
129 
131  bool HasItems(const cItem & a_ItemStack);
132 
134  int GetFirstEmptySlot(void) const;
135 
137  int GetFirstUsedSlot(void) const;
138 
140  int GetLastEmptySlot(void) const;
141 
143  int GetLastUsedSlot(void) const;
144 
146  int GetNextEmptySlot(int a_StartFrom) const;
147 
149  int GetNextUsedSlot(int a_StartFrom) const;
150 
152  void CopyToItems(cItems & a_Items) const;
153 
155  bool DamageItem(int a_SlotNum, short a_Amount);
156 
158  bool DamageItem(int a_X, int a_Y, short a_Amount);
159 
160  // tolua_end
161 
162 
164  bool IsValidSlotCoords(int a_X, int a_Y) const;
165 
167  bool IsValidSlotNum(int a_SlotNum) const;
168 
173  void GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, size_t a_CountLootProbabs, int a_NumSlots, int a_Seed);
174 
176  void AddListener(cListener & a_Listener);
177 
179  void RemoveListener(cListener & a_Listener);
180 
181  // tolua_begin
182 
183 protected:
184  int m_Width;
185  int m_Height;
187 
188  cListeners m_Listeners;
191 
193  void TriggerListeners(int a_SlotNum);
194 
198  int AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack);
199 } ;
200 // tolua_end
201 
202 
203 
int GetSlotNum(int a_X, int a_Y) const
Converts XY coords into slot number; returns -1 on invalid coords.
Definition: ItemGrid.cpp:48
std::vector< cListener * > cListeners
Definition: ItemGrid.h:33
void TriggerListeners(int a_SlotNum)
Calls all m_Listeners for the specified slot number.
Definition: ItemGrid.cpp:814
void GenerateRandomLootWithBooks(const cLootProbab *a_LootProbabs, size_t a_CountLootProbabs, int a_NumSlots, int a_Seed)
Generates random loot from the specified loot probability table, with a chance of enchanted books add...
Definition: ItemGrid.cpp:728
int GetFirstUsedSlot(void) const
Returns the index of the first non-empty slot; -1 if all empty.
Definition: ItemGrid.cpp:580
void EmptySlot(int a_X, int a_Y)
Definition: ItemGrid.cpp:169
int GetWidth(void) const
Definition: ItemGrid.h:38
void CopyFrom(const cItemGrid &a_Src)
Copies all items from a_Src to this grid.
Definition: ItemGrid.cpp:83
int HowManyItems(const cItem &a_Item)
Returns the number of items of type a_Item that are stored.
Definition: ItemGrid.cpp:539
int RemoveItem(const cItem &a_ItemStack)
Removes the specified item from the grid, as many as possible, up to a_ItemStack.m_ItemCount.
Definition: ItemGrid.cpp:405
cItemGrid(int a_Width, int a_Height)
Definition: ItemGrid.cpp:15
int ChangeSlotCount(int a_SlotNum, int a_AddToCount)
Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot...
Definition: ItemGrid.cpp:443
int GetFirstEmptySlot(void) const
Returns the index of the first empty slot; -1 if all full.
Definition: ItemGrid.cpp:571
void CopyToItems(cItems &a_Items) const
Copies the contents into a cItems object; preserves the original a_Items contents.
Definition: ItemGrid.cpp:679
cItem RemoveOneItem(int a_SlotNum)
Removes one item from the stack in the specified slot, and returns it.
Definition: ItemGrid.cpp:492
void Clear(void)
Sets all items as empty.
Definition: ItemGrid.cpp:228
This class is used as a callback for when a slot changes.
Definition: ItemGrid.h:25
cListeners m_Listeners
Listeners which should be notified on slot changes; the pointers are not owned by this object...
Definition: ItemGrid.h:188
void AddListener(cListener &a_Listener)
Adds a callback that gets called whenever a slot changes.
Definition: ItemGrid.cpp:785
bool IsSlotEmpty(int a_SlotNum) const
Returns true if the specified slot is empty or the slot doesn&#39;t exist.
Definition: ItemGrid.cpp:203
bool HasItems(const cItem &a_ItemStack)
Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack.
Definition: ItemGrid.cpp:561
void RemoveListener(cListener &a_Listener)
Removes a slot-change-callback.
Definition: ItemGrid.cpp:796
bool m_IsInTriggerListeners
Set to true while TriggerListeners is running, to detect attempts to manipulate listener list while t...
Definition: ItemGrid.h:190
virtual ~cListener()
Definition: ItemGrid.h:28
void GetSlotCoords(int a_SlotNum, int &a_X, int &a_Y) const
Converts slot number into XY coords; sets coords to -1 on invalid slot number.
Definition: ItemGrid.cpp:64
int m_Height
Definition: ItemGrid.h:185
int AddItem(cItem &a_ItemStack, bool a_AllowNewStacks=true, int a_PrioritySlot=-1)
Adds as many items out of a_ItemStack as can fit.
Definition: ItemGrid.cpp:313
int GetLastEmptySlot(void) const
Returns the index of the last empty slot; -1 if all full.
Definition: ItemGrid.cpp:589
size_type size() const NOEXCEPT
Definition: LazyArray.h:80
int GetLastUsedSlot(void) const
Returns the index of the last used slot; -1 if all empty.
Definition: ItemGrid.cpp:605
int GetNextUsedSlot(int a_StartFrom) const
Returns the index of the first used slot following a_StartFrom (a_StartFrom is not checked) ...
Definition: ItemGrid.cpp:650
int HowManyCanFit(const cItem &a_ItemStack, bool a_AllowNewStacks=true)
Returns number of items out of a_ItemStack that can fit in the storage.
Definition: ItemGrid.cpp:246
cLazyArray< cItem > m_Slots
Definition: ItemGrid.h:186
cCriticalSection m_CSListeners
CS that guards the m_Listeners against multi-thread access.
Definition: ItemGrid.h:189
bool IsValidSlotNum(int a_SlotNum) const
Returns true if slot number is within the grid.
Definition: ItemGrid.cpp:27
int GetHeight(void) const
Definition: ItemGrid.h:39
Used to store loot probability tables.
Definition: Item.h:278
int m_Width
Definition: ItemGrid.h:184
bool IsValidSlotCoords(int a_X, int a_Y) const
Returns true if slot coordinates lie within the grid.
Definition: ItemGrid.cpp:36
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
int GetNumSlots(void) const
Definition: ItemGrid.h:40
virtual void OnSlotChanged(cItemGrid *a_ItemGrid, int a_SlotNum)=0
Called whenever a slot changes.
int AddItemToSlot(const cItem &a_ItemStack, int a_Slot, int a_Num, int a_MaxStack)
Adds up to a_Num items out of a_ItemStack, as many as can fit, in specified slot Returns the number o...
Definition: ItemGrid.cpp:283
Definition: Item.h:36
int AddItems(cItems &a_ItemStackList, bool a_AllowNewStacks=true, int a_PrioritySlot=-1)
Same as AddItem, but works on an entire list of item stacks.
Definition: ItemGrid.cpp:381
int GetNextEmptySlot(int a_StartFrom) const
Returns the index of the first empty slot following a_StartFrom (a_StartFrom is not checked) ...
Definition: ItemGrid.cpp:626
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
bool DamageItem(int a_SlotNum, short a_Amount)
Adds the specified damage to the specified item; returns true if the item broke (but the item is left...
Definition: ItemGrid.cpp:699
void SetSlot(int a_X, int a_Y, const cItem &a_Item)
Definition: ItemGrid.cpp:121