[8483] Implement glyph 43361.
[getmangos.git] / src / game / LootMgr.h
blobb67ef9a3117cac0668fa8946c783e91d55610978
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_LOOTMGR_H
20 #define MANGOS_LOOTMGR_H
22 #include "ItemEnchantmentMgr.h"
23 #include "ByteBuffer.h"
24 #include "Utilities/LinkedReference/RefManager.h"
26 #include <map>
27 #include <vector>
29 enum RollType
31 ROLL_PASS = 0,
32 ROLL_NEED = 1,
33 ROLL_GREED = 2
36 #define MAX_NR_LOOT_ITEMS 16
37 // note: the client cannot show more than 16 items total
38 #define MAX_NR_QUEST_ITEMS 32
39 // unrelated to the number of quest items shown, just for reserve
41 enum LootMethod
43 FREE_FOR_ALL = 0,
44 ROUND_ROBIN = 1,
45 MASTER_LOOT = 2,
46 GROUP_LOOT = 3,
47 NEED_BEFORE_GREED = 4
50 enum PermissionTypes
52 ALL_PERMISSION = 0,
53 GROUP_PERMISSION = 1,
54 MASTER_PERMISSION = 2,
55 NONE_PERMISSION = 3
58 enum LootType
60 LOOT_CORPSE = 1,
61 LOOT_PICKPOCKETING = 2,
62 LOOT_FISHING = 3,
63 LOOT_DISENCHANTING = 4,
64 // ignored always by client
65 LOOT_SKINNING = 6,
66 LOOT_PROSPECTING = 7,
67 LOOT_MILLING = 8,
69 LOOT_FISHINGHOLE = 20, // unsupported by client, sending LOOT_FISHING instead
70 LOOT_INSIGNIA = 21 // unsupported by client, sending LOOT_CORPSE instead
73 class Player;
74 class LootStore;
76 struct LootStoreItem
78 uint32 itemid; // id of the item
79 float chance; // always positive, chance to drop for both quest and non-quest items, chance to be used for refs
80 int32 mincountOrRef; // mincount for drop items (positive) or minus referenced TemplateleId (negative)
81 uint8 group :7;
82 bool needs_quest :1; // quest drop (negative ChanceOrQuestChance in DB)
83 uint8 maxcount :8; // max drop count for the item (mincountOrRef positive) or Ref multiplicator (mincountOrRef negative)
84 uint16 conditionId :16; // additional loot condition Id
86 // Constructor, converting ChanceOrQuestChance -> (chance, needs_quest)
87 // displayid is filled in IsValid() which must be called after
88 LootStoreItem(uint32 _itemid, float _chanceOrQuestChance, int8 _group, uint8 _conditionId, int32 _mincountOrRef, uint8 _maxcount)
89 : itemid(_itemid), chance(fabs(_chanceOrQuestChance)), mincountOrRef(_mincountOrRef),
90 group(_group), needs_quest(_chanceOrQuestChance < 0), maxcount(_maxcount), conditionId(_conditionId)
93 bool Roll(bool rate) const; // Checks if the entry takes it's chance (at loot generation)
94 bool IsValid(LootStore const& store, uint32 entry) const;
95 // Checks correctness of values
98 struct LootItem
100 uint32 itemid;
101 uint32 randomSuffix;
102 int32 randomPropertyId;
103 uint16 conditionId :16; // allow compiler pack structure
104 uint8 count : 8;
105 bool is_looted : 1;
106 bool is_blocked : 1;
107 bool freeforall : 1; // free for all
108 bool is_underthreshold : 1;
109 bool is_counted : 1;
110 bool needs_quest : 1; // quest drop
112 // Constructor, copies most fields from LootStoreItem, generates random count and random suffixes/properties
113 // Should be called for non-reference LootStoreItem entries only (mincountOrRef > 0)
114 explicit LootItem(LootStoreItem const& li);
116 // Basic checks for player/item compatibility - if false no chance to see the item in the loot
117 bool AllowedForPlayer(Player const * player) const;
120 struct QuestItem
122 uint8 index; // position in quest_items;
123 bool is_looted;
125 QuestItem()
126 : index(0), is_looted(false) {}
128 QuestItem(uint8 _index, bool _islooted = false)
129 : index(_index), is_looted(_islooted) {}
132 struct Loot;
133 class LootTemplate;
135 typedef std::vector<QuestItem> QuestItemList;
136 typedef std::map<uint32, QuestItemList *> QuestItemMap;
137 typedef std::vector<LootStoreItem> LootStoreItemList;
138 typedef UNORDERED_MAP<uint32, LootTemplate*> LootTemplateMap;
140 typedef std::set<uint32> LootIdSet;
142 class LootStore
144 public:
145 explicit LootStore(char const* name, char const* entryName, bool ratesAllowed)
146 : m_name(name), m_entryName(entryName), m_ratesAllowed(ratesAllowed) {}
147 virtual ~LootStore() { Clear(); }
149 void Verify() const;
151 void LoadAndCollectLootIds(LootIdSet& ids_set);
152 void CheckLootRefs(LootIdSet* ref_set = NULL) const;// check existence reference and remove it from ref_set
153 void ReportUnusedIds(LootIdSet const& ids_set) const;
154 void ReportNotExistedId(uint32 id) const;
156 bool HaveLootFor(uint32 loot_id) const { return m_LootTemplates.find(loot_id) != m_LootTemplates.end(); }
157 bool HaveQuestLootFor(uint32 loot_id) const;
158 bool HaveQuestLootForPlayer(uint32 loot_id,Player* player) const;
160 LootTemplate const* GetLootFor(uint32 loot_id) const;
162 char const* GetName() const { return m_name; }
163 char const* GetEntryName() const { return m_entryName; }
164 bool IsRatesAllowed() const { return m_ratesAllowed; }
165 protected:
166 void LoadLootTable();
167 void Clear();
168 private:
169 LootTemplateMap m_LootTemplates;
170 char const* m_name;
171 char const* m_entryName;
172 bool m_ratesAllowed;
175 class LootTemplate
177 class LootGroup; // A set of loot definitions for items (refs are not allowed inside)
178 typedef std::vector<LootGroup> LootGroups;
180 public:
181 // Adds an entry to the group (at loading stage)
182 void AddEntry(LootStoreItem& item);
183 // Rolls for every item in the template and adds the rolled items the the loot
184 void Process(Loot& loot, LootStore const& store, bool rate, uint8 GroupId = 0) const;
186 // True if template includes at least 1 quest drop entry
187 bool HasQuestDrop(LootTemplateMap const& store, uint8 GroupId = 0) const;
188 // True if template includes at least 1 quest drop for an active quest of the player
189 bool HasQuestDropForPlayer(LootTemplateMap const& store, Player const * player, uint8 GroupId = 0) const;
191 // Checks integrity of the template
192 void Verify(LootStore const& store, uint32 Id) const;
193 void CheckLootRefs(LootIdSet* ref_set) const;
194 private:
195 LootStoreItemList Entries; // not grouped only
196 LootGroups Groups; // groups have own (optimised) processing, grouped entries go there
199 //=====================================================
201 class LootValidatorRef : public Reference<Loot, LootValidatorRef>
203 public:
204 LootValidatorRef() {}
205 void targetObjectDestroyLink() {}
206 void sourceObjectDestroyLink() {}
209 //=====================================================
211 class LootValidatorRefManager : public RefManager<Loot, LootValidatorRef>
213 public:
214 typedef LinkedListHead::Iterator< LootValidatorRef > iterator;
216 LootValidatorRef* getFirst() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getFirst(); }
217 LootValidatorRef* getLast() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getLast(); }
219 iterator begin() { return iterator(getFirst()); }
220 iterator end() { return iterator(NULL); }
221 iterator rbegin() { return iterator(getLast()); }
222 iterator rend() { return iterator(NULL); }
225 //=====================================================
226 struct LootView;
228 ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li);
229 ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
231 struct Loot
233 friend ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
235 QuestItemMap const& GetPlayerQuestItems() const { return PlayerQuestItems; }
236 QuestItemMap const& GetPlayerFFAItems() const { return PlayerFFAItems; }
237 QuestItemMap const& GetPlayerNonQuestNonFFAConditionalItems() const { return PlayerNonQuestNonFFAConditionalItems; }
239 std::vector<LootItem> items;
240 uint32 gold;
241 uint8 unlootedCount;
242 LootType loot_type; // required for achievement system
244 Loot(uint32 _gold = 0) : gold(_gold), unlootedCount(0), loot_type(LOOT_CORPSE) {}
245 ~Loot() { clear(); }
247 // if loot becomes invalid this reference is used to inform the listener
248 void addLootValidatorRef(LootValidatorRef* pLootValidatorRef)
250 i_LootValidatorRefManager.insertFirst(pLootValidatorRef);
253 // void clear();
254 void clear()
256 for (QuestItemMap::const_iterator itr = PlayerQuestItems.begin(); itr != PlayerQuestItems.end(); ++itr)
257 delete itr->second;
258 PlayerQuestItems.clear();
260 for (QuestItemMap::const_iterator itr = PlayerFFAItems.begin(); itr != PlayerFFAItems.end(); ++itr)
261 delete itr->second;
262 PlayerFFAItems.clear();
264 for (QuestItemMap::const_iterator itr = PlayerNonQuestNonFFAConditionalItems.begin(); itr != PlayerNonQuestNonFFAConditionalItems.end(); ++itr)
265 delete itr->second;
266 PlayerNonQuestNonFFAConditionalItems.clear();
268 PlayersLooting.clear();
269 items.clear();
270 quest_items.clear();
271 gold = 0;
272 unlootedCount = 0;
273 i_LootValidatorRefManager.clearReferences();
276 bool empty() const { return items.empty() && gold == 0; }
277 bool isLooted() const { return gold == 0 && unlootedCount == 0; }
279 void NotifyItemRemoved(uint8 lootIndex);
280 void NotifyQuestItemRemoved(uint8 questIndex);
281 void NotifyMoneyRemoved();
282 void AddLooter(uint64 GUID) { PlayersLooting.insert(GUID); }
283 void RemoveLooter(uint64 GUID) { PlayersLooting.erase(GUID); }
285 void generateMoneyLoot(uint32 minAmount, uint32 maxAmount);
286 void FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner, bool personal);
288 // Inserts the item into the loot (called by LootTemplate processors)
289 void AddItem(LootStoreItem const & item);
291 LootItem* LootItemInSlot(uint32 lootslot, Player* player, QuestItem** qitem = NULL, QuestItem** ffaitem = NULL, QuestItem** conditem = NULL);
292 uint32 GetMaxSlotInLootFor(Player* player) const;
294 private:
295 void FillNotNormalLootFor(Player* player);
296 QuestItemList* FillFFALoot(Player* player);
297 QuestItemList* FillQuestLoot(Player* player);
298 QuestItemList* FillNonQuestNonFFAConditionalLoot(Player* player);
300 std::vector<LootItem> quest_items;
301 std::set<uint64> PlayersLooting;
302 QuestItemMap PlayerQuestItems;
303 QuestItemMap PlayerFFAItems;
304 QuestItemMap PlayerNonQuestNonFFAConditionalItems;
306 // All rolls are registered here. They need to know, when the loot is not valid anymore
307 LootValidatorRefManager i_LootValidatorRefManager;
310 struct LootView
312 Loot &loot;
313 Player *viewer;
314 PermissionTypes permission;
315 LootView(Loot &_loot, Player *_viewer,PermissionTypes _permission = ALL_PERMISSION)
316 : loot(_loot), viewer(_viewer), permission(_permission) {}
319 extern LootStore LootTemplates_Creature;
320 extern LootStore LootTemplates_Fishing;
321 extern LootStore LootTemplates_Gameobject;
322 extern LootStore LootTemplates_Item;
323 extern LootStore LootTemplates_Milling;
324 extern LootStore LootTemplates_Pickpocketing;
325 extern LootStore LootTemplates_Skinning;
326 extern LootStore LootTemplates_Disenchant;
327 extern LootStore LootTemplates_Prospecting;
328 extern LootStore LootTemplates_QuestMail;
329 extern LootStore LootTemplates_Spell;
331 void LoadLootTemplates_Creature();
332 void LoadLootTemplates_Fishing();
333 void LoadLootTemplates_Gameobject();
334 void LoadLootTemplates_Item();
335 void LoadLootTemplates_Milling();
336 void LoadLootTemplates_Pickpocketing();
337 void LoadLootTemplates_Skinning();
338 void LoadLootTemplates_Disenchant();
339 void LoadLootTemplates_Prospecting();
340 void LoadLootTemplates_QuestMail();
342 void LoadLootTemplates_Spell();
343 void LoadLootTemplates_Reference();
345 inline void LoadLootTables()
347 LoadLootTemplates_Creature();
348 LoadLootTemplates_Fishing();
349 LoadLootTemplates_Gameobject();
350 LoadLootTemplates_Item();
351 LoadLootTemplates_Milling();
352 LoadLootTemplates_Pickpocketing();
353 LoadLootTemplates_Skinning();
354 LoadLootTemplates_Disenchant();
355 LoadLootTemplates_Prospecting();
356 LoadLootTemplates_QuestMail();
357 LoadLootTemplates_Spell();
359 LoadLootTemplates_Reference();
362 #endif