Skills.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "Skills.h"
  17. #include "Spawn.h"
  18. #include "../common/Log.h"
  19. extern ConfigReader configReader;
  20. MasterSkillList::MasterSkillList(){
  21. }
  22. MasterSkillList::~MasterSkillList(){
  23. map<int32, Skill*>::iterator itr;
  24. for(itr = skills.begin(); itr != skills.end(); itr++){
  25. safe_delete(itr->second);
  26. }
  27. map<int16, EQ2Packet*>::iterator itr2;
  28. for(itr2 = populate_packets.begin(); itr2 != populate_packets.end(); itr2++){
  29. safe_delete(itr2->second);
  30. }
  31. }
  32. Skill::Skill(){
  33. skill_id = 0;
  34. current_val = 0;
  35. previous_val = 0;
  36. max_val = 0;
  37. skill_type = 0;
  38. display = 0;
  39. save_needed = false;
  40. }
  41. Skill::Skill(Skill* skill){
  42. skill_id = skill->skill_id;
  43. current_val = skill->current_val;
  44. previous_val = skill->current_val;
  45. max_val = skill->max_val;
  46. skill_type = skill->skill_type;
  47. display = skill->display;
  48. short_name = skill->short_name;
  49. name = skill->name;
  50. description = skill->description;
  51. save_needed = false;
  52. }
  53. map<int32, Skill*>* MasterSkillList::GetAllSkills(){
  54. return &skills;
  55. }
  56. Skill* MasterSkillList::GetSkill(int32 skill_id){
  57. if(skills.count(skill_id) > 0)
  58. return skills[skill_id];
  59. else
  60. return 0;
  61. }
  62. Skill* MasterSkillList::GetSkillByName(const char* skill_name) {
  63. Skill* skill = 0;
  64. map<int32, Skill*>::iterator itr;
  65. for (itr = skills.begin(); itr != skills.end(); itr++) {
  66. Skill* current_skill = itr->second;
  67. if (::ToLower(string(current_skill->name.data.c_str())) == ::ToLower(string(skill_name))) {
  68. skill = current_skill;
  69. break;
  70. }
  71. }
  72. return skill;
  73. }
  74. int16 MasterSkillList::GetSkillCount(){
  75. return skills.size();
  76. }
  77. void MasterSkillList::AddSkill(Skill* skill){
  78. if(skill)
  79. skills[skill->skill_id] = skill;
  80. }
  81. EQ2Packet* MasterSkillList::GetPopulateSkillsPacket(int16 version){
  82. EQ2Packet* ret = 0;
  83. int16 packet_version = configReader.GetStructVersion("WS_SkillMap", version);
  84. if(populate_packets.count(packet_version) > 0)
  85. ret = populate_packets[packet_version];
  86. else{
  87. PacketStruct* packet = configReader.getStruct("WS_SkillMap", packet_version);
  88. int32 count = skills.size();
  89. Skill* skill = 0;
  90. int32 i = 0;
  91. packet->setArrayLengthByName("skill_count", count);
  92. map<int32, Skill*>::iterator itr;
  93. for(itr = skills.begin(); itr != skills.end(); itr++, i++){
  94. skill = itr->second;
  95. packet->setArrayDataByName("skill_id", skill->skill_id, i);
  96. packet->setArrayDataByName("short_name", &skill->short_name, i);
  97. packet->setArrayDataByName("name", &skill->name, i);
  98. packet->setArrayDataByName("description", &skill->description, i);
  99. }
  100. populate_packets[packet_version] = packet->serialize();
  101. safe_delete(packet);
  102. ret = populate_packets[packet_version];
  103. }
  104. if(ret)
  105. return ret->Copy(); //need to return a copy as the packet is deleted after the client confirms it and we want to keep the packet to prevent constant generation of the same data
  106. else
  107. return ret;
  108. }
  109. PlayerSkillList::PlayerSkillList(){
  110. xor_packet = 0;
  111. orig_packet = 0;
  112. packet_count = 0;
  113. has_updates = false;
  114. MSkillUpdates.SetName("PlayerSkillList::MSkillUpdates");
  115. }
  116. PlayerSkillList::~PlayerSkillList(){
  117. map<int32, Skill*>::iterator itr;
  118. for(itr = skills.begin(); itr != skills.end(); itr++){
  119. safe_delete(itr->second);
  120. }
  121. MutexMap<int32, SkillBonus*>::iterator sb_itr = skill_bonus_list.begin();
  122. while (sb_itr.Next())
  123. RemoveSkillBonus(sb_itr.first);
  124. safe_delete_array(xor_packet);
  125. safe_delete_array(orig_packet);
  126. }
  127. void PlayerSkillList::AddSkill(Skill* new_skill){
  128. skills[new_skill->skill_id] = new_skill;
  129. }
  130. void PlayerSkillList::RemoveSkill(Skill* skill) {
  131. if (skill)
  132. skills.erase(skill->skill_id);
  133. }
  134. map<int32, Skill*>* PlayerSkillList::GetAllSkills(){
  135. return &skills;
  136. }
  137. void PlayerSkillList::SetSkillCapsByType(int8 type, int16 value){
  138. map<int32, Skill*>::iterator itr;
  139. for(itr = skills.begin(); itr != skills.end(); itr++){
  140. if(itr->second && itr->second->skill_type == type)
  141. SetSkillCap(itr->second, value);
  142. }
  143. }
  144. void PlayerSkillList::IncreaseSkillCapsByType(int8 type, int16 value){
  145. map<int32, Skill*>::iterator itr;
  146. for(itr = skills.begin(); itr != skills.end(); itr++){
  147. if(itr->second && itr->second->skill_type == type)
  148. IncreaseSkillCap(itr->second, value);
  149. }
  150. }
  151. void PlayerSkillList::IncreaseAllSkillCaps(int16 value){
  152. map<int32, Skill*>::iterator itr;
  153. for(itr = skills.begin(); itr != skills.end(); itr++){
  154. IncreaseSkillCap(itr->second, value);
  155. }
  156. }
  157. bool PlayerSkillList::HasSkill(int32 skill_id){
  158. return (skills.count(skill_id) > 0);
  159. }
  160. Skill* PlayerSkillList::GetSkill(int32 skill_id){
  161. if(skills.count(skill_id) > 0)
  162. return skills[skill_id];
  163. else
  164. return 0;
  165. }
  166. void PlayerSkillList::IncreaseSkill(Skill* skill, int16 amount){
  167. if(skill){
  168. AddSkillUpdateNeeded(skill);
  169. skill->previous_val = skill->current_val;
  170. skill->current_val += amount;
  171. if(skill->current_val > skill->max_val)
  172. skill->max_val = skill->current_val;
  173. skill->save_needed = true;
  174. }
  175. }
  176. void PlayerSkillList::IncreaseSkill(int32 skill_id, int16 amount){
  177. IncreaseSkill(GetSkill(skill_id), amount);
  178. }
  179. void PlayerSkillList::DecreaseSkill(Skill* skill, int16 amount){
  180. if(skill){
  181. skill->previous_val = skill->current_val;
  182. if((skill->current_val - amount) < 0)
  183. skill->current_val = 0;
  184. else
  185. skill->current_val -= amount;
  186. skill->save_needed = true;
  187. AddSkillUpdateNeeded(skill);
  188. }
  189. }
  190. void PlayerSkillList::DecreaseSkill(int32 skill_id, int16 amount){
  191. DecreaseSkill(GetSkill(skill_id), amount);
  192. }
  193. void PlayerSkillList::SetSkill(Skill* skill, int16 value){
  194. if(skill){
  195. skill->previous_val = skill->current_val;
  196. skill->current_val = value;
  197. if(skill->current_val > skill->max_val)
  198. skill->max_val = skill->current_val;
  199. skill->save_needed = true;
  200. AddSkillUpdateNeeded(skill);
  201. }
  202. }
  203. void PlayerSkillList::SetSkill(int32 skill_id, int16 value){
  204. SetSkill(GetSkill(skill_id), value);
  205. }
  206. void PlayerSkillList::IncreaseSkillCap(Skill* skill, int16 amount){
  207. if(skill){
  208. skill->max_val += amount;
  209. skill->save_needed = true;
  210. }
  211. }
  212. void PlayerSkillList::IncreaseSkillCap(int32 skill_id, int16 amount){
  213. IncreaseSkillCap(GetSkill(skill_id), amount);
  214. }
  215. void PlayerSkillList::DecreaseSkillCap(Skill* skill, int16 amount){
  216. if(skill){
  217. if((skill->max_val - amount) < 0)
  218. skill->max_val = 0;
  219. else
  220. skill->max_val -= amount;
  221. if(skill->current_val > skill->max_val){
  222. skill->previous_val = skill->current_val;
  223. skill->current_val = skill->max_val;
  224. AddSkillUpdateNeeded(skill);
  225. }
  226. skill->save_needed = true;
  227. }
  228. }
  229. void PlayerSkillList::DecreaseSkillCap(int32 skill_id, int16 amount){
  230. DecreaseSkillCap(GetSkill(skill_id), amount);
  231. }
  232. void PlayerSkillList::SetSkillCap(Skill* skill, int16 value){
  233. if(skill){
  234. skill->max_val = value;
  235. if(skill->current_val > skill->max_val){
  236. skill->previous_val = skill->current_val;
  237. skill->current_val = skill->max_val;
  238. AddSkillUpdateNeeded(skill);
  239. }
  240. skill->save_needed = true;
  241. }
  242. }
  243. void PlayerSkillList::SetSkillCap(int32 skill_id, int16 value){
  244. SetSkillCap(GetSkill(skill_id), value);
  245. }
  246. int16 PlayerSkillList::CalculateSkillValue(int32 skill_id, int16 current_val){
  247. if (current_val > 5) {
  248. int16 new_val = current_val;
  249. MutexMap<int32, SkillBonus*>::iterator itr = skill_bonus_list.begin();
  250. while (itr.Next()) {
  251. SkillBonus* sb = itr.second;
  252. map<int32, SkillBonusValue*>::iterator sbv_itr;
  253. for (sbv_itr = sb->skills.begin(); sbv_itr != sb->skills.end(); sbv_itr++) {
  254. SkillBonusValue* sbv = sbv_itr->second;
  255. if (sbv->skill_id == skill_id)
  256. new_val += (int16)sbv->value;
  257. }
  258. }
  259. return new_val;
  260. }
  261. }
  262. int16 PlayerSkillList::CalculateSkillMaxValue(int32 skill_id, int16 max_val) {
  263. int16 new_val = max_val;
  264. MutexMap<int32, SkillBonus*>::iterator itr = skill_bonus_list.begin();
  265. while (itr.Next()) {
  266. SkillBonus* sb = itr->second;
  267. map<int32, SkillBonusValue*>::iterator sbv_itr;
  268. for (sbv_itr = sb->skills.begin(); sbv_itr != sb->skills.end(); sbv_itr++) {
  269. SkillBonusValue* sbv = sbv_itr->second;
  270. if (sbv->skill_id == skill_id)
  271. new_val += (int16)sbv->value;
  272. }
  273. }
  274. return new_val;
  275. }
  276. EQ2Packet* PlayerSkillList::GetSkillPacket(int16 version){
  277. PacketStruct* packet = configReader.getStruct("WS_UpdateSkillBook", version);
  278. if(packet){
  279. if(packet_count < skills.size()){
  280. if(!orig_packet){
  281. int16 size = 21*skills.size()+8;
  282. xor_packet = new uchar[size];
  283. orig_packet = new uchar[size];
  284. memset(xor_packet, 0, size);
  285. memset(orig_packet, 0, size);
  286. }
  287. else{
  288. int16 size = 21*skills.size()+8;
  289. uchar* tmp = new uchar[size];
  290. memset(tmp, 0, size);
  291. memcpy(tmp, orig_packet, 21*packet_count+8);
  292. safe_delete_array(orig_packet);
  293. orig_packet = tmp;
  294. safe_delete_array(xor_packet);
  295. xor_packet = new uchar[size];
  296. }
  297. packet_count = skills.size();
  298. }
  299. packet->setArrayLengthByName("skill_count", skills.size());
  300. map<int32, Skill*>::iterator itr;
  301. Skill* skill = 0;
  302. int32 i=0;
  303. for(itr = skills.begin(); itr != skills.end(); itr++){
  304. skill = itr->second;
  305. if(skill){
  306. int16 skill_max_with_bonuses = CalculateSkillMaxValue(skill->skill_id, skill->max_val);
  307. int16 skill_with_bonuses = int(CalculateSkillValue(skill->skill_id, skill->current_val));
  308. if (skill->skill_id == 613995491) {
  309. int x_current = skill->current_val;
  310. int x_previous = skill->previous_val;
  311. int x_max = skill->max_val;
  312. int x_delta2 = skill_max_with_bonuses - skill->max_val;
  313. int xxx = 1;
  314. }
  315. packet->setArrayDataByName("skill_id", skill->skill_id, i);
  316. packet->setArrayDataByName("type", skill->skill_type, i);
  317. packet->setArrayDataByName("current_val", skill->current_val, i);
  318. packet->setArrayDataByName("base_val", skill->current_val, i);// skill->
  319. packet->setArrayDataByName("skill_delta", 0, i);// skill_with_bonuses- skill->current_val
  320. packet->setArrayDataByName("skill_delta2", skill_max_with_bonuses - skill->max_val, i);// skill_max_with_bonuses - skill->max_val, i);
  321. packet->setArrayDataByName("max_val", skill->max_val, i);
  322. packet->setArrayDataByName("display_minval", skill->display, i);
  323. packet->setArrayDataByName("display_maxval", skill->display, i);
  324. i++;
  325. }
  326. }
  327. EQ2Packet* ret = packet->serializeCountPacket(version, 1, orig_packet, xor_packet);
  328. //packet->PrintPacket();
  329. //DumpPacket(ret);
  330. safe_delete(packet);
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. bool PlayerSkillList::CheckSkillIncrease(Skill* skill){
  336. if(!skill || skill->current_val >= skill->max_val)
  337. return false;
  338. // Assuming that skills will be used more at higher levels, increase chances are:
  339. // skill val of 1 ~ 20% chance, value of 100 ~ 10%, value of 400 ~ 4%
  340. int8 percent = (int8)(((float)((float)100/(float)(50 + skill->current_val)))*10);
  341. if(rand()%100 < percent){ // skill increase
  342. IncreaseSkill(skill, 1);
  343. return true;
  344. }
  345. else
  346. return false;
  347. }
  348. Skill* PlayerSkillList::GetSkillByName(const char* name){
  349. if(name_skill_map.size() == 0){
  350. map<int32, Skill*>::iterator itr;
  351. Skill* skill = 0;
  352. for(itr = skills.begin(); itr != skills.end(); itr++){
  353. skill = itr->second;
  354. name_skill_map[skill->name.data] = skill;
  355. }
  356. }
  357. if(name_skill_map.count(name) > 0)
  358. return name_skill_map[name];
  359. else
  360. return 0;
  361. }
  362. vector<Skill*>* PlayerSkillList::GetSaveNeededSkills(){
  363. vector<Skill*>* ret = new vector<Skill*>;
  364. map<int32, Skill*>::iterator itr;
  365. for(itr = skills.begin(); itr != skills.end(); itr++){
  366. if(itr->second->save_needed){
  367. ret->push_back(itr->second);
  368. itr->second->save_needed = false;
  369. }
  370. }
  371. return ret;
  372. }
  373. void PlayerSkillList::AddSkillUpdateNeeded(Skill* skill){
  374. MSkillUpdates.writelock(__FUNCTION__, __LINE__);
  375. skill_updates.push_back(skill);
  376. has_updates = true;
  377. MSkillUpdates.releasewritelock(__FUNCTION__, __LINE__);
  378. }
  379. vector<Skill*>* PlayerSkillList::GetSkillUpdates(){
  380. vector<Skill*>* ret = 0;
  381. vector<Skill*>::iterator itr;
  382. MSkillUpdates.writelock(__FUNCTION__, __LINE__);
  383. if(skill_updates.size() > 0){
  384. ret = new vector<Skill*>();
  385. ret->insert(ret->begin(), skill_updates.begin(), skill_updates.end());
  386. skill_updates.clear();
  387. }
  388. has_updates = false;
  389. MSkillUpdates.releasewritelock(__FUNCTION__, __LINE__);
  390. return ret;
  391. }
  392. bool PlayerSkillList::HasSkillUpdates(){
  393. return has_updates;
  394. }
  395. void PlayerSkillList::AddSkillBonus(int32 spell_id, int32 skill_id, float value) {
  396. if (value != 0) {
  397. SkillBonus* sb;
  398. if (skill_bonus_list.count(spell_id) == 0) {
  399. sb = new SkillBonus;
  400. sb->spell_id = spell_id;
  401. skill_bonus_list.Put(spell_id, sb);
  402. }
  403. else
  404. sb = skill_bonus_list.Get(spell_id);
  405. if (sb->skills[skill_id] == 0) {
  406. SkillBonusValue* sbv = new SkillBonusValue;
  407. sbv->skill_id = skill_id;
  408. sbv->value = value;
  409. sb->skills[skill_id] = sbv;
  410. }
  411. }
  412. }
  413. SkillBonus* PlayerSkillList::GetSkillBonus(int32 spell_id) {
  414. SkillBonus *ret = 0;
  415. if (skill_bonus_list.count(spell_id) > 0)
  416. ret = skill_bonus_list.Get(spell_id);
  417. return ret;
  418. }
  419. void PlayerSkillList::RemoveSkillBonus(int32 spell_id) {
  420. if (skill_bonus_list.count(spell_id) > 0) {
  421. SkillBonus* sb = skill_bonus_list.Get(spell_id);
  422. skill_bonus_list.erase(spell_id);
  423. map<int32, SkillBonusValue*>::iterator itr;
  424. for (itr = sb->skills.begin(); itr != sb->skills.end(); itr++)
  425. safe_delete(itr->second);
  426. safe_delete(sb);
  427. }
  428. }