OregonCore  revision fb2a440-git
Your Favourite TBC server
LootMgr.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef OREGON_LOOTMGR_H
19 #define OREGON_LOOTMGR_H
20 
21 #include "ItemEnchantmentMgr.h"
22 #include "ByteBuffer.h"
24 #include "ConditionMgr.h"
25 
26 #include <map>
27 #include <vector>
28 
29 #define MAX_NR_LOOT_ITEMS 16
30 // note: the client cannot show more than 16 items total
31 #define MAX_NR_QUEST_ITEMS 32
32 // unrelated to the number of quest items shown, just for reserve
33 
35 {
41 };
42 
44 {
52 };
53 
55 {
56  LOOT_NONE = 0,
57 
61 
64 
68 };
69 
71 {
72  LOOT_ERROR_DIDNT_KILL = 0, // You don't have permission to loot that corpse.
73  LOOT_ERROR_TOO_FAR = 4, // You are too far away to loot that corpse.
74  LOOT_ERROR_BAD_FACING = 5, // You must be facing the corpse to loot it.
75  LOOT_ERROR_LOCKED = 6, // Someone is already looting that corpse.
76  LOOT_ERROR_NOTSTANDING = 8, // You need to be standing up to loot something!
77  LOOT_ERROR_STUNNED = 9, // You can't loot anything while stunned!
78  LOOT_ERROR_PLAYER_NOT_FOUND = 10, // Player not found
79  LOOT_ERROR_PLAY_TIME_EXCEEDED = 11, // Maximum play time exceeded
80  LOOT_ERROR_MASTER_INV_FULL = 12, // That player's inventory is full
81  LOOT_ERROR_MASTER_UNIQUE_ITEM = 13, // Player has too many of that item already
82  LOOT_ERROR_MASTER_OTHER = 14, // Can't assign item to that player
83  LOOT_ERROR_ALREADY_PICKPOCKETED = 15, // Your target has already had its pockets picked
84  LOOT_ERROR_NOT_WHILE_SHAPESHIFTED = 16 // You can't do that while shapeshifted.
85 };
86 
87 // type of Loot Item in Loot View
89 {
90  LOOT_SLOT_TYPE_ALLOW_LOOT = 0, // player can loot the item.
91  LOOT_SLOT_TYPE_ROLL_ONGOING = 1, // roll is ongoing. player cannot loot.
92  LOOT_SLOT_TYPE_MASTER = 2, // item can only be distributed by group loot master.
93  LOOT_SLOT_TYPE_LOCKED = 3, // item is shown in red. player cannot loot.
94  LOOT_SLOT_TYPE_OWNER = 4 // ignore binding confirmation and etc, for single player looting
95 };
96 
97 class Player;
98 class LootStore;
99 class ConditionMgr;
100 
102 {
103  uint32 itemid; // id of the item
104 
105  uint32 reference; // referenced TemplateleId
106  float chance; // chance to drop for both quest and non-quest items, chance to be used for refs
107  bool needs_quest : 1; // quest drop (quest is required for item to drop)
109  uint8 mincount; // mincount for drop items
110  uint8 maxcount : 8; // max drop count for the item mincount or Ref multiplicator
111  ConditionList conditions; // additional loot condition
112 
113  // Constructor
114  // displayid is filled in IsValid() which must be called after
115  LootStoreItem(uint32 _itemid, uint32 _reference, float _chance, bool _needs_quest, uint8 _groupid, int32 _mincount, uint8 _maxcount)
116  : itemid(_itemid), reference(_reference), chance(_chance), needs_quest(_needs_quest),
117  groupid(_groupid), mincount(_mincount), maxcount(_maxcount) {}
118 
119  bool Roll() const; // Checks if the entry takes it's chance (at loot generation)
120  bool IsValid(LootStore const& store, uint32 entry) const;
121  // Checks correctness of values
122 };
123 
124 struct LootItem
125 {
129  ConditionList conditions; // additional loot condition
130  uint8 count : 8;
131  bool is_looted : 1;
132  bool is_blocked : 1;
133  bool freeforall : 1; // free for all
134  bool is_underthreshold : 1;
135  bool is_counted : 1;
136  bool needs_quest : 1; // quest drop
137 
138  // Constructor, copies most fields from LootStoreItem, generates random count and random suffixes/properties
139  // Should be called for non-reference LootStoreItem entries only (reference = 0)
140  explicit LootItem(LootStoreItem const& li);
141 
142  // Basic checks for player/item compatibility - if false no chance to see the item in the loot
143  bool AllowedForPlayer(Player const* player) const;
144 };
145 
146 struct QuestItem
147 {
148  uint8 index; // position in quest_items;
149  bool is_looted;
150 
152  : index(0), is_looted(false) {}
153 
154  QuestItem(uint8 _index, bool _islooted = false)
155  : index(_index), is_looted(_islooted) {}
156 };
157 
158 struct Loot;
160 
161 typedef std::vector<QuestItem> QuestItemList;
162 typedef std::map<uint32, QuestItemList*> QuestItemMap;
163 typedef std::vector<LootStoreItem> LootStoreItemList;
164 typedef UNORDERED_MAP<uint32, LootTemplate*> LootTemplateMap;
165 
166 typedef std::set<uint32> LootIdSet;
167 
169 {
170  public:
171  explicit LootStore(char const* name, char const* entryName) : m_name(name), m_entryName(entryName) {}
172  virtual ~LootStore()
173  {
174  Clear();
175  }
176 
177  void Verify() const;
178 
179  void LoadAndCollectLootIds(LootIdSet& ids_set);
180  void CheckLootRefs(LootIdSet* ref_set = NULL) const;// check existence reference and remove it from ref_set
181  void ReportUnusedIds(LootIdSet const& ids_set) const;
182  void ReportNonExistingId(uint32 id) const;
183 
184  bool HaveLootFor(uint32 loot_id) const
185  {
186  return m_LootTemplates.find(loot_id) != m_LootTemplates.end();
187  }
188  bool HaveQuestLootFor(uint32 loot_id) const;
189  bool HaveQuestLootForPlayer(uint32 loot_id, Player* player) const;
190 
191  LootTemplate const* GetLootFor(uint32 loot_id) const;
192  void ResetConditions();
193  LootTemplate* GetLootForConditionFill(uint32 loot_id);
194 
195  char const* GetName() const
196  {
197  return m_name;
198  }
199  char const* GetEntryName() const
200  {
201  return m_entryName;
202  }
203  protected:
204  void LoadLootTable();
205  void Clear();
206  private:
208  char const* m_name;
209  char const* m_entryName;
210 };
211 
213 {
214  class LootGroup; // A set of loot definitions for items (refs are not allowed inside)
215  typedef std::vector<LootGroup> LootGroups;
216 
217  public:
218  // Adds an entry to the group (at loading stage)
219  void AddEntry(LootStoreItem& item);
220  // Rolls for every item in the template and adds the rolled items the the loot
221  void Process(Loot& loot, LootStore const& store, uint8 GroupId = 0) const;
222  void CopyConditions(ConditionList conditions);
223 
224  // True if template includes at least 1 quest drop entry
225  bool HasQuestDrop(LootTemplateMap const& store, uint8 GroupId = 0) const;
226  // True if template includes at least 1 quest drop for an active quest of the player
227  bool HasQuestDropForPlayer(LootTemplateMap const& store, Player const* player, uint8 GroupId = 0) const;
228 
229  // Checks integrity of the template
230  void Verify(LootStore const& store, uint32 Id) const;
231  void CheckLootRefs(LootTemplateMap const& store, LootIdSet* ref_set) const;
232  bool addConditionItem(Condition* cond);
233  bool isReference(uint32 id);
234 
235  private:
236  LootStoreItemList Entries; // not grouped only
237  LootGroups Groups; // groups have own (optimised) processing, grouped entries go there
238 };
239 
240 //=====================================================
241 
242 class LootValidatorRef : public Reference<Loot, LootValidatorRef>
243 {
244  public:
246  void targetObjectDestroyLink() override {}
247  void sourceObjectDestroyLink() override {}
248 };
249 
250 //=====================================================
251 
252 class LootValidatorRefManager : public RefManager<Loot, LootValidatorRef>
253 {
254  public:
256 
258  {
260  }
262  {
264  }
265 
266  iterator begin()
267  {
268  return iterator(getFirst());
269  }
270  iterator end()
271  {
272  return iterator(NULL);
273  }
274  iterator rbegin()
275  {
276  return iterator(getLast());
277  }
278  iterator rend()
279  {
280  return iterator(NULL);
281  }
282 };
283 
284 //=====================================================
285 struct LootView;
286 
287 ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li);
288 ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
289 
290 struct Loot
291 {
292  friend ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
293 
295  {
296  return PlayerQuestItems;
297  }
299  {
300  return PlayerFFAItems;
301  }
303  {
304  return PlayerNonQuestNonFFAConditionalItems;
305  }
306 
307  std::vector<LootItem> items;
308  std::vector<LootItem> quest_items;
311  uint64 roundRobinPlayer; // GUID of the player having the Round-Robin ownership for the loot. If 0, round robin owner has released
313 
314  Loot(uint32 _gold = 0) : gold(_gold), unlootedCount(0), roundRobinPlayer(0), loot_type(LOOT_CORPSE) {}
316  {
317  clear();
318  }
319 
320  // if loot becomes invalid this reference is used to inform the listener
321  void addLootValidatorRef(LootValidatorRef* pLootValidatorRef)
322  {
323  i_LootValidatorRefManager.insertFirst(pLootValidatorRef);
324  }
325 
326  // void clear();
327  void clear()
328  {
329  gold = 0;
330  PlayersLooting.clear();
331  for (QuestItemMap::iterator itr = PlayerQuestItems.begin(); itr != PlayerQuestItems.end(); ++itr)
332  delete itr->second;
333  for (QuestItemMap::iterator itr = PlayerFFAItems.begin(); itr != PlayerFFAItems.end(); ++itr)
334  delete itr->second;
335  for (QuestItemMap::iterator itr = PlayerNonQuestNonFFAConditionalItems.begin(); itr != PlayerNonQuestNonFFAConditionalItems.end(); ++itr)
336  delete itr->second;
337 
338  PlayerQuestItems.clear();
339  PlayerFFAItems.clear();
340  PlayerNonQuestNonFFAConditionalItems.clear();
341 
342  items.clear();
343  quest_items.clear();
344  gold = 0;
345  unlootedCount = 0;
346  i_LootValidatorRefManager.clearReferences();
347  roundRobinPlayer = 0;
348  loot_type = LOOT_NONE;
349  }
350 
351  bool empty() const
352  {
353  return items.empty() && gold == 0;
354  }
355  bool isLooted() const
356  {
357  return gold == 0 && unlootedCount == 0;
358  }
359 
360  void NotifyItemRemoved(uint8 lootIndex);
361  void NotifyQuestItemRemoved(uint8 questIndex);
362  void NotifyMoneyRemoved();
363  void AddLooter(uint64 GUID)
364  {
365  PlayersLooting.insert(GUID);
366  }
367  void RemoveLooter(uint64 GUID)
368  {
369  PlayersLooting.erase(GUID);
370  }
371 
372  void generateMoneyLoot(uint32 minAmount, uint32 maxAmount);
373  void FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner, bool personal);
374  void FillNotNormalLootFor(Player* player);
375 
376  // Inserts the item into the loot (called by LootTemplate processors)
377  void AddItem(LootStoreItem const& item);
378 
379  LootItem* LootItemInSlot(uint32 lootslot, Player* player, QuestItem** qitem = NULL, QuestItem** ffaitem = NULL, QuestItem** conditem = NULL);
380  uint32 GetMaxSlotInLootFor(Player* player) const;
381  bool hasItemForAll() const;
382  bool hasItemFor(Player* player) const;
383  bool hasOverThresholdItem() const;
384 
385  private:
386  QuestItemList* FillFFALoot(Player* player);
387  QuestItemList* FillQuestLoot(Player* player);
388  QuestItemList* FillNonQuestNonFFAConditionalLoot(Player* player);
389 
390  std::set<uint64> PlayersLooting;
394 
395  // All rolls are registered here. They need to know, when the loot is not valid anymore
397 
398 };
399 
400 struct LootView
401 {
405  LootView(Loot& _loot, Player* _viewer, PermissionTypes _permission = ALL_PERMISSION)
406  : loot(_loot), viewer(_viewer), permission(_permission) {}
407 };
408 
419 
430 
431 inline void LoadLootTables()
432 {
444 }
445 
446 #endif
447 
PermissionTypes
Definition: LootMgr.h:43
ConditionList conditions
Definition: LootMgr.h:129
char const * m_entryName
Definition: LootMgr.h:209
uint8 maxcount
Definition: LootMgr.h:110
void LoadLootTables()
Definition: LootMgr.h:431
void addLootValidatorRef(LootValidatorRef *pLootValidatorRef)
Definition: LootMgr.h:321
std::vector< LootGroup > LootGroups
Definition: LootMgr.h:214
QuestItemMap PlayerFFAItems
Definition: LootMgr.h:392
bool is_looted
Definition: LootMgr.h:149
LootStore LootTemplates_Mail
unsupported by client, sending LOOT_FISHING instead
Definition: LootMgr.h:67
std::set< uint64 > PlayersLooting
Definition: LootMgr.h:390
unsupported by client, sending LOOT_SKINNING instead
Definition: LootMgr.h:65
std::list< Condition * > ConditionList
Definition: ConditionMgr.h:223
float chance
Definition: LootMgr.h:106
std::map< uint32, QuestItemList * > QuestItemMap
Definition: LootMgr.h:162
LootStoreItemList Entries
Definition: LootMgr.h:236
LootGroups Groups
Definition: LootMgr.h:237
LootStore LootTemplates_Item
void LoadLootTemplates_Skinning()
Definition: LootMgr.cpp:1452
uint32 randomSuffix
Definition: LootMgr.h:127
ACE_INT32 int32
Definition: Define.h:67
std::vector< LootStoreItem > LootStoreItemList
Definition: LootMgr.h:163
NULL Dbg ErrDB Arena Chat Char Map MMap false
Definition: Log.cpp:556
uint32 itemid
Definition: LootMgr.h:103
char const * GetEntryName() const
Definition: LootMgr.h:199
void LoadLootTemplates_Prospecting()
Definition: LootMgr.cpp:1421
uint8 groupid
Definition: LootMgr.h:108
ConditionList conditions
Definition: LootMgr.h:111
void sourceObjectDestroyLink() override
Definition: LootMgr.h:247
LootStore LootTemplates_Reference
void clear()
Definition: LootMgr.h:327
bool needs_quest
Definition: LootMgr.h:107
LootSlotType
Definition: LootMgr.h:88
LootValidatorRef * getLast()
Definition: LootMgr.h:261
PermissionTypes permission
Definition: LootMgr.h:404
int32 randomPropertyId
Definition: LootMgr.h:128
QuestItemMap const & GetPlayerNonQuestNonFFAConditionalItems() const
Definition: LootMgr.h:302
QuestItemMap const & GetPlayerQuestItems() const
Definition: LootMgr.h:294
ACE_UINT8 uint8
Definition: Define.h:73
Reference< TO, FROM > * getLast()
Definition: RefManager.h:42
uint8 unlootedCount
Definition: LootMgr.h:310
void targetObjectDestroyLink() override
Definition: LootMgr.h:246
char const * m_name
Definition: LootMgr.h:208
LootType
Definition: LootMgr.h:54
Definition: LootMgr.h:290
uint8 index
Definition: LootMgr.h:148
LootStore LootTemplates_Fishing
void LoadLootTemplates_Gameobject()
Definition: LootMgr.cpp:1354
LootTemplateMap m_LootTemplates
Definition: LootMgr.h:207
bool Roll() const
Definition: LootMgr.cpp:231
uint64 roundRobinPlayer
Definition: LootMgr.h:311
QuestItem(uint8 _index, bool _islooted=false)
Definition: LootMgr.h:154
void LoadLootTemplates_Mail()
Definition: LootMgr.cpp:1436
LootStore LootTemplates_Creature
LootStoreItem(uint32 _itemid, uint32 _reference, float _chance, bool _needs_quest, uint8 _groupid, int32 _mincount, uint8 _maxcount)
Definition: LootMgr.h:115
std::set< uint32 > LootIdSet
Definition: LootMgr.h:166
unsupported by client, sending LOOT_SKINNING instead
Definition: LootMgr.h:63
Player * viewer
Definition: LootMgr.h:403
bool empty() const
Definition: LootMgr.h:351
uint8 mincount
Definition: LootMgr.h:109
bool HaveLootFor(uint32 loot_id) const
Definition: LootMgr.h:184
LootView(Loot &_loot, Player *_viewer, PermissionTypes _permission=ALL_PERMISSION)
Definition: LootMgr.h:405
char const * GetName() const
Definition: LootMgr.h:195
uint32 gold
Definition: LootMgr.h:309
virtual ~LootStore()
Definition: LootMgr.h:172
Loot(uint32 _gold=0)
Definition: LootMgr.h:314
LootStore LootTemplates_Gameobject
void LoadLootTemplates_Reference()
Definition: LootMgr.cpp:1478
ACE_UINT64 uint64
Definition: Define.h:70
bool IsValid(LootStore const &store, uint32 entry) const
Definition: LootMgr.cpp:247
void AddLooter(uint64 GUID)
Definition: LootMgr.h:363
QuestItem()
Definition: LootMgr.h:151
Reference< TO, FROM > * getFirst()
Definition: RefManager.h:34
std::vector< LootItem > items
Definition: LootMgr.h:307
bool isLooted() const
Definition: LootMgr.h:355
unsupported by client, sending LOOT_SKINNING instead
Definition: LootMgr.h:66
UNORDERED_MAP< uint32, LootTemplate * > LootTemplateMap
Definition: LootMgr.h:164
LootStore LootTemplates_Pickpocketing
LootType loot_type
Definition: LootMgr.h:312
std::vector< QuestItem > QuestItemList
Definition: LootMgr.h:159
LootStore LootTemplates_Disenchant
LootValidatorRefManager i_LootValidatorRefManager
Definition: LootMgr.h:396
LootStore(char const *name, char const *entryName)
Definition: LootMgr.h:171
unsupported by client, sending LOOT_SKINNING instead
Definition: LootMgr.h:62
LootMethod
Definition: LootMgr.h:34
LinkedListHead::Iterator< LootValidatorRef > iterator
Definition: LootMgr.h:255
void RemoveLooter(uint64 GUID)
Definition: LootMgr.h:367
QuestItemMap PlayerQuestItems
Definition: LootMgr.h:391
QuestItemMap const & GetPlayerFFAItems() const
Definition: LootMgr.h:298
LootStore LootTemplates_Prospecting
ByteBuffer & operator<<(ByteBuffer &b, LootItem const &li)
Definition: LootMgr.cpp:751
void LoadLootTemplates_Fishing()
Definition: LootMgr.cpp:1339
Loot & loot
Definition: LootMgr.h:402
void LoadLootTemplates_Item()
Definition: LootMgr.cpp:1380
ACE_UINT32 uint32
Definition: Define.h:71
~Loot()
Definition: LootMgr.h:315
LootStore LootTemplates_Skinning
QuestItemMap PlayerNonQuestNonFFAConditionalItems
Definition: LootMgr.h:393
void LoadLootTemplates_Disenchant()
Definition: LootMgr.cpp:1314
uint32 itemid
Definition: LootMgr.h:126
Definition: Player.h:922
std::vector< LootItem > quest_items
Definition: LootMgr.h:308
uint32 reference
Definition: LootMgr.h:105
LootValidatorRef * getFirst()
Definition: LootMgr.h:257
LootError
Definition: LootMgr.h:70
void LoadLootTemplates_Creature()
Definition: LootMgr.cpp:1288
void LoadLootTemplates_Pickpocketing()
Definition: LootMgr.cpp:1395