[8779] Not output error at empty item loot for mail template at mail creating.
[getmangos.git] / src / game / LootMgr.h
blob9263e323b41ef2cd7a76914ccb8e06e46f3794ca
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,
34 MAX_ROLL_TYPE = 3
37 #define MAX_NR_LOOT_ITEMS 16
38 // note: the client cannot show more than 16 items total
39 #define MAX_NR_QUEST_ITEMS 32
40 // unrelated to the number of quest items shown, just for reserve
42 enum LootMethod
44 FREE_FOR_ALL = 0,
45 ROUND_ROBIN = 1,
46 MASTER_LOOT = 2,
47 GROUP_LOOT = 3,
48 NEED_BEFORE_GREED = 4
51 enum PermissionTypes
53 ALL_PERMISSION = 0,
54 GROUP_PERMISSION = 1,
55 MASTER_PERMISSION = 2,
56 NONE_PERMISSION = 3
59 enum LootType
61 LOOT_CORPSE = 1,
62 LOOT_PICKPOCKETING = 2,
63 LOOT_FISHING = 3,
64 LOOT_DISENCHANTING = 4,
65 // ignored always by client
66 LOOT_SKINNING = 6,
67 LOOT_PROSPECTING = 7,
68 LOOT_MILLING = 8,
70 LOOT_FISHINGHOLE = 20, // unsupported by client, sending LOOT_FISHING instead
71 LOOT_INSIGNIA = 21 // unsupported by client, sending LOOT_CORPSE instead
74 class Player;
75 class LootStore;
77 struct LootStoreItem
79 uint32 itemid; // id of the item
80 float chance; // always positive, chance to drop for both quest and non-quest items, chance to be used for refs
81 int32 mincountOrRef; // mincount for drop items (positive) or minus referenced TemplateleId (negative)
82 uint8 group :7;
83 bool needs_quest :1; // quest drop (negative ChanceOrQuestChance in DB)
84 uint8 maxcount :8; // max drop count for the item (mincountOrRef positive) or Ref multiplicator (mincountOrRef negative)
85 uint16 conditionId :16; // additional loot condition Id
87 // Constructor, converting ChanceOrQuestChance -> (chance, needs_quest)
88 // displayid is filled in IsValid() which must be called after
89 LootStoreItem(uint32 _itemid, float _chanceOrQuestChance, int8 _group, uint8 _conditionId, int32 _mincountOrRef, uint8 _maxcount)
90 : itemid(_itemid), chance(fabs(_chanceOrQuestChance)), mincountOrRef(_mincountOrRef),
91 group(_group), needs_quest(_chanceOrQuestChance < 0), maxcount(_maxcount), conditionId(_conditionId)
94 bool Roll(bool rate) const; // Checks if the entry takes it's chance (at loot generation)
95 bool IsValid(LootStore const& store, uint32 entry) const;
96 // Checks correctness of values
99 struct LootItem
101 uint32 itemid;
102 uint32 randomSuffix;
103 int32 randomPropertyId;
104 uint16 conditionId :16; // allow compiler pack structure
105 uint8 count : 8;
106 bool is_looted : 1;
107 bool is_blocked : 1;
108 bool freeforall : 1; // free for all
109 bool is_underthreshold : 1;
110 bool is_counted : 1;
111 bool needs_quest : 1; // quest drop
113 // Constructor, copies most fields from LootStoreItem, generates random count and random suffixes/properties
114 // Should be called for non-reference LootStoreItem entries only (mincountOrRef > 0)
115 explicit LootItem(LootStoreItem const& li);
117 // Basic checks for player/item compatibility - if false no chance to see the item in the loot
118 bool AllowedForPlayer(Player const * player) const;
121 struct QuestItem
123 uint8 index; // position in quest_items;
124 bool is_looted;
126 QuestItem()
127 : index(0), is_looted(false) {}
129 QuestItem(uint8 _index, bool _islooted = false)
130 : index(_index), is_looted(_islooted) {}
133 struct Loot;
134 class LootTemplate;
136 typedef std::vector<QuestItem> QuestItemList;
137 typedef std::map<uint32, QuestItemList *> QuestItemMap;
138 typedef std::vector<LootStoreItem> LootStoreItemList;
139 typedef UNORDERED_MAP<uint32, LootTemplate*> LootTemplateMap;
141 typedef std::set<uint32> LootIdSet;
143 class LootStore
145 public:
146 explicit LootStore(char const* name, char const* entryName, bool ratesAllowed)
147 : m_name(name), m_entryName(entryName), m_ratesAllowed(ratesAllowed) {}
148 virtual ~LootStore() { Clear(); }
150 void Verify() const;
152 void LoadAndCollectLootIds(LootIdSet& ids_set);
153 void CheckLootRefs(LootIdSet* ref_set = NULL) const;// check existence reference and remove it from ref_set
154 void ReportUnusedIds(LootIdSet const& ids_set) const;
155 void ReportNotExistedId(uint32 id) const;
157 bool HaveLootFor(uint32 loot_id) const { return m_LootTemplates.find(loot_id) != m_LootTemplates.end(); }
158 bool HaveQuestLootFor(uint32 loot_id) const;
159 bool HaveQuestLootForPlayer(uint32 loot_id,Player* player) const;
161 LootTemplate const* GetLootFor(uint32 loot_id) const;
163 char const* GetName() const { return m_name; }
164 char const* GetEntryName() const { return m_entryName; }
165 bool IsRatesAllowed() const { return m_ratesAllowed; }
166 protected:
167 void LoadLootTable();
168 void Clear();
169 private:
170 LootTemplateMap m_LootTemplates;
171 char const* m_name;
172 char const* m_entryName;
173 bool m_ratesAllowed;
176 class LootTemplate
178 class LootGroup; // A set of loot definitions for items (refs are not allowed inside)
179 typedef std::vector<LootGroup> LootGroups;
181 public:
182 // Adds an entry to the group (at loading stage)
183 void AddEntry(LootStoreItem& item);
184 // Rolls for every item in the template and adds the rolled items the the loot
185 void Process(Loot& loot, LootStore const& store, bool rate, uint8 GroupId = 0) const;
187 // True if template includes at least 1 quest drop entry
188 bool HasQuestDrop(LootTemplateMap const& store, uint8 GroupId = 0) const;
189 // True if template includes at least 1 quest drop for an active quest of the player
190 bool HasQuestDropForPlayer(LootTemplateMap const& store, Player const * player, uint8 GroupId = 0) const;
192 // Checks integrity of the template
193 void Verify(LootStore const& store, uint32 Id) const;
194 void CheckLootRefs(LootIdSet* ref_set) const;
195 private:
196 LootStoreItemList Entries; // not grouped only
197 LootGroups Groups; // groups have own (optimised) processing, grouped entries go there
200 //=====================================================
202 class LootValidatorRef : public Reference<Loot, LootValidatorRef>
204 public:
205 LootValidatorRef() {}
206 void targetObjectDestroyLink() {}
207 void sourceObjectDestroyLink() {}
210 //=====================================================
212 class LootValidatorRefManager : public RefManager<Loot, LootValidatorRef>
214 public:
215 typedef LinkedListHead::Iterator< LootValidatorRef > iterator;
217 LootValidatorRef* getFirst() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getFirst(); }
218 LootValidatorRef* getLast() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getLast(); }
220 iterator begin() { return iterator(getFirst()); }
221 iterator end() { return iterator(NULL); }
222 iterator rbegin() { return iterator(getLast()); }
223 iterator rend() { return iterator(NULL); }
226 //=====================================================
227 struct LootView;
229 ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li);
230 ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
232 struct Loot
234 friend ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
236 QuestItemMap const& GetPlayerQuestItems() const { return PlayerQuestItems; }
237 QuestItemMap const& GetPlayerFFAItems() const { return PlayerFFAItems; }
238 QuestItemMap const& GetPlayerNonQuestNonFFAConditionalItems() const { return PlayerNonQuestNonFFAConditionalItems; }
240 std::vector<LootItem> items;
241 uint32 gold;
242 uint8 unlootedCount;
243 LootType loot_type; // required for achievement system
245 Loot(uint32 _gold = 0) : gold(_gold), unlootedCount(0), loot_type(LOOT_CORPSE) {}
246 ~Loot() { clear(); }
248 // if loot becomes invalid this reference is used to inform the listener
249 void addLootValidatorRef(LootValidatorRef* pLootValidatorRef)
251 i_LootValidatorRefManager.insertFirst(pLootValidatorRef);
254 // void clear();
255 void clear()
257 for (QuestItemMap::const_iterator itr = PlayerQuestItems.begin(); itr != PlayerQuestItems.end(); ++itr)
258 delete itr->second;
259 PlayerQuestItems.clear();
261 for (QuestItemMap::const_iterator itr = PlayerFFAItems.begin(); itr != PlayerFFAItems.end(); ++itr)
262 delete itr->second;
263 PlayerFFAItems.clear();
265 for (QuestItemMap::const_iterator itr = PlayerNonQuestNonFFAConditionalItems.begin(); itr != PlayerNonQuestNonFFAConditionalItems.end(); ++itr)
266 delete itr->second;
267 PlayerNonQuestNonFFAConditionalItems.clear();
269 PlayersLooting.clear();
270 items.clear();
271 quest_items.clear();
272 gold = 0;
273 unlootedCount = 0;
274 i_LootValidatorRefManager.clearReferences();
277 bool empty() const { return items.empty() && gold == 0; }
278 bool isLooted() const { return gold == 0 && unlootedCount == 0; }
280 void NotifyItemRemoved(uint8 lootIndex);
281 void NotifyQuestItemRemoved(uint8 questIndex);
282 void NotifyMoneyRemoved();
283 void AddLooter(uint64 GUID) { PlayersLooting.insert(GUID); }
284 void RemoveLooter(uint64 GUID) { PlayersLooting.erase(GUID); }
286 void generateMoneyLoot(uint32 minAmount, uint32 maxAmount);
287 bool FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner, bool personal, bool noEmptyError = false);
289 // Inserts the item into the loot (called by LootTemplate processors)
290 void AddItem(LootStoreItem const & item);
292 LootItem* LootItemInSlot(uint32 lootslot, Player* player, QuestItem** qitem = NULL, QuestItem** ffaitem = NULL, QuestItem** conditem = NULL);
293 uint32 GetMaxSlotInLootFor(Player* player) const;
295 private:
296 void FillNotNormalLootFor(Player* player);
297 QuestItemList* FillFFALoot(Player* player);
298 QuestItemList* FillQuestLoot(Player* player);
299 QuestItemList* FillNonQuestNonFFAConditionalLoot(Player* player);
301 std::vector<LootItem> quest_items;
302 std::set<uint64> PlayersLooting;
303 QuestItemMap PlayerQuestItems;
304 QuestItemMap PlayerFFAItems;
305 QuestItemMap PlayerNonQuestNonFFAConditionalItems;
307 // All rolls are registered here. They need to know, when the loot is not valid anymore
308 LootValidatorRefManager i_LootValidatorRefManager;
311 struct LootView
313 Loot &loot;
314 Player *viewer;
315 PermissionTypes permission;
316 LootView(Loot &_loot, Player *_viewer,PermissionTypes _permission = ALL_PERMISSION)
317 : loot(_loot), viewer(_viewer), permission(_permission) {}
320 extern LootStore LootTemplates_Creature;
321 extern LootStore LootTemplates_Fishing;
322 extern LootStore LootTemplates_Gameobject;
323 extern LootStore LootTemplates_Item;
324 extern LootStore LootTemplates_Mail;
325 extern LootStore LootTemplates_Milling;
326 extern LootStore LootTemplates_Pickpocketing;
327 extern LootStore LootTemplates_Skinning;
328 extern LootStore LootTemplates_Disenchant;
329 extern LootStore LootTemplates_Prospecting;
330 extern LootStore LootTemplates_Spell;
332 void LoadLootTemplates_Creature();
333 void LoadLootTemplates_Fishing();
334 void LoadLootTemplates_Gameobject();
335 void LoadLootTemplates_Item();
336 void LoadLootTemplates_Mail();
337 void LoadLootTemplates_Milling();
338 void LoadLootTemplates_Pickpocketing();
339 void LoadLootTemplates_Skinning();
340 void LoadLootTemplates_Disenchant();
341 void LoadLootTemplates_Prospecting();
343 void LoadLootTemplates_Spell();
344 void LoadLootTemplates_Reference();
346 inline void LoadLootTables()
348 LoadLootTemplates_Creature();
349 LoadLootTemplates_Fishing();
350 LoadLootTemplates_Gameobject();
351 LoadLootTemplates_Item();
352 LoadLootTemplates_Mail();
353 LoadLootTemplates_Milling();
354 LoadLootTemplates_Pickpocketing();
355 LoadLootTemplates_Skinning();
356 LoadLootTemplates_Disenchant();
357 LoadLootTemplates_Prospecting();
358 LoadLootTemplates_Spell();
360 LoadLootTemplates_Reference();
363 #endif