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
22 #include "Policies/Singleton.h"
23 #include "Database/DatabaseEnv.h"
25 #include "DBCStores.h"
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
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
60 struct AchievementCriteriaData
62 AchievementCriteriaDataType dataType
;
65 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_CREATURE
70 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_CLASS_RACE
76 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_LESS_HEALTH
81 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_DEAD
86 // ACHIEVEMENT_CRITERIA_DATA_TYPE_T_AURA
87 // ACHIEVEMENT_CRITERIA_DATA_TYPE_S_AURA
93 // ACHIEVEMENT_CRITERIA_DATA_TYPE_S_AREA
106 AchievementCriteriaData() : dataType(ACHIEVEMENT_CRITERIA_DATA_TYPE_NONE
)
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;
132 typedef std::map
<uint32
,AchievementCriteriaDataSet
> AchievementCriteriaDataMap
;
134 struct AchievementReward
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
160 typedef UNORDERED_MAP
<uint32
, CriteriaProgress
> CriteriaProgressMap
;
161 typedef UNORDERED_MAP
<uint32
, CompletedAchievementData
> CompletedAchievementMap
;
170 AchievementMgr(Player
* pl
);
174 static void DeleteFromDB(uint32 lowguid
);
175 void LoadFromDB(QueryResult
*achievementResult
, QueryResult
*criteriaResult
);
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
;}
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
);
197 CriteriaProgressMap m_criteriaProgress
;
198 CompletedAchievementMap m_completedAchievements
;
201 class AchievementGlobalMgr
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();
250 void LoadRewardLocales();
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()