root/trunk/src/game/LootMgr.h @ 39

Revision 2, 12.3 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_LOOTMGR_H
20#define MANGOS_LOOTMGR_H
21
22#include "ItemEnchantmentMgr.h"
23#include "ByteBuffer.h"
24#include "Utilities/LinkedReference/RefManager.h"
25
26#include <map>
27#include <vector>
28
29enum RollType
30{
31    ROLL_PASS         = 0,
32    ROLL_NEED         = 1,
33    ROLL_GREED        = 2
34};
35
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
40
41enum LootMethod
42{
43    FREE_FOR_ALL      = 0,
44    ROUND_ROBIN       = 1,
45    MASTER_LOOT       = 2,
46    GROUP_LOOT        = 3,
47    NEED_BEFORE_GREED = 4
48};
49
50enum PermissionTypes
51{
52    ALL_PERMISSION    = 0,
53    GROUP_PERMISSION  = 1,
54    MASTER_PERMISSION = 2,
55    NONE_PERMISSION   = 3
56};
57
58class Player;
59class LootStore;
60
61struct LootStoreItem
62{
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)
70
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) {}
77
78    bool Roll() 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
81};
82
83struct LootItem
84{
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
96
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);
100
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;
103};
104
105struct QuestItem
106{
107    uint8   index;                                          // position in quest_items;
108    bool    is_looted;
109
110    QuestItem()
111        : index(0), is_looted(false) {}
112
113    QuestItem(uint8 _index, bool _islooted = false)
114        : index(_index), is_looted(_islooted) {}
115};
116
117struct Loot;
118class LootTemplate;
119
120typedef std::vector<QuestItem> QuestItemList;
121typedef std::map<uint32, QuestItemList *> QuestItemMap;
122typedef std::vector<LootStoreItem> LootStoreItemList;
123typedef HM_NAMESPACE::hash_map<uint32, LootTemplate*> LootTemplateMap;
124
125typedef std::set<uint32> LootIdSet;
126
127class LootStore
128{
129    public:
130        explicit LootStore(char const* name, char const* entryName) : m_name(name), m_entryName(entryName) {}
131        virtual ~LootStore() { Clear(); }
132
133        void Verify() const;
134
135        void LoadAndCollectLootIds(LootIdSet& ids_set);
136        void CheckLootRefs(LootIdSet* ref_set = NULL) const;// check existence reference and remove it from ref_set
137        void ReportUnusedIds(LootIdSet const& ids_set) const;
138        void ReportNotExistedId(uint32 id) const;
139
140        bool HaveLootFor(uint32 loot_id) const { return m_LootTemplates.find(loot_id) != m_LootTemplates.end(); }
141        bool HaveQuestLootFor(uint32 loot_id) const;
142        bool HaveQuestLootForPlayer(uint32 loot_id,Player* player) const;
143
144        LootTemplate const* GetLootFor(uint32 loot_id) const;
145
146        char const* GetName() const { return m_name; }
147        char const* GetEntryName() const { return m_entryName; }
148    protected:
149        void LoadLootTable();
150        void Clear();
151    private:
152        LootTemplateMap m_LootTemplates;
153        char const* m_name;
154        char const* m_entryName;
155};
156
157class LootTemplate
158{
159    class  LootGroup;                                       // A set of loot definitions for items (refs are not allowed inside)
160    typedef std::vector<LootGroup> LootGroups;
161
162    public:
163        // Adds an entry to the group (at loading stage)
164        void AddEntry(LootStoreItem& item);
165        // Rolls for every item in the template and adds the rolled items the the loot
166        void Process(Loot& loot, LootStore const& store, uint8 GroupId = 0) const;
167
168        // True if template includes at least 1 quest drop entry
169        bool HasQuestDrop(LootTemplateMap const& store, uint8 GroupId = 0) const;
170        // True if template includes at least 1 quest drop for an active quest of the player
171        bool HasQuestDropForPlayer(LootTemplateMap const& store, Player const * player, uint8 GroupId = 0) const;
172
173        // Checks integrity of the template
174        void Verify(LootStore const& store, uint32 Id) const;
175        void CheckLootRefs(LootTemplateMap const& store, LootIdSet* ref_set) const;
176    private:
177        LootStoreItemList Entries;                          // not grouped only
178        LootGroups        Groups;                           // groups have own (optimised) processing, grouped entries go there
179};
180
181//=====================================================
182
183class LootValidatorRef :  public Reference<Loot, LootValidatorRef>
184{
185    public:
186        LootValidatorRef() {}
187        void targetObjectDestroyLink() {}
188        void sourceObjectDestroyLink() {}
189};
190
191//=====================================================
192
193class LootValidatorRefManager : public RefManager<Loot, LootValidatorRef>
194{
195    public:
196        typedef LinkedListHead::Iterator< LootValidatorRef > iterator;
197
198        LootValidatorRef* getFirst() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getFirst(); }
199        LootValidatorRef* getLast() { return (LootValidatorRef*)RefManager<Loot, LootValidatorRef>::getLast(); }
200
201        iterator begin() { return iterator(getFirst()); }
202        iterator end() { return iterator(NULL); }
203        iterator rbegin() { return iterator(getLast()); }
204        iterator rend() { return iterator(NULL); }
205};
206
207//=====================================================
208
209struct Loot
210{
211    QuestItemMap const& GetPlayerQuestItems() const { return PlayerQuestItems; }
212    QuestItemMap const& GetPlayerFFAItems() const { return PlayerFFAItems; }
213    QuestItemMap const& GetPlayerNonQuestNonFFAConditionalItems() const { return PlayerNonQuestNonFFAConditionalItems; }
214
215    QuestItemList* FillFFALoot(Player* player);
216    QuestItemList* FillQuestLoot(Player* player);
217    QuestItemList* FillNonQuestNonFFAConditionalLoot(Player* player);
218
219    std::vector<LootItem> items;
220    std::vector<LootItem> quest_items;
221    uint32 gold;
222    uint8 unlootedCount;
223
224    Loot(uint32 _gold = 0) : gold(_gold), unlootedCount(0) {}
225    ~Loot() { clear(); }
226
227    // if loot becomes invalid this reference is used to inform the listener
228    void addLootValidatorRef(LootValidatorRef* pLootValidatorRef)
229    {
230        i_LootValidatorRefManager.insertFirst(pLootValidatorRef);
231    }
232
233    // void clear();
234    void clear()
235    {
236        items.clear(); gold = 0; PlayersLooting.clear();
237        for (QuestItemMap::iterator itr = PlayerQuestItems.begin(); itr != PlayerQuestItems.end(); ++itr)
238            delete itr->second;
239        for (QuestItemMap::iterator itr = PlayerFFAItems.begin(); itr != PlayerFFAItems.end(); ++itr)
240            delete itr->second;
241        for (QuestItemMap::iterator itr = PlayerNonQuestNonFFAConditionalItems.begin(); itr != PlayerNonQuestNonFFAConditionalItems.end(); ++itr)
242            delete itr->second;
243
244        PlayerQuestItems.clear();
245        PlayerFFAItems.clear();
246        PlayerNonQuestNonFFAConditionalItems.clear();
247
248        items.clear();
249        quest_items.clear();
250        gold = 0;
251        unlootedCount = 0;
252        i_LootValidatorRefManager.clearReferences();
253    }
254
255    bool empty() const { return items.empty() && gold == 0; }
256    bool isLooted() const { return gold == 0 && unlootedCount == 0; }
257
258    void NotifyItemRemoved(uint8 lootIndex);
259    void NotifyQuestItemRemoved(uint8 questIndex);
260    void NotifyMoneyRemoved();
261    void AddLooter(uint64 GUID) { PlayersLooting.insert(GUID); }
262    void RemoveLooter(uint64 GUID) { PlayersLooting.erase(GUID); }
263
264    void generateMoneyLoot(uint32 minAmount, uint32 maxAmount);
265    void FillLoot(uint32 loot_id, LootStore const& store, Player* loot_owner);
266
267    // Inserts the item into the loot (called by LootTemplate processors)
268    void AddItem(LootStoreItem const & item);
269
270    LootItem* LootItemInSlot(uint32 lootslot, Player* player, QuestItem** qitem = NULL, QuestItem** ffaitem = NULL, QuestItem** conditem = NULL);
271    private:
272        std::set<uint64> PlayersLooting;
273        QuestItemMap PlayerQuestItems;
274        QuestItemMap PlayerFFAItems;
275        QuestItemMap PlayerNonQuestNonFFAConditionalItems;
276
277        // All rolls are registered here. They need to know, when the loot is not valid anymore
278        LootValidatorRefManager i_LootValidatorRefManager;
279
280};
281
282struct LootView
283{
284    Loot &loot;
285    QuestItemList *qlist;
286    QuestItemList *ffalist;
287    QuestItemList *conditionallist;
288    Player *viewer;
289    PermissionTypes permission;
290    LootView(Loot &_loot, QuestItemList *_qlist, QuestItemList *_ffalist, QuestItemList *_conditionallist, Player *_viewer,PermissionTypes _permission = ALL_PERMISSION)
291        : loot(_loot), qlist(_qlist), ffalist(_ffalist), conditionallist(_conditionallist), viewer(_viewer), permission(_permission) {}
292};
293
294extern LootStore LootTemplates_Creature;
295extern LootStore LootTemplates_Fishing;
296extern LootStore LootTemplates_Gameobject;
297extern LootStore LootTemplates_Item;
298extern LootStore LootTemplates_Pickpocketing;
299extern LootStore LootTemplates_Skinning;
300extern LootStore LootTemplates_Disenchant;
301extern LootStore LootTemplates_Prospecting;
302extern LootStore LootTemplates_QuestMail;
303
304void LoadLootTemplates_Creature();
305void LoadLootTemplates_Fishing();
306void LoadLootTemplates_Gameobject();
307void LoadLootTemplates_Item();
308void LoadLootTemplates_Pickpocketing();
309void LoadLootTemplates_Skinning();
310void LoadLootTemplates_Disenchant();
311void LoadLootTemplates_Prospecting();
312void LoadLootTemplates_QuestMail();
313void LoadLootTemplates_Reference();
314
315inline void LoadLootTables()
316{
317    LoadLootTemplates_Creature();
318    LoadLootTemplates_Fishing();
319    LoadLootTemplates_Gameobject();
320    LoadLootTemplates_Item();
321    LoadLootTemplates_Pickpocketing();
322    LoadLootTemplates_Skinning();
323    LoadLootTemplates_Disenchant();
324    LoadLootTemplates_Prospecting();
325    LoadLootTemplates_QuestMail();
326    LoadLootTemplates_Reference();
327}
328
329ByteBuffer& operator<<(ByteBuffer& b, LootItem const& li);
330ByteBuffer& operator<<(ByteBuffer& b, LootView const& lv);
331#endif
Note: See TracBrowser for help on using the browser.