[7160] Implement (un-)learning skill level dependent spells at skill level change.
[getmangos.git] / src / game / LootMgr.h
blob7c493859f535bdfceecbab54f0e59b0966843808
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 //=====================================================
212 struct Loot
214 QuestItemMap const& GetPlayerQuestItems() const { return PlayerQuestItems; }
215 QuestItemMap const& GetPlayerFFAItems() const { return PlayerFFAItems; }
216 QuestItemMap const& GetPlayerNonQuestNonFFAConditionalItems() const { return PlayerNonQuestNonFFAConditionalItems; }
218 QuestItemList* FillFFALoot(Player* player);
219 QuestItemList* FillQuestLoot(Player* player);
220 QuestItemList* FillNonQuestNonFFAConditionalLoot(Player* player);
222 std::vector<LootItem> items;
223 std::vector<LootItem> quest_items;
224 uint32 gold;
225 uint8 unlootedCount;
227 Loot(uint32 _gold = 0) : gold(_gold), unlootedCount(0) {}
228 ~Loot() { clear(); }
230 // if loot becomes invalid this reference is used to inform the listener
231 void addLootValidatorRef(LootValidatorRef* pLootValidatorRef)
233 i_LootValidatorRefManager.insertFirst(pLootValidatorRef);
236 // void clear();
237 void clear()
239 items.clear(); gold = 0; PlayersLooting.clear();
240 for (QuestItemMap::iterator itr = PlayerQuestItems.begin(); itr != PlayerQuestItems.end(); ++itr)
241 delete itr->second;
242 for (QuestItemMap::iterator itr = PlayerFFAItems.begin(); itr != PlayerFFAItems.end(); ++itr)
243 delete itr->second;
244 for (QuestItemMap::iterator itr = PlayerNonQuestNonFFAConditionalItems.begin(); itr != PlayerNonQuestNonFFAConditionalItems.end(); ++itr)
245 delete itr->second;
247 PlayerQuestItems.clear();
248 PlayerFFAItems.clear();
249 PlayerNonQuestNonFFAConditionalItems.clear();
251 items.clear();
252 quest_items.clear();
253 gold = 0;
254 unlootedCount = 0;
255 i_LootValidatorRefManager.clearReferences();
258 bool empty() const { return items.empty() && gold == 0; }
259 bool isLooted() const { return gold == 0 && unlootedCount == 0; }
261 void NotifyItemRemoved(uint8 lootIndex);
262 void NotifyQuestItemRemoved(uint8 questIndex);
263 void NotifyMoneyRemoved();
264 void AddLooter(uint64 GUID) { PlayersLooting.insert(GUID); }
265 void RemoveLooter(uint64 GUID) { PlayersLooting.erase(GUID); }
267 void generateMoneyLoot(uint32 minAmount, uint32 maxAmount);
268 void FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner);
270 // Inserts the item into the loot (called by LootTemplate processors)
271 void AddItem(LootStoreItem const & item);
273 LootItem* LootItemInSlot(uint32 lootslot, Player* player, QuestItem** qitem = NULL, QuestItem** ffaitem = NULL, QuestItem** conditem = NULL);
274 private:
275 std::set<uint64> PlayersLooting;
276 QuestItemMap PlayerQuestItems;
277 QuestItemMap PlayerFFAItems;
278 QuestItemMap PlayerNonQuestNonFFAConditionalItems;
280 // All rolls are registered here. They need to know, when the loot is not valid anymore
281 LootValidatorRefManager i_LootValidatorRefManager;
285 struct LootView
287 Loot &loot;
288 QuestItemList *qlist;
289 QuestItemList *ffalist;
290 QuestItemList *conditionallist;
291 Player *viewer;
292 PermissionTypes permission;
293 LootView(Loot &_loot, QuestItemList *_qlist, QuestItemList *_ffalist, QuestItemList *_conditionallist, Player *_viewer,PermissionTypes _permission = ALL_PERMISSION)
294 : loot(_loot), qlist(_qlist), ffalist(_ffalist), conditionallist(_conditionallist), viewer(_viewer), permission(_permission) {}
297 extern LootStore LootTemplates_Creature;
298 extern LootStore LootTemplates_Fishing;
299 extern LootStore LootTemplates_Gameobject;
300 extern LootStore LootTemplates_Item;
301 extern LootStore LootTemplates_Milling;
302 extern LootStore LootTemplates_Pickpocketing;
303 extern LootStore LootTemplates_Skinning;
304 extern LootStore LootTemplates_Disenchant;
305 extern LootStore LootTemplates_Prospecting;
306 extern LootStore LootTemplates_QuestMail;
307 extern LootStore LootTemplates_Spell;
309 void LoadLootTemplates_Creature();
310 void LoadLootTemplates_Fishing();
311 void LoadLootTemplates_Gameobject();
312 void LoadLootTemplates_Item();
313 void LoadLootTemplates_Milling();
314 void LoadLootTemplates_Pickpocketing();
315 void LoadLootTemplates_Skinning();
316 void LoadLootTemplates_Disenchant();
317 void LoadLootTemplates_Prospecting();
318 void LoadLootTemplates_QuestMail();
320 void LoadLootTemplates_Spell();
321 void LoadLootTemplates_Reference();
323 inline void LoadLootTables()
325 LoadLootTemplates_Creature();
326 LoadLootTemplates_Fishing();
327 LoadLootTemplates_Gameobject();
328 LoadLootTemplates_Item();
329 LoadLootTemplates_Milling();
330 LoadLootTemplates_Pickpocketing();
331 LoadLootTemplates_Skinning();
332 LoadLootTemplates_Disenchant();
333 LoadLootTemplates_Prospecting();
334 LoadLootTemplates_QuestMail();
335 LoadLootTemplates_Spell();
337 LoadLootTemplates_Reference();
340 ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li);
341 ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
342 #endif