Quests.cpp 52 KB

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