Quests.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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 "Quests.h"
  17. #include "../common/ConfigReader.h"
  18. #include "Player.h"
  19. #include "LuaInterface.h"
  20. #include "Spells.h"
  21. #include "RaceTypes/RaceTypes.h"
  22. #include "../common/Log.h"
  23. #ifdef WIN32
  24. #include <time.h>
  25. #else
  26. #include <sys/time.h>
  27. #endif
  28. extern LuaInterface* lua_interface;
  29. extern ConfigReader configReader;
  30. extern MasterFactionList master_faction_list;
  31. extern MasterRaceTypeList race_types_list;
  32. QuestStep::QuestStep(int32 in_id, int8 in_type, string in_description, vector<int32>* in_ids, int32 in_quantity, const char* in_task_group, vector<Location>* in_locations, float in_max_variation, float in_percentage, int32 in_usableitemid){
  33. type = in_type;
  34. description = in_description;
  35. ids = 0;
  36. locations = 0;
  37. if(in_task_group)
  38. task_group = string(in_task_group);
  39. if(type != QUEST_STEP_TYPE_LOCATION) {
  40. if (in_ids){
  41. ids = new std::map<int32, bool>();
  42. for(int32 i=0;i<in_ids->size();i++)
  43. ids->insert(make_pair(in_ids->at(i), true));
  44. }
  45. }
  46. else { // location step
  47. if (in_locations) {
  48. locations = new vector<Location>;
  49. for(int32 i=0; i < in_locations->size(); i++)
  50. locations->push_back(in_locations->at(i));
  51. }
  52. }
  53. max_variation = in_max_variation;
  54. quantity = in_quantity;
  55. step_progress = 0;
  56. icon = 11;
  57. id = in_id;
  58. updated = false;
  59. percentage = in_percentage;
  60. usableitemid = in_usableitemid;
  61. }
  62. QuestStep::QuestStep(QuestStep* old_step){
  63. type = old_step->type;
  64. description = old_step->description;
  65. task_group = old_step->task_group;
  66. quantity = old_step->quantity;
  67. max_variation = old_step->max_variation;
  68. step_progress = 0;
  69. ids = 0;
  70. locations = 0;
  71. if(type != QUEST_STEP_TYPE_LOCATION) {
  72. if (old_step->ids){
  73. ids = new std::map<int32, bool>();
  74. std::map<int32, bool>::iterator itr;
  75. for(itr = old_step->ids->begin();itr != old_step->ids->end();itr++)
  76. ids->insert(make_pair(itr->first, itr->second));
  77. }
  78. }
  79. else { // location step
  80. if (old_step->locations) {
  81. locations = new vector<Location>;
  82. for(int32 i=0; i < old_step->locations->size(); i++)
  83. locations->push_back(old_step->locations->at(i));
  84. }
  85. }
  86. icon = old_step->icon;
  87. id = old_step->id;
  88. updated = false;
  89. percentage = old_step->percentage;
  90. usableitemid = old_step->usableitemid;
  91. }
  92. QuestStep::~QuestStep(){
  93. safe_delete(ids);
  94. safe_delete(locations);
  95. }
  96. bool QuestStep::WasUpdated(){
  97. return updated;
  98. }
  99. void QuestStep::WasUpdated(bool val){
  100. updated = val;
  101. }
  102. float QuestStep::GetPercentage(){
  103. return percentage;
  104. }
  105. int32 QuestStep::GetStepID(){
  106. return id;
  107. }
  108. int32 QuestStep::GetItemID() {
  109. return usableitemid;
  110. }
  111. void QuestStep::SetComplete(){
  112. step_progress = quantity;
  113. updated = true;
  114. }
  115. bool QuestStep::Complete(){
  116. return step_progress >= quantity;
  117. }
  118. int8 QuestStep::GetStepType(){
  119. return type;
  120. }
  121. bool QuestStep::CheckStepReferencedID(int32 id){
  122. bool ret = false;
  123. if(ids){
  124. std::map<int32, bool>::iterator itr;
  125. itr = ids->find(id);
  126. if(itr != ids->end())
  127. ret = true;
  128. }
  129. return ret;
  130. }
  131. bool QuestStep::CheckStepKillRaceReqUpdate(Spawn* spawn){
  132. bool ret = false;
  133. if(ids){
  134. std::map<int32, bool>::iterator itr;
  135. for(itr = ids->begin();itr != ids->end();itr++){
  136. int32 curid = itr->first;
  137. if(curid == spawn->GetRace() ||
  138. curid == race_types_list.GetRaceType(spawn->GetModelType()) ||
  139. curid == race_types_list.GetRaceBaseType(spawn->GetModelType())){
  140. ret = true;
  141. break;
  142. }
  143. }
  144. }
  145. return ret;
  146. }
  147. int16 QuestStep::GetIcon(){
  148. return icon;
  149. }
  150. void QuestStep::SetIcon(int16 in_icon){
  151. icon = in_icon;
  152. }
  153. const char* QuestStep::GetUpdateName(){
  154. if(update_name.length() > 0)
  155. return update_name.c_str();
  156. return 0;
  157. }
  158. void QuestStep::SetUpdateName(const char* name){
  159. update_name = string(name);
  160. }
  161. const char* QuestStep::GetUpdateTargetName(){
  162. if(update_target_name.length() > 0)
  163. return update_target_name.c_str();
  164. return 0;
  165. }
  166. void QuestStep::SetUpdateTargetName(const char* name){
  167. update_target_name = string(name);
  168. }
  169. bool QuestStep::CheckStepLocationUpdate(float char_x, float char_y, float char_z, int32 zone_id){
  170. bool ret = false;
  171. if (locations) {
  172. for (int32 i=0; i < locations->size(); i++) {
  173. float total_diff = 0;
  174. Location loc = locations->at(i);
  175. if(loc.zone_id > 0 && loc.zone_id != zone_id)
  176. continue;
  177. float diff = loc.x - char_x; //Check X
  178. if(diff < 0)
  179. diff *= -1;
  180. if(diff <= max_variation) {
  181. total_diff += diff;
  182. diff = loc.z - char_z; //Check Z (we check Z first because it is far more likely to be a much greater variation than y)
  183. if(diff < 0)
  184. diff *= -1;
  185. if(diff <= max_variation) {
  186. total_diff += diff;
  187. if(total_diff <= max_variation) { //Check Total
  188. diff = loc.y - char_y; //Check Y
  189. if(diff < 0)
  190. diff *= -1;
  191. if(diff <= max_variation) {
  192. total_diff += diff;
  193. if(total_diff <= max_variation) { //Check Total
  194. ret = true;
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. return ret;
  204. }
  205. void QuestStep::SetStepProgress(int32 val){
  206. step_progress = val;
  207. }
  208. int32 QuestStep::AddStepProgress(int32 val){
  209. updated = true;
  210. if (val > (quantity - step_progress)){
  211. step_progress += (quantity - step_progress);
  212. return (quantity - step_progress);
  213. }
  214. else
  215. step_progress += val;
  216. return val;
  217. }
  218. int32 QuestStep::GetStepProgress() {
  219. return step_progress;
  220. }
  221. void QuestStep::ResetTaskGroup(){
  222. task_group = "";
  223. }
  224. const char* QuestStep::GetTaskGroup(){
  225. if(task_group.length() > 0)
  226. return task_group.c_str();
  227. return 0;
  228. }
  229. const char* QuestStep::GetDescription(){
  230. if(description.length() > 0)
  231. return description.c_str();
  232. return 0;
  233. }
  234. void QuestStep::SetDescription(string desc){
  235. description = desc;
  236. }
  237. int16 QuestStep::GetQuestCurrentQuantity(){
  238. return (int16)step_progress;
  239. }
  240. int16 QuestStep::GetQuestNeededQuantity(){
  241. return (int16)quantity;
  242. }
  243. Quest::Quest(int32 in_id){
  244. id = in_id;
  245. reward_status = 0;
  246. quest_giver = 0;
  247. deleted = false;
  248. turned_in = false;
  249. update_needed = true;
  250. player = 0;
  251. return_id = 0;
  252. task_group_num = 1;
  253. prereq_level = 1;
  254. prereq_tslevel = 0;
  255. prereq_max_level = 0;
  256. prereq_max_tslevel = 0;
  257. reward_coins = 0;
  258. reward_coins_max = 0;
  259. completed_flag = false;
  260. has_sent_last_update = false;
  261. enc_level = 0;
  262. reward_exp = 0;
  263. reward_tsexp = 0;
  264. m_featherColor = 0;
  265. m_repeatable = false;
  266. yellow_name = false;
  267. m_hidden = false;
  268. generated_coin = 0;
  269. m_questFlags = 0;
  270. m_timestamp = 0;
  271. m_completeCount = 0;
  272. MQuestSteps.SetName("Quest::MQuestSteps");
  273. MCompletedActions.SetName("Quest::MCompletedActions");
  274. MProgressActions.SetName("Quest::MProgressActions");
  275. MFailedActions.SetName("Quest::failed_Actions");
  276. quest_state_temporary = false;
  277. tmp_reward_status = 0;
  278. tmp_reward_coins = 0;
  279. completed_description = string("");
  280. quest_temporary_description = string("");
  281. }
  282. Quest::Quest(Quest* old_quest){
  283. task_group_num = 1;
  284. name = old_quest->name;
  285. type = old_quest->type;
  286. zone = old_quest->zone;
  287. level = old_quest->level;
  288. enc_level = old_quest->enc_level;
  289. description = old_quest->description;
  290. prereq_level = old_quest->prereq_level;
  291. prereq_tslevel = old_quest->prereq_tslevel;
  292. prereq_max_level = old_quest->prereq_max_level;
  293. prereq_max_tslevel = old_quest->prereq_max_tslevel;
  294. prereq_quests = old_quest->prereq_quests;
  295. prereq_classes = old_quest->prereq_classes;
  296. prereq_tradeskillclass = old_quest->prereq_tradeskillclass;
  297. prereq_races = old_quest->prereq_races;
  298. prereq_factions = old_quest->prereq_factions;
  299. reward_coins = old_quest->reward_coins;
  300. reward_coins_max = old_quest->reward_coins_max;
  301. reward_factions = old_quest->reward_factions;
  302. reward_status = old_quest->reward_status;
  303. reward_comment = old_quest->reward_comment;
  304. reward_exp = old_quest->reward_exp;
  305. reward_tsexp = old_quest->reward_tsexp;
  306. generated_coin = old_quest->generated_coin;
  307. completed_flag = old_quest->completed_flag;
  308. has_sent_last_update = old_quest->has_sent_last_update;
  309. yellow_name = old_quest->yellow_name;
  310. m_questFlags = old_quest->m_questFlags;
  311. id = old_quest->id;
  312. vector<QuestStep*> quest_steps;
  313. for(int32 i=0;i<old_quest->quest_steps.size();i++)
  314. AddQuestStep(new QuestStep(old_quest->quest_steps[i]));
  315. for(int32 i=0;i<old_quest->prereq_items.size();i++)
  316. AddPrereqItem(new Item(old_quest->prereq_items[i]));
  317. for(int32 i=0;i<old_quest->reward_items.size();i++)
  318. AddRewardItem(new Item(old_quest->reward_items[i]));
  319. for(int32 i=0;i<old_quest->selectable_reward_items.size();i++)
  320. AddSelectableRewardItem(new Item(old_quest->selectable_reward_items[i]));
  321. complete_actions = old_quest->complete_actions;
  322. progress_actions = old_quest->progress_actions;
  323. failed_actions = old_quest->failed_actions;
  324. time_t raw;
  325. struct tm *newtime;
  326. time(&raw);
  327. newtime = localtime(&raw);
  328. day = newtime->tm_mday;
  329. month = newtime->tm_mon + 1;
  330. year = newtime->tm_year - 100;
  331. visible = 1;
  332. step_updates.clear();
  333. step_failures.clear();
  334. completed_description = old_quest->completed_description;
  335. deleted = old_quest->deleted;
  336. update_needed = old_quest->update_needed;
  337. turned_in = old_quest->turned_in;
  338. quest_giver = old_quest->quest_giver;
  339. player = old_quest->player;
  340. return_id = old_quest->return_id;
  341. m_featherColor = old_quest->m_featherColor;
  342. m_repeatable = old_quest->IsRepeatable();
  343. m_hidden = false;
  344. m_timestamp = 0;
  345. m_completeCount = old_quest->GetCompleteCount();
  346. MQuestSteps.SetName("Quest::MQuestSteps");
  347. MProgressActions.SetName("Quest::MProgressActions");
  348. MCompletedActions.SetName("Quest::MCompletedActions");
  349. quest_state_temporary = false;
  350. tmp_reward_status = 0;
  351. tmp_reward_coins = 0;
  352. quest_temporary_description = string("");
  353. }
  354. Quest::~Quest(){
  355. for(int32 i=0;i<quest_steps.size();i++)
  356. safe_delete(quest_steps[i]);
  357. for(int32 i=0;i<prereq_items.size();i++)
  358. safe_delete(prereq_items[i]);
  359. for(int32 i=0;i<reward_items.size();i++)
  360. safe_delete(reward_items[i]);
  361. for(int32 i=0;i<tmp_reward_items.size();i++)
  362. safe_delete(tmp_reward_items[i]);
  363. for(int32 i=0;i<selectable_reward_items.size();i++)
  364. safe_delete(selectable_reward_items[i]);
  365. quest_step_map.clear();
  366. task_group_order.clear();
  367. task_group.clear();
  368. complete_actions.clear();
  369. progress_actions.clear();
  370. failed_actions.clear();
  371. }
  372. void Quest::RegisterQuest(string in_name, string in_type, string in_zone, int8 in_level, string in_desc){
  373. name = in_name;
  374. type = in_type;
  375. zone = in_zone;
  376. level = in_level;
  377. description = in_desc;
  378. }
  379. const char* Quest::GetDescription(){
  380. return description.c_str();
  381. }
  382. const char* Quest::GetName(){
  383. return name.c_str();
  384. }
  385. const char* Quest::GetType(){
  386. return type.c_str();
  387. }
  388. void Quest::SetZone(string in_zone) {
  389. zone = in_zone;
  390. }
  391. const char* Quest::GetZone(){
  392. return zone.c_str();
  393. }
  394. int8 Quest::GetLevel(){
  395. return level;
  396. }
  397. int8 Quest::GetDay(){
  398. return day;
  399. }
  400. int8 Quest::GetMonth(){
  401. return month;
  402. }
  403. int8 Quest::GetYear(){
  404. return year;
  405. }
  406. int32 Quest::GetStatusPoints(){
  407. return reward_status;
  408. }
  409. void Quest::SetDay(int8 value){
  410. day = value;
  411. }
  412. void Quest::SetMonth(int8 value){
  413. month = value;
  414. }
  415. void Quest::SetYear(int8 value){
  416. year = value;
  417. }
  418. QuestStep* Quest::GetQuestStep(int32 step_id){
  419. QuestStep* ret = 0;
  420. MQuestSteps.lock();
  421. for(int32 i=0;i<quest_steps.size(); i++){
  422. if(quest_steps[i] && quest_steps[i]->GetStepID() == step_id){
  423. ret = quest_steps[i];
  424. break;
  425. }
  426. }
  427. MQuestSteps.unlock();
  428. return ret;
  429. }
  430. vector<QuestStep*>* Quest::GetQuestSteps(){
  431. return &quest_steps;
  432. }
  433. vector<QuestStep*>* Quest::GetQuestUpdates(){
  434. return &step_updates;
  435. }
  436. vector<QuestStep*>* Quest::GetQuestFailures(){
  437. return &step_failures;
  438. }
  439. bool Quest::CheckQuestReferencedSpawns(Spawn* spawn){
  440. QuestStep* step = 0;
  441. bool ret = false;
  442. int32 spawnDBID = spawn->GetDatabaseID();
  443. MQuestSteps.lock();
  444. for(int32 i=0;i<quest_steps.size(); i++){
  445. step = quest_steps[i];
  446. if(!step || step->Complete())
  447. continue;
  448. switch(step->GetStepType())
  449. {
  450. case QUEST_STEP_TYPE_KILL:
  451. case QUEST_STEP_TYPE_NORMAL: {
  452. if(step->CheckStepReferencedID(spawnDBID))
  453. ret = true;
  454. break;
  455. }
  456. case QUEST_STEP_TYPE_KILL_RACE_REQ: {
  457. if(step->CheckStepKillRaceReqUpdate(spawn))
  458. ret = true;
  459. break;
  460. }
  461. }
  462. }
  463. MQuestSteps.unlock();
  464. return ret;
  465. }
  466. bool Quest::CheckQuestKillUpdate(Spawn* spawn, bool update){
  467. QuestStep* step = 0;
  468. bool ret = false;
  469. int32 id = spawn->GetDatabaseID();
  470. int32 prog_added = 0;
  471. MQuestSteps.lock();
  472. for(int32 i=0;i<quest_steps.size(); i++){
  473. step = quest_steps[i];
  474. if(!step)
  475. continue;
  476. if((step->GetStepType() == QUEST_STEP_TYPE_KILL && !step->Complete() && step->CheckStepReferencedID(id)) ||
  477. (step->GetStepType() == QUEST_STEP_TYPE_KILL_RACE_REQ && !step->Complete() && step->CheckStepKillRaceReqUpdate(spawn)))
  478. {
  479. if (update == true) {
  480. bool passed = true;
  481. if (step->GetPercentage() < 100)
  482. passed = (step->GetPercentage() > MakeRandomFloat(0, 100));
  483. if (passed) {
  484. //Call the progress action function with the total amount of progress actually granted
  485. prog_added = step->AddStepProgress(1);
  486. if (lua_interface && progress_actions[step->GetStepID()].length() > 0 && prog_added > 0)
  487. lua_interface->CallQuestFunction(this, progress_actions[step->GetStepID()].c_str(), player, prog_added);
  488. step_updates.push_back(step);
  489. step->SetUpdateName(spawn->GetName());
  490. ret = true;
  491. }
  492. else
  493. step_failures.push_back(step);
  494. }
  495. else {
  496. ret = true;
  497. }
  498. }
  499. }
  500. MQuestSteps.unlock();
  501. if(ret && update)
  502. SetSaveNeeded(true);
  503. return ret;
  504. }
  505. int16 Quest::GetTaskGroupStep(){
  506. int16 ret = task_group_order.size();
  507. map<int16, string>::iterator itr;
  508. MQuestSteps.lock();
  509. for(itr = task_group_order.begin(); itr != task_group_order.end(); itr++){
  510. if(task_group.count(itr->second) > 0){
  511. vector<QuestStep*> tmp_steps = task_group[itr->second];
  512. bool complete = true;
  513. for(int32 i=0;i<tmp_steps.size();i++){
  514. if(tmp_steps[i]->Complete() == false)
  515. complete = false;
  516. }
  517. if(!complete){
  518. if(itr->first < ret)
  519. ret = itr->first;
  520. }
  521. }
  522. }
  523. MQuestSteps.unlock();
  524. return ret;
  525. }
  526. bool Quest::SetStepComplete(int32 step){
  527. QuestStep* quest_step = 0;
  528. bool ret = false;
  529. MQuestSteps.lock();
  530. for(int32 i=0;i<quest_steps.size(); i++){
  531. quest_step = quest_steps[i];
  532. if(quest_step && quest_step->GetStepID() == step){
  533. quest_step->SetComplete();
  534. step_updates.push_back(quest_step);
  535. ret = true;
  536. break;
  537. }
  538. }
  539. MQuestSteps.unlock();
  540. if (ret) {
  541. SetSaveNeeded(true);
  542. }
  543. return ret;
  544. }
  545. bool Quest::AddStepProgress(int32 step_id, int32 progress) {
  546. QuestStep* quest_step = 0;
  547. bool ret = false;
  548. int32 prog_added = 0;
  549. MQuestSteps.lock();
  550. for (int32 i = 0; i < quest_steps.size(); i++) {
  551. quest_step = quest_steps[i];
  552. if (quest_step && quest_step->GetStepID() == step_id) {
  553. bool passed = true;
  554. if(quest_step->GetPercentage() < 100 && quest_step->GetPercentage() != 0)
  555. passed = (quest_step->GetPercentage() > MakeRandomFloat(0, 100));
  556. if(passed) {
  557. //Call the progress action function with the total amount of progress actually granted
  558. prog_added = quest_step->AddStepProgress(progress);
  559. if(lua_interface && progress_actions[step_id].length() > 0 && prog_added > 0)
  560. lua_interface->CallQuestFunction(this, progress_actions[step_id].c_str(), player, prog_added);
  561. step_updates.push_back(quest_step);
  562. ret = true;
  563. break;
  564. }
  565. else {
  566. step_failures.push_back(quest_step);
  567. break;
  568. }
  569. }
  570. }
  571. MQuestSteps.unlock();
  572. if(ret)
  573. SetSaveNeeded(true);
  574. return ret;
  575. }
  576. int32 Quest::GetStepProgress(int32 step_id) {
  577. QuestStep* quest_step = 0;
  578. int32 ret = 0;
  579. MQuestSteps.lock();
  580. for (int32 i = 0; i < quest_steps.size(); i++) {
  581. quest_step = quest_steps[i];
  582. if (quest_step && quest_step->GetStepID() == step_id) {
  583. ret = quest_step->GetStepProgress();
  584. break;
  585. }
  586. }
  587. MQuestSteps.unlock();
  588. return ret;
  589. }
  590. bool Quest::GetQuestStepCompleted(int32 step_id){
  591. bool ret = false;
  592. QuestStep* quest_step = 0;
  593. MQuestSteps.lock();
  594. for(int32 i=0;i<quest_steps.size(); i++){
  595. quest_step = quest_steps[i];
  596. if(quest_step && quest_step->GetStepID() == step_id && quest_step->Complete()){
  597. ret = true;
  598. break;
  599. }
  600. }
  601. MQuestSteps.unlock();
  602. return ret;
  603. }
  604. int16 Quest::GetQuestStep(){
  605. int16 ret = 0;
  606. QuestStep* step = 0;
  607. MQuestSteps.lock();
  608. for(int32 i=0;i<quest_steps.size(); i++){
  609. step = quest_steps[i];
  610. if(step && !step->Complete()){
  611. ret = step->GetStepID();
  612. break;
  613. }
  614. }
  615. MQuestSteps.unlock();
  616. return ret;
  617. }
  618. bool Quest::QuestStepIsActive(int16 quest_step_id) {
  619. bool ret = false;
  620. QuestStep* step = 0;
  621. MQuestSteps.lock();
  622. for (int32 i = 0; i < quest_steps.size(); i++) {
  623. step = quest_steps[i];
  624. if (step->GetStepID() == quest_step_id && !step->Complete()) {
  625. ret = true;
  626. break;
  627. }
  628. }
  629. MQuestSteps.unlock();
  630. return ret;
  631. }
  632. bool Quest::CheckQuestChatUpdate(int32 id, bool update){
  633. QuestStep* step = 0;
  634. bool ret = false;
  635. int32 prog_added = 0;
  636. MQuestSteps.lock();
  637. for(int32 i=0;i<quest_steps.size(); i++){
  638. step = quest_steps[i];
  639. if(step && step->GetStepType() == QUEST_STEP_TYPE_CHAT && !step->Complete() && step->CheckStepReferencedID(id)){
  640. if(update){
  641. //Call the progress action function with the total amount of progress actually granted
  642. prog_added = step->AddStepProgress(1);
  643. if(lua_interface && progress_actions[step->GetStepID()].length() > 0 && prog_added > 0)
  644. lua_interface->CallQuestFunction(this, progress_actions[step->GetStepID()].c_str(), player, prog_added);
  645. step_updates.push_back(step);
  646. }
  647. ret = true;
  648. }
  649. }
  650. MQuestSteps.unlock();
  651. if(ret)
  652. SetSaveNeeded(true);
  653. return ret;
  654. }
  655. bool Quest::CheckQuestItemUpdate(int32 id, int8 quantity){
  656. QuestStep* step = 0;
  657. bool ret = false;
  658. int32 prog_added = 0;
  659. MQuestSteps.lock();
  660. for(int32 i=0;i<quest_steps.size(); i++){
  661. step = quest_steps[i];
  662. if(step && step->GetStepType() == QUEST_STEP_TYPE_OBTAIN_ITEM && !step->Complete() && step->CheckStepReferencedID(id)){
  663. bool passed = true;
  664. if(step->GetPercentage() < 100)
  665. passed = (step->GetPercentage() > MakeRandomFloat(0, 100));
  666. if(passed){
  667. //Call the progress action function with the total amount of progress actually granted
  668. prog_added = step->AddStepProgress(quantity);
  669. if(lua_interface && progress_actions[step->GetStepID()].length() > 0 && prog_added > 0)
  670. lua_interface->CallQuestFunction(this, progress_actions[step->GetStepID()].c_str(), player, prog_added);
  671. step_updates.push_back(step);
  672. ret = true;
  673. }
  674. else
  675. step_failures.push_back(step);
  676. }
  677. }
  678. MQuestSteps.unlock();
  679. if(ret)
  680. SetSaveNeeded(true);
  681. return ret;
  682. }
  683. bool Quest::CheckQuestRefIDUpdate(int32 id, int32 quantity){
  684. QuestStep* step = 0;
  685. bool ret = false;
  686. int32 prog_added = 0;
  687. MQuestSteps.lock();
  688. for(int32 i=0;i<quest_steps.size(); i++){
  689. step = quest_steps[i];
  690. if(step)
  691. {
  692. if(step->Complete())
  693. continue;
  694. switch(step->GetStepType()) {
  695. case QUEST_STEP_TYPE_HARVEST:
  696. case QUEST_STEP_TYPE_CRAFT: {
  697. map<int32, bool>* id_list = step->GetUpdateIDs();
  698. map<int32, bool>::iterator itr;
  699. for(itr = id_list->begin();itr != id_list->end(); itr++){
  700. int32 update_id = itr->first;
  701. if(update_id == id){
  702. bool passed = true;
  703. if(step->GetPercentage() < 100)
  704. passed = (step->GetPercentage() > MakeRandomFloat(0, 100));
  705. if(passed){
  706. //Call the progress action function with the total amount of progress actually granted
  707. prog_added = step->AddStepProgress(quantity);
  708. if(lua_interface && progress_actions[step->GetStepID()].length() > 0 && prog_added > 0)
  709. lua_interface->CallQuestFunction(this, progress_actions[step->GetStepID()].c_str(), player, prog_added);
  710. step_updates.push_back(step);
  711. }
  712. else
  713. step_failures.push_back(step);
  714. ret = true;
  715. }
  716. }
  717. break;
  718. }
  719. }
  720. }
  721. }
  722. MQuestSteps.unlock();
  723. if(ret)
  724. SetSaveNeeded(true);
  725. return ret;
  726. }
  727. bool Quest::CheckQuestLocationUpdate(float char_x, float char_y, float char_z, int32 zone_id){
  728. QuestStep* step = 0;
  729. bool ret = false;
  730. int32 prog_added = 0;
  731. MQuestSteps.lock();
  732. for(int32 i=0;i<quest_steps.size(); i++){
  733. step = quest_steps[i];
  734. if(step && step->GetStepType() == QUEST_STEP_TYPE_LOCATION && !step->Complete() && step->CheckStepLocationUpdate(char_x, char_y, char_z, zone_id)){
  735. //Call the progress action function with the total amount of progress actually granted
  736. prog_added = step->AddStepProgress(1);
  737. if(lua_interface && progress_actions[step->GetStepID()].length() > 0 && prog_added > 0)
  738. lua_interface->CallQuestFunction(this, progress_actions[step->GetStepID()].c_str(), player, prog_added);
  739. step_updates.push_back(step);
  740. ret = true;
  741. }
  742. }
  743. MQuestSteps.unlock();
  744. if(ret)
  745. SetSaveNeeded(true);
  746. return ret;
  747. }
  748. bool Quest::CheckQuestSpellUpdate(Spell* spell){
  749. QuestStep* step = 0;
  750. bool ret = false;
  751. int32 id = spell->GetSpellID();
  752. int32 prog_added = 0;
  753. MQuestSteps.lock();
  754. for (int32 i = 0; i < quest_steps.size(); i++) {
  755. step = quest_steps[i];
  756. if(step && step->GetStepType() == QUEST_STEP_TYPE_SPELL && !step->Complete() && step->CheckStepReferencedID(id)){
  757. bool passed = true;
  758. if(step->GetPercentage() < 100)
  759. passed = (step->GetPercentage() > MakeRandomFloat(0, 100));
  760. if(passed) {
  761. //Call the progress action function with the total amount of progress actually granted
  762. prog_added = step->AddStepProgress(1);
  763. if(lua_interface && progress_actions[step->GetStepID()].length() > 0 && prog_added > 0)
  764. lua_interface->CallQuestFunction(this, progress_actions[step->GetStepID()].c_str(), player, prog_added);
  765. step_updates.push_back(step);
  766. //step->SetUpdateName(spawn->GetName());
  767. ret = true;
  768. }
  769. else
  770. step_failures.push_back(step);
  771. }
  772. }
  773. MQuestSteps.unlock();
  774. if (ret)
  775. SetSaveNeeded(true);
  776. return ret;
  777. }
  778. void Quest::SetQuestID(int32 in_id){
  779. id = in_id;
  780. }
  781. EQ2Packet* Quest::OfferQuest(int16 version, Player* player){
  782. PacketStruct* packet = configReader.getStruct("WS_OfferQuest", version);
  783. if(packet){
  784. packet->setDataByName("reward", "Quest Reward!");
  785. packet->setDataByName("title", name.c_str());
  786. packet->setDataByName("description", description.c_str());
  787. if(type == "Tradeskill")
  788. packet->setDataByName("quest_difficulty", player->GetTSArrowColor(level));
  789. else
  790. packet->setDataByName("quest_difficulty", player->GetArrowColor(level));
  791. packet->setDataByName("encounter_level", enc_level);
  792. packet->setDataByName("unknown0", 255);
  793. if (version >= 1188)
  794. packet->setDataByName("unknown4b", 1);
  795. else
  796. packet->setDataByName("unknown", 5);
  797. packet->setDataByName("level", level);
  798. if(reward_coins > 0){
  799. packet->setDataByName("min_coin", reward_coins);
  800. if (reward_coins_max)
  801. packet->setDataByName("max_coin", reward_coins_max);
  802. }
  803. packet->setDataByName("status_points", reward_status);
  804. if(reward_comment.length() > 0)
  805. packet->setDataByName("text", reward_comment.c_str());
  806. if(reward_items.size() > 0){
  807. packet->setArrayLengthByName("num_rewards", reward_items.size());
  808. Item* item = 0;
  809. for(int32 i=0;i<reward_items.size();i++){
  810. item = reward_items[i];
  811. packet->setArrayDataByName("reward_id", item->details.item_id, i);
  812. if(version < 860)
  813. packet->setItemArrayDataByName("item", item, player, i, 0, -1);
  814. else if (version < 1193)
  815. packet->setItemArrayDataByName("item", item, player, i);
  816. else
  817. packet->setItemArrayDataByName("item", item, player, i, 0, 2);
  818. }
  819. }
  820. if(selectable_reward_items.size() > 0){
  821. packet->setArrayLengthByName("num_select_rewards", selectable_reward_items.size());
  822. Item* item = 0;
  823. for(int32 i=0;i<selectable_reward_items.size();i++){
  824. item = selectable_reward_items[i];
  825. packet->setArrayDataByName("select_reward_id", item->details.item_id, i);
  826. if(version < 860)
  827. packet->setItemArrayDataByName("select_item", item, player, i, 0, -1);
  828. else if (version < 1193)
  829. packet->setItemArrayDataByName("select_item", item, player, i);
  830. else
  831. packet->setItemArrayDataByName("select_item", item, player, i, 0, 2);
  832. }
  833. }
  834. map<int32, sint32>* reward_factions = GetRewardFactions();
  835. if (reward_factions && reward_factions->size() > 0) {
  836. packet->setArrayLengthByName("num_factions", reward_factions->size());
  837. map<int32, sint32>::iterator itr;
  838. int16 index = 0;
  839. for (itr = reward_factions->begin(); itr != reward_factions->end(); itr++) {
  840. int32 faction_id = itr->first;
  841. sint32 amount = itr->second;
  842. const char* faction_name = master_faction_list.GetFactionNameByID(faction_id);
  843. if (faction_name) {
  844. packet->setArrayDataByName("faction_name", const_cast<char*>(faction_name), index);
  845. packet->setArrayDataByName("amount", amount, index);
  846. }
  847. index++;
  848. }
  849. }
  850. char accept[35] = {0};
  851. char decline[35] = {0};
  852. sprintf(accept, "q_accept_pending_quest %u", id);
  853. sprintf(decline, "q_deny_pending_quest %u", id);
  854. packet->setDataByName("accept_command", accept);
  855. packet->setDataByName("decline_command", decline);
  856. EQ2Packet* outapp = packet->serialize();
  857. #if EQDEBUG >= 9
  858. DumpPacket(outapp);
  859. #endif
  860. safe_delete(packet);
  861. return outapp;
  862. }
  863. return 0;
  864. }
  865. int32 Quest::GetQuestID(){
  866. return id;
  867. }
  868. int8 Quest::GetQuestLevel(){
  869. return level;
  870. }
  871. int8 Quest::GetVisible(){
  872. return visible;
  873. }
  874. EQ2Packet* Quest::QuestJournalReply(int16 version, int32 player_crc, Player* player, QuestStep* updateStep, int8 update_count, bool old_completed_quest, bool quest_failure, bool display_quest_helper) {
  875. PacketStruct* packet = configReader.getStruct("WS_QuestJournalReply", version);
  876. if (packet) {
  877. packet->setDataByName("quest_id", id);
  878. packet->setDataByName("player_crc", player_crc);
  879. packet->setDataByName("name", name.c_str());
  880. packet->setDataByName("description", description.c_str());
  881. packet->setDataByName("zone", zone.c_str());
  882. packet->setDataByName("type", type.c_str());
  883. packet->setDataByName("complete_header", "To complete this quest, I must do the following tasks:");
  884. packet->setDataByName("day", day);
  885. packet->setDataByName("month", month);
  886. packet->setDataByName("year", year);
  887. packet->setDataByName("level", level);
  888. packet->setDataByName("visible", 1);
  889. /* To get the quest timer to work you need to set unknown, index 4 to 1 and the time stamp
  890. to the current time + the time in seconds you want to show in the journal*/
  891. if (m_timestamp > 0) {
  892. packet->setDataByName("unknown", 1, 4);
  893. packet->setDataByName("time_stamp", m_timestamp);
  894. }
  895. int8 difficulty = 0;
  896. if (type == "Tradeskill")
  897. difficulty = player->GetTSArrowColor(level);
  898. else
  899. difficulty = player->GetArrowColor(level);
  900. packet->setDataByName("difficulty", difficulty);
  901. if (enc_level > 4)
  902. packet->setDataByName("encounter_level", enc_level);
  903. else
  904. packet->setDataByName("encounter_level", 4);
  905. packet->setDataByName("unknown2b", 4);
  906. int16 task_groups_completed = 0;
  907. int16 total_task_groups = 0;
  908. vector<QuestStep*> primary_order;
  909. vector<QuestStep*> secondary_order;
  910. packet->setDataByName("unknown8", 1, 1);
  911. packet->setSubstructArrayDataByName("reward_data", "unknown8", 1, 1);
  912. packet->setDataByName("unknown8b", 255);
  913. if (version >= 1096) {
  914. packet->setDataByName("deletable", 1);
  915. packet->setDataByName("shareable", 1);
  916. }
  917. else {
  918. packet->setDataByName("unknown3", 1, 5);
  919. packet->setDataByName("unknown3", 1, 6);
  920. }
  921. int8 map_data_count = 0;
  922. packet->setDataByName("bullets", 1);
  923. if (old_completed_quest) {
  924. if (version >= 1096 || version == 546) {
  925. packet->setDataByName("complete", 1);
  926. packet->setDataByName("complete2", 1);
  927. packet->setDataByName("complete3", 1);
  928. }
  929. else {
  930. packet->setDataByName("unknown3", 1);
  931. packet->setDataByName("unknown3", 1, 1);
  932. packet->setDataByName("unknown3", 1, 2);
  933. packet->setDataByName("unknown3", 1, 5);
  934. packet->setDataByName("unknown3", 1, 6);
  935. }
  936. }
  937. // must always send for newer clients like AoM or else crash!
  938. else if (GetCompleted() && ((version >= 1096) || (version == 546 && HasSentLastUpdate()))) { //need to send last quest update before erasing all progress of the quest
  939. packet->setDataByName("complete", 1);
  940. packet->setDataByName("complete2", 1);
  941. packet->setDataByName("complete3", 1);
  942. packet->setDataByName("num_task_groups", 1);
  943. packet->setArrayDataByName("task_group", completed_description.c_str());
  944. packet->setArrayDataByName("unknown4", 0xFFFFFFFF);
  945. packet->setDataByName("onscreen_update", 1);
  946. packet->setDataByName("update_task_number", 1);
  947. packet->setDataByName("onscreen_update", 1);
  948. packet->setDataByName("onscreen_update_text", completed_description.c_str());
  949. if (updateStep)
  950. packet->setDataByName("onscreen_update_icon", updateStep->GetIcon());
  951. else
  952. packet->setDataByName("onscreen_update_icon", 0);
  953. // Is Heritage or db entry for classic eq quest turn in sound
  954. packet->setDataByName("classic_eq_sound", 0);
  955. // No clue what this does, set it to mach live packets
  956. packet->setDataByName("unknown12b", 1, 0);
  957. }
  958. else {
  959. map<QuestStep*, string> task_group_names;
  960. map<int16, string>::iterator order_itr;
  961. vector<QuestStep*>* steps = 0;
  962. MQuestSteps.lock();
  963. for (order_itr = task_group_order.begin(); order_itr != task_group_order.end(); order_itr++) {
  964. //The following is kind of crazy, but necessary to order the quests with completed ones first. This is to make the packet like live's packet
  965. //for(itr = task_group.begin(); itr != task_group.end(); itr++){
  966. bool complete = true;
  967. if (task_group.count(order_itr->second) > 0) {
  968. steps = &(task_group[order_itr->second]);
  969. for (int32 i = 0; i < steps->size(); i++) {
  970. if (steps->at(i)->Complete() == false)
  971. complete = false;
  972. }
  973. if (complete) {
  974. for (int32 i = 0; i < steps->size(); i++) {
  975. if (i == 0)
  976. task_group_names[steps->at(i)] = order_itr->second;
  977. primary_order.push_back(steps->at(i));
  978. }
  979. task_groups_completed++;
  980. total_task_groups++;
  981. }
  982. else {
  983. for (int32 i = 0; i < steps->size(); i++) {
  984. if (i == 0)
  985. task_group_names[steps->at(i)] = order_itr->second;
  986. secondary_order.push_back(steps->at(i));
  987. }
  988. total_task_groups++;
  989. }
  990. }
  991. }
  992. packet->setDataByName("task_groups_completed", task_groups_completed);
  993. packet->setArrayLengthByName("num_task_groups", total_task_groups);
  994. if (IsTracked() /*display_quest_helper*/ && task_groups_completed < total_task_groups)
  995. packet->setDataByName("display_quest_helper", 1);
  996. int16 index = 0;
  997. QuestStep* step = 0;
  998. for (int32 i = 0; i < primary_order.size(); i++) {
  999. if (primary_order[i]->GetTaskGroup()) {
  1000. if (task_group_names.count(primary_order[i]) > 0)
  1001. packet->setArrayDataByName("task_group", primary_order[i]->GetTaskGroup(), index);
  1002. else
  1003. continue;
  1004. packet->setSubArrayLengthByName("num_tasks", task_group[primary_order[i]->GetTaskGroup()].size(), index);
  1005. packet->setSubArrayLengthByName("num_updates", task_group[primary_order[i]->GetTaskGroup()].size(), index);
  1006. map_data_count += task_group[primary_order[i]->GetTaskGroup()].size();
  1007. if (task_group[primary_order[i]->GetTaskGroup()].size() > 0) {
  1008. packet->setDataByName("bullets", 1);
  1009. }
  1010. for (int32 x = 0; x < task_group[primary_order[i]->GetTaskGroup()].size(); x++) {
  1011. step = task_group[primary_order[i]->GetTaskGroup()].at(x);
  1012. if (!step)
  1013. continue;
  1014. if (step->GetDescription())
  1015. packet->setSubArrayDataByName("task", step->GetDescription(), index, x);
  1016. packet->setSubArrayDataByName("task_completed", 1, index, x);
  1017. packet->setSubArrayDataByName("index", x, index, x);
  1018. packet->setSubArrayDataByName("update_currentval", step->GetQuestCurrentQuantity(), index, x);
  1019. if(step->GetQuestCurrentQuantity() > 0)
  1020. packet->setDataByName("journal_updated", 1);
  1021. packet->setSubArrayDataByName("update_maxval", step->GetQuestNeededQuantity(), index, x);
  1022. if (step->GetUpdateTargetName())
  1023. packet->setSubArrayDataByName("update_target_name", step->GetUpdateTargetName(), index, x);
  1024. packet->setSubArrayDataByName("icon", step->GetIcon(), index, x);
  1025. if (updateStep && step == updateStep) {
  1026. packet->setDataByName("update", 1);
  1027. // packet->setDataByName("unknown5d", 1);
  1028. if (!quest_failure)
  1029. packet->setDataByName("onscreen_update", 1);
  1030. packet->setDataByName("onscreen_update_count", update_count);
  1031. packet->setDataByName("onscreen_update_icon", step->GetIcon());
  1032. if (step->GetUpdateName())
  1033. packet->setDataByName("onscreen_update_text", step->GetUpdateName());
  1034. else if (step->GetDescription())
  1035. packet->setDataByName("onscreen_update_text", step->GetDescription());
  1036. packet->setDataByName("update_task_number", x);
  1037. packet->setDataByName("update_taskgroup_number", index);
  1038. }
  1039. packet->setArrayDataByName("unknown4", 0xFFFFFFFF, x);
  1040. }
  1041. index++;
  1042. }
  1043. else {
  1044. if (task_group_names.count(primary_order[i]) > 0) {
  1045. step = primary_order[i];
  1046. if (updateStep && step == updateStep) {
  1047. packet->setDataByName("update", 1);
  1048. // packet->setDataByName("unknown5d", 1);
  1049. if (!quest_failure)
  1050. packet->setDataByName("onscreen_update", 1);
  1051. packet->setDataByName("onscreen_update_count", update_count);
  1052. packet->setDataByName("onscreen_update_icon", step->GetIcon());
  1053. if (step->GetUpdateName())
  1054. packet->setDataByName("onscreen_update_text", step->GetUpdateName());
  1055. else if (step->GetDescription())
  1056. packet->setDataByName("onscreen_update_text", step->GetDescription());
  1057. packet->setDataByName("update_task_number", i);
  1058. packet->setDataByName("update_taskgroup_number", index);
  1059. }
  1060. packet->setArrayDataByName("task_group", primary_order[i]->GetDescription(), index);
  1061. index++;
  1062. }
  1063. }
  1064. }
  1065. for (int32 i = 0; i < secondary_order.size(); i++) {
  1066. if (secondary_order[i]->GetTaskGroup()) {
  1067. if (task_group_names.count(secondary_order[i]) > 0)
  1068. packet->setArrayDataByName("task_group", secondary_order[i]->GetTaskGroup(), index);
  1069. else
  1070. continue;
  1071. packet->setSubArrayLengthByName("num_tasks", task_group[secondary_order[i]->GetTaskGroup()].size(), index);
  1072. packet->setSubArrayLengthByName("num_updates", task_group[secondary_order[i]->GetTaskGroup()].size(), index);
  1073. map_data_count += task_group[secondary_order[i]->GetTaskGroup()].size();
  1074. if (task_group[secondary_order[i]->GetTaskGroup()].size() > 0)
  1075. packet->setDataByName("bullets", 1);
  1076. for (int32 x = 0; x < task_group[secondary_order[i]->GetTaskGroup()].size(); x++) {
  1077. step = task_group[secondary_order[i]->GetTaskGroup()].at(x);
  1078. if (!step)
  1079. continue;
  1080. if (step->GetDescription())
  1081. packet->setSubArrayDataByName("task", step->GetDescription(), index, x);
  1082. if (step->Complete())
  1083. packet->setSubArrayDataByName("task_completed", 1, index, x);
  1084. else
  1085. packet->setSubArrayDataByName("task_completed", 0, index, x);
  1086. packet->setSubArrayDataByName("index", x, index, x);
  1087. packet->setSubArrayDataByName("update_currentval", step->GetQuestCurrentQuantity(), index, x);
  1088. if (step->GetQuestCurrentQuantity() > 0)
  1089. packet->setDataByName("journal_updated", 1);
  1090. packet->setSubArrayDataByName("update_maxval", step->GetQuestNeededQuantity(), index, x);
  1091. packet->setSubArrayDataByName("icon", step->GetIcon(), index, x);
  1092. if (step->GetUpdateTargetName())
  1093. packet->setSubArrayDataByName("update_target_name", step->GetUpdateTargetName(), index, x);
  1094. if (updateStep && step == updateStep) {
  1095. packet->setDataByName("update", 1);
  1096. if (!quest_failure)
  1097. packet->setDataByName("onscreen_update", 1);
  1098. packet->setDataByName("onscreen_update_count", update_count);
  1099. packet->setDataByName("onscreen_update_icon", step->GetIcon());
  1100. if (step->GetUpdateName())
  1101. packet->setDataByName("onscreen_update_text", step->GetUpdateName());
  1102. else if (step->GetDescription())
  1103. packet->setDataByName("onscreen_update_text", step->GetDescription());
  1104. if (quest_failure)
  1105. packet->setDataByName("onscreen_update_text2", "failed");
  1106. packet->setDataByName("update_task_number", x);
  1107. packet->setDataByName("update_taskgroup_number", index);
  1108. }
  1109. packet->setArrayDataByName("unknown4", 0xFFFFFFFF, x);
  1110. }
  1111. index++;
  1112. }
  1113. else {
  1114. if (task_group_names.count(secondary_order[i]) > 0) {
  1115. step = secondary_order[i];
  1116. if (updateStep && step == updateStep) {
  1117. packet->setDataByName("update", 1);
  1118. if (!quest_failure)
  1119. packet->setDataByName("onscreen_update", 1);
  1120. packet->setDataByName("onscreen_update_count", update_count);
  1121. packet->setDataByName("onscreen_update_icon", step->GetIcon());
  1122. if (step->GetUpdateName())
  1123. packet->setDataByName("onscreen_update_text", step->GetUpdateName());
  1124. else if (step->GetDescription())
  1125. packet->setDataByName("onscreen_update_text", step->GetDescription());
  1126. packet->setDataByName("update_task_number", i);
  1127. packet->setDataByName("update_taskgroup_number", index);
  1128. }
  1129. if (task_group_names.size() == 1) {
  1130. packet->setSubArrayLengthByName("num_tasks", 1, index);
  1131. packet->setSubArrayDataByName("task", secondary_order[i]->GetDescription(), index);
  1132. packet->setDataByName("bullets", 0);
  1133. }
  1134. else
  1135. packet->setArrayDataByName("task_group", secondary_order[i]->GetDescription(), index);
  1136. index++;
  1137. }
  1138. }
  1139. }
  1140. for (int16 i = 0; i < total_task_groups; i++)
  1141. packet->setArrayDataByName("unknown4", 0xFFFFFFFF, i);
  1142. if (step && step->GetItemID() > 0) {
  1143. packet->setArrayLengthByName("usable_item_count", 1);
  1144. Item* item = player->GetPlayerItemList()->GetItemFromID(step->GetItemID(), 1);
  1145. if (item) {
  1146. packet->setArrayDataByName("item_id", item->details.item_id, 0);
  1147. packet->setArrayDataByName("item_unique_id", item->details.unique_id, 0);
  1148. packet->setArrayDataByName("item_icon", item->details.icon, 0);
  1149. packet->setArrayDataByName("unknown2", 0xFFFFFFFF, 0);//item->details.item_id, 0);
  1150. }
  1151. }
  1152. if (GetCompleted()) { //mark the last update as being sent, next time we send the quest reply, it will only be a brief portion
  1153. SetSentLastUpdate(true);
  1154. }
  1155. }
  1156. MQuestSteps.unlock();
  1157. string reward_str = "";
  1158. if (version >= 1096 || version == 546)
  1159. reward_str = "reward_data_";
  1160. string tmp = reward_str + "reward";
  1161. packet->setDataByName(tmp.c_str(), "Quest Reward!");
  1162. if (reward_coins > 0) {
  1163. tmp = reward_str + "min_coin";
  1164. packet->setDataByName(tmp.c_str(), reward_coins);
  1165. tmp = reward_str + "max_coin";
  1166. packet->setDataByName(tmp.c_str(), reward_coins_max);
  1167. }
  1168. tmp = reward_str + "status_points";
  1169. packet->setDataByName(tmp.c_str(), reward_status);
  1170. if (reward_comment.length() > 0) {
  1171. tmp = reward_str + "text";
  1172. packet->setDataByName(tmp.c_str(), reward_comment.c_str());
  1173. }
  1174. if (reward_items.size() > 0) {
  1175. tmp = reward_str + "num_rewards";
  1176. packet->setArrayLengthByName(tmp.c_str(), reward_items.size());
  1177. Item* item = 0;
  1178. for (int32 i = 0; i < reward_items.size(); i++) {
  1179. item = reward_items[i];
  1180. packet->setArrayDataByName("reward_id", item->details.item_id, i);
  1181. if (version < 860)
  1182. packet->setItemArrayDataByName("item", item, player, i, 0, -1);
  1183. else if (version < 1193)
  1184. packet->setItemArrayDataByName("item", item, player, i);
  1185. else
  1186. packet->setItemArrayDataByName("item", item, player, i, 0, 2);
  1187. }
  1188. }
  1189. if (selectable_reward_items.size() > 0) {
  1190. tmp = reward_str + "num_select_rewards";
  1191. packet->setArrayLengthByName(tmp.c_str(), selectable_reward_items.size());
  1192. Item* item = 0;
  1193. for (int32 i = 0; i < selectable_reward_items.size(); i++) {
  1194. item = selectable_reward_items[i];
  1195. packet->setArrayDataByName("select_reward_id", item->details.item_id, i);
  1196. if (version < 860)
  1197. packet->setItemArrayDataByName("select_item", item, player, i, 0, -1);
  1198. else if (version < 1193)
  1199. packet->setItemArrayDataByName("select_item", item, player, i);
  1200. else
  1201. packet->setItemArrayDataByName("select_item", item, player, i, 0, 2);
  1202. }
  1203. }
  1204. map<int32, sint32>* reward_factions = GetRewardFactions();
  1205. if (reward_factions && reward_factions->size() > 0) {
  1206. tmp = reward_str + "num_factions";
  1207. packet->setArrayLengthByName(tmp.c_str(), reward_factions->size());
  1208. map<int32, sint32>::iterator itr;
  1209. int16 index = 0;
  1210. for (itr = reward_factions->begin(); itr != reward_factions->end(); itr++) {
  1211. int32 faction_id = itr->first;
  1212. sint32 amount = itr->second;
  1213. const char* faction_name = master_faction_list.GetFactionNameByID(faction_id);
  1214. if (faction_name) {
  1215. packet->setArrayDataByName("faction_name", const_cast<char*>(faction_name), index);
  1216. packet->setArrayDataByName("amount", amount, index);
  1217. }
  1218. index++;
  1219. }
  1220. }
  1221. //packet->setArrayLengthByName("map_data_array_size", map_data_count);
  1222. EQ2Packet* outapp = packet->serialize();
  1223. //packet->PrintPacket();
  1224. //DumpPacket(outapp);
  1225. safe_delete(packet);
  1226. return outapp;
  1227. }
  1228. return 0;
  1229. }
  1230. bool Quest::AddQuestStep(QuestStep* step){
  1231. bool ret = true;
  1232. MQuestSteps.lock();
  1233. if(quest_step_map.count(step->GetStepID()) == 0){
  1234. quest_steps.push_back(step);
  1235. quest_step_reverse_map[step] = step->GetStepID();
  1236. quest_step_map[step->GetStepID()] = step;
  1237. if(step->GetTaskGroup()){
  1238. string tmp_task_group = string(step->GetTaskGroup());
  1239. if(task_group.count(tmp_task_group) == 0){
  1240. task_group_order[task_group_num] = tmp_task_group;
  1241. task_group_num++;
  1242. }
  1243. task_group[tmp_task_group].push_back(step);
  1244. }
  1245. else{
  1246. string tmp_task_group = string(step->GetDescription());
  1247. if(task_group.count(tmp_task_group) == 0){
  1248. task_group_order[task_group_num] = tmp_task_group;
  1249. task_group_num++;
  1250. }
  1251. task_group[tmp_task_group].push_back(step);
  1252. }
  1253. }
  1254. else{
  1255. LogWrite(QUEST__ERROR, 0, "Quest", "Quest Warning in '%s': step %u used multiple times!", GetName(), step->GetStepID());
  1256. ret = false;
  1257. }
  1258. MQuestSteps.unlock();
  1259. return ret;
  1260. }
  1261. bool Quest::RemoveQuestStep(int32 step, Client* client) {
  1262. bool ret = true;
  1263. MQuestSteps.writelock(__FUNCTION__, __LINE__);
  1264. if (quest_step_map.count(step) > 0) {
  1265. QuestStep* quest_step = quest_step_map[step];
  1266. if (quest_step) {
  1267. client->QueuePacket(QuestJournalReply(client->GetVersion(), client->GetNameCRC(), client->GetPlayer(), quest_step, 0, 0, true));
  1268. int32 step2 = step - 1;
  1269. QuestStep* reset_step = quest_step_map[step2];
  1270. reset_step->SetStepProgress(0);
  1271. MCompletedActions.lock();
  1272. complete_actions.erase(step);
  1273. MCompletedActions.unlock();
  1274. string tmp_task_group = string(quest_step->GetTaskGroup());
  1275. if(task_group.count(tmp_task_group) > 0) {
  1276. task_group.erase(tmp_task_group);
  1277. int task_num = 0;
  1278. map<int16, string>::iterator itr;
  1279. for (itr = task_group_order.begin(); itr != task_group_order.end(); itr++) {
  1280. if (itr->second == tmp_task_group) {
  1281. task_num = itr->first;
  1282. break;
  1283. }
  1284. }
  1285. if (task_num > 0) {
  1286. task_group_order.erase(task_num);
  1287. task_group_num--;
  1288. }
  1289. }
  1290. // Remove the step from the various maps before we delete it
  1291. quest_step_map.erase(step);
  1292. quest_step_reverse_map.erase(quest_step);
  1293. vector<QuestStep*>::iterator itr;
  1294. for (itr = quest_steps.begin(); itr != quest_steps.end(); itr++) {
  1295. if ((*itr) == quest_step) {
  1296. quest_steps.erase(itr);
  1297. break;
  1298. }
  1299. }
  1300. safe_delete(quest_step);
  1301. }
  1302. else {
  1303. LogWrite(QUEST__ERROR, 0, "Quest", "Unable to get a valid step (%u) for quest %s", step, GetName());
  1304. ret = false;
  1305. }
  1306. }
  1307. else {
  1308. LogWrite(QUEST__ERROR, 0, "Quest", "Unable to remove step (%u) for quest %s", step, GetName());
  1309. ret = false;
  1310. }
  1311. MQuestSteps.releasewritelock(__FUNCTION__, __LINE__);
  1312. return ret;
  1313. }
  1314. QuestStep* Quest::AddQuestStep(int32 id, int8 in_type, string in_description, vector<int32>* in_ids, int32 in_quantity, const char* in_task_group, vector<Location>* in_locations, float in_max_variation, float in_percentage,int32 in_usableitemid){
  1315. QuestStep* step = new QuestStep(id, in_type, in_description, in_ids, in_quantity, in_task_group, in_locations, in_max_variation, in_percentage, in_usableitemid);
  1316. if(!AddQuestStep(step)){
  1317. safe_delete(step);
  1318. return 0;
  1319. }
  1320. return step;
  1321. }
  1322. void Quest::AddPrereqClass(int8 class_id){
  1323. prereq_classes.push_back(class_id);
  1324. }
  1325. void Quest::AddPrereqTradeskillClass(int8 class_id) {
  1326. prereq_tradeskillclass.push_back(class_id);
  1327. }
  1328. void Quest::AddPrereqModelType(int16 model_type){
  1329. prereq_model_types.push_back(model_type);
  1330. }
  1331. void Quest::AddPrereqRace(int8 race){
  1332. prereq_races.push_back(race);
  1333. }
  1334. void Quest::SetPrereqLevel(int8 lvl){
  1335. prereq_level = lvl;
  1336. }
  1337. void Quest::SetPrereqTSLevel(int8 lvl) {
  1338. prereq_tslevel = lvl;
  1339. }
  1340. void Quest::AddPrereqQuest(int32 quest_id){
  1341. prereq_quests.push_back(quest_id);
  1342. }
  1343. void Quest::AddPrereqFaction(int32 faction_id, sint32 min, sint32 max){
  1344. QuestFactionPrereq faction;
  1345. faction.faction_id = faction_id;
  1346. faction.min = min;
  1347. faction.max = max;
  1348. prereq_factions.push_back(faction);
  1349. }
  1350. void Quest::AddPrereqItem(Item* item){
  1351. prereq_items.push_back(item);
  1352. }
  1353. void Quest::AddSelectableRewardItem(Item* item){
  1354. selectable_reward_items.push_back(item);
  1355. }
  1356. void Quest::AddRewardItem(Item* item){
  1357. reward_items.push_back(item);
  1358. }
  1359. void Quest::AddTmpRewardItem(Item* item){
  1360. tmp_reward_items.push_back(item);
  1361. }
  1362. void Quest::GetTmpRewardItemsByID(std::vector<int32>* items) {
  1363. if(!items)
  1364. return;
  1365. for(int32 i=0;i<tmp_reward_items.size();i++)
  1366. items->push_back(tmp_reward_items[i]->details.item_id);
  1367. }
  1368. void Quest::AddRewardCoins(int32 copper, int32 silver, int32 gold, int32 plat){
  1369. reward_coins = copper + (silver*100) + (gold*10000) + ((int64)plat*1000000);
  1370. }
  1371. void Quest::AddRewardCoinsMax(int64 coins){
  1372. reward_coins_max = coins;
  1373. }
  1374. void Quest::AddRewardFaction(int32 faction_id, sint32 amount){
  1375. reward_factions[faction_id] = amount;
  1376. }
  1377. void Quest::SetRewardStatus(int32 amount){
  1378. reward_status = amount;
  1379. }
  1380. void Quest::SetRewardComment(string comment){
  1381. reward_comment = comment;
  1382. }
  1383. void Quest::SetRewardXP(int32 xp){
  1384. reward_exp = xp;
  1385. }
  1386. vector<int8>* Quest::GetPrereqClasses(){
  1387. return &prereq_classes;
  1388. }
  1389. vector<int8>* Quest::GetPrereqTradeskillClasses(){
  1390. return &prereq_tradeskillclass;
  1391. }
  1392. vector<QuestFactionPrereq>* Quest::GetPrereqFactions(){
  1393. return &prereq_factions;
  1394. }
  1395. vector<int8>* Quest::GetPrereqRaces(){
  1396. return &prereq_races;
  1397. }
  1398. vector<int16>* Quest::GetPrereqModelTypes(){
  1399. return &prereq_model_types;
  1400. }
  1401. int8 Quest::GetPrereqLevel(){
  1402. return prereq_level;
  1403. }
  1404. int8 Quest::GetPrereqTSLevel() {
  1405. return prereq_tslevel;
  1406. }
  1407. vector<int32>* Quest::GetPrereqQuests(){
  1408. return &prereq_quests;
  1409. }
  1410. vector<Item*>* Quest::GetPrereqItems(){
  1411. return &prereq_items;
  1412. }
  1413. vector<Item*>* Quest::GetRewardItems(){
  1414. return &reward_items;
  1415. }
  1416. vector<Item*>* Quest::GetTmpRewardItems(){
  1417. return &tmp_reward_items;
  1418. }
  1419. vector<Item*>* Quest::GetSelectableRewardItems(){
  1420. return &selectable_reward_items;
  1421. }
  1422. map<int32, sint32>* Quest::GetRewardFactions() {
  1423. return &reward_factions;
  1424. }
  1425. void Quest::SetTaskGroupDescription(int32 step, string desc, bool display_bullets){
  1426. MQuestSteps.lock();
  1427. if(step <= task_group_num && task_group_order.count(step) > 0){
  1428. string old_desc = task_group_order[step];
  1429. if(display_bullets)
  1430. task_group[desc] = task_group[old_desc];
  1431. else{
  1432. if(task_group[old_desc].size() > 0){
  1433. task_group[desc].push_back(task_group[old_desc].at(0));
  1434. task_group[desc].at(0)->ResetTaskGroup();
  1435. task_group[desc].at(0)->SetDescription(desc);
  1436. }
  1437. }
  1438. task_group.erase(old_desc);
  1439. task_group_order[step] = desc;
  1440. }
  1441. MQuestSteps.unlock();
  1442. }
  1443. void Quest::SetStepDescription(int32 step, string desc){
  1444. MQuestSteps.lock();
  1445. for(int32 i=0;i<quest_steps.size();i++){
  1446. if(quest_steps[i]->GetStepID() == step){
  1447. quest_steps[i]->SetDescription(desc);
  1448. break;
  1449. }
  1450. }
  1451. MQuestSteps.unlock();
  1452. }
  1453. void Quest::SetDescription(string desc){
  1454. description = desc;
  1455. }
  1456. void Quest::SetCompletedDescription(string desc){
  1457. completed_description = desc;
  1458. }
  1459. const char* Quest::GetCompletedDescription(){
  1460. return completed_description.c_str();
  1461. }
  1462. void Quest::AddCompleteAction(int32 step, string action){
  1463. MCompletedActions.lock();
  1464. complete_actions[step] = action;
  1465. MCompletedActions.unlock();
  1466. }
  1467. void Quest::AddProgressAction(int32 step, string action){
  1468. MProgressActions.lock();
  1469. progress_actions[step] = action;
  1470. MProgressActions.unlock();
  1471. }
  1472. void Quest::AddFailedAction(int32 step, string action) {
  1473. MFailedActions.writelock(__FUNCTION__, __LINE__);
  1474. failed_actions[step] = action;
  1475. MFailedActions.releasewritelock(__FUNCTION__, __LINE__);
  1476. }
  1477. void Quest::SetCompleteAction(string action){
  1478. complete_action = action;
  1479. }
  1480. const char* Quest::GetCompleteAction(){
  1481. if(complete_action.length() > 0)
  1482. return complete_action.c_str();
  1483. return 0;
  1484. }
  1485. const char* Quest::GetCompleteAction(int32 step){
  1486. const char* ret = 0;
  1487. MCompletedActions.lock();
  1488. if(complete_actions.count(step) > 0)
  1489. ret = complete_actions[step].c_str();
  1490. MCompletedActions.unlock();
  1491. return ret;
  1492. }
  1493. void Quest::SetQuestReturnNPC(int32 id){
  1494. return_id = id;
  1495. }
  1496. int32 Quest::GetQuestReturnNPC(){
  1497. return return_id;
  1498. }
  1499. void Quest::SetQuestGiver(int32 id){
  1500. quest_giver = id;
  1501. if(return_id == 0)
  1502. return_id = id;
  1503. }
  1504. bool Quest::GetCompleted(){
  1505. bool ret = true;
  1506. for(int32 i=0;i<quest_steps.size();i++){
  1507. if(quest_steps[i]->Complete() == false){
  1508. ret = false;
  1509. break;
  1510. }
  1511. }
  1512. return ret;
  1513. }
  1514. bool Quest::CheckCategoryYellow(){
  1515. bool ret = false;
  1516. string category = GetType();
  1517. //Check for these category names, return true to set category as yellow in the quest journal
  1518. if (category == "Signature" || category == "Heritage" || category == "Hallmark" || category == "Deity" || category == "Miscellaneaous" || category == "Language" || category == "Lore and Legend" || category == "World Event" || category == "Tradeskill")
  1519. ret = true;
  1520. return ret;
  1521. }
  1522. Player* Quest::GetPlayer(){
  1523. return player;
  1524. }
  1525. void Quest::SetPlayer(Player* in_player){
  1526. player = in_player;
  1527. }
  1528. void Quest::SetGeneratedCoin(int64 coin){
  1529. generated_coin = coin;
  1530. }
  1531. int32 Quest::GetQuestGiver(){
  1532. return quest_giver;
  1533. }
  1534. int64 Quest::GetCoinsReward(){
  1535. return reward_coins;
  1536. }
  1537. int64 Quest::GetCoinsRewardMax(){
  1538. return reward_coins_max;
  1539. }
  1540. int64 Quest::GetGeneratedCoin(){
  1541. return generated_coin;
  1542. }
  1543. int32 Quest::GetExpReward(){
  1544. return reward_exp;
  1545. }
  1546. void Quest::GiveQuestReward(Player* player){
  1547. if(reward_coins > 0)
  1548. player->AddCoins(reward_coins);
  1549. if(!GetQuestTemporaryState())
  1550. reward_items.clear();
  1551. }
  1552. bool Quest::GetDeleted(){
  1553. return deleted;
  1554. }
  1555. void Quest::SetDeleted(bool val){
  1556. deleted = val;
  1557. }
  1558. bool Quest::GetUpdateRequired(){
  1559. return update_needed;
  1560. }
  1561. void Quest::SetUpdateRequired(bool val){
  1562. update_needed = val;
  1563. }
  1564. void Quest::SetTurnedIn(bool val){
  1565. turned_in = val;
  1566. visible = 0;
  1567. }
  1568. bool Quest::GetTurnedIn(){
  1569. return turned_in;
  1570. }
  1571. void Quest::SetName(string in_name) {
  1572. name = in_name;
  1573. }
  1574. void Quest::SetType(string in_type) {
  1575. type = in_type;
  1576. }
  1577. void Quest::SetLevel(int8 in_level) {
  1578. level = in_level;
  1579. }
  1580. void Quest::SetCompletedFlag(bool val) {
  1581. completed_flag = val;
  1582. SetUpdateRequired(true);
  1583. if(player && player->GetClient())
  1584. player->GetClient()->SendQuestJournalUpdate(this, true);
  1585. }
  1586. void Quest::SetStepTimer(int32 duration) {
  1587. m_timestamp = duration == 0 ? 0 : Timer::GetUnixTimeStamp() + duration;
  1588. }
  1589. void Quest::StepFailed(int32 step) {
  1590. if(lua_interface && failed_actions.count(step) > 0 && failed_actions[step].length() > 0)
  1591. lua_interface->CallQuestFunction(this, failed_actions[step].c_str(), player);
  1592. }
  1593. MasterQuestList::MasterQuestList(){
  1594. MQuests.SetName("MasterQuestList::MQuests");
  1595. }
  1596. void MasterQuestList::Reload(){
  1597. MQuests.lock();
  1598. quests.clear(); //deletes taken care of in LuaInterface
  1599. if(lua_interface)
  1600. lua_interface->DestroyQuests(true);
  1601. MQuests.unlock();
  1602. }
  1603. void MasterQuestList::LockQuests(){
  1604. MQuests.lock();
  1605. }
  1606. void MasterQuestList::UnlockQuests(){
  1607. MQuests.unlock();
  1608. }
  1609. void Quest::SetQuestTemporaryState(bool tempState, std::string customDescription)
  1610. {
  1611. if(!tempState)
  1612. {
  1613. tmp_reward_coins = 0;
  1614. tmp_reward_status = 0;
  1615. for(int32 i=0;i<tmp_reward_items.size();i++)
  1616. safe_delete(tmp_reward_items[i]);
  1617. tmp_reward_items.clear();
  1618. }
  1619. quest_state_temporary = tempState;
  1620. quest_temporary_description = customDescription;
  1621. }