Quests.cpp 51 KB

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