Quests.cpp 51 KB

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