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"
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
54 MASTER_PERMISSION
= 2,
61 LOOT_PICKPOCKETING
= 2,
63 LOOT_DISENCHANTING
= 4,
64 // ignored always by client
69 LOOT_FISHINGHOLE
= 20, // unsupported by client, sending LOOT_FISHING instead
70 LOOT_INSIGNIA
= 21 // unsupported by client, sending LOOT_CORPSE instead
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)
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
102 int32 randomPropertyId
;
103 uint16 conditionId
:16; // allow compiler pack structure
107 bool freeforall
: 1; // free for all
108 bool is_underthreshold
: 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;
122 uint8 index
; // position in quest_items;
126 : index(0), is_looted(false) {}
128 QuestItem(uint8 _index
, bool _islooted
= false)
129 : index(_index
), is_looted(_islooted
) {}
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
;
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(); }
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
; }
166 void LoadLootTable();
169 LootTemplateMap m_LootTemplates
;
171 char const* m_entryName
;
177 class LootGroup
; // A set of loot definitions for items (refs are not allowed inside)
178 typedef std::vector
<LootGroup
> LootGroups
;
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;
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
>
204 LootValidatorRef() {}
205 void targetObjectDestroyLink() {}
206 void sourceObjectDestroyLink() {}
209 //=====================================================
211 class LootValidatorRefManager
: public RefManager
<Loot
, LootValidatorRef
>
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 //=====================================================
228 ByteBuffer
& operator<<(ByteBuffer
& b
, LootItem
const& li
);
229 ByteBuffer
& operator<<(ByteBuffer
& b
, LootView
const& lv
);
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
;
242 LootType loot_type
; // required for achievement system
244 Loot(uint32 _gold
= 0) : gold(_gold
), unlootedCount(0), loot_type(LOOT_CORPSE
) {}
247 // if loot becomes invalid this reference is used to inform the listener
248 void addLootValidatorRef(LootValidatorRef
* pLootValidatorRef
)
250 i_LootValidatorRefManager
.insertFirst(pLootValidatorRef
);
256 for (QuestItemMap::const_iterator itr
= PlayerQuestItems
.begin(); itr
!= PlayerQuestItems
.end(); ++itr
)
258 PlayerQuestItems
.clear();
260 for (QuestItemMap::const_iterator itr
= PlayerFFAItems
.begin(); itr
!= PlayerFFAItems
.end(); ++itr
)
262 PlayerFFAItems
.clear();
264 for (QuestItemMap::const_iterator itr
= PlayerNonQuestNonFFAConditionalItems
.begin(); itr
!= PlayerNonQuestNonFFAConditionalItems
.end(); ++itr
)
266 PlayerNonQuestNonFFAConditionalItems
.clear();
268 PlayersLooting
.clear();
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;
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
;
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();