instance.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "instance.hpp"
  4. #include <stdlib.h>
  5. #include <yaml-cpp/yaml.h>
  6. #include "../common/cbasetypes.hpp"
  7. #include "../common/db.hpp"
  8. #include "../common/ers.hpp" // ers_destroy
  9. #include "../common/malloc.hpp"
  10. #include "../common/nullpo.hpp"
  11. #include "../common/showmsg.hpp"
  12. #include "../common/socket.hpp"
  13. #include "../common/strlib.hpp"
  14. #include "../common/timer.hpp"
  15. #include "../common/utilities.hpp"
  16. #include "clan.hpp"
  17. #include "clif.hpp"
  18. #include "guild.hpp"
  19. #include "map.hpp"
  20. #include "npc.hpp"
  21. #include "party.hpp"
  22. #include "pc.hpp"
  23. using namespace rathena;
  24. /// Instance Idle Queue data
  25. struct s_instance_wait {
  26. std::deque<int> id;
  27. int timer;
  28. } instance_wait;
  29. #define INSTANCE_INTERVAL 60000 // Interval used to check when an instance is to be destroyed (ms)
  30. int16 instance_start = 0; // Instance MapID start
  31. int instance_count = 1; // Total created instances
  32. std::unordered_map<int, std::shared_ptr<s_instance_data>> instances;
  33. const std::string InstanceDatabase::getDefaultLocation() {
  34. return std::string(db_path) + "/instance_db.yml";
  35. }
  36. /**
  37. * Reads and parses an entry from the instance_db.
  38. * @param node: YAML node containing the entry.
  39. * @return count of successfully parsed rows
  40. */
  41. uint64 InstanceDatabase::parseBodyNode(const YAML::Node &node) {
  42. int32 instance_id = 0;
  43. if (!this->asInt32(node, "Id", instance_id))
  44. return 0;
  45. if (instance_id <= 0) {
  46. this->invalidWarning(node, "Instance Id is invalid. Valid range 1~%d, skipping.\n", INT_MAX);
  47. return 0;
  48. }
  49. std::shared_ptr<s_instance_db> instance = this->find(instance_id);
  50. bool exists = instance != nullptr;
  51. if (!exists) {
  52. if (!this->nodesExist(node, { "Name", "Enter" }))
  53. return 0;
  54. instance = std::make_shared<s_instance_db>();
  55. instance->id = instance_id;
  56. }
  57. if (this->nodeExists(node, "Name")) {
  58. std::string name;
  59. if (!this->asString(node, "Name", name))
  60. return 0;
  61. for (const auto &instance : instance_db) {
  62. if (instance.second->name.compare(name) == 0) {
  63. this->invalidWarning(node["Name"], "Instance name %s already exists, skipping.\n", name.c_str());
  64. return 0;
  65. }
  66. }
  67. instance->name = name;
  68. }
  69. if (this->nodeExists(node, "TimeLimit")) {
  70. uint32 limit;
  71. if (!this->asUInt32(node, "TimeLimit", limit))
  72. return 0;
  73. instance->limit = limit;
  74. } else {
  75. if (!exists)
  76. instance->limit = 3600;
  77. }
  78. if (this->nodeExists(node, "IdleTimeOut")) {
  79. uint32 idle;
  80. if (!this->asUInt32(node, "IdleTimeOut", idle))
  81. return 0;
  82. instance->timeout = idle;
  83. } else {
  84. if (!exists)
  85. instance->timeout = 300;
  86. }
  87. if (this->nodeExists(node, "Destroyable")) {
  88. bool destroy;
  89. if (!this->asBool(node, "Destroyable", destroy))
  90. return 0;
  91. instance->destroyable = destroy;
  92. } else {
  93. if (!exists)
  94. instance->destroyable = true;
  95. }
  96. if (this->nodeExists(node, "Enter")) {
  97. const YAML::Node &enterNode = node["Enter"];
  98. if (!this->nodesExist(enterNode, { "Map", "X", "Y" }))
  99. return 0;
  100. if (this->nodeExists(enterNode, "Map")) {
  101. std::string map;
  102. if (!this->asString(enterNode, "Map", map))
  103. return 0;
  104. int16 m = map_mapname2mapid(map.c_str());
  105. if (m == -1) {
  106. this->invalidWarning(enterNode["Map"], "Map %s is not a valid map, skipping.\n", map.c_str());
  107. return 0;
  108. }
  109. instance->enter.map = m;
  110. }
  111. if (this->nodeExists(enterNode, "X")) {
  112. int16 x;
  113. if (!this->asInt16(enterNode, "X", x))
  114. return 0;
  115. instance->enter.x = x;
  116. }
  117. if (this->nodeExists(enterNode, "Y")) {
  118. int16 y;
  119. if (!this->asInt16(enterNode, "Y", y))
  120. return 0;
  121. instance->enter.y = y;
  122. }
  123. }
  124. if (this->nodeExists(node, "AdditionalMaps")) {
  125. const YAML::Node &mapNode = node["AdditionalMaps"];
  126. for (const auto &mapIt : mapNode) {
  127. std::string map = mapIt.first.as<std::string>();
  128. int16 m = map_mapname2mapid(map.c_str());
  129. if (m == instance->enter.map) {
  130. this->invalidWarning(mapNode, "Additional Map %s is already listed as the EnterMap.\n", map.c_str());
  131. continue;
  132. }
  133. if (m == -1) {
  134. this->invalidWarning(mapNode, "Additional Map %s is not a valid map, skipping.\n", map.c_str());
  135. return 0;
  136. }
  137. bool active;
  138. if (!this->asBool(mapNode, map, active))
  139. return 0;
  140. if (active)
  141. instance->maplist.push_back(m);
  142. else
  143. util::vector_erase_if_exists(instance->maplist, m);
  144. }
  145. }
  146. if (!exists)
  147. this->put(instance_id, instance);
  148. return 1;
  149. }
  150. InstanceDatabase instance_db;
  151. /**
  152. * Searches for an instance name in the database
  153. * @param instance_name: Instance to search for
  154. * @return shared_ptr of instance or nullptr on failure
  155. */
  156. std::shared_ptr<s_instance_db> instance_search_db_name(const char *instance_name)
  157. {
  158. for (const auto &it : instance_db) {
  159. if (!strcmp(it.second->name.c_str(), instance_name))
  160. return it.second;
  161. }
  162. return nullptr;
  163. }
  164. /**
  165. * Search for a sd of an Instance
  166. * @param instance_id: Instance ID
  167. * @param sd: Pointer to player data
  168. * @param target: Target display type
  169. */
  170. void instance_getsd(int instance_id, struct map_session_data *&sd, enum send_target *target) {
  171. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  172. if (!idata) {
  173. sd = nullptr;
  174. return;
  175. }
  176. switch(idata->mode) {
  177. case IM_NONE:
  178. sd = nullptr;
  179. (*target) = SELF;
  180. break;
  181. case IM_GUILD:
  182. sd = guild_getavailablesd(guild_search(idata->owner_id));
  183. (*target) = GUILD;
  184. break;
  185. case IM_PARTY:
  186. sd = party_getavailablesd(party_search(idata->owner_id));
  187. (*target) = PARTY;
  188. break;
  189. case IM_CHAR:
  190. sd = map_charid2sd(idata->owner_id);
  191. (*target) = SELF;
  192. break;
  193. case IM_CLAN:
  194. sd = clan_getavailablesd(clan_search(idata->owner_id));
  195. (*target) = CLAN;
  196. }
  197. return;
  198. }
  199. /**
  200. * Deletes an instance timer (Destroys instance)
  201. */
  202. static TIMER_FUNC(instance_delete_timer){
  203. instance_destroy(id);
  204. return 0;
  205. }
  206. /**
  207. * Create subscription timer
  208. */
  209. static TIMER_FUNC(instance_subscription_timer){
  210. int instance_id = instance_wait.id[0];
  211. if (instance_id <= 0 || instance_wait.id.empty())
  212. return 0;
  213. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  214. if (!idata)
  215. return 0;
  216. struct map_session_data *sd;
  217. struct party_data *pd;
  218. struct guild *gd;
  219. struct clan *cd;
  220. e_instance_mode mode = idata->mode;
  221. int ret = instance_addmap(instance_id); // Check that maps have been added
  222. switch(mode) {
  223. case IM_NONE:
  224. break;
  225. case IM_CHAR:
  226. if (ret == 0 && (sd = map_charid2sd(idata->owner_id))) // If no maps are created, tell player to wait
  227. clif_instance_changewait(instance_id, 0xffff);
  228. break;
  229. case IM_PARTY:
  230. if (ret == 0 && (pd = party_search(idata->owner_id))) // If no maps are created, tell party to wait
  231. clif_instance_changewait(instance_id, 0xffff);
  232. break;
  233. case IM_GUILD:
  234. if (ret == 0 && (gd = guild_search(idata->owner_id))) // If no maps are created, tell guild to wait
  235. clif_instance_changewait(instance_id, 0xffff);
  236. break;
  237. case IM_CLAN:
  238. if (ret == 0 && (cd = clan_search(idata->owner_id))) // If no maps are created, tell clan to wait
  239. clif_instance_changewait(instance_id, 0xffff);
  240. break;
  241. default:
  242. return 0;
  243. }
  244. instance_wait.id.pop_front();
  245. for (int i = 0; i < instance_wait.id.size(); i++) {
  246. if (idata->state == INSTANCE_IDLE && ((mode == IM_CHAR && sd) || (mode == IM_GUILD && gd) || (mode == IM_PARTY && pd) || (mode == IM_CLAN && cd)))
  247. clif_instance_changewait(instance_id, i + 1);
  248. }
  249. if (!instance_wait.id.empty())
  250. instance_wait.timer = add_timer(gettick() + INSTANCE_INTERVAL, instance_subscription_timer, 0, 0);
  251. else
  252. instance_wait.timer = INVALID_TIMER;
  253. return 0;
  254. }
  255. /**
  256. * Adds timer back to members entering instance
  257. * @param idata: Instance data
  258. * @param instance_id: Instance ID to notify
  259. * @return True on success or false on failure
  260. */
  261. bool instance_startkeeptimer(std::shared_ptr<s_instance_data> idata, int instance_id)
  262. {
  263. // No timer
  264. if (!idata || idata->keep_timer != INVALID_TIMER)
  265. return false;
  266. std::shared_ptr<s_instance_db> db = instance_db.find(idata->id);
  267. if (!db)
  268. return false;
  269. // Add timer
  270. idata->keep_limit = static_cast<unsigned int>(time(nullptr)) + db->limit;
  271. idata->keep_timer = add_timer(gettick() + db->limit * 1000, instance_delete_timer, instance_id, 0);
  272. switch(idata->mode) {
  273. case IM_NONE:
  274. break;
  275. case IM_CHAR:
  276. if (map_charid2sd(idata->owner_id)) // Notify player of the added instance timer
  277. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  278. break;
  279. case IM_PARTY:
  280. if (party_search(idata->owner_id)) // Notify party of the added instance timer
  281. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  282. break;
  283. case IM_GUILD:
  284. if (guild_search(idata->owner_id)) // Notify guild of the added instance timer
  285. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  286. break;
  287. case IM_CLAN:
  288. if (clan_search(idata->owner_id)) // Notify clan of the added instance timer
  289. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  290. break;
  291. default:
  292. return false;
  293. }
  294. return true;
  295. }
  296. /**
  297. * Creates an idle timer for an instance, default is 5 minutes
  298. * @param idata: Instance data
  299. * @param instance_id: Instance ID to notify
  300. * @param True on success or false on failure
  301. */
  302. bool instance_startidletimer(std::shared_ptr<s_instance_data> idata, int instance_id)
  303. {
  304. // No current timer
  305. if (!idata || idata->idle_timer != INVALID_TIMER)
  306. return false;
  307. std::shared_ptr<s_instance_db> db = instance_db.find(idata->id);
  308. if (!db)
  309. return false;
  310. // Add the timer
  311. idata->idle_limit = static_cast<unsigned int>(time(nullptr)) + db->timeout;
  312. idata->idle_timer = add_timer(gettick() + db->timeout * 1000, instance_delete_timer, instance_id, 0);
  313. switch(idata->mode) {
  314. case IM_NONE:
  315. break;
  316. case IM_CHAR:
  317. if (map_charid2sd(idata->owner_id)) // Notify player of added instance timer
  318. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  319. break;
  320. case IM_PARTY:
  321. if (party_search(idata->owner_id)) // Notify party of added instance timer
  322. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  323. break;
  324. case IM_GUILD:
  325. if (guild_search(idata->owner_id)) // Notify guild of added instance timer
  326. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  327. break;
  328. case IM_CLAN:
  329. if (clan_search(idata->owner_id)) // Notify clan of added instance timer
  330. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  331. break;
  332. default:
  333. return false;
  334. }
  335. return true;
  336. }
  337. /**
  338. * Remove the idle timer from an instance
  339. * @param idata: Instace data
  340. * @param instance_id: Instance ID to notify
  341. * @return True on success or false on failure
  342. */
  343. bool instance_stopidletimer(std::shared_ptr<s_instance_data> idata, int instance_id)
  344. {
  345. // No timer
  346. if (!idata || idata->idle_timer == INVALID_TIMER)
  347. return false;
  348. // Delete the timer - Party has returned or instance is destroyed
  349. idata->idle_limit = 0;
  350. delete_timer(idata->idle_timer, instance_delete_timer);
  351. idata->idle_timer = INVALID_TIMER;
  352. switch(idata->mode) {
  353. case IM_NONE:
  354. break;
  355. case IM_CHAR:
  356. if (map_charid2sd(idata->owner_id)) // Notify the player
  357. clif_instance_changestatus(instance_id, IN_NOTIFY, idata->idle_limit);
  358. break;
  359. case IM_PARTY:
  360. if (party_search(idata->owner_id)) // Notify the party
  361. clif_instance_changestatus(instance_id, IN_NOTIFY, idata->idle_limit);
  362. break;
  363. case IM_GUILD:
  364. if (guild_search(idata->owner_id)) // Notify the guild
  365. clif_instance_changestatus(instance_id, IN_NOTIFY, idata->idle_limit);
  366. break;
  367. case IM_CLAN:
  368. if (clan_search(idata->owner_id)) // Notify the clan
  369. clif_instance_changestatus(instance_id, IN_NOTIFY, idata->idle_limit);
  370. break;
  371. default:
  372. return false;
  373. }
  374. return true;
  375. }
  376. /**
  377. * Run the OnInstanceInit events for duplicated NPCs
  378. */
  379. static int instance_npcinit(struct block_list *bl, va_list ap)
  380. {
  381. struct npc_data* nd;
  382. nullpo_retr(0, bl);
  383. nullpo_retr(0, ap);
  384. nullpo_retr(0, nd = (struct npc_data *)bl);
  385. return npc_instanceinit(nd);
  386. }
  387. /**
  388. * Run the OnInstanceDestroy events for duplicated NPCs
  389. */
  390. static int instance_npcdestroy(struct block_list *bl, va_list ap)
  391. {
  392. struct npc_data* nd;
  393. nullpo_retr(0, bl);
  394. nullpo_retr(0, ap);
  395. nullpo_retr(0, nd = (struct npc_data *)bl);
  396. return npc_instancedestroy(nd);
  397. }
  398. /**
  399. * Update instance with new NPC
  400. */
  401. static int instance_addnpc_sub(struct block_list *bl, va_list ap)
  402. {
  403. struct npc_data* nd;
  404. nullpo_retr(0, bl);
  405. nullpo_retr(0, ap);
  406. nullpo_retr(0, nd = (struct npc_data *)bl);
  407. return npc_duplicate4instance(nd, va_arg(ap, int));
  408. }
  409. /**
  410. * Add an NPC to an instance
  411. * @param idata: Instance data
  412. */
  413. void instance_addnpc(std::shared_ptr<s_instance_data> idata)
  414. {
  415. // First add the NPCs
  416. for (const auto &it : idata->map) {
  417. struct map_data *mapdata = map_getmapdata(it.m);
  418. map_foreachinallarea(instance_addnpc_sub, it.src_m, 0, 0, mapdata->xs, mapdata->ys, BL_NPC, it.m);
  419. }
  420. // Now run their OnInstanceInit
  421. for (const auto &it : idata->map) {
  422. struct map_data *mapdata = map_getmapdata(it.m);
  423. map_foreachinallarea(instance_npcinit, it.m, 0, 0, mapdata->xs, mapdata->ys, BL_NPC, it.m);
  424. }
  425. }
  426. /**
  427. * Create an instance
  428. * @param owner_id: Owner block ID
  429. * @param name: Instance name
  430. * @param mode: Instance mode
  431. * @return -4 = no free instances | -3 = already exists | -2 = character/party/guild not found | -1 = invalid type | On success return instance_id
  432. */
  433. int instance_create(int owner_id, const char *name, e_instance_mode mode) {
  434. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  435. if (!db) {
  436. ShowError("instance_create: Unknown instance %s creation was attempted.\n", name);
  437. return -1;
  438. }
  439. struct map_session_data *sd = nullptr;
  440. struct party_data *pd;
  441. struct guild *gd;
  442. struct clan* cd;
  443. switch(mode) {
  444. case IM_NONE:
  445. break;
  446. case IM_CHAR:
  447. if (!(sd = map_charid2sd(owner_id))) {
  448. ShowError("instance_create: Character %d not found for instance '%s'.\n", owner_id, name);
  449. return -2;
  450. }
  451. if (sd->instance_id > 0)
  452. return -3; // Player already instancing
  453. break;
  454. case IM_PARTY:
  455. if (!(pd = party_search(owner_id))) {
  456. ShowError("instance_create: Party %d not found for instance '%s'.\n", owner_id, name);
  457. return -2;
  458. }
  459. if (pd->instance_id > 0)
  460. return -3; // Party already instancing
  461. break;
  462. case IM_GUILD:
  463. if (!(gd = guild_search(owner_id))) {
  464. ShowError("instance_create: Guild %d not found for instance '%s'.\n", owner_id, name);
  465. return -2;
  466. }
  467. if (gd->instance_id > 0)
  468. return -3; // Guild already instancing
  469. break;
  470. case IM_CLAN:
  471. if (!(cd = clan_search(owner_id))) {
  472. ShowError("instance_create: Clan %d not found for instance '%s'.\n", owner_id, name);
  473. return -2;
  474. }
  475. if (cd->instance_id > 0)
  476. return -3; // Clan already instancing
  477. break;
  478. default:
  479. ShowError("instance_create: Unknown mode %u for owner_id %d and name %s.\n", mode, owner_id, name);
  480. return -2;
  481. }
  482. if (instance_count <= 0)
  483. return -4;
  484. int instance_id = instance_count++;
  485. std::shared_ptr<s_instance_data> entry = std::make_shared<s_instance_data>();
  486. entry->id = db->id;
  487. entry->owner_id = owner_id;
  488. entry->mode = mode;
  489. entry->regs.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  490. entry->regs.arrays = nullptr;
  491. instances.insert({ instance_id, entry });
  492. switch(mode) {
  493. case IM_CHAR:
  494. sd->instance_id = instance_id;
  495. break;
  496. case IM_PARTY:
  497. pd->instance_id = instance_id;
  498. int32 i;
  499. ARR_FIND(0, MAX_PARTY, i, pd->party.member[i].leader);
  500. if (i < MAX_PARTY)
  501. sd = map_charid2sd(pd->party.member[i].char_id);
  502. break;
  503. case IM_GUILD:
  504. gd->instance_id = instance_id;
  505. sd = map_charid2sd(gd->member[0].char_id);
  506. break;
  507. case IM_CLAN:
  508. cd->instance_id = instance_id;
  509. break;
  510. }
  511. if (sd != nullptr)
  512. sd->instance_mode = mode;
  513. instance_wait.id.push_back(instance_id);
  514. clif_instance_create(instance_id, instance_wait.id.size());
  515. instance_subscription_timer(0,0,0,0);
  516. ShowInfo("[Instance] Created: %s (%d)\n", name, instance_id);
  517. // Start the instance timer on instance creation
  518. instance_startkeeptimer(entry, instance_id);
  519. return instance_id;
  520. }
  521. /**
  522. * Adds maps to the instance
  523. * @param instance_id: Instance ID to add map to
  524. * @return 0 on failure or map count on success
  525. */
  526. int instance_addmap(int instance_id) {
  527. if (instance_id <= 0)
  528. return 0;
  529. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  530. // If the instance isn't idle, we can't do anything
  531. if (idata->state != INSTANCE_IDLE)
  532. return 0;
  533. std::shared_ptr<s_instance_db> db = instance_db.find(idata->id);
  534. if (!db)
  535. return 0;
  536. // Set to busy, update timers
  537. idata->state = INSTANCE_BUSY;
  538. idata->idle_limit = static_cast<unsigned int>(time(nullptr)) + db->timeout;
  539. idata->idle_timer = add_timer(gettick() + db->timeout * 1000, instance_delete_timer, instance_id, 0);
  540. int16 m;
  541. // Add initial map
  542. if ((m = map_addinstancemap(db->enter.map, instance_id)) < 0) {
  543. ShowError("instance_addmap: Failed to create initial map for instance '%s' (%d).\n", db->name.c_str(), instance_id);
  544. return 0;
  545. }
  546. struct s_instance_map entry;
  547. entry.m = m;
  548. entry.src_m = db->enter.map;
  549. idata->map.push_back(entry);
  550. // Add extra maps (if any)
  551. for (const auto &it : db->maplist) {
  552. if ((m = map_addinstancemap(it, instance_id)) < 0) { // An error occured adding a map
  553. ShowError("instance_addmap: No maps added to instance '%s' (%d).\n", db->name.c_str(), instance_id);
  554. return 0;
  555. } else {
  556. entry.m = m;
  557. entry.src_m = it;
  558. idata->map.push_back(entry);
  559. }
  560. }
  561. // Create NPCs on all maps
  562. instance_addnpc(idata);
  563. switch(idata->mode) {
  564. case IM_NONE:
  565. break;
  566. case IM_CHAR:
  567. if (map_charid2sd(idata->owner_id)) // Inform player of the created instance
  568. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  569. break;
  570. case IM_PARTY:
  571. if (party_search(idata->owner_id)) // Inform party members of the created instance
  572. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  573. break;
  574. case IM_GUILD:
  575. if (guild_search(idata->owner_id)) // Inform guild members of the created instance
  576. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  577. break;
  578. case IM_CLAN:
  579. if (clan_search(idata->owner_id)) // Inform clan members of the created instance
  580. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  581. break;
  582. default:
  583. return 0;
  584. }
  585. return idata->map.size();
  586. }
  587. /**
  588. * Returns an instance map ID
  589. * @param m: Source map ID
  590. * @param instance_id: Instance to search
  591. * @return Map ID in this instance or -1 on failure
  592. */
  593. int16 instance_mapid(int16 m, int instance_id)
  594. {
  595. const char *name = map_mapid2mapname(m);
  596. if (name == nullptr) {
  597. ShowError("instance_mapid: Map ID %d does not exist.\n", m);
  598. return -1;
  599. }
  600. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  601. if(!idata || idata->state != INSTANCE_BUSY)
  602. return -1;
  603. for (const auto &it : idata->map) {
  604. if (it.src_m == m) {
  605. char iname[MAP_NAME_LENGTH], alt_name[MAP_NAME_LENGTH];
  606. strcpy(iname, name);
  607. if (!(strchr(iname, '@')) && strlen(iname) > 8) {
  608. memmove(iname, iname + (strlen(iname) - 9), strlen(iname));
  609. snprintf(alt_name, sizeof(alt_name), "%d#%s", (instance_id % 1000), iname);
  610. } else
  611. snprintf(alt_name, sizeof(alt_name), "%.3d%s", (instance_id % 1000), iname);
  612. return map_mapname2mapid(alt_name);
  613. }
  614. }
  615. return m;
  616. }
  617. /**
  618. * Removes an instance, all its maps, and NPCs invoked by the client button.
  619. * @param sd: Player data
  620. */
  621. void instance_destroy_command(map_session_data *sd) {
  622. nullpo_retv(sd);
  623. std::shared_ptr<s_instance_data> idata;
  624. int instance_id = 0;
  625. if (sd->instance_mode == IM_CHAR && sd->instance_id > 0) {
  626. idata = util::umap_find(instances, sd->instance_id);
  627. if (idata == nullptr)
  628. return;
  629. instance_id = sd->instance_id;
  630. } else if (sd->instance_mode == IM_PARTY && sd->status.party_id > 0) {
  631. party_data *pd = party_search(sd->status.party_id);
  632. if (pd == nullptr)
  633. return;
  634. idata = util::umap_find(instances, pd->instance_id);
  635. if (idata == nullptr)
  636. return;
  637. int32 i;
  638. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  639. if (i == MAX_PARTY) // Player is not party leader
  640. return;
  641. instance_id = pd->instance_id;
  642. } else if (sd->instance_mode == IM_GUILD && sd->guild != nullptr && sd->guild->instance_id > 0) {
  643. guild *gd = guild_search(sd->status.guild_id);
  644. if (gd == nullptr)
  645. return;
  646. idata = util::umap_find(instances, gd->instance_id);
  647. if (idata == nullptr)
  648. return;
  649. if (strcmp(sd->status.name, gd->master) != 0) // Player is not guild master
  650. return;
  651. instance_id = gd->instance_id;
  652. }
  653. if (instance_id == 0) // Checks above failed
  654. return;
  655. if (!instance_db.find(idata->id)->destroyable) // Instance is flagged as non-destroyable
  656. return;
  657. instance_destroy(instance_id);
  658. // Check for any other active instances and display their info
  659. if (sd->instance_id > 0)
  660. instance_reqinfo(sd, sd->instance_id);
  661. if (sd->status.party_id > 0) {
  662. party_data *pd = party_search(sd->status.party_id);
  663. if (pd == nullptr)
  664. return;
  665. if (pd->instance_id > 0)
  666. instance_reqinfo(sd, pd->instance_id);
  667. }
  668. if (sd->guild != nullptr && sd->guild->instance_id > 0) {
  669. guild *gd = guild_search(sd->status.guild_id);
  670. if (gd == nullptr)
  671. return;
  672. instance_reqinfo(sd, gd->instance_id);
  673. }
  674. }
  675. /**
  676. * Removes an instance, all its maps, and NPCs.
  677. * @param instance_id: Instance to remove
  678. * @return True on sucess or false on failure
  679. */
  680. bool instance_destroy(int instance_id)
  681. {
  682. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  683. if (!idata)
  684. return false;
  685. struct map_session_data *sd;
  686. struct party_data *pd;
  687. struct guild *gd;
  688. struct clan *cd;
  689. e_instance_mode mode = idata->mode;
  690. e_instance_notify type = IN_NOTIFY;
  691. switch(mode) {
  692. case IM_NONE:
  693. break;
  694. case IM_CHAR:
  695. sd = map_charid2sd(idata->owner_id);
  696. break;
  697. case IM_PARTY:
  698. pd = party_search(idata->owner_id);
  699. break;
  700. case IM_GUILD:
  701. gd = guild_search(idata->owner_id);
  702. break;
  703. case IM_CLAN:
  704. cd = clan_search(idata->owner_id);
  705. break;
  706. }
  707. if(idata->state == INSTANCE_IDLE) {
  708. for (auto instance_it = instance_wait.id.begin(); instance_it != instance_wait.id.end(); ++instance_it) {
  709. if (*instance_it == instance_id) {
  710. instance_wait.id.erase(instance_it);
  711. for (int i = 0; i < instance_wait.id.size(); i++) {
  712. if (util::umap_find(instances, instance_wait.id[i])->state == INSTANCE_IDLE)
  713. if ((mode == IM_CHAR && sd) || (mode == IM_PARTY && pd) || (mode == IM_GUILD && gd) || (mode == IM_CLAN && cd))
  714. clif_instance_changewait(instance_id, i + 1);
  715. }
  716. if (!instance_wait.id.empty())
  717. instance_wait.timer = add_timer(gettick() + INSTANCE_INTERVAL, instance_subscription_timer, 0, 0);
  718. else
  719. instance_wait.timer = INVALID_TIMER;
  720. break;
  721. }
  722. }
  723. } else {
  724. unsigned int now = static_cast<unsigned int>(time(nullptr));
  725. if(idata->keep_limit && idata->keep_limit <= now)
  726. type = IN_DESTROY_LIVE_TIMEOUT;
  727. else if(idata->idle_limit && idata->idle_limit <= now)
  728. type = IN_DESTROY_ENTER_TIMEOUT;
  729. else
  730. type = IN_DESTROY_USER_REQUEST;
  731. // Run OnInstanceDestroy on all NPCs in the instance
  732. for (const auto &it : idata->map) {
  733. struct map_data *mapdata = map_getmapdata(it.m);
  734. map_foreachinallarea(instance_npcdestroy, it.m, 0, 0, mapdata->xs, mapdata->ys, BL_NPC, it.m);
  735. map_delinstancemap(it.m);
  736. }
  737. }
  738. if(idata->keep_timer != INVALID_TIMER) {
  739. delete_timer(idata->keep_timer, instance_delete_timer);
  740. idata->keep_timer = INVALID_TIMER;
  741. }
  742. if(idata->idle_timer != INVALID_TIMER) {
  743. delete_timer(idata->idle_timer, instance_delete_timer);
  744. idata->idle_timer = INVALID_TIMER;
  745. }
  746. if (mode == IM_CHAR && sd)
  747. sd->instance_id = 0;
  748. else if (mode == IM_PARTY && pd)
  749. pd->instance_id = 0;
  750. else if (mode == IM_GUILD && gd)
  751. gd->instance_id = 0;
  752. else if (mode == IM_CLAN && cd)
  753. cd->instance_id = 0;
  754. if (mode != IM_NONE) {
  755. if(type != IN_NOTIFY)
  756. clif_instance_changestatus(instance_id, type, 0);
  757. else
  758. clif_instance_changewait(instance_id, 0xffff);
  759. }
  760. if( idata->regs.vars ) {
  761. db_destroy(idata->regs.vars);
  762. idata->regs.vars = NULL;
  763. }
  764. if( idata->regs.arrays )
  765. idata->regs.arrays->destroy(idata->regs.arrays, script_free_array_db);
  766. ShowInfo("[Instance] Destroyed: %s (%d)\n", instance_db.find(idata->id)->name.c_str(), instance_id);
  767. instances.erase(instance_id);
  768. return true;
  769. }
  770. /**
  771. * Warp a user into an instance
  772. * @param sd: Player to warp
  773. * @param instance_id: Instance to warp to
  774. * @param name: Map name
  775. * @param x: X coordinate
  776. * @param y: Y coordinate
  777. * @return e_instance_enter value
  778. */
  779. e_instance_enter instance_enter(struct map_session_data *sd, int instance_id, const char *name, short x, short y)
  780. {
  781. nullpo_retr(IE_OTHER, sd);
  782. std::shared_ptr<s_instance_db> db = instance_search_db_name(name);
  783. if (!db) {
  784. ShowError("instance_enter: Unknown instance \"%s\".\n", name);
  785. return IE_OTHER;
  786. }
  787. // If one of the two coordinates was not given or is below zero, we use the entry point from the database
  788. if (x < 0 || y < 0) {
  789. x = db->enter.x;
  790. y = db->enter.y;
  791. }
  792. std::shared_ptr<s_instance_data> idata = nullptr;
  793. struct party_data *pd;
  794. struct guild *gd;
  795. struct clan *cd;
  796. e_instance_mode mode;
  797. if (instance_id <= 0) // Default party checks will be used
  798. mode = IM_PARTY;
  799. else {
  800. idata = util::umap_find(instances, instance_id);
  801. mode = idata->mode;
  802. }
  803. switch(mode) {
  804. case IM_NONE:
  805. break;
  806. case IM_CHAR:
  807. if (sd->instance_id == 0) // Player must have an instance
  808. return IE_NOINSTANCE;
  809. if (idata->owner_id != sd->status.char_id)
  810. return IE_OTHER;
  811. break;
  812. case IM_PARTY:
  813. if (sd->status.party_id == 0) // Character must be in instance party
  814. return IE_NOMEMBER;
  815. if (!(pd = party_search(sd->status.party_id)))
  816. return IE_NOMEMBER;
  817. if (pd->instance_id == 0 || idata == nullptr) // Party must have an instance
  818. return IE_NOINSTANCE;
  819. if (idata->owner_id != pd->party.party_id)
  820. return IE_OTHER;
  821. break;
  822. case IM_GUILD:
  823. if (sd->status.guild_id == 0) // Character must be in instance guild
  824. return IE_NOMEMBER;
  825. if (!(gd = guild_search(sd->status.guild_id)))
  826. return IE_NOMEMBER;
  827. if (gd->instance_id == 0) // Guild must have an instance
  828. return IE_NOINSTANCE;
  829. if (idata->owner_id != gd->guild_id)
  830. return IE_OTHER;
  831. break;
  832. case IM_CLAN:
  833. if (sd->status.clan_id == 0) // Character must be in instance clan
  834. return IE_NOMEMBER;
  835. if (!(cd = clan_search(sd->status.clan_id)))
  836. return IE_NOMEMBER;
  837. if (cd->instance_id == 0) // Clan must have an instance
  838. return IE_NOINSTANCE;
  839. if (idata->owner_id != cd->id)
  840. return IE_OTHER;
  841. break;
  842. }
  843. if (idata->state != INSTANCE_BUSY)
  844. return IE_OTHER;
  845. if (idata->id != db->id)
  846. return IE_OTHER;
  847. int16 m;
  848. // Does the instance match?
  849. if ((m = instance_mapid(db->enter.map, instance_id)) < 0)
  850. return IE_OTHER;
  851. if (pc_setpos(sd, map_id2index(m), x, y, CLR_OUTSIGHT))
  852. return IE_OTHER;
  853. return IE_OK;
  854. }
  855. /**
  856. * Request some info about the instance
  857. * @param sd: Player to display info to
  858. * @param instance_id: Instance to request
  859. * @return True on success or false on failure
  860. */
  861. bool instance_reqinfo(struct map_session_data *sd, int instance_id)
  862. {
  863. nullpo_retr(false, sd);
  864. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  865. if (!idata || !instance_db.find(idata->id))
  866. return false;
  867. // Say it's created if instance is not busy
  868. if(idata->state == INSTANCE_IDLE) {
  869. for (int i = 0; i < instance_wait.id.size(); i++) {
  870. if (instance_wait.id[i] == instance_id) {
  871. clif_instance_create(instance_id, i + 1);
  872. sd->instance_mode = idata->mode;
  873. break;
  874. }
  875. }
  876. } else if (idata->state == INSTANCE_BUSY) { // Give info on the instance if busy
  877. int map_instance_id = map_getmapdata(sd->bl.m)->instance_id;
  878. if (map_instance_id == 0 || map_instance_id == instance_id) {
  879. clif_instance_status(instance_id, idata->keep_limit, idata->idle_limit);
  880. sd->instance_mode = idata->mode;
  881. }
  882. }
  883. return true;
  884. }
  885. /**
  886. * Add players to the instance (for timers) -- Unused?
  887. * @param instance_id: Instance to add
  888. * @return True on success or false on failure
  889. */
  890. bool instance_addusers(int instance_id)
  891. {
  892. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  893. if(!idata || idata->state != INSTANCE_BUSY)
  894. return false;
  895. // Stop the idle timer if we had one
  896. instance_stopidletimer(idata, instance_id);
  897. // Start the instance keep timer
  898. instance_startkeeptimer(idata, instance_id);
  899. return true;
  900. }
  901. /**
  902. * Delete players from the instance (for timers)
  903. * @param instance_id: Instance to remove
  904. * @return True on success or false on failure
  905. */
  906. bool instance_delusers(int instance_id)
  907. {
  908. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, instance_id);
  909. if(!idata || idata->state != INSTANCE_BUSY)
  910. return false;
  911. int users = 0;
  912. // If no one is in the instance, start the idle timer
  913. for (const auto &it : idata->map)
  914. users += max(map_getmapdata(it.m)->users,0);
  915. // We check the actual map.users before being updated, hence the 1
  916. // The instance should be empty if users are now <= 1
  917. if(users <= 1)
  918. instance_startidletimer(idata, instance_id);
  919. return true;
  920. }
  921. /**
  922. * Reloads the instance in runtime (reloadscript)
  923. */
  924. void do_reload_instance(void)
  925. {
  926. for (const auto &it : instances) {
  927. std::shared_ptr<s_instance_data> idata = it.second;
  928. if (!idata || idata->map.empty())
  929. continue;
  930. else {
  931. // First we load the NPCs again
  932. instance_addnpc(idata);
  933. // Create new keep timer
  934. std::shared_ptr<s_instance_db> db = instance_db.find(idata->id);
  935. if (db)
  936. idata->keep_limit = static_cast<unsigned int>(time(nullptr)) + db->limit;
  937. }
  938. }
  939. // Reset player to instance beginning
  940. struct s_mapiterator *iter = mapit_getallusers();
  941. struct map_session_data *sd;
  942. for (sd = (TBL_PC *)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC *)mapit_next(iter)) {
  943. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  944. if (sd && mapdata->instance_id > 0) {
  945. struct party_data *pd;
  946. struct guild *gd;
  947. struct clan *cd;
  948. int instance_id;
  949. std::shared_ptr<s_instance_data> idata = util::umap_find(instances, map[sd->bl.m].instance_id);
  950. std::shared_ptr<s_instance_db> db = instance_db.find(idata->id);
  951. switch (idata->mode) {
  952. case IM_NONE:
  953. continue;
  954. case IM_CHAR:
  955. if (sd->instance_id != mapdata->instance_id) // Someone who is not instance owner is on instance map
  956. continue;
  957. instance_id = sd->instance_id;
  958. break;
  959. case IM_PARTY:
  960. if ((!(pd = party_search(sd->status.party_id)) || pd->instance_id != mapdata->instance_id)) // Someone not in party is on instance map
  961. continue;
  962. instance_id = pd->instance_id;
  963. break;
  964. case IM_GUILD:
  965. if (!(gd = guild_search(sd->status.guild_id)) || gd->instance_id != mapdata->instance_id) // Someone not in guild is on instance map
  966. continue;
  967. instance_id = gd->instance_id;
  968. break;
  969. case IM_CLAN:
  970. if (!(cd = clan_search(sd->status.clan_id)) || cd->instance_id != mapdata->instance_id) // Someone not in clan is on instance map
  971. continue;
  972. instance_id = cd->instance_id;
  973. break;
  974. default:
  975. ShowError("do_reload_instance: Unexpected instance mode for instance %s (id=%d, mode=%u).\n", (db) ? db->name.c_str() : "Unknown", mapdata->instance_id, (uint8)idata->mode);
  976. continue;
  977. }
  978. if (db && instance_enter(sd, instance_id, db->name.c_str(), -1, -1) == IE_OK) { // All good
  979. clif_displaymessage(sd->fd, msg_txt(sd, 515)); // Instance has been reloaded
  980. instance_reqinfo(sd, instance_id);
  981. } else // Something went wrong
  982. ShowError("do_reload_instance: Error setting character at instance start: character_id=%d instance=%s.\n", sd->status.char_id, db->name.c_str());
  983. }
  984. }
  985. mapit_free(iter);
  986. }
  987. /**
  988. * Initializes the instance database
  989. */
  990. void do_init_instance(void) {
  991. instance_start = map_num;
  992. instance_db.load();
  993. instance_wait.timer = INVALID_TIMER;
  994. add_timer_func_list(instance_delete_timer,"instance_delete_timer");
  995. add_timer_func_list(instance_subscription_timer,"instance_subscription_timer");
  996. }
  997. /**
  998. * Finalizes the instances and instance database
  999. */
  1000. void do_final_instance(void) {
  1001. for (const auto &it : instances)
  1002. instance_destroy(it.first);
  1003. }