PageRenderTime 145ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/map/unit.c

https://gitlab.com/evol/hercules
C | 2942 lines | 2285 code | 369 blank | 288 comment | 1050 complexity | 6bd9a1720c1aff62b72f910d5127fa24 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. /**
  2. * This file is part of Hercules.
  3. * http://herc.ws - http://github.com/HerculesWS/Hercules
  4. *
  5. * Copyright (C) 2012-2015 Hercules Dev Team
  6. * Copyright (C) Athena Dev Teams
  7. *
  8. * Hercules is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define HERCULES_CORE
  22. #include "config/core.h" // RENEWAL_CAST
  23. #include "unit.h"
  24. #include "map/battle.h"
  25. #include "map/battleground.h"
  26. #include "map/chat.h"
  27. #include "map/chrif.h"
  28. #include "map/clif.h"
  29. #include "map/duel.h"
  30. #include "map/elemental.h"
  31. #include "map/guild.h"
  32. #include "map/homunculus.h"
  33. #include "map/instance.h"
  34. #include "map/intif.h"
  35. #include "map/map.h"
  36. #include "map/mercenary.h"
  37. #include "map/mob.h"
  38. #include "map/npc.h"
  39. #include "map/party.h"
  40. #include "map/path.h"
  41. #include "map/pc.h"
  42. #include "map/pet.h"
  43. #include "map/script.h"
  44. #include "map/skill.h"
  45. #include "map/status.h"
  46. #include "map/storage.h"
  47. #include "map/trade.h"
  48. #include "map/vending.h"
  49. #include "common/HPM.h"
  50. #include "common/db.h"
  51. #include "common/memmgr.h"
  52. #include "common/nullpo.h"
  53. #include "common/random.h"
  54. #include "common/showmsg.h"
  55. #include "common/socket.h"
  56. #include "common/timer.h"
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. const short dirx[8]={0,-1,-1,-1,0,1,1,1};
  61. const short diry[8]={1,1,0,-1,-1,-1,0,1};
  62. struct unit_interface unit_s;
  63. struct unit_interface *unit;
  64. /**
  65. * Returns the unit_data for the given block_list. If the object is using
  66. * shared unit_data (i.e. in case of BL_NPC), it returns the shared data.
  67. * @param bl block_list to process
  68. * @return a pointer to the given object's unit_data
  69. **/
  70. struct unit_data* unit_bl2ud(struct block_list *bl)
  71. {
  72. if (bl == NULL) return NULL;
  73. if (bl->type == BL_PC) return &BL_UCAST(BL_PC, bl)->ud;
  74. if (bl->type == BL_MOB) return &BL_UCAST(BL_MOB, bl)->ud;
  75. if (bl->type == BL_PET) return &BL_UCAST(BL_PET, bl)->ud;
  76. if (bl->type == BL_NPC) return BL_UCAST(BL_NPC, bl)->ud;
  77. if (bl->type == BL_HOM) return &BL_UCAST(BL_HOM, bl)->ud;
  78. if (bl->type == BL_MER) return &BL_UCAST(BL_MER, bl)->ud;
  79. if (bl->type == BL_ELEM) return &BL_UCAST(BL_ELEM, bl)->ud;
  80. return NULL;
  81. }
  82. /**
  83. * Returns the unit_data for the given block_list. If the object is using
  84. * shared unit_data (i.e. in case of BL_NPC), it recreates a copy of the
  85. * data so that it's safe to modify.
  86. * @param bl block_list to process
  87. * @return a pointer to the given object's unit_data
  88. */
  89. struct unit_data *unit_bl2ud2(struct block_list *bl)
  90. {
  91. struct npc_data *nd = BL_CAST(BL_NPC, bl);
  92. if (nd != NULL && nd->ud == &npc->base_ud) {
  93. nd->ud = NULL;
  94. CREATE(nd->ud, struct unit_data, 1);
  95. unit->dataset(&nd->bl);
  96. }
  97. return unit->bl2ud(bl);
  98. }
  99. int unit_walktoxy_sub(struct block_list *bl)
  100. {
  101. int i;
  102. struct walkpath_data wpd;
  103. struct unit_data *ud = NULL;
  104. nullpo_retr(1, bl);
  105. ud = unit->bl2ud(bl);
  106. if(ud == NULL) return 0;
  107. memset(&wpd, 0, sizeof(wpd));
  108. if( !path->search(&wpd,bl,bl->m,bl->x,bl->y,ud->to_x,ud->to_y,ud->state.walk_easy,CELL_CHKNOPASS) )
  109. return 0;
  110. #ifdef OFFICIAL_WALKPATH
  111. if( !path->search_long(NULL, bl, bl->m, bl->x, bl->y, ud->to_x, ud->to_y, CELL_CHKNOPASS) // Check if there is an obstacle between
  112. && wpd.path_len > 14 // Official number of walkable cells is 14 if and only if there is an obstacle between. [malufett]
  113. && (bl->type != BL_NPC) ) // If type is a NPC, please disregard.
  114. return 0;
  115. #endif
  116. memcpy(&ud->walkpath,&wpd,sizeof(wpd));
  117. if (ud->target_to && ud->chaserange>1) {
  118. //Generally speaking, the walk path is already to an adjacent tile
  119. //so we only need to shorten the path if the range is greater than 1.
  120. //Trim the last part of the path to account for range,
  121. //but always move at least one cell when requested to move.
  122. for (i = (ud->chaserange*10)-10; i > 0 && ud->walkpath.path_len>1;) {
  123. uint8 dir;
  124. ud->walkpath.path_len--;
  125. dir = ud->walkpath.path[ud->walkpath.path_len];
  126. if (dir&1)
  127. i -= MOVE_COST*20; //When chasing, units will target a diamond-shaped area in range [Playtester]
  128. else
  129. i -= MOVE_COST;
  130. ud->to_x -= dirx[dir];
  131. ud->to_y -= diry[dir];
  132. }
  133. }
  134. ud->state.change_walk_target=0;
  135. if (bl->type == BL_PC) {
  136. struct map_session_data *sd = BL_UCAST(BL_PC, bl);
  137. sd->head_dir = 0;
  138. clif->walkok(sd);
  139. }
  140. clif->move(ud);
  141. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  142. i = -1;
  143. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  144. i = status->get_speed(bl)*MOVE_DIAGONAL_COST/MOVE_COST;
  145. else
  146. i = status->get_speed(bl);
  147. if( i > 0)
  148. ud->walktimer = timer->add(timer->gettick()+i,unit->walktoxy_timer,bl->id,i);
  149. return 1;
  150. }
  151. /**
  152. * Triggered on full step if stepaction is true and executes remembered action.
  153. * @param tid: Timer ID
  154. * @param tick: Unused
  155. * @param id: ID of bl to do the action
  156. * @param data: Not used
  157. * @return 1: Success 0: Fail (No valid bl)
  158. */
  159. int unit_step_timer(int tid, int64 tick, int id, intptr_t data)
  160. {
  161. struct block_list *bl;
  162. struct unit_data *ud;
  163. int target_id;
  164. bl = map->id2bl(id);
  165. if (!bl || bl->prev == NULL)
  166. return 0;
  167. ud = unit->bl2ud(bl);
  168. if(!ud)
  169. return 0;
  170. if(ud->steptimer != tid) {
  171. ShowError("unit_step_timer mismatch %d != %d\n",ud->steptimer,tid);
  172. return 0;
  173. }
  174. ud->steptimer = INVALID_TIMER;
  175. if(!ud->stepaction)
  176. return 0;
  177. //Set to false here because if an error occurs, it should not be executed again
  178. ud->stepaction = false;
  179. if(!ud->target_to)
  180. return 0;
  181. //Flush target_to as it might contain map coordinates which should not be used by other functions
  182. target_id = ud->target_to;
  183. ud->target_to = 0;
  184. //If stepaction is set then we remembered a client request that should be executed on the next step
  185. //Execute request now if target is in attack range
  186. if(ud->stepskill_id && skill->get_inf(ud->stepskill_id) & INF_GROUND_SKILL) {
  187. //Execute ground skill
  188. struct map_data *md = &map->list[bl->m];
  189. unit->skilluse_pos(bl, target_id%md->xs, target_id/md->xs, ud->stepskill_id, ud->stepskill_lv);
  190. } else {
  191. //If a player has target_id set and target is in range, attempt attack
  192. struct block_list *tbl = map->id2bl(target_id);
  193. if (!tbl || !status->check_visibility(bl, tbl)) {
  194. return 0;
  195. }
  196. if(ud->stepskill_id == 0) {
  197. //Execute normal attack
  198. unit->attack(bl, tbl->id, (ud->state.attack_continue) + 2);
  199. } else {
  200. //Execute non-ground skill
  201. unit->skilluse_id(bl, tbl->id, ud->stepskill_id, ud->stepskill_lv);
  202. }
  203. }
  204. return 1;
  205. }
  206. int unit_walktoxy_timer(int tid, int64 tick, int id, intptr_t data) {
  207. int i;
  208. int x,y,dx,dy;
  209. unsigned char icewall_walk_block;
  210. uint8 dir;
  211. struct block_list *bl;
  212. struct map_session_data *sd;
  213. struct mob_data *md;
  214. struct unit_data *ud;
  215. struct mercenary_data *mrd;
  216. bl = map->id2bl(id);
  217. if(bl == NULL)
  218. return 0;
  219. sd = BL_CAST(BL_PC, bl);
  220. md = BL_CAST(BL_MOB, bl);
  221. mrd = BL_CAST(BL_MER, bl);
  222. ud = unit->bl2ud(bl);
  223. if(ud == NULL) return 0;
  224. if(ud->walktimer != tid){
  225. ShowError("unit_walk_timer mismatch %d != %d\n",ud->walktimer,tid);
  226. return 0;
  227. }
  228. ud->walktimer = INVALID_TIMER;
  229. if (bl->prev == NULL) return 0; // Stop moved because it is missing from the block_list
  230. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  231. return 0;
  232. if(ud->walkpath.path[ud->walkpath.path_pos]>=8)
  233. return 1;
  234. x = bl->x;
  235. y = bl->y;
  236. dir = ud->walkpath.path[ud->walkpath.path_pos];
  237. ud->dir = dir;
  238. dx = dirx[(int)dir];
  239. dy = diry[(int)dir];
  240. //Get icewall walk block depending on boss mode (players can't be trapped)
  241. if(md && md->status.mode&MD_BOSS)
  242. icewall_walk_block = battle_config.boss_icewall_walk_block;
  243. else if(md)
  244. icewall_walk_block = battle_config.mob_icewall_walk_block;
  245. else
  246. icewall_walk_block = 0;
  247. //Monsters will walk into an icewall from the west and south if they already started walking
  248. if (map->getcell(bl->m, bl, x + dx, y + dy, CELL_CHKNOPASS)
  249. && (icewall_walk_block == 0 || !map->getcell(bl->m, bl, x + dx, y + dy, CELL_CHKICEWALL) || dx < 0 || dy < 0))
  250. return unit->walktoxy_sub(bl);
  251. //Monsters can only leave icewalls to the west and south
  252. //But if movement fails more than icewall_walk_block times, they can ignore this rule
  253. if (md && md->walktoxy_fail_count < icewall_walk_block && map->getcell(bl->m, bl, x, y, CELL_CHKICEWALL) && (dx > 0 || dy > 0)) {
  254. //Needs to be done here so that rudeattack skills are invoked
  255. md->walktoxy_fail_count++;
  256. clif->fixpos(bl);
  257. //Monsters in this situation first use a chase skill, then unlock target and then use an idle skill
  258. if (!(++ud->walk_count%WALK_SKILL_INTERVAL))
  259. mob->skill_use(md, tick, -1);
  260. mob->unlocktarget(md, tick);
  261. if (!(++ud->walk_count%WALK_SKILL_INTERVAL))
  262. mob->skill_use(md, tick, -1);
  263. return 0;
  264. }
  265. //Refresh view for all those we lose sight
  266. map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
  267. x += dx;
  268. y += dy;
  269. map->moveblock(bl, x, y, tick);
  270. ud->walk_count++; //walked cell counter, to be used for walk-triggered skills. [Skotlex]
  271. status_change_end(bl, SC_ROLLINGCUTTER, INVALID_TIMER); //If you move, you lose your counters. [malufett]
  272. if (bl->x != x || bl->y != y || ud->walktimer != INVALID_TIMER)
  273. return 0; //map->moveblock has altered the object beyond what we expected (moved/warped it)
  274. ud->walktimer = -2; // arbitrary non-INVALID_TIMER value to make the clif code send walking packets
  275. map->foreachinmovearea(clif->insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
  276. ud->walktimer = INVALID_TIMER;
  277. if(sd) {
  278. if( sd->touching_id )
  279. npc->touchnext_areanpc(sd,false);
  280. if (map->getcell(bl->m, bl, x, y, CELL_CHKNPC)) {
  281. npc->touch_areanpc(sd,bl->m,x,y);
  282. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  283. return 0;
  284. } else
  285. npc->untouch_areanpc(sd, bl->m, x, y);
  286. if( sd->md ) { // mercenary should be warped after being 3 seconds too far from the master [greenbox]
  287. if( !check_distance_bl(&sd->bl, &sd->md->bl, MAX_MER_DISTANCE) ) {
  288. if (sd->md->masterteleport_timer == 0)
  289. sd->md->masterteleport_timer = timer->gettick();
  290. else if (DIFF_TICK(timer->gettick(), sd->md->masterteleport_timer) > 3000) {
  291. sd->md->masterteleport_timer = 0;
  292. unit->warp( &sd->md->bl, sd->bl.m, sd->bl.x, sd->bl.y, CLR_TELEPORT );
  293. }
  294. } else // reset the tick, he is not far anymore
  295. sd->md->masterteleport_timer = 0;
  296. }
  297. if( sd->hd ) {
  298. if( homun_alive(sd->hd) && !check_distance_bl(&sd->bl, &sd->hd->bl, MAX_MER_DISTANCE) ) {
  299. if (sd->hd->masterteleport_timer == 0)
  300. sd->hd->masterteleport_timer = timer->gettick();
  301. else if (DIFF_TICK(timer->gettick(), sd->hd->masterteleport_timer) > 3000) {
  302. sd->hd->masterteleport_timer = 0;
  303. unit->warp( &sd->hd->bl, sd->bl.m, sd->bl.x, sd->bl.y, CLR_TELEPORT );
  304. }
  305. } else
  306. sd->hd->masterteleport_timer = 0;
  307. }
  308. } else if (md) {
  309. //Movement was successful, reset walktoxy_fail_count
  310. md->walktoxy_fail_count = 0;
  311. if (map->getcell(bl->m, bl, x, y, CELL_CHKNPC)) {
  312. if( npc->touch_areanpc2(md) ) return 0; // Warped
  313. } else
  314. md->areanpc_id = 0;
  315. if (md->min_chase > md->db->range3) md->min_chase--;
  316. //Walk skills are triggered regardless of target due to the idle-walk mob state.
  317. //But avoid triggering on stop-walk calls.
  318. if (tid != INVALID_TIMER
  319. && !(ud->walk_count%WALK_SKILL_INTERVAL)
  320. && map->list[bl->m].users > 0
  321. && mob->skill_use(md, tick, -1)
  322. ) {
  323. if (!(ud->skill_id == NPC_SELFDESTRUCTION && ud->skilltimer != INVALID_TIMER)
  324. && md->state.skillstate != MSS_WALK //Walk skills are supposed to be used while walking
  325. ) {
  326. //Skill used, abort walking
  327. clif->fixpos(bl); //Fix position as walk has been canceled.
  328. return 0;
  329. }
  330. //Resend walk packet for proper Self Destruction display.
  331. clif->move(ud);
  332. }
  333. }
  334. else if( mrd && mrd->master )
  335. {
  336. if (!check_distance_bl(&mrd->master->bl, bl, MAX_MER_DISTANCE))
  337. {
  338. // mercenary should be warped after being 3 seconds too far from the master [greenbox]
  339. if (mrd->masterteleport_timer == 0)
  340. {
  341. mrd->masterteleport_timer = timer->gettick();
  342. }
  343. else if (DIFF_TICK(timer->gettick(), mrd->masterteleport_timer) > 3000)
  344. {
  345. mrd->masterteleport_timer = 0;
  346. unit->warp( bl, mrd->master->bl.m, mrd->master->bl.x, mrd->master->bl.y, CLR_TELEPORT );
  347. }
  348. }
  349. else
  350. {
  351. mrd->masterteleport_timer = 0;
  352. }
  353. }
  354. if(tid == INVALID_TIMER) //A directly invoked timer is from battle_stop_walking, therefore the rest is irrelevant.
  355. return 0;
  356. //If stepaction is set then we remembered a client request that should be executed on the next step
  357. if (ud->stepaction && ud->target_to) {
  358. //Delete old stepaction even if not executed yet, the latest command is what counts
  359. if(ud->steptimer != INVALID_TIMER) {
  360. timer->delete(ud->steptimer, unit->step_timer);
  361. ud->steptimer = INVALID_TIMER;
  362. }
  363. //Delay stepactions by half a step (so they are executed at full step)
  364. if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  365. i = status->get_speed(bl)*14/20;
  366. else
  367. i = status->get_speed(bl)/2;
  368. ud->steptimer = timer->add(tick+i, unit->step_timer, bl->id, 0);
  369. }
  370. if(ud->state.change_walk_target) {
  371. if(unit->walktoxy_sub(bl)) {
  372. return 1;
  373. } else {
  374. clif->fixpos(bl);
  375. return 0;
  376. }
  377. }
  378. ud->walkpath.path_pos++;
  379. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  380. i = -1;
  381. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  382. i = status->get_speed(bl)*14/10;
  383. else
  384. i = status->get_speed(bl);
  385. if(i > 0) {
  386. ud->walktimer = timer->add(tick+i,unit->walktoxy_timer,id,i);
  387. if( md && DIFF_TICK(tick,md->dmgtick) < 3000 )//not required not damaged recently
  388. clif->move(ud);
  389. } else if(ud->state.running) {
  390. //Keep trying to run.
  391. if ( !(unit->run(bl, NULL, SC_RUN) || unit->run(bl, sd, SC_WUGDASH)) )
  392. ud->state.running = 0;
  393. } else if (!ud->stepaction && ud->target_to) {
  394. //Update target trajectory.
  395. struct block_list *tbl = map->id2bl(ud->target_to);
  396. if (!tbl || !status->check_visibility(bl, tbl)) {
  397. //Cancel chase.
  398. ud->to_x = bl->x;
  399. ud->to_y = bl->y;
  400. if (tbl && bl->type == BL_MOB && mob->warpchase(BL_UCAST(BL_MOB, bl), tbl))
  401. return 0;
  402. ud->target_to = 0;
  403. return 0;
  404. }
  405. if (tbl->m == bl->m && check_distance_bl(bl, tbl, ud->chaserange)) {
  406. //Reached destination.
  407. if (ud->state.attack_continue) {
  408. //Aegis uses one before every attack, we should
  409. //only need this one for syncing purposes. [Skotlex]
  410. ud->target_to = 0;
  411. clif->fixpos(bl);
  412. unit->attack(bl, tbl->id, ud->state.attack_continue);
  413. }
  414. } else { //Update chase-path
  415. unit->walktobl(bl, tbl, ud->chaserange, ud->state.walk_easy|(ud->state.attack_continue? 1 : 0));
  416. return 0;
  417. }
  418. } else {
  419. //Stopped walking. Update to_x and to_y to current location [Skotlex]
  420. ud->to_x = bl->x;
  421. ud->to_y = bl->y;
  422. if(battle_config.official_cell_stack_limit && map->count_oncell(bl->m, x, y, BL_CHAR|BL_NPC, 1) > battle_config.official_cell_stack_limit) {
  423. //Walked on occupied cell, call unit_walktoxy again
  424. if(ud->steptimer != INVALID_TIMER) {
  425. //Execute step timer on next step instead
  426. timer->delete(ud->steptimer, unit->step_timer);
  427. ud->steptimer = INVALID_TIMER;
  428. }
  429. return unit->walktoxy(bl, x, y, 8);
  430. }
  431. }
  432. return 0;
  433. }
  434. int unit_delay_walktoxy_timer(int tid, int64 tick, int id, intptr_t data) {
  435. struct block_list *bl = map->id2bl(id);
  436. if (!bl || bl->prev == NULL)
  437. return 0;
  438. unit->walktoxy(bl, (short)((data>>16)&0xffff), (short)(data&0xffff), 0);
  439. return 1;
  440. }
  441. //flag parameter:
  442. //&1 -> 1/0 = easy/hard
  443. //&2 -> force walking
  444. //&4 -> Delay walking if the reason you can't walk is the canwalk delay
  445. //&8 -> Search for an unoccupied cell and cancel if none available
  446. int unit_walktoxy( struct block_list *bl, short x, short y, int flag)
  447. {
  448. struct unit_data* ud = NULL;
  449. struct status_change* sc = NULL;
  450. struct walkpath_data wpd;
  451. nullpo_ret(bl);
  452. ud = unit->bl2ud(bl);
  453. if( ud == NULL) return 0;
  454. if (battle_config.check_occupied_cells && (flag&8) && !map->closest_freecell(bl->m, bl, &x, &y, BL_CHAR|BL_NPC, 1)) //This might change x and y
  455. return 0;
  456. if (!path->search(&wpd, bl, bl->m, bl->x, bl->y, x, y, flag&1, CELL_CHKNOPASS)) // Count walk path cells
  457. return 0;
  458. #ifdef OFFICIAL_WALKPATH
  459. if( !path->search_long(NULL, bl, bl->m, bl->x, bl->y, x, y, CELL_CHKNOPASS) // Check if there is an obstacle between
  460. && (wpd.path_len > (battle_config.max_walk_path/17)*14) // Official number of walkable cells is 14 if and only if there is an obstacle between. [malufett]
  461. && (bl->type != BL_NPC) ) // If type is a NPC, please disregard.
  462. return 0;
  463. #endif
  464. if ((wpd.path_len > battle_config.max_walk_path) && (bl->type != BL_NPC))
  465. return 0;
  466. if (flag&4 && DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0 &&
  467. DIFF_TICK(ud->canmove_tick, timer->gettick()) < 2000) {
  468. // Delay walking command. [Skotlex]
  469. timer->add(ud->canmove_tick+1, unit->delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF));
  470. return 1;
  471. }
  472. if(!(flag&2) && (!(status_get_mode(bl)&MD_CANMOVE) || !unit->can_move(bl)))
  473. return 0;
  474. ud->state.walk_easy = flag&1;
  475. ud->to_x = x;
  476. ud->to_y = y;
  477. unit->stop_attack(bl); //Sets target to 0
  478. sc = status->get_sc(bl);
  479. if( sc ) {
  480. if( sc->data[SC_CONFUSION] || sc->data[SC__CHAOS] ) //Randomize the target position
  481. map->random_dir(bl, &ud->to_x, &ud->to_y);
  482. if( sc->data[SC_COMBOATTACK] )
  483. status_change_end(bl, SC_COMBOATTACK, INVALID_TIMER);
  484. }
  485. if(ud->walktimer != INVALID_TIMER) {
  486. // When you come to the center of the grid because the change of destination while you're walking right now
  487. // Call a function from a timer unit->walktoxy_sub
  488. ud->state.change_walk_target = 1;
  489. return 1;
  490. }
  491. return unit->walktoxy_sub(bl);
  492. }
  493. //To set Mob's CHASE/FOLLOW states (shouldn't be done if there's no path to reach)
  494. static inline void set_mobstate(struct block_list* bl, int flag)
  495. {
  496. struct mob_data* md = BL_CAST(BL_MOB,bl);
  497. if( md && flag )
  498. md->state.skillstate = md->state.aggressive ? MSS_FOLLOW : MSS_RUSH;
  499. }
  500. int unit_walktobl_sub(int tid, int64 tick, int id, intptr_t data) {
  501. struct block_list *bl = map->id2bl(id);
  502. struct unit_data *ud = bl?unit->bl2ud(bl):NULL;
  503. if (ud && ud->walktimer == INVALID_TIMER && ud->target == data) {
  504. if (DIFF_TICK(ud->canmove_tick, tick) > 0) //Keep waiting?
  505. timer->add(ud->canmove_tick+1, unit->walktobl_sub, id, data);
  506. else if (unit->can_move(bl)) {
  507. if (unit->walktoxy_sub(bl))
  508. set_mobstate(bl, ud->state.attack_continue);
  509. }
  510. }
  511. return 0;
  512. }
  513. // Chases a tbl. If the flag&1, use hard-path seek,
  514. // if flag&2, start attacking upon arrival within range, otherwise just walk to that character.
  515. int unit_walktobl(struct block_list *bl, struct block_list *tbl, int range, int flag)
  516. {
  517. struct unit_data *ud = NULL;
  518. struct status_change *sc = NULL;
  519. nullpo_ret(bl);
  520. nullpo_ret(tbl);
  521. ud = unit->bl2ud(bl);
  522. if( ud == NULL) return 0;
  523. if (!(status_get_mode(bl)&MD_CANMOVE))
  524. return 0;
  525. if (!unit->can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, flag&1, &ud->to_x, &ud->to_y)) {
  526. ud->to_x = bl->x;
  527. ud->to_y = bl->y;
  528. ud->target_to = 0;
  529. return 0;
  530. } else if (range == 0) {
  531. //Should walk on the same cell as target (for looters)
  532. ud->to_x = tbl->x;
  533. ud->to_y = tbl->y;
  534. }
  535. ud->state.walk_easy = flag&1;
  536. ud->target_to = tbl->id;
  537. ud->chaserange = range; //Note that if flag&2, this SHOULD be attack-range
  538. ud->state.attack_continue = (flag&2) ? 1 : 0; //Chase to attack.
  539. unit->stop_attack(bl); //Sets target to 0
  540. sc = status->get_sc(bl);
  541. if (sc && (sc->data[SC_CONFUSION] || sc->data[SC__CHAOS])) //Randomize the target position
  542. map->random_dir(bl, &ud->to_x, &ud->to_y);
  543. if(ud->walktimer != INVALID_TIMER) {
  544. ud->state.change_walk_target = 1;
  545. set_mobstate(bl, flag&2);
  546. return 1;
  547. }
  548. if (DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0) {
  549. //Can't move, wait a bit before invoking the movement.
  550. timer->add(ud->canmove_tick+1, unit->walktobl_sub, bl->id, ud->target);
  551. return 1;
  552. }
  553. if(!unit->can_move(bl))
  554. return 0;
  555. if (unit->walktoxy_sub(bl)) {
  556. set_mobstate(bl, flag&2);
  557. return 1;
  558. }
  559. return 0;
  560. }
  561. /**
  562. * Called by unit_run when an object was hit
  563. * @param sd Required only when using SC_WUGDASH
  564. **/
  565. void unit_run_hit( struct block_list *bl, struct status_change *sc, struct map_session_data *sd, enum sc_type type ) {
  566. int lv = sc->data[type]->val1;
  567. //If you can't run forward, you must be next to a wall, so bounce back. [Skotlex]
  568. if( type == SC_RUN )
  569. clif->sc_load(bl,bl->id,AREA,SI_TING,0,0,0);
  570. //Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
  571. unit->bl2ud(bl)->state.running = 0;
  572. status_change_end(bl, type, INVALID_TIMER);
  573. if( type == SC_RUN ) {
  574. skill->blown(bl,bl,skill->get_blewcount(TK_RUN,lv),unit->getdir(bl),0);
  575. clif->fixpos(bl); //Why is a clif->slide (skill->blown) AND a fixpos needed? Ask Aegis.
  576. clif->sc_end(bl,bl->id,AREA,SI_TING);
  577. } else if( sd ) {
  578. clif->fixpos(bl);
  579. skill->castend_damage_id(bl, &sd->bl, RA_WUGDASH, lv, timer->gettick(), SD_LEVEL);
  580. }
  581. return;
  582. }
  583. /**
  584. * Makes character run, used for SC_RUN and SC_WUGDASH
  585. * @param sd Required only when using SC_WUGDASH
  586. * @retval true Finished running
  587. * @retval false Hit an object/Couldn't run
  588. **/
  589. bool unit_run( struct block_list *bl, struct map_session_data *sd, enum sc_type type ) {
  590. struct status_change *sc;
  591. short to_x,to_y,dir_x,dir_y;
  592. int i;
  593. nullpo_retr(false, bl);
  594. sc = status->get_sc(bl);
  595. if( !(sc && sc->data[type]) )
  596. return false;
  597. if( !unit->can_move(bl) ) {
  598. status_change_end(bl, type, INVALID_TIMER);
  599. return false;
  600. }
  601. dir_x = dirx[sc->data[type]->val2];
  602. dir_y = diry[sc->data[type]->val2];
  603. // determine destination cell
  604. to_x = bl->x;
  605. to_y = bl->y;
  606. // Search for available path
  607. for(i = 0; i < AREA_SIZE; i++) {
  608. if (!map->getcell(bl->m, bl, to_x + dir_x, to_y + dir_y, CELL_CHKPASS))
  609. break;
  610. //if sprinting and there's a PC/Mob/NPC, block the path [Kevin]
  611. if ( map->count_oncell(bl->m, to_x + dir_x, to_y + dir_y, BL_PC | BL_MOB | BL_NPC, 0x2) )
  612. break;
  613. to_x += dir_x;
  614. to_y += dir_y;
  615. }
  616. // Can't run forward
  617. if( (to_x == bl->x && to_y == bl->y ) || (to_x == (bl->x+1) || to_y == (bl->y+1)) || (to_x == (bl->x-1) || to_y == (bl->y-1))) {
  618. unit->run_hit(bl, sc, sd, type);
  619. return false;
  620. }
  621. if( unit->walktoxy(bl, to_x, to_y, 1) )
  622. return true;
  623. //There must be an obstacle nearby. Attempt walking one cell at a time.
  624. do {
  625. to_x -= dir_x;
  626. to_y -= dir_y;
  627. } while (--i > 0 && !unit->walktoxy(bl, to_x, to_y, 1));
  628. if ( i == 0 ) {
  629. unit->run_hit(bl, sc, sd, type);
  630. return false;
  631. }
  632. return 1;
  633. }
  634. //Makes bl attempt to run dist cells away from target. Uses hard-paths.
  635. int unit_escape(struct block_list *bl, struct block_list *target, short dist) {
  636. uint8 dir = map->calc_dir(target, bl->x, bl->y);
  637. while (dist > 0 && map->getcell(bl->m, bl, bl->x + dist * dirx[dir], bl->y + dist * diry[dir], CELL_CHKNOREACH))
  638. dist--;
  639. return ( dist > 0 && unit->walktoxy(bl, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], 0) );
  640. }
  641. //Instant warp function.
  642. int unit_movepos(struct block_list *bl, short dst_x, short dst_y, int easy, bool checkpath) {
  643. short dx,dy;
  644. uint8 dir;
  645. struct unit_data *ud = NULL;
  646. struct map_session_data *sd = NULL;
  647. nullpo_ret(bl);
  648. sd = BL_CAST(BL_PC, bl);
  649. ud = unit->bl2ud(bl);
  650. if( ud == NULL) return 0;
  651. unit->stop_walking(bl, STOPWALKING_FLAG_FIXPOS);
  652. unit->stop_attack(bl);
  653. if (checkpath && (map->getcell(bl->m, bl, dst_x, dst_y, CELL_CHKNOPASS) || !path->search(NULL, bl, bl->m, bl->x, bl->y, dst_x, dst_y, easy, CELL_CHKNOREACH)) )
  654. return 0; // unreachable
  655. ud->to_x = dst_x;
  656. ud->to_y = dst_y;
  657. dir = map->calc_dir(bl, dst_x, dst_y);
  658. ud->dir = dir;
  659. dx = dst_x - bl->x;
  660. dy = dst_y - bl->y;
  661. map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
  662. map->moveblock(bl, dst_x, dst_y, timer->gettick());
  663. ud->walktimer = -2; // arbitrary non-INVALID_TIMER value to make the clif code send walking packets
  664. map->foreachinmovearea(clif->insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
  665. ud->walktimer = INVALID_TIMER;
  666. if(sd) {
  667. if( sd->touching_id )
  668. npc->touchnext_areanpc(sd,false);
  669. if (map->getcell(bl->m, bl, bl->x, bl->y, CELL_CHKNPC)) {
  670. npc->touch_areanpc(sd,bl->m,bl->x,bl->y);
  671. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  672. return 0;
  673. } else
  674. npc->untouch_areanpc(sd, bl->m, bl->x, bl->y);
  675. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  676. { // Check if pet needs to be teleported. [Skotlex]
  677. int flag = 0;
  678. struct block_list* pbl = &sd->pd->bl;
  679. if( !checkpath && !path->search(NULL,pbl,pbl->m,pbl->x,pbl->y,dst_x,dst_y,0,CELL_CHKNOPASS) )
  680. flag = 1;
  681. else if (!check_distance_bl(&sd->bl, pbl, AREA_SIZE)) //Too far, teleport.
  682. flag = 2;
  683. if( flag )
  684. {
  685. unit->movepos(pbl,sd->bl.x,sd->bl.y, 0, 0);
  686. clif->slide(pbl,pbl->x,pbl->y);
  687. }
  688. }
  689. }
  690. return 1;
  691. }
  692. int unit_setdir(struct block_list *bl,unsigned char dir)
  693. {
  694. struct unit_data *ud;
  695. nullpo_ret(bl );
  696. ud = unit->bl2ud(bl);
  697. if (!ud) return 0;
  698. ud->dir = dir;
  699. if (bl->type == BL_PC)
  700. BL_UCAST(BL_PC, bl)->head_dir = 0;
  701. clif->changed_dir(bl, AREA);
  702. return 0;
  703. }
  704. uint8 unit_getdir(struct block_list *bl) {
  705. struct unit_data *ud;
  706. nullpo_ret(bl);
  707. if( bl->type == BL_NPC )
  708. return BL_UCCAST(BL_NPC, bl)->dir;
  709. ud = unit->bl2ud(bl);
  710. if (!ud) return 0;
  711. return ud->dir;
  712. }
  713. // Pushes a unit by given amount of cells into given direction. Only
  714. // map cell restrictions are respected.
  715. // flag:
  716. // &1 Do not send position update packets.
  717. int unit_blown(struct block_list* bl, int dx, int dy, int count, int flag)
  718. {
  719. if(count) {
  720. struct map_session_data* sd;
  721. struct skill_unit* su = NULL;
  722. int nx, ny, result;
  723. nullpo_ret(bl);
  724. sd = BL_CAST(BL_PC, bl);
  725. su = BL_CAST(BL_SKILL, bl);
  726. result = path->blownpos(bl, bl->m, bl->x, bl->y, dx, dy, count);
  727. nx = result>>16;
  728. ny = result&0xffff;
  729. if(!su) {
  730. unit->stop_walking(bl, STOPWALKING_FLAG_NONE);
  731. }
  732. if( sd ) {
  733. unit->stop_stepaction(bl); //Stop stepaction when knocked back
  734. sd->ud.to_x = nx;
  735. sd->ud.to_y = ny;
  736. }
  737. dx = nx-bl->x;
  738. dy = ny-bl->y;
  739. if(dx || dy) {
  740. map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, bl->type == BL_PC ? BL_ALL : BL_PC, bl);
  741. if(su) {
  742. skill->unit_move_unit_group(su->group, bl->m, dx, dy);
  743. } else {
  744. map->moveblock(bl, nx, ny, timer->gettick());
  745. }
  746. map->foreachinmovearea(clif->insight, bl, AREA_SIZE, -dx, -dy, bl->type == BL_PC ? BL_ALL : BL_PC, bl);
  747. if(!(flag&1)) {
  748. clif->blown(bl);
  749. }
  750. if(sd) {
  751. if(sd->touching_id) {
  752. npc->touchnext_areanpc(sd, false);
  753. }
  754. if (map->getcell(bl->m, bl, bl->x, bl->y, CELL_CHKNPC)) {
  755. npc->touch_areanpc(sd, bl->m, bl->x, bl->y);
  756. } else {
  757. npc->untouch_areanpc(sd, bl->m, bl->x, bl->y);;
  758. }
  759. }
  760. }
  761. count = path->distance(dx, dy);
  762. }
  763. return count; // return amount of knocked back cells
  764. }
  765. //Warps a unit/ud to a given map/position.
  766. //In the case of players, pc->setpos is used.
  767. //it respects the no warp flags, so it is safe to call this without doing nowarpto/nowarp checks.
  768. int unit_warp(struct block_list *bl,short m,short x,short y,clr_type type)
  769. {
  770. struct unit_data *ud;
  771. nullpo_ret(bl);
  772. ud = unit->bl2ud(bl);
  773. if(bl->prev==NULL || !ud)
  774. return 1;
  775. if (type == CLR_DEAD)
  776. //Type 1 is invalid, since you shouldn't warp a bl with the "death"
  777. //animation, it messes up with unit_remove_map! [Skotlex]
  778. return 1;
  779. if( m<0 ) m=bl->m;
  780. switch (bl->type) {
  781. case BL_MOB:
  782. {
  783. const struct mob_data *md = BL_UCCAST(BL_MOB, bl);
  784. if (map->list[bl->m].flag.monster_noteleport && md->master_id == 0)
  785. return 1;
  786. if (m != bl->m && map->list[m].flag.nobranch && battle_config.mob_warp&4 && md->master_id == 0)
  787. return 1;
  788. }
  789. break;
  790. case BL_PC:
  791. if (map->list[bl->m].flag.noteleport)
  792. return 1;
  793. break;
  794. }
  795. if (x<0 || y<0) {
  796. //Random map position.
  797. if (!map->search_freecell(NULL, m, &x, &y, -1, -1, 1)) {
  798. ShowWarning("unit_warp failed. Unit Id:%d/Type:%d, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map->list[m].name, x, y);
  799. return 2;
  800. }
  801. } else if (map->getcell(m, bl, x, y, CELL_CHKNOREACH)) {
  802. //Invalid target cell
  803. ShowWarning("unit_warp: Specified non-walkable target cell: %d (%s) at [%d,%d]\n", m, map->list[m].name, x,y);
  804. if (!map->search_freecell(NULL, m, &x, &y, 4, 4, 1)) {
  805. //Can't find a nearby cell
  806. ShowWarning("unit_warp failed. Unit Id:%d/Type:%d, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map->list[m].name, x, y);
  807. return 2;
  808. }
  809. }
  810. if (bl->type == BL_PC) //Use pc_setpos
  811. return pc->setpos(BL_UCAST(BL_PC, bl), map_id2index(m), x, y, type);
  812. if (!unit->remove_map(bl, type, ALC_MARK))
  813. return 3;
  814. if (bl->m != m && battle_config.clear_unit_onwarp &&
  815. battle_config.clear_unit_onwarp&bl->type)
  816. skill->clear_unitgroup(bl);
  817. bl->x=ud->to_x=x;
  818. bl->y=ud->to_y=y;
  819. bl->m=m;
  820. map->addblock(bl);
  821. clif->spawn(bl);
  822. skill->unit_move(bl,timer->gettick(),1);
  823. return 0;
  824. }
  825. /*==========================================
  826. * Caused the target object to stop moving.
  827. * Flag values: @see unit_stopwalking_flag.
  828. * Upper bytes may be used for other purposes depending on the unit type.
  829. *------------------------------------------*/
  830. int unit_stop_walking(struct block_list *bl, int flag) {
  831. struct unit_data *ud;
  832. const struct TimerData* td;
  833. int64 tick;
  834. nullpo_ret(bl);
  835. ud = unit->bl2ud(bl);
  836. if(!ud || ud->walktimer == INVALID_TIMER)
  837. return 0;
  838. //NOTE: We are using timer data after deleting it because we know the
  839. //timer->delete function does not messes with it. If the function's
  840. //behavior changes in the future, this code could break!
  841. td = timer->get(ud->walktimer);
  842. timer->delete(ud->walktimer, unit->walktoxy_timer);
  843. ud->walktimer = INVALID_TIMER;
  844. ud->state.change_walk_target = 0;
  845. tick = timer->gettick();
  846. if( (flag&STOPWALKING_FLAG_ONESTEP && !ud->walkpath.path_pos) //Force moving at least one cell.
  847. || (flag&STOPWALKING_FLAG_NEXTCELL && td && DIFF_TICK(td->tick, tick) <= td->data/2) //Enough time has passed to cover half-cell
  848. ) {
  849. ud->walkpath.path_len = ud->walkpath.path_pos+1;
  850. unit->walktoxy_timer(INVALID_TIMER, tick, bl->id, ud->walkpath.path_pos);
  851. }
  852. if(flag&STOPWALKING_FLAG_FIXPOS)
  853. clif->fixpos(bl);
  854. ud->walkpath.path_len = 0;
  855. ud->walkpath.path_pos = 0;
  856. ud->to_x = bl->x;
  857. ud->to_y = bl->y;
  858. if(bl->type == BL_PET && flag&~STOPWALKING_FLAG_MASK)
  859. ud->canmove_tick = timer->gettick() + (flag>>8);
  860. //Read, the check in unit_set_walkdelay means dmg during running won't fall through to this place in code [Kevin]
  861. if (ud->state.running) {
  862. status_change_end(bl, SC_RUN, INVALID_TIMER);
  863. status_change_end(bl, SC_WUGDASH, INVALID_TIMER);
  864. }
  865. return 1;
  866. }
  867. int unit_skilluse_id(struct block_list *src, int target_id, uint16 skill_id, uint16 skill_lv)
  868. {
  869. return unit->skilluse_id2(
  870. src, target_id, skill_id, skill_lv,
  871. skill->cast_fix(src, skill_id, skill_lv),
  872. skill->get_castcancel(skill_id)
  873. );
  874. }
  875. int unit_is_walking(struct block_list *bl)
  876. {
  877. struct unit_data *ud = unit->bl2ud(bl);
  878. nullpo_ret(bl);
  879. if(!ud) return 0;
  880. return (ud->walktimer != INVALID_TIMER);
  881. }
  882. /*==========================================
  883. * Determines if the bl can move based on status changes. [Skotlex]
  884. *------------------------------------------*/
  885. int unit_can_move(struct block_list *bl) {
  886. struct map_session_data *sd;
  887. struct unit_data *ud;
  888. struct status_change *sc;
  889. nullpo_ret(bl);
  890. ud = unit->bl2ud(bl);
  891. sc = status->get_sc(bl);
  892. sd = BL_CAST(BL_PC, bl);
  893. if (!ud)
  894. return 0;
  895. if (ud->skilltimer != INVALID_TIMER && ud->skill_id != LG_EXEEDBREAK && (!sd || !pc->checkskill(sd, SA_FREECAST) || skill->get_inf2(ud->skill_id)&INF2_GUILD_SKILL))
  896. return 0; // prevent moving while casting
  897. if (DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0)
  898. return 0;
  899. if (sd && (
  900. pc_issit(sd) ||
  901. sd->state.vending ||
  902. sd->state.buyingstore ||
  903. sd->state.blockedmove
  904. ))
  905. return 0; //Can't move
  906. // Status changes that block movement
  907. if (sc) {
  908. if( sc->count
  909. && (
  910. sc->data[SC_ANKLESNARE]
  911. || sc->data[SC_AUTOCOUNTER]
  912. || sc->data[SC_TRICKDEAD]
  913. || sc->data[SC_BLADESTOP]
  914. || sc->data[SC_BLADESTOP_WAIT]
  915. || (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF) // cannot move while gospel is in effect
  916. || (sc->data[SC_BASILICA] && sc->data[SC_BASILICA]->val4 == bl->id) // Basilica caster cannot move
  917. || sc->data[SC_STOP]
  918. || sc->data[SC_FALLENEMPIRE]
  919. || sc->data[SC_RG_CCONFINE_M]
  920. || sc->data[SC_RG_CCONFINE_S]
  921. || sc->data[SC_GS_MADNESSCANCEL]
  922. || (sc->data[SC_GRAVITATION] && sc->data[SC_GRAVITATION]->val3 == BCT_SELF)
  923. || sc->data[SC_WHITEIMPRISON]
  924. || sc->data[SC_ELECTRICSHOCKER]
  925. || sc->data[SC_WUGBITE]
  926. || sc->data[SC_THORNS_TRAP]
  927. || ( sc->data[SC_MAGNETICFIELD] && !sc->data[SC_HOVERING] )
  928. || sc->data[SC__MANHOLE]
  929. || sc->data[SC_CURSEDCIRCLE_ATKER]
  930. || sc->data[SC_CURSEDCIRCLE_TARGET]
  931. || (sc->data[SC_COLD] && bl->type != BL_MOB)
  932. || sc->data[SC_DEEP_SLEEP]
  933. || (sc->data[SC_CAMOUFLAGE] && sc->data[SC_CAMOUFLAGE]->val1 < 3 && !(sc->data[SC_CAMOUFLAGE]->val3&1))
  934. || sc->data[SC_MEIKYOUSISUI]
  935. || sc->data[SC_KG_KAGEHUMI]
  936. || sc->data[SC_NEEDLE_OF_PARALYZE]
  937. || sc->data[SC_VACUUM_EXTREME]
  938. || (sc->data[SC_FEAR] && sc->data[SC_FEAR]->val2 > 0)
  939. || sc->data[SC_NETHERWORLD]
  940. || (sc->data[SC_SPIDERWEB] && sc->data[SC_SPIDERWEB]->val1)
  941. || (sc->data[SC_CLOAKING] && sc->data[SC_CLOAKING]->val1 < 3 && !(sc->data[SC_CLOAKING]->val4&1)) //Need wall at level 1-2
  942. || (
  943. sc->data[SC_DANCING] && sc->data[SC_DANCING]->val4
  944. && (
  945. !sc->data[SC_LONGING]
  946. || (sc->data[SC_DANCING]->val1&0xFFFF) == CG_MOONLIT
  947. || (sc->data[SC_DANCING]->val1&0xFFFF) == CG_HERMODE
  948. )
  949. )
  950. )
  951. )
  952. return 0;
  953. if (sc->opt1 > 0 && sc->opt1 != OPT1_STONEWAIT && sc->opt1 != OPT1_BURNING && !(sc->opt1 == OPT1_CRYSTALIZE && bl->type == BL_MOB))
  954. return 0;
  955. if ((sc->option & OPTION_HIDE) && (!sd || pc->checkskill(sd, RG_TUNNELDRIVE) <= 0))
  956. return 0;
  957. }
  958. // Icewall walk block special trapped monster mode
  959. if(bl->type == BL_MOB) {
  960. struct mob_data *md = BL_CAST(BL_MOB, bl);
  961. if (md && ((md->status.mode&MD_BOSS && battle_config.boss_icewall_walk_block == 1 && map->getcell(bl->m, bl, bl->x, bl->y, CELL_CHKICEWALL))
  962. || (!(md->status.mode&MD_BOSS) && battle_config.mob_icewall_walk_block == 1 && map->getcell(bl->m, bl, bl->x, bl->y, CELL_CHKICEWALL)))) {
  963. md->walktoxy_fail_count = 1; //Make sure rudeattacked skills are invoked
  964. return 0;
  965. }
  966. }
  967. return 1;
  968. }
  969. /*==========================================
  970. * Resume running after a walk delay
  971. *------------------------------------------*/
  972. int unit_resume_running(int tid, int64 tick, int id, intptr_t data) {
  973. struct unit_data *ud = (struct unit_data *)data;
  974. struct map_session_data *sd = map->id2sd(id);
  975. if(sd && pc_isridingwug(sd))
  976. clif->skill_nodamage(ud->bl,ud->bl,RA_WUGDASH,ud->skill_lv,
  977. sc_start4(ud->bl,ud->bl,status->skill2sc(RA_WUGDASH),100,ud->skill_lv,unit->getdir(ud->bl),0,0,1));
  978. else
  979. clif->skill_nodamage(ud->bl,ud->bl,TK_RUN,ud->skill_lv,
  980. sc_start4(ud->bl,ud->bl,status->skill2sc(TK_RUN),100,ud->skill_lv,unit->getdir(ud->bl),0,0,0));
  981. if (sd) clif->walkok(sd);
  982. return 0;
  983. }
  984. /*==========================================
  985. * Applies walk delay to character, considering that
  986. * if type is 0, this is a damage induced delay: if previous delay is active, do not change it.
  987. * if type is 1, this is a skill induced delay: walk-delay may only be increased, not decreased.
  988. *------------------------------------------*/
  989. int unit_set_walkdelay(struct block_list *bl, int64 tick, int delay, int type) {
  990. struct unit_data *ud = unit->bl2ud(bl);
  991. if (delay <= 0 || !ud) return 0;
  992. if (type) {
  993. //Bosses can ignore skill induced walkdelay (but not damage induced)
  994. if (bl->type == BL_MOB && (BL_UCCAST(BL_MOB, bl)->status.mode&MD_BOSS))
  995. return 0;
  996. //Make sure walk delay is not decreased
  997. if (DIFF_TICK(ud->canmove_tick, tick+delay) > 0)
  998. return 0;
  999. } else {
  1000. //Don't set walk delays when already trapped.
  1001. if (!unit->can_move(bl))
  1002. return 0;
  1003. //Immune to being stopped for double the flinch time
  1004. if (DIFF_TICK(ud->canmove_tick, tick-delay) > 0)
  1005. return 0;
  1006. }
  1007. ud->canmove_tick = tick + delay;
  1008. if (ud->walktimer != INVALID_TIMER) {
  1009. //Stop walking, if chasing, readjust timers.
  1010. if (delay == 1) {
  1011. //Minimal delay (walk-delay) disabled. Just stop walking.
  1012. unit->stop_walking(bl, STOPWALKING_FLAG_NEXTCELL);
  1013. } else {
  1014. //Resume running after can move again [Kevin]
  1015. if (ud->state.running) {
  1016. timer->add(ud->canmove_tick, unit->resume_running, bl->id, (intptr_t)ud);
  1017. } else {
  1018. unit->stop_walking(bl, STOPWALKING_FLAG_NEXTCELL);
  1019. if (ud->target)
  1020. timer->add(ud->canmove_tick+1, unit->walktobl_sub, bl->id, ud->target);
  1021. }
  1022. }
  1023. }
  1024. return 1;
  1025. }
  1026. int unit_skilluse_id2(struct block_list *src, int target_id, uint16 skill_id, uint16 skill_lv, int casttime, int castcancel) {
  1027. struct unit_data *ud;
  1028. struct status_data *tstatus;
  1029. struct status_change *sc;
  1030. struct map_session_data *sd = NULL;
  1031. struct block_list * target = NULL;
  1032. int64 tick = timer->gettick();
  1033. int temp = 0, range;
  1034. nullpo_ret(src);
  1035. if(status->isdead(src))
  1036. return 0; //Do not continue source is dead
  1037. sd = BL_CAST(BL_PC, src);
  1038. ud = unit->bl2ud(src);
  1039. if(ud == NULL) return 0;
  1040. sc = status->get_sc(src);
  1041. if (sc && !sc->count)
  1042. sc = NULL; //Unneeded
  1043. //temp: used to signal combo-skills right now.
  1044. if (sc && sc->data[SC_COMBOATTACK]
  1045. && skill->is_combo(skill_id)
  1046. && (sc->data[SC_COMBOATTACK]->val1 == skill_id
  1047. || ( sd?skill->check_condition_castbegin(sd,skill_id,skill_lv):0 )
  1048. )
  1049. ) {
  1050. if (sc->data[SC_COMBOATTACK]->val2)
  1051. target_id = sc->data[SC_COMBOATTACK]->val2;
  1052. else if( skill->get_inf(skill_id) != 1 ) // Only non-targetable skills should use auto target
  1053. target_id = ud->target;
  1054. if( skill->get_inf(skill_id)&INF_SELF_SKILL && skill->get_nk(skill_id)&NK_NO_DAMAGE )// exploit fix
  1055. target_id = src->id;
  1056. temp = 1;
  1057. } else if ( target_id == src->id &&
  1058. skill->get_inf(skill_id)&INF_SELF_SKILL &&
  1059. skill->get_inf2(skill_id)&INF2_NO_TARGET_SELF )
  1060. {
  1061. target_id = ud->target; //Auto-select target. [Skotlex]
  1062. temp = 1;
  1063. }
  1064. if (sd) {
  1065. //Target_id checking.
  1066. if(skill->not_ok(skill_id, sd)) // [MouseJstr]
  1067. return 0;
  1068. switch (skill_id) {
  1069. //Check for skills that auto-select target
  1070. case MO_CHAINCOMBO:
  1071. if (sc && sc->data[SC_BLADESTOP]) {
  1072. if ((target=map->id2bl(sc->data[SC_BLADESTOP]->val4)) == NULL)
  1073. return 0;
  1074. }
  1075. break;
  1076. case WE_MALE:
  1077. case WE_FEMALE:
  1078. {
  1079. struct map_session_data *p_sd = NULL;
  1080. if (!sd->status.partner_id)
  1081. return 0;
  1082. p_sd = map->charid2sd(sd->status.partner_id);
  1083. if (p_sd == NULL) {
  1084. clif->skill_fail(sd,skill_id,USESKILL_FAIL_LEVEL,0);
  1085. return 0;
  1086. }
  1087. target = &p_sd->bl;
  1088. }
  1089. break;
  1090. case GC_WEAPONCRUSH:
  1091. if( sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == GC_WEAPONBLOCKING ) {
  1092. if( (target=map->id2bl(sc->data[SC_COMBOATTACK]->val2)) == NULL ) {
  1093. clif->skill_fail(sd,skill_id,USESKILL_FAIL_GC_WEAPONBLOCKING,0);
  1094. return 0;
  1095. }
  1096. } else {
  1097. clif->skill_fail(sd,skill_id,USESKILL_FAIL_GC_WEAPONBLOCKING,0);
  1098. return 0;
  1099. }
  1100. break;
  1101. }
  1102. if (target)
  1103. target_id = target->id;
  1104. }
  1105. if (src->type==BL_HOM)
  1106. switch(skill_id) { //Homun-auto-target skills.
  1107. case HLIF_HEAL:
  1108. case HLIF_AVOID:
  1109. case HAMI_DEFENCE:
  1110. case HAMI_CASTLE:
  1111. target = battle->get_master(src);
  1112. if (!target) return 0;
  1113. target_id = target->id;
  1114. }
  1115. if( !target ) // choose default target
  1116. target = map->id2bl(target_id);
  1117. if( !target || src->m != target->m || !src->prev || !target->prev )
  1118. return 0;
  1119. if( battle_config.ksprotection && sd && mob->ksprotected(src, target) )
  1120. return 0;
  1121. //Normally not needed because clif.c checks for it, but the at/char/script commands don't! [Skotlex]
  1122. if(ud->skilltimer != INVALID_TIMER && skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST)
  1123. return 0;
  1124. if(skill->get_inf2(skill_id)&INF2_NO_TARGET_SELF && src->id == target_id)
  1125. return 0;
  1126. if(!status->check_skilluse(src, target, skill_id, 0))
  1127. return 0;
  1128. if( src != target && status->isdead(target) ) {
  1129. /**
  1130. * Skills that may be cast on dead targets
  1131. **/
  1132. switch( skill_id ) {
  1133. case NPC_WIDESOULDRAIN:
  1134. case PR_REDEMPTIO:
  1135. case ALL_RESURRECTION:
  1136. case WM_DEADHILLHERE:
  1137. break;
  1138. default:
  1139. return 1;
  1140. }
  1141. }
  1142. tstatus = status->get_status_data(target);
  1143. // Record the status of the previous skill)
  1144. if (sd) {
  1145. if ((skill->get_inf2(skill_id)&INF2_ENSEMBLE_SKILL) && skill->check_pc_partner(sd, skill_id, &skill_lv, 1, 0) < 1) {
  1146. clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0);
  1147. return 0;
  1148. }
  1149. switch (skill_id){
  1150. case SA_CASTCANCEL:
  1151. if (ud->skill_id != skill_id){
  1152. sd->skill_id_old = ud->skill_id;
  1153. sd->skill_lv_old = ud->skill_lv;
  1154. }
  1155. break;
  1156. case BD_ENCORE:
  1157. //Prevent using the dance skill if you no longer have the skill in your tree.
  1158. if (!sd->skill_id_dance || pc->checkskill(sd, sd->skill_id_dance) <= 0){
  1159. clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0);
  1160. return 0;
  1161. }
  1162. sd->skill_id_old = skill_id;
  1163. break;
  1164. case WL_WHITEIMPRISON:
  1165. if (battle->check_target(src, target, BCT_SELF | BCT_ENEMY) < 0) {
  1166. clif->skill_fail(sd, skill_id, USESKILL_FAIL_TOTARGET, 0);
  1167. return 0;
  1168. }
  1169. break;
  1170. case MG_FIREBOLT:
  1171. case MG_LIGHTNINGBOLT:
  1172. case MG_COLDBOLT:
  1173. sd->skill_id_old = skill_id;
  1174. sd->skill_lv_old = skill_lv;
  1175. break;
  1176. }
  1177. }
  1178. if (src->type == BL_HOM) {
  1179. // In case of homunuculus, set the sd to the homunculus' master, as needed below
  1180. struct block_list *master = battle->get_master(src);
  1181. if (master)
  1182. sd = map->id2sd(master->id);
  1183. }
  1184. if (sd) {
  1185. /* temporarily disabled, awaiting for kenpachi to detail this so we can make it work properly */
  1186. #if 0
  1187. if (sd->skillitem != skill_id && !skill->check_condition_castbegin(sd, skill_id, skill_lv))
  1188. #else
  1189. if (!skill->check_condition_castbegin(sd, skill_id, skill_lv))
  1190. #endif
  1191. return 0;
  1192. }
  1193. if (src->type == BL_MOB) {
  1194. const struct mob_data *src_md = BL_UCCAST(BL_MOB, src);
  1195. switch (skill_id) {
  1196. case NPC_SUMMONSLAVE:
  1197. case NPC_SUMMONMONSTER:
  1198. case AL_TELEPORT:
  1199. if (src_md->master_id != 0 && src_md->special_state.ai != AI_NONE)
  1200. return 0;
  1201. }
  1202. }
  1203. if (src->type == BL_NPC) // NPC-objects can override cast distance
  1204. range = AREA_SIZE; // Maximum visible distance before NPC goes out of sight
  1205. else
  1206. range = skill->get_range2(src, skill_id, skill_lv); // Skill cast distance from database
  1207. // New action request received, delete previous action request if not executed yet
  1208. if(ud->stepaction || ud->steptimer != INVALID_TIMER)
  1209. unit->stop_stepaction(src);
  1210. // Remember the skill request from the client while walking to the next cell
  1211. if(src->type == BL_PC && ud->walktimer != INVALID_TIMER && !battle->check_range(src, target, range-1)) {
  1212. ud->stepaction = true;
  1213. ud->target_to = target_id;
  1214. ud->stepskill_id = skill_id;
  1215. ud->stepskill_lv = skill_lv;
  1216. return 0; // Attacking will be handled by unit_walktoxy_timer in this case
  1217. }
  1218. //Check range when not using skill on yourself or is a combo-skill during attack
  1219. //(these are supposed to always have the same range as your attack)
  1220. if( src->id != target_id && (!temp || ud->attacktimer == INVALID_TIMER) ) {
  1221. if( skill->get_state(ud->skill_id) == ST_MOVE_ENABLE ) {
  1222. if( !unit->can_reach_bl(src, target, range + 1, 1, NULL, NULL) )
  1223. return 0; // Walk-path check failed.
  1224. } else if( src->type == BL_MER && skill_id == MA_REMOVETRAP ) {
  1225. if( !battle->check_range(battle->get_master(src), target, range + 1) )
  1226. return 0; // Aegis calc remove trap based on Master position, ignoring mercenary O.O
  1227. } else if( !battle->check_range(src, target, range + (skill_id == RG_CLOSECONFINE?0:2)) ) {
  1228. return 0; // Arrow-path check failed.
  1229. }
  1230. }
  1231. if (!temp) //Stop attack on non-combo skills [Skotlex]
  1232. unit->stop_attack(src);
  1233. else if(ud->attacktimer != INVALID_TIMER) //Else-wise, delay current attack sequence
  1234. ud->attackabletime = tick + status_get_adelay(src);
  1235. ud->state.skillcastcancel = castcancel;
  1236. //temp: Used to signal force cast now.
  1237. temp = 0;
  1238. switch(skill_id){
  1239. case ALL_RESURRECTION:
  1240. if(battle->check_undead(tstatus->race,tstatus->def_ele)) {
  1241. temp = 1;
  1242. } else if (!status->isdead(target))
  1243. return 0; //Can't cast on non-dead characters.
  1244. break;
  1245. case MO_FINGEROFFENSIVE:
  1246. if(sd)
  1247. casttime += casttime * min(skill_lv, sd->spiritball);
  1248. break;
  1249. case MO_EXTREMITYFIST:
  1250. if (sc && sc->data[SC_COMBOATTACK] &&
  1251. (sc->data[SC_COMBOATTACK]->val1 == MO_COMBOFINISH ||
  1252. sc->data[SC_COMBOATTACK]->val1 == CH_TIGERFIST ||
  1253. sc->data[SC_COMBOATTACK]->val1 == CH_CHAINCRUSH))
  1254. casttime = -1;
  1255. temp = 1;
  1256. break;
  1257. case CR_DEVOTION:
  1258. if (sd) {
  1259. int i = 0, count = min(skill_lv, 5);
  1260. ARR_FIND(0, count, i, sd->devotion[i] == target_id);
  1261. if (i == count) {
  1262. ARR_FIND(0, count, i, sd->devotion[i] == 0);
  1263. if(i == count) {
  1264. clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0);
  1265. return 0; // Can't cast on other characters when limit is reached
  1266. }
  1267. }
  1268. }
  1269. break;
  1270. case AB_CLEARANCE:
  1271. if( target->type != BL_MOB && battle->check_target(src,target,BCT_PARTY) <= 0 && sd ) {
  1272. clif->skill_fail(sd, skill_id, USESKILL_FAIL_TOTARGET, 0);
  1273. return 0;
  1274. }
  1275. break;
  1276. case SR_GATEOFHELL:
  1277. case SR_TIGERCANNON:
  1278. if (sc && sc->data[SC_COMBOATTACK] &&
  1279. sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE)
  1280. casttime = -1;
  1281. temp = 1;
  1282. break;
  1283. case SA_SPELLBREAKER:
  1284. temp = 1;
  1285. break;
  1286. case ST_CHASEWALK:
  1287. if (sc && sc->data[SC_CHASEWALK])
  1288. casttime = -1;
  1289. break;
  1290. case TK_RUN:
  1291. if (sc && sc->data[SC_RUN])
  1292. casttime = -1;
  1293. break;
  1294. case HP_BASILICA:
  1295. if( sc && sc->data[SC_BASILICA] )
  1296. casttime = -1; // No Casting time on basilica cancel
  1297. break;
  1298. case KN_CHARGEATK:
  1299. {
  1300. unsigned int k = (distance_bl(src,target)-1)/3; //+100% every 3 cells of distance
  1301. if( k > 2 ) k = 2; // ...but hard-limited to 300%.
  1302. casttime += casttime * k;
  1303. }
  1304. break;
  1305. case GD_EMERGENCYCALL: //Emergency Call double cast when the user has learned Leap [Daegaladh]
  1306. if( sd && pc->checkskill(sd,TK_HIGHJUMP) )
  1307. casttime *= 2;
  1308. break;
  1309. case RA_WUGDASH:
  1310. if (sc && sc->data[SC_WUGDASH])
  1311. casttime = -1;
  1312. break;
  1313. case EL_WIND_SLASH:
  1314. case EL_HURRICANE:
  1315. case EL_TYPOON_MIS:
  1316. case EL_STONE_HAMMER:
  1317. case EL_ROCK_CRUSHER:
  1318. case EL_STONE_RAIN:
  1319. case EL_ICE_NEEDLE:
  1320. case EL_WATER_SCREW:
  1321. case EL_TIDAL_WEAPON:
  1322. if( src->type == BL_ELEM ){
  1323. sd = BL_CAST(BL_PC, battle->get_master(src));
  1324. if( sd && sd->skill_id_old == SO_EL_ACTION ){
  1325. casttime = -1;
  1326. sd->skill_id_old = 0;
  1327. }
  1328. }
  1329. break;
  1330. case NC_DISJOINT:
  1331. if( target->type == BL_PC ){
  1332. struct mob_data *md;
  1333. if( (md = map->id2md(target->id)) && md->master_id != src->id )
  1334. casttime <<= 1;
  1335. }
  1336. break;
  1337. }
  1338. // moved here to prevent Suffragium from ending if skill fails
  1339. #ifndef RENEWAL_CAST
  1340. if (!(skill->get_castnodex(skill_id, skill_lv)&2))
  1341. casttime = skill->cast_fix_sc(src, casttime);
  1342. #else
  1343. casttime = skill->vf_cast_fix(src, casttime, skill_id, skill_lv);
  1344. #endif
  1345. if (src->type == BL_NPC) { // NPC-objects do not have cast time
  1346. casttime = 0;
  1347. }
  1348. if( sc ) {
  1349. /**
  1350. * why the if else chain: these 3 status do not stack, so its efficient that way.
  1351. **/
  1352. if( sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4) && skill_id != AS_CLOAKING ) {
  1353. status_change_end(src, SC_CLOAKING, INVALID_TIMER);
  1354. if (!src->prev) return 0; //Warped away!
  1355. } else if( sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4&4) && skill_id != GC_CLOAKINGEXCEED ) {
  1356. status_change_end(src,SC_CLOAKINGEXCEED, INVALID_TIMER);
  1357. if (!src->prev) return 0;
  1358. }
  1359. }
  1360. if (!ud->state.running) //need TK_RUN or WUGDASH handler to be done before that, see bugreport:6026
  1361. unit->stop_walking(src, STOPWALKING_FLAG_FIXPOS);// even though this is not how official works but this will do the trick. bugreport:6829
  1362. // in official this is triggered even if no cast time.
  1363. clif->skillcasting(src, src->id, target_id, 0,0, skill_id, skill->get_ele(skill_id, skill_lv), casttime);
  1364. if( casttime > 0 || temp )
  1365. {
  1366. if (sd != NULL && target->type == BL_MOB) {
  1367. struct mob_data *md = BL_UCAST(BL_MOB, target);
  1368. mob->skill_event(md, src, tick, -1); //Cast targeted skill event.
  1369. if (tstatus->mode&(MD_CASTSENSOR_IDLE|MD_CASTSENSOR_CHASE) &&
  1370. battle->check_target(target, src, BCT_ENEMY) > 0)
  1371. {
  1372. switch (md->state.skillstate) {
  1373. case MSS_RUSH:
  1374. case MSS_FOLLOW:
  1375. if (!(tstatus->mode&MD_CASTSENSOR_CHASE))
  1376. break;
  1377. md->target_id = src->id;
  1378. md->state.aggressive = (tstatus->mode&MD_ANGRY)?1:0;
  1379. md->min_chase = md->db->range3;
  1380. break;
  1381. case MSS_IDLE:
  1382. case MSS_WALK:
  1383. if (!(tstatus->mode&MD_CASTSENSOR_IDLE))
  1384. break;
  1385. md->target_id = src->id;
  1386. md->state.aggressive = (tstatus->mode&MD_ANGRY)?1:0;
  1387. md->min_chase = md->db->range3;
  1388. break;
  1389. }
  1390. }
  1391. }
  1392. }
  1393. if( casttime <= 0 )
  1394. ud->state.skillcastcancel = 0;
  1395. if( !sd || sd->skillitem != skill_id || skill->get_cast(skill_id,skill_lv) )
  1396. ud->canact_tick = tick + casttime + 100;
  1397. if( sd )
  1398. {
  1399. switch( skill_id )
  1400. {
  1401. case CG_ARROWVULCAN:
  1402. sd->canequip_tick = tick + casttime;
  1403. break;
  1404. }
  1405. }
  1406. ud->skilltarget = target_id;
  1407. ud->skillx = 0;
  1408. ud->skilly = 0;
  1409. ud->skill_id = skill_id;
  1410. ud->skill_lv = skill_lv;
  1411. if( casttime > 0 ) {
  1412. if (src->id != target->id) // self-targeted skills shouldn't show different direction
  1413. unit->setdir(src, map->calc_dir(src, target->x, target->y));
  1414. ud->skilltimer = timer->add( tick+casttime, skill->castend_id, src->id, 0 );
  1415. if( sd && (pc->checkskill(sd,SA_FREECAST) > 0 || skill_id == LG_EXEEDBREAK) )
  1416. status_calc_bl(&sd->bl, SCB_SPEED|SCB_ASPD);
  1417. } else
  1418. skill->castend_id(ud->skilltimer,tick,src->id,0);
  1419. return 1;
  1420. }
  1421. int unit_skilluse_pos(struct block_list *src, short skill_x, short skill_y, uint16 skill_id, uint16 skill_lv)
  1422. {
  1423. return unit->skilluse_pos2(
  1424. src, skill_x, skill_y, skill_id, skill_lv,
  1425. skill->cast_fix(src, skill_id, skill_lv),
  1426. skill->get_castcancel(skill_id)
  1427. );
  1428. }
  1429. int unit_skilluse_pos2( struct block_list *src, short skill_x, short skill_y, uint16 skill_id, uint16 skill_lv, int casttime, int castcancel)
  1430. {
  1431. struct map_session_data *sd = NULL;
  1432. struct unit_data *ud = NULL;
  1433. struct status_change *sc;
  1434. struct block_list bl;
  1435. int64 tick = timer->gettick();
  1436. int range;
  1437. nullpo_ret(src);
  1438. if (!src->prev) return 0; // not on the map
  1439. if(status->isdead(src)) return 0;
  1440. sd = BL_CAST(BL_PC, src);
  1441. ud = unit->bl2ud(src);
  1442. if(ud == NULL) return 0;
  1443. if(ud->skilltimer != INVALID_TIMER) //Normally not needed since clif.c checks for it, but at/char/script commands don't! [Skotlex]
  1444. return 0;
  1445. sc = status->get_sc(src);
  1446. if (sc && !sc->count)
  1447. sc = NULL;
  1448. if( sd )
  1449. {
  1450. if( skill->not_ok(skill_id, sd) || !skill->check_condition_castbegin(sd, skill_id, skill_lv) )
  1451. return 0;
  1452. /**
  1453. * "WHY IS IT HEREE": ice wall cannot be canceled past this point, the client displays the animation even,
  1454. * if we cancel it from castend_pos, so it has to be here for it to not display the animation.
  1455. **/
  1456. if (skill_id == WZ_ICEWALL && map->getcell(src->m, src, skill_x, skill_y, CELL_CHKNOICEWALL))
  1457. return 0;
  1458. }
  1459. if (!status->check_skilluse(src, NULL, skill_id, 0))
  1460. return 0;
  1461. if (map->getcell(src->m, src, skill_x, skill_y, CELL_CHKWALL)) {
  1462. // can't cast ground targeted spells on wall cells
  1463. if (sd) clif->skill_fail(sd,skill_id,USESKILL_FAIL_LEVEL,0);
  1464. return 0;
  1465. }
  1466. /* Check range and obstacle */
  1467. bl.type = BL_NUL;
  1468. bl.m = src->m;
  1469. bl.x = skill_x;
  1470. bl.y = skill_y;
  1471. if (src->type == BL_NPC) // NPC-objects can override cast distance
  1472. range = AREA_SIZE; // Maximum visible distance before NPC goes out of sight
  1473. else
  1474. range = skill->get_range2(src, skill_id, skill_lv); // Skill cast distance from database
  1475. // New action request received, delete previous action request if not executed yet
  1476. if(ud->stepaction || ud->steptimer != INVALID_TIMER)
  1477. unit->stop_stepaction(src);
  1478. // Remember the skill request from the client while walking to the next cell
  1479. if(src->type == BL_PC && ud->walktimer != INVALID_TIMER && !battle->check_range(src, &bl, range-1)) {
  1480. struct map_data *md = &map->list[src->m];
  1481. // Convert coordinates to target_to so we can use it as target later
  1482. ud->stepaction = true;
  1483. ud->target_to = (skill_x + skill_y*md->xs);
  1484. ud->stepskill_id = skill_id;
  1485. ud->stepskill_lv = skill_lv;
  1486. return 0; // Attacking will be handled by unit_walktoxy_timer in this case
  1487. }
  1488. if( skill->get_state(ud->skill_id) == ST_MOVE_ENABLE ) {
  1489. if( !unit->can_reach_bl(src, &bl, range + 1, 1, NULL, NULL) )
  1490. return 0; //Walk-path check failed.
  1491. } else if( !battle->check_range(src, &bl, range) )
  1492. return 0; //Arrow-path check failed.
  1493. unit->stop_attack(src);
  1494. // moved here to prevent Suffragium from ending if skill fails
  1495. #ifndef RENEWAL_CAST
  1496. if (!(skill->get_castnodex(skill_id, skill_lv)&2))
  1497. casttime = skill->cast_fix_sc(src, casttime);
  1498. #else
  1499. casttime = skill->vf_cast_fix(src, casttime, skill_id, skill_lv );
  1500. #endif
  1501. if (src->type == BL_NPC) { // NPC-objects do not have cast time
  1502. casttime = 0;
  1503. }
  1504. ud->state.skillcastcancel = castcancel&&casttime>0?1:0;
  1505. if( !sd || sd->skillitem != skill_id || skill->get_cast(skill_id,skill_lv) )
  1506. ud->canact_tick = tick + casttime + 100;
  1507. #if 0
  1508. if (sd) {
  1509. switch (skill_id) {
  1510. case ????:
  1511. sd->canequip_tick = tick + casttime;
  1512. }
  1513. }
  1514. #endif // 0
  1515. ud->skill_id = skill_id;
  1516. ud->skill_lv = skill_lv;
  1517. ud->skillx = skill_x;
  1518. ud->skilly = skill_y;
  1519. ud->skilltarget = 0;
  1520. if( sc ) {
  1521. /**
  1522. * why the if else chain: these 3 status do not stack, so its efficient that way.
  1523. **/
  1524. if (sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4)) {
  1525. status_change_end(src, SC_CLOAKING, INVALID_TIMER);
  1526. if (!src->prev) return 0; //Warped away!
  1527. } else if (sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4&4)) {
  1528. status_change_end(src, SC_CLOAKINGEXCEED, INVALID_TIMER);
  1529. if (!src->prev) return 0;
  1530. }
  1531. }
  1532. unit->stop_walking(src, STOPWALKING_FLAG_FIXPOS);
  1533. // in official this is triggered even if no cast time.
  1534. clif->skillcasting(src, src->id, 0, skill_x, skill_y, skill_id, skill->get_ele(skill_id, skill_lv), casttime);
  1535. if( casttime > 0 ) {
  1536. unit->setdir(src, map->calc_dir(src, skill_x, skill_y));
  1537. ud->skilltimer = timer->add( tick+casttime, skill->castend_pos, src->id, 0 );
  1538. if ( (sd && pc->checkskill(sd, SA_FREECAST) > 0) || skill_id == LG_EXEEDBREAK ) {
  1539. status_calc_bl(&sd->bl, SCB_SPEED|SCB_ASPD);
  1540. }
  1541. } else {
  1542. ud->skilltimer = INVALID_TIMER;
  1543. skill->castend_pos(ud->skilltimer,tick,src->id,0);
  1544. }
  1545. return 1;
  1546. }
  1547. /*========================================
  1548. * update a block's attack target
  1549. *----------------------------------------*/
  1550. int unit_set_target(struct unit_data* ud, int target_id)
  1551. {
  1552. nullpo_ret(ud);
  1553. if (ud->target != target_id) {
  1554. struct unit_data * ux;
  1555. struct block_list* target;
  1556. if (ud->target && (target = map->id2bl(ud->target)) != NULL && (ux = unit->bl2ud(target)) != NULL && ux->target_count > 0)
  1557. --ux->target_count;
  1558. if (target_id && (target = map->id2bl(target_id)) != NULL && (ux = unit->bl2ud(target)) != NULL)
  1559. ++ux->target_count;
  1560. }
  1561. ud->target = target_id;
  1562. return 0;
  1563. }
  1564. /**
  1565. * Stop a unit's attacks
  1566. * @param bl: Object to stop
  1567. */
  1568. void unit_stop_attack(struct block_list *bl)
  1569. {
  1570. struct unit_data *ud;
  1571. nullpo_retv(bl);
  1572. ud = unit->bl2ud(bl);
  1573. nullpo_retv(ud);
  1574. //Clear target
  1575. unit->set_target(ud, 0);
  1576. if(ud->attacktimer == INVALID_TIMER)
  1577. return;
  1578. //Clear timer
  1579. timer->delete(ud->attacktimer, unit->attack_timer);
  1580. ud->attacktimer = INVALID_TIMER;
  1581. }
  1582. /**
  1583. * Stop a unit's step action
  1584. * @param bl: Object to stop
  1585. */
  1586. void unit_stop_stepaction(struct block_list *bl)
  1587. {
  1588. struct unit_data *ud;
  1589. nullpo_retv(bl);
  1590. ud = unit->bl2ud(bl);
  1591. nullpo_retv(ud);
  1592. //Clear remembered step action
  1593. ud->stepaction = false;
  1594. ud->target_to = 0;
  1595. ud->stepskill_id = 0;
  1596. ud->stepskill_lv = 0;
  1597. if(ud->steptimer == INVALID_TIMER)
  1598. return;
  1599. //Clear timer
  1600. timer->delete(ud->steptimer, unit->step_timer);
  1601. ud->steptimer = INVALID_TIMER;
  1602. }
  1603. //Means current target is unattackable. For now only unlocks mobs.
  1604. int unit_unattackable(struct block_list *bl)
  1605. {
  1606. struct unit_data *ud = unit->bl2ud(bl);
  1607. if (ud) {
  1608. ud->state.attack_continue = 0;
  1609. ud->state.step_attack = 0;
  1610. unit->set_target(ud, 0);
  1611. }
  1612. if (bl->type == BL_MOB)
  1613. mob->unlocktarget(BL_UCAST(BL_MOB, bl), timer->gettick());
  1614. else if (bl->type == BL_PET)
  1615. pet->unlocktarget(BL_UCAST(BL_PET, bl));
  1616. return 0;
  1617. }
  1618. /*==========================================
  1619. * Attack request
  1620. * If type is an ongoing attack
  1621. *------------------------------------------*/
  1622. int unit_attack(struct block_list *src,int target_id,int continuous) {
  1623. struct block_list *target;
  1624. struct unit_data *ud;
  1625. int range;
  1626. nullpo_ret(ud = unit->bl2ud(src));
  1627. target = map->id2bl(target_id);
  1628. if( target==NULL || status->isdead(target) ) {
  1629. unit->unattackable(src);
  1630. return 1;
  1631. }
  1632. if (src->type == BL_PC) {
  1633. struct map_session_data *sd = BL_UCAST(BL_PC, src);
  1634. if( target->type == BL_NPC ) { // monster npcs [Valaris]
  1635. npc->click(sd, BL_UCAST(BL_NPC, target)); // submitted by leinsirk10 [Celest]
  1636. return 0;
  1637. }
  1638. if( pc_is90overweight(sd) || pc_isridingwug(sd) ) { // overweight or mounted on warg - stop attacking
  1639. unit->stop_attack(src);
  1640. return 0;
  1641. }
  1642. if( !pc->can_attack(sd, target_id) ) {
  1643. unit->stop_attack(src);
  1644. return 0;
  1645. }
  1646. }
  1647. if( battle->check_target(src,target,BCT_ENEMY) <= 0 || !status->check_skilluse(src, target, 0, 0) ) {
  1648. unit->unattackable(src);
  1649. return 1;
  1650. }
  1651. ud->state.attack_continue = (continuous&1)?1:0;
  1652. ud->state.step_attack = (continuous&2)?1:0;
  1653. unit->set_target(ud, target_id);
  1654. range = status_get_range(src);
  1655. if (continuous) //If you're to attack continuously, set to auto-case character
  1656. ud->chaserange = range;
  1657. //Just change target/type. [Skotlex]
  1658. if(ud->attacktimer != INVALID_TIMER)
  1659. return 0;
  1660. // New action request received, delete previous action request if not executed yet
  1661. if(ud->stepaction || ud->steptimer != INVALID_TIMER)
  1662. unit->stop_stepaction(src);
  1663. // Remember the attack request from the client while walking to the next cell
  1664. if(src->type == BL_PC && ud->walktimer != INVALID_TIMER && !battle->check_range(src, target, range-1)) {
  1665. ud->stepaction = true;
  1666. ud->target_to = ud->target;
  1667. ud->stepskill_id = 0;
  1668. ud->stepskill_lv = 0;
  1669. return 0; // Attacking will be handled by unit_walktoxy_timer in this case
  1670. }
  1671. if(DIFF_TICK(ud->attackabletime, timer->gettick()) > 0)
  1672. //Do attack next time it is possible. [Skotlex]
  1673. ud->attacktimer=timer->add(ud->attackabletime,unit->attack_timer,src->id,0);
  1674. else //Attack NOW.
  1675. unit->attack_timer(INVALID_TIMER, timer->gettick(), src->id, 0);
  1676. return 0;
  1677. }
  1678. //Cancels an ongoing combo, resets attackable time and restarts the
  1679. //attack timer to resume attacking after amotion time. [Skotlex]
  1680. int unit_cancel_combo(struct block_list *bl)
  1681. {
  1682. struct unit_data *ud;
  1683. if (!status_change_end(bl, SC_COMBOATTACK, INVALID_TIMER))
  1684. return 0; //Combo wasn't active.
  1685. ud = unit->bl2ud(bl);
  1686. nullpo_ret(ud);
  1687. ud->attackabletime = timer->gettick() + status_get_amotion(bl);
  1688. if (ud->attacktimer == INVALID_TIMER)
  1689. return 1; //Nothing more to do.
  1690. timer->delete(ud->attacktimer, unit->attack_timer);
  1691. ud->attacktimer=timer->add(ud->attackabletime,unit->attack_timer,bl->id,0);
  1692. return 1;
  1693. }
  1694. /*==========================================
  1695. *
  1696. *------------------------------------------*/
  1697. bool unit_can_reach_pos(struct block_list *bl,int x,int y, int easy)
  1698. {
  1699. nullpo_retr(false, bl);
  1700. if (bl->x == x && bl->y == y) //Same place
  1701. return true;
  1702. return path->search(NULL,bl,bl->m,bl->x,bl->y,x,y,easy,CELL_CHKNOREACH);
  1703. }
  1704. /*==========================================
  1705. *
  1706. *------------------------------------------*/
  1707. bool unit_can_reach_bl(struct block_list *bl,struct block_list *tbl, int range, int easy, short *x, short *y)
  1708. {
  1709. short dx,dy;
  1710. struct walkpath_data wpd;
  1711. nullpo_retr(false, bl);
  1712. nullpo_retr(false, tbl);
  1713. if( bl->m != tbl->m)
  1714. return false;
  1715. if( bl->x==tbl->x && bl->y==tbl->y )
  1716. return true;
  1717. if(range>0 && !check_distance_bl(bl, tbl, range))
  1718. return false;
  1719. // It judges whether it can adjoin or not.
  1720. dx=tbl->x - bl->x;
  1721. dy=tbl->y - bl->y;
  1722. dx=(dx>0)?1:((dx<0)?-1:0);
  1723. dy=(dy>0)?1:((dy<0)?-1:0);
  1724. if (map->getcell(tbl->m, bl, tbl->x - dx, tbl->y - dy, CELL_CHKNOPASS)) {
  1725. int i;
  1726. //Look for a suitable cell to place in.
  1727. for (i=0;i<8 && map->getcell(tbl->m, bl, tbl->x - dirx[i], tbl->y - diry[i], CELL_CHKNOPASS); i++);
  1728. if (i==8) return false; //No valid cells.
  1729. dx = dirx[i];
  1730. dy = diry[i];
  1731. }
  1732. if (x) *x = tbl->x-dx;
  1733. if (y) *y = tbl->y-dy;
  1734. if (!path->search(&wpd,bl,bl->m,bl->x,bl->y,tbl->x-dx,tbl->y-dy,easy,CELL_CHKNOREACH))
  1735. return false;
  1736. #ifdef OFFICIAL_WALKPATH
  1737. if( !path->search_long(NULL, bl, bl->m, bl->x, bl->y, tbl->x-dx, tbl->y-dy, CELL_CHKNOPASS) // Check if there is an obstacle between
  1738. && wpd.path_len > 14 // Official number of walkable cells is 14 if and only if there is an obstacle between. [malufett]
  1739. && (bl->type != BL_NPC) ) // If type is a NPC, please disregard.
  1740. return false;
  1741. #endif
  1742. return true;
  1743. }
  1744. /*==========================================
  1745. * Calculates position of Pet/Mercenary/Homunculus/Elemental
  1746. *------------------------------------------*/
  1747. int unit_calc_pos(struct block_list *bl, int tx, int ty, uint8 dir)
  1748. {
  1749. int dx, dy, x, y;
  1750. struct unit_data *ud = unit->bl2ud(bl);
  1751. nullpo_ret(ud);
  1752. if(dir > 7)
  1753. return 1;
  1754. ud->to_x = tx;
  1755. ud->to_y = ty;
  1756. // 2 cells from Master Position
  1757. dx = -dirx[dir] * 2;
  1758. dy = -diry[dir] * 2;
  1759. x = tx + dx;
  1760. y = ty + dy;
  1761. if (!unit->can_reach_pos(bl, x, y, 0)) {
  1762. if( dx > 0 ) x--; else if( dx < 0 ) x++;
  1763. if( dy > 0 ) y--; else if( dy < 0 ) y++;
  1764. if (!unit->can_reach_pos(bl, x, y, 0)) {
  1765. int i;
  1766. for (i = 0; i < 12; i++) {
  1767. int k = rnd()%8; // Pick a Random Dir
  1768. dx = -dirx[k] * 2;
  1769. dy = -diry[k] * 2;
  1770. x = tx + dx;
  1771. y = ty + dy;
  1772. if (unit->can_reach_pos(bl, x, y, 0)) {
  1773. break;
  1774. } else {
  1775. if( dx > 0 ) x--; else if( dx < 0 ) x++;
  1776. if( dy > 0 ) y--; else if( dy < 0 ) y++;
  1777. if( unit->can_reach_pos(bl, x, y, 0) )
  1778. break;
  1779. }
  1780. }
  1781. if (i == 12) {
  1782. x = tx; y = tx; // Exactly Master Position
  1783. if (!unit->can_reach_pos(bl, x, y, 0))
  1784. return 1;
  1785. }
  1786. }
  1787. }
  1788. ud->to_x = x;
  1789. ud->to_y = y;
  1790. return 0;
  1791. }
  1792. /*==========================================
  1793. * Continuous Attack (function timer)
  1794. *------------------------------------------*/
  1795. int unit_attack_timer_sub(struct block_list* src, int tid, int64 tick) {
  1796. struct block_list *target;
  1797. struct unit_data *ud;
  1798. struct status_data *sstatus;
  1799. struct map_session_data *sd = NULL;
  1800. struct mob_data *md = NULL;
  1801. int range;
  1802. if( (ud=unit->bl2ud(src))==NULL )
  1803. return 0;
  1804. if( ud->attacktimer != tid )
  1805. {
  1806. ShowError("unit_attack_timer %d != %d\n",ud->attacktimer,tid);
  1807. return 0;
  1808. }
  1809. sd = BL_CAST(BL_PC, src);
  1810. md = BL_CAST(BL_MOB, src);
  1811. ud->attacktimer = INVALID_TIMER;
  1812. target=map->id2bl(ud->target);
  1813. if( src == NULL || src->prev == NULL || target==NULL || target->prev == NULL )
  1814. return 0;
  1815. if( status->isdead(src) || status->isdead(target)
  1816. || battle->check_target(src,target,BCT_ENEMY) <= 0 || !status->check_skilluse(src, target, 0, 0)
  1817. #ifdef OFFICIAL_WALKPATH
  1818. || !path->search_long(NULL, src, src->m, src->x, src->y, target->x, target->y, CELL_CHKWALL)
  1819. #endif
  1820. || (sd && !pc->can_attack(sd, ud->target) )
  1821. )
  1822. return 0; // can't attack under these conditions
  1823. if (src->m != target->m) {
  1824. if (src->type == BL_MOB && mob->warpchase(BL_UCAST(BL_MOB, src), target))
  1825. return 1; // Follow up.
  1826. return 0;
  1827. }
  1828. if( ud->skilltimer != INVALID_TIMER && !(sd && pc->checkskill(sd,SA_FREECAST) > 0) )
  1829. return 0; // can't attack while casting
  1830. if( !battle_config.sdelay_attack_enable && DIFF_TICK(ud->canact_tick,tick) > 0 && !(sd && pc->checkskill(sd,SA_FREECAST) > 0) )
  1831. { // attacking when under cast delay has restrictions:
  1832. if( tid == INVALID_TIMER ) { //requested attack.
  1833. if(sd) clif->skill_fail(sd,1,USESKILL_FAIL_SKILLINTERVAL,0);
  1834. return 0;
  1835. }
  1836. //Otherwise, we are in a combo-attack, delay this until your canact time is over. [Skotlex]
  1837. if( ud->state.attack_continue ) {
  1838. if( DIFF_TICK(ud->canact_tick, ud->attackabletime) > 0 )
  1839. ud->attackabletime = ud->canact_tick;
  1840. ud->attacktimer=timer->add(ud->attackabletime,unit->attack_timer,src->id,0);
  1841. }
  1842. return 1;
  1843. }
  1844. sstatus = status->get_status_data(src);
  1845. range = sstatus->rhw.range;
  1846. if( (unit->is_walking(target) || ud->state.step_attack)
  1847. && (target->type == BL_PC || !map->getcell(target->m, src, target->x, target->y, CELL_CHKICEWALL)))
  1848. range++; // Extra range when chasing (does not apply to mobs locked in an icewall)
  1849. if(sd && !check_distance_client_bl(src,target,range)) {
  1850. // Player tries to attack but target is too far, notify client
  1851. clif->movetoattack(sd,target);
  1852. return 1;
  1853. } else if(md && !check_distance_bl(src,target,range)) {
  1854. // Monster: Chase if required
  1855. unit->walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1856. return 1;
  1857. }
  1858. if( !battle->check_range(src,target,range) ) {
  1859. //Within range, but no direct line of attack
  1860. if( ud->state.attack_continue ) {
  1861. if(ud->chaserange > 2) ud->chaserange-=2;
  1862. unit->walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1863. }
  1864. return 1;
  1865. }
  1866. //Sync packet only for players.
  1867. //Non-players use the sync packet on the walk timer. [Skotlex]
  1868. if (tid == INVALID_TIMER && sd) clif->fixpos(src);
  1869. if( DIFF_TICK(ud->attackabletime,tick) <= 0 ) {
  1870. if (battle_config.attack_direction_change && (src->type&battle_config.attack_direction_change)) {
  1871. ud->dir = map->calc_dir(src, target->x,target->y );
  1872. }
  1873. if(ud->walktimer != INVALID_TIMER)
  1874. unit->stop_walking(src, STOPWALKING_FLAG_FIXPOS);
  1875. if(md) {
  1876. //First attack is always a normal attack
  1877. if(md->state.skillstate == MSS_ANGRY || md->state.skillstate == MSS_BERSERK) {
  1878. if (mob->skill_use(md,tick,-1))
  1879. return 1;
  1880. } else {
  1881. // Set mob's ANGRY/BERSERK states.
  1882. md->state.skillstate = md->state.aggressive?MSS_ANGRY:MSS_BERSERK;
  1883. }
  1884. if (sstatus->mode&MD_ASSIST && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME) {
  1885. // Link monsters nearby [Skotlex]
  1886. md->last_linktime = tick;
  1887. map->foreachinrange(mob->linksearch, src, md->db->range2, BL_MOB, md->class_, target, tick);
  1888. }
  1889. }
  1890. if (src->type == BL_PET && pet->attackskill(BL_UCAST(BL_PET, src), target->id))
  1891. return 1;
  1892. map->freeblock_lock();
  1893. ud->attacktarget_lv = battle->weapon_attack(src,target,tick,0);
  1894. if(sd && sd->status.pet_id > 0 && sd->pd && battle_config.pet_attack_support)
  1895. pet->target_check(sd,target,0);
  1896. map->freeblock_unlock();
  1897. /**
  1898. * Applied when you're unable to attack (e.g. out of ammo)
  1899. * We should stop here otherwise timer keeps on and this happens endlessly
  1900. **/
  1901. if( ud->attacktarget_lv == ATK_NONE )
  1902. return 1;
  1903. ud->attackabletime = tick + sstatus->adelay;
  1904. // You can't move if you can't attack neither.
  1905. if (src->type&battle_config.attack_walk_delay)
  1906. unit->set_walkdelay(src, tick, sstatus->amotion, 1);
  1907. }
  1908. if(ud->state.attack_continue) {
  1909. unit->setdir(src, map->calc_dir(src, target->x, target->y));
  1910. if( src->type == BL_PC )
  1911. pc->update_idle_time(sd, BCIDLE_ATTACK);
  1912. ud->attacktimer = timer->add(ud->attackabletime,unit->attack_timer,src->id,0);
  1913. }
  1914. return 1;
  1915. }
  1916. int unit_attack_timer(int tid, int64 tick, int id, intptr_t data) {
  1917. struct block_list *bl;
  1918. bl = map->id2bl(id);
  1919. if(bl && unit->attack_timer_sub(bl, tid, tick) == 0)
  1920. unit->unattackable(bl);
  1921. return 0;
  1922. }
  1923. /*==========================================
  1924. * Cancels an ongoing skill cast.
  1925. * flag&1: Cast-Cancel invoked.
  1926. * flag&2: Cancel only if skill is can be cancel.
  1927. *------------------------------------------*/
  1928. int unit_skillcastcancel(struct block_list *bl,int type)
  1929. {
  1930. struct map_session_data *sd = NULL;
  1931. struct unit_data *ud = unit->bl2ud( bl);
  1932. int64 tick = timer->gettick();
  1933. int ret=0, skill_id;
  1934. nullpo_ret(bl);
  1935. if (!ud || ud->skilltimer == INVALID_TIMER)
  1936. return 0; //Nothing to cancel.
  1937. sd = BL_CAST(BL_PC, bl);
  1938. if (type&2) {
  1939. //See if it can be canceled.
  1940. if (!ud->state.skillcastcancel)
  1941. return 0;
  1942. if (sd && (sd->special_state.no_castcancel2
  1943. || (sd->special_state.no_castcancel && !map_flag_gvg(bl->m) && !map->list[bl->m].flag.battleground))) //fixed flags being read the wrong way around [blackhole89]
  1944. return 0;
  1945. }
  1946. ud->canact_tick = tick;
  1947. if(type&1 && sd)
  1948. skill_id = sd->skill_id_old;
  1949. else
  1950. skill_id = ud->skill_id;
  1951. if (skill->get_inf(skill_id) & INF_GROUND_SKILL)
  1952. ret = timer->delete( ud->skilltimer, skill->castend_pos );
  1953. else
  1954. ret = timer->delete( ud->skilltimer, skill->castend_id );
  1955. if( ret < 0 )
  1956. ShowError("delete timer error %d : skill %d (%s)\n",ret,skill_id,skill->get_name(skill_id));
  1957. ud->skilltimer = INVALID_TIMER;
  1958. if( sd && pc->checkskill(sd,SA_FREECAST) > 0 )
  1959. status_calc_bl(&sd->bl, SCB_SPEED|SCB_ASPD);
  1960. if( sd ) {
  1961. switch( skill_id ) {
  1962. case CG_ARROWVULCAN:
  1963. sd->canequip_tick = tick;
  1964. break;
  1965. }
  1966. }
  1967. if (bl->type == BL_MOB)
  1968. BL_UCAST(BL_MOB, bl)->skill_idx = -1;
  1969. clif->skillcastcancel(bl);
  1970. return 1;
  1971. }
  1972. // unit_data initialization process
  1973. void unit_dataset(struct block_list *bl) {
  1974. struct unit_data *ud;
  1975. nullpo_retv(ud = unit->bl2ud(bl));
  1976. memset( ud, 0, sizeof( struct unit_data) );
  1977. ud->bl = bl;
  1978. ud->walktimer = INVALID_TIMER;
  1979. ud->skilltimer = INVALID_TIMER;
  1980. ud->attacktimer = INVALID_TIMER;
  1981. ud->steptimer = INVALID_TIMER;
  1982. ud->attackabletime =
  1983. ud->canact_tick =
  1984. ud->canmove_tick = timer->gettick();
  1985. }
  1986. /*==========================================
  1987. * Counts the number of units attacking 'bl'
  1988. *------------------------------------------*/
  1989. int unit_counttargeted(struct block_list* bl)
  1990. {
  1991. struct unit_data* ud;
  1992. if (bl && (ud = unit->bl2ud(bl)) != NULL)
  1993. return ud->target_count;
  1994. return 0;
  1995. }
  1996. /*==========================================
  1997. *
  1998. *------------------------------------------*/
  1999. int unit_fixdamage(struct block_list *src, struct block_list *target, int sdelay, int ddelay, int64 damage, short div, unsigned char type, int64 damage2) {
  2000. nullpo_ret(target);
  2001. if(damage+damage2 <= 0)
  2002. return 0;
  2003. return status_fix_damage(src,target,damage+damage2,clif->damage(target,target,sdelay,ddelay,damage,div,type,damage2));
  2004. }
  2005. /*==========================================
  2006. * To change the size of the char (player or mob only)
  2007. *------------------------------------------*/
  2008. int unit_changeviewsize(struct block_list *bl,short size)
  2009. {
  2010. nullpo_ret(bl);
  2011. size=(size<0)?-1:(size>0)?1:0;
  2012. if(bl->type == BL_PC) {
  2013. BL_UCAST(BL_PC, bl)->state.size = size;
  2014. } else if(bl->type == BL_MOB) {
  2015. BL_UCAST(BL_MOB, bl)->special_state.size = size;
  2016. } else
  2017. return 0;
  2018. if(size!=0)
  2019. clif->specialeffect(bl,421+size, AREA);
  2020. return 0;
  2021. }
  2022. /*==========================================
  2023. * Removes a bl/ud from the map.
  2024. * Returns 1 on success. 0 if it couldn't be removed or the bl was free'd
  2025. * if clrtype is 1 (death), appropriate cleanup is performed.
  2026. * Otherwise it is assumed bl is being warped.
  2027. * On-Kill specific stuff is not performed here, look at status->damage for that.
  2028. *------------------------------------------*/
  2029. int unit_remove_map(struct block_list *bl, clr_type clrtype, const char* file, int line, const char* func) {
  2030. struct unit_data *ud = unit->bl2ud(bl);
  2031. struct status_change *sc = status->get_sc(bl);
  2032. nullpo_ret(ud);
  2033. if(bl->prev == NULL)
  2034. return 0; //Already removed?
  2035. map->freeblock_lock();
  2036. if (ud->walktimer != INVALID_TIMER)
  2037. unit->stop_walking(bl, STOPWALKING_FLAG_NONE);
  2038. if (ud->skilltimer != INVALID_TIMER)
  2039. unit->skillcastcancel(bl,0);
  2040. //Clear target even if there is no timer
  2041. if (ud->target || ud->attacktimer != INVALID_TIMER)
  2042. unit->stop_attack(bl);
  2043. //Clear stepaction even if there is no timer
  2044. if (ud->stepaction || ud->steptimer != INVALID_TIMER)
  2045. unit->stop_stepaction(bl);
  2046. // Do not reset can-act delay. [Skotlex]
  2047. ud->attackabletime = ud->canmove_tick /*= ud->canact_tick*/ = timer->gettick();
  2048. if(sc && sc->count ) { //map-change/warp dispells.
  2049. status_change_end(bl, SC_BLADESTOP, INVALID_TIMER);
  2050. status_change_end(bl, SC_BASILICA, INVALID_TIMER);
  2051. status_change_end(bl, SC_ANKLESNARE, INVALID_TIMER);
  2052. status_change_end(bl, SC_TRICKDEAD, INVALID_TIMER);
  2053. status_change_end(bl, SC_BLADESTOP_WAIT, INVALID_TIMER);
  2054. status_change_end(bl, SC_RUN, INVALID_TIMER);
  2055. status_change_end(bl, SC_DANCING, INVALID_TIMER);
  2056. status_change_end(bl, SC_WARM, INVALID_TIMER);
  2057. status_change_end(bl, SC_DEVOTION, INVALID_TIMER);
  2058. status_change_end(bl, SC_MARIONETTE_MASTER, INVALID_TIMER);
  2059. status_change_end(bl, SC_MARIONETTE, INVALID_TIMER);
  2060. status_change_end(bl, SC_RG_CCONFINE_M, INVALID_TIMER);
  2061. status_change_end(bl, SC_RG_CCONFINE_S, INVALID_TIMER);
  2062. status_change_end(bl, SC_HIDING, INVALID_TIMER);
  2063. // Ensure the bl is a PC; if so, we'll handle the removal of cloaking and cloaking exceed later
  2064. if ( bl->type != BL_PC ) {
  2065. status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  2066. status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  2067. }
  2068. status_change_end(bl, SC_CHASEWALK, INVALID_TIMER);
  2069. if (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF)
  2070. status_change_end(bl, SC_GOSPEL, INVALID_TIMER);
  2071. status_change_end(bl, SC_HLIF_CHANGE, INVALID_TIMER);
  2072. status_change_end(bl, SC_STOP, INVALID_TIMER);
  2073. status_change_end(bl, SC_WUGDASH, INVALID_TIMER);
  2074. status_change_end(bl, SC_CAMOUFLAGE, INVALID_TIMER);
  2075. status_change_end(bl, SC_MAGNETICFIELD, INVALID_TIMER);
  2076. status_change_end(bl, SC_NEUTRALBARRIER_MASTER, INVALID_TIMER);
  2077. status_change_end(bl, SC_NEUTRALBARRIER, INVALID_TIMER);
  2078. status_change_end(bl, SC_STEALTHFIELD_MASTER, INVALID_TIMER);
  2079. status_change_end(bl, SC_STEALTHFIELD, INVALID_TIMER);
  2080. status_change_end(bl, SC__SHADOWFORM, INVALID_TIMER);
  2081. status_change_end(bl, SC__MANHOLE, INVALID_TIMER);
  2082. status_change_end(bl, SC_VACUUM_EXTREME, INVALID_TIMER);
  2083. status_change_end(bl, SC_CURSEDCIRCLE_ATKER, INVALID_TIMER); //callme before warp
  2084. status_change_end(bl, SC_NETHERWORLD, INVALID_TIMER);
  2085. }
  2086. if (bl->type&(BL_CHAR|BL_PET)) {
  2087. skill->unit_move(bl,timer->gettick(),4);
  2088. skill->cleartimerskill(bl);
  2089. }
  2090. switch( bl->type ) {
  2091. case BL_PC:
  2092. {
  2093. struct map_session_data *sd = BL_UCAST(BL_PC, bl);
  2094. if(sd->shadowform_id) {
  2095. struct block_list *d_bl = map->id2bl(sd->shadowform_id);
  2096. if( d_bl )
  2097. status_change_end(d_bl,SC__SHADOWFORM,INVALID_TIMER);
  2098. }
  2099. //Leave/reject all invitations.
  2100. if(sd->chatID)
  2101. chat->leave(sd, false);
  2102. if(sd->trade_partner)
  2103. trade->cancel(sd);
  2104. buyingstore->close(sd);
  2105. searchstore->close(sd);
  2106. if( sd->menuskill_id != AL_TELEPORT ) { // issue: 8027
  2107. if(sd->state.storage_flag == STORAGE_FLAG_NORMAL)
  2108. storage->pc_quit(sd,0);
  2109. else if (sd->state.storage_flag == STORAGE_FLAG_GUILD)
  2110. gstorage->pc_quit(sd,0);
  2111. sd->state.storage_flag = STORAGE_FLAG_CLOSED; //Force close it when being warped.
  2112. }
  2113. if(sd->party_invite>0)
  2114. party->reply_invite(sd,sd->party_invite,0);
  2115. if(sd->guild_invite>0)
  2116. guild->reply_invite(sd,sd->guild_invite,0);
  2117. if(sd->guild_alliance>0)
  2118. guild->reply_reqalliance(sd,sd->guild_alliance_account,0);
  2119. if(sd->menuskill_id)
  2120. sd->menuskill_id = sd->menuskill_val = 0;
  2121. if( sd->touching_id )
  2122. npc->touchnext_areanpc(sd,true);
  2123. // Check if warping and not changing the map.
  2124. if ( sd->state.warping && !sd->state.changemap ) {
  2125. status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  2126. status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  2127. }
  2128. sd->npc_shopid = 0;
  2129. sd->adopt_invite = 0;
  2130. if(sd->pvp_timer != INVALID_TIMER) {
  2131. timer->delete(sd->pvp_timer,pc->calc_pvprank_timer);
  2132. sd->pvp_timer = INVALID_TIMER;
  2133. sd->pvp_rank = 0;
  2134. }
  2135. if(sd->duel_group > 0)
  2136. duel->leave(sd->duel_group, sd);
  2137. if(pc_issit(sd)) {
  2138. pc->setstand(sd);
  2139. skill->sit(sd,0);
  2140. }
  2141. party->send_dot_remove(sd);//minimap dot fix [Kevin]
  2142. guild->send_dot_remove(sd);
  2143. bg->send_dot_remove(sd);
  2144. if( map->list[bl->m].users <= 0 || sd->state.debug_remove_map ) {
  2145. // 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]
  2146. if( sd->debug_file == NULL || !(sd->state.debug_remove_map) ) {
  2147. sd->debug_file = "";
  2148. sd->debug_line = 0;
  2149. sd->debug_func = "";
  2150. }
  2151. ShowDebug("unit_remove_map: unexpected state when removing player AID/CID:%d/%d"
  2152. " (active=%d connect_new=%d rewarp=%d changemap=%d debug_remove_map=%d)"
  2153. " from map=%s (users=%d)."
  2154. " Previous call from %s:%d(%s), current call from %s:%d(%s)."
  2155. " Please report this!!!\n",
  2156. sd->status.account_id, sd->status.char_id,
  2157. sd->state.active, sd->state.connect_new, sd->state.rewarp, sd->state.changemap, sd->state.debug_remove_map,
  2158. map->list[bl->m].name, map->list[bl->m].users,
  2159. sd->debug_file, sd->debug_line, sd->debug_func, file, line, func);
  2160. } else if (--map->list[bl->m].users == 0 && battle_config.dynamic_mobs) //[Skotlex]
  2161. map->removemobs(bl->m);
  2162. if (!(pc_isinvisible(sd))) {
  2163. // decrement the number of active pvp players on the map
  2164. --map->list[bl->m].users_pvp;
  2165. }
  2166. if( map->list[bl->m].instance_id >= 0 ) {
  2167. instance->list[map->list[bl->m].instance_id].users--;
  2168. instance->check_idle(map->list[bl->m].instance_id);
  2169. }
  2170. if( sd->state.hpmeter_visible ) {
  2171. map->list[bl->m].hpmeter_visible--;
  2172. sd->state.hpmeter_visible = 0;
  2173. }
  2174. sd->state.debug_remove_map = 1; // temporary state to track double remove_map's [FlavioJS]
  2175. sd->debug_file = file;
  2176. sd->debug_line = line;
  2177. sd->debug_func = func;
  2178. break;
  2179. }
  2180. case BL_MOB:
  2181. {
  2182. struct mob_data *md = BL_UCAST(BL_MOB, bl);
  2183. // Drop previous target mob_slave_keep_target: no.
  2184. if (!battle_config.mob_slave_keep_target)
  2185. md->target_id=0;
  2186. md->attacked_id=0;
  2187. md->state.skillstate= MSS_IDLE;
  2188. break;
  2189. }
  2190. case BL_PET:
  2191. {
  2192. struct pet_data *pd = BL_UCAST(BL_PET, bl);
  2193. if( pd->pet.intimate <= 0 && !(pd->msd && !pd->msd->state.active) ) {
  2194. //If logging out, this is deleted on unit->free
  2195. clif->clearunit_area(bl,clrtype);
  2196. map->delblock(bl);
  2197. unit->free(bl,CLR_OUTSIGHT);
  2198. map->freeblock_unlock();
  2199. return 0;
  2200. }
  2201. break;
  2202. }
  2203. case BL_HOM: {
  2204. struct homun_data *hd = BL_UCAST(BL_HOM, bl);
  2205. if( !hd->homunculus.intimacy && !(hd->master && !hd->master->state.active) ) {
  2206. //If logging out, this is deleted on unit->free
  2207. clif->emotion(bl, E_SOB);
  2208. clif->clearunit_area(bl,clrtype);
  2209. map->delblock(bl);
  2210. unit->free(bl,CLR_OUTSIGHT);
  2211. map->freeblock_unlock();
  2212. return 0;
  2213. }
  2214. break;
  2215. }
  2216. case BL_MER: {
  2217. struct mercenary_data *md = BL_UCAST(BL_MER, bl);
  2218. ud->canact_tick = ud->canmove_tick;
  2219. if( mercenary->get_lifetime(md) <= 0 && !(md->master && !md->master->state.active) ) {
  2220. clif->clearunit_area(bl,clrtype);
  2221. map->delblock(bl);
  2222. unit->free(bl,CLR_OUTSIGHT);
  2223. map->freeblock_unlock();
  2224. return 0;
  2225. }
  2226. break;
  2227. }
  2228. case BL_ELEM: {
  2229. struct elemental_data *ed = BL_UCAST(BL_ELEM, bl);
  2230. ud->canact_tick = ud->canmove_tick;
  2231. if( elemental->get_lifetime(ed) <= 0 && !(ed->master && !ed->master->state.active) ) {
  2232. clif->clearunit_area(bl,clrtype);
  2233. map->delblock(bl);
  2234. unit->free(bl,0);
  2235. map->freeblock_unlock();
  2236. return 0;
  2237. }
  2238. break;
  2239. }
  2240. default: break;// do nothing
  2241. }
  2242. /**
  2243. * BL_MOB is handled by mob_dead unless the monster is not dead.
  2244. **/
  2245. if( bl->type != BL_MOB || !status->isdead(bl) )
  2246. clif->clearunit_area(bl,clrtype);
  2247. map->delblock(bl);
  2248. map->freeblock_unlock();
  2249. return 1;
  2250. }
  2251. void unit_remove_map_pc(struct map_session_data *sd, clr_type clrtype)
  2252. {
  2253. unit->remove_map(&sd->bl,clrtype,ALC_MARK);
  2254. //CLR_RESPAWN is the warp from logging out, CLR_TELEPORT is the warp from teleporting, but pets/homunc need to just 'vanish' instead of showing the warping animation.
  2255. if (clrtype == CLR_RESPAWN || clrtype == CLR_TELEPORT) clrtype = CLR_OUTSIGHT;
  2256. if(sd->pd)
  2257. unit->remove_map(&sd->pd->bl, clrtype, ALC_MARK);
  2258. if(homun_alive(sd->hd))
  2259. unit->remove_map(&sd->hd->bl, clrtype, ALC_MARK);
  2260. if(sd->md)
  2261. unit->remove_map(&sd->md->bl, clrtype, ALC_MARK);
  2262. if(sd->ed)
  2263. unit->remove_map(&sd->ed->bl, clrtype, ALC_MARK);
  2264. }
  2265. void unit_free_pc(struct map_session_data *sd)
  2266. {
  2267. if (sd->pd) unit->free(&sd->pd->bl,CLR_OUTSIGHT);
  2268. if (sd->hd) unit->free(&sd->hd->bl,CLR_OUTSIGHT);
  2269. if (sd->md) unit->free(&sd->md->bl,CLR_OUTSIGHT);
  2270. if (sd->ed) unit->free(&sd->ed->bl,CLR_OUTSIGHT);
  2271. unit->free(&sd->bl,CLR_TELEPORT);
  2272. }
  2273. /*==========================================
  2274. * Function to free all related resources to the bl
  2275. * if unit is on map, it is removed using the clrtype specified
  2276. *------------------------------------------*/
  2277. int unit_free(struct block_list *bl, clr_type clrtype) {
  2278. struct unit_data *ud = unit->bl2ud( bl );
  2279. nullpo_ret(ud);
  2280. map->freeblock_lock();
  2281. if( bl->prev ) //Players are supposed to logout with a "warp" effect.
  2282. unit->remove_map(bl, clrtype, ALC_MARK);
  2283. switch( bl->type ) {
  2284. case BL_PC:
  2285. {
  2286. struct map_session_data *sd = BL_UCAST(BL_PC, bl);
  2287. sd->state.loggingout = 1;
  2288. if( status->isdead(bl) )
  2289. pc->setrestartvalue(sd,2);
  2290. pc->delinvincibletimer(sd);
  2291. pc->delautobonus(sd,sd->autobonus,ARRAYLENGTH(sd->autobonus),false);
  2292. pc->delautobonus(sd,sd->autobonus2,ARRAYLENGTH(sd->autobonus2),false);
  2293. pc->delautobonus(sd,sd->autobonus3,ARRAYLENGTH(sd->autobonus3),false);
  2294. if( sd->followtimer != INVALID_TIMER )
  2295. pc->stop_following(sd);
  2296. if( sd->duel_invite > 0 )
  2297. duel->reject(sd->duel_invite, sd);
  2298. // Notify friends that this char logged out. [Skotlex]
  2299. map->foreachpc(clif->friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
  2300. party->send_logout(sd);
  2301. guild->send_memberinfoshort(sd,0);
  2302. pc->cleareventtimer(sd);
  2303. pc->inventory_rental_clear(sd);
  2304. pc->delspiritball(sd,sd->spiritball,1);
  2305. pc->del_charm(sd, sd->charm_count, sd->charm_type);
  2306. if( sd->st && sd->st->state != RUN ) {// free attached scripts that are waiting
  2307. script->free_state(sd->st);
  2308. sd->st = NULL;
  2309. sd->npc_id = 0;
  2310. }
  2311. if( sd->combos ) {
  2312. aFree(sd->combos);
  2313. sd->combos = NULL;
  2314. }
  2315. sd->combo_count = 0;
  2316. /* [Ind/Hercules] */
  2317. if( sd->sc_display_count ) {
  2318. int i;
  2319. for(i = 0; i < sd->sc_display_count; i++) {
  2320. ers_free(pc->sc_display_ers, sd->sc_display[i]);
  2321. }
  2322. sd->sc_display_count = 0;
  2323. }
  2324. if( sd->sc_display != NULL ) {
  2325. aFree(sd->sc_display);
  2326. sd->sc_display = NULL;
  2327. }
  2328. if( sd->instance != NULL ) {
  2329. aFree(sd->instance);
  2330. sd->instance = NULL;
  2331. }
  2332. VECTOR_CLEAR(sd->script_queues);
  2333. if( sd->quest_log != NULL ) {
  2334. aFree(sd->quest_log);
  2335. sd->quest_log = NULL;
  2336. sd->num_quests = sd->avail_quests = 0;
  2337. }
  2338. HPM->data_store_destroy(&sd->hdata);
  2339. break;
  2340. }
  2341. case BL_PET:
  2342. {
  2343. struct pet_data *pd = BL_UCAST(BL_PET, bl);
  2344. struct map_session_data *sd = pd->msd;
  2345. pet->hungry_timer_delete(pd);
  2346. if( pd->a_skill )
  2347. {
  2348. aFree(pd->a_skill);
  2349. pd->a_skill = NULL;
  2350. }
  2351. if( pd->s_skill )
  2352. {
  2353. if (pd->s_skill->timer != INVALID_TIMER) {
  2354. timer->delete(pd->s_skill->timer, pet->skill_support_timer);
  2355. }
  2356. aFree(pd->s_skill);
  2357. pd->s_skill = NULL;
  2358. }
  2359. if( pd->recovery )
  2360. {
  2361. if(pd->recovery->timer != INVALID_TIMER)
  2362. timer->delete(pd->recovery->timer, pet->recovery_timer);
  2363. aFree(pd->recovery);
  2364. pd->recovery = NULL;
  2365. }
  2366. if( pd->bonus )
  2367. {
  2368. if (pd->bonus->timer != INVALID_TIMER)
  2369. timer->delete(pd->bonus->timer, pet->skill_bonus_timer);
  2370. aFree(pd->bonus);
  2371. pd->bonus = NULL;
  2372. }
  2373. if( pd->loot )
  2374. {
  2375. pet->lootitem_drop(pd,sd);
  2376. if (pd->loot->item)
  2377. aFree(pd->loot->item);
  2378. aFree (pd->loot);
  2379. pd->loot = NULL;
  2380. }
  2381. if (pd->pet.intimate > 0) {
  2382. intif->save_petdata(pd->pet.account_id,&pd->pet);
  2383. } else {
  2384. //Remove pet.
  2385. intif->delete_petdata(pd->pet.pet_id);
  2386. if (sd) sd->status.pet_id = 0;
  2387. }
  2388. if( sd )
  2389. sd->pd = NULL;
  2390. break;
  2391. }
  2392. case BL_MOB:
  2393. {
  2394. struct mob_data *md = BL_UCAST(BL_MOB, bl);
  2395. if( md->spawn_timer != INVALID_TIMER )
  2396. {
  2397. timer->delete(md->spawn_timer,mob->delayspawn);
  2398. md->spawn_timer = INVALID_TIMER;
  2399. }
  2400. if( md->deletetimer != INVALID_TIMER )
  2401. {
  2402. timer->delete(md->deletetimer,mob->timer_delete);
  2403. md->deletetimer = INVALID_TIMER;
  2404. }
  2405. if( md->lootitem )
  2406. {
  2407. aFree(md->lootitem);
  2408. md->lootitem=NULL;
  2409. }
  2410. if( md->guardian_data )
  2411. {
  2412. struct guild_castle* gc = md->guardian_data->castle;
  2413. if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
  2414. {
  2415. gc->guardian[md->guardian_data->number].id = 0;
  2416. }
  2417. else
  2418. {
  2419. int i;
  2420. ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == md->bl.id);
  2421. if( i < gc->temp_guardians_max )
  2422. gc->temp_guardians[i] = 0;
  2423. }
  2424. aFree(md->guardian_data);
  2425. md->guardian_data = NULL;
  2426. }
  2427. if( md->spawn )
  2428. {
  2429. md->spawn->active--;
  2430. if( !md->spawn->state.dynamic )
  2431. {// permanently remove the mob
  2432. if( --md->spawn->num == 0 )
  2433. {// Last freed mob is responsible for deallocating the group's spawn data.
  2434. aFree(md->spawn);
  2435. md->spawn = NULL;
  2436. }
  2437. }
  2438. }
  2439. if( md->base_status)
  2440. {
  2441. aFree(md->base_status);
  2442. md->base_status = NULL;
  2443. }
  2444. if( mob->is_clone(md->class_) )
  2445. mob->clone_delete(md);
  2446. if( md->tomb_nid )
  2447. mob->mvptomb_destroy(md);
  2448. HPM->data_store_destroy(&md->hdata);
  2449. break;
  2450. }
  2451. case BL_HOM:
  2452. {
  2453. struct homun_data *hd = BL_UCAST(BL_HOM, bl);
  2454. struct map_session_data *sd = hd->master;
  2455. homun->hunger_timer_delete(hd);
  2456. if( hd->homunculus.intimacy > 0 )
  2457. homun->save(hd);
  2458. else {
  2459. intif->homunculus_requestdelete(hd->homunculus.hom_id);
  2460. if( sd )
  2461. sd->status.hom_id = 0;
  2462. }
  2463. if( sd )
  2464. sd->hd = NULL;
  2465. break;
  2466. }
  2467. case BL_MER:
  2468. {
  2469. struct mercenary_data *md = BL_UCAST(BL_MER, bl);
  2470. struct map_session_data *sd = md->master;
  2471. if( mercenary->get_lifetime(md) > 0 )
  2472. mercenary->save(md);
  2473. else
  2474. {
  2475. intif->mercenary_delete(md->mercenary.mercenary_id);
  2476. if( sd )
  2477. sd->status.mer_id = 0;
  2478. }
  2479. if( sd )
  2480. sd->md = NULL;
  2481. mercenary->contract_stop(md);
  2482. break;
  2483. }
  2484. case BL_ELEM: {
  2485. struct elemental_data *ed = BL_UCAST(BL_ELEM, bl);
  2486. struct map_session_data *sd = ed->master;
  2487. if( elemental->get_lifetime(ed) > 0 )
  2488. elemental->save(ed);
  2489. else {
  2490. intif->elemental_delete(ed->elemental.elemental_id);
  2491. if( sd )
  2492. sd->status.ele_id = 0;
  2493. }
  2494. if( sd )
  2495. sd->ed = NULL;
  2496. elemental->summon_stop(ed);
  2497. break;
  2498. }
  2499. }
  2500. skill->clear_unitgroup(bl);
  2501. status->change_clear(bl,1);
  2502. map->deliddb(bl);
  2503. if( bl->type != BL_PC ) //Players are handled by map_quit
  2504. map->freeblock(bl);
  2505. map->freeblock_unlock();
  2506. return 0;
  2507. }
  2508. int do_init_unit(bool minimal) {
  2509. if (minimal)
  2510. return 0;
  2511. timer->add_func_list(unit->attack_timer, "unit_attack_timer");
  2512. timer->add_func_list(unit->walktoxy_timer,"unit_walktoxy_timer");
  2513. timer->add_func_list(unit->walktobl_sub, "unit_walktobl_sub");
  2514. timer->add_func_list(unit->delay_walktoxy_timer,"unit_delay_walktoxy_timer");
  2515. timer->add_func_list(unit->step_timer,"unit_step_timer");
  2516. return 0;
  2517. }
  2518. int do_final_unit(void) {
  2519. // nothing to do
  2520. return 0;
  2521. }
  2522. void unit_defaults(void) {
  2523. unit = &unit_s;
  2524. unit->init = do_init_unit;
  2525. unit->final = do_final_unit;
  2526. /* */
  2527. unit->bl2ud = unit_bl2ud;
  2528. unit->bl2ud2 = unit_bl2ud2;
  2529. unit->attack_timer = unit_attack_timer;
  2530. unit->walktoxy_timer = unit_walktoxy_timer;
  2531. unit->walktoxy_sub = unit_walktoxy_sub;
  2532. unit->delay_walktoxy_timer = unit_delay_walktoxy_timer;
  2533. unit->walktoxy = unit_walktoxy;
  2534. unit->walktobl_sub = unit_walktobl_sub;
  2535. unit->walktobl = unit_walktobl;
  2536. unit->run = unit_run;
  2537. unit->run_hit = unit_run_hit;
  2538. unit->escape = unit_escape;
  2539. unit->movepos = unit_movepos;
  2540. unit->setdir = unit_setdir;
  2541. unit->getdir = unit_getdir;
  2542. unit->blown = unit_blown;
  2543. unit->warp = unit_warp;
  2544. unit->stop_walking = unit_stop_walking;
  2545. unit->skilluse_id = unit_skilluse_id;
  2546. unit->step_timer = unit_step_timer;
  2547. unit->stop_stepaction = unit_stop_stepaction;
  2548. unit->is_walking = unit_is_walking;
  2549. unit->can_move = unit_can_move;
  2550. unit->resume_running = unit_resume_running;
  2551. unit->set_walkdelay = unit_set_walkdelay;
  2552. unit->skilluse_id2 = unit_skilluse_id2;
  2553. unit->skilluse_pos = unit_skilluse_pos;
  2554. unit->skilluse_pos2 = unit_skilluse_pos2;
  2555. unit->set_target = unit_set_target;
  2556. unit->stop_attack = unit_stop_attack;
  2557. unit->unattackable = unit_unattackable;
  2558. unit->attack = unit_attack;
  2559. unit->cancel_combo = unit_cancel_combo;
  2560. unit->can_reach_pos = unit_can_reach_pos;
  2561. unit->can_reach_bl = unit_can_reach_bl;
  2562. unit->calc_pos = unit_calc_pos;
  2563. unit->attack_timer_sub = unit_attack_timer_sub;
  2564. unit->skillcastcancel = unit_skillcastcancel;
  2565. unit->dataset = unit_dataset;
  2566. unit->counttargeted = unit_counttargeted;
  2567. unit->fixdamage = unit_fixdamage;
  2568. unit->changeviewsize = unit_changeviewsize;
  2569. unit->remove_map = unit_remove_map;
  2570. unit->remove_map_pc = unit_remove_map_pc;
  2571. unit->free_pc = unit_free_pc;
  2572. unit->free = unit_free;
  2573. }