instance.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/socket.h"
  5. #include "../common/timer.h"
  6. #include "../common/malloc.h"
  7. #include "../common/version.h"
  8. #include "../common/nullpo.h"
  9. #include "../common/showmsg.h"
  10. #include "../common/strlib.h"
  11. #include "../common/utils.h"
  12. #include "../common/db.h"
  13. #include "clif.h"
  14. #include "instance.h"
  15. #include "map.h"
  16. #include "npc.h"
  17. #include "party.h"
  18. #include "pc.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #include <time.h>
  24. int instance_start = 0; // To keep the last index + 1 of normal map inserted in the map[ARRAY]
  25. struct s_instance instance[MAX_INSTANCE];
  26. /*--------------------------------------
  27. * name : instance name
  28. * Return value could be
  29. * -4 = already exists | -3 = no free instances | -2 = missing parameter | -1 = invalid type
  30. * On success return instance_id
  31. *--------------------------------------*/
  32. int instance_create(int party_id, const char *name)
  33. {
  34. int i;
  35. struct party_data *p = NULL;
  36. if( !party_id || !name )
  37. {
  38. ShowError("map_instance_create: missing parameter.\n");
  39. return -2;
  40. }
  41. p = party_search(party_id);
  42. if( !p || p->instance_id )
  43. return -4; // Party already instancing
  44. // Searching a Free Instance
  45. // 0 is ignored as this mean "no instance" on maps
  46. ARR_FIND(1, MAX_INSTANCE, i, instance[i].state == INSTANCE_FREE);
  47. if( i == MAX_INSTANCE )
  48. {
  49. ShowError("map_instance_create: no free instances, consider increasing MAX_INSTANCE.\n");
  50. return -3;
  51. }
  52. instance[i].state = INSTANCE_IDLE;
  53. instance[i].instance_id = i;
  54. instance[i].idle_timer = INVALID_TIMER;
  55. instance[i].idle_timeout = instance[i].idle_timeoutval = 0;
  56. instance[i].progress_timer = INVALID_TIMER;
  57. instance[i].progress_timeout = instance[i].progress_timeoutval = 0;
  58. instance[i].users = 0;
  59. instance[i].party_id = party_id;
  60. instance[i].ivar = NULL;
  61. instance[i].svar = NULL;
  62. memcpy( instance[i].name, name, sizeof(instance[i].name) );
  63. memset( instance[i].map, 0x00, sizeof(instance[i].map) );
  64. p->instance_id = i;
  65. clif_instance(i, 1, 0); // Start instancing window
  66. ShowInfo("[Instance] Created: %s.\n", name);
  67. return i;
  68. }
  69. /*--------------------------------------
  70. * Add a map to the instance using src map "name"
  71. *--------------------------------------*/
  72. int instance_add_map(const char *name, int instance_id, bool usebasename)
  73. {
  74. int m = map_mapname2mapid(name), i, im = -1;
  75. size_t num_cell, size;
  76. if( m < 0 )
  77. return -1; // source map not found
  78. if( instance[instance_id].state == INSTANCE_FREE )
  79. {
  80. ShowError("instance_add_map: trying to attach '%s' map to non-existing instance %d.\n", name, instance_id);
  81. return -1;
  82. }
  83. if( instance[instance_id].num_map >= MAX_MAP_PER_INSTANCE )
  84. {
  85. 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);
  86. return -2;
  87. }
  88. if( map[m].instance_id != 0 )
  89. { // Source map already belong to a Instance.
  90. ShowError("instance_add_map: trying to instance already instanced map %s.\n", name);
  91. return -4;
  92. }
  93. ARR_FIND( instance_start, map_num, i, !map[i].name[0] ); // Searching for a Free Map
  94. if( i < map_num ) im = i; // Unused map found (old instance)
  95. else if( map_num - 1 >= MAX_MAP_PER_SERVER )
  96. { // No more free maps
  97. ShowError("instance_add_map: no more free space to create maps on this server.\n");
  98. return -5;
  99. }
  100. else im = map_num++; // Using next map index
  101. memcpy( &map[im], &map[m], sizeof(struct map_data) ); // Copy source map
  102. snprintf(map[im].name, MAP_NAME_LENGTH, (usebasename ? "%.3d#%s" : "%.3d%s"), instance_id, name); // Generate Name for Instance Map
  103. map[im].index = mapindex_addmap(-1, map[im].name); // Add map index
  104. if( !map[im].index )
  105. {
  106. map[im].name[0] = '\0';
  107. ShowError("instance_add_map: no more free map indexes.\n");
  108. return -3; // No free map index
  109. }
  110. // Reallocate cells
  111. num_cell = map[im].xs * map[im].ys;
  112. CREATE( map[im].cell, struct mapcell, num_cell );
  113. memcpy( map[im].cell, map[m].cell, num_cell * sizeof(struct mapcell) );
  114. size = map[im].bxs * map[im].bys * sizeof(struct block_list*);
  115. map[im].block = (struct block_list**)aCalloc(size, 1);
  116. map[im].block_mob = (struct block_list**)aCalloc(size, 1);
  117. memset(map[im].npc, 0x00, sizeof(map[i].npc));
  118. map[im].npc_num = 0;
  119. memset(map[im].moblist, 0x00, sizeof(map[im].moblist));
  120. map[im].mob_delete_timer = INVALID_TIMER;
  121. map[im].m = im;
  122. map[im].instance_id = instance_id;
  123. map[im].instance_src_map = m;
  124. map[m].flag.src4instance = 1; // Flag this map as a src map for instances
  125. instance[instance_id].map[instance[instance_id].num_map++] = im; // Attach to actual instance
  126. map_addmap2db(&map[im]);
  127. return im;
  128. }
  129. /*--------------------------------------
  130. * m : source map of this instance
  131. * party_id : source party of this instance
  132. * type : result (0 = map id | 1 = instance id)
  133. *--------------------------------------*/
  134. int instance_map2imap(int m, int instance_id)
  135. {
  136. int i;
  137. for( i = 0; i < instance[instance_id].num_map; i++ )
  138. {
  139. if( instance[instance_id].map[i] && map[instance[instance_id].map[i]].instance_src_map == m )
  140. return instance[instance_id].map[i];
  141. }
  142. return -1;
  143. }
  144. /*--------------------------------------
  145. * m : source map
  146. * instance_id : where to search
  147. * result : mapid of map "m" in this instance
  148. *--------------------------------------*/
  149. int instance_mapid2imapid(int m, int instance_id)
  150. {
  151. int i, max;
  152. if( map[m].flag.src4instance == 0 )
  153. return m; // not instances found for this map
  154. else if( map[m].instance_id )
  155. { // This map is a instance, not a src map instance
  156. ShowError("map_instance_mapid2imapid: already instanced (%d / %d)\n", m, instance_id);
  157. return -1;
  158. }
  159. if( instance_id <= 0 )
  160. return -1;
  161. max = instance[instance_id].num_map;
  162. for( i = 0; i < max; i++ )
  163. if( map[instance[instance_id].map[i]].instance_src_map == m )
  164. return instance[instance_id].map[i];
  165. return -1;
  166. }
  167. /*--------------------------------------
  168. * map_instance_map_npcsub
  169. * Used on Init instance. Duplicates each script on source map
  170. *--------------------------------------*/
  171. int instance_map_npcsub(struct block_list* bl, va_list args)
  172. {
  173. struct npc_data* nd = (struct npc_data*)bl;
  174. int m = va_arg(args, int); // Destination Map
  175. npc_duplicate4instance(nd, m);
  176. return 1;
  177. }
  178. /*--------------------------------------
  179. * Init all map on the instance. Npcs are created here
  180. *--------------------------------------*/
  181. void instance_init(int instance_id)
  182. {
  183. int i;
  184. if( !instance_id ) return;
  185. if( instance[instance_id].state != INSTANCE_IDLE )
  186. {
  187. ShowError("instance_init: instance already initialited.\n");
  188. return;
  189. }
  190. for( i = 0; i < instance[instance_id].num_map; i++ )
  191. map_foreachinmap(instance_map_npcsub, map[instance[instance_id].map[i]].instance_src_map, BL_NPC, instance[instance_id].map[i]);
  192. instance[instance_id].state = INSTANCE_BUSSY;
  193. ShowInfo("[Instance] Initialized %s.\n", instance[instance_id].name);
  194. }
  195. /*--------------------------------------
  196. * Used on instance deleting process.
  197. * Warps all players on each instance map to its save points.
  198. *--------------------------------------*/
  199. int instance_del_load(struct map_session_data* sd, va_list args)
  200. {
  201. int m = va_arg(args,int);
  202. if( !sd || sd->bl.m != m )
  203. return 0;
  204. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 0);
  205. return 1;
  206. }
  207. /*--------------------------------------
  208. * Removes a simple instance map
  209. *--------------------------------------*/
  210. void instance_del_map(int m)
  211. {
  212. int sm, i;
  213. if( m <= 0 || !map[m].instance_id )
  214. {
  215. ShowError("Tried to remove non-existing instance map (%d)\n", m);
  216. return;
  217. }
  218. sm = map[m].instance_src_map;
  219. map_foreachpc(instance_del_load, m);
  220. map_foreachinmap(cleanup_sub, m, BL_ALL);
  221. if( map[m].mob_delete_timer != INVALID_TIMER )
  222. delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
  223. mapindex_removemap( map[m].index );
  224. // Free memory
  225. aFree(map[m].cell);
  226. aFree(map[m].block);
  227. aFree(map[m].block_mob);
  228. // Remove from instance
  229. for( i = 0; i < instance[map[m].instance_id].num_map; i++ )
  230. {
  231. if( instance[map[m].instance_id].map[i] == m )
  232. {
  233. instance[map[m].instance_id].num_map--;
  234. for( ; i < instance[map[m].instance_id].num_map; i++ )
  235. instance[map[m].instance_id].map[i] = instance[map[m].instance_id].map[i+1];
  236. i = -1;
  237. break;
  238. }
  239. }
  240. if( i == instance[map[m].instance_id].num_map )
  241. ShowError("map_instance_del: failed to remove %s from instance list (%s): %d\n", map[m].name, instance[map[m].instance_id].name, m);
  242. map_removemapdb(&map[m]);
  243. memset(&map[m], 0x00, sizeof(map[0]));
  244. }
  245. /*--------------------------------------
  246. * Used for instance variables. Clean each variable from memory.
  247. *--------------------------------------*/
  248. void instance_destroy_freesvar(void *key, void *data, va_list args)
  249. {
  250. if( data ) aFree(data);
  251. }
  252. /*--------------------------------------
  253. * Timer to destroy instance by process or idle
  254. *--------------------------------------*/
  255. int instance_destroy_timer(int tid, unsigned int tick, int id, intptr data)
  256. {
  257. instance_destroy(id);
  258. return 0;
  259. }
  260. /*--------------------------------------
  261. * Removes a instance, all its maps and npcs.
  262. *--------------------------------------*/
  263. void instance_destroy(int instance_id)
  264. {
  265. int last = 0, type;
  266. struct party_data *p;
  267. time_t now = time(NULL);
  268. if( !instance_id || instance[instance_id].state == INSTANCE_FREE )
  269. return;
  270. if( instance[instance_id].progress_timeout && instance[instance_id].progress_timeout <= now )
  271. type = 1;
  272. else if( instance[instance_id].idle_timeout && instance[instance_id].idle_timeout <= now )
  273. type = 2;
  274. else
  275. type = 3;
  276. clif_instance(instance_id, 5, type); // Report users this instance has been destroyed
  277. while( instance[instance_id].num_map && last != instance[instance_id].map[0] )
  278. { // Remove all maps from instance
  279. last = instance[instance_id].map[0];
  280. instance_del_map( instance[instance_id].map[0] );
  281. }
  282. if( instance[instance_id].ivar )
  283. linkdb_final( &instance[instance_id].ivar ); // Remove numeric vars
  284. if( instance[instance_id].svar )
  285. { // Remove string vars
  286. linkdb_foreach( &instance[instance_id].svar, instance_destroy_freesvar );
  287. linkdb_final( &instance[instance_id].svar );
  288. }
  289. if( instance[instance_id].progress_timer != INVALID_TIMER )
  290. delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
  291. if( instance[instance_id].idle_timer != INVALID_TIMER )
  292. delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
  293. instance[instance_id].ivar = NULL;
  294. instance[instance_id].svar = NULL;
  295. if( instance[instance_id].party_id && (p = party_search(instance[instance_id].party_id)) != NULL )
  296. p->instance_id = 0; // Update Party information
  297. ShowInfo("[Instance] Destroyed %s.\n", instance[instance_id].name);
  298. memset( &instance[instance_id], 0x00, sizeof(instance[0]) );
  299. instance[instance_id].state = INSTANCE_FREE;
  300. }
  301. /*--------------------------------------
  302. * Checks if there are users in the instance or not to start idle timer
  303. *--------------------------------------*/
  304. void instance_check_idle(int instance_id)
  305. {
  306. bool idle = true;
  307. time_t now = time(NULL);
  308. if( !instance_id || instance[instance_id].idle_timeoutval == 0 )
  309. return;
  310. if( instance[instance_id].users )
  311. idle = false;
  312. if( instance[instance_id].idle_timer != INVALID_TIMER && !idle )
  313. {
  314. delete_timer(instance[instance_id].idle_timer, instance_destroy_timer);
  315. instance[instance_id].idle_timer = INVALID_TIMER;
  316. instance[instance_id].idle_timeout = 0;
  317. clif_instance(instance_id, 3, 0); // Notify instance users normal instance expiration
  318. }
  319. else if( instance[instance_id].idle_timer == INVALID_TIMER && idle )
  320. {
  321. instance[instance_id].idle_timeout = now + instance[instance_id].idle_timeoutval;
  322. instance[instance_id].idle_timer = add_timer( gettick() + (unsigned int)instance[instance_id].idle_timeoutval * 1000, instance_destroy_timer, instance_id, 0);
  323. clif_instance(instance_id, 4, 0); // Notify instance users it will be destroyed of no user join it again in "X" time
  324. }
  325. }
  326. /*--------------------------------------
  327. * Set instance Timers
  328. *--------------------------------------*/
  329. void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout)
  330. {
  331. time_t now = time(0);
  332. if( !instance_id )
  333. return;
  334. if( instance[instance_id].progress_timer != INVALID_TIMER )
  335. delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
  336. if( instance[instance_id].idle_timer != INVALID_TIMER )
  337. delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
  338. if( progress_timeout )
  339. {
  340. instance[instance_id].progress_timeoutval = progress_timeout;
  341. instance[instance_id].progress_timeout = now + progress_timeout;
  342. instance[instance_id].progress_timer = add_timer( gettick() + progress_timeout * 1000, instance_destroy_timer, instance_id, 0);
  343. }
  344. else
  345. {
  346. instance[instance_id].progress_timeoutval = 0;
  347. instance[instance_id].progress_timeout = 0;
  348. instance[instance_id].progress_timer = INVALID_TIMER;
  349. }
  350. if( idle_timeout )
  351. {
  352. instance[instance_id].idle_timeoutval = idle_timeout;
  353. instance[instance_id].idle_timer = INVALID_TIMER;
  354. instance_check_idle(instance_id);
  355. }
  356. else
  357. {
  358. instance[instance_id].idle_timeoutval = 0;
  359. instance[instance_id].idle_timeout = 0;
  360. instance[instance_id].idle_timer = INVALID_TIMER;
  361. }
  362. if( instance[instance_id].idle_timer == INVALID_TIMER && instance[instance_id].progress_timer != INVALID_TIMER )
  363. clif_instance(instance_id, 3, 0);
  364. }
  365. /*--------------------------------------
  366. * Checks if sd in on a instance and should be kicked from it
  367. *--------------------------------------*/
  368. void instance_check_kick(struct map_session_data *sd)
  369. {
  370. int m = sd->bl.m;
  371. clif_instance_leave(sd->fd);
  372. if( map[m].instance_id )
  373. { // User was on the instance map
  374. if( map[m].save.map )
  375. pc_setpos(sd, map[m].save.map, map[m].save.x, map[m].save.y, 3);
  376. else
  377. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  378. }
  379. }
  380. void do_init_instance(void)
  381. {
  382. memset(instance, 0x00, sizeof(instance));
  383. add_timer_func_list(instance_destroy_timer, "instance_destroy_timer");
  384. }