[7918] Improve portability in work with uint64 string format specifiers and in code...
[getmangos.git] / src / game / AchievementMgr.h
blob0749beb16250b1f8e3403de98fd4219ef2881b5b
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
18 #ifndef __MANGOS_ACHIEVEMENTMGR_H
19 #define __MANGOS_ACHIEVEMENTMGR_H
21 #include "Common.h"
22 #include "Policies/Singleton.h"
23 #include "Database/DatabaseEnv.h"
24 #include "DBCEnums.h"
25 #include "DBCStores.h"
27 #include <map>
28 #include <string>
30 typedef std::list<AchievementCriteriaEntry const*> AchievementCriteriaEntryList;
31 typedef std::list<AchievementEntry const*> AchievementEntryList;
33 typedef std::map<uint32,AchievementCriteriaEntryList> AchievementCriteriaListByAchievement;
34 typedef std::map<uint32,AchievementEntryList> AchievementListByReferencedId;
36 struct CriteriaProgress
38 uint32 counter;
39 time_t date;
40 bool changed;
43 enum AchievementCriteriaDataType
44 { // value1 value2 comment
45 ACHIEVEMENT_CRITERIA_DATA_TYPE_NONE = 0, // 0 0
46 ACHIEVEMENT_CRITERIA_DATA_TYPE_T_CREATURE = 1, // creature_id 0
47 ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_CLASS_RACE = 2, // class_id race_id
48 ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_LESS_HEALTH= 3, // health_percent 0
49 ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_DEAD = 4, // own_team 0 not corpse (not released body), own_team==false if enemy team expected
50 ACHIEVEMENT_CRITERIA_DATA_TYPE_S_AURA = 5, // spell_id effect_idx
51 ACHIEVEMENT_CRITERIA_DATA_TYPE_S_AREA = 6, // area id 0
52 ACHIEVEMENT_CRITERIA_DATA_TYPE_T_AURA = 7, // spell_id effect_idx
55 #define MAX_ACHIEVEMENT_CRITERIA_DATA_TYPE 8 // maximum value in AchievementCriteriaDataType enum
57 class Player;
58 class Unit;
60 struct AchievementCriteriaData
62 AchievementCriteriaDataType dataType;
63 union
65 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_CREATURE
66 struct
68 uint32 id;
69 } creature;
70 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_CLASS_RACE
71 struct
73 uint32 class_id;
74 uint32 race_id;
75 } classRace;
76 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_LESS_HEALTH
77 struct
79 uint32 percent;
80 } health;
81 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_DEAD
82 struct
84 uint32 own_team_flag;
85 } player_dead;
86 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_AURA
87 // ACHIEVEMENT_CRITERIA_DATA_TYPE_S_AURA
88 struct
90 uint32 spell_id;
91 uint32 effect_idx;
92 } aura;
93 // ACHIEVEMENT_CRITERIA_DATA_TYPE_S_AREA
94 struct
96 uint32 id;
97 } area;
98 // ...
99 struct
101 uint32 value1;
102 uint32 value2;
103 } raw;
106 AchievementCriteriaData() : dataType(ACHIEVEMENT_CRITERIA_DATA_TYPE_NONE)
108 raw.value1 = 0;
109 raw.value2 = 0;
112 AchievementCriteriaData(uint32 _dataType, uint32 _value1, uint32 _value2) : dataType(AchievementCriteriaDataType(_dataType))
114 raw.value1 = _value1;
115 raw.value2 = _value2;
118 bool IsValid(AchievementCriteriaEntry const* criteria);
119 bool Meets(Player const* source, Unit const* target) const;
122 struct AchievementCriteriaDataSet
124 typedef std::vector<AchievementCriteriaData> Storage;
125 void Add(AchievementCriteriaData const& data) { storage.push_back(data); }
126 bool Meets(Player const* source, Unit const* target) const;
127 private:
128 Storage storage;
132 typedef std::map<uint32,AchievementCriteriaDataSet> AchievementCriteriaDataMap;
134 struct AchievementReward
136 uint32 titleId[2];
137 uint32 itemId;
138 uint32 sender;
139 std::string subject;
140 std::string text;
143 typedef std::map<uint32,AchievementReward> AchievementRewards;
145 struct AchievementRewardLocale
147 std::vector<std::string> subject;
148 std::vector<std::string> text;
151 typedef std::map<uint32,AchievementRewardLocale> AchievementRewardLocales;
154 struct CompletedAchievementData
156 time_t date;
157 bool changed;
160 typedef UNORDERED_MAP<uint32, CriteriaProgress> CriteriaProgressMap;
161 typedef UNORDERED_MAP<uint32, CompletedAchievementData> CompletedAchievementMap;
163 class Unit;
164 class Player;
165 class WorldPacket;
167 class AchievementMgr
169 public:
170 AchievementMgr(Player* pl);
171 ~AchievementMgr();
173 void Reset();
174 static void DeleteFromDB(uint32 lowguid);
175 void LoadFromDB(QueryResult *achievementResult, QueryResult *criteriaResult);
176 void SaveToDB();
177 void ResetAchievementCriteria(AchievementCriteriaTypes type, uint32 miscvalue1=0, uint32 miscvalue2=0);
178 void UpdateAchievementCriteria(AchievementCriteriaTypes type, uint32 miscvalue1=0, uint32 miscvalue2=0, Unit *unit=NULL, uint32 time=0);
179 void CheckAllAchievementCriteria();
180 void SendAllAchievementData();
181 void SendRespondInspectAchievements(Player* player);
182 Player* GetPlayer() { return m_player;}
184 private:
185 enum ProgressType { PROGRESS_SET, PROGRESS_ACCUMULATE, PROGRESS_HIGHEST };
186 void SendAchievementEarned(AchievementEntry const* achievement);
187 void SendCriteriaUpdate(uint32 id, CriteriaProgress const* progress);
188 void SetCriteriaProgress(AchievementCriteriaEntry const* entry, uint32 changeValue, ProgressType ptype = PROGRESS_SET);
189 void CompletedCriteriaFor(AchievementEntry const* achievement);
190 void CompletedAchievement(AchievementEntry const* entry);
191 bool IsCompletedCriteria(AchievementCriteriaEntry const* criteria, AchievementEntry const* achievement);
192 bool IsCompletedAchievement(AchievementEntry const* entry);
193 void CompleteAchievementsWithRefs(AchievementEntry const* entry);
194 void BuildAllDataPacket(WorldPacket *data);
196 Player* m_player;
197 CriteriaProgressMap m_criteriaProgress;
198 CompletedAchievementMap m_completedAchievements;
201 class AchievementGlobalMgr
203 public:
204 AchievementCriteriaEntryList const& GetAchievementCriteriaByType(AchievementCriteriaTypes type);
205 AchievementCriteriaEntryList const* GetAchievementCriteriaByAchievement(uint32 id)
207 AchievementCriteriaListByAchievement::const_iterator itr = m_AchievementCriteriaListByAchievement.find(id);
208 return itr != m_AchievementCriteriaListByAchievement.end() ? &itr->second : NULL;
211 AchievementEntryList const* GetAchievementByReferencedId(uint32 id) const
213 AchievementListByReferencedId::const_iterator itr = m_AchievementListByReferencedId.find(id);
214 return itr != m_AchievementListByReferencedId.end() ? &itr->second : NULL;
217 AchievementReward const* GetAchievementReward(AchievementEntry const* achievement) const
219 AchievementRewards::const_iterator iter = m_achievementRewards.find(achievement->ID);
220 return iter!=m_achievementRewards.end() ? &iter->second : NULL;
223 AchievementRewardLocale const* GetAchievementRewardLocale(AchievementEntry const* achievement) const
225 AchievementRewardLocales::const_iterator iter = m_achievementRewardLocales.find(achievement->ID);
226 return iter!=m_achievementRewardLocales.end() ? &iter->second : NULL;
229 AchievementCriteriaDataSet const* GetCriteriaDataSet(AchievementCriteriaEntry const *achievementCriteria)
231 AchievementCriteriaDataMap::const_iterator iter = m_criteriaDataMap.find(achievementCriteria->ID);
232 return iter!=m_criteriaDataMap.end() ? &iter->second : NULL;
235 bool IsRealmCompleted(AchievementEntry const* achievement) const
237 return m_allCompletedAchievements.find(achievement->ID) != m_allCompletedAchievements.end();
240 void SetRealmCompleted(AchievementEntry const* achievement)
242 m_allCompletedAchievements.insert(achievement->ID);
245 void LoadAchievementCriteriaList();
246 void LoadAchievementCriteriaData();
247 void LoadAchievementReferenceList();
248 void LoadCompletedAchievements();
249 void LoadRewards();
250 void LoadRewardLocales();
251 private:
252 AchievementCriteriaDataMap m_criteriaDataMap;
254 // store achievement criterias by type to speed up lookup
255 AchievementCriteriaEntryList m_AchievementCriteriasByType[ACHIEVEMENT_CRITERIA_TYPE_TOTAL];
256 // store achievement criterias by achievement to speed up lookup
257 AchievementCriteriaListByAchievement m_AchievementCriteriaListByAchievement;
258 // store achievements by referenced achievement id to speed up lookup
259 AchievementListByReferencedId m_AchievementListByReferencedId;
261 typedef std::set<uint32> AllCompletedAchievements;
262 AllCompletedAchievements m_allCompletedAchievements;
264 AchievementRewards m_achievementRewards;
265 AchievementRewardLocales m_achievementRewardLocales;
268 #define achievementmgr MaNGOS::Singleton<AchievementGlobalMgr>::Instance()
270 #endif