instance.c 14 KB

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