[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / LootMgr.h
blob0c34d790461881ff1a3609986593ce8ed1ef40fc
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 class Player;
59 class LootStore;
61 struct LootStoreItem
63 uint32 itemid; // id of the item
64 float chance; // always positive, chance to drop for both quest and non-quest items, chance to be used for refs
65 int32 mincountOrRef; // mincount for drop items (positive) or minus referenced TemplateleId (negative)
66 uint8 group :8;
67 uint8 maxcount :8; // max drop count for the item (mincountOrRef positive) or Ref multiplicator (mincountOrRef negative)
68 uint16 conditionId :16; // additional loot condition Id
69 bool needs_quest :1; // quest drop (negative ChanceOrQuestChance in DB)
71 // Constructor, converting ChanceOrQuestChance -> (chance, needs_quest)
72 // displayid is filled in IsValid() which must be called after
73 LootStoreItem(uint32 _itemid, float _chanceOrQuestChance, int8 _group, uint8 _conditionId, int32 _mincountOrRef, uint8 _maxcount)
74 : itemid(_itemid), chance(fabs(_chanceOrQuestChance)), mincountOrRef(_mincountOrRef),
75 group(_group), maxcount(_maxcount), conditionId(_conditionId),
76 needs_quest(_chanceOrQuestChance < 0) {}
78 bool Roll(bool rate) const; // Checks if the entry takes it's chance (at loot generation)
79 bool IsValid(LootStore const& store, uint32 entry) const;
80 // Checks correctness of values
83 struct LootItem
85 uint32 itemid;
86 uint32 randomSuffix;
87 int32 randomPropertyId;
88 uint16 conditionId :16; // allow compiler pack structure
89 uint8 count : 8;
90 bool is_looted : 1;
91 bool is_blocked : 1;
92 bool freeforall : 1; // free for all
93 bool is_underthreshold : 1;
94 bool is_counted : 1;
95 bool needs_quest : 1; // quest drop
97 // Constructor, copies most fields from LootStoreItem, generates random count and random suffixes/properties
98 // Should be called for non-reference LootStoreItem entries only (mincountOrRef > 0)
99 explicit LootItem(LootStoreItem const& li);
101 // Basic checks for player/item compatibility - if false no chance to see the item in the loot
102 bool AllowedForPlayer(Player const * player) const;
105 struct QuestItem
107 uint8 index; // position in quest_items;
108 bool is_looted;
110 QuestItem()
111 : index(0), is_looted(false) {}
113 QuestItem(uint8 _index, bool _islooted = false)
114 : index(_index), is_looted(_islooted) {}
117 struct Loot;
118 class LootTemplate;
120 typedef std::vector<QuestItem> QuestItemList;
121 typedef std::map<uint32, QuestItemList *> QuestItemMap;
122 typedef std::vector<LootStoreItem> LootStoreItemList;
123 typedef UNORDERED_MAP<uint32, LootTemplate*> LootTemplateMap;
125 typedef std::set<uint32> LootIdSet;
127 class LootStore
129 public:
130 explicit LootStore(char const* name, char const* entryName, bool ratesAllowed)
131 : m_name(name), m_entryName(entryName), m_ratesAllowed(ratesAllowed) {}
132 virtual ~LootStore() { Clear(); }
134 void Verify() const;
136 void LoadAndCollectLootIds(LootIdSet& ids_set);
137 void CheckLootRefs(LootIdSet* ref_set = NULL) const;// check existence reference and remove it from ref_set
138 void ReportUnusedIds(LootIdSet const& ids_set) const;
139 void ReportNotExistedId(uint32 id) const;
141 bool HaveLootFor(uint32 loot_id) const { return m_LootTemplates.find(loot_id) != m_LootTemplates.end(); }
142 bool HaveQuestLootFor(uint32 loot_id) const;
143 bool HaveQuestLootForPlayer(uint32 loot_id,Player* player) const;
145 LootTemplate const* GetLootFor(uint32 loot_id) const;
147 char const* GetName() const { return m_name; }
148 char const* GetEntryName() const { return m_entryName; }
149 bool IsRatesAllowed() const { return m_ratesAllowed; }
150 protected:
151 void LoadLootTable();
152 void Clear();
153 private:
154 LootTemplateMap m_LootTemplates;
155 char const* m_name;
156 char const* m_entryName;
157 bool m_ratesAllowed;
160 class LootTemplate
162 class LootGroup; // A set of loot definitions for items (refs are not allowed inside)
163 typedef std::vector<LootGroup> LootGroups;
165 public:
166 // Adds an entry to the group (at loading stage)
167 void AddEntry(LootStoreItem& item);
168 // Rolls for every item in the template and adds the rolled items the the loot
169 void Process(Loot& loot, LootStore const& store, bool rate, uint8 GroupId = 0) const;
171 // True if template includes at least 1 quest drop entry
172 bool HasQuestDrop(LootTemplateMap const& store, uint8 GroupId = 0) const;
173 // True if template includes at least 1 quest drop for an active quest of the player
174 bool HasQuestDropForPlayer(LootTemplateMap const& store, Player const * player, uint8 GroupId = 0) const;
176 // Checks integrity of the template
177 void Verify(LootStore const& store, uint32 Id) const;
178 void CheckLootRefs(LootIdSet* ref_set) const;
179 private:
180 LootStoreItemList Entries; // not grouped only
181 LootGroups Groups; // groups have own (optimised) processing, grouped entries go there
184 //=====================================================
186 class LootValidatorRef : public Reference<Loot, LootValidatorRef>
188 public:
189 LootValidatorRef() {}
190 void targetObjectDestroyLink() {}
191 void sourceObjectDestroyLink() {}
194 //=====================================================
196 class LootValidatorRefManager : public RefManager<Loot, LootValidatorRef>
198 public:
199 typedef LinkedListHead::Iterator< LootValidatorRef > iterator;
201 LootValidatorRef* getFirst() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getFirst(); }
202 LootValidatorRef* getLast() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getLast(); }
204 iterator begin() { return iterator(getFirst()); }
205 iterator end() { return iterator(NULL); }
206 iterator rbegin() { return iterator(getLast()); }
207 iterator rend() { return iterator(NULL); }
210 //=====================================================
211 struct LootView;
213 ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li);
214 ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
216 struct Loot
218 friend ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
220 QuestItemMap const& GetPlayerQuestItems() const { return PlayerQuestItems; }
221 QuestItemMap const& GetPlayerFFAItems() const { return PlayerFFAItems; }
222 QuestItemMap const& GetPlayerNonQuestNonFFAConditionalItems() const { return PlayerNonQuestNonFFAConditionalItems; }
224 std::vector<LootItem> items;
225 uint32 gold;
226 uint8 unlootedCount;
228 Loot(uint32 _gold = 0) : gold(_gold), unlootedCount(0) {}
229 ~Loot() { clear(); }
231 // if loot becomes invalid this reference is used to inform the listener
232 void addLootValidatorRef(LootValidatorRef* pLootValidatorRef)
234 i_LootValidatorRefManager.insertFirst(pLootValidatorRef);
237 // void clear();
238 void clear()
240 for (QuestItemMap::iterator itr = PlayerQuestItems.begin(); itr != PlayerQuestItems.end(); ++itr)
241 delete itr->second;
242 PlayerQuestItems.clear();
244 for (QuestItemMap::iterator itr = PlayerFFAItems.begin(); itr != PlayerFFAItems.end(); ++itr)
245 delete itr->second;
246 PlayerFFAItems.clear();
248 for (QuestItemMap::iterator itr = PlayerNonQuestNonFFAConditionalItems.begin(); itr != PlayerNonQuestNonFFAConditionalItems.end(); ++itr)
249 delete itr->second;
250 PlayerNonQuestNonFFAConditionalItems.clear();
252 PlayersLooting.clear();
253 items.clear();
254 quest_items.clear();
255 gold = 0;
256 unlootedCount = 0;
257 i_LootValidatorRefManager.clearReferences();
260 bool empty() const { return items.empty() && gold == 0; }
261 bool isLooted() const { return gold == 0 && unlootedCount == 0; }
263 void NotifyItemRemoved(uint8 lootIndex);
264 void NotifyQuestItemRemoved(uint8 questIndex);
265 void NotifyMoneyRemoved();
266 void AddLooter(uint64 GUID) { PlayersLooting.insert(GUID); }
267 void RemoveLooter(uint64 GUID) { PlayersLooting.erase(GUID); }
269 void generateMoneyLoot(uint32 minAmount, uint32 maxAmount);
270 void FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner, bool personal);
272 // Inserts the item into the loot (called by LootTemplate processors)
273 void AddItem(LootStoreItem const & item);
275 LootItem* LootItemInSlot(uint32 lootslot, Player* player, QuestItem** qitem = NULL, QuestItem** ffaitem = NULL, QuestItem** conditem = NULL);
276 uint32 GetMaxSlotInLootFor(Player* player) const;
278 private:
279 void FillNotNormalLootFor(Player* player);
280 QuestItemList* FillFFALoot(Player* player);
281 QuestItemList* FillQuestLoot(Player* player);
282 QuestItemList* FillNonQuestNonFFAConditionalLoot(Player* player);
284 std::vector<LootItem> quest_items;
285 std::set<uint64> PlayersLooting;
286 QuestItemMap PlayerQuestItems;
287 QuestItemMap PlayerFFAItems;
288 QuestItemMap PlayerNonQuestNonFFAConditionalItems;
290 // All rolls are registered here. They need to know, when the loot is not valid anymore
291 LootValidatorRefManager i_LootValidatorRefManager;
294 struct LootView
296 Loot &loot;
297 Player *viewer;
298 PermissionTypes permission;
299 LootView(Loot &_loot, Player *_viewer,PermissionTypes _permission = ALL_PERMISSION)
300 : loot(_loot), viewer(_viewer), permission(_permission) {}
303 extern LootStore LootTemplates_Creature;
304 extern LootStore LootTemplates_Fishing;
305 extern LootStore LootTemplates_Gameobject;
306 extern LootStore LootTemplates_Item;
307 extern LootStore LootTemplates_Milling;
308 extern LootStore LootTemplates_Pickpocketing;
309 extern LootStore LootTemplates_Skinning;
310 extern LootStore LootTemplates_Disenchant;
311 extern LootStore LootTemplates_Prospecting;
312 extern LootStore LootTemplates_QuestMail;
313 extern LootStore LootTemplates_Spell;
315 void LoadLootTemplates_Creature();
316 void LoadLootTemplates_Fishing();
317 void LoadLootTemplates_Gameobject();
318 void LoadLootTemplates_Item();
319 void LoadLootTemplates_Milling();
320 void LoadLootTemplates_Pickpocketing();
321 void LoadLootTemplates_Skinning();
322 void LoadLootTemplates_Disenchant();
323 void LoadLootTemplates_Prospecting();
324 void LoadLootTemplates_QuestMail();
326 void LoadLootTemplates_Spell();
327 void LoadLootTemplates_Reference();
329 inline void LoadLootTables()
331 LoadLootTemplates_Creature();
332 LoadLootTemplates_Fishing();
333 LoadLootTemplates_Gameobject();
334 LoadLootTemplates_Item();
335 LoadLootTemplates_Milling();
336 LoadLootTemplates_Pickpocketing();
337 LoadLootTemplates_Skinning();
338 LoadLootTemplates_Disenchant();
339 LoadLootTemplates_Prospecting();
340 LoadLootTemplates_QuestMail();
341 LoadLootTemplates_Spell();
343 LoadLootTemplates_Reference();
346 #endif