Quests.cpp 50 KB

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