Achievements.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator 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. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Achievements.h"
  17. #include "../../common/Log.h"
  18. #include "../../common/ConfigReader.h"
  19. #include <assert.h>
  20. extern ConfigReader configReader;
  21. extern MasterAchievementList master_achievement_list;
  22. Achievement::Achievement() {
  23. id = 0;
  24. memset(title, 0, sizeof(title));
  25. memset(uncompleted_text, 0, sizeof(uncompleted_text));
  26. memset(completed_text, 0, sizeof(completed_text));
  27. memset(category, 0, sizeof(category));
  28. memset(expansion, 0, sizeof(expansion));
  29. icon = 0;
  30. point_value = 0;
  31. qty_req = 0;
  32. hide = false;
  33. unknown3a = 0;
  34. unknown3b = 0;
  35. }
  36. Achievement::Achievement(Achievement *in) {
  37. vector<struct AchievementRequirements *> *requirements_in;
  38. vector<struct AchievementRewards *> *rewards_in;
  39. vector<struct AchievementRequirements *>::iterator itr;
  40. vector<struct AchievementRewards *>::iterator itr2;
  41. struct AchievementRequirements *achievement_requirement;
  42. struct AchievementRewards *achievement_reward;
  43. assert(in);
  44. id = in->GetID();
  45. strncpy(title, in->GetTitle(), sizeof(title));
  46. strncpy(uncompleted_text, in->GetUncompletedText(), sizeof(uncompleted_text));
  47. strncpy(completed_text, in->GetCompletedText(), sizeof(completed_text));
  48. strncpy(category, in->GetCategory(), sizeof(category));
  49. strncpy(expansion, in->GetExpansion(), sizeof(expansion));
  50. icon = in->GetIcon();
  51. point_value = in->GetPointValue();
  52. qty_req = in->GetQtyReq();
  53. hide = in->GetHide();
  54. unknown3a = in->GetUnknown3a();
  55. unknown3b = in->GetUnknown3b();
  56. requirements_in = in->GetRequirements();
  57. for (itr = requirements_in->begin(); itr != requirements_in->end(); itr++) {
  58. achievement_requirement = new struct AchievementRequirements;
  59. achievement_requirement->achievement_id = (*itr)->achievement_id;
  60. achievement_requirement->name = (*itr)->name;
  61. achievement_requirement->qty_req = (*itr)->qty_req;
  62. requirements.push_back(achievement_requirement);
  63. }
  64. rewards_in = in->GetRewards();
  65. for (itr2 = rewards_in->begin(); itr2 != rewards_in->end(); itr2++) {
  66. achievement_reward = new struct AchievementRewards;
  67. achievement_reward->achievement_id = (*itr2)->achievement_id;
  68. achievement_reward->reward = (*itr2)->reward;
  69. rewards.push_back(achievement_reward);
  70. }
  71. }
  72. Achievement::~Achievement() {
  73. vector<struct AchievementRequirements *>::iterator itr;
  74. vector<struct AchievementRewards *>::iterator itr2;
  75. for (itr = requirements.begin(); itr != requirements.end(); itr++)
  76. safe_delete(*itr);
  77. for (itr2 = rewards.begin(); itr2 != rewards.end(); itr2++)
  78. safe_delete(*itr2);
  79. }
  80. void Achievement::AddAchievementRequirement(struct AchievementRequirements *requirement) {
  81. assert(requirement);
  82. requirements.push_back(requirement);
  83. }
  84. void Achievement::AddAchievementReward(struct AchievementRewards *reward) {
  85. assert(reward);
  86. rewards.push_back(reward);
  87. }
  88. void AchievementUpdate::AddAchievementUpdateItems(struct AchievementUpdateItems *update_item) {
  89. assert(update_item);
  90. update_items.push_back(update_item);
  91. }
  92. MasterAchievementList::MasterAchievementList() {
  93. m_packetsCreated = false;
  94. masterPacket = 0;
  95. mutex_achievements.SetName("MasterAchievementList::achievements");
  96. }
  97. MasterAchievementList::~MasterAchievementList() {
  98. ClearAchievements();
  99. }
  100. bool MasterAchievementList::AddAchievement(Achievement *achievement) {
  101. bool ret = false;
  102. assert(achievement);
  103. mutex_achievements.writelock(__FUNCTION__, __LINE__);
  104. if (achievements.count(achievement->GetID()) == 0) {
  105. achievements[achievement->GetID()] = achievement;
  106. ret = true;
  107. }
  108. mutex_achievements.releasewritelock(__FUNCTION__, __LINE__);
  109. return ret;
  110. }
  111. Achievement * MasterAchievementList::GetAchievement(int32 achievement_id) {
  112. Achievement *achievement = 0;
  113. mutex_achievements.readlock(__FUNCTION__, __LINE__);
  114. if (achievements.count(achievement_id) > 0)
  115. achievement = achievements[achievement_id];
  116. mutex_achievements.releasereadlock(__FUNCTION__, __LINE__);
  117. return achievement;
  118. }
  119. void MasterAchievementList::ClearAchievements() {
  120. map<int32, Achievement *>::iterator itr;
  121. mutex_achievements.writelock(__FUNCTION__, __LINE__);
  122. for (itr = achievements.begin(); itr != achievements.end(); itr++)
  123. safe_delete(itr->second);
  124. achievements.clear();
  125. mutex_achievements.releasewritelock(__FUNCTION__, __LINE__);
  126. }
  127. int32 MasterAchievementList::Size() {
  128. int32 size;
  129. mutex_achievements.readlock(__FUNCTION__, __LINE__);
  130. size = achievements.size();
  131. mutex_achievements.releasereadlock(__FUNCTION__, __LINE__);
  132. return size;
  133. }
  134. PlayerAchievementList::PlayerAchievementList() {
  135. }
  136. PlayerAchievementList::~PlayerAchievementList() {
  137. ClearAchievements();
  138. }
  139. bool PlayerAchievementList::AddAchievement(Achievement *achievement) {
  140. assert(achievement);
  141. if (achievements.count(achievement->GetID()) == 0) {
  142. achievements[achievement->GetID()] = achievement;
  143. return true;
  144. }
  145. return false;
  146. }
  147. Achievement * PlayerAchievementList::GetAchievement(int32 achievement_id) {
  148. if (achievements.count(achievement_id) > 0)
  149. return achievements[achievement_id];
  150. return 0;
  151. }
  152. void PlayerAchievementList::ClearAchievements() {
  153. map<int32, Achievement *>::iterator itr;
  154. for (itr = achievements.begin(); itr != achievements.end(); itr++)
  155. safe_delete(itr->second);
  156. achievements.clear();
  157. }
  158. int32 PlayerAchievementList::Size() {
  159. return achievements.size();
  160. }
  161. AchievementUpdate::AchievementUpdate() {
  162. id = 0;
  163. completed_date = 0;
  164. }
  165. AchievementUpdate::AchievementUpdate(AchievementUpdate *in) {
  166. vector<struct AchievementUpdateItems *> *items_in;
  167. vector<struct AchievementUpdateItems *>::iterator itr;
  168. struct AchievementUpdateItems *items;
  169. assert(in);
  170. id = in->GetID();
  171. completed_date = in->GetCompletedDate();
  172. items_in = in->GetUpdateItems();
  173. for (itr = items_in->begin(); itr != items_in->end(); itr++) {
  174. items = new struct AchievementUpdateItems;
  175. items->achievement_id = (*itr)->achievement_id;
  176. items->item_update = (*itr)->item_update;
  177. update_items.push_back(items);
  178. }
  179. }
  180. AchievementUpdate::~AchievementUpdate() {
  181. vector<struct AchievementUpdateItems *>::iterator itr;
  182. for (itr = update_items.begin(); itr != update_items.end(); itr++)
  183. safe_delete(*itr);
  184. }
  185. PlayerAchievementUpdateList::PlayerAchievementUpdateList() {
  186. }
  187. PlayerAchievementUpdateList::~PlayerAchievementUpdateList() {
  188. ClearAchievementUpdates();
  189. }
  190. bool PlayerAchievementUpdateList::AddAchievementUpdate(AchievementUpdate *update) {
  191. assert(update);
  192. if (achievement_updates.count(update->GetID()) == 0) {
  193. achievement_updates[update->GetID()] = update;
  194. return true;
  195. }
  196. return false;
  197. }
  198. void PlayerAchievementUpdateList::ClearAchievementUpdates() {
  199. map<int32, AchievementUpdate *>::iterator itr;
  200. for (itr = achievement_updates.begin(); itr != achievement_updates.end(); itr++)
  201. safe_delete(itr->second);
  202. achievement_updates.clear();
  203. }
  204. int32 PlayerAchievementUpdateList::Size() {
  205. return achievement_updates.size();
  206. }
  207. void MasterAchievementList::CreateMasterAchievementListPacket() {
  208. map<int32, Achievement *>::iterator itr;
  209. Achievement *achievement;
  210. vector<AchievementRequirements *> *requirements = 0;
  211. vector<AchievementRequirements *>::iterator itr2;
  212. AchievementRequirements *requirement;
  213. vector<AchievementRewards *> *rewards = 0;
  214. vector<AchievementRewards *>::iterator itr3;
  215. AchievementRewards *reward;
  216. PacketStruct *packet;
  217. int16 i = 0;
  218. int16 j = 0;
  219. int16 k = 0;
  220. int16 version = 1096;
  221. if (!(packet = configReader.getStruct("WS_CharacterAchievements", version))) {
  222. return;
  223. }
  224. packet->setArrayLengthByName("num_achievements" , achievements.size());
  225. for (itr = achievements.begin(); itr != achievements.end(); itr++) {
  226. achievement = itr->second;
  227. packet->setArrayDataByName("achievement_id", achievement->GetID(), i);
  228. packet->setArrayDataByName("title", achievement->GetTitle(), i);
  229. packet->setArrayDataByName("uncompleted_text", achievement->GetUncompletedText(), i);
  230. packet->setArrayDataByName("completed_text", achievement->GetCompletedText(), i);
  231. packet->setArrayDataByName("category", achievement->GetCategory(), i);
  232. packet->setArrayDataByName("expansion", achievement->GetExpansion(), i);
  233. packet->setArrayDataByName("icon", achievement->GetIcon(), i);
  234. packet->setArrayDataByName("point_value", achievement->GetPointValue(), i);
  235. packet->setArrayDataByName("qty_req", achievement->GetQtyReq(), i);
  236. packet->setArrayDataByName("hide_achievement", achievement->GetHide(), i);
  237. packet->setArrayDataByName("unknown3", achievement->GetUnknown3a(), i);
  238. packet->setArrayDataByName("unknown3", achievement->GetUnknown3b(), i);
  239. requirements = achievement->GetRequirements();
  240. rewards = achievement->GetRewards();
  241. j = 0;
  242. k = 0;
  243. packet->setSubArrayLengthByName("num_items", requirements->size(), i, j);
  244. for (itr2 = requirements->begin(); itr2 != requirements->end(); itr2++) {
  245. requirement = *itr2;
  246. packet->setSubArrayDataByName("item_name", requirement->name.c_str(), i, j);
  247. packet->setSubArrayDataByName("item_qty_req", requirement->qty_req, i, j);
  248. j++;
  249. }
  250. packet->setSubArrayLengthByName("num_rewards", achievement->GetRewards()->size(), i, k);
  251. for (itr3 = rewards->begin(); itr3 != rewards->end(); itr3++) {
  252. reward = *itr3;
  253. packet->setSubArrayDataByName("reward_item", reward->reward.c_str(), i, k);
  254. k++;
  255. }
  256. i++;
  257. }
  258. //packet->PrintPacket();
  259. EQ2Packet* data = packet->serialize();
  260. masterPacket = new EQ2Packet(OP_ClientCmdMsg, data->pBuffer, data->size);
  261. safe_delete(packet);
  262. safe_delete(data);
  263. //DumpPacket(app);
  264. m_packetsCreated = true;
  265. }