instance.cpp 33 KB

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