2 * Copyright (C) 2005-2010 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"
29 #define MAX_NR_LOOT_ITEMS 16
30 // note: the client cannot show more than 16 items total
31 #define MAX_NR_QUEST_ITEMS 32
32 // unrelated to the number of quest items shown, just for reserve
38 MASTER_PERMISSION
= 2,
45 LOOT_PICKPOCKETING
= 2,
47 LOOT_DISENCHANTING
= 4,
48 // ignored always by client
53 LOOT_FISHINGHOLE
= 20, // unsupported by client, sending LOOT_FISHING instead
54 LOOT_INSIGNIA
= 21 // unsupported by client, sending LOOT_CORPSE instead
62 uint32 itemid
; // id of the item
63 float chance
; // always positive, chance to drop for both quest and non-quest items, chance to be used for refs
64 int32 mincountOrRef
; // mincount for drop items (positive) or minus referenced TemplateleId (negative)
66 bool needs_quest
:1; // quest drop (negative ChanceOrQuestChance in DB)
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
70 // Constructor, converting ChanceOrQuestChance -> (chance, needs_quest)
71 // displayid is filled in IsValid() which must be called after
72 LootStoreItem(uint32 _itemid
, float _chanceOrQuestChance
, int8 _group
, uint16 _conditionId
, int32 _mincountOrRef
, uint8 _maxcount
)
73 : itemid(_itemid
), chance(fabs(_chanceOrQuestChance
)), mincountOrRef(_mincountOrRef
),
74 group(_group
), needs_quest(_chanceOrQuestChance
< 0), maxcount(_maxcount
), conditionId(_conditionId
)
77 bool Roll(bool rate
) const; // Checks if the entry takes it's chance (at loot generation)
78 bool IsValid(LootStore
const& store
, uint32 entry
) const;
79 // Checks correctness of values
86 int32 randomPropertyId
;
87 uint16 conditionId
:16; // allow compiler pack structure
91 bool freeforall
: 1; // free for all
92 bool is_underthreshold
: 1;
94 bool needs_quest
: 1; // quest drop
96 // Constructor, copies most fields from LootStoreItem, generates random count and random suffixes/properties
97 // Should be called for non-reference LootStoreItem entries only (mincountOrRef > 0)
98 explicit LootItem(LootStoreItem
const& li
);
100 // Basic checks for player/item compatibility - if false no chance to see the item in the loot
101 bool AllowedForPlayer(Player
const * player
) const;
104 typedef std::vector
<LootItem
> LootItemList
;
108 uint8 index
; // position in quest_items;
112 : index(0), is_looted(false) {}
114 QuestItem(uint8 _index
, bool _islooted
= false)
115 : index(_index
), is_looted(_islooted
) {}
121 typedef std::vector
<QuestItem
> QuestItemList
;
122 typedef std::map
<uint32
, QuestItemList
*> QuestItemMap
;
123 typedef std::vector
<LootStoreItem
> LootStoreItemList
;
124 typedef UNORDERED_MAP
<uint32
, LootTemplate
*> LootTemplateMap
;
126 typedef std::set
<uint32
> LootIdSet
;
131 explicit LootStore(char const* name
, char const* entryName
, bool ratesAllowed
)
132 : m_name(name
), m_entryName(entryName
), m_ratesAllowed(ratesAllowed
) {}
133 virtual ~LootStore() { Clear(); }
137 void LoadAndCollectLootIds(LootIdSet
& ids_set
);
138 void CheckLootRefs(LootIdSet
* ref_set
= NULL
) const;// check existence reference and remove it from ref_set
139 void ReportUnusedIds(LootIdSet
const& ids_set
) const;
140 void ReportNotExistedId(uint32 id
) const;
142 bool HaveLootFor(uint32 loot_id
) const { return m_LootTemplates
.find(loot_id
) != m_LootTemplates
.end(); }
143 bool HaveQuestLootFor(uint32 loot_id
) const;
144 bool HaveQuestLootForPlayer(uint32 loot_id
,Player
* player
) const;
146 LootTemplate
const* GetLootFor(uint32 loot_id
) const;
148 char const* GetName() const { return m_name
; }
149 char const* GetEntryName() const { return m_entryName
; }
150 bool IsRatesAllowed() const { return m_ratesAllowed
; }
152 void LoadLootTable();
155 LootTemplateMap m_LootTemplates
;
157 char const* m_entryName
;
163 class LootGroup
; // A set of loot definitions for items (refs are not allowed inside)
164 typedef std::vector
<LootGroup
> LootGroups
;
167 // Adds an entry to the group (at loading stage)
168 void AddEntry(LootStoreItem
& item
);
169 // Rolls for every item in the template and adds the rolled items the the loot
170 void Process(Loot
& loot
, LootStore
const& store
, bool rate
, uint8 GroupId
= 0) const;
172 // True if template includes at least 1 quest drop entry
173 bool HasQuestDrop(LootTemplateMap
const& store
, uint8 GroupId
= 0) const;
174 // True if template includes at least 1 quest drop for an active quest of the player
175 bool HasQuestDropForPlayer(LootTemplateMap
const& store
, Player
const * player
, uint8 GroupId
= 0) const;
177 // Checks integrity of the template
178 void Verify(LootStore
const& store
, uint32 Id
) const;
179 void CheckLootRefs(LootIdSet
* ref_set
) const;
181 LootStoreItemList Entries
; // not grouped only
182 LootGroups Groups
; // groups have own (optimised) processing, grouped entries go there
185 //=====================================================
187 class LootValidatorRef
: public Reference
<Loot
, LootValidatorRef
>
190 LootValidatorRef() {}
191 void targetObjectDestroyLink() {}
192 void sourceObjectDestroyLink() {}
195 //=====================================================
197 class LootValidatorRefManager
: public RefManager
<Loot
, LootValidatorRef
>
200 typedef LinkedListHead::Iterator
< LootValidatorRef
> iterator
;
202 LootValidatorRef
* getFirst() { return (LootValidatorRef
*)RefManager
<Loot
, LootValidatorRef
>::getFirst(); }
203 LootValidatorRef
* getLast() { return (LootValidatorRef
*)RefManager
<Loot
, LootValidatorRef
>::getLast(); }
205 iterator
begin() { return iterator(getFirst()); }
206 iterator
end() { return iterator(NULL
); }
207 iterator
rbegin() { return iterator(getLast()); }
208 iterator
rend() { return iterator(NULL
); }
211 //=====================================================
214 ByteBuffer
& operator<<(ByteBuffer
& b
, LootItem
const& li
);
215 ByteBuffer
& operator<<(ByteBuffer
& b
, LootView
const& lv
);
219 friend ByteBuffer
& operator<<(ByteBuffer
& b
, LootView
const& lv
);
221 QuestItemMap
const& GetPlayerQuestItems() const { return PlayerQuestItems
; }
222 QuestItemMap
const& GetPlayerFFAItems() const { return PlayerFFAItems
; }
223 QuestItemMap
const& GetPlayerNonQuestNonFFAConditionalItems() const { return PlayerNonQuestNonFFAConditionalItems
; }
228 LootType loot_type
; // required for achievement system
230 Loot(uint32 _gold
= 0) : gold(_gold
), unlootedCount(0), loot_type(LOOT_CORPSE
) {}
233 // if loot becomes invalid this reference is used to inform the listener
234 void addLootValidatorRef(LootValidatorRef
* pLootValidatorRef
)
236 i_LootValidatorRefManager
.insertFirst(pLootValidatorRef
);
242 for (QuestItemMap::const_iterator itr
= PlayerQuestItems
.begin(); itr
!= PlayerQuestItems
.end(); ++itr
)
244 PlayerQuestItems
.clear();
246 for (QuestItemMap::const_iterator itr
= PlayerFFAItems
.begin(); itr
!= PlayerFFAItems
.end(); ++itr
)
248 PlayerFFAItems
.clear();
250 for (QuestItemMap::const_iterator itr
= PlayerNonQuestNonFFAConditionalItems
.begin(); itr
!= PlayerNonQuestNonFFAConditionalItems
.end(); ++itr
)
252 PlayerNonQuestNonFFAConditionalItems
.clear();
254 PlayersLooting
.clear();
259 i_LootValidatorRefManager
.clearReferences();
262 bool empty() const { return items
.empty() && gold
== 0; }
263 bool isLooted() const { return gold
== 0 && unlootedCount
== 0; }
265 void NotifyItemRemoved(uint8 lootIndex
);
266 void NotifyQuestItemRemoved(uint8 questIndex
);
267 void NotifyMoneyRemoved();
268 void AddLooter(uint64 GUID
) { PlayersLooting
.insert(GUID
); }
269 void RemoveLooter(uint64 GUID
) { PlayersLooting
.erase(GUID
); }
271 void generateMoneyLoot(uint32 minAmount
, uint32 maxAmount
);
272 bool FillLoot(uint32 loot_id
, LootStore
const& store
, Player
* loot_owner
, bool personal
, bool noEmptyError
= false);
274 // Inserts the item into the loot (called by LootTemplate processors)
275 void AddItem(LootStoreItem
const & item
);
277 LootItem
* LootItemInSlot(uint32 lootslot
, Player
* player
, QuestItem
** qitem
= NULL
, QuestItem
** ffaitem
= NULL
, QuestItem
** conditem
= NULL
);
278 uint32
GetMaxSlotInLootFor(Player
* player
) const;
281 void FillNotNormalLootFor(Player
* player
);
282 QuestItemList
* FillFFALoot(Player
* player
);
283 QuestItemList
* FillQuestLoot(Player
* player
);
284 QuestItemList
* FillNonQuestNonFFAConditionalLoot(Player
* player
);
286 LootItemList quest_items
;
287 std::set
<uint64
> PlayersLooting
;
288 QuestItemMap PlayerQuestItems
;
289 QuestItemMap PlayerFFAItems
;
290 QuestItemMap PlayerNonQuestNonFFAConditionalItems
;
292 // All rolls are registered here. They need to know, when the loot is not valid anymore
293 LootValidatorRefManager i_LootValidatorRefManager
;
300 PermissionTypes permission
;
301 LootView(Loot
&_loot
, Player
*_viewer
,PermissionTypes _permission
= ALL_PERMISSION
)
302 : loot(_loot
), viewer(_viewer
), permission(_permission
) {}
305 extern LootStore LootTemplates_Creature
;
306 extern LootStore LootTemplates_Fishing
;
307 extern LootStore LootTemplates_Gameobject
;
308 extern LootStore LootTemplates_Item
;
309 extern LootStore LootTemplates_Mail
;
310 extern LootStore LootTemplates_Milling
;
311 extern LootStore LootTemplates_Pickpocketing
;
312 extern LootStore LootTemplates_Skinning
;
313 extern LootStore LootTemplates_Disenchant
;
314 extern LootStore LootTemplates_Prospecting
;
315 extern LootStore LootTemplates_Spell
;
317 void LoadLootTemplates_Creature();
318 void LoadLootTemplates_Fishing();
319 void LoadLootTemplates_Gameobject();
320 void LoadLootTemplates_Item();
321 void LoadLootTemplates_Mail();
322 void LoadLootTemplates_Milling();
323 void LoadLootTemplates_Pickpocketing();
324 void LoadLootTemplates_Skinning();
325 void LoadLootTemplates_Disenchant();
326 void LoadLootTemplates_Prospecting();
328 void LoadLootTemplates_Spell();
329 void LoadLootTemplates_Reference();
331 inline void LoadLootTables()
333 LoadLootTemplates_Creature();
334 LoadLootTemplates_Fishing();
335 LoadLootTemplates_Gameobject();
336 LoadLootTemplates_Item();
337 LoadLootTemplates_Mail();
338 LoadLootTemplates_Milling();
339 LoadLootTemplates_Pickpocketing();
340 LoadLootTemplates_Skinning();
341 LoadLootTemplates_Disenchant();
342 LoadLootTemplates_Prospecting();
343 LoadLootTemplates_Spell();
345 LoadLootTemplates_Reference();