quest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "quest.hpp"
  4. #include <stdlib.h>
  5. #include "../common/cbasetypes.hpp"
  6. #include "../common/malloc.hpp"
  7. #include "../common/nullpo.hpp"
  8. #include "../common/random.hpp"
  9. #include "../common/showmsg.hpp"
  10. #include "../common/socket.hpp"
  11. #include "../common/strlib.hpp"
  12. #include "../common/utilities.hpp"
  13. #include "../common/utils.hpp"
  14. #include "battle.hpp"
  15. #include "chrif.hpp"
  16. #include "clif.hpp"
  17. #include "intif.hpp"
  18. #include "itemdb.hpp"
  19. #include "log.hpp"
  20. #include "map.hpp"
  21. #include "mob.hpp"
  22. #include "party.hpp"
  23. #include "pc.hpp"
  24. static int split_exact_quest_time(char* modif_p, int* day, int* hour, int* minute, int *second);
  25. const std::string QuestDatabase::getDefaultLocation() {
  26. return std::string(db_path) + "/quest_db.yml";
  27. }
  28. /**
  29. * Reads and parses an entry from the quest_db.
  30. * @param node: YAML node containing the entry.
  31. * @return count of successfully parsed rows
  32. */
  33. uint64 QuestDatabase::parseBodyNode(const ryml::NodeRef& node) {
  34. uint32 quest_id;
  35. if (!this->asUInt32(node, "Id", quest_id))
  36. return 0;
  37. std::shared_ptr<s_quest_db> quest = this->find(quest_id);
  38. bool exists = quest != nullptr;
  39. if (!exists) {
  40. if (!this->nodesExist(node, { "Title" }))
  41. return 0;
  42. quest = std::make_shared<s_quest_db>();
  43. quest->id = quest_id;
  44. }
  45. if (this->nodeExists(node, "Title")) {
  46. std::string name;
  47. if (!this->asString(node, "Title", name))
  48. return 0;
  49. quest->name = name;
  50. }
  51. if (this->nodeExists(node, "TimeLimit")) {
  52. std::string time;
  53. if (!this->asString(node, "TimeLimit", time))
  54. return 0;
  55. if (time.find("+") != std::string::npos) {
  56. double timediff = solve_time(const_cast<char *>(time.c_str()));
  57. if (timediff <= 0) {
  58. this->invalidWarning(node["TimeLimit"], "Incorrect TimeLimit format %s given, skipping.\n", time.c_str());
  59. return 0;
  60. }
  61. quest->time = static_cast<time_t>(timediff);
  62. }
  63. else {// '+' not found, set to specific time
  64. int32 day, hour, minute, second;
  65. if (split_exact_quest_time(const_cast<char *>(time.c_str()), &day, &hour, &minute, &second) == 0) {
  66. this->invalidWarning(node["TimeLimit"], "Incorrect TimeLimit format %s given, skipping.\n", time.c_str());
  67. return 0;
  68. }
  69. quest->time = day * 86400 + hour * 3600 + minute * 60 + second;
  70. quest->time_at = true;
  71. }
  72. } else {
  73. if (!exists) {
  74. quest->time = 0;
  75. quest->time_at = false;
  76. }
  77. }
  78. if (this->nodeExists(node, "Targets")) {
  79. const auto& targets = node["Targets"];
  80. for (const auto& targetNode : targets) {
  81. if (quest->objectives.size() >= MAX_QUEST_OBJECTIVES) {
  82. this->invalidWarning(targetNode, "Targets list exceeds the maximum of %d, skipping.\n", MAX_QUEST_OBJECTIVES);
  83. return 0;
  84. }
  85. if (!this->nodeExists(targetNode, "Mob") && !this->nodeExists(targetNode, "Id")) {
  86. this->invalidWarning(targetNode, "Missing Target 'Mob' or 'Id', skipping.\n");
  87. return 0;
  88. }
  89. std::shared_ptr<s_quest_objective> target;
  90. std::vector<std::shared_ptr<s_quest_objective>>::iterator it;
  91. uint16 index = 0, mob_id = 0;
  92. if (this->nodeExists(targetNode, "Mob")) {
  93. std::string mob_name;
  94. if (!this->asString(targetNode, "Mob", mob_name))
  95. return 0;
  96. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname(mob_name.c_str());
  97. if (!mob) {
  98. this->invalidWarning(targetNode["Mob"], "Mob %s does not exist, skipping.\n", mob_name.c_str());
  99. return 0;
  100. }
  101. mob_id = mob->id;
  102. it = std::find_if(quest->objectives.begin(), quest->objectives.end(), [&](std::shared_ptr<s_quest_objective> const &v) {
  103. return (*v).mob_id == mob_id;
  104. });
  105. }
  106. else {
  107. if (!this->asUInt16(targetNode, "Id", index)) {
  108. this->invalidWarning(targetNode, "Missing 'Id', skipping.\n");
  109. return 0;
  110. }
  111. if (index == 0) {
  112. this->invalidWarning(targetNode["Id"], "'Id' can't be 0, skipping.\n");
  113. return 0;
  114. }
  115. it = std::find_if(quest->objectives.begin(), quest->objectives.end(), [&](std::shared_ptr<s_quest_objective> const &v) {
  116. return (*v).index == index;
  117. });
  118. }
  119. if (it != quest->objectives.end())
  120. target = (*it);
  121. else
  122. target = nullptr;
  123. bool targetExists = target != nullptr;
  124. if (!targetExists) {
  125. if (!this->nodeExists(targetNode, "Count")) {
  126. this->invalidWarning(targetNode["Count"], "Targets has no Count value specified, skipping.\n");
  127. return 0;
  128. }
  129. if (!this->nodeExists(targetNode, "Mob") && !this->nodeExists(targetNode, "MinLevel") && !this->nodeExists(targetNode, "MaxLevel") &&
  130. !this->nodeExists(targetNode, "Race") && !this->nodeExists(targetNode, "Size") && !this->nodeExists(targetNode, "Element") &&
  131. !this->nodeExists(targetNode, "Location") && !this->nodeExists(targetNode, "MapName")) {
  132. this->invalidWarning(targetNode, "Targets is missing required field, skipping.\n");
  133. return 0;
  134. }
  135. target = std::make_shared<s_quest_objective>();
  136. target->index = index;
  137. target->mob_id = mob_id;
  138. target->min_level = 0;
  139. target->max_level = 0;
  140. target->race = RC_ALL;
  141. target->size = SZ_ALL;
  142. target->element = ELE_ALL;
  143. target->mapid = -1;
  144. target->map_name = "";
  145. }
  146. if (!this->nodeExists(targetNode, "Mob")) {
  147. if (this->nodeExists(targetNode, "MinLevel")) {
  148. uint16 level;
  149. if (!this->asUInt16(targetNode, "MinLevel", level))
  150. return 0;
  151. target->min_level = level;
  152. }
  153. if (this->nodeExists(targetNode, "MaxLevel")) {
  154. uint16 level;
  155. if (!this->asUInt16(targetNode, "MaxLevel", level))
  156. return 0;
  157. if (target->min_level > level) {
  158. this->invalidWarning(targetNode["MaxLevel"], "%d's MinLevel is greater than MaxLevel. Defaulting MaxLevel to %d.\n", target->min_level, MAX_LEVEL);
  159. level = MAX_LEVEL;
  160. }
  161. target->max_level = level;
  162. }
  163. if (this->nodeExists(targetNode, "Race")) {
  164. std::string race;
  165. if (!this->asString(targetNode, "Race", race))
  166. return 0;
  167. std::string race_constant = "RC_" + race;
  168. int64 constant;
  169. if (!script_get_constant(race_constant.c_str(), &constant)) {
  170. this->invalidWarning(targetNode["Race"], "Invalid race %s, skipping.\n", race.c_str());
  171. return 0;
  172. }
  173. if (constant < RC_FORMLESS || constant > RC_ALL || constant == RC_PLAYER_HUMAN || constant == RC_PLAYER_DORAM) {
  174. this->invalidWarning(targetNode["Race"], "Unsupported race %s, skipping.\n", race.c_str());
  175. return 0;
  176. }
  177. target->race = static_cast<e_race>(constant);
  178. }
  179. if (this->nodeExists(targetNode, "Size")) {
  180. std::string size_;
  181. if (!this->asString(targetNode, "Size", size_))
  182. return 0;
  183. std::string size_constant = "Size_" + size_;
  184. int64 constant;
  185. if (!script_get_constant(size_constant.c_str(), &constant)) {
  186. this->invalidWarning(targetNode["Size"], "Invalid size type %s, skipping.\n", size_.c_str());
  187. return 0;
  188. }
  189. if (constant < SZ_SMALL || constant > SZ_ALL) {
  190. this->invalidWarning(targetNode["size"], "Unsupported size %s, skipping.\n", size_.c_str());
  191. return 0;
  192. }
  193. target->size = static_cast<e_size>(constant);
  194. }
  195. if (this->nodeExists(targetNode, "Element")) {
  196. std::string element;
  197. if (!this->asString(targetNode, "Element", element))
  198. return 0;
  199. std::string element_constant = "Ele_" + element;
  200. int64 constant;
  201. if (!script_get_constant(element_constant.c_str(), &constant)) {
  202. this->invalidWarning(targetNode["Element"], "Invalid element %s, skipping.\n", element.c_str());
  203. return 0;
  204. }
  205. if (constant < ELE_NEUTRAL || constant > ELE_ALL) {
  206. this->invalidWarning(targetNode["Element"], "Unsupported element %s, skipping.\n", element.c_str());
  207. return 0;
  208. }
  209. target->element = static_cast<e_element>(constant);
  210. }
  211. if (this->nodeExists(targetNode, "Location")) {
  212. std::string location;
  213. if (!this->asString(targetNode, "Location", location))
  214. return 0;
  215. uint16 mapindex = mapindex_name2idx(location.c_str(), nullptr);
  216. if (mapindex == 0) {
  217. this->invalidWarning(targetNode["Location"], "Map \"%s\" not found.\n", location.c_str());
  218. return 0;
  219. }
  220. target->mapid = map_mapindex2mapid(mapindex);
  221. }
  222. if (this->nodeExists(targetNode, "MapName")) {
  223. std::string map_name;
  224. if (!this->asString(targetNode, "MapName", map_name))
  225. return 0;
  226. target->map_name = map_name;
  227. }
  228. // if max_level is set, min_level is 1
  229. if (target->min_level == 0 && target->max_level > 0)
  230. target->min_level = 1;
  231. }
  232. if (this->nodeExists(targetNode, "Count")) {
  233. uint16 count;
  234. if (!this->asUInt16(targetNode, "Count", count))
  235. return 0;
  236. target->count = count;
  237. }
  238. quest->objectives.push_back(target);
  239. }
  240. }
  241. if (this->nodeExists(node, "Drops")) {
  242. const auto& drops = node["Drops"];
  243. for (const auto& dropNode : drops) {
  244. uint32 mob_id = 0; // Can be 0 which means all monsters
  245. if (this->nodeExists(dropNode, "Mob")) {
  246. std::string mob_name;
  247. if (!this->asString(dropNode, "Mob", mob_name))
  248. return 0;
  249. std::shared_ptr<s_mob_db> mob = mobdb_search_aegisname(mob_name.c_str());
  250. if (!mob) {
  251. this->invalidWarning(dropNode["Mob"], "Mob %s does not exist, skipping.\n", mob_name.c_str());
  252. continue;
  253. }
  254. mob_id = mob->id;
  255. }
  256. //std::shared_ptr<s_quest_dropitem> target = util::vector_find(quest->dropitem, mob_id);
  257. std::shared_ptr<s_quest_dropitem> target;
  258. std::vector<std::shared_ptr<s_quest_dropitem>>::iterator it = std::find_if(quest->dropitem.begin(), quest->dropitem.end(), [&](std::shared_ptr<s_quest_dropitem> const &v) {
  259. return (*v).mob_id == mob_id;
  260. });
  261. if (it != quest->dropitem.end())
  262. target = (*it);
  263. else
  264. target = nullptr;
  265. bool targetExists = target != nullptr;
  266. if (!targetExists) {
  267. if (!this->nodeExists(dropNode, "Item")) {
  268. this->invalidWarning(dropNode["Item"], "Drops has no Item value specified, skipping.\n");
  269. continue;
  270. }
  271. if (!this->nodeExists(dropNode, "Rate")) {
  272. this->invalidWarning(dropNode["Item"], "Drops has no Rate value specified, skipping.\n");
  273. continue;
  274. }
  275. target = std::make_shared<s_quest_dropitem>();
  276. target->mob_id = mob_id;
  277. }
  278. if (this->nodeExists(dropNode, "Item")) {
  279. std::string item_name;
  280. if (!this->asString(dropNode, "Item", item_name))
  281. return 0;
  282. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  283. if (!item) {
  284. this->invalidWarning(dropNode["Item"], "Item %s does not exist, skipping.\n", item_name.c_str());
  285. continue;
  286. }
  287. target->nameid = item->nameid;
  288. }
  289. if (this->nodeExists(dropNode, "Count")) {
  290. uint16 count;
  291. if (!this->asUInt16(dropNode, "Count", count))
  292. return 0;
  293. if (!itemdb_isstackable(target->nameid)) {
  294. this->invalidWarning(dropNode["Count"], "Item %s is not stackable, capping to 1.\n", itemdb_name(target->nameid));
  295. count = 1;
  296. }
  297. target->count = count;
  298. } else {
  299. if (!targetExists)
  300. target->count = 1;
  301. }
  302. if (this->nodeExists(dropNode, "Rate")) {
  303. uint16 rate;
  304. if (!this->asUInt16(dropNode, "Rate", rate))
  305. return 0;
  306. target->rate = rate;
  307. }
  308. quest->dropitem.push_back(target);
  309. }
  310. }
  311. if (!exists)
  312. this->put(quest_id, quest);
  313. return 1;
  314. }
  315. static int split_exact_quest_time(char* modif_p, int* day, int* hour, int* minute, int *second) {
  316. int d = -1, h = -1, mn = -1, s = -1;
  317. nullpo_retr(0, modif_p);
  318. while (modif_p[0] != '\0') {
  319. int value = atoi(modif_p);
  320. if (modif_p[0] == '-' || modif_p[0] == '+')
  321. modif_p++;
  322. while (modif_p[0] >= '0' && modif_p[0] <= '9')
  323. modif_p++;
  324. if (modif_p[0] == 's') {
  325. s = value;
  326. modif_p++;
  327. } else if (modif_p[0] == 'm' && modif_p[1] == 'n') {
  328. mn = value;
  329. modif_p = modif_p + 2;
  330. } else if (modif_p[0] == 'h') {
  331. h = value;
  332. modif_p++;
  333. } else if (modif_p[0] == 'd' || modif_p[0] == 'j') {
  334. d = value;
  335. modif_p++;
  336. } else if (modif_p[0] != '\0') {
  337. modif_p++;
  338. }
  339. }
  340. if (h < 0 || h > 23 || mn > 59 || s > 59) // hour is required
  341. return 0;
  342. *day = max(0,d);
  343. *hour = h;
  344. *minute = max(0,mn);
  345. *second = max(0,s);
  346. return 1;
  347. }
  348. /**
  349. * Searches a quest by ID.
  350. * @param quest_id : ID to lookup
  351. * @return Quest entry or nullptr on failure
  352. */
  353. std::shared_ptr<s_quest_db> quest_search(int quest_id)
  354. {
  355. auto quest = quest_db.find(quest_id);
  356. if (!quest)
  357. return nullptr;
  358. return quest;
  359. }
  360. /**
  361. * Sends quest info to the player on login.
  362. * @param sd : Player's data
  363. * @return 0 in case of success, nonzero otherwise (i.e. the player has no quests)
  364. */
  365. int quest_pc_login(struct map_session_data *sd)
  366. {
  367. if (!sd->avail_quests)
  368. return 1;
  369. clif_quest_send_list(sd);
  370. #if PACKETVER < 20141022
  371. clif_quest_send_mission(sd);
  372. //@TODO[Haru]: Is this necessary? Does quest_send_mission not take care of this?
  373. for (int i = 0; i < sd->avail_quests; i++)
  374. clif_quest_update_objective(sd, &sd->quest_log[i]);
  375. #endif
  376. return 0;
  377. }
  378. /**
  379. * Determine a quest's time limit.
  380. * @param qi: Quest data
  381. * @return Time limit value
  382. */
  383. static time_t quest_time(std::shared_ptr<s_quest_db> qi)
  384. {
  385. if (!qi || qi->time < 0)
  386. return 0;
  387. if (!qi->time_at && qi->time > 0)
  388. return time(nullptr) + qi->time;
  389. else if (qi->time_at) {
  390. time_t t = time(nullptr);
  391. struct tm *lt = localtime(&t);
  392. uint32 time_today = lt->tm_hour * 3600 + lt->tm_min * 60 + lt->tm_sec;
  393. if (time_today < (qi->time % 86400))
  394. return static_cast<time_t>(t + qi->time - time_today);
  395. else // Carry over to the next day
  396. return static_cast<time_t>(t + 86400 + qi->time - time_today);
  397. }
  398. return 0;
  399. }
  400. /**
  401. * Adds a quest to the player's list.
  402. * New quest will be added as Q_ACTIVE.
  403. * @param sd : Player's data
  404. * @param quest_id : ID of the quest to add.
  405. * @return 0 in case of success, nonzero otherwise
  406. */
  407. int quest_add(struct map_session_data *sd, int quest_id)
  408. {
  409. std::shared_ptr<s_quest_db> qi = quest_search(quest_id);
  410. if (!qi) {
  411. ShowError("quest_add: quest %d not found in DB.\n", quest_id);
  412. return -1;
  413. }
  414. if (quest_check(sd, quest_id, HAVEQUEST) >= 0) {
  415. ShowError("quest_add: Character %d already has quest %d.\n", sd->status.char_id, quest_id);
  416. return -1;
  417. }
  418. int n = sd->avail_quests; //Insertion point
  419. sd->num_quests++;
  420. sd->avail_quests++;
  421. RECREATE(sd->quest_log, struct quest, sd->num_quests);
  422. //The character has some completed quests, make room before them so that they will stay at the end of the array
  423. if (sd->avail_quests != sd->num_quests)
  424. memmove(&sd->quest_log[n + 1], &sd->quest_log[n], sizeof(struct quest) * (sd->num_quests-sd->avail_quests));
  425. sd->quest_log[n] = {};
  426. sd->quest_log[n].quest_id = qi->id;
  427. sd->quest_log[n].time = (uint32)quest_time(qi);
  428. sd->quest_log[n].state = Q_ACTIVE;
  429. sd->save_quest = true;
  430. clif_quest_add(sd, &sd->quest_log[n]);
  431. clif_quest_update_objective(sd, &sd->quest_log[n]);
  432. if( save_settings&CHARSAVE_QUEST )
  433. chrif_save(sd, CSAVE_NORMAL);
  434. return 0;
  435. }
  436. /**
  437. * Replaces a quest in a player's list with another one.
  438. * @param sd : Player's data
  439. * @param qid1 : Current quest to replace
  440. * @param qid2 : New quest to add
  441. * @return 0 in case of success, nonzero otherwise
  442. */
  443. int quest_change(struct map_session_data *sd, int qid1, int qid2)
  444. {
  445. std::shared_ptr<s_quest_db> qi = quest_search(qid2);
  446. if (!qi) {
  447. ShowError("quest_change: quest %d not found in DB.\n", qid2);
  448. return -1;
  449. }
  450. if (quest_check(sd, qid2, HAVEQUEST) >= 0) {
  451. ShowError("quest_change: Character %d already has quest %d.\n", sd->status.char_id, qid2);
  452. return -1;
  453. }
  454. if (quest_check(sd, qid1, HAVEQUEST) < 0) {
  455. ShowError("quest_change: Character %d doesn't have quest %d.\n", sd->status.char_id, qid1);
  456. return -1;
  457. }
  458. int i;
  459. ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == qid1);
  460. if (i == sd->avail_quests) {
  461. ShowError("quest_change: Character %d has completed quest %d.\n", sd->status.char_id, qid1);
  462. return -1;
  463. }
  464. sd->quest_log[i] = {};
  465. sd->quest_log[i].quest_id = qi->id;
  466. sd->quest_log[i].time = (uint32)quest_time(qi);
  467. sd->quest_log[i].state = Q_ACTIVE;
  468. sd->save_quest = true;
  469. clif_quest_delete(sd, qid1);
  470. clif_quest_add(sd, &sd->quest_log[i]);
  471. clif_quest_update_objective(sd, &sd->quest_log[i]);
  472. if( save_settings&CHARSAVE_QUEST )
  473. chrif_save(sd, CSAVE_NORMAL);
  474. return 0;
  475. }
  476. /**
  477. * Removes a quest from a player's list
  478. * @param sd : Player's data
  479. * @param quest_id : ID of the quest to remove
  480. * @return 0 in case of success, nonzero otherwise
  481. */
  482. int quest_delete(struct map_session_data *sd, int quest_id)
  483. {
  484. int i;
  485. //Search for quest
  486. ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
  487. if (i == sd->num_quests) {
  488. ShowError("quest_delete: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
  489. return -1;
  490. }
  491. if (sd->quest_log[i].state != Q_COMPLETE)
  492. sd->avail_quests--;
  493. if (i < --sd->num_quests) //Compact the array
  494. memmove(&sd->quest_log[i], &sd->quest_log[i + 1], sizeof(struct quest) * (sd->num_quests - i));
  495. if (sd->num_quests == 0) {
  496. aFree(sd->quest_log);
  497. sd->quest_log = NULL;
  498. } else
  499. RECREATE(sd->quest_log, struct quest, sd->num_quests);
  500. sd->save_quest = true;
  501. clif_quest_delete(sd, quest_id);
  502. if( save_settings&CHARSAVE_QUEST )
  503. chrif_save(sd, CSAVE_NORMAL);
  504. return 0;
  505. }
  506. /**
  507. * Map iterator subroutine to update quest objectives for a party after killing a monster.
  508. * @see map_foreachinrange
  509. * @param ap : Argument list, expecting:
  510. * int Party ID
  511. * int Mob ID
  512. * int Mob Level
  513. * int Mob Race
  514. * int Mob Size
  515. * int Mob Element
  516. */
  517. int quest_update_objective_sub(struct block_list *bl, va_list ap)
  518. {
  519. nullpo_ret(bl);
  520. struct map_session_data *sd = BL_CAST(BL_PC, bl);
  521. nullpo_ret(sd);
  522. if( !sd->avail_quests )
  523. return 0;
  524. if( sd->status.party_id != va_arg(ap, int))
  525. return 0;
  526. quest_update_objective(sd, va_arg(ap, struct mob_data*));
  527. return 1;
  528. }
  529. /**
  530. * Updates the quest objectives for a character after killing a monster, including the handling of quest-granted drops.
  531. * @param sd: Character's data
  532. * @param mob_id: Monster ID
  533. * @param mob_level: Monster Level
  534. * @param mob_race: Monster Race
  535. * @param mob_size: Monster Size
  536. * @param mob_element: Monster Element
  537. */
  538. void quest_update_objective(struct map_session_data *sd, struct mob_data* md)
  539. {
  540. nullpo_retv(sd);
  541. for (int i = 0; i < sd->avail_quests; i++) {
  542. if (sd->quest_log[i].state == Q_COMPLETE) // Skip complete quests
  543. continue;
  544. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  545. if (!qi)
  546. continue;
  547. // Process quest objectives
  548. for (int j = 0; j < qi->objectives.size(); j++) {
  549. uint8 objective_check = 0; // Must pass all 6 checks
  550. if (qi->objectives[j]->mob_id == md->mob_id)
  551. objective_check = 6;
  552. else if (qi->objectives[j]->mob_id == 0) {
  553. if (qi->objectives[j]->min_level == 0 || qi->objectives[j]->min_level <= md->level)
  554. objective_check++;
  555. if (qi->objectives[j]->max_level == 0 || qi->objectives[j]->max_level >= md->level)
  556. objective_check++;
  557. if (qi->objectives[j]->race == RC_ALL || qi->objectives[j]->race == md->status.race)
  558. objective_check++;
  559. if (qi->objectives[j]->size == SZ_ALL || qi->objectives[j]->size == md->status.size)
  560. objective_check++;
  561. if (qi->objectives[j]->element == ELE_ALL || qi->objectives[j]->element == md->status.def_ele)
  562. objective_check++;
  563. if (qi->objectives[j]->mapid < 0 || (qi->objectives[j]->mapid == sd->bl.m && md->spawn != nullptr))
  564. objective_check++;
  565. else if (qi->objectives[j]->mapid >= 0) {
  566. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  567. if (mapdata->instance_id && mapdata->instance_src_map == qi->objectives[j]->mapid)
  568. objective_check++;
  569. }
  570. }
  571. if (objective_check == 6 && sd->quest_log[i].count[j] < qi->objectives[j]->count) {
  572. sd->quest_log[i].count[j]++;
  573. sd->save_quest = true;
  574. clif_quest_update_objective(sd, &sd->quest_log[i]);
  575. }
  576. }
  577. // Process quest-granted extra drop bonuses
  578. for (const auto &it : qi->dropitem) {
  579. if (it->mob_id != 0 && it->mob_id != md->mob_id)
  580. continue;
  581. if (it->rate < 10000 && rnd()%10000 >= it->rate)
  582. continue; // TODO: Should this be affected by server rates?
  583. if (!itemdb_exists(it->nameid))
  584. continue;
  585. struct item entry = {};
  586. entry.nameid = it->nameid;
  587. entry.identify = itemdb_isidentified(it->nameid);
  588. entry.amount = it->count;
  589. //#ifdef BOUND_ITEMS
  590. // entry.bound = it->bound;
  591. //#endif
  592. // if (it.isGUID)
  593. // item.unique_id = pc_generate_unique_id(sd);
  594. e_additem_result result;
  595. if ((result = pc_additem(sd, &entry, 1, LOG_TYPE_QUEST)) != ADDITEM_SUCCESS) // Failed to obtain the item
  596. clif_additem(sd, 0, 0, result);
  597. // else if (it.isAnnounced || itemdb_exists(it.nameid)->flag.broadcast)
  598. // intif_broadcast_obtain_special_item(sd, it.nameid, it.mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  599. }
  600. }
  601. pc_show_questinfo(sd);
  602. }
  603. /**
  604. * Updates a quest's state.
  605. * Only status of active and inactive quests can be updated. Completed quests can't (for now).
  606. * @param sd : Character's data
  607. * @param quest_id : Quest ID to update
  608. * @param qs : New quest state
  609. * @return 0 in case of success, nonzero otherwise
  610. * @author [Inkfish]
  611. */
  612. int quest_update_status(struct map_session_data *sd, int quest_id, e_quest_state status)
  613. {
  614. int i;
  615. ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
  616. if (i == sd->avail_quests) {
  617. ShowError("quest_update_status: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
  618. return -1;
  619. }
  620. sd->quest_log[i].state = status;
  621. sd->save_quest = true;
  622. if (status < Q_COMPLETE) {
  623. clif_quest_update_status(sd, quest_id, status == Q_ACTIVE ? true : false);
  624. return 0;
  625. }
  626. // The quest is complete, so it needs to be moved to the completed quests block at the end of the array.
  627. if (i < (--sd->avail_quests)) {
  628. struct quest tmp_quest;
  629. memcpy(&tmp_quest, &sd->quest_log[i], sizeof(struct quest));
  630. memcpy(&sd->quest_log[i], &sd->quest_log[sd->avail_quests], sizeof(struct quest));
  631. memcpy(&sd->quest_log[sd->avail_quests], &tmp_quest, sizeof(struct quest));
  632. }
  633. clif_quest_delete(sd, quest_id);
  634. if (save_settings&CHARSAVE_QUEST)
  635. chrif_save(sd, CSAVE_NORMAL);
  636. return 0;
  637. }
  638. /**
  639. * Queries quest information for a character.
  640. * @param sd : Character's data
  641. * @param quest_id : Quest ID
  642. * @param type : Check type
  643. * @return -1 if the quest was not found, otherwise it depends on the type:
  644. * HAVEQUEST: The quest's state
  645. * PLAYTIME: 2 if the quest's timeout has expired
  646. * 1 if the quest was completed
  647. * 0 otherwise
  648. * HUNTING: 2 if the quest has not been marked as completed yet, and its objectives have been fulfilled
  649. * 1 if the quest's timeout has expired
  650. * 0 otherwise
  651. */
  652. int quest_check(struct map_session_data *sd, int quest_id, e_quest_check_type type)
  653. {
  654. int i;
  655. ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
  656. if (i == sd->num_quests)
  657. return -1;
  658. switch (type) {
  659. case HAVEQUEST:
  660. if (sd->quest_log[i].state == Q_INACTIVE) // Player has the quest but it's in the inactive state; send it as Q_ACTIVE.
  661. return 1;
  662. return sd->quest_log[i].state;
  663. case PLAYTIME:
  664. return (sd->quest_log[i].time < (unsigned int)time(nullptr) ? 2 : sd->quest_log[i].state == Q_COMPLETE ? 1 : 0);
  665. case HUNTING:
  666. if (sd->quest_log[i].state == Q_INACTIVE || sd->quest_log[i].state == Q_ACTIVE) {
  667. int j;
  668. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  669. ARR_FIND(0, qi->objectives.size(), j, sd->quest_log[i].count[j] < qi->objectives[j]->count);
  670. if (j == qi->objectives.size())
  671. return 2;
  672. if (sd->quest_log[i].time < (unsigned int)time(nullptr))
  673. return 1;
  674. }
  675. return 0;
  676. default:
  677. ShowError("quest_check_quest: Unknown parameter %d",type);
  678. break;
  679. }
  680. return -1;
  681. }
  682. /**
  683. * Map iterator to ensures a player has no invalid quest log entries.
  684. * Any entries that are no longer in the db are removed.
  685. * @see map_foreachpc
  686. * @param sd : Character's data
  687. * @param ap : Ignored
  688. */
  689. static int quest_reload_check_sub(struct map_session_data *sd, va_list ap)
  690. {
  691. nullpo_ret(sd);
  692. int i, j = 0;
  693. for (i = 0; i < sd->num_quests; i++) {
  694. std::shared_ptr<s_quest_db> qi = quest_search(sd->quest_log[i].quest_id);
  695. if (!qi) { //Remove no longer existing entries
  696. if (sd->quest_log[i].state != Q_COMPLETE) //And inform the client if necessary
  697. clif_quest_delete(sd, sd->quest_log[i].quest_id);
  698. continue;
  699. }
  700. if (i != j) {
  701. //Move entries if there's a gap to fill
  702. memcpy(&sd->quest_log[j], &sd->quest_log[i], sizeof(struct quest));
  703. }
  704. j++;
  705. }
  706. sd->num_quests = j;
  707. ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].state == Q_COMPLETE);
  708. sd->avail_quests = i;
  709. return 1;
  710. }
  711. bool QuestDatabase::reload() {
  712. if (!TypesafeYamlDatabase::reload())
  713. return false;
  714. // Update quest data for players, to ensure no entries about removed quests are left over.
  715. map_foreachpc(&quest_reload_check_sub);
  716. return true;
  717. }
  718. QuestDatabase quest_db;
  719. /**
  720. * Initializes the quest interface.
  721. */
  722. void do_init_quest(void)
  723. {
  724. quest_db.load();
  725. }
  726. /**
  727. * Finalizes the quest interface before shutdown.
  728. */
  729. void do_final_quest(void)
  730. {
  731. quest_db.clear();
  732. }