Quests.cpp 52 KB

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