viewing paste Unknown #6380 | Diff

Posted on the
  1. Index: src/map/atcommand.c
  2. ===================================================================
  3. --- src/map/atcommand.c	(revision 17368)
  4. +++ src/map/atcommand.c	(working copy)
  5. @@ -3945,8 +3945,6 @@
  6.  		strcat(atcmd_output, msg_txt(sd,1097)); // GuildLock |
  7.  	if (map[m_id].flag.loadevent)
  8.  		strcat(atcmd_output, msg_txt(sd,1098)); // Loadevent |
  9. -	if (map[m_id].flag.src4instance)
  10. -		strcat(atcmd_output, msg_txt(sd,1099)); // Src4instance |
  11.  	if (map[m_id].flag.chmautojoin)
  12.  		strcat(atcmd_output, msg_txt(sd,1100)); // Chmautojoin |
  13.  	if (map[m_id].flag.nousecart)
  14. @@ -7653,7 +7651,7 @@
  15.  		checkflag(nogo);				checkflag(nobaseexp);
  16.  		checkflag(nojobexp);			checkflag(nomobloot);			checkflag(nomvploot);	checkflag(nightenabled);
  17.  		checkflag(restricted);			checkflag(nodrop);				checkflag(novending);	checkflag(loadevent);
  18. -		checkflag(nochat);				checkflag(partylock);			checkflag(guildlock);	checkflag(src4instance);
  19. +		checkflag(nochat);				checkflag(partylock);			checkflag(guildlock);
  20.  		clif_displaymessage(sd->fd," ");
  21.  		clif_displaymessage(sd->fd,msg_txt(sd,1312)); // Usage: "@mapflag monster_noteleport 1" (0=Off | 1=On)
  22.  		clif_displaymessage(sd->fd,msg_txt(sd,1313)); // Type "@mapflag available" to list the available mapflags.
  23. @@ -7673,7 +7671,7 @@
  24.  	setflag(nogo);				setflag(nobaseexp);
  25.  	setflag(nojobexp);			setflag(nomobloot);			setflag(nomvploot);			setflag(nightenabled);
  26.  	setflag(restricted);		setflag(nodrop);			setflag(novending);			setflag(loadevent);
  27. -	setflag(nochat);			setflag(partylock);			setflag(guildlock);			setflag(src4instance);
  28. +	setflag(nochat);			setflag(partylock);			setflag(guildlock);
  29.  
  30.  	clif_displaymessage(sd->fd,msg_txt(sd,1314)); // Invalid flag name or flag.
  31.  	clif_displaymessage(sd->fd,msg_txt(sd,1312)); // Usage: "@mapflag monster_noteleport 1" (0=Off | 1=On)
  32. @@ -7685,7 +7683,7 @@
  33.  	clif_displaymessage(sd->fd,"nozenypenalty, notrade, noskill, nowarp, nowarpto, noicewall, snow, clouds, clouds2,");
  34.  	clif_displaymessage(sd->fd,"fog, fireworks, sakura, leaves, nogo, nobaseexp, nojobexp, nomobloot,");
  35.  	clif_displaymessage(sd->fd,"nomvploot, nightenabled, restricted, nodrop, novending, loadevent, nochat, partylock,");
  36. -	clif_displaymessage(sd->fd,"guildlock, src4instance");
  37. +	clif_displaymessage(sd->fd,"guildlock");
  38.  
  39.  #undef checkflag
  40.  #undef setflag
  41. Index: src/map/clif.c
  42. ===================================================================
  43. --- src/map/clif.c	(revision 17368)
  44. +++ src/map/clif.c	(working copy)
  45. @@ -9185,10 +9185,10 @@
  46.  	if( !(sd->sc.option&OPTION_INVISIBLE) ) { // increment the number of pvp players on the map
  47.  		map[sd->bl.m].users_pvp++;
  48.  	}
  49. -	if( map[sd->bl.m].instance_id ) {
  50. +/*	if( map[sd->bl.m].instance_id ) {
  51.  		instance[map[sd->bl.m].instance_id].users++;
  52.  		instance_check_idle(map[sd->bl.m].instance_id);
  53. -	}
  54. +	}*/
  55.  	sd->state.debug_remove_map = 0; // temporary state to track double remove_map's [FlavioJS]
  56.  
  57.  	// reset the callshop flag if the player changes map
  58. @@ -15264,102 +15264,95 @@
  59.  
  60.  
  61.  /*==========================================
  62. - * Instancing Window
  63. + * Notifies party members of instance change
  64.   *------------------------------------------*/
  65. -int clif_instance(int instance_id, int type, int flag)
  66. +void clif_instance_create(struct map_session_data *sd, const char *name, int num, int flag)
  67.  {
  68. -	struct map_session_data *sd;
  69. -	struct party_data *p;
  70. -	unsigned char buf[255];
  71. +#if PACKETVER >= 20071128
  72. +	unsigned char buf[65];
  73.  
  74. -	if( (p = party_search(instance[instance_id].party_id)) == NULL || (sd = party_getavailablesd(p)) == NULL )
  75. -		return 0;
  76. +	nullpo_retv(sd);
  77.  
  78. -	switch( type )
  79. -	{
  80. -	case 1:
  81. -		// S 0x2cb <Instance name>.61B <Standby Position>.W
  82. -		// Required to start the instancing information window on Client
  83. -		// This window re-appear each "refresh" of client automatically until type 4 is send to client.
  84. -		WBUFW(buf,0) = 0x02CB;
  85. -		memcpy(WBUFP(buf,2),instance[instance_id].name,INSTANCE_NAME_LENGTH);
  86. -		WBUFW(buf,63) = flag;
  87. -		clif_send(buf,packet_len(0x02CB),&sd->bl,PARTY);
  88. -		break;
  89. -	case 2:
  90. -		// S 0x2cc <Standby Position>.W
  91. -		// To announce Instancing queue creation if no maps available
  92. -		WBUFW(buf,0) = 0x02CC;
  93. -		WBUFW(buf,2) = flag;
  94. -		clif_send(buf,packet_len(0x02CC),&sd->bl,PARTY);
  95. -		break;
  96. -	case 3:
  97. -	case 4:
  98. -		// S 0x2cd <Instance Name>.61B <Instance Remaining Time>.L <Instance Noplayers close time>.L
  99. -		WBUFW(buf,0) = 0x02CD;
  100. -		memcpy(WBUFP(buf,2),instance[instance_id].name,61);
  101. -		if( type == 3 )
  102. -		{
  103. -			WBUFL(buf,63) = (uint32)instance[instance_id].progress_timeout;
  104. -			WBUFL(buf,67) = 0;
  105. -		}
  106. -		else
  107. -		{
  108. -			WBUFL(buf,63) = 0;
  109. -			WBUFL(buf,67) = (uint32)instance[instance_id].idle_timeout;
  110. -		}
  111. -		clif_send(buf,packet_len(0x02CD),&sd->bl,PARTY);
  112. -		break;
  113. -	case 5:
  114. -		// S 0x2ce <Message ID>.L
  115. -		// 0 = Notification (EnterLimitDate update?)
  116. -		// 1 = The Memorial Dungeon expired; it has been destroyed
  117. -		// 2 = The Memorial Dungeon's entry time limit expired; it has been destroyed
  118. -		// 3 = The Memorial Dungeon has been removed.
  119. -		// 4 = Create failure (removes the instance window)
  120. -		WBUFW(buf,0) = 0x02CE;
  121. -		WBUFL(buf,2) = flag;
  122. -		//WBUFL(buf,6) = EnterLimitDate;
  123. -		clif_send(buf,packet_len(0x02CE),&sd->bl,PARTY);
  124. -		break;
  125. +	WBUFW(buf,0) = 0x2cb;
  126. +	memcpy(WBUFP(buf,2),name,61);
  127. +	WBUFB(buf,62) = '\0';	// \0 terminal
  128. +	WBUFW(buf,63) = num;
  129. +	if(flag) {	// A timer has changed or been added
  130. +		clif_send(buf,packet_len(0x2cb),&sd->bl,PARTY);
  131. +	} else {	// No notification
  132. +		memcpy(WFIFOP(sd->fd,0),buf,packet_len(0x2cb));
  133. +		WFIFOSET(sd->fd,packet_len(0x2cb));
  134.  	}
  135. -	return 0;
  136. +#endif
  137. +
  138. +	return;
  139.  }
  140.  
  141. -void clif_instance_join(int fd, int instance_id)
  142. +void clif_instance_changewait(struct map_session_data *sd, int num, int flag)
  143.  {
  144. -	if( instance[instance_id].idle_timer != INVALID_TIMER ) {
  145. -		WFIFOHEAD(fd,packet_len(0x02CD));
  146. -		WFIFOW(fd,0) = 0x02CD;
  147. -		memcpy(WFIFOP(fd,2),instance[instance_id].name,61);
  148. -		WFIFOL(fd,63) = 0;
  149. -		WFIFOL(fd,67) = (uint32)instance[instance_id].idle_timeout;
  150. -		WFIFOSET(fd,packet_len(0x02CD));
  151. -	} else if( instance[instance_id].progress_timer != INVALID_TIMER ) {
  152. -		WFIFOHEAD(fd,packet_len(0x02CD));
  153. -		WFIFOW(fd,0) = 0x02CD;
  154. -		memcpy(WFIFOP(fd,2),instance[instance_id].name,61);
  155. -		WFIFOL(fd,63) = (uint32)instance[instance_id].progress_timeout;;
  156. -		WFIFOL(fd,67) = 0;
  157. -		WFIFOSET(fd,packet_len(0x02CD));
  158. -	} else {
  159. -		WFIFOHEAD(fd,packet_len(0x02CB));
  160. -		WFIFOW(fd,0) = 0x02CB;
  161. -		memcpy(WFIFOP(fd,2),instance[instance_id].name,61);
  162. -		WFIFOW(fd,63) = 0;
  163. -		WFIFOSET(fd,packet_len(0x02CB));
  164. +#if PACKETVER >= 20071128
  165. +	unsigned char buf[4];
  166. +
  167. +	nullpo_retv(sd);
  168. +
  169. +	WBUFW(buf,0) = 0x2cc;
  170. +	WBUFW(buf,2) = num;
  171. +	if(flag) {	// A timer has changed or been added
  172. +		clif_send(buf,packet_len(0x2cc),&sd->bl,PARTY);
  173. +	} else {	// No notification
  174. +		memcpy(WFIFOP(sd->fd,0),buf,packet_len(0x2cc));
  175. +		WFIFOSET(sd->fd,packet_len(0x2cc));
  176.  	}
  177. +#endif
  178. +
  179. +	return;
  180.  }
  181.  
  182. -void clif_instance_leave(int fd)
  183. +void clif_instance_status(struct map_session_data *sd, const char *name, unsigned int limit1, unsigned int limit2, int flag)
  184.  {
  185. -	WFIFOHEAD(fd,packet_len(0x02CE));
  186. -	WFIFOW(fd,0) = 0x02ce;
  187. -	WFIFOL(fd,2) = 4;
  188. -	WFIFOSET(fd,packet_len(0x02CE));
  189. +#if PACKETVER >= 20071128
  190. +	unsigned char buf[71];
  191. +
  192. +	nullpo_retv(sd);
  193. +
  194. +	WBUFW(buf,0) = 0x2cd;
  195. +	memcpy(WBUFP(buf,2),name,61);
  196. +	WBUFB(buf,62) = '\0';	// \0 terminal
  197. +	WBUFL(buf,63) = limit1;
  198. +	WBUFL(buf,67) = limit2;
  199. +	if(flag) {	// A timer has changed or been added
  200. +		clif_send(buf,packet_len(0x2cd),&sd->bl,PARTY);
  201. +	} else {	// No notification
  202. +		memcpy(WFIFOP(sd->fd,0),buf,packet_len(0x2cd));
  203. +		WFIFOSET(sd->fd,packet_len(0x2cd));
  204. +	}
  205. +#endif
  206. +
  207. +	return;
  208.  }
  209.  
  210. +void clif_instance_changestatus(struct map_session_data *sd, int type, unsigned int limit, int flag)
  211. +{
  212. +#if PACKETVER >= 20071128
  213. +	unsigned char buf[10];
  214.  
  215. +	nullpo_retv(sd);
  216. +
  217. +	WBUFW(buf,0) = 0x2ce;
  218. +	WBUFL(buf,2) = type;
  219. +	WBUFL(buf,6) = limit;
  220. +	if(flag) {	// A timer has changed or been added
  221. +		clif_send(buf,packet_len(0x2ce),&sd->bl,PARTY);
  222. +	} else {	// No notification
  223. +		memcpy(WFIFOP(sd->fd,0),buf,packet_len(0x2ce));
  224. +		WFIFOSET(sd->fd,packet_len(0x2ce));
  225. +	}
  226. +#endif
  227. +
  228. +	return;
  229. +}
  230. +
  231. +
  232.  /// Notifies clients about item picked up by a party member (ZC_ITEM_PICKUP_PARTY).
  233.  /// 02b8 <account id>.L <name id>.W <identified>.B <damaged>.B <refine>.B <card1>.W <card2>.W <card3>.W <card4>.W <equip location>.W <item type>.B
  234.  void clif_party_show_picker(struct map_session_data * sd, struct item * item_data)
  235. Index: src/map/clif.h
  236. ===================================================================
  237. --- src/map/clif.h	(revision 17368)
  238. +++ src/map/clif.h	(working copy)
  239. @@ -570,9 +570,10 @@
  240.  void clif_sendbgemblem_single(int fd, struct map_session_data *sd);
  241.  
  242.  // Instancing
  243. -int clif_instance(int instance_id, int type, int flag);
  244. -void clif_instance_join(int fd, int instance_id);
  245. -void clif_instance_leave(int fd);
  246. +void clif_instance_create(struct map_session_data *sd, const char *name, int num, int flag);
  247. +void clif_instance_changewait(struct map_session_data *sd, int num, int flag);
  248. +void clif_instance_status(struct map_session_data *sd, const char *name, unsigned int limit1, unsigned int limit2, int flag);
  249. +void clif_instance_changestatus(struct map_session_data *sd, int type, unsigned int limit, int flag);
  250.  
  251.  // Custom Fonts
  252.  void clif_font(struct map_session_data *sd);
  253. Index: src/map/instance.c
  254. ===================================================================
  255. --- src/map/instance.c	(revision 17368)
  256. +++ src/map/instance.c	(working copy)
  257. @@ -24,27 +24,251 @@
  258.  #include <stdarg.h>
  259.  #include <time.h>
  260.  
  261. +#define MAX_INSTANCE_DB		15	// Max number of instance types
  262. +#define INSTANCE_INTERVAL	60000	// Interval used to check when an instance is to be destroyed (ms)
  263. +#define INSTANCE_LIMIT		30000	// Idle timer before instance is destroyed (ms)
  264. +
  265.  int instance_start = 0; // To keep the last index + 1 of normal map inserted in the map[ARRAY]
  266. -struct s_instance instance[MAX_INSTANCE];
  267.  
  268. +struct instance_data instance_data[MAX_INSTANCE_DATA];
  269.  
  270. -/// Checks whether given instance id is valid or not.
  271. -static bool instance_is_valid(int instance_id)
  272. +static struct instance_db{
  273. +	short type;
  274. +	char name[61];
  275. +	int limit;
  276. +	struct {
  277. +		char mapname[MAP_NAME_LENGTH_EXT];
  278. +		short x, y;
  279. +	} enter;
  280. +	char mapname[MAX_MAP_PER_INSTANCE][MAP_NAME_LENGTH_EXT];
  281. +} instance_db[MAX_INSTANCE_DB];
  282. +
  283. +static struct {
  284. +	int id[MAX_INSTANCE_DATA];
  285. +	int count;
  286. +	int timer;
  287. +} instance_wait;
  288. +
  289. +/*==========================================
  290. + * Searches for an instance ID in the database
  291. + *------------------------------------------*/
  292. +static struct instance_db *instance_searchtype_db(short instance_type)
  293.  {
  294. -	if( instance_id < 1 || instance_id >= ARRAYLENGTH(instance) )
  295. -	{// out of range
  296. -		return false;
  297. +	int i;
  298. +
  299. +	for(i=0; i < MAX_INSTANCE_DB; i++) {
  300. +		if(instance_db[i].type == instance_type)
  301. +			return &instance_db[i];
  302.  	}
  303.  
  304. -	if( instance[instance_id].state == INSTANCE_FREE )
  305. -	{// uninitialized/freed instance slot
  306. -		return false;
  307. +	return NULL;
  308. +}
  309. +
  310. +/*==========================================
  311. + * Searches for an instance name in the database
  312. + *------------------------------------------*/
  313. +static struct instance_db *instance_searchname_db(const char *instance_name)
  314. +{
  315. +	int i;
  316. +
  317. +	for(i=0; i < MAX_INSTANCE_DB; i++) {
  318. +		if(strcmp(instance_db[i].name, instance_name) == 0)
  319. +			return &instance_db[i];
  320.  	}
  321.  
  322. -	return true;
  323. +	return NULL;
  324.  }
  325.  
  326. +/*==========================================
  327. + * Deletes an instance timer (Destroys instance)
  328. + *------------------------------------------*/
  329. +static int instance_delete_timer(int tid, unsigned int tick, int id, intptr_t data)
  330. +{
  331. +	instance_destroy(id);
  332.  
  333. +	return 0;
  334. +}
  335. +
  336. +/*==========================================
  337. + * Create subscription timer for party
  338. + *------------------------------------------*/
  339. +static int instance_subscription_timer(int tid, unsigned int tick, int id, intptr_t data)
  340. +{
  341. +	int i, j, ret;
  342. +	int instance_id = instance_wait.id[0];
  343. +	struct party_data *p;
  344. +
  345. +	if(instance_wait.count == 0 || instance_id <= 0)
  346. +		return 0;
  347. +
  348. +	// Check that maps have been added
  349. +	ret = instance_addmap(instance_id);
  350. +
  351. +	ShowDebug("Number of maps returned:%d.\n", ret);
  352. +
  353. +	// If no maps are created, tell party to wait
  354. +	if(ret == 0) {
  355. +		if((p = party_search(instance_data[instance_id].party_id)) != NULL) {
  356. +			for(i = 0; i < MAX_PARTY; i++) {
  357. +				if(p->data[i].sd) {
  358. +					clif_instance_changewait(p->data[i].sd, 0xffff, 1);
  359. +					break;
  360. +				}
  361. +			}
  362. +		}
  363. +	}
  364. +
  365. +	instance_wait.count--;
  366. +	memmove(&instance_wait.id[0],&instance_wait.id[1],sizeof(instance_wait.id[0])*instance_wait.count);
  367. +	memset(&instance_wait.id[instance_wait.count], 0, sizeof(instance_wait.id[0]));
  368. +
  369. +	for(i = 0; i < instance_wait.count; i++) {
  370. +		if(instance_data[instance_wait.id[i]].state == INSTANCE_IDLE) {
  371. +			if((p = party_search(instance_data[instance_wait.id[i]].party_id)) != NULL) {
  372. +				for(j = 0; j < MAX_PARTY; j++) {
  373. +					if(p->data[j].sd) {
  374. +						clif_instance_changewait(p->data[j].sd, i+1, 1);
  375. +						break;
  376. +					}
  377. +				}
  378. +			}
  379. +		}
  380. +	}
  381. +
  382. +	if(instance_wait.count)
  383. +		instance_wait.timer = add_timer(gettick()+INSTANCE_INTERVAL, instance_subscription_timer, 0, 0);
  384. +	else
  385. +		instance_wait.timer = -1;
  386. +
  387. +	return 0;
  388. +}
  389. +
  390. +/*==========================================
  391. + * Adds timer back to party entering instance
  392. + *------------------------------------------*/
  393. +static int instance_startkeeptimer(struct instance_data *im, short instance_id)
  394. +{
  395. +	struct instance_db *db;
  396. +	struct party_data *p;
  397. +	int i;
  398. +
  399. +	nullpo_retr(0, im);
  400. +
  401. +	// No timer
  402. +	if(im->keep_timer != -1)
  403. +		return 1;
  404. +
  405. +	if((db = instance_searchtype_db(im->type)) == NULL)
  406. +		return 1;
  407. +
  408. +	// Add timer
  409. +	im->keep_limit = (unsigned int)time(NULL) + db->limit;
  410. +	im->keep_timer = add_timer(gettick()+db->limit*1000, instance_delete_timer, instance_id, 0);
  411. +
  412. +	// Notify party of the added instance timer
  413. +	if((p = party_search(im->party_id)) != NULL) {
  414. +		for(i = 0; i < MAX_PARTY; i++) {
  415. +			if(p->data[i].sd) {
  416. +				ShowDebug("Sending startkeeptimer to player.\n");
  417. +				clif_instance_status(p->data[i].sd, db->name, im->keep_limit, im->idle_limit, 1);
  418. +				break;
  419. +			}
  420. +		}
  421. +	}
  422. +
  423. +	return 0;
  424. +}
  425. +
  426. +/*==========================================
  427. + * Creates idle timer
  428. + * Default before instance destroy is 5 minutes
  429. + *------------------------------------------*/
  430. +static int instance_startidletimer(struct instance_data *im, short instance_id)
  431. +{
  432. +	struct instance_db *db;
  433. +	struct party_data *p;
  434. +	int i;
  435. +
  436. +	nullpo_retr(1, im);
  437. +
  438. +	// No current timer
  439. +	if(im->idle_timer != -1)
  440. +		return 1;
  441. +
  442. +	// Add the timer
  443. +	im->idle_limit = (unsigned int)time(NULL) + INSTANCE_LIMIT;
  444. +	im->idle_timer = add_timer(gettick()+INSTANCE_LIMIT, instance_delete_timer, instance_id, 0);
  445. +
  446. +	// Notify party of added instance timer
  447. +	if((p = party_search(im->party_id)) && (db = instance_searchtype_db(im->type))) {
  448. +		for(i = 0; i < MAX_PARTY; i++) {
  449. +			if(p->data[i].sd) {
  450. +				ShowDebug("Sending idle timer to player.\n");
  451. +				clif_instance_status(p->data[i].sd, db->name, im->keep_limit, im->idle_limit, 1);
  452. +				break;
  453. +			}
  454. +		}
  455. +	}
  456. +
  457. +	return 0;
  458. +}
  459. +
  460. +/*==========================================
  461. + * Delete the idle timer
  462. + *------------------------------------------*/
  463. +static int instance_stopidletimer(struct instance_data *im)
  464. +{
  465. +	struct party_data *p;
  466. +	int i;
  467. +
  468. +	nullpo_retr(0, im);
  469. +	
  470. +	// No timer
  471. +	if(im->idle_timer == -1)
  472. +		return 1;
  473. +
  474. +	// Delete the timer - Party has returned or instance is destroyed
  475. +	im->idle_limit = 0;
  476. +	delete_timer(im->idle_timer, instance_delete_timer);
  477. +	im->idle_timer = -1;
  478. +
  479. +	// Notify the party
  480. +	if((p = party_search(im->party_id)) != NULL) {
  481. +		for(i = 0; i < MAX_PARTY; i++) {
  482. +			if(p->data[i].sd) {
  483. +				ShowDebug("Stopping idle timer for player.\n");
  484. +				clif_instance_changestatus(p->data[i].sd, 0, im->idle_limit, 1);
  485. +				break;
  486. +			}
  487. +		}
  488. +	}
  489. +
  490. +	return 0;
  491. +}
  492. +
  493. +/*==========================================
  494. + * Add an NPC to an instance
  495. + *------------------------------------------*/
  496. +static int instance_addnpc_sub(struct block_list *bl, va_list ap)
  497. +{
  498. +	struct npc_data* nd;
  499. +
  500. +	nullpo_retr(0, bl);
  501. +	nullpo_retr(0, ap);
  502. +	nullpo_retr(0, nd = (struct npc_data *)bl);
  503. +
  504. +	return npc_duplicate4instance(nd, va_arg(ap, int));
  505. +}
  506. +
  507. +// Separate function used for reloading
  508. +void instance_addnpc(struct instance_data *im)
  509. +{
  510. +	int i;
  511. +
  512. +	for(i = 0; i < im->cnt_map; i++)
  513. +		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);
  514. +}
  515. +
  516.  /*--------------------------------------
  517.   * name : instance name
  518.   * Return value could be
  519. @@ -53,11 +277,17 @@
  520.   *--------------------------------------*/
  521.  int instance_create(int party_id, const char *name)
  522.  {
  523. -	int i;
  524. -	struct party_data* p;
  525. +	short i;
  526. +	int k;
  527. +	struct instance_db *db = instance_searchname_db(name);
  528. +	struct party_data *p = party_search(party_id);
  529.  
  530. -	if( ( p = party_search(party_id) ) == NULL )
  531. -	{
  532. +	if(db == NULL) {
  533. +		ShowError("instance_create: map %s not found in instance database.\n", name);
  534. +		return -1;
  535. +	}
  536. +
  537. +	if( p == NULL ) {
  538.  		ShowError("instance_create: party %d not found for instance '%s'.\n", party_id, name);
  539.  		return -2;
  540.  	}
  541. @@ -67,422 +297,456 @@
  542.  
  543.  	// Searching a Free Instance
  544.  	// 0 is ignored as this mean "no instance" on maps
  545. -	ARR_FIND(1, MAX_INSTANCE, i, instance[i].state == INSTANCE_FREE);
  546. -	if( i == MAX_INSTANCE )
  547. -	{
  548. -		ShowError("instance_create: no free instances, consider increasing MAX_INSTANCE.\n");
  549. +	ARR_FIND(1, MAX_INSTANCE_DB, i, instance_data[i].state == INSTANCE_FREE);
  550. +	if( i >= MAX_INSTANCE_DB )
  551.  		return -3;
  552. -	}
  553.  
  554. -	instance[i].state = INSTANCE_IDLE;
  555. -	instance[i].instance_id = i;
  556. -	instance[i].idle_timer = INVALID_TIMER;
  557. -	instance[i].idle_timeout = instance[i].idle_timeoutval = 0;
  558. -	instance[i].progress_timer = INVALID_TIMER;
  559. -	instance[i].progress_timeout = 0;
  560. -	instance[i].users = 0;
  561. -	instance[i].party_id = party_id;
  562. -	instance[i].vars = idb_alloc(DB_OPT_RELEASE_DATA);
  563. +	instance_data[i].type = db->type;
  564. +	instance_data[i].state = INSTANCE_IDLE;
  565. +	instance_data[i].party_id = p->party.party_id;
  566. +	instance_data[i].keep_limit = 0;
  567. +	instance_data[i].keep_timer = -1;
  568. +	instance_data[i].idle_limit = 0;
  569. +	instance_data[i].idle_timer = -1;
  570. +	instance_data[i].users = 0;
  571. +	memset(instance_data[i].map, 0, sizeof(instance_data[i].map));
  572.  
  573. -	safestrncpy( instance[i].name, name, sizeof(instance[i].name) );
  574. -	memset( instance[i].map, 0x00, sizeof(instance[i].map) );
  575.  	p->instance_id = i;
  576.  
  577. -	clif_instance(i, 1, 0); // Start instancing window
  578. +	ShowDebug("Created instance id %d.\n",p->instance_id);
  579. +
  580. +	instance_wait.id[instance_wait.count++] = p->instance_id;
  581. +
  582. +	for(k = 0; k < MAX_PARTY; k++) {
  583. +		if(p->data[k].sd) {
  584. +			ShowDebug("Sending created instance timer to player.\n");
  585. +			clif_instance_create(p->data[k].sd, name, instance_wait.count, 1);
  586. +			break;
  587. +		}
  588. +	}
  589. +
  590. +	instance_subscription_timer(0,0,0,0);
  591. +
  592.  	ShowInfo("[Instance] Created: %s.\n", name);
  593. +
  594.  	return i;
  595.  }
  596.  
  597.  /*--------------------------------------
  598. - * Add a map to the instance using src map "name"
  599. + * Adds maps to the instance
  600.   *--------------------------------------*/
  601. -int instance_add_map(const char *name, int instance_id, bool usebasename)
  602. +int instance_addmap(short instance_id)
  603.  {
  604. -	int16 m = map_mapname2mapid(name);
  605. -	int i, im = -1;
  606. -	size_t num_cell, size;
  607. +	int i, m;
  608. +	int cnt_map = 0;
  609. +	struct instance_data *im;
  610. +	struct instance_db *db;
  611. +	struct party_data *p;
  612.  
  613. -	if( m < 0 )
  614. -		return -1; // source map not found
  615. +	if(instance_id <= 0)
  616. +		return 0;
  617.  
  618. -	if( !instance_is_valid(instance_id) )
  619. -	{
  620. -		ShowError("instance_add_map: trying to attach '%s' map to non-existing instance %d.\n", name, instance_id);
  621. -		return -1;
  622. -	}
  623. -	if( instance[instance_id].num_map >= MAX_MAP_PER_INSTANCE )
  624. -	{
  625. -		ShowError("instance_add_map: trying to add '%s' map to instance %d (%s) failed. Please increase MAX_MAP_PER_INSTANCE.\n", name, instance_id, instance[instance_id].name);
  626. -		return -2;
  627. -	}
  628. -	if( map[m].instance_id != 0 )
  629. -	{ // Source map already belong to a Instance.
  630. -		ShowError("instance_add_map: trying to instance already instanced map %s.\n", name);
  631. -		return -4;
  632. -	}
  633. +	im = &instance_data[instance_id];
  634.  
  635. -	ARR_FIND( instance_start, map_num, i, !map[i].name[0] ); // Searching for a Free Map
  636. -	if( i < map_num ) im = i; // Unused map found (old instance)
  637. -	else if( map_num - 1 >= MAX_MAP_PER_SERVER )
  638. -	{ // No more free maps
  639. -		ShowError("instance_add_map: no more free space to create maps on this server.\n");
  640. -		return -5;
  641. +	// If the instance isn't idle, we can't do anything
  642. +	if(im->state != INSTANCE_IDLE) {
  643. +		ShowDebug("Instance isn't idle.\n");
  644. +		return 0;
  645.  	}
  646. -	else im = map_num++; // Using next map index
  647.  
  648. -	memcpy( &map[im], &map[m], sizeof(struct map_data) ); // Copy source map
  649. -	snprintf(map[im].name, MAP_NAME_LENGTH, (usebasename ? "%.3d#%s" : "%.3d%s"), instance_id, name); // Generate Name for Instance Map
  650. -	map[im].index = mapindex_addmap(-1, map[im].name); // Add map index
  651. -
  652. -	if( !map[im].index )
  653. -	{
  654. -		map[im].name[0] = '\0';
  655. -		ShowError("instance_add_map: no more free map indexes.\n");
  656. -		return -3; // No free map index
  657. +	if((db = instance_searchtype_db(im->type)) == NULL) {
  658. +		ShowDebug("Instance isn't in db.\n");
  659. +		return 0;
  660.  	}
  661.  
  662. -	// Reallocate cells
  663. -	num_cell = map[im].xs * map[im].ys;
  664. -	CREATE( map[im].cell, struct mapcell, num_cell );
  665. -	memcpy( map[im].cell, map[m].cell, num_cell * sizeof(struct mapcell) );
  666. +	ShowDebug("Are we pulling currect DB? type=%d name=%s first_map=%s\n", db->type, db->name, db->mapname[0]);
  667.  
  668. -	size = map[im].bxs * map[im].bys * sizeof(struct block_list*);
  669. -	map[im].block = (struct block_list**)aCalloc(size, 1);
  670. -	map[im].block_mob = (struct block_list**)aCalloc(size, 1);
  671. +	// Set to busy, update timers
  672. +	im->state = INSTANCE_BUSY;
  673. +	im->idle_limit = (unsigned int)time(NULL) + INSTANCE_LIMIT;
  674. +	im->idle_timer = add_timer(gettick()+INSTANCE_LIMIT, instance_delete_timer, instance_id, 0);
  675.  
  676. -	memset(map[im].npc, 0x00, sizeof(map[i].npc));
  677. -	map[im].npc_num = 0;
  678. +	// Add the maps
  679. +	for(i = 0; i < MAX_MAP_PER_INSTANCE; i++) {
  680. +		if(strlen(db->mapname[i]) < 1)
  681. +			continue;
  682. +		else if( (m = map_addinstancemap(db->mapname[i], instance_id)) < 0) {
  683. +			// An error occured adding a map
  684. +			ShowError("instance_addmap: No maps added to instance %d.\n",instance_id);
  685. +			return 0;
  686. +		} else {
  687. +			im->map[cnt_map].m = m;
  688. +			im->map[cnt_map].src_m = map_mapname2mapid(db->mapname[i]);
  689. +			cnt_map++;
  690. +		}
  691. +	}
  692.  
  693. -	memset(map[im].moblist, 0x00, sizeof(map[im].moblist));
  694. -	map[im].mob_delete_timer = INVALID_TIMER;
  695. +	im->cnt_map = cnt_map;
  696.  
  697. -	map[im].m = im;
  698. -	map[im].instance_id = instance_id;
  699. -	map[im].instance_src_map = m;
  700. -	map[m].flag.src4instance = 1; // Flag this map as a src map for instances
  701. +	// Create NPCs on all maps
  702. +	instance_addnpc(im);
  703.  
  704. -	instance[instance_id].map[instance[instance_id].num_map++] = im; // Attach to actual instance
  705. -	map_addmap2db(&map[im]);
  706.  
  707. -	return im;
  708. -}
  709. -
  710. -/*--------------------------------------
  711. - * m : source map of this instance
  712. - * party_id : source party of this instance
  713. - * type : result (0 = map id | 1 = instance id)
  714. - *--------------------------------------*/
  715. -int instance_map2imap(int16 m, int instance_id)
  716. -{
  717. - 	int i;
  718. -
  719. -	if( !instance_is_valid(instance_id) )
  720. -	{
  721. -		return -1;
  722. +	// Inform party members of the created instance
  723. +	if((p = party_search(im->party_id)) != NULL) {
  724. +		for(i = 0; i < MAX_PARTY; i++) {
  725. +			ShowDebug("Sending instance timer to player.\n");
  726. +			clif_instance_status(p->data[i].sd, db->name, im->keep_limit, im->idle_limit, 1);
  727. +			break;
  728. +		}
  729.  	}
  730.  
  731. -	for( i = 0; i < instance[instance_id].num_map; i++ )
  732. - 	{
  733. -		if( instance[instance_id].map[i] && map[instance[instance_id].map[i]].instance_src_map == m )
  734. -			return instance[instance_id].map[i];
  735. - 	}
  736. - 	return -1;
  737. +	return cnt_map;
  738.  }
  739.  
  740. -/*--------------------------------------
  741. - * m : source map
  742. +
  743. +/*==========================================
  744. + * Returns an instance map ID using a map name
  745. + * name : source map
  746.   * instance_id : where to search
  747. - * result : mapid of map "m" in this instance
  748. - *--------------------------------------*/
  749. -int instance_mapid2imapid(int16 m, int instance_id)
  750. + * result : mapid of map "name" in this instance
  751. + *------------------------------------------*/
  752. +int instance_mapname2mapid(const char *name, short instance_id)
  753.  {
  754. -	if( map[m].flag.src4instance == 0 )
  755. -		return m; // not instances found for this map
  756. -	else if( map[m].instance_id )
  757. -	{ // This map is a instance, not a src map instance
  758. -		ShowError("map_instance_mapid2imapid: already instanced (%d / %d)\n", m, instance_id);
  759. -		return -1;
  760. +	struct instance_data *im;
  761. +	int m = map_mapname2mapid(name);
  762. +	char iname[12];
  763. +	int i;
  764. +
  765. +	if(m < 0) {
  766. +		ShowError("instance_mapname2mapid: map name %s does not exist.\n",name);
  767. +		return m;
  768.  	}
  769.  
  770. -	if( !instance_is_valid(instance_id) )
  771. -		return -1;
  772. +	ShowDebug("We attempt to enter instance map %s. Index=%d.\n",name,mapindex_name2id(name));
  773.  
  774. -	return instance_map2imap(m, instance_id);
  775. -}
  776. +	if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  777. +		return m;
  778.  
  779. -/*--------------------------------------
  780. - * map_instance_map_npcsub
  781. - * Used on Init instance. Duplicates each script on source map
  782. - *--------------------------------------*/
  783. -int instance_map_npcsub(struct block_list* bl, va_list args)
  784. -{
  785. -	struct npc_data* nd = (struct npc_data*)bl;
  786. -	int16 m = va_arg(args, int); // Destination Map
  787. +	im = &instance_data[instance_id];
  788. +	if(im->state != INSTANCE_BUSY)
  789. +		return m;
  790.  
  791. -	npc_duplicate4instance(nd, m);
  792. -	return 1;
  793. +	for(i = 0; i < MAX_MAP_PER_INSTANCE; i++) {
  794. +		if(im->map[i].src_m == m) {
  795. +			snprintf(iname, sizeof(iname), "%03d%s", instance_id, name);
  796. +			ShowDebug("Now we pull a map name %s and ID %d.\n",iname,mapindex_name2id(iname));
  797. +			return mapindex_name2id(iname);
  798. +		}
  799. +	}
  800. +
  801. +	return m;
  802.  }
  803.  
  804. -/*--------------------------------------
  805. - * Init all map on the instance. Npcs are created here
  806. - *--------------------------------------*/
  807. -void instance_init(int instance_id)
  808. +/*==========================================
  809. + * Removes a instance, all its maps and npcs.
  810. + *------------------------------------------*/
  811. +int instance_destroy(short instance_id)
  812.  {
  813. -	int i;
  814. +	struct instance_data *im;
  815. +	struct party_data *p;
  816. +	int i, j, type = 0, count = 0;
  817. +	unsigned int now = (unsigned int)time(NULL);
  818.  
  819. -	if( !instance_is_valid(instance_id) )
  820. -		return; // nothing to do
  821. +	if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  822. +		return 1;
  823.  
  824. -	for( i = 0; i < instance[instance_id].num_map; i++ )
  825. -		map_foreachinmap(instance_map_npcsub, map[instance[instance_id].map[i]].instance_src_map, BL_NPC, instance[instance_id].map[i]);
  826. +	im = &instance_data[instance_id];
  827.  
  828. -	instance[instance_id].state = INSTANCE_BUSY;
  829. -	ShowInfo("[Instance] Initialized %s.\n", instance[instance_id].name);
  830. -}
  831. +	if(im->state == INSTANCE_FREE)
  832. +		return 1;
  833.  
  834. -/*--------------------------------------
  835. - * Used on instance deleting process.
  836. - * Warps all players on each instance map to its save points.
  837. - *--------------------------------------*/
  838. -int instance_del_load(struct map_session_data* sd, va_list args)
  839. -{
  840. -	int16 m = va_arg(args,int);
  841. -	if( !sd || sd->bl.m != m )
  842. -		return 0;
  843. +	if(im->state == INSTANCE_IDLE) {
  844. +		for(i = 0; i < instance_wait.count; i++) {
  845. +			if(instance_wait.id[i] == instance_id) {
  846. +				instance_wait.count--;
  847. +				memmove(&instance_wait.id[i],&instance_wait.id[i+1],sizeof(instance_wait.id[0])*(instance_wait.count-i));
  848. +				memset(&instance_wait.id[instance_wait.count], 0, sizeof(instance_wait.id[0]));
  849.  
  850. -	pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_OUTSIGHT);
  851. -	return 1;
  852. -}
  853. +				for(i = 0; i < instance_wait.count; i++) {
  854. +					if(instance_data[instance_wait.id[i]].state == INSTANCE_IDLE) {
  855. +						if((p = party_search(instance_data[instance_wait.id[i]].party_id)) != NULL) {
  856. +							for(j = 0; j < MAX_PARTY; j++) {
  857. +								if(p->data[j].sd) {
  858. +									clif_instance_changewait(p->data[j].sd, i+1, 1);
  859. +									break;
  860. +								}
  861. +							}
  862. +						}
  863. +					}
  864. +				}
  865.  
  866. -/* for npcs behave differently when being unloaded within a instance */
  867. -int instance_cleanup_sub(struct block_list *bl, va_list ap) {
  868. -	nullpo_ret(bl);
  869. +				if(instance_wait.count)
  870. +					instance_wait.timer = add_timer(gettick()+INSTANCE_INTERVAL, instance_subscription_timer, 0, 0);
  871. +				else
  872. +					instance_wait.timer = -1;
  873. +				type = 0;
  874. +				break;
  875. +			}
  876. +		}
  877. +	} else {
  878. +		if(im->keep_limit && im->keep_limit <= now)
  879. +			type = 1;
  880. +		else if(im->idle_limit && im->idle_limit <= now)
  881. +			type = 2;
  882. +		else
  883. +			type = 3;
  884.  
  885. -	switch(bl->type) {
  886. -		case BL_PC:
  887. -			map_quit((struct map_session_data *) bl);
  888. -			break;
  889. -		case BL_NPC:
  890. -			npc_unload((struct npc_data *)bl,true);
  891. -			break;
  892. -		case BL_MOB:
  893. -			unit_free(bl,CLR_OUTSIGHT);
  894. -			break;
  895. -		case BL_PET:
  896. -			//There is no need for this, the pet is removed together with the player. [Skotlex]
  897. -			break;
  898. -		case BL_ITEM:
  899. -			map_clearflooritem(bl);
  900. -			break;
  901. -		case BL_SKILL:
  902. -			skill_delunit((struct skill_unit *) bl);
  903. -			break;
  904. +		for(i = 0; i < MAX_MAP_PER_INSTANCE; i++)
  905. +			count += map_delinstancemap(im->map[i].m);
  906.  	}
  907.  
  908. -	return 1;
  909. +	if(im->keep_timer != -1) {
  910. +		delete_timer(im->keep_timer, instance_delete_timer);
  911. +		im->keep_timer = -1;
  912. +	}
  913. +	if(im->idle_timer != -1) {
  914. +		delete_timer(im->idle_timer, instance_delete_timer);
  915. +		im->idle_timer = -1;
  916. +	}
  917. +
  918. +	if((p = party_search(im->party_id))) {
  919. +		p->instance_id = 0;
  920. +		for(j = 0; j < MAX_PARTY; j++) {
  921. +			if(p->data[j].sd) {
  922. +				if(type)
  923. +					clif_instance_changestatus(p->data[j].sd, type, 0, 1);
  924. +				else
  925. +					clif_instance_changewait(p->data[j].sd, 0xffff, 1);
  926. +				break;
  927. +			}
  928. +		}
  929. +	}
  930. +
  931. +	im->type = 0;
  932. +	im->state = INSTANCE_FREE;
  933. +	im->party_id = 0;
  934. +	im->keep_limit = 0;
  935. +	im->idle_limit = 0;
  936. +	im->users = 0;
  937. +	ShowInfo("[Instance] Destroyed %d.\n", instance_data[i].map);
  938. +
  939. +	memset(instance_data[i].map, 0, sizeof(instance_data[i].map));
  940. +
  941. +	return 0;
  942.  }
  943.  
  944. -/*--------------------------------------
  945. - * Removes a simple instance map
  946. - *--------------------------------------*/
  947. -void instance_del_map(int16 m)
  948. +/*==========================================
  949. + * Allows a user to enter an instance
  950. + *------------------------------------------*/
  951. +int instance_enter(struct map_session_data *sd, const char *name)
  952.  {
  953. -	int i;
  954. -	if( m <= 0 || !map[m].instance_id )
  955. -	{
  956. -		ShowError("Tried to remove non-existing instance map (%d)\n", m);
  957. -		return;
  958. -	}
  959. +	struct instance_data *im;
  960. +	struct instance_db *db = instance_searchname_db(name);
  961. +	struct party_data *p;
  962. +	int m;
  963.  
  964. -	map_foreachpc(instance_del_load, m);
  965. -	map_foreachinmap(instance_cleanup_sub, m, BL_ALL);
  966. +	nullpo_retr(-1, sd);
  967.  
  968. -	if( map[m].mob_delete_timer != INVALID_TIMER )
  969. -		delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
  970. +	if(db == NULL)
  971. +		return 1;
  972.  
  973. -	mapindex_removemap( map[m].index );
  974. +	// Character must be in instance party
  975. +	if(sd->status.party_id == 0)
  976. +		return 2;
  977. +	if((p = party_search(sd->status.party_id)) == NULL)
  978. +		return 2;
  979.  
  980. -	// Free memory
  981. -	aFree(map[m].cell);
  982. -	aFree(map[m].block);
  983. -	aFree(map[m].block_mob);
  984. +	// Party must have an instance
  985. +	if(p->instance_id == 0)
  986. +		return 3;
  987.  
  988. -	// Remove from instance
  989. -	for( i = 0; i < instance[map[m].instance_id].num_map; i++ )
  990. -	{
  991. -		if( instance[map[m].instance_id].map[i] == m )
  992. -		{
  993. -			instance[map[m].instance_id].num_map--;
  994. -			for( ; i < instance[map[m].instance_id].num_map; i++ )
  995. -				instance[map[m].instance_id].map[i] = instance[map[m].instance_id].map[i+1];
  996. -			i = -1;
  997. -			break;
  998. -		}
  999. -	}
  1000. -	if( i == instance[map[m].instance_id].num_map )
  1001. -		ShowError("map_instance_del: failed to remove %s from instance list (%s): %d\n", map[m].name, instance[map[m].instance_id].name, m);
  1002. +	im = &instance_data[p->instance_id];
  1003. +	if(im->party_id != p->party.party_id)
  1004. +		return 3;
  1005. +	if(im->state != INSTANCE_BUSY)
  1006. +		return 3;
  1007. +	if(im->type != db->type)
  1008. +		return 3;
  1009.  
  1010. -	map_removemapdb(&map[m]);
  1011. -	memset(&map[m], 0x00, sizeof(map[0]));
  1012. +	// Does the instance match?
  1013. +	if((m = instance_mapname2mapid(db->enter.mapname, p->instance_id)) < 0)
  1014. +		return 4;
  1015.  
  1016. -	/* for it is default and makes it not try to delete a non-existent timer since we did not delete this entry. */
  1017. -	map[m].mob_delete_timer = INVALID_TIMER;
  1018. -}
  1019. +	if(pc_setpos(sd, m, db->enter.x, db->enter.y, 0))
  1020. +		return 4;
  1021.  
  1022. -/*--------------------------------------
  1023. - * Timer to destroy instance by process or idle
  1024. - *--------------------------------------*/
  1025. -int instance_destroy_timer(int tid, unsigned int tick, int id, intptr_t data)
  1026. -{
  1027. -	instance_destroy(id);
  1028. +	// If there was an idle timer, let's stop it
  1029. +	instance_stopidletimer(im);
  1030. +
  1031. +	// Now we start the instance timer
  1032. +	instance_startkeeptimer(im, p->instance_id);
  1033. +
  1034.  	return 0;
  1035.  }
  1036.  
  1037. -/*--------------------------------------
  1038. - * Removes a instance, all its maps and npcs.
  1039. - *--------------------------------------*/
  1040. -void instance_destroy(int instance_id)
  1041. +/*==========================================
  1042. + * Request some info about the instance
  1043. + *------------------------------------------*/
  1044. +int instance_reqinfo(struct map_session_data *sd, short instance_id)
  1045.  {
  1046. -	int last = 0, type;
  1047. -	struct party_data *p;
  1048. -	time_t now = time(NULL);
  1049. +	struct instance_data *im;
  1050. +	struct instance_db *db;
  1051. +	int i;
  1052.  
  1053. -	if( !instance_is_valid(instance_id) )
  1054. -		return; // nothing to do
  1055. +	nullpo_retr(1, sd);
  1056.  
  1057. -	if( instance[instance_id].progress_timeout && instance[instance_id].progress_timeout <= now )
  1058. -		type = 1;
  1059. -	else if( instance[instance_id].idle_timeout && instance[instance_id].idle_timeout <= now )
  1060. -		type = 2;
  1061. -	else
  1062. -		type = 3;
  1063. +	if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  1064. +		return 1;
  1065.  
  1066. -	clif_instance(instance_id, 5, type); // Report users this instance has been destroyed
  1067. +	im = &instance_data[instance_id];
  1068.  
  1069. -	while( instance[instance_id].num_map && last != instance[instance_id].map[0] )
  1070. -	{ // Remove all maps from instance
  1071. -		last = instance[instance_id].map[0];
  1072. -		instance_del_map( instance[instance_id].map[0] );
  1073. +	if((db = instance_searchtype_db(im->type)) == NULL)
  1074. +		return 1;
  1075. +
  1076. +	// Say it's created if instance is not busy
  1077. +	if(im->state == INSTANCE_IDLE) {
  1078. +		for(i = 0; i < instance_wait.count; i++) {
  1079. +			if(instance_wait.id[i] == instance_id) {
  1080. +				clif_instance_create(sd, db->name, i+1, 0);
  1081. +				break;
  1082. +			}
  1083. +		}
  1084.  	}
  1085. +	// Give info on the instance if busy
  1086. +	else if(im->state == INSTANCE_BUSY)
  1087. +		clif_instance_status(sd, db->name, im->keep_limit, im->idle_limit, 0);
  1088.  
  1089. -	if( instance[instance_id].vars )
  1090. -		db_destroy(instance[instance_id].vars);
  1091. +	return 0;
  1092. +}
  1093.  
  1094. -	if( instance[instance_id].progress_timer != INVALID_TIMER )
  1095. -		delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
  1096. -	if( instance[instance_id].idle_timer != INVALID_TIMER )
  1097. -		delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
  1098. +/*==========================================
  1099. + * Add players to the instance (for timers)
  1100. + *------------------------------------------*/
  1101. +int instance_addusers(short instance_id)
  1102. +{
  1103. +	struct instance_data *im;
  1104.  
  1105. -	instance[instance_id].vars = NULL;
  1106. +	if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  1107. +		return 1;
  1108.  
  1109. -	if( instance[instance_id].party_id && (p = party_search(instance[instance_id].party_id)) != NULL )
  1110. -		p->instance_id = 0; // Update Party information
  1111. +	im = &instance_data[instance_id];
  1112. +	if(im->state != INSTANCE_BUSY)
  1113. +		return 1;
  1114.  
  1115. -	ShowInfo("[Instance] Destroyed %s.\n", instance[instance_id].name);
  1116. -	memset( &instance[instance_id], 0x00, sizeof(instance[0]) );
  1117. +	// Increment the amount of players in instance
  1118. +	im->users++;
  1119.  
  1120. -	instance[instance_id].state = INSTANCE_FREE;
  1121. +	// Stop the idle timer if we had one
  1122. +	instance_stopidletimer(im);
  1123. +
  1124. +	// Start the instance keep timer
  1125. +	instance_startkeeptimer(im, instance_id);
  1126. +
  1127. +	return 0;
  1128.  }
  1129.  
  1130. -/*--------------------------------------
  1131. - * Checks if there are users in the instance or not to start idle timer
  1132. - *--------------------------------------*/
  1133. -void instance_check_idle(int instance_id)
  1134. +/*==========================================
  1135. + * Delete players from the instance (for timers)
  1136. + *------------------------------------------*/
  1137. +int instance_delusers(short instance_id)
  1138.  {
  1139. -	bool idle = true;
  1140. -	time_t now = time(NULL);
  1141. +	struct instance_data *im;
  1142.  
  1143. -	if( !instance_is_valid(instance_id) || instance[instance_id].idle_timeoutval == 0 )
  1144. -		return;
  1145. +	if(instance_id <= 0 || instance_id > MAX_INSTANCE_DATA)
  1146. +		return 1;
  1147.  
  1148. -	if( instance[instance_id].users )
  1149. -		idle = false;
  1150. +	im = &instance_data[instance_id];
  1151. +	if(im->state != INSTANCE_BUSY)
  1152. +		return 1;
  1153.  
  1154. -	if( instance[instance_id].idle_timer != INVALID_TIMER && !idle )
  1155. -	{
  1156. -		delete_timer(instance[instance_id].idle_timer, instance_destroy_timer);
  1157. -		instance[instance_id].idle_timer = INVALID_TIMER;
  1158. -		instance[instance_id].idle_timeout = 0;
  1159. -		clif_instance(instance_id, 3, 0); // Notify instance users normal instance expiration
  1160. -	}
  1161. -	else if( instance[instance_id].idle_timer == INVALID_TIMER && idle )
  1162. -	{
  1163. -		instance[instance_id].idle_timeout = now + instance[instance_id].idle_timeoutval;
  1164. -		instance[instance_id].idle_timer = add_timer( gettick() + (unsigned int)instance[instance_id].idle_timeoutval * 1000, instance_destroy_timer, instance_id, 0);
  1165. -		clif_instance(instance_id, 4, 0); // Notify instance users it will be destroyed of no user join it again in "X" time
  1166. -	}
  1167. +	// Increment the amount of players in instance
  1168. +	im->users--;
  1169. +
  1170. +	// If no one is in the instance, start the idle timer
  1171. +	if(im->users <= 0)
  1172. +		instance_startidletimer(im, instance_id);
  1173. +
  1174. +	return 0;
  1175.  }
  1176.  
  1177. -/*--------------------------------------
  1178. - * Set instance Timers
  1179. - *--------------------------------------*/
  1180. -void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout)
  1181. +/*==========================================
  1182. + * Read the instance_db.txt file
  1183. + *------------------------------------------*/
  1184. +static bool instance_readdb(char* str[], int columns, int current)
  1185.  {
  1186. -	time_t now = time(0);
  1187. +	
  1188. +	int i, type, k=0;
  1189.  
  1190. -	if( !instance_is_valid(instance_id) )
  1191. -		return;
  1192. +	type=atoi(str[0]);
  1193.  
  1194. -	if( instance[instance_id].progress_timer != INVALID_TIMER )
  1195. -		delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
  1196. -	if( instance[instance_id].idle_timer != INVALID_TIMER )
  1197. -		delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
  1198. +	instance_db[type].type=type;
  1199. +	safestrncpy(instance_db[type].name, str[1], 24);
  1200. +	instance_db[type].limit=atoi(str[2]);
  1201. +	safestrncpy(instance_db[type].enter.mapname, str[3], MAP_NAME_LENGTH);
  1202. +	instance_db[type].enter.x=atoi(str[4]);
  1203. +	instance_db[type].enter.y=atoi(str[5]);
  1204.  
  1205. -	if( progress_timeout )
  1206. -	{
  1207. -		instance[instance_id].progress_timeout = now + progress_timeout;
  1208. -		instance[instance_id].progress_timer = add_timer( gettick() + progress_timeout * 1000, instance_destroy_timer, instance_id, 0);
  1209. +	//Instance maps
  1210. +	for(i=6; i<columns; i++) {
  1211. +		if(strlen(str[i])) {
  1212. +			ShowDebug("Adding map name %s to type %d\n", str[i], atoi(str[0]));
  1213. +			safestrncpy(instance_db[type].mapname[k], str[i], MAP_NAME_LENGTH);
  1214. +			k++;
  1215. +		}
  1216.  	}
  1217. -	else
  1218. -	{
  1219. -		instance[instance_id].progress_timeout = 0;
  1220. -		instance[instance_id].progress_timer = INVALID_TIMER;
  1221. -	}
  1222.  
  1223. -	if( idle_timeout )
  1224. -	{
  1225. -		instance[instance_id].idle_timeoutval = idle_timeout;
  1226. -		instance[instance_id].idle_timer = INVALID_TIMER;
  1227. -		instance_check_idle(instance_id);
  1228. -	}
  1229. -	else
  1230. -	{
  1231. -		instance[instance_id].idle_timeoutval = 0;
  1232. -		instance[instance_id].idle_timeout = 0;
  1233. -		instance[instance_id].idle_timer = INVALID_TIMER;
  1234. -	}
  1235. -
  1236. -	if( instance[instance_id].idle_timer == INVALID_TIMER && instance[instance_id].progress_timer != INVALID_TIMER )
  1237. -		clif_instance(instance_id, 3, 0);
  1238. +	return true;
  1239.  }
  1240.  
  1241. -/*--------------------------------------
  1242. - * Checks if sd in on a instance and should be kicked from it
  1243. - *--------------------------------------*/
  1244. -void instance_check_kick(struct map_session_data *sd)
  1245. +/*==========================================
  1246. + * Reloads the instance in runtime (reloadscript)
  1247. + *------------------------------------------*/
  1248. +void do_reload_instance(void)
  1249.  {
  1250. -	int16 m = sd->bl.m;
  1251. +	struct instance_data *im;
  1252. +	struct instance_db *db;
  1253. +	struct s_mapiterator* iter;
  1254. +	struct map_session_data* pl_sd;
  1255. +	int i;
  1256.  
  1257. -	clif_instance_leave(sd->fd);
  1258. -	if( map[m].instance_id )
  1259. -	{ // User was on the instance map
  1260. -		if( map[m].save.map )
  1261. -			pc_setpos(sd, map[m].save.map, map[m].save.x, map[m].save.y, CLR_TELEPORT);
  1262. -		else
  1263. -			pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  1264. +	for( i = 1; i < MAX_INSTANCE_DATA; i++ ) {
  1265. +		im = &instance_data[i];
  1266. +		if(!im->type)
  1267. +			continue;
  1268. +		else {
  1269. +			// First we load the NPCs again
  1270. +			instance_addnpc(im);
  1271. +			// Create new keep timer
  1272. +			if((db = instance_searchtype_db(im->type)) != NULL)
  1273. +				im->keep_limit = (unsigned int)time(NULL) + db->limit;
  1274. +			// Reset player to instance beginning
  1275. +			iter = mapit_getallusers();
  1276. +			for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  1277. +				if(pl_sd && map[pl_sd->bl.m].instance_id) {
  1278. +					struct party_data *p;
  1279. +					if(!(p = party_search(pl_sd->status.party_id)) || instance_enter(pl_sd,db->name)) // Something went wrong
  1280. +						ShowError("do_reload_instance: Error setting character at instance start: character_id=%d instance=%s.\n",pl_sd->status.char_id,db->name);
  1281. +					else // All good
  1282. +						clif_displaymessage(pl_sd->fd, msg_txt(pl_sd,515)); // Instance has been reloaded
  1283. +				}
  1284. +			mapit_free(iter);
  1285. +		}
  1286.  	}
  1287.  }
  1288.  
  1289. +void do_init_instance(void)
  1290. +{
  1291. +	memset(&instance_db, 0, sizeof(instance_db));
  1292. +
  1293. +	sv_readdb(db_path, DBPATH"instance_db.txt", ',', 7, 14, MAX_INSTANCE_DB, &instance_readdb);
  1294. +	memset(instance_data, 0, sizeof(instance_data));
  1295. +	memset(&instance_wait, 0, sizeof(instance_wait));
  1296. +	instance_wait.timer = -1;
  1297. +
  1298. +	add_timer_func_list(instance_delete_timer,"instance_delete_timer");
  1299. +	add_timer_func_list(instance_subscription_timer,"instance_subscription_timer");
  1300. +}
  1301. +
  1302.  void do_final_instance(void)
  1303.  {
  1304.  	int i;
  1305.  
  1306. -	for( i = 1; i < MAX_INSTANCE; i++ )
  1307. +	for( i = 1; i < MAX_INSTANCE_DATA; i++ )
  1308.  		instance_destroy(i);
  1309.  }
  1310. -
  1311. -void do_init_instance(void)
  1312. -{
  1313. -	memset(instance, 0x00, sizeof(instance));
  1314. -	add_timer_func_list(instance_destroy_timer, "instance_destroy_timer");
  1315. -}
  1316. Index: src/map/instance.h
  1317. ===================================================================
  1318. --- src/map/instance.h	(revision 17368)
  1319. +++ src/map/instance.h	(working copy)
  1320. @@ -4,48 +4,45 @@
  1321.  #ifndef _INSTANCE_H_
  1322.  #define _INSTANCE_H_
  1323.  
  1324. -#define MAX_MAP_PER_INSTANCE 10
  1325. -#define MAX_INSTANCE 500
  1326. +#define MAX_INSTANCE_DATA	500	// Essentially how many instances we can create, but instance creation is primarily decided by MAX_MAP_PER_SERVER	
  1327. +#define MAX_MAP_PER_INSTANCE 	8	// Max number of maps per instance
  1328.  
  1329.  #define INSTANCE_NAME_LENGTH (60+1)
  1330.  
  1331.  typedef enum instance_state { INSTANCE_FREE, INSTANCE_IDLE, INSTANCE_BUSY } instance_state;
  1332.  
  1333. -struct s_instance {
  1334. -	char name[INSTANCE_NAME_LENGTH]; // Instance Name - required for clif functions.
  1335. -	instance_state state;
  1336. -	short instance_id;
  1337. +struct instance_data {
  1338. +	short type, cnt_map;
  1339. +	int state;
  1340.  	int party_id;
  1341. -
  1342. -	int map[MAX_MAP_PER_INSTANCE];
  1343. -	int num_map;
  1344. +	unsigned int keep_limit;
  1345. +	int keep_timer;
  1346. +	unsigned int idle_limit;
  1347. +	int idle_timer;
  1348.  	int users;
  1349.  
  1350.  	struct DBMap* vars; // Instance Variable for scripts
  1351. -
  1352. -	int progress_timer;
  1353. -	time_t progress_timeout;
  1354. -
  1355. -	int idle_timer;
  1356. -	time_t idle_timeout, idle_timeoutval;
  1357. +	struct {
  1358. +		int m;
  1359. +		int src_m;
  1360. +	} map[MAX_MAP_PER_INSTANCE];
  1361.  };
  1362.  
  1363.  extern int instance_start;
  1364. -extern struct s_instance instance[MAX_INSTANCE];
  1365. +extern struct instance_data instance_data[MAX_INSTANCE_DATA];
  1366.  
  1367.  int instance_create(int party_id, const char *name);
  1368. -int instance_add_map(const char *name, int instance_id, bool usebasename);
  1369. -void instance_del_map(int16 m);
  1370. -int instance_map2imap(int16 m, int instance_id);
  1371. -int instance_mapid2imapid(int16 m, int instance_id);
  1372. -void instance_destroy(int instance_id);
  1373. -void instance_init(int instance_id);
  1374. +int instance_destroy(short instance_id);
  1375. +int instance_enter(struct map_session_data *sd, const char *name);
  1376. +int instance_reqinfo(struct map_session_data *sd, short instance_id);
  1377. +int instance_addusers(short instance_id);
  1378. +int instance_delusers(short instance_id);
  1379. +int instance_mapname2mapid(const char *name, short instance_id);
  1380. +int instance_addmap(short instance_id);
  1381.  
  1382. -void instance_check_idle(int instance_id);
  1383. -void instance_check_kick(struct map_session_data *sd);
  1384. -void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout);
  1385. -
  1386. +void instance_addnpc(struct instance_data *im);
  1387. +void do_reload_instance(void);
  1388. +void do_init_instance(void);
  1389.  void do_final_instance(void);
  1390. -void do_init_instance(void);
  1391.  
  1392.  #endif
  1393. Index: src/map/map.c
  1394. ===================================================================
  1395. --- src/map/map.c	(revision 17368)
  1396. +++ src/map/map.c	(working copy)
  1397. @@ -2170,6 +2170,137 @@
  1398.  	return true;
  1399.  }
  1400.  
  1401. +/*==========================================
  1402. + * Add an instance map
  1403. + *------------------------------------------*/
  1404. +int map_addinstancemap(const char *name, int id)
  1405. +{
  1406. +	int src_m = map_mapname2mapid(name);
  1407. +	int dst_m = -1, i;
  1408. +	size_t size;
  1409. +
  1410. +	if(src_m < 0)
  1411. +		return -1;
  1412. +
  1413. +	if(strlen(name) > 20) {
  1414. +		// against buffer overflow
  1415. +		ShowError("map_addisntancemap: can't add long map name \"%s\"\n", name);
  1416. +		return -2;
  1417. +	}
  1418. +
  1419. +	for(i = instance_start; i < MAX_MAP_PER_SERVER; i++) {
  1420. +		if(!map[i].name[0])
  1421. +			break;
  1422. +	}
  1423. +	if(i < map_num) // Destination map value overwrites another
  1424. +		dst_m = i;
  1425. +	else if(i < MAX_MAP_PER_SERVER) // Destination map value increments to new map
  1426. +		dst_m = map_num++;
  1427. +	else {
  1428. +		// Out of bounds
  1429. +		ShowError("map_addinstancemap failed. map_num(%d) > map_max(%d)\n",map_num, MAX_MAP_PER_SERVER);
  1430. +		return -3;
  1431. +	}
  1432. +
  1433. +	// Copy the map
  1434. +	memcpy(&map[dst_m], &map[src_m], sizeof(struct map_data));
  1435. +
  1436. +	// Alter the name
  1437. +	snprintf(map[dst_m].name, sizeof(map[dst_m].name), "%03d%s", id, name);
  1438. +	map[dst_m].name[MAP_NAME_LENGTH-1] = '\0';
  1439. +
  1440. +	map[dst_m].m = dst_m;
  1441. +	map[dst_m].instance_id = id;
  1442. +	map[dst_m].users = 0;
  1443. +
  1444. +	memset(map[dst_m].npc, 0, sizeof(map[dst_m].npc));
  1445. +	map[dst_m].npc_num = 0;
  1446. +
  1447. +	size = map[dst_m].bxs * map[dst_m].bys * sizeof(struct block_list*);
  1448. +	map[dst_m].block = (struct block_list **)aCalloc(1,size);
  1449. +	map[dst_m].block_mob = (struct block_list **)aCalloc(1,size);
  1450. +
  1451. +	map[dst_m].index = mapindex_addmap(-1, map[dst_m].name);
  1452. +
  1453. +	map_addmap2db(&map[dst_m]);
  1454. +
  1455. +	ShowDebug("Created map %s for instancing with id %d.\n",map[dst_m].name, map[dst_m].m);
  1456. +
  1457. +	return dst_m;
  1458. +}
  1459. +
  1460. +/*==========================================
  1461. + * Set player to save point when they leave
  1462. + *------------------------------------------*/
  1463. +static int map_instancemap_leave(struct block_list *bl, va_list ap)
  1464. +{
  1465. +	struct map_session_data* sd;
  1466. +
  1467. +	nullpo_retr(0, bl);
  1468. +	nullpo_retr(0, sd = (struct map_session_data *)bl);
  1469. +
  1470. +	pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  1471. +
  1472. +	return 1;
  1473. +}
  1474. +
  1475. +/*==========================================
  1476. + * Remove all units from instance
  1477. + *------------------------------------------*/
  1478. +static int map_instancemap_clean(struct block_list *bl, va_list ap)
  1479. +{
  1480. +	nullpo_retr(0, bl);
  1481. +	switch(bl->type) {
  1482. +		case BL_PC:
  1483. +			map_quit((struct map_session_data *) bl);
  1484. +			break;
  1485. +		case BL_NPC:
  1486. +			npc_unload((struct npc_data *)bl,true);
  1487. +			break;
  1488. +		case BL_MOB:
  1489. +			unit_free(bl,CLR_OUTSIGHT);
  1490. +			break;
  1491. +		case BL_PET:
  1492. +			//There is no need for this, the pet is removed together with the player. [Skotlex]
  1493. +			break;
  1494. +		case BL_ITEM:
  1495. +			map_clearflooritem(bl);
  1496. +			break;
  1497. +		case BL_SKILL:
  1498. +			skill_delunit((struct skill_unit *) bl);
  1499. +			break;
  1500. +	}
  1501. +
  1502. +	return 1;
  1503. +}
  1504. +
  1505. +/*==========================================
  1506. + * Deleting an instance map
  1507. + *------------------------------------------*/
  1508. +int map_delinstancemap(int m)
  1509. +{
  1510. +	if(m < 0)
  1511. +		return 0;
  1512. +	if(map[m].instance_id == 0)
  1513. +		return 0;
  1514. +
  1515. +	// Kick everyone out
  1516. +	map_foreachinarea(map_instancemap_leave, m, 0, 0, map[m].xs, map[m].ys, BL_PC);
  1517. +
  1518. +	// Do the unit cleanup
  1519. +	map_foreachinarea(map_instancemap_clean, m, 0, 0, map[m].xs, map[m].ys, BL_ALL);
  1520. +
  1521. +	if(map[m].block)
  1522. +		aFree(map[m].block);
  1523. +	if(map[m].block_mob)
  1524. +		aFree(map[m].block_mob);
  1525. +
  1526. +	map_removemapdb(&map[m]);
  1527. +	memset(&map[m], 0x00, sizeof(map[0]));
  1528. +
  1529. +	return 1;
  1530. +}
  1531. +
  1532.  /*=========================================
  1533.   * Dynamic Mobs [Wizputer]
  1534.   *-----------------------------------------*/
  1535. @@ -3085,7 +3216,7 @@
  1536.  
  1537.  	// finished map loading
  1538.  	ShowInfo("Successfully loaded '"CL_WHITE"%d"CL_RESET"' maps."CL_CLL"\n",map_num);
  1539. -	instance_start = map_num; // Next Map Index will be instances
  1540. +	instance_start = map_num + 1; // Next Map Index will be instances
  1541.  
  1542.  	if (maps_removed)
  1543.  		ShowNotice("Maps removed: '"CL_WHITE"%d"CL_RESET"'\n",maps_removed);
  1544. Index: src/map/map.h
  1545. ===================================================================
  1546. --- src/map/map.h	(revision 17368)
  1547. +++ src/map/map.h	(working copy)
  1548. @@ -37,7 +37,7 @@
  1549.  #define AREA_SIZE battle_config.area_size
  1550.  #define DAMAGELOG_SIZE 30
  1551.  #define LOOTITEM_SIZE 10
  1552. -#define MAX_MOBSKILL 50	//Max 128, see mob skill_idx type if need this higher
  1553. +#define MAX_MOBSKILL 50		//Max 128, see mob skill_idx type if need this higher
  1554.  #define MAX_MOB_LIST_PER_MAP 128
  1555.  #define MAX_EVENTQUEUE 2
  1556.  #define MAX_EVENTTIMER 32
  1557. @@ -46,9 +46,9 @@
  1558.  #define MAX_FLOORITEM START_ACCOUNT_NUM
  1559.  #define MAX_LEVEL 160
  1560.  #define MAX_DROP_PER_MAP 48
  1561. -#define MAX_IGNORE_LIST 20 // official is 14
  1562. +#define MAX_IGNORE_LIST 20 	// official is 14
  1563.  #define MAX_VENDING 12
  1564. -#define MAX_MAP_SIZE 512*512 // Wasn't there something like this already? Can't find it.. [Shinryo]
  1565. +#define MAX_MAP_SIZE 512*512 	// Wasn't there something like this already? Can't find it.. [Shinryo]
  1566.  
  1567.  // Added definitions for WoESE objects. [L0ne_W0lf]
  1568.  enum MOBID {
  1569. @@ -573,7 +573,6 @@
  1570.  		unsigned nochat :1;
  1571.  		unsigned partylock :1;
  1572.  		unsigned guildlock :1;
  1573. -		unsigned src4instance : 1; // To flag this map when it's used as a src map for instances
  1574.  		unsigned reset :1; // [Daegaladh]
  1575.  		unsigned chmautojoin : 1; //prevent to auto join map channel
  1576.  		unsigned nousecart : 1;	//prevent open up cart @FIXME client side only atm
  1577. @@ -684,6 +683,10 @@
  1578.  void map_clearflooritem(struct block_list* bl);
  1579.  int map_addflooritem(struct item *item_data,int amount,int16 m,int16 x,int16 y,int first_charid,int second_charid,int third_charid,int flags);
  1580.  
  1581. +// instances
  1582. +int map_addinstancemap(const char*,int);
  1583. +int map_delinstancemap(int);
  1584. +
  1585.  // player to map session
  1586.  void map_addnickdb(int charid, const char* nick);
  1587.  void map_delnickdb(int charid, const char* nick);
  1588. Index: src/map/npc.c
  1589. ===================================================================
  1590. --- src/map/npc.c	(revision 17368)
  1591. +++ src/map/npc.c	(working copy)
  1592. @@ -2579,15 +2579,11 @@
  1593.  	type = dnd->subtype;
  1594.  
  1595.  	// get placement
  1596. -	if( (type==SHOP || type==CASHSHOP || type==SCRIPT) && strcmp(w1, "-") == 0 )
  1597. -	{// floating shop/chashshop/script
  1598. +	if( (type==SHOP || type==CASHSHOP || type==SCRIPT) && strcmp(w1, "-") == 0 ) {// floating shop/chashshop/script
  1599.  		x = y = dir = 0;
  1600.  		m = -1;
  1601. -	}
  1602. -	else
  1603. -	{
  1604. -		if( sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 )// <map name>,<x>,<y>,<facing>
  1605. -		{
  1606. +	} else {
  1607. +		if( sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 ) { // <map name>,<x>,<y>,<facing>
  1608.  			ShowError("npc_parse_duplicate: Invalid placement format for duplicate in file '%s', line '%d'. Skipping line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
  1609.  			return end;// next line, try to continue
  1610.  		}
  1611. @@ -2601,8 +2597,7 @@
  1612.  	if( type == WARP && sscanf(w4, "%d,%d", &xs, &ys) == 2 );// <spanx>,<spany>
  1613.  	else if( type == SCRIPT && sscanf(w4, "%d,%d,%d", &class_, &xs, &ys) == 3);// <sprite id>,<triggerX>,<triggerY>
  1614.  	else if( type != WARP ) class_ = atoi(w4);// <sprite id>
  1615. -	else
  1616. -	{
  1617. +	else {
  1618.  		ShowError("npc_parse_duplicate: Invalid span format for duplicate warp in file '%s', line '%d'. Skipping line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
  1619.  		return end;// next line, try to continue
  1620.  	}
  1621. @@ -2620,56 +2615,51 @@
  1622.  	nd->src_id = src_id;
  1623.  	nd->bl.type = BL_NPC;
  1624.  	nd->subtype = (enum npc_subtype)type;
  1625. -	switch( type )
  1626. -	{
  1627. -	case SCRIPT:
  1628. -		++npc_script;
  1629. -		nd->u.scr.xs = xs;
  1630. -		nd->u.scr.ys = ys;
  1631. -		nd->u.scr.script = dnd->u.scr.script;
  1632. -		nd->u.scr.label_list = dnd->u.scr.label_list;
  1633. -		nd->u.scr.label_list_num = dnd->u.scr.label_list_num;
  1634. -		break;
  1635. +	switch( type ) {
  1636. +		case SCRIPT:
  1637. +			++npc_script;
  1638. +			nd->u.scr.xs = xs;
  1639. +			nd->u.scr.ys = ys;
  1640. +			nd->u.scr.script = dnd->u.scr.script;
  1641. +			nd->u.scr.label_list = dnd->u.scr.label_list;
  1642. +			nd->u.scr.label_list_num = dnd->u.scr.label_list_num;
  1643. +			break;
  1644.  
  1645. -	case SHOP:
  1646. -	case CASHSHOP:
  1647. -		++npc_shop;
  1648. -		nd->u.shop.shop_item = dnd->u.shop.shop_item;
  1649. -		nd->u.shop.count = dnd->u.shop.count;
  1650. -		break;
  1651. +		case SHOP:
  1652. +		case CASHSHOP:
  1653. +			++npc_shop;
  1654. +			nd->u.shop.shop_item = dnd->u.shop.shop_item;
  1655. +			nd->u.shop.count = dnd->u.shop.count;
  1656. +			break;
  1657.  
  1658. -	case WARP:
  1659. -		++npc_warp;
  1660. -		if( !battle_config.warp_point_debug )
  1661. -			nd->class_ = WARP_CLASS;
  1662. -		else
  1663. -			nd->class_ = WARP_DEBUG_CLASS;
  1664. -		nd->u.warp.xs = xs;
  1665. -		nd->u.warp.ys = ys;
  1666. -		nd->u.warp.mapindex = dnd->u.warp.mapindex;
  1667. -		nd->u.warp.x = dnd->u.warp.x;
  1668. -		nd->u.warp.y = dnd->u.warp.y;
  1669. -		break;
  1670. +		case WARP:
  1671. +			++npc_warp;
  1672. +			if( !battle_config.warp_point_debug )
  1673. +				nd->class_ = WARP_CLASS;
  1674. +			else
  1675. +				nd->class_ = WARP_DEBUG_CLASS;
  1676. +			nd->u.warp.xs = xs;
  1677. +			nd->u.warp.ys = ys;
  1678. +			nd->u.warp.mapindex = dnd->u.warp.mapindex;
  1679. +			nd->u.warp.x = dnd->u.warp.x;
  1680. +			nd->u.warp.y = dnd->u.warp.y;
  1681. +			break;
  1682.  	}
  1683.  
  1684.  	//Add the npc to its location
  1685. -	if( m >= 0 )
  1686. -	{
  1687. +	if( m >= 0 ) {
  1688.  		map_addnpc(m, nd);
  1689.  		status_change_init(&nd->bl);
  1690.  		unit_dataset(&nd->bl);
  1691.  		nd->ud.dir = dir;
  1692.  		npc_setcells(nd);
  1693.  		map_addblock(&nd->bl);
  1694. -		if( class_ >= 0 )
  1695. -		{
  1696. +		if( class_ >= 0 ) {
  1697.  			status_set_viewdata(&nd->bl, nd->class_);
  1698.  			if( map[nd->bl.m].users )
  1699.  				clif_spawn(&nd->bl);
  1700.  		}
  1701. -	}
  1702. -	else
  1703. -	{
  1704. +	} else {
  1705.  		// we skip map_addnpc, but still add it to the list of ID's
  1706.  		map_addiddb(&nd->bl);
  1707.  	}
  1708. @@ -2688,6 +2678,18 @@
  1709.  		npc_timerevent_export(nd, i);
  1710.  	}
  1711.  
  1712. +	if(!strcmp(filepath,"INSTANCING")) { //Instance NPCs will use this for commands
  1713. +		char evname[EVENT_NAME_LENGTH];
  1714. +		struct event_data *ev;
  1715. +		ShowDebug("We created an instance NPC %s with instance %d.\n",nd->bl.id,map[m].instance_id);
  1716. +		nd->instance_id = map[m].instance_id;
  1717. +
  1718. +		snprintf(evname, ARRAYLENGTH(evname), "%s::OnInstanceInit", nd->exname);
  1719. +
  1720. +		if( ( ev = (struct event_data*)strdb_get(ev_db, evname) ) )
  1721. +			run_script(nd->u.scr.script,ev->pos,0,nd->bl.id);
  1722. +	}
  1723. +
  1724.  	nd->u.scr.timerid = INVALID_TIMER;
  1725.  
  1726.  	return end;
  1727. @@ -2700,21 +2702,18 @@
  1728.  		return 1;
  1729.  
  1730.  	snprintf(newname, ARRAYLENGTH(newname), "dup_%d_%d", map[m].instance_id, snd->bl.id);
  1731. -	if( npc_name2id(newname) != NULL )
  1732. -	{ // Name already in use
  1733. +	if( npc_name2id(newname) != NULL ) { // Name already in use
  1734.  		ShowError("npc_duplicate4instance: the npcname (%s) is already in use while trying to duplicate npc %s in instance %d.\n", newname, snd->exname, map[m].instance_id);
  1735.  		return 1;
  1736.  	}
  1737.  
  1738. -	if( snd->subtype == WARP )
  1739. -	{ // Adjust destination, if instanced
  1740. +	if( snd->subtype == WARP ) { // Adjust destination, if instanced
  1741.  		struct npc_data *wnd = NULL; // New NPC
  1742.  		int dm = map_mapindex2mapid(snd->u.warp.mapindex), im;
  1743.  		if( dm < 0 ) return 1;
  1744.  
  1745. -		im = instance_mapid2imapid(dm, map[m].instance_id);
  1746. -		if( im == -1 )
  1747. -		{
  1748. +		im = instance_mapname2mapid(map[m].name, map[m].instance_id);
  1749. +		if( im == -1 ) {
  1750.  			ShowError("npc_duplicate4instance: warp (%s) leading to instanced map (%s), but instance map is not attached to current instance.\n", map[dm].name, snd->exname);
  1751.  			return 1;
  1752.  		}
  1753. @@ -2745,9 +2744,7 @@
  1754.  		if( map[wnd->bl.m].users )
  1755.  			clif_spawn(&wnd->bl);
  1756.  		strdb_put(npcname_db, wnd->exname, wnd);
  1757. -	}
  1758. -	else
  1759. -	{
  1760. +	} else {
  1761.  		static char w1[50], w2[50], w3[50], w4[50];
  1762.  		const char* stat_buf = "- call from instancing subsystem -\n";
  1763.  
  1764. @@ -3772,11 +3769,8 @@
  1765.  		"\t-'"CL_WHITE"%d"CL_RESET"' Mobs Not Cached\n",
  1766.  		npc_id - npc_new_min, npc_warp, npc_shop, npc_script, npc_mob, npc_cache_mob, npc_delay_mob);
  1767.  
  1768. -	do_final_instance();
  1769. +	do_reload_instance();
  1770.  
  1771. -	for( i = 0; i < ARRAYLENGTH(instance); ++i )
  1772. -		instance_init(instance[i].instance_id);
  1773. -
  1774.  	//Re-read the NPC Script Events cache.
  1775.  	npc_read_event_script();
  1776.  
  1777. Index: src/map/npc.h
  1778. ===================================================================
  1779. --- src/map/npc.h	(revision 17368)
  1780. +++ src/map/npc.h	(working copy)
  1781. @@ -29,19 +29,16 @@
  1782.  	struct view_data *vd;
  1783.  	struct status_change sc; //They can't have status changes, but.. they want the visual opt values.
  1784.  	struct npc_data *master_nd;
  1785. -	short class_;
  1786. -	short speed;
  1787. +	short class_,speed,instance_id;
  1788.  	char name[NAME_LENGTH+1];// display name
  1789.  	char exname[NAME_LENGTH+1];// unique npc name
  1790. -	int chat_id;
  1791. -	int touching_id;
  1792. +	int chat_id,touching_id;
  1793.  	unsigned int next_walktime;
  1794.  
  1795.  	unsigned size : 2;
  1796.  
  1797.  	struct status_data status;
  1798. -	unsigned int level;
  1799. -	unsigned int stat_point;
  1800. +	unsigned int level,stat_point;
  1801.  
  1802.  	void* chatdb; // pointer to a npc_parse struct (see npc_chat.c)
  1803.  	char* path;/* path dir */
  1804. Index: src/map/party.c
  1805. ===================================================================
  1806. --- src/map/party.c	(revision 17368)
  1807. +++ src/map/party.c	(working copy)
  1808. @@ -320,7 +320,7 @@
  1809.  		clif_party_option(p,sd,0x100);
  1810.  		clif_party_info(p,NULL);
  1811.  		if( p->instance_id != 0 )
  1812. -			clif_instance_join(sd->fd, p->instance_id);
  1813. +			instance_reqinfo(sd,p->instance_id);
  1814.  	}
  1815.  	if( char_id != 0 )// requester
  1816.  	{
  1817. @@ -439,7 +439,7 @@
  1818.  	{
  1819.  		p->data[i].sd = sd;
  1820.  		if( p->instance_id )
  1821. -			clif_instance_join(sd->fd,p->instance_id);
  1822. +			instance_reqinfo(sd,p->instance_id);
  1823.  	}
  1824.  	else
  1825.  		sd->status.party_id = 0; //He does not belongs to the party really?
  1826. @@ -498,7 +498,7 @@
  1827.  	clif_charnameupdate(sd); //Update char name's display [Skotlex]
  1828.  
  1829.  	if( p->instance_id )
  1830. -		clif_instance_join(sd->fd, p->instance_id);
  1831. +		instance_reqinfo(sd,p->instance_id);
  1832.  
  1833.  	return 0;
  1834.  }
  1835. @@ -575,8 +575,16 @@
  1836.  		sd->status.party_id = 0;
  1837.  		clif_charnameupdate(sd); //Update name display [Skotlex]
  1838.  		//TODO: hp bars should be cleared too
  1839. -		if( p->instance_id )
  1840. -			instance_check_kick(sd);
  1841. +		if( p->instance_id ) {
  1842. +			int16 m = sd->bl.m;
  1843. +
  1844. +			if( map[m].instance_id ) { // User was on the instance map
  1845. +				if( map[m].save.map )
  1846. +					pc_setpos(sd, map[m].save.map, map[m].save.x, map[m].save.y, CLR_TELEPORT);
  1847. +				else
  1848. +					pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  1849. +			}
  1850. +		}
  1851.  	}
  1852.  
  1853.  	return 0;
  1854. @@ -594,7 +602,7 @@
  1855.  
  1856.  	if( p->instance_id )
  1857.  	{
  1858. -		instance[p->instance_id].party_id = 0;
  1859. +		instance_data[p->instance_id].party_id = 0;
  1860.  		instance_destroy( p->instance_id );
  1861.  	}
  1862.  
  1863. Index: src/map/pc.c
  1864. ===================================================================
  1865. --- src/map/pc.c	(revision 17368)
  1866. +++ src/map/pc.c	(working copy)
  1867. @@ -4756,36 +4756,21 @@
  1868.   *------------------------------------------*/
  1869.  int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  1870.  {
  1871. -	struct party_data *p;
  1872.  	int16 m;
  1873.  
  1874.  	nullpo_ret(sd);
  1875.  
  1876. -	if( !mapindex || !mapindex_id2name(mapindex) )
  1877. -	{
  1878. +	if( !mapindex || !mapindex_id2name(mapindex) ) {
  1879.  		ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  1880.  		return 1;
  1881.  	}
  1882.  
  1883. -	if( pc_isdead(sd) )
  1884. -	{ //Revive dead people before warping them
  1885. +	if( pc_isdead(sd) ) { //Revive dead people before warping them
  1886.  		pc_setstand(sd);
  1887.  		pc_setrestartvalue(sd,1);
  1888.  	}
  1889.  
  1890.  	m = map_mapindex2mapid(mapindex);
  1891. -	if( map[m].flag.src4instance && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  1892. -	{
  1893. -		// Request the mapid of this src map into the instance of the party
  1894. -		int im = instance_map2imap(m, p->instance_id);
  1895. -		if( im < 0 )
  1896. -			; // Player will enter the src map for instances
  1897. -		else
  1898. -		{ // Changes destiny to the instance map, not the source map
  1899. -			m = im;
  1900. -			mapindex = map_id2index(m);
  1901. -		}
  1902. -	}
  1903.  
  1904.  	sd->state.changemap = (sd->mapindex != mapindex);
  1905.  	sd->state.warping = 1;
  1906. Index: src/map/script.c
  1907. ===================================================================
  1908. --- src/map/script.c	(revision 17368)
  1909. +++ src/map/script.c	(working copy)
  1910. @@ -2565,12 +2565,16 @@
  1911.  			}
  1912.  			break;
  1913.  		case '\'':
  1914. -				if (st->instance_id) {
  1915. -					data->u.str = (char*)idb_get(instance[st->instance_id].vars,reference_getuid(data));
  1916. -				} else {
  1917. +			{
  1918. +				struct npc_data *nd;
  1919. +				nd = map_id2nd(st->oid);
  1920. +				if( nd && nd->instance_id > 0 )
  1921. +					data->u.str = (char*)idb_get(instance_data[nd->instance_id].vars,reference_getuid(data));
  1922. +				else {
  1923.  					ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  1924.  					data->u.str = NULL;
  1925.  				}
  1926. +			}
  1927.  			break;
  1928.  		default:
  1929.  			data->u.str = pc_readglobalreg_str(sd, name);
  1930. @@ -2630,12 +2634,16 @@
  1931.  			}
  1932.  			break;
  1933.  		case '\'':
  1934. -				if( st->instance_id )
  1935. -					data->u.num = (int)idb_iget(instance[st->instance_id].vars,reference_getuid(data));
  1936. +			{
  1937. +				struct npc_data *nd;
  1938. +				nd = map_id2nd(st->oid);
  1939. +				if( nd && nd->instance_id > 0 )
  1940. +					data->u.num = (int)idb_iget(instance_data[nd->instance_id].vars,reference_getuid(data));
  1941.  				else {
  1942.  					ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  1943.  					data->u.num = 0;
  1944.  				}
  1945. +			}
  1946.  			break;
  1947.  		default:
  1948.  			data->u.num = pc_readglobalreg(sd, name);
  1949. @@ -2691,11 +2699,15 @@
  1950.  			}
  1951.  			return 1;
  1952.  		case '\'':
  1953. -			if( st->instance_id ) {
  1954. -				idb_remove(instance[st->instance_id].vars, num);
  1955. -				if( str[0] ) idb_put(instance[st->instance_id].vars, num, aStrdup(str));
  1956. +			{
  1957. +				struct npc_data *nd;
  1958. +				nd = map_id2nd(st->oid);
  1959. +				if( nd && nd->instance_id > 0 ) {
  1960. +					idb_remove(instance_data[nd->instance_id].vars, num);
  1961. +					if( str[0] ) idb_put(instance_data[nd->instance_id].vars, num, aStrdup(str));
  1962. +				}
  1963. +				return 1;
  1964.  			}
  1965. -			return 1;
  1966.  		default:
  1967.  			return pc_setglobalreg_str(sd, name, str);
  1968.  		}
  1969. @@ -2739,12 +2751,16 @@
  1970.  			}
  1971.  			return 1;
  1972.  		case '\'':
  1973. -			if( st->instance_id ) {
  1974. -				idb_remove(instance[st->instance_id].vars, num);
  1975. -				if( val != 0 )
  1976. -					idb_iput(instance[st->instance_id].vars, num, val);
  1977. +			{
  1978. +				struct npc_data *nd;
  1979. +				nd = map_id2nd(st->oid);
  1980. +				if( nd && nd->instance_id > 0 ) {
  1981. +					idb_remove(instance_data[nd->instance_id].vars, num);
  1982. +					if( val != 0 )
  1983. +						idb_iput(instance_data[nd->instance_id].vars, num, val);
  1984. +				}
  1985. +				return 1;
  1986.  			}
  1987. -			return 1;
  1988.  		default:
  1989.  			return pc_setglobalreg(sd, name, val);
  1990.  		}
  1991. @@ -3665,14 +3681,9 @@
  1992.  	int gotocount = script_config.check_gotocount;
  1993.  	TBL_PC *sd;
  1994.  	struct script_stack *stack=st->stack;
  1995. -	struct npc_data *nd;
  1996.  
  1997.  	script_attach_state(st);
  1998.  
  1999. -	nd = map_id2nd(st->oid);
  2000. -	if( nd && map[nd->bl.m].instance_id > 0 )
  2001. -		st->instance_id = map[nd->bl.m].instance_id;
  2002. -
  2003.  	if(st->state == RERUNLINE) {
  2004.  		run_func(st);
  2005.  		if(st->state == GOTO)
  2006. @@ -8815,34 +8826,28 @@
  2007.  	struct map_session_data* sd;
  2008.  	int16 m;
  2009.  
  2010. -	if (script_hasdata(st, 8))
  2011. -	{
  2012. +	if (script_hasdata(st, 8)) {
  2013.  		event = script_getstr(st, 8);
  2014.  		check_event(st, event);
  2015.  	}
  2016.  
  2017. -	if (script_hasdata(st, 9))
  2018. -	{
  2019. +	if (script_hasdata(st, 9)) {
  2020.  		size = script_getnum(st, 9);
  2021. -		if (size > 3)
  2022. -		{
  2023. +		if (size > 3) {
  2024.  			ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  2025.  			return 1;
  2026.  		}
  2027.  	}
  2028.  
  2029. -	if (script_hasdata(st, 10))
  2030. -	{
  2031. +	if (script_hasdata(st, 10)) {
  2032.  		ai = script_getnum(st, 10);
  2033. -		if (ai > 4)
  2034. -		{
  2035. +		if (ai > 4) {
  2036.  			ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  2037.  			return 1;
  2038.  		}
  2039.  	}
  2040.  
  2041. -	if (class_ >= 0 && !mobdb_checkid(class_))
  2042. -	{
  2043. +	if (class_ >= 0 && !mobdb_checkid(class_)) {
  2044.  		ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  2045.  		return 1;
  2046.  	}
  2047. @@ -8852,17 +8857,7 @@
  2048.  	if (sd && strcmp(mapn, "this") == 0)
  2049.  		m = sd->bl.m;
  2050.  	else
  2051. -	{
  2052.  		m = map_mapname2mapid(mapn);
  2053. -		if (map[m].flag.src4instance && st->instance_id)
  2054. -		{ // Try to redirect to the instance map, not the src map
  2055. -			if ((m = instance_mapid2imapid(m, st->instance_id)) < 0)
  2056. -			{
  2057. -				ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  2058. -				return 1;
  2059. -			}
  2060. -		}
  2061. -	}
  2062.  
  2063.  	mob_once_spawn(sd, m, x, y, str, class_, amount, event, size, ai);
  2064.  	return 0;
  2065. @@ -8922,27 +8917,22 @@
  2066.  	struct map_session_data* sd;
  2067.  	int16 m;
  2068.  
  2069. -	if (script_hasdata(st,10))
  2070. -	{
  2071. +	if (script_hasdata(st,10)) {
  2072.  		event = script_getstr(st, 10);
  2073.  		check_event(st, event);
  2074.  	}
  2075.  
  2076. -	if (script_hasdata(st, 11))
  2077. -	{
  2078. +	if (script_hasdata(st, 11)) {
  2079.  		size = script_getnum(st, 11);
  2080. -		if (size > 3)
  2081. -		{
  2082. +		if (size > 3) {
  2083.  			ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  2084.  			return 1;
  2085.  		}
  2086.  	}
  2087.  
  2088. -	if (script_hasdata(st, 12))
  2089. -	{
  2090. +	if (script_hasdata(st, 12)) {
  2091.  		ai = script_getnum(st, 12);
  2092. -		if (ai > 4)
  2093. -		{
  2094. +		if (ai > 4) {
  2095.  			ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  2096.  			return 1;
  2097.  		}
  2098. @@ -8953,17 +8943,7 @@
  2099.  	if (sd && strcmp(mapn, "this") == 0)
  2100.  		m = sd->bl.m;
  2101.  	else
  2102. -	{
  2103.  		m = map_mapname2mapid(mapn);
  2104. -		if (map[m].flag.src4instance && st->instance_id)
  2105. -		{ // Try to redirect to the instance map, not the src map
  2106. -			if ((m = instance_mapid2imapid(m, st->instance_id)) < 0)
  2107. -			{
  2108. -				ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  2109. -				return 1;
  2110. -			}
  2111. -		}
  2112. -	}
  2113.  
  2114.  	mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  2115.  	return 0;
  2116. @@ -9018,9 +8998,6 @@
  2117.  	if( (m=map_mapname2mapid(mapname))<0 )
  2118.  		return 0;
  2119.  
  2120. -	if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  2121. -		return 0;
  2122. -
  2123.  	if( script_hasdata(st,4) ) {
  2124.  		if ( script_getnum(st,4) == 1 ) {
  2125.  			map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  2126. @@ -9059,9 +9036,6 @@
  2127.  	if( (m = map_mapname2mapid(mapname))<0 )
  2128.  		return 0;
  2129.  
  2130. -	if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  2131. -		return 0;
  2132. -
  2133.  	if( script_hasdata(st,3) ) {
  2134.  		if ( script_getnum(st,3) == 1 ) {
  2135.  			map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  2136. @@ -11569,12 +11543,6 @@
  2137.  		return 0;
  2138.  	}
  2139.  
  2140. -	if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  2141. -	{
  2142. -		script_pushint(st,-1);
  2143. -		return 0;
  2144. -	}
  2145. -
  2146.  	script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  2147.  
  2148.  	return 0;
  2149. @@ -16475,58 +16443,80 @@
  2150.  }
  2151.  
  2152.  /*==========================================
  2153. - * Instancing Script Commands
  2154. + * Instancing System
  2155.   *------------------------------------------*/
  2156.  
  2157. +//Returns an Instance ID
  2158. +//Checks NPC first, then if player is attached we check party
  2159. +static int buildin_instancegetid_sub(struct script_state* st)
  2160. +{
  2161. +	short instance_id = 0;	
  2162. +
  2163. +	struct npc_data *nd;
  2164. +	if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  2165. +		instance_id = nd->instance_id;
  2166. +	else {
  2167. +		struct map_session_data *sd;
  2168. +		struct party_data *p;
  2169. +		if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2170. +			instance_id = p->instance_id;
  2171. +	}
  2172. +
  2173. +	return instance_id;
  2174. +}
  2175. +
  2176. +/*==========================================
  2177. + * Creates the instance
  2178. + * Returns Instance ID if created successfully
  2179. + *------------------------------------------*/
  2180.  BUILDIN_FUNC(instance_create)
  2181.  {
  2182. -	const char *name;
  2183. -	int party_id, res;
  2184.  
  2185. -	name = script_getstr(st, 2);
  2186. -	party_id = script_getnum(st, 3);
  2187. +	struct map_session_data *sd;
  2188. +	int res;
  2189.  
  2190. -	res = instance_create(party_id, name);
  2191. -	if( res == -4 ) // Already exists
  2192. -	{
  2193. -		script_pushint(st, -1);
  2194. -		return 0;
  2195. +	if((sd = script_rid2sd(st)) == NULL)
  2196. +		return 1;
  2197. +
  2198. +	if(!sd->status.party_id) {
  2199. +		ShowError("script:instance_create: attempting to start an instance with no attached party.\n");
  2200. +		return 1;
  2201.  	}
  2202. -	else if( res < 0 )
  2203. -	{
  2204. +
  2205. +	res = instance_create(sd->status.party_id, script_getstr(st, 2));
  2206. +	if( res < 0 ) {
  2207.  		const char *err;
  2208. -		switch(res)
  2209. -		{
  2210. -		case -3: err = "No free instances"; break;
  2211. -		case -2: err = "Invalid party ID"; break;
  2212. -		case -1: err = "Invalid type"; break;
  2213. -		default: err = "Unknown"; break;
  2214. +		switch(res) {
  2215. +			case -4: err = "Instance already exists"; break;
  2216. +			case -3: err = "No free instances"; break;
  2217. +			case -2: err = "Invalid party ID"; break;
  2218. +			case -1: err = "Invalid type"; break;
  2219. +			default: err = "Unknown"; break;
  2220.  		}
  2221.  		ShowError("buildin_instance_create: %s [%d].\n", err, res);
  2222. -		script_pushint(st, -2);
  2223. -		return 0;
  2224.  	}
  2225.  
  2226.  	script_pushint(st, res);
  2227.  	return 0;
  2228.  }
  2229.  
  2230. +/*==========================================
  2231. + * Destroys an instance (unofficial)
  2232. + * Officially instances are only destroyed by timeout
  2233. + *
  2234. + * instance_destroy {<instance_id>};
  2235. + *
  2236. + *------------------------------------------*/
  2237.  BUILDIN_FUNC(instance_destroy)
  2238.  {
  2239. -	int instance_id;
  2240. -	struct map_session_data *sd;
  2241. -	struct party_data *p;
  2242. +	short instance_id;
  2243.  
  2244. -	if( script_hasdata(st, 2) )
  2245. -		instance_id = script_getnum(st, 2);
  2246. -	else if( st->instance_id )
  2247. -		instance_id = st->instance_id;
  2248. -	else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2249. -		instance_id = p->instance_id;
  2250. -	else return 0;
  2251. +	if( script_hasdata(st,2) )
  2252. +		instance_id = script_getnum(st,2);
  2253. +	else
  2254. +		instance_id = buildin_instancegetid_sub(st);
  2255.  
  2256. -	if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  2257. -	{
  2258. +	if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  2259.  		ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  2260.  		return 0;
  2261.  	}
  2262. @@ -16535,189 +16525,51 @@
  2263.  	return 0;
  2264.  }
  2265.  
  2266. -BUILDIN_FUNC(instance_attachmap)
  2267. +/*==========================================
  2268. + * Warps player to instance
  2269. + * Results:
  2270. + *	0: Success
  2271. + *	1: Instance not in DB
  2272. + *	2: Character not in party
  2273. + *	3: Party doesn't have instance
  2274. + *	4: Instance doesn't match with party
  2275. + *------------------------------------------*/
  2276. +BUILDIN_FUNC(instance_enter)
  2277.  {
  2278. -	const char *name;
  2279. -	int16 m;
  2280. -	int instance_id;
  2281. -	bool usebasename = false;
  2282. -
  2283. -	name = script_getstr(st,2);
  2284. -	instance_id = script_getnum(st,3);
  2285. -	if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  2286. -		usebasename = true;
  2287. -
  2288. -	if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  2289. -	{
  2290. -		ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  2291. -		script_pushconststr(st, "");
  2292. -		return 0;
  2293. -	}
  2294. -	script_pushconststr(st, map[m].name);
  2295. -
  2296. -	return 0;
  2297. -}
  2298. -
  2299. -BUILDIN_FUNC(instance_detachmap)
  2300. -{
  2301.  	struct map_session_data *sd;
  2302. -	struct party_data *p;
  2303. -	const char *str;
  2304. -	int16 m;
  2305. -	int instance_id;
  2306.  
  2307. -	str = script_getstr(st, 2);
  2308. -	if( script_hasdata(st, 3) )
  2309. -		instance_id = script_getnum(st, 3);
  2310. -	else if( st->instance_id )
  2311. -		instance_id = st->instance_id;
  2312. -	else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2313. -		instance_id = p->instance_id;
  2314. -	else return 0;
  2315. -
  2316. -	if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  2317. -	{
  2318. -		ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  2319. -		return 0;
  2320. -	}
  2321. -
  2322. -	instance_del_map(m);
  2323. -	return 0;
  2324. -}
  2325. -
  2326. -BUILDIN_FUNC(instance_attach)
  2327. -{
  2328. -	int instance_id;
  2329. -
  2330. -	instance_id = script_getnum(st, 2);
  2331. -	if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  2332. -		return 0;
  2333. -
  2334. -	st->instance_id = instance_id;
  2335. -	return 0;
  2336. -}
  2337. -
  2338. -BUILDIN_FUNC(instance_id)
  2339. -{
  2340. -	int instance_id;
  2341. -
  2342. -	if( script_hasdata(st, 2) )
  2343. -	{
  2344. -		struct map_session_data *sd;
  2345. -		struct party_data *p;
  2346. -		int type;
  2347. -		type = script_getnum(st, 2);
  2348. -		if( type == 0 )
  2349. -			instance_id = st->instance_id;
  2350. -		else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  2351. -			instance_id = p->instance_id;
  2352. -		else
  2353. -			instance_id = 0;
  2354. -	}
  2355. +	if((sd = script_rid2sd(st)) != NULL)
  2356. +		script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  2357.  	else
  2358. -		instance_id = st->instance_id;
  2359. -
  2360. -	script_pushint(st, instance_id);
  2361. +		return 1;
  2362.  	return 0;
  2363. -}
  2364.  
  2365. -BUILDIN_FUNC(instance_set_timeout)
  2366. -{
  2367. -	int progress_timeout, idle_timeout;
  2368. -	int instance_id;
  2369. -	struct map_session_data *sd;
  2370. -	struct party_data *p;
  2371. -
  2372. -	progress_timeout = script_getnum(st, 2);
  2373. -	idle_timeout = script_getnum(st, 3);
  2374. -
  2375. -	if( script_hasdata(st, 4) )
  2376. -		instance_id = script_getnum(st, 4);
  2377. -	else if( st->instance_id )
  2378. -		instance_id = st->instance_id;
  2379. -	else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2380. -		instance_id = p->instance_id;
  2381. -	else return 0;
  2382. -
  2383. -	if( instance_id > 0 )
  2384. -		instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  2385. -
  2386. -	return 0;
  2387.  }
  2388.  
  2389. -BUILDIN_FUNC(instance_init)
  2390. -{
  2391. -	int instance_id = script_getnum(st, 2);
  2392. -
  2393. -	if( instance[instance_id].state != INSTANCE_IDLE )
  2394. -	{
  2395. -		ShowError("instance_init: instance already initialized.\n");
  2396. -		return 0;
  2397. -	}
  2398. -
  2399. -	instance_init(instance_id);
  2400. -	return 0;
  2401. -}
  2402. -
  2403. -BUILDIN_FUNC(instance_announce)
  2404. -{
  2405. -	int         instance_id = script_getnum(st,2);
  2406. -	const char *mes         = script_getstr(st,3);
  2407. -	int         flag        = script_getnum(st,4);
  2408. -	const char *fontColor   = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  2409. -	int         fontType    = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  2410. -	int         fontSize    = script_hasdata(st,7) ? script_getnum(st,7) : 12;    // default fontSize
  2411. -	int         fontAlign   = script_hasdata(st,8) ? script_getnum(st,8) : 0;     // default fontAlign
  2412. -	int         fontY       = script_hasdata(st,9) ? script_getnum(st,9) : 0;     // default fontY
  2413. -
  2414. -	int i;
  2415. -	struct map_session_data *sd;
  2416. -	struct party_data *p;
  2417. -
  2418. -	if( instance_id == 0 )
  2419. -	{
  2420. -		if( st->instance_id )
  2421. -			instance_id = st->instance_id;
  2422. -		else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2423. -			instance_id = p->instance_id;
  2424. -		else return 0;
  2425. -	}
  2426. -
  2427. -	if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  2428. -		return 0;
  2429. -
  2430. -	for( i = 0; i < instance[instance_id].num_map; i++ )
  2431. -		map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  2432. -			mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  2433. -
  2434. -	return 0;
  2435. -}
  2436. -
  2437. +/*==========================================
  2438. + * Returns the name of a duplicated NPC
  2439. + *
  2440. + * instance_npcname <npc_name>{,<instance_id};
  2441. + *
  2442. + *------------------------------------------*/
  2443.  BUILDIN_FUNC(instance_npcname)
  2444.  {
  2445.  	const char *str;
  2446. -	int instance_id = 0;
  2447. +	short instance_id = 0;
  2448.  
  2449. -	struct map_session_data *sd;
  2450. -	struct party_data *p;
  2451.  	struct npc_data *nd;
  2452.  
  2453. -	str = script_getstr(st, 2);
  2454. -	if( script_hasdata(st, 3) )
  2455. -		instance_id = script_getnum(st, 3);
  2456. -	else if( st->instance_id )
  2457. -		instance_id = st->instance_id;
  2458. -	else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2459. -		instance_id = p->instance_id;
  2460. +	str = script_getstr(st,2);
  2461. +	if( script_hasdata(st,3) )
  2462. +		instance_id = script_getnum(st,3);
  2463. +	else
  2464. +		instance_id = buildin_instancegetid_sub(st);
  2465.  
  2466. -	if( instance_id && (nd = npc_name2id(str)) != NULL )
  2467. - 	{
  2468. +	if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  2469.  		static char npcname[NAME_LENGTH];
  2470.  		snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  2471.   		script_pushconststr(st,npcname);
  2472. -	}
  2473. -	else
  2474. -	{
  2475. +	} else {
  2476.  		ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  2477.  		st->state = END;
  2478.  		return 1;
  2479. @@ -16726,62 +16578,85 @@
  2480.  	return 0;
  2481.  }
  2482.  
  2483. -BUILDIN_FUNC(has_instance)
  2484. +/*==========================================
  2485. + * Returns the name of a duplicated map
  2486. + *
  2487. + * instance_mapname <map_name>{,<instance_id};
  2488. + *
  2489. + *------------------------------------------*/
  2490. +BUILDIN_FUNC(instance_mapname)
  2491.  {
  2492. -	struct map_session_data *sd;
  2493. -	struct party_data *p;
  2494.   	const char *str;
  2495.  	int16 m;
  2496. -	int instance_id = 0;
  2497. +	short instance_id = 0;
  2498.  
  2499. - 	str = script_getstr(st, 2);
  2500. -	if( script_hasdata(st, 3) )
  2501. -		instance_id = script_getnum(st, 3);
  2502. -	else if( st->instance_id )
  2503. -		instance_id = st->instance_id;
  2504. -	else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  2505. -		instance_id = p->instance_id;
  2506. + 	str = script_getstr(st,2);
  2507. +	if( script_hasdata(st,3) )
  2508. +		instance_id = script_getnum(st,3);
  2509. +	else
  2510. +		instance_id = buildin_instancegetid_sub(st);
  2511.  
  2512. -	if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  2513. -	{
  2514. +	if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_mapname2mapid(map[m].name, instance_id)) < 0 )
  2515.  		script_pushconststr(st, "");
  2516. -		return 0;
  2517. +	else
  2518. +		script_pushconststr(st, map[m].name);
  2519. +
  2520. +	return 0;
  2521. +}
  2522. +
  2523. +/*==========================================
  2524. + * Returns an Instance ID
  2525. + *------------------------------------------*/
  2526. +BUILDIN_FUNC(instance_id)
  2527. +{
  2528. +	short instance_id;
  2529. +
  2530. +	instance_id = buildin_instancegetid_sub(st);
  2531. +
  2532. +	if(!instance_id) {
  2533. +		ShowError("script:instance_id: No instance attached to NPC");
  2534. +		script_pushint(st, 0);
  2535. +		return 1;
  2536.  	}
  2537.  
  2538. -	script_pushconststr(st, map[m].name);
  2539. +	script_pushint(st, instance_id);
  2540. +		
  2541.  	return 0;
  2542.  }
  2543.  
  2544. +/*==========================================
  2545. + * Warps all players inside an instance
  2546. + *
  2547. + * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  2548. + *
  2549. + *------------------------------------------*/
  2550.  BUILDIN_FUNC(instance_warpall)
  2551.  {
  2552. +	struct party_data *p;
  2553.  	struct map_session_data *pl_sd;
  2554.  	int16 m, i;
  2555. -	int instance_id;
  2556. +	short instance_id;
  2557.  	const char *mapn;
  2558.  	int x, y;
  2559.  	unsigned short mapindex;
  2560. -	struct party_data *p = NULL;
  2561.  
  2562.  	mapn = script_getstr(st,2);
  2563.  	x    = script_getnum(st,3);
  2564.  	y    = script_getnum(st,4);
  2565.  	if( script_hasdata(st,5) )
  2566.  		instance_id = script_getnum(st,5);
  2567. -	else if( st->instance_id )
  2568. -		instance_id = st->instance_id;
  2569. -	else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id )
  2570. -		instance_id = p->instance_id;
  2571. -	else return 0;
  2572. +	else
  2573. +		instance_id = buildin_instancegetid_sub(st);
  2574.  
  2575. -	if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  2576. +	if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  2577.  		return 0;
  2578.  
  2579. -	if( !(p = party_search(instance[instance_id].party_id)) )
  2580. +	if( !(p = party_search(instance_data[instance_id].party_id)) )
  2581.  		return 0;
  2582.  
  2583.  	mapindex = map_id2index(m);
  2584.  	for( i = 0; i < MAX_PARTY; i++ )
  2585. -		if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == st->instance_id ) pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  2586. +		if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == instance_id ) pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  2587.  
  2588.  	return 0;
  2589.  }
  2590. @@ -16806,39 +16681,39 @@
  2591.  	min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  2592.  	max  = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  2593.  
  2594. -	if( min < 1 || min > MAX_LEVEL){
  2595. -		ShowError("instance_check_party: Invalid min level, %d\n", min);
  2596. -		return 0;
  2597. -	}else if(  max < 1 || max > MAX_LEVEL){
  2598. -		ShowError("instance_check_party: Invalid max level, %d\n", max);
  2599. -		return 0;
  2600. +	if( min < 1 || min > MAX_LEVEL) {
  2601. +		ShowError("script:check_party: Invalid min level, %d\n", min);
  2602. +		return 1;
  2603. +	} else if(  max < 1 || max > MAX_LEVEL) {
  2604. +		ShowError("script:check_party: Invalid max level, %d\n", max);
  2605. +		return 1;
  2606.  	}
  2607.  
  2608.  	if( script_hasdata(st,2) )
  2609.  		party_id = script_getnum(st,2);
  2610.  	else return 0;
  2611.  
  2612. -	if( !(p = party_search(party_id)) ){
  2613. +	if( !(p = party_search(party_id)) ) {
  2614.  		script_pushint(st, 0); // Returns false if party does not exist.
  2615.  		return 0;
  2616.  	}
  2617.  
  2618.  	for( i = 0; i < MAX_PARTY; i++ )
  2619.  		if( (pl_sd = p->data[i].sd) )
  2620. -			if(map_id2bl(pl_sd->bl.id)){
  2621. -				if(pl_sd->status.base_level < min){
  2622. +			if(map_id2bl(pl_sd->bl.id)) {
  2623. +				if(pl_sd->status.base_level < min) {
  2624.  					script_pushint(st, 0);
  2625.  					return 0;
  2626. -				}else if(pl_sd->status.base_level > max){
  2627. +				} else if(pl_sd->status.base_level > max) {
  2628.  					script_pushint(st, 0);
  2629.  					return 0;
  2630.  				}
  2631.  					c++;
  2632.  			}
  2633.  
  2634. -	if(c < amount){
  2635. +	if(c < amount)
  2636.  		script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  2637. -	}else
  2638. +	else
  2639.  		script_pushint(st, 1);
  2640.  
  2641.  	return 0;
  2642. @@ -16911,15 +16786,11 @@
  2643.  	int16 m;
  2644.  	int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  2645.  
  2646. -	if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  2647. -	{
  2648. +	if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  2649.  		ShowError("areamobuseskill: invalid map name.\n");
  2650.  		return 0;
  2651.  	}
  2652.  
  2653. -	if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  2654. -		return 0;
  2655. -
  2656.  	center.m = m;
  2657.  	center.x = script_getnum(st,3);
  2658.  	center.y = script_getnum(st,4);
  2659. @@ -18123,17 +17994,12 @@
  2660.  	BUILDIN_DEF(bg_updatescore,"sii"),
  2661.  
  2662.  	// Instancing
  2663. -	BUILDIN_DEF(instance_create,"si"),
  2664. +	BUILDIN_DEF(instance_create,"s"),
  2665.  	BUILDIN_DEF(instance_destroy,"?"),
  2666. -	BUILDIN_DEF(instance_attachmap,"si?"),
  2667. -	BUILDIN_DEF(instance_detachmap,"s?"),
  2668. -	BUILDIN_DEF(instance_attach,"i"),
  2669. -	BUILDIN_DEF(instance_id,"?"),
  2670. -	BUILDIN_DEF(instance_set_timeout,"ii?"),
  2671. -	BUILDIN_DEF(instance_init,"i"),
  2672. -	BUILDIN_DEF(instance_announce,"isi?????"),
  2673. +	BUILDIN_DEF(instance_id,""),
  2674. +	BUILDIN_DEF(instance_enter,"s"),
  2675.  	BUILDIN_DEF(instance_npcname,"s?"),
  2676. -	BUILDIN_DEF(has_instance,"s?"),
  2677. +	BUILDIN_DEF(instance_mapname,"s?"),
  2678.  	BUILDIN_DEF(instance_warpall,"sii?"),
  2679.  	BUILDIN_DEF(instance_check_party,"i???"),
  2680.  	/**
  2681. Index: src/map/script.h
  2682. ===================================================================
  2683. --- src/map/script.h	(revision 17368)
  2684. +++ src/map/script.h	(working copy)
  2685. @@ -126,7 +126,6 @@
  2686.  	struct sleep_data {
  2687.  		int tick,timer,charid;
  2688.  	} sleep;
  2689. -	int instance_id;
  2690.  	//For backing up purposes
  2691.  	struct script_state *bk_st;
  2692.  	int bk_npcid;
  2693. Index: src/map/unit.c
  2694. ===================================================================
  2695. --- src/map/unit.c	(revision 17368)
  2696. +++ src/map/unit.c	(working copy)
  2697. @@ -2100,8 +2100,7 @@
  2698.  		status_change_end(bl, SC_TINDER_BREAKER2, INVALID_TIMER);
  2699.  		status_change_end(bl, SC_HIDING, INVALID_TIMER);
  2700.  		// Ensure the bl is a PC; if so, we'll handle the removal of cloaking and cloaking exceed later
  2701. -		if ( bl->type != BL_PC )
  2702. -		{
  2703. +		if ( bl->type != BL_PC ) {
  2704.  			status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  2705.  			status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  2706.  		}
  2707. @@ -2156,8 +2155,7 @@
  2708.  				npc_touchnext_areanpc(sd,true);
  2709.  
  2710.  			// Check if warping and not changing the map.
  2711. -			if ( sd->state.warping && !sd->state.changemap )
  2712. -			{
  2713. +			if ( sd->state.warping && !sd->state.changemap ) {
  2714.  				status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  2715.  				status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  2716.  			}
  2717. @@ -2183,8 +2181,7 @@
  2718.  
  2719.  			if( map[bl->m].users <= 0 || sd->state.debug_remove_map )
  2720.  			{// this is only place where map users is decreased, if the mobs were removed too soon then this function was executed too many times [FlavioJS]
  2721. -				if( sd->debug_file == NULL || !(sd->state.debug_remove_map) )
  2722. -				{
  2723. +				if( sd->debug_file == NULL || !(sd->state.debug_remove_map) ) {
  2724.  					sd->debug_file = "";
  2725.  					sd->debug_line = 0;
  2726.  					sd->debug_func = "";
  2727. @@ -2206,11 +2203,8 @@
  2728.  			{// decrement the number of active pvp players on the map
  2729.  				--map[bl->m].users_pvp;
  2730.  			}
  2731. -			if( map[bl->m].instance_id )
  2732. -			{
  2733. -				instance[map[bl->m].instance_id].users--;
  2734. -				instance_check_idle(map[bl->m].instance_id);
  2735. -			}
  2736. +			if(map[sd->bl.m].instance_id && sd->state.changemap)
  2737. +				instance_delusers(map[sd->bl.m].instance_id);
  2738.  			sd->state.debug_remove_map = 1; // temporary state to track double remove_map's [FlavioJS]
  2739.  			sd->debug_file = file;
  2740.  			sd->debug_line = line;
Viewed 864 times, submitted by Guest.