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"
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
55 MASTER_PERMISSION
= 2,
62 LOOT_PICKPOCKETING
= 2,
64 LOOT_DISENCHANTING
= 4,
65 // ignored always by client
70 LOOT_FISHINGHOLE
= 20, // unsupported by client, sending LOOT_FISHING instead
71 LOOT_INSIGNIA
= 21 // unsupported by client, sending LOOT_CORPSE instead
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)
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
103 int32 randomPropertyId
;
104 uint16 conditionId
:16; // allow compiler pack structure
108 bool freeforall
: 1; // free for all
109 bool is_underthreshold
: 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;
123 uint8 index
; // position in quest_items;
127 : index(0), is_looted(false) {}
129 QuestItem(uint8 _index
, bool _islooted
= false)
130 : index(_index
), is_looted(_islooted
) {}
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
;
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(); }
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
; }
167 void LoadLootTable();
170 LootTemplateMap m_LootTemplates
;
172 char const* m_entryName
;
178 class LootGroup
; // A set of loot definitions for items (refs are not allowed inside)
179 typedef std::vector
<LootGroup
> LootGroups
;
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;
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
>
205 LootValidatorRef() {}
206 void targetObjectDestroyLink() {}
207 void sourceObjectDestroyLink() {}
210 //=====================================================
212 class LootValidatorRefManager
: public RefManager
<Loot
, LootValidatorRef
>
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 //=====================================================
229 ByteBuffer
& operator<<(ByteBuffer
& b
, LootItem
const& li
);
230 ByteBuffer
& operator<<(ByteBuffer
& b
, LootView
const& lv
);
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
;
243 LootType loot_type
; // required for achievement system
245 Loot(uint32 _gold
= 0) : gold(_gold
), unlootedCount(0), loot_type(LOOT_CORPSE
) {}
248 // if loot becomes invalid this reference is used to inform the listener
249 void addLootValidatorRef(LootValidatorRef
* pLootValidatorRef
)
251 i_LootValidatorRefManager
.insertFirst(pLootValidatorRef
);
257 for (QuestItemMap::const_iterator itr
= PlayerQuestItems
.begin(); itr
!= PlayerQuestItems
.end(); ++itr
)
259 PlayerQuestItems
.clear();
261 for (QuestItemMap::const_iterator itr
= PlayerFFAItems
.begin(); itr
!= PlayerFFAItems
.end(); ++itr
)
263 PlayerFFAItems
.clear();
265 for (QuestItemMap::const_iterator itr
= PlayerNonQuestNonFFAConditionalItems
.begin(); itr
!= PlayerNonQuestNonFFAConditionalItems
.end(); ++itr
)
267 PlayerNonQuestNonFFAConditionalItems
.clear();
269 PlayersLooting
.clear();
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;
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
;
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();