instance.c 19 KB

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