Quests.cpp 52 KB

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