instance.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 )
  185. return; // nothing to do
  186. for( i = 0; i < instance[instance_id].num_map; i++ )
  187. map_foreachinmap(instance_map_npcsub, map[instance[instance_id].map[i]].instance_src_map, BL_NPC, instance[instance_id].map[i]);
  188. instance[instance_id].state = INSTANCE_BUSSY;
  189. ShowInfo("[Instance] Initialized %s.\n", instance[instance_id].name);
  190. }
  191. /*--------------------------------------
  192. * Used on instance deleting process.
  193. * Warps all players on each instance map to its save points.
  194. *--------------------------------------*/
  195. int instance_del_load(struct map_session_data* sd, va_list args)
  196. {
  197. int m = va_arg(args,int);
  198. if( !sd || sd->bl.m != m )
  199. return 0;
  200. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 0);
  201. return 1;
  202. }
  203. /*--------------------------------------
  204. * Removes a simple instance map
  205. *--------------------------------------*/
  206. void instance_del_map(int m)
  207. {
  208. int sm, i;
  209. if( m <= 0 || !map[m].instance_id )
  210. {
  211. ShowError("Tried to remove non-existing instance map (%d)\n", m);
  212. return;
  213. }
  214. sm = map[m].instance_src_map;
  215. map_foreachpc(instance_del_load, m);
  216. map_foreachinmap(cleanup_sub, m, BL_ALL);
  217. if( map[m].mob_delete_timer != INVALID_TIMER )
  218. delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
  219. mapindex_removemap( map[m].index );
  220. // Free memory
  221. aFree(map[m].cell);
  222. aFree(map[m].block);
  223. aFree(map[m].block_mob);
  224. // Remove from instance
  225. for( i = 0; i < instance[map[m].instance_id].num_map; i++ )
  226. {
  227. if( instance[map[m].instance_id].map[i] == m )
  228. {
  229. instance[map[m].instance_id].num_map--;
  230. for( ; i < instance[map[m].instance_id].num_map; i++ )
  231. instance[map[m].instance_id].map[i] = instance[map[m].instance_id].map[i+1];
  232. i = -1;
  233. break;
  234. }
  235. }
  236. if( i == instance[map[m].instance_id].num_map )
  237. ShowError("map_instance_del: failed to remove %s from instance list (%s): %d\n", map[m].name, instance[map[m].instance_id].name, m);
  238. map_removemapdb(&map[m]);
  239. memset(&map[m], 0x00, sizeof(map[0]));
  240. }
  241. /*--------------------------------------
  242. * Used for instance variables. Clean each variable from memory.
  243. *--------------------------------------*/
  244. void instance_destroy_freesvar(void *key, void *data, va_list args)
  245. {
  246. if( data ) aFree(data);
  247. }
  248. /*--------------------------------------
  249. * Timer to destroy instance by process or idle
  250. *--------------------------------------*/
  251. int instance_destroy_timer(int tid, unsigned int tick, int id, intptr data)
  252. {
  253. instance_destroy(id);
  254. return 0;
  255. }
  256. /*--------------------------------------
  257. * Removes a instance, all its maps and npcs.
  258. *--------------------------------------*/
  259. void instance_destroy(int instance_id)
  260. {
  261. int last = 0, type;
  262. struct party_data *p;
  263. time_t now = time(NULL);
  264. if( !instance_id || instance[instance_id].state == INSTANCE_FREE )
  265. return; // nothing to do
  266. if( instance[instance_id].progress_timeout && instance[instance_id].progress_timeout <= now )
  267. type = 1;
  268. else if( instance[instance_id].idle_timeout && instance[instance_id].idle_timeout <= now )
  269. type = 2;
  270. else
  271. type = 3;
  272. clif_instance(instance_id, 5, type); // Report users this instance has been destroyed
  273. while( instance[instance_id].num_map && last != instance[instance_id].map[0] )
  274. { // Remove all maps from instance
  275. last = instance[instance_id].map[0];
  276. instance_del_map( instance[instance_id].map[0] );
  277. }
  278. if( instance[instance_id].ivar )
  279. linkdb_final( &instance[instance_id].ivar ); // Remove numeric vars
  280. if( instance[instance_id].svar )
  281. { // Remove string vars
  282. linkdb_foreach( &instance[instance_id].svar, instance_destroy_freesvar );
  283. linkdb_final( &instance[instance_id].svar );
  284. }
  285. if( instance[instance_id].progress_timer != INVALID_TIMER )
  286. delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
  287. if( instance[instance_id].idle_timer != INVALID_TIMER )
  288. delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
  289. instance[instance_id].ivar = NULL;
  290. instance[instance_id].svar = NULL;
  291. if( instance[instance_id].party_id && (p = party_search(instance[instance_id].party_id)) != NULL )
  292. p->instance_id = 0; // Update Party information
  293. ShowInfo("[Instance] Destroyed %s.\n", instance[instance_id].name);
  294. memset( &instance[instance_id], 0x00, sizeof(instance[0]) );
  295. instance[instance_id].state = INSTANCE_FREE;
  296. }
  297. /*--------------------------------------
  298. * Checks if there are users in the instance or not to start idle timer
  299. *--------------------------------------*/
  300. void instance_check_idle(int instance_id)
  301. {
  302. bool idle = true;
  303. time_t now = time(NULL);
  304. if( !instance_id || instance[instance_id].idle_timeoutval == 0 )
  305. return;
  306. if( instance[instance_id].users )
  307. idle = false;
  308. if( instance[instance_id].idle_timer != INVALID_TIMER && !idle )
  309. {
  310. delete_timer(instance[instance_id].idle_timer, instance_destroy_timer);
  311. instance[instance_id].idle_timer = INVALID_TIMER;
  312. instance[instance_id].idle_timeout = 0;
  313. clif_instance(instance_id, 3, 0); // Notify instance users normal instance expiration
  314. }
  315. else if( instance[instance_id].idle_timer == INVALID_TIMER && idle )
  316. {
  317. instance[instance_id].idle_timeout = now + instance[instance_id].idle_timeoutval;
  318. instance[instance_id].idle_timer = add_timer( gettick() + (unsigned int)instance[instance_id].idle_timeoutval * 1000, instance_destroy_timer, instance_id, 0);
  319. clif_instance(instance_id, 4, 0); // Notify instance users it will be destroyed of no user join it again in "X" time
  320. }
  321. }
  322. /*--------------------------------------
  323. * Set instance Timers
  324. *--------------------------------------*/
  325. void instance_set_timeout(int instance_id, unsigned int progress_timeout, unsigned int idle_timeout)
  326. {
  327. time_t now = time(0);
  328. if( !instance_id )
  329. return;
  330. if( instance[instance_id].progress_timer != INVALID_TIMER )
  331. delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);
  332. if( instance[instance_id].idle_timer != INVALID_TIMER )
  333. delete_timer( instance[instance_id].idle_timer, instance_destroy_timer);
  334. if( progress_timeout )
  335. {
  336. instance[instance_id].progress_timeoutval = progress_timeout;
  337. instance[instance_id].progress_timeout = now + progress_timeout;
  338. instance[instance_id].progress_timer = add_timer( gettick() + progress_timeout * 1000, instance_destroy_timer, instance_id, 0);
  339. }
  340. else
  341. {
  342. instance[instance_id].progress_timeoutval = 0;
  343. instance[instance_id].progress_timeout = 0;
  344. instance[instance_id].progress_timer = INVALID_TIMER;
  345. }
  346. if( idle_timeout )
  347. {
  348. instance[instance_id].idle_timeoutval = idle_timeout;
  349. instance[instance_id].idle_timer = INVALID_TIMER;
  350. instance_check_idle(instance_id);
  351. }
  352. else
  353. {
  354. instance[instance_id].idle_timeoutval = 0;
  355. instance[instance_id].idle_timeout = 0;
  356. instance[instance_id].idle_timer = INVALID_TIMER;
  357. }
  358. if( instance[instance_id].idle_timer == INVALID_TIMER && instance[instance_id].progress_timer != INVALID_TIMER )
  359. clif_instance(instance_id, 3, 0);
  360. }
  361. /*--------------------------------------
  362. * Checks if sd in on a instance and should be kicked from it
  363. *--------------------------------------*/
  364. void instance_check_kick(struct map_session_data *sd)
  365. {
  366. int m = sd->bl.m;
  367. clif_instance_leave(sd->fd);
  368. if( map[m].instance_id )
  369. { // User was on the instance map
  370. if( map[m].save.map )
  371. pc_setpos(sd, map[m].save.map, map[m].save.x, map[m].save.y, 3);
  372. else
  373. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  374. }
  375. }
  376. void do_init_instance(void)
  377. {
  378. memset(instance, 0x00, sizeof(instance));
  379. add_timer_func_list(instance_destroy_timer, "instance_destroy_timer");
  380. }