instance.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/socket.h"
  5. #include "../common/timer.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/showmsg.h"
  8. #include "../common/strlib.h"
  9. #include "../common/db.h"
  10. #include "../common/malloc.h"
  11. #include "clif.h"
  12. #include "instance.h"
  13. #include "map.h"
  14. #include "npc.h"
  15. #include "party.h"
  16. #include "pc.h"
  17. #include <stdlib.h>
  18. #define INSTANCE_INTERVAL 60000 // Interval used to check when an instance is to be destroyed (ms)
  19. #define INSTANCE_LIMIT 300000 // Idle timer before instance is destroyed (ms) : 5 Minute Default
  20. int instance_start = 0; // To keep the last index + 1 of normal map inserted in the map[ARRAY]
  21. struct instance_data instance_data[MAX_INSTANCE_DATA];
  22. struct instance_db {
  23. unsigned short id;
  24. StringBuf *name;
  25. unsigned int limit;
  26. struct {
  27. StringBuf *mapname; ///< Mapname, the limit should be MAP_NAME_LENGTH_EXT
  28. unsigned short x, y; ///< Map coordinates
  29. } enter;
  30. StringBuf **maplist; ///< Used maps, the limit should be MAP_NAME_LENGTH_EXT
  31. uint8 maplist_count;
  32. };
  33. static DBMap *InstanceDB; /// Instance DB: struct instance_db, key: id
  34. static DBMap *InstanceNameDB; /// instance id, key: name
  35. static struct {
  36. int id[MAX_INSTANCE_DATA];
  37. int count;
  38. int timer;
  39. } instance_wait;
  40. /*==========================================
  41. * Searches for an instance ID in the database
  42. *------------------------------------------*/
  43. static struct instance_db *instance_searchtype_db(unsigned short instance_id) {
  44. return (struct instance_db *)uidb_get(InstanceDB,instance_id);
  45. }
  46. static uint16 instance_name2id(const char *instance_name) {
  47. return (uint16)strdb_uiget(InstanceNameDB,instance_name);
  48. }
  49. /*==========================================
  50. * Searches for an instance name in the database
  51. *------------------------------------------*/
  52. static struct instance_db *instance_searchname_db(const char *instance_name) {
  53. uint16 id = instance_name2id(instance_name);
  54. if (id == 0)
  55. return NULL;
  56. return (struct instance_db *)uidb_get(InstanceDB,id);
  57. }
  58. /*==========================================
  59. * Deletes an instance timer (Destroys instance)
  60. *------------------------------------------*/
  61. static int instance_delete_timer(int tid, unsigned int tick, int id, intptr_t data)
  62. {
  63. instance_destroy(id);
  64. return 0;
  65. }
  66. /*==========================================
  67. * Create subscription timer for party
  68. *------------------------------------------*/
  69. static int instance_subscription_timer(int tid, unsigned int tick, int id, intptr_t data)
  70. {
  71. int i, ret;
  72. int instance_id = instance_wait.id[0];
  73. struct party_data *p;
  74. if(instance_wait.count == 0 || instance_id <= 0)
  75. return 0;
  76. // Check that maps have been added
  77. ret = instance_addmap(instance_id);
  78. // If no maps are created, tell party to wait
  79. if(ret == 0 && ( p = party_search( instance_data[instance_id].party_id ) ) != NULL)
  80. clif_instance_changewait( party_getavailablesd( p ), 0xffff, 1 );
  81. instance_wait.count--;
  82. memmove(&instance_wait.id[0],&instance_wait.id[1],sizeof(instance_wait.id[0])*instance_wait.count);
  83. memset(&instance_wait.id[instance_wait.count], 0, sizeof(instance_wait.id[0]));
  84. for(i = 0; i < instance_wait.count; i++) {
  85. if( instance_data[instance_wait.id[i]].state == INSTANCE_IDLE &&
  86. ( p = party_search( instance_data[instance_wait.id[i]].party_id ) ) != NULL
  87. ){
  88. clif_instance_changewait( party_getavailablesd( p ), i + 1, 1 );
  89. }
  90. }
  91. if(instance_wait.count)
  92. instance_wait.timer = add_timer(gettick()+INSTANCE_INTERVAL, instance_subscription_timer, 0, 0);
  93. else
  94. instance_wait.timer = INVALID_TIMER;
  95. return 0;
  96. }
  97. /*==========================================
  98. * Adds timer back to party entering instance
  99. *------------------------------------------*/
  100. static int instance_startkeeptimer(struct instance_data *im, short instance_id)
  101. {
  102. struct instance_db *db;
  103. struct party_data *p;
  104. nullpo_retr(0, im);
  105. // No timer
  106. if(im->keep_timer != INVALID_TIMER)
  107. return 1;
  108. if((db = instance_searchtype_db(im->type)) == NULL)
  109. return 1;
  110. // Add timer
  111. im->keep_limit = (unsigned int)time(NULL) + db->limit;
  112. im->keep_timer = add_timer(gettick()+db->limit*1000, instance_delete_timer, instance_id, 0);
  113. // Notify party of the added instance timer
  114. if( ( p = party_search( im->party_id ) ) != NULL )
  115. clif_instance_status( party_getavailablesd( p ), StringBuf_Value(db->name), im->keep_limit, im->idle_limit, 1 );
  116. return 0;
  117. }
  118. /*==========================================
  119. * Creates idle timer
  120. * Default before instance destroy is 5 minutes
  121. *------------------------------------------*/
  122. static int instance_startidletimer(struct instance_data *im, short instance_id)
  123. {
  124. struct instance_db *db;
  125. struct party_data *p;
  126. nullpo_retr(1, im);
  127. // No current timer
  128. if(im->idle_timer != INVALID_TIMER)
  129. return 1;
  130. // Add the timer
  131. im->idle_limit = (unsigned int)time(NULL) + INSTANCE_LIMIT/1000;
  132. im->idle_timer = add_timer(gettick()+INSTANCE_LIMIT, instance_delete_timer, instance_id, 0);
  133. // Notify party of added instance timer
  134. if( ( p = party_search( im->party_id ) ) != NULL &&
  135. ( db = instance_searchtype_db( im->type ) ) != NULL
  136. )
  137. {
  138. clif_instance_status( party_getavailablesd( p ), StringBuf_Value(db->name), im->keep_limit, im->idle_limit, 1 );
  139. }
  140. return 0;
  141. }
  142. /*==========================================
  143. * Delete the idle timer
  144. *------------------------------------------*/
  145. static int instance_stopidletimer(struct instance_data *im)
  146. {
  147. struct party_data *p;
  148. nullpo_retr(0, im);
  149. // No timer
  150. if(im->idle_timer == INVALID_TIMER)
  151. return 1;
  152. // Delete the timer - Party has returned or instance is destroyed
  153. im->idle_limit = 0;
  154. delete_timer(im->idle_timer, instance_delete_timer);
  155. im->idle_timer = INVALID_TIMER;
  156. // Notify the party
  157. if( ( p = party_search( im->party_id ) ) != NULL )
  158. clif_instance_changestatus( party_getavailablesd( p ), 0, im->idle_limit, 1 );
  159. return 0;
  160. }
  161. /*==========================================
  162. * Run the OnInstanceInit events for duplicated NPCs
  163. *------------------------------------------*/
  164. static int instance_npcinit(struct block_list *bl, va_list ap)
  165. {
  166. struct npc_data* nd;
  167. nullpo_retr(0, bl);
  168. nullpo_retr(0, ap);
  169. nullpo_retr(0, nd = (struct npc_data *)bl);
  170. return npc_instanceinit(nd);
  171. }
  172. /*==========================================
  173. * Add an NPC to an instance
  174. *------------------------------------------*/
  175. static int instance_addnpc_sub(struct block_list *bl, va_list ap)
  176. {
  177. struct npc_data* nd;
  178. nullpo_retr(0, bl);
  179. nullpo_retr(0, ap);
  180. nullpo_retr(0, nd = (struct npc_data *)bl);
  181. return npc_duplicate4instance(nd, va_arg(ap, int));
  182. }
  183. // Separate function used for reloading
  184. void instance_addnpc(struct instance_data *im)
  185. {
  186. int i;
  187. // First add the NPCs
  188. for(i = 0; i < im->cnt_map; i++)
  189. map_foreachinarea(instance_addnpc_sub, im->map[i].src_m, 0, 0, map[im->map[i].src_m].xs, map[im->map[i].src_m].ys, BL_NPC, im->map[i].m);
  190. // Now run their OnInstanceInit
  191. for(i = 0; i < im->cnt_map; i++)
  192. map_foreachinarea(instance_npcinit, im->map[i].m, 0, 0, map[im->map[i].m].xs, map[im->map[i].m].ys, BL_NPC, im->map[i].m);
  193. }
  194. /*--------------------------------------
  195. * name : instance name
  196. * Return value could be
  197. * -4 = no free instances | -3 = already exists | -2 = party not found | -1 = invalid type
  198. * On success return instance_id
  199. *--------------------------------------*/
  200. int instance_create(int party_id, const char *name)
  201. {
  202. short i;
  203. struct instance_db *db = instance_searchname_db(name);
  204. struct party_data *p = party_search(party_id);
  205. if(db == NULL)
  206. return -1;
  207. if( p == NULL )
  208. return -2;
  209. if( p->instance_id )
  210. return -3; // Party already instancing
  211. // Searching a Free Instance
  212. // 0 is ignored as this mean "no instance" on maps
  213. ARR_FIND(1, MAX_INSTANCE_DATA, i, instance_data[i].state == INSTANCE_FREE);
  214. if( i >= MAX_INSTANCE_DATA )
  215. return -4;
  216. instance_data[i].type = db->id;
  217. instance_data[i].state = INSTANCE_IDLE;
  218. instance_data[i].party_id = p->party.party_id;
  219. instance_data[i].keep_limit = 0;
  220. instance_data[i].keep_timer = INVALID_TIMER;
  221. instance_data[i].idle_limit = 0;
  222. instance_data[i].idle_timer = INVALID_TIMER;
  223. instance_data[i].vars = idb_alloc(DB_OPT_RELEASE_DATA);
  224. memset(instance_data[i].map, 0, sizeof(instance_data[i].map));
  225. p->instance_id = i;
  226. instance_wait.id[instance_wait.count++] = p->instance_id;
  227. clif_instance_create( party_getavailablesd( p ), name, instance_wait.count, 1);
  228. instance_subscription_timer(0,0,0,0);
  229. ShowInfo("[Instance] Created: %s.\n", name);
  230. return i;
  231. }
  232. /*--------------------------------------
  233. * Adds maps to the instance
  234. *--------------------------------------*/
  235. int instance_addmap(short instance_id)
  236. {
  237. int i, m;
  238. int cnt_map = 0;
  239. struct instance_data *im;
  240. struct instance_db *db;
  241. struct party_data *p;
  242. if(instance_id <= 0)
  243. return 0;
  244. im = &instance_data[instance_id];
  245. // If the instance isn't idle, we can't do anything
  246. if(im->state != INSTANCE_IDLE)
  247. return 0;
  248. if((db = instance_searchtype_db(im->type)) == NULL)
  249. return 0;
  250. // Set to busy, update timers
  251. im->state = INSTANCE_BUSY;
  252. im->idle_limit = (unsigned int)time(NULL) + INSTANCE_LIMIT/1000;
  253. im->idle_timer = add_timer(gettick()+INSTANCE_LIMIT, instance_delete_timer, instance_id, 0);
  254. // Add the maps
  255. for(i = 0; i < db->maplist_count; i++) {
  256. if(strlen(StringBuf_Value(db->maplist[i])) < 1)
  257. continue;
  258. else if( (m = map_addinstancemap(StringBuf_Value(db->maplist[i]), instance_id)) < 0) {
  259. // An error occured adding a map
  260. ShowError("instance_addmap: No maps added to instance %d.\n",instance_id);
  261. return 0;
  262. } else {
  263. im->map[cnt_map].m = m;
  264. im->map[cnt_map].src_m = map_mapname2mapid(StringBuf_Value(db->maplist[i]));
  265. cnt_map++;
  266. }
  267. }
  268. im->cnt_map = cnt_map;
  269. // Create NPCs on all maps
  270. instance_addnpc(im);
  271. // Inform party members of the created instance
  272. if( (p = party_search( im->party_id ) ) != NULL )
  273. clif_instance_status( party_getavailablesd( p ), StringBuf_Value(db->name), im->keep_limit, im->idle_limit, 1);
  274. return cnt_map;
  275. }
  276. /*==========================================
  277. * Returns an instance map ID using a map name
  278. * name : source map
  279. * instance_id : where to search
  280. * result : mapid of map "name" in this instance
  281. *------------------------------------------*/
  282. int instance_mapname2mapid(const char *name, short instance_id)
  283. {
  284. struct instance_data *im;
  285. int m = map_mapname2mapid(name);
  286. char iname[MAP_NAME_LENGTH];
  287. int i;
  288. if(m < 0) {
  289. ShowError("instance_mapname2mapid: map name %s does not exist.\n",name);
  290. return m;
  291. }
  292. strcpy(iname,name);
  293. if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  294. return m;
  295. im = &instance_data[instance_id];
  296. if(im->state != INSTANCE_BUSY)
  297. return m;
  298. for(i = 0; i < MAX_MAP_PER_INSTANCE; i++)
  299. if(im->map[i].src_m == m) {
  300. char alt_name[MAP_NAME_LENGTH];
  301. if((strchr(iname,'@') == NULL) && strlen(iname) > 8) {
  302. memmove(iname, iname+(strlen(iname)-9), strlen(iname));
  303. snprintf(alt_name, sizeof(alt_name),"%d#%s", instance_id, iname);
  304. } else
  305. snprintf(alt_name, sizeof(alt_name),"%.3d%s", instance_id, iname);
  306. return map_mapname2mapid(alt_name);
  307. }
  308. return m;
  309. }
  310. /*==========================================
  311. * Removes a instance, all its maps and npcs.
  312. *------------------------------------------*/
  313. int instance_destroy(short instance_id)
  314. {
  315. struct instance_data *im;
  316. struct party_data *p;
  317. int i, type = 0;
  318. unsigned int now = (unsigned int)time(NULL);
  319. if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  320. return 1;
  321. im = &instance_data[instance_id];
  322. if(im->state == INSTANCE_FREE)
  323. return 1;
  324. if(im->state == INSTANCE_IDLE) {
  325. for(i = 0; i < instance_wait.count; i++) {
  326. if(instance_wait.id[i] == instance_id) {
  327. instance_wait.count--;
  328. memmove(&instance_wait.id[i],&instance_wait.id[i+1],sizeof(instance_wait.id[0])*(instance_wait.count-i));
  329. memset(&instance_wait.id[instance_wait.count], 0, sizeof(instance_wait.id[0]));
  330. for(i = 0; i < instance_wait.count; i++)
  331. if(instance_data[instance_wait.id[i]].state == INSTANCE_IDLE)
  332. if((p = party_search(instance_data[instance_wait.id[i]].party_id)) != NULL)
  333. clif_instance_changewait( party_getavailablesd( p ), i+1, 1);
  334. if(instance_wait.count)
  335. instance_wait.timer = add_timer(gettick()+INSTANCE_INTERVAL, instance_subscription_timer, 0, 0);
  336. else
  337. instance_wait.timer = INVALID_TIMER;
  338. type = 0;
  339. break;
  340. }
  341. }
  342. } else {
  343. if(im->keep_limit && im->keep_limit <= now)
  344. type = 1;
  345. else if(im->idle_limit && im->idle_limit <= now)
  346. type = 2;
  347. else
  348. type = 3;
  349. for(i = 0; i < MAX_MAP_PER_INSTANCE; i++)
  350. map_delinstancemap(im->map[i].m);
  351. }
  352. if(im->keep_timer != INVALID_TIMER) {
  353. delete_timer(im->keep_timer, instance_delete_timer);
  354. im->keep_timer = INVALID_TIMER;
  355. }
  356. if(im->idle_timer != INVALID_TIMER) {
  357. delete_timer(im->idle_timer, instance_delete_timer);
  358. im->idle_timer = INVALID_TIMER;
  359. }
  360. if((p = party_search(im->party_id))) {
  361. p->instance_id = 0;
  362. if(type)
  363. clif_instance_changestatus( party_getavailablesd( p ), type, 0, 1 );
  364. else
  365. clif_instance_changewait( party_getavailablesd( p ), 0xffff, 1 );
  366. }
  367. if( im->vars ) {
  368. db_destroy(im->vars);
  369. im->vars = NULL;
  370. }
  371. ShowInfo("[Instance] Destroyed %d.\n", instance_id);
  372. memset(&instance_data[instance_id], 0, sizeof(instance_data[instance_id]));
  373. return 0;
  374. }
  375. /*==========================================
  376. * Allows a user to enter an instance
  377. *------------------------------------------*/
  378. int instance_enter(struct map_session_data *sd, const char *name)
  379. {
  380. struct instance_db *db = instance_searchname_db(name);
  381. if(db == NULL)
  382. return 3;
  383. return instance_enter_position(sd, name, db->enter.x, db->enter.y);
  384. }
  385. /*==========================================
  386. * Warp a user into instance
  387. *------------------------------------------*/
  388. int instance_enter_position(struct map_session_data *sd, const char *name, short x, short y)
  389. {
  390. struct instance_data *im;
  391. struct instance_db *db = instance_searchname_db(name);
  392. struct party_data *p;
  393. int m;
  394. nullpo_retr(-1, sd);
  395. // Character must be in instance party
  396. if(sd->status.party_id == 0)
  397. return 1;
  398. if((p = party_search(sd->status.party_id)) == NULL)
  399. return 1;
  400. // Party must have an instance
  401. if(p->instance_id == 0)
  402. return 2;
  403. if(db == NULL)
  404. return 3;
  405. im = &instance_data[p->instance_id];
  406. if(im->party_id != p->party.party_id)
  407. return 3;
  408. if(im->state != INSTANCE_BUSY)
  409. return 3;
  410. if(im->type != db->id)
  411. return 3;
  412. // Does the instance match?
  413. if((m = instance_mapname2mapid(StringBuf_Value(db->enter.mapname), p->instance_id)) < 0)
  414. return 3;
  415. if(pc_setpos(sd, map_id2index(m), x, y, CLR_OUTSIGHT))
  416. return 3;
  417. // If there was an idle timer, let's stop it
  418. instance_stopidletimer(im);
  419. // Now we start the instance timer
  420. instance_startkeeptimer(im, p->instance_id);
  421. return 0;
  422. }
  423. /*==========================================
  424. * Request some info about the instance
  425. *------------------------------------------*/
  426. int instance_reqinfo(struct map_session_data *sd, short instance_id)
  427. {
  428. struct instance_data *im;
  429. struct instance_db *db;
  430. nullpo_retr(1, sd);
  431. if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  432. return 1;
  433. im = &instance_data[instance_id];
  434. if((db = instance_searchtype_db(im->type)) == NULL)
  435. return 1;
  436. // Say it's created if instance is not busy
  437. if(im->state == INSTANCE_IDLE) {
  438. int i;
  439. for(i = 0; i < instance_wait.count; i++) {
  440. if(instance_wait.id[i] == instance_id) {
  441. clif_instance_create(sd, StringBuf_Value(db->name), i+1, 0);
  442. break;
  443. }
  444. }
  445. } else if(im->state == INSTANCE_BUSY) // Give info on the instance if busy
  446. clif_instance_status(sd, StringBuf_Value(db->name), im->keep_limit, im->idle_limit, 0);
  447. return 0;
  448. }
  449. /*==========================================
  450. * Add players to the instance (for timers)
  451. *------------------------------------------*/
  452. int instance_addusers(short instance_id)
  453. {
  454. struct instance_data *im;
  455. if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  456. return 1;
  457. im = &instance_data[instance_id];
  458. if(im->state != INSTANCE_BUSY)
  459. return 1;
  460. // Stop the idle timer if we had one
  461. instance_stopidletimer(im);
  462. // Start the instance keep timer
  463. instance_startkeeptimer(im, instance_id);
  464. return 0;
  465. }
  466. /*==========================================
  467. * Delete players from the instance (for timers)
  468. *------------------------------------------*/
  469. int instance_delusers(short instance_id)
  470. {
  471. struct instance_data *im;
  472. int i, idle = 0;
  473. if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  474. return 1;
  475. im = &instance_data[instance_id];
  476. if(im->state != INSTANCE_BUSY)
  477. return 1;
  478. // If no one is in the instance, start the idle timer
  479. for(i = 0; im->map[i].m && i < MAX_MAP_PER_INSTANCE; i++)
  480. if(map[im->map[i].m].users > 1) // We check this before the actual map.users are updated, hence the 1
  481. idle++;
  482. if(!idle) // If idle wasn't added to, we know no one was in the instance
  483. instance_startidletimer(im, instance_id);
  484. return 0;
  485. }
  486. /*==========================================
  487. * Read the instance_db.txt file
  488. *------------------------------------------*/
  489. static bool instance_readdb_sub(char* str[], int columns, int current)
  490. {
  491. uint8 i;
  492. int id = atoi(str[0]);
  493. struct instance_db *db;
  494. bool isNew = false;
  495. if (!id || id > USHRT_MAX) {
  496. ShowError("instance_readdb_sub: Cannot add instance with ID '%d'. Valid ID is 1 ~ %d.\n", id, USHRT_MAX);
  497. return false;
  498. }
  499. if (!(db = (struct instance_db *)uidb_get(InstanceDB, id))) {
  500. CREATE(db, struct instance_db, 1);
  501. db->id = id;
  502. db->name = StringBuf_Malloc();
  503. db->enter.mapname = StringBuf_Malloc();
  504. isNew = true;
  505. }
  506. else {
  507. StringBuf_Clear(db->name);
  508. StringBuf_Clear(db->enter.mapname);
  509. if (db->maplist_count) {
  510. for (i = 0; i < db->maplist_count; i++)
  511. StringBuf_Free(db->maplist[i]);
  512. aFree(db->maplist);
  513. db->maplist = NULL;
  514. }
  515. }
  516. StringBuf_AppendStr(db->name, str[1]);
  517. db->limit = atoi(str[2]);
  518. StringBuf_AppendStr(db->enter.mapname, str[3]);
  519. db->enter.x = atoi(str[4]);
  520. db->enter.y = atoi(str[5]);
  521. //Instance maps
  522. for (i = 6; i < columns; i++) {
  523. if (strlen(str[i])) {
  524. RECREATE(db->maplist, StringBuf *, db->maplist_count+1);
  525. db->maplist[db->maplist_count] = StringBuf_Malloc();
  526. StringBuf_AppendStr(db->maplist[db->maplist_count], str[i]);
  527. db->maplist_count++;
  528. }
  529. }
  530. if (isNew) {
  531. uidb_put(InstanceDB, id, db);
  532. strdb_uiput(InstanceNameDB, StringBuf_Value(db->name), id);
  533. }
  534. return true;
  535. }
  536. static int instance_db_free(DBKey key, DBData *data, va_list ap) {
  537. struct instance_db *db = (struct instance_db *)db_data2ptr(data);
  538. bool clear = va_arg(ap, bool);
  539. if (!db)
  540. return 1;
  541. StringBuf_Free(db->name);
  542. StringBuf_Free(db->enter.mapname);
  543. if (db->maplist_count) {
  544. uint8 i;
  545. for (i = 0; i < db->maplist_count; i++)
  546. StringBuf_Free(db->maplist[i]);
  547. aFree(db->maplist);
  548. }
  549. aFree(db);
  550. return 0;
  551. }
  552. void instance_readdb(void) {
  553. const char* filename[] = { DBPATH"instance_db.txt", "import/instance_db.txt"};
  554. int f;
  555. for (f = 0; f<ARRAYLENGTH(filename); f++) {
  556. sv_readdb(db_path, filename[f], ',', 7, 7+MAX_MAP_PER_INSTANCE, -1, &instance_readdb_sub, f);
  557. }
  558. }
  559. void instance_reload(void) {
  560. InstanceDB->clear(InstanceDB, instance_db_free);
  561. db_clear(InstanceNameDB);
  562. instance_readdb();
  563. }
  564. /*==========================================
  565. * Reloads the instance in runtime (reloadscript)
  566. *------------------------------------------*/
  567. void do_reload_instance(void)
  568. {
  569. struct instance_data *im;
  570. struct instance_db *db;
  571. struct s_mapiterator* iter;
  572. struct map_session_data *sd;
  573. int i;
  574. for( i = 1; i < MAX_INSTANCE_DATA; i++ ) {
  575. im = &instance_data[i];
  576. if(!im->cnt_map)
  577. continue;
  578. else {
  579. // First we load the NPCs again
  580. instance_addnpc(im);
  581. // Create new keep timer
  582. if((db = instance_searchtype_db(im->type)) != NULL)
  583. im->keep_limit = (unsigned int)time(NULL) + db->limit;
  584. }
  585. }
  586. // Reset player to instance beginning
  587. iter = mapit_getallusers();
  588. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  589. if(sd && map[sd->bl.m].instance_id) {
  590. struct party_data *p;
  591. if(!(p = party_search(sd->status.party_id)) || p->instance_id != map[sd->bl.m].instance_id) // Someone not in party is on instance map
  592. continue;
  593. im = &instance_data[p->instance_id];
  594. if((db = instance_searchtype_db(im->type)) != NULL && !instance_enter(sd,StringBuf_Value(db->name))) { // All good
  595. clif_displaymessage(sd->fd, msg_txt(sd,515)); // Instance has been reloaded
  596. instance_reqinfo(sd,p->instance_id);
  597. } else // Something went wrong
  598. ShowError("do_reload_instance: Error setting character at instance start: character_id=%d instance=%s.\n",sd->status.char_id,db->name);
  599. }
  600. mapit_free(iter);
  601. }
  602. void do_init_instance(void) {
  603. InstanceDB = uidb_alloc(DB_OPT_BASE);
  604. InstanceNameDB = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA,0);
  605. instance_readdb();
  606. memset(instance_data, 0, sizeof(instance_data));
  607. memset(&instance_wait, 0, sizeof(instance_wait));
  608. instance_wait.timer = -1;
  609. add_timer_func_list(instance_delete_timer,"instance_delete_timer");
  610. add_timer_func_list(instance_subscription_timer,"instance_subscription_timer");
  611. }
  612. void do_final_instance(void) {
  613. int i;
  614. for( i = 1; i < MAX_INSTANCE_DATA; i++ )
  615. instance_destroy(i);
  616. InstanceDB->destroy(InstanceDB, instance_db_free);
  617. db_destroy(InstanceNameDB);
  618. }