unit.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/showmsg.h"
  4. #include "../common/timer.h"
  5. #include "../common/nullpo.h"
  6. #include "../common/db.h"
  7. #include "../common/malloc.h"
  8. #include "unit.h"
  9. #include "map.h"
  10. #include "path.h"
  11. #include "pc.h"
  12. #include "mob.h"
  13. #include "pet.h"
  14. #include "homunculus.h"
  15. #include "mercenary.h"
  16. #include "skill.h"
  17. #include "clif.h"
  18. #include "npc.h"
  19. #include "guild.h"
  20. #include "status.h"
  21. #include "battle.h"
  22. #include "chat.h"
  23. #include "trade.h"
  24. #include "vending.h"
  25. #include "party.h"
  26. #include "intif.h"
  27. #include "chrif.h"
  28. #include "script.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. const short dirx[8]={0,-1,-1,-1,0,1,1,1};
  33. const short diry[8]={1,1,0,-1,-1,-1,0,1};
  34. struct unit_data* unit_bl2ud(struct block_list *bl)
  35. {
  36. if( bl == NULL) return NULL;
  37. if( bl->type == BL_PC) return &((struct map_session_data*)bl)->ud;
  38. if( bl->type == BL_MOB) return &((struct mob_data*)bl)->ud;
  39. if( bl->type == BL_PET) return &((struct pet_data*)bl)->ud;
  40. if( bl->type == BL_NPC) return &((struct npc_data*)bl)->ud;
  41. if( bl->type == BL_HOM) return &((struct homun_data*)bl)->ud;
  42. if( bl->type == BL_MER) return &((struct mercenary_data*)bl)->ud;
  43. return NULL;
  44. }
  45. static int unit_attack_timer(int tid, unsigned int tick, int id, intptr data);
  46. static int unit_walktoxy_timer(int tid, unsigned int tick, int id, intptr data);
  47. int unit_walktoxy_sub(struct block_list *bl)
  48. {
  49. int i;
  50. struct walkpath_data wpd;
  51. struct unit_data *ud = NULL;
  52. nullpo_retr(1, bl);
  53. ud = unit_bl2ud(bl);
  54. if(ud == NULL) return 0;
  55. if( !path_search(&wpd,bl->m,bl->x,bl->y,ud->to_x,ud->to_y,ud->state.walk_easy,CELL_CHKNOPASS) )
  56. return 0;
  57. memcpy(&ud->walkpath,&wpd,sizeof(wpd));
  58. if (ud->target && ud->chaserange>1) {
  59. //Generally speaking, the walk path is already to an adjacent tile
  60. //so we only need to shorten the path if the range is greater than 1.
  61. int dir;
  62. //Trim the last part of the path to account for range,
  63. //but always move at least one cell when requested to move.
  64. for (i = ud->chaserange*10; i > 0 && ud->walkpath.path_len>1;) {
  65. ud->walkpath.path_len--;
  66. dir = ud->walkpath.path[ud->walkpath.path_len];
  67. if(dir&1)
  68. i-=14;
  69. else
  70. i-=10;
  71. ud->to_x -= dirx[dir];
  72. ud->to_y -= diry[dir];
  73. }
  74. }
  75. ud->state.change_walk_target=0;
  76. if (bl->type == BL_PC) {
  77. ((TBL_PC *)bl)->head_dir = 0;
  78. clif_walkok((TBL_PC*)bl);
  79. }
  80. clif_move(ud);
  81. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  82. i = -1;
  83. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  84. i = status_get_speed(bl)*14/10;
  85. else
  86. i = status_get_speed(bl);
  87. if( i > 0)
  88. ud->walktimer = add_timer(gettick()+i,unit_walktoxy_timer,bl->id,i);
  89. return 1;
  90. }
  91. static int unit_walktoxy_timer(int tid, unsigned int tick, int id, intptr data)
  92. {
  93. int i;
  94. int x,y,dx,dy;
  95. uint8 dir;
  96. struct block_list *bl;
  97. struct map_session_data *sd;
  98. struct mob_data *md;
  99. struct unit_data *ud;
  100. bl = map_id2bl(id);
  101. if(bl == NULL)
  102. return 0;
  103. sd = BL_CAST(BL_PC, bl);
  104. md = BL_CAST(BL_MOB, bl);
  105. ud = unit_bl2ud(bl);
  106. if(ud == NULL) return 0;
  107. if(ud->walktimer != tid){
  108. ShowError("unit_walk_timer mismatch %d != %d\n",ud->walktimer,tid);
  109. return 0;
  110. }
  111. ud->walktimer = INVALID_TIMER;
  112. if( bl->prev == NULL ) return 0; // block_list から抜けているので移動停止する
  113. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  114. return 0;
  115. if(ud->walkpath.path[ud->walkpath.path_pos]>=8)
  116. return 1;
  117. x = bl->x;
  118. y = bl->y;
  119. dir = ud->walkpath.path[ud->walkpath.path_pos];
  120. ud->dir = dir;
  121. dx = dirx[(int)dir];
  122. dy = diry[(int)dir];
  123. if(map_getcell(bl->m,x+dx,y+dy,CELL_CHKNOPASS))
  124. return unit_walktoxy_sub(bl);
  125. // バシリカ判定
  126. map_foreachinmovearea(clif_outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
  127. x += dx;
  128. y += dy;
  129. map_moveblock(bl, x, y, tick);
  130. ud->walk_count++; //walked cell counter, to be used for walk-triggered skills. [Skotlex]
  131. if (bl->x != x || bl->y != y || ud->walktimer != -1)
  132. return 0; //map_moveblock has altered the object beyond what we expected (moved/warped it)
  133. ud->walktimer = 1; //FIXME: why '1'? [ultramage]
  134. map_foreachinmovearea(clif_insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
  135. ud->walktimer = INVALID_TIMER;
  136. if(sd) {
  137. if(map_getcell(bl->m,x,y,CELL_CHKNPC)) {
  138. npc_touch_areanpc(sd,bl->m,x,y);
  139. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  140. return 0;
  141. } else
  142. sd->areanpc_id=0;
  143. if (sd->state.gmaster_flag &&
  144. (battle_config.guild_aura&(agit_flag?2:1)) &&
  145. (battle_config.guild_aura&(map_flag_gvg2(bl->m)?8:4))
  146. )
  147. { //Guild Aura: Likely needs to be recoded, this method seems inefficient.
  148. struct guild *g = sd->state.gmaster_flag;
  149. int skill, strvit= 0, agidex = 0;
  150. if ((skill = guild_checkskill(g, GD_LEADERSHIP)) > 0) strvit |= (skill&0xFFFF)<<16;
  151. if ((skill = guild_checkskill(g, GD_GLORYWOUNDS)) > 0) strvit |= (skill&0xFFFF);
  152. if ((skill = guild_checkskill(g, GD_SOULCOLD)) > 0) agidex |= (skill&0xFFFF)<<16;
  153. if ((skill = guild_checkskill(g, GD_HAWKEYES)) > 0) agidex |= skill&0xFFFF;
  154. if (strvit || agidex)
  155. map_foreachinrange(skill_guildaura_sub, bl,2, BL_PC,
  156. bl->id, sd->status.guild_id, strvit, agidex);
  157. }
  158. } else if (md) {
  159. if( map_getcell(bl->m,x,y,CELL_CHKNPC) ) {
  160. if( npc_touch_areanpc2(md) ) return 0; // Warped
  161. } else
  162. md->areanpc_id = 0;
  163. if (md->min_chase > md->db->range3) md->min_chase--;
  164. //Walk skills are triggered regardless of target due to the idle-walk mob state.
  165. //But avoid triggering on stop-walk calls.
  166. if(tid != -1 &&
  167. !(ud->walk_count%WALK_SKILL_INTERVAL) &&
  168. mobskill_use(md, tick, -1))
  169. {
  170. if (!(ud->skillid == NPC_SELFDESTRUCTION && ud->skilltimer != -1))
  171. { //Skill used, abort walking
  172. clif_fixpos(bl); //Fix position as walk has been cancelled.
  173. return 0;
  174. }
  175. //Resend walk packet for proper Self Destruction display.
  176. clif_move(ud);
  177. }
  178. }
  179. if(tid == -1) //A directly invoked timer is from battle_stop_walking, therefore the rest is irrelevant.
  180. return 0;
  181. if(ud->state.change_walk_target)
  182. return unit_walktoxy_sub(bl);
  183. ud->walkpath.path_pos++;
  184. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  185. i = -1;
  186. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  187. i = status_get_speed(bl)*14/10;
  188. else
  189. i = status_get_speed(bl);
  190. if(i > 0)
  191. ud->walktimer = add_timer(tick+i,unit_walktoxy_timer,id,i);
  192. else if(ud->state.running) {
  193. //Keep trying to run.
  194. if (!unit_run(bl))
  195. ud->state.running = 0;
  196. }
  197. else if (ud->target) {
  198. //Update target trajectory.
  199. struct block_list *tbl = map_id2bl(ud->target);
  200. if (!tbl || !status_check_visibility(bl, tbl)) { //Cancel chase.
  201. ud->to_x = bl->x;
  202. ud->to_y = bl->y;
  203. if (tbl && bl->type == BL_MOB) //See if the mob can do a warp chase.
  204. mob_warpchase((TBL_MOB*)bl, tbl);
  205. return 0;
  206. }
  207. if (tbl->m == bl->m && check_distance_bl(bl, tbl, ud->chaserange))
  208. { //Reached destination.
  209. if (ud->state.attack_continue)
  210. { //Aegis uses one before every attack, we should
  211. //only need this one for syncing purposes. [Skotlex]
  212. clif_fixpos(bl);
  213. unit_attack(bl, tbl->id, ud->state.attack_continue);
  214. }
  215. } else { //Update chase-path
  216. unit_walktobl(bl, tbl, ud->chaserange, ud->state.walk_easy|(ud->state.attack_continue?2:0));
  217. return 0;
  218. }
  219. }
  220. else { //Stopped walking. Update to_x and to_y to current location [Skotlex]
  221. ud->to_x = bl->x;
  222. ud->to_y = bl->y;
  223. }
  224. return 0;
  225. }
  226. static int unit_delay_walktoxy_timer(int tid, unsigned int tick, int id, intptr data)
  227. {
  228. struct block_list *bl = map_id2bl(id);
  229. if (!bl || bl->prev == NULL)
  230. return 0;
  231. unit_walktoxy(bl, (short)((data>>16)&0xffff), (short)(data&0xffff), 0);
  232. return 1;
  233. }
  234. //flag parameter:
  235. //&1 -> 1/0 = easy/hard
  236. //&2 -> force walking
  237. //&4 -> Delay walking if the reason you can't walk is the canwalk delay
  238. int unit_walktoxy( struct block_list *bl, short x, short y, int flag)
  239. {
  240. struct unit_data* ud = NULL;
  241. struct status_change* sc = NULL;
  242. nullpo_retr(0, bl);
  243. ud = unit_bl2ud(bl);
  244. if( ud == NULL) return 0;
  245. if (flag&4 && DIFF_TICK(ud->canmove_tick, gettick()) > 0 &&
  246. DIFF_TICK(ud->canmove_tick, gettick()) < 2000)
  247. { // Delay walking command. [Skotlex]
  248. add_timer(ud->canmove_tick+1, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF));
  249. return 1;
  250. }
  251. if(!(flag&2) && (!(status_get_mode(bl)&MD_CANMOVE) || !unit_can_move(bl)))
  252. return 0;
  253. ud->state.walk_easy = flag&1;
  254. ud->target = 0;
  255. ud->to_x = x;
  256. ud->to_y = y;
  257. sc = status_get_sc(bl);
  258. if (sc && sc->data[SC_CONFUSION]) //Randomize the target position
  259. map_random_dir(bl, &ud->to_x, &ud->to_y);
  260. if(ud->walktimer != -1) {
  261. // 現在歩いている最中の目的地変更なのでマス目の中心に来た時に
  262. // timer関数からunit_walktoxy_subを呼ぶようにする
  263. ud->state.change_walk_target = 1;
  264. return 1;
  265. }
  266. if(ud->attacktimer != -1) {
  267. delete_timer( ud->attacktimer, unit_attack_timer );
  268. ud->attacktimer = INVALID_TIMER;
  269. }
  270. return unit_walktoxy_sub(bl);
  271. }
  272. //To set Mob's CHASE/FOLLOW states (shouldn't be done if there's no path to reach)
  273. #define set_mobstate(bl, flag) \
  274. if((bl)->type == BL_MOB && (flag)) \
  275. ((TBL_MOB*)(bl))->state.skillstate = ((TBL_MOB*)(bl))->state.aggressive?MSS_FOLLOW:MSS_RUSH;
  276. static int unit_walktobl_sub(int tid, unsigned int tick, int id, intptr data)
  277. {
  278. struct block_list *bl = map_id2bl(id);
  279. struct unit_data *ud = bl?unit_bl2ud(bl):NULL;
  280. if (ud && ud->walktimer == -1 && ud->target == data)
  281. {
  282. if (DIFF_TICK(ud->canmove_tick, tick) > 0) //Keep waiting?
  283. add_timer(ud->canmove_tick+1, unit_walktobl_sub, id, data);
  284. else if (unit_can_move(bl))
  285. {
  286. if (unit_walktoxy_sub(bl))
  287. set_mobstate(bl, ud->state.attack_continue);
  288. }
  289. }
  290. return 0;
  291. }
  292. // Chases a tbl. If the flag&1, use hard-path seek,
  293. // if flag&2, start attacking upon arrival within range, otherwise just walk to that character.
  294. int unit_walktobl(struct block_list *bl, struct block_list *tbl, int range, int flag)
  295. {
  296. struct unit_data *ud = NULL;
  297. struct status_change *sc = NULL;
  298. nullpo_retr(0, bl);
  299. nullpo_retr(0, tbl);
  300. ud = unit_bl2ud(bl);
  301. if( ud == NULL) return 0;
  302. if (!(status_get_mode(bl)&MD_CANMOVE))
  303. return 0;
  304. if (!unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, flag&1, &ud->to_x, &ud->to_y)) {
  305. ud->to_x = bl->x;
  306. ud->to_y = bl->y;
  307. return 0;
  308. }
  309. ud->state.walk_easy = flag&1;
  310. ud->target = tbl->id;
  311. ud->chaserange = range; //Note that if flag&2, this SHOULD be attack-range
  312. ud->state.attack_continue = flag&2?1:0; //Chase to attack.
  313. sc = status_get_sc(bl);
  314. if (sc && sc->data[SC_CONFUSION]) //Randomize the target position
  315. map_random_dir(bl, &ud->to_x, &ud->to_y);
  316. if(ud->walktimer != -1) {
  317. ud->state.change_walk_target = 1;
  318. set_mobstate(bl, flag&2);
  319. return 1;
  320. }
  321. if(DIFF_TICK(ud->canmove_tick, gettick()) > 0)
  322. { //Can't move, wait a bit before invoking the movement.
  323. add_timer(ud->canmove_tick+1, unit_walktobl_sub, bl->id, ud->target);
  324. return 1;
  325. }
  326. if(!unit_can_move(bl))
  327. return 0;
  328. if(ud->attacktimer != -1) {
  329. delete_timer( ud->attacktimer, unit_attack_timer );
  330. ud->attacktimer = INVALID_TIMER;
  331. }
  332. if (unit_walktoxy_sub(bl)) {
  333. set_mobstate(bl, flag&2);
  334. return 1;
  335. }
  336. return 0;
  337. }
  338. #undef set_mobstate
  339. int unit_run(struct block_list *bl)
  340. {
  341. struct status_change *sc = status_get_sc(bl);
  342. short to_x,to_y,dir_x,dir_y;
  343. int lv;
  344. int i;
  345. if (!(sc && sc->data[SC_RUN]))
  346. return 0;
  347. if (!unit_can_move(bl)) {
  348. status_change_end(bl,SC_RUN,-1);
  349. return 0;
  350. }
  351. lv = sc->data[SC_RUN]->val1;
  352. dir_x = dirx[sc->data[SC_RUN]->val2];
  353. dir_y = diry[sc->data[SC_RUN]->val2];
  354. // determine destination cell
  355. to_x = bl->x;
  356. to_y = bl->y;
  357. for(i=0;i<AREA_SIZE;i++)
  358. {
  359. if(!map_getcell(bl->m,to_x+dir_x,to_y+dir_y,CELL_CHKPASS))
  360. break;
  361. //if sprinting and there's a PC/Mob/NPC, block the path [Kevin]
  362. if(sc->data[SC_RUN] && map_count_oncell(bl->m, to_x+dir_x, to_y+dir_y, BL_PC|BL_MOB|BL_NPC))
  363. break;
  364. to_x += dir_x;
  365. to_y += dir_y;
  366. }
  367. if(to_x == bl->x && to_y == bl->y) {
  368. //If you can't run forward, you must be next to a wall, so bounce back. [Skotlex]
  369. clif_status_change(bl, SI_BUMP, 1);
  370. //Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
  371. unit_bl2ud(bl)->state.running = 0;
  372. status_change_end(bl,SC_RUN,-1);
  373. skill_blown(bl,bl,skill_get_blewcount(TK_RUN,lv),unit_getdir(bl),0);
  374. clif_fixpos(bl); //Why is a clif_slide (skill_blown) AND a fixpos needed? Ask Aegis.
  375. clif_status_change(bl, SI_BUMP, 0);
  376. return 0;
  377. }
  378. if (unit_walktoxy(bl, to_x, to_y, 1))
  379. return 1;
  380. //There must be an obstacle nearby. Attempt walking one cell at a time.
  381. do {
  382. to_x -= dir_x;
  383. to_y -= dir_y;
  384. } while (--i > 0 && !unit_walktoxy(bl, to_x, to_y, 1));
  385. if (i==0) {
  386. // copy-paste from above
  387. clif_status_change(bl, SI_BUMP, 1);
  388. //Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
  389. unit_bl2ud(bl)->state.running = 0;
  390. status_change_end(bl,SC_RUN,-1);
  391. skill_blown(bl,bl,skill_get_blewcount(TK_RUN,lv),unit_getdir(bl),0);
  392. clif_fixpos(bl);
  393. clif_status_change(bl, SI_BUMP, 0);
  394. return 0;
  395. }
  396. return 1;
  397. }
  398. //Makes bl attempt to run dist cells away from target. Uses hard-paths.
  399. int unit_escape(struct block_list *bl, struct block_list *target, short dist)
  400. {
  401. int dir = map_calc_dir(target, bl->x, bl->y);
  402. while( dist > 0 && map_getcell(bl->m, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], CELL_CHKNOREACH) )
  403. dist--;
  404. return ( dist > 0 && unit_walktoxy(bl, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], 0) );
  405. }
  406. //Instant warp function.
  407. int unit_movepos(struct block_list *bl, short dst_x, short dst_y, int easy, bool checkpath)
  408. {
  409. short dx,dy;
  410. uint8 dir;
  411. struct unit_data *ud = NULL;
  412. struct map_session_data *sd = NULL;
  413. nullpo_retr(0, bl);
  414. sd = BL_CAST(BL_PC, bl);
  415. ud = unit_bl2ud(bl);
  416. if( ud == NULL) return 0;
  417. unit_stop_walking(bl,1);
  418. unit_stop_attack(bl);
  419. if( checkpath && (map_getcell(bl->m,dst_x,dst_y,CELL_CHKNOPASS) || !path_search(NULL,bl->m,bl->x,bl->y,dst_x,dst_y,easy,CELL_CHKNOREACH)) )
  420. return 0; // unreachable
  421. dir = map_calc_dir(bl, dst_x,dst_y);
  422. ud->dir = dir;
  423. dx = dst_x - bl->x;
  424. dy = dst_y - bl->y;
  425. map_foreachinmovearea(clif_outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
  426. map_moveblock(bl, dst_x, dst_y, gettick());
  427. ud->walktimer = 1; //FIXME: why '1'? [ultramage]
  428. map_foreachinmovearea(clif_insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
  429. ud->walktimer = INVALID_TIMER;
  430. if(sd) {
  431. if(map_getcell(bl->m,bl->x,bl->y,CELL_CHKNPC)) {
  432. npc_touch_areanpc(sd,bl->m,bl->x,bl->y);
  433. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  434. return 0;
  435. } else
  436. sd->areanpc_id=0;
  437. if(sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0)
  438. { //Check if pet needs to be teleported. [Skotlex]
  439. int flag = 0;
  440. struct block_list* bl = &sd->pd->bl;
  441. if( !checkpath && !path_search(NULL,bl->m,bl->x,bl->y,dst_x,dst_y,0,CELL_CHKNOPASS) )
  442. flag = 1;
  443. else if (!check_distance_bl(&sd->bl, bl, AREA_SIZE)) //Too far, teleport.
  444. flag = 2;
  445. if (flag) {
  446. unit_movepos(bl,sd->bl.x,sd->bl.y, 0, 0);
  447. clif_slide(bl,bl->x,bl->y);
  448. }
  449. }
  450. }
  451. return 1;
  452. }
  453. int unit_setdir(struct block_list *bl,unsigned char dir)
  454. {
  455. struct unit_data *ud;
  456. nullpo_retr( 0, bl );
  457. ud = unit_bl2ud(bl);
  458. if (!ud) return 0;
  459. ud->dir = dir;
  460. if (bl->type == BL_PC)
  461. ((TBL_PC *)bl)->head_dir = 0;
  462. clif_changed_dir(bl, AREA);
  463. return 0;
  464. }
  465. uint8 unit_getdir(struct block_list *bl)
  466. {
  467. struct unit_data *ud;
  468. nullpo_retr( 0, bl );
  469. ud = unit_bl2ud(bl);
  470. if (!ud) return 0;
  471. return ud->dir;
  472. }
  473. //Warps a unit/ud to a given map/position.
  474. //In the case of players, pc_setpos is used.
  475. //it respects the no warp flags, so it is safe to call this without doing nowarpto/nowarp checks.
  476. int unit_warp(struct block_list *bl,short m,short x,short y,int type)
  477. {
  478. struct unit_data *ud;
  479. nullpo_retr(0, bl);
  480. ud = unit_bl2ud(bl);
  481. if(bl->prev==NULL || !ud)
  482. return 1;
  483. if (type < 0 || type == 1)
  484. //Type 1 is invalid, since you shouldn't warp a bl with the "death"
  485. //animation, it messes up with unit_remove_map! [Skotlex]
  486. return 1;
  487. if( m<0 ) m=bl->m;
  488. switch (bl->type) {
  489. case BL_MOB:
  490. if (map[bl->m].flag.monster_noteleport)
  491. return 1;
  492. if (m != bl->m && map[m].flag.nobranch && battle_config.mob_warp&4)
  493. return 1;
  494. break;
  495. case BL_PC:
  496. if (map[bl->m].flag.noteleport)
  497. return 1;
  498. break;
  499. }
  500. if (x<0 || y<0)
  501. { //Random map position.
  502. if (!map_search_freecell(NULL, m, &x, &y, -1, -1, 1)) {
  503. ShowWarning("unit_warp failed. Unit Id:%d/Type:%d, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map[m].name, x, y);
  504. return 2;
  505. }
  506. } else if (map_getcell(m,x,y,CELL_CHKNOREACH))
  507. { //Invalid target cell
  508. ShowWarning("unit_warp: Specified non-walkable target cell: %d (%s) at [%d,%d]\n", m, map[m].name, x,y);
  509. if (!map_search_freecell(NULL, m, &x, &y, 4, 4, 1))
  510. { //Can't find a nearby cell
  511. ShowWarning("unit_warp failed. Unit Id:%d/Type:%d, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map[m].name, x, y);
  512. return 2;
  513. }
  514. }
  515. if (bl->type == BL_PC) //Use pc_setpos
  516. return pc_setpos((TBL_PC*)bl, map_id2index(m), x, y, type);
  517. if (!unit_remove_map(bl, type))
  518. return 3;
  519. if (bl->m != m && battle_config.clear_unit_onwarp &&
  520. battle_config.clear_unit_onwarp&bl->type)
  521. skill_clear_unitgroup(bl);
  522. bl->x=ud->to_x=x;
  523. bl->y=ud->to_y=y;
  524. bl->m=m;
  525. map_addblock(bl);
  526. clif_spawn(bl);
  527. skill_unit_move(bl,gettick(),1);
  528. return 0;
  529. }
  530. /*==========================================
  531. * 歩行停止
  532. *------------------------------------------*/
  533. int unit_stop_walking(struct block_list *bl,int type)
  534. {
  535. struct unit_data *ud;
  536. const struct TimerData* td;
  537. unsigned int tick;
  538. nullpo_retr(0, bl);
  539. ud = unit_bl2ud(bl);
  540. if(!ud || ud->walktimer == -1)
  541. return 0;
  542. //NOTE: We are using timer data after deleting it because we know the
  543. //delete_timer function does not messes with it. If the function's
  544. //behaviour changes in the future, this code could break!
  545. td = get_timer(ud->walktimer);
  546. delete_timer(ud->walktimer, unit_walktoxy_timer);
  547. ud->walktimer = INVALID_TIMER;
  548. ud->state.change_walk_target = 0;
  549. tick = gettick();
  550. if ((type&0x02 && !ud->walkpath.path_pos) //Force moving at least one cell.
  551. || (td && DIFF_TICK(td->tick, tick) <= td->data/2)) //Enough time has passed to cover half-cell
  552. {
  553. ud->walkpath.path_len = ud->walkpath.path_pos+1;
  554. unit_walktoxy_timer(-1, tick, bl->id, ud->walkpath.path_pos);
  555. }
  556. if(type&0x01)
  557. clif_fixpos(bl);
  558. ud->walkpath.path_len = 0;
  559. ud->walkpath.path_pos = 0;
  560. ud->to_x = bl->x;
  561. ud->to_y = bl->y;
  562. if(bl->type == BL_PET && type&~0xff)
  563. ud->canmove_tick = gettick() + (type>>8);
  564. //Readded, the check in unit_set_walkdelay means dmg during running won't fall through to this place in code [Kevin]
  565. if (ud->state.running)
  566. status_change_end(bl, SC_RUN, -1);
  567. return 1;
  568. }
  569. int unit_skilluse_id(struct block_list *src, int target_id, short skill_num, short skill_lv)
  570. {
  571. if(skill_num < 0) return 0;
  572. return unit_skilluse_id2(
  573. src, target_id, skill_num, skill_lv,
  574. skill_castfix(src, skill_num, skill_lv),
  575. skill_get_castcancel(skill_num)
  576. );
  577. }
  578. int unit_is_walking(struct block_list *bl)
  579. {
  580. struct unit_data *ud = unit_bl2ud(bl);
  581. nullpo_retr(0, bl);
  582. if(!ud) return 0;
  583. return (ud->walktimer != -1);
  584. }
  585. /*==========================================
  586. * Determines if the bl can move based on status changes. [Skotlex]
  587. *------------------------------------------*/
  588. int unit_can_move(struct block_list *bl)
  589. {
  590. struct map_session_data *sd;
  591. struct unit_data *ud;
  592. struct status_change *sc;
  593. nullpo_retr(0, bl);
  594. ud = unit_bl2ud(bl);
  595. sc = status_get_sc(bl);
  596. sd = BL_CAST(BL_PC, bl);
  597. if (!ud)
  598. return 0;
  599. if (ud->skilltimer != -1 && (!sd || !pc_checkskill(sd, SA_FREECAST) || skill_get_inf2(ud->skillid)&INF2_GUILD_SKILL))
  600. return 0; // prevent moving while casting
  601. if (DIFF_TICK(ud->canmove_tick, gettick()) > 0)
  602. return 0;
  603. if (sd && (
  604. pc_issit(sd) ||
  605. sd->vender_id ||
  606. sd->state.blockedmove
  607. ))
  608. return 0; //Can't move
  609. if (sc) {
  610. if (sc->opt1 > 0 && sc->opt1 != OPT1_STONEWAIT)
  611. return 0;
  612. if ((sc->option & OPTION_HIDE) && (!sd || pc_checkskill(sd, RG_TUNNELDRIVE) <= 0))
  613. return 0;
  614. if (sc->count && (
  615. sc->data[SC_ANKLE]
  616. || sc->data[SC_AUTOCOUNTER]
  617. || sc->data[SC_TRICKDEAD]
  618. || sc->data[SC_BLADESTOP]
  619. || sc->data[SC_BLADESTOP_WAIT]
  620. || sc->data[SC_SPIDERWEB]
  621. || (sc->data[SC_DANCING] && sc->data[SC_DANCING]->val4 && (
  622. !sc->data[SC_LONGING] ||
  623. (sc->data[SC_DANCING]->val1&0xFFFF) == CG_MOONLIT ||
  624. (sc->data[SC_DANCING]->val1&0xFFFF) == CG_HERMODE
  625. ))
  626. || (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF) // cannot move while gospel is in effect
  627. || (sc->data[SC_BASILICA] && sc->data[SC_BASILICA]->val4 == bl->id) // Basilica caster cannot move
  628. || sc->data[SC_STOP]
  629. || sc->data[SC_CLOSECONFINE]
  630. || sc->data[SC_CLOSECONFINE2]
  631. || (sc->data[SC_CLOAKING] && //Need wall at level 1-2
  632. sc->data[SC_CLOAKING]->val1 < 3 && !(sc->data[SC_CLOAKING]->val4&1))
  633. || sc->data[SC_MADNESSCANCEL]
  634. || (sc->data[SC_GRAVITATION] && sc->data[SC_GRAVITATION]->val3 == BCT_SELF)
  635. ))
  636. return 0;
  637. }
  638. return 1;
  639. }
  640. /*==========================================
  641. * Resume running after a walk delay
  642. *------------------------------------------*/
  643. int unit_resume_running(int tid, unsigned int tick, int id, intptr data)
  644. {
  645. struct unit_data *ud = (struct unit_data *)data;
  646. TBL_PC * sd = map_id2sd(id);
  647. clif_skill_nodamage(ud->bl,ud->bl,TK_RUN,ud->skilllv,
  648. sc_start4(ud->bl,status_skill2sc(TK_RUN),100,ud->skilllv,unit_getdir(ud->bl),0,0,0));
  649. if (sd) clif_walkok(sd);
  650. return 0;
  651. }
  652. /*==========================================
  653. * Applies walk delay to character, considering that
  654. * if type is 0, this is a damage induced delay: if previous delay is active, do not change it.
  655. * if type is 1, this is a skill induced delay: walk-delay may only be increased, not decreased.
  656. *------------------------------------------*/
  657. int unit_set_walkdelay(struct block_list *bl, unsigned int tick, int delay, int type)
  658. {
  659. struct unit_data *ud = unit_bl2ud(bl);
  660. if (delay <= 0 || !ud) return 0;
  661. if (type) {
  662. if (DIFF_TICK(ud->canmove_tick, tick+delay) > 0)
  663. return 0;
  664. } else {
  665. if (DIFF_TICK(ud->canmove_tick, tick) > 0)
  666. return 0;
  667. }
  668. ud->canmove_tick = tick + delay;
  669. if (ud->walktimer != -1)
  670. { //Stop walking, if chasing, readjust timers.
  671. if (delay == 1)
  672. { //Minimal delay (walk-delay) disabled. Just stop walking.
  673. unit_stop_walking(bl,0);
  674. } else {
  675. //Resume running after can move again [Kevin]
  676. if(ud->state.running)
  677. {
  678. add_timer(ud->canmove_tick, unit_resume_running, bl->id, (int)ud);
  679. }
  680. else
  681. {
  682. unit_stop_walking(bl,2);
  683. if(ud->target)
  684. add_timer(ud->canmove_tick+1, unit_walktobl_sub, bl->id, ud->target);
  685. }
  686. }
  687. }
  688. return 1;
  689. }
  690. int unit_skilluse_id2(struct block_list *src, int target_id, short skill_num, short skill_lv, int casttime, int castcancel)
  691. {
  692. struct unit_data *ud;
  693. struct status_data *tstatus;
  694. struct status_change *sc;
  695. struct map_session_data *sd = NULL;
  696. struct block_list * target = NULL;
  697. unsigned int tick = gettick();
  698. int temp;
  699. nullpo_retr(0, src);
  700. if(status_isdead(src))
  701. return 0; // 死んでいないか
  702. sd = BL_CAST(BL_PC, src);
  703. ud = unit_bl2ud(src);
  704. if(ud == NULL) return 0;
  705. sc = status_get_sc(src);
  706. if (sc && !sc->count)
  707. sc = NULL; //Unneeded
  708. //temp: used to signal combo-skills right now.
  709. temp = (target_id == src->id && !(sd && sd->state.skill_flag)
  710. && skill_get_inf(skill_num)&INF_SELF_SKILL
  711. && skill_get_inf2(skill_num)&INF2_NO_TARGET_SELF);
  712. if (temp)
  713. target_id = ud->target; //Auto-select skills. [Skotlex]
  714. if (sd) {
  715. //Target_id checking.
  716. if(skillnotok(skill_num, sd)) // [MouseJstr]
  717. return 0;
  718. mob_ksprotected(src, map_id2bl(target_id));
  719. switch(skill_num)
  720. { //Check for skills that auto-select target
  721. case MO_CHAINCOMBO:
  722. if (sc && sc->data[SC_BLADESTOP]){
  723. if ((target=(struct block_list *)sc->data[SC_BLADESTOP]->val4) == NULL)
  724. return 0;
  725. }
  726. break;
  727. case TK_JUMPKICK:
  728. case TK_COUNTER:
  729. case HT_POWER:
  730. if (sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == skill_num)
  731. target_id = sc->data[SC_COMBO]->val2;
  732. break;
  733. case WE_MALE:
  734. case WE_FEMALE:
  735. if (!sd->status.partner_id)
  736. return 0;
  737. target = (struct block_list*)map_charid2sd(sd->status.partner_id);
  738. if (!target) {
  739. clif_skill_fail(sd,skill_num,0,0);
  740. return 0;
  741. }
  742. break;
  743. }
  744. if (target)
  745. target_id = target->id;
  746. }
  747. if (src->type==BL_HOM)
  748. switch(skill_num)
  749. { //Homun-auto-target skills.
  750. case HLIF_HEAL:
  751. case HLIF_AVOID:
  752. case HAMI_DEFENCE:
  753. case HAMI_CASTLE:
  754. target = battle_get_master(src);
  755. if (!target) return 0;
  756. target_id = target->id;
  757. }
  758. if( !target ) // choose default target
  759. target = map_id2bl(target_id);
  760. if( !target || src->m != target->m || !src->prev || !target->prev )
  761. return 0;
  762. //Normally not needed because clif.c checks for it, but the at/char/script commands don't! [Skotlex]
  763. if(ud->skilltimer != -1 && skill_num != SA_CASTCANCEL)
  764. return 0;
  765. if(skill_get_inf2(skill_num)&INF2_NO_TARGET_SELF && src->id == target_id)
  766. return 0;
  767. if(!status_check_skilluse(src, target, skill_num, 0))
  768. return 0;
  769. tstatus = status_get_status_data(target);
  770. //直前のスキル状況の記録
  771. if(sd) {
  772. switch(skill_num){
  773. case SA_CASTCANCEL:
  774. if(ud->skillid != skill_num){
  775. sd->skillid_old = ud->skillid;
  776. sd->skilllv_old = ud->skilllv;
  777. }
  778. break;
  779. case BD_ENCORE:
  780. //Prevent using the dance skill if you no longer have the skill in your tree.
  781. if(!sd->skillid_dance || pc_checkskill(sd,sd->skillid_dance)<=0){
  782. clif_skill_fail(sd,skill_num,0,0);
  783. return 0;
  784. }
  785. sd->skillid_old = skill_num;
  786. break;
  787. case BD_LULLABY:
  788. case BD_RICHMANKIM:
  789. case BD_ETERNALCHAOS:
  790. case BD_DRUMBATTLEFIELD:
  791. case BD_RINGNIBELUNGEN:
  792. case BD_ROKISWEIL:
  793. case BD_INTOABYSS:
  794. case BD_SIEGFRIED:
  795. case CG_MOONLIT:
  796. if (skill_check_pc_partner(sd, skill_num, &skill_lv, 1, 0) < 1)
  797. {
  798. clif_skill_fail(sd,skill_num,0,0);
  799. return 0;
  800. }
  801. break;
  802. }
  803. if (!skill_check_condition(sd, skill_num, skill_lv, 0))
  804. return 0;
  805. }
  806. //TODO: Add type-independant skill_check_condition function.
  807. if (src->type == BL_MOB) {
  808. switch (skill_num) {
  809. case NPC_SUMMONSLAVE:
  810. case NPC_SUMMONMONSTER:
  811. case AL_TELEPORT:
  812. if (((TBL_MOB*)src)->master_id && ((TBL_MOB*)src)->special_state.ai)
  813. return 0;
  814. }
  815. }
  816. //Check range when not using skill on yourself or is a combo-skill during attack
  817. //(these are supposed to always have the same range as your attack)
  818. if(src->id != target_id && (!temp || ud->attacktimer == -1))
  819. {
  820. if (skill_get_state(ud->skillid) == ST_MOVE_ENABLE)
  821. {
  822. if (!unit_can_reach_bl(src, target, skill_get_range2(src, skill_num,skill_lv)+1, 1, NULL, NULL))
  823. return 0; //Walk-path check failed.
  824. } else
  825. if (!battle_check_range(src, target, skill_get_range2(src, skill_num,skill_lv)
  826. +(skill_num==RG_CLOSECONFINE?0:1)))
  827. //Close confine is exploitable thanks to this extra range "feature" of the client. [Skotlex]
  828. return 0; //Arrow-path check failed.
  829. }
  830. if (!temp) //Stop attack on non-combo skills [Skotlex]
  831. unit_stop_attack(src);
  832. else if(ud->attacktimer != -1) //Elsewise, delay current attack sequence
  833. ud->attackabletime = tick + status_get_adelay(src);
  834. ud->state.skillcastcancel = castcancel;
  835. //temp: Used to signal force cast now.
  836. temp = 0;
  837. switch(skill_num){
  838. case ALL_RESURRECTION:
  839. if(battle_check_undead(tstatus->race,tstatus->def_ele)) {
  840. temp = 1;
  841. casttime = skill_castfix(src, PR_TURNUNDEAD, skill_lv);
  842. } else if (!status_isdead(target))
  843. return 0; //Can't cast on non-dead characters.
  844. break;
  845. case MO_FINGEROFFENSIVE:
  846. if(sd)
  847. casttime += casttime * min(skill_lv, sd->spiritball);
  848. break;
  849. case MO_EXTREMITYFIST:
  850. if (sc && sc->data[SC_COMBO] &&
  851. (sc->data[SC_COMBO]->val1 == MO_COMBOFINISH ||
  852. sc->data[SC_COMBO]->val1 == CH_TIGERFIST ||
  853. sc->data[SC_COMBO]->val1 == CH_CHAINCRUSH))
  854. casttime = 0;
  855. temp = 1;
  856. break;
  857. case SA_SPELLBREAKER:
  858. temp = 1;
  859. break;
  860. case ST_CHASEWALK:
  861. if (sc && sc->data[SC_CHASEWALK])
  862. casttime = 0;
  863. break;
  864. case TK_RUN:
  865. if (sc && sc->data[SC_RUN])
  866. casttime = 0;
  867. break;
  868. case HP_BASILICA:
  869. if( sc && sc->data[SC_BASILICA] )
  870. casttime = 0; // No Casting time on basilica cancel
  871. break;
  872. case KN_CHARGEATK:
  873. {
  874. unsigned int k = (distance_bl(src,target)-1)/3; //+100% every 3 cells of distance
  875. if( k > 2 ) k = 2; // ...but hard-limited to 300%.
  876. casttime += casttime * k;
  877. }
  878. break;
  879. }
  880. // moved here to prevent Suffragium from ending if skill fails
  881. if (!(skill_get_castnodex(skill_num, skill_lv)&2))
  882. casttime = skill_castfix_sc(src, casttime);
  883. if( casttime>0 || temp){
  884. clif_skillcasting(src, src->id, target_id, 0,0, skill_num, skill_get_ele(skill_num, skill_lv), casttime);
  885. if (sd && target->type == BL_MOB)
  886. {
  887. TBL_MOB *md = (TBL_MOB*)target;
  888. mobskill_event(md, src, tick, -1); //Cast targetted skill event.
  889. //temp: used to store mob's mode now.
  890. if (tstatus->mode&(MD_CASTSENSOR_IDLE|MD_CASTSENSOR_CHASE) &&
  891. battle_check_target(target, src, BCT_ENEMY) > 0)
  892. {
  893. switch (md->state.skillstate) {
  894. case MSS_RUSH:
  895. case MSS_FOLLOW:
  896. if (!(tstatus->mode&MD_CASTSENSOR_CHASE))
  897. break;
  898. md->target_id = src->id;
  899. md->state.aggressive = (temp&MD_ANGRY)?1:0;
  900. md->min_chase = md->db->range3;
  901. break;
  902. case MSS_IDLE:
  903. case MSS_WALK:
  904. if (!(tstatus->mode&MD_CASTSENSOR_IDLE))
  905. break;
  906. md->target_id = src->id;
  907. md->state.aggressive = (temp&MD_ANGRY)?1:0;
  908. md->min_chase = md->db->range3;
  909. break;
  910. }
  911. }
  912. }
  913. }
  914. if( casttime<=0 )
  915. ud->state.skillcastcancel=0;
  916. ud->canact_tick = tick + casttime + 100;
  917. ud->skilltarget = target_id;
  918. ud->skillx = 0;
  919. ud->skilly = 0;
  920. ud->skillid = skill_num;
  921. ud->skilllv = skill_lv;
  922. if(sc && sc->data[SC_CLOAKING] &&
  923. !(sc->data[SC_CLOAKING]->val4&4) && skill_num != AS_CLOAKING)
  924. {
  925. status_change_end(src,SC_CLOAKING,-1);
  926. if (!src->prev) return 0; //Warped away!
  927. }
  928. if(casttime > 0) {
  929. ud->skilltimer = add_timer( tick+casttime, skill_castend_id, src->id, 0 );
  930. if( sd && pc_checkskill(sd,SA_FREECAST) > 0 )
  931. status_calc_bl(&sd->bl, SCB_SPEED);
  932. else
  933. unit_stop_walking(src,1);
  934. }
  935. else
  936. skill_castend_id(ud->skilltimer,tick,src->id,0);
  937. return 1;
  938. }
  939. int unit_skilluse_pos(struct block_list *src, short skill_x, short skill_y, short skill_num, short skill_lv)
  940. {
  941. if(skill_num < 0)
  942. return 0;
  943. return unit_skilluse_pos2(
  944. src, skill_x, skill_y, skill_num, skill_lv,
  945. skill_castfix(src, skill_num, skill_lv),
  946. skill_get_castcancel(skill_num)
  947. );
  948. }
  949. int unit_skilluse_pos2( struct block_list *src, short skill_x, short skill_y, short skill_num, short skill_lv, int casttime, int castcancel)
  950. {
  951. struct map_session_data *sd = NULL;
  952. struct unit_data *ud = NULL;
  953. struct status_change *sc;
  954. struct block_list bl;
  955. unsigned int tick = gettick();
  956. nullpo_retr(0, src);
  957. if(!src->prev) return 0; // map 上に存在するか
  958. if(status_isdead(src)) return 0;
  959. sd = BL_CAST(BL_PC, src);
  960. ud = unit_bl2ud(src);
  961. if(ud == NULL) return 0;
  962. if(ud->skilltimer != -1) //Normally not needed since clif.c checks for it, but at/char/script commands don't! [Skotlex]
  963. return 0;
  964. sc = status_get_sc(src);
  965. if (sc && !sc->count)
  966. sc = NULL;
  967. if( sd )
  968. {
  969. if( skillnotok(skill_num, sd) || !skill_check_condition(sd, skill_num, skill_lv,0) )
  970. return 0;
  971. }
  972. if (!status_check_skilluse(src, NULL, skill_num, 0))
  973. return 0;
  974. if( map_getcell(src->m, skill_x, skill_y, CELL_CHKWALL) )
  975. {// can't cast ground targeted spells on wall cells
  976. if (sd) clif_skill_fail(sd,skill_num,0,0);
  977. return 0;
  978. }
  979. /* 射程と障害物チェック */
  980. bl.type = BL_NUL;
  981. bl.m = src->m;
  982. bl.x = skill_x;
  983. bl.y = skill_y;
  984. if (skill_get_state(ud->skillid) == ST_MOVE_ENABLE)
  985. {
  986. if (!unit_can_reach_bl(src, &bl, skill_get_range2(src, skill_num,skill_lv)+1, 1, NULL, NULL))
  987. return 0; //Walk-path check failed.
  988. } else
  989. if (!battle_check_range(src,&bl,skill_get_range2(src, skill_num,skill_lv)+1))
  990. return 0; //Arrow-path check failed.
  991. unit_stop_attack(src);
  992. ud->state.skillcastcancel = castcancel;
  993. // moved here to prevent Suffragium from ending if skill fails
  994. if (!(skill_get_castnodex(skill_num, skill_lv)&2))
  995. casttime = skill_castfix_sc(src, casttime);
  996. if( casttime>0 ) {
  997. unit_stop_walking( src, 1);
  998. clif_skillcasting(src, src->id, 0, skill_x, skill_y, skill_num, skill_get_ele(skill_num, skill_lv), casttime);
  999. } else
  1000. ud->state.skillcastcancel=0;
  1001. ud->canact_tick = tick + casttime + 100;
  1002. ud->skillid = skill_num;
  1003. ud->skilllv = skill_lv;
  1004. ud->skillx = skill_x;
  1005. ud->skilly = skill_y;
  1006. ud->skilltarget = 0;
  1007. if (sc && sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4))
  1008. {
  1009. status_change_end(src,SC_CLOAKING,-1);
  1010. if (!src->prev) return 0; //Warped away!
  1011. }
  1012. if(casttime > 0) {
  1013. ud->skilltimer = add_timer( tick+casttime, skill_castend_pos, src->id, 0 );
  1014. if( sd && pc_checkskill(sd,SA_FREECAST) > 0 )
  1015. status_calc_bl(&sd->bl, SCB_SPEED);
  1016. else
  1017. unit_stop_walking(src,1);
  1018. }
  1019. else {
  1020. ud->skilltimer = INVALID_TIMER;
  1021. skill_castend_pos(ud->skilltimer,tick,src->id,0);
  1022. }
  1023. return 1;
  1024. }
  1025. static int unit_attack_timer(int tid, unsigned int tick, int id, intptr data);
  1026. int unit_stop_attack(struct block_list *bl)
  1027. {
  1028. struct unit_data *ud = unit_bl2ud(bl);
  1029. nullpo_retr(0, bl);
  1030. if(!ud || ud->attacktimer == -1)
  1031. return 0;
  1032. delete_timer( ud->attacktimer, unit_attack_timer );
  1033. ud->attacktimer = INVALID_TIMER;
  1034. ud->target = 0;
  1035. return 0;
  1036. }
  1037. //Means current target is unattackable. For now only unlocks mobs.
  1038. int unit_unattackable(struct block_list *bl)
  1039. {
  1040. struct unit_data *ud = unit_bl2ud(bl);
  1041. if (ud) {
  1042. ud->target = 0;
  1043. ud->state.attack_continue = 0;
  1044. }
  1045. if(bl->type == BL_MOB)
  1046. mob_unlocktarget((struct mob_data*)bl, gettick()) ;
  1047. else if(bl->type == BL_PET)
  1048. pet_unlocktarget((struct pet_data*)bl);
  1049. return 0;
  1050. }
  1051. /*==========================================
  1052. * 攻撃要求
  1053. * typeが1なら継続攻撃
  1054. *------------------------------------------*/
  1055. int unit_attack(struct block_list *src,int target_id,int continuous)
  1056. {
  1057. struct block_list *target;
  1058. struct unit_data *ud;
  1059. nullpo_retr(0, ud = unit_bl2ud(src));
  1060. target = map_id2bl(target_id);
  1061. if( target==NULL || status_isdead(target) )
  1062. {
  1063. unit_unattackable(src);
  1064. return 1;
  1065. }
  1066. if( src->type == BL_PC )
  1067. {
  1068. TBL_PC* sd = (TBL_PC*)src;
  1069. if( target->type == BL_NPC )
  1070. { // monster npcs [Valaris]
  1071. npc_click(sd,(TBL_NPC*)target); // submitted by leinsirk10 [Celest]
  1072. return 0;
  1073. }
  1074. else if( pc_is90overweight(sd) )
  1075. { // overwheight - stop attacking and walking
  1076. unit_stop_attack(src);
  1077. unit_stop_walking(src,1);
  1078. return 0;
  1079. }
  1080. }
  1081. if( battle_check_target(src,target,BCT_ENEMY) <= 0 || !status_check_skilluse(src, target, 0, 0) )
  1082. {
  1083. unit_unattackable(src);
  1084. return 1;
  1085. }
  1086. ud->target = target_id;
  1087. ud->state.attack_continue = continuous;
  1088. if (continuous) //If you're to attack continously, set to auto-case character
  1089. ud->chaserange = status_get_range(src);
  1090. //Just change target/type. [Skotlex]
  1091. if(ud->attacktimer != -1)
  1092. return 0;
  1093. //Set Mob's ANGRY/BERSERK states.
  1094. if(src->type == BL_MOB)
  1095. ((TBL_MOB*)src)->state.skillstate = ((TBL_MOB*)src)->state.aggressive?MSS_ANGRY:MSS_BERSERK;
  1096. if(DIFF_TICK(ud->attackabletime, gettick()) > 0)
  1097. //Do attack next time it is possible. [Skotlex]
  1098. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1099. else //Attack NOW.
  1100. unit_attack_timer(-1,gettick(),src->id,0);
  1101. return 0;
  1102. }
  1103. //Cancels an ongoing combo, resets attackable time and restarts the
  1104. //attack timer to resume attacking after amotion time. [Skotlex]
  1105. int unit_cancel_combo(struct block_list *bl)
  1106. {
  1107. struct unit_data *ud;
  1108. if (!status_change_end(bl, SC_COMBO, -1))
  1109. return 0; //Combo wasn't active.
  1110. ud = unit_bl2ud(bl);
  1111. nullpo_retr(0, ud);
  1112. ud->attackabletime = gettick() + status_get_amotion(bl);
  1113. if (ud->attacktimer == -1)
  1114. return 1; //Nothing more to do.
  1115. delete_timer(ud->attacktimer, unit_attack_timer);
  1116. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,bl->id,0);
  1117. return 1;
  1118. }
  1119. /*==========================================
  1120. *
  1121. *------------------------------------------*/
  1122. bool unit_can_reach_pos(struct block_list *bl,int x,int y, int easy)
  1123. {
  1124. nullpo_retr(false, bl);
  1125. if( bl->x==x && bl->y==y ) // 同じマス
  1126. return true;
  1127. return path_search(NULL,bl->m,bl->x,bl->y,x,y,easy,CELL_CHKNOREACH);
  1128. }
  1129. /*==========================================
  1130. *
  1131. *------------------------------------------*/
  1132. bool unit_can_reach_bl(struct block_list *bl,struct block_list *tbl, int range, int easy, short *x, short *y)
  1133. {
  1134. int i;
  1135. short dx,dy;
  1136. nullpo_retr(false, bl);
  1137. nullpo_retr(false, tbl);
  1138. if( bl->m != tbl->m)
  1139. return false;
  1140. if( bl->x==tbl->x && bl->y==tbl->y )
  1141. return true;
  1142. if(range>0 && !check_distance_bl(bl, tbl, range))
  1143. return false;
  1144. // It judges whether it can adjoin or not.
  1145. dx=tbl->x - bl->x;
  1146. dy=tbl->y - bl->y;
  1147. dx=(dx>0)?1:((dx<0)?-1:0);
  1148. dy=(dy>0)?1:((dy<0)?-1:0);
  1149. if (map_getcell(tbl->m,tbl->x-dx,tbl->y-dy,CELL_CHKNOPASS))
  1150. { //Look for a suitable cell to place in.
  1151. for(i=0;i<9 && map_getcell(tbl->m,tbl->x-dirx[i],tbl->y-diry[i],CELL_CHKNOPASS);i++);
  1152. if (i==9) return false; //No valid cells.
  1153. dx = dirx[i];
  1154. dy = diry[i];
  1155. }
  1156. if (x) *x = tbl->x-dx;
  1157. if (y) *y = tbl->y-dy;
  1158. return path_search(NULL,bl->m,bl->x,bl->y,tbl->x-dx,tbl->y-dy,easy,CELL_CHKNOREACH);
  1159. }
  1160. /*==========================================
  1161. * Calculates position of Pet/Mercenary/Homunculus
  1162. *------------------------------------------*/
  1163. int unit_calc_pos(struct block_list *bl, int tx, int ty, int dir)
  1164. {
  1165. int dx, dy, x, y, i, k;
  1166. struct unit_data *ud = unit_bl2ud(bl);
  1167. nullpo_retr(0, ud);
  1168. if( dir < 0 || dir > 7 )
  1169. return 1;
  1170. ud->to_x = tx;
  1171. ud->to_y = ty;
  1172. // 2 cells from Master Position
  1173. dx = -dirx[dir] * 2;
  1174. dy = -diry[dir] * 2;
  1175. x = tx + dx;
  1176. y = ty + dy;
  1177. if( !unit_can_reach_pos(bl, x, y, 0) )
  1178. {
  1179. if( dx > 0 ) x--; else if( dx < 0 ) x++;
  1180. if( dy > 0 ) y--; else if( dy < 0 ) y++;
  1181. if( !unit_can_reach_pos(bl, x, y, 0) )
  1182. {
  1183. for( i = 0; i < 12; i++ )
  1184. {
  1185. k = rand()%8; // Pick a Random Dir
  1186. dx = -dirx[k] * 2;
  1187. dy = -diry[k] * 2;
  1188. x = tx + dx;
  1189. y = ty + dy;
  1190. if( unit_can_reach_pos(bl, x, y, 0) )
  1191. break;
  1192. else
  1193. {
  1194. if( dx > 0 ) x--; else if( dx < 0 ) x++;
  1195. if( dy > 0 ) y--; else if( dy < 0 ) y++;
  1196. if( unit_can_reach_pos(bl, x, y, 0) )
  1197. break;
  1198. }
  1199. }
  1200. if( i == 12 )
  1201. {
  1202. x = tx; y = tx; // Exactly Master Position
  1203. if( !unit_can_reach_pos(bl, x, y, 0) )
  1204. return 1;
  1205. }
  1206. }
  1207. }
  1208. ud->to_x = x;
  1209. ud->to_y = y;
  1210. return 0;
  1211. }
  1212. /*==========================================
  1213. * PCの攻撃 (timer関数)
  1214. *------------------------------------------*/
  1215. static int unit_attack_timer_sub(struct block_list* src, int tid, unsigned int tick)
  1216. {
  1217. struct block_list *target;
  1218. struct unit_data *ud;
  1219. struct status_data *sstatus;
  1220. struct map_session_data *sd = NULL;
  1221. struct mob_data *md = NULL;
  1222. int range;
  1223. if( (ud=unit_bl2ud(src))==NULL )
  1224. return 0;
  1225. if( ud->attacktimer != tid )
  1226. {
  1227. ShowError("unit_attack_timer %d != %d\n",ud->attacktimer,tid);
  1228. return 0;
  1229. }
  1230. sd = BL_CAST(BL_PC, src);
  1231. md = BL_CAST(BL_MOB, src);
  1232. ud->attacktimer = INVALID_TIMER;
  1233. target=map_id2bl(ud->target);
  1234. if( src == NULL || src->prev == NULL || target==NULL || target->prev == NULL )
  1235. return 0;
  1236. if( status_isdead(src) || status_isdead(target) || !status_check_skilluse(src, target, 0, 0) )
  1237. return 0; // can't attack under these conditions
  1238. if( src->m != target->m )
  1239. {
  1240. if( src->type == BL_MOB && mob_warpchase((TBL_MOB*)src, target) )
  1241. return 1; // Follow up.
  1242. return 0;
  1243. }
  1244. if( ud->skilltimer != -1 && !(sd && pc_checkskill(sd,SA_FREECAST) > 0) )
  1245. return 0; // can't attack while casting
  1246. if( !battle_config.sdelay_attack_enable && DIFF_TICK(ud->canact_tick,tick) > 0 && !(sd && pc_checkskill(sd,SA_FREECAST) > 0) )
  1247. { // attacking when under cast delay has restrictions:
  1248. if( tid == -1 )
  1249. { //requested attack.
  1250. if(sd) clif_skill_fail(sd,1,4,0);
  1251. return 0;
  1252. }
  1253. //Otherwise, we are in a combo-attack, delay this until your canact time is over. [Skotlex]
  1254. if( ud->state.attack_continue )
  1255. {
  1256. if( DIFF_TICK(ud->canact_tick, ud->attackabletime) > 0 )
  1257. ud->attackabletime = ud->canact_tick;
  1258. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1259. }
  1260. return 1;
  1261. }
  1262. sstatus = status_get_status_data(src);
  1263. range = sstatus->rhw.range;
  1264. if( !sd || sd->status.weapon != W_BOW )
  1265. range++; //Dunno why everyone but bows gets this extra range...
  1266. if( unit_is_walking(target) )
  1267. range++; //Extra range when chasing
  1268. if( !check_distance_bl(src,target,range) )
  1269. { //Chase if required.
  1270. if(sd)
  1271. clif_movetoattack(sd,target);
  1272. else if(ud->state.attack_continue)
  1273. unit_walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1274. return 1;
  1275. }
  1276. if( !battle_check_range(src,target,range) )
  1277. {
  1278. //Within range, but no direct line of attack
  1279. if( ud->state.attack_continue )
  1280. {
  1281. if(ud->chaserange > 2) ud->chaserange-=2;
  1282. unit_walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1283. }
  1284. return 1;
  1285. }
  1286. //Sync packet only for players.
  1287. //Non-players use the sync packet on the walk timer. [Skotlex]
  1288. if (tid == -1 && sd) clif_fixpos(src);
  1289. if( DIFF_TICK(ud->attackabletime,tick) <= 0 )
  1290. {
  1291. if (battle_config.attack_direction_change && (src->type&battle_config.attack_direction_change)) {
  1292. ud->dir = map_calc_dir(src, target->x,target->y );
  1293. }
  1294. if(ud->walktimer != -1)
  1295. unit_stop_walking(src,1);
  1296. if(md) {
  1297. if (mobskill_use(md,tick,-1))
  1298. return 1;
  1299. if (sstatus->mode&MD_ASSIST && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME)
  1300. { // Link monsters nearby [Skotlex]
  1301. md->last_linktime = tick;
  1302. map_foreachinrange(mob_linksearch, src, md->db->range2, BL_MOB, md->class_, target, tick);
  1303. }
  1304. }
  1305. if(src->type == BL_PET && pet_attackskill((TBL_PET*)src, target->id))
  1306. return 1;
  1307. map_freeblock_lock();
  1308. ud->attacktarget_lv = battle_weapon_attack(src,target,tick,0);
  1309. if(sd && sd->status.pet_id > 0 && sd->pd && battle_config.pet_attack_support)
  1310. pet_target_check(sd,target,0);
  1311. map_freeblock_unlock();
  1312. ud->attackabletime = tick + sstatus->adelay;
  1313. // You can't move if you can't attack neither.
  1314. if (src->type&battle_config.attack_walk_delay)
  1315. unit_set_walkdelay(src, tick, sstatus->amotion, 1);
  1316. }
  1317. if(ud->state.attack_continue)
  1318. ud->attacktimer = add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1319. return 1;
  1320. }
  1321. static int unit_attack_timer(int tid, unsigned int tick, int id, intptr data)
  1322. {
  1323. struct block_list *bl;
  1324. bl = map_id2bl(id);
  1325. if(bl && unit_attack_timer_sub(bl, tid, tick) == 0)
  1326. unit_unattackable(bl);
  1327. return 0;
  1328. }
  1329. /*==========================================
  1330. * Cancels an ongoing skill cast.
  1331. * flag&1: Cast-Cancel invoked.
  1332. * flag&2: Cancel only if skill is cancellable.
  1333. *------------------------------------------*/
  1334. int unit_skillcastcancel(struct block_list *bl,int type)
  1335. {
  1336. struct map_session_data *sd = NULL;
  1337. struct unit_data *ud = unit_bl2ud( bl);
  1338. unsigned int tick=gettick();
  1339. int ret=0, skill;
  1340. nullpo_retr(0, bl);
  1341. if (!ud || ud->skilltimer==-1)
  1342. return 0; //Nothing to cancel.
  1343. sd = BL_CAST(BL_PC, bl);
  1344. if (type&2) {
  1345. //See if it can be cancelled.
  1346. if (!ud->state.skillcastcancel)
  1347. return 0;
  1348. if (sd && (sd->special_state.no_castcancel2 ||
  1349. (sd->special_state.no_castcancel && !map_flag_gvg(bl->m)))) //fixed flags being read the wrong way around [blackhole89]
  1350. return 0;
  1351. }
  1352. ud->canact_tick = tick;
  1353. if(type&1 && sd)
  1354. skill = sd->skillid_old;
  1355. else
  1356. skill = ud->skillid;
  1357. if (skill_get_inf(skill) & INF_GROUND_SKILL)
  1358. ret=delete_timer( ud->skilltimer, skill_castend_pos );
  1359. else
  1360. ret=delete_timer( ud->skilltimer, skill_castend_id );
  1361. if(ret<0)
  1362. ShowError("delete timer error : skillid : %d\n",ret);
  1363. ud->skilltimer = INVALID_TIMER;
  1364. if( sd && pc_checkskill(sd,SA_FREECAST) > 0 )
  1365. status_calc_bl(&sd->bl, SCB_SPEED);
  1366. if(bl->type==BL_MOB) ((TBL_MOB*)bl)->skillidx = -1;
  1367. clif_skillcastcancel(bl);
  1368. return 1;
  1369. }
  1370. // unit_data の初期化処理
  1371. void unit_dataset(struct block_list *bl)
  1372. {
  1373. struct unit_data *ud;
  1374. nullpo_retv(ud = unit_bl2ud(bl));
  1375. memset( ud, 0, sizeof( struct unit_data) );
  1376. ud->bl = bl;
  1377. ud->walktimer = -1;
  1378. ud->skilltimer = -1;
  1379. ud->attacktimer = -1;
  1380. ud->attackabletime =
  1381. ud->canact_tick =
  1382. ud->canmove_tick = gettick();
  1383. }
  1384. /*==========================================
  1385. * Returns 1 if this unit is attacking target 'id'
  1386. *------------------------------------------*/
  1387. static int unit_counttargeted_sub(struct block_list* bl, va_list ap)
  1388. {
  1389. int id = va_arg(ap, int);
  1390. int target_lv = va_arg(ap, int); // extra condition
  1391. struct unit_data* ud;
  1392. if(bl->id == id)
  1393. return 0;
  1394. ud = unit_bl2ud(bl);
  1395. if (ud && ud->target == id && ud->attacktimer != -1 && ud->attacktarget_lv >= target_lv)
  1396. return 1;
  1397. return 0;
  1398. }
  1399. /*==========================================
  1400. * Counts the number of units attacking 'bl'
  1401. *------------------------------------------*/
  1402. int unit_counttargeted(struct block_list* bl, int target_lv)
  1403. {
  1404. nullpo_retr(0, bl);
  1405. return (map_foreachinrange(unit_counttargeted_sub, bl, AREA_SIZE, BL_CHAR, bl->id, target_lv));
  1406. }
  1407. /*==========================================
  1408. *
  1409. *------------------------------------------*/
  1410. int unit_fixdamage(struct block_list *src,struct block_list *target,unsigned int tick,int sdelay,int ddelay,int damage,int div,int type,int damage2)
  1411. {
  1412. nullpo_retr(0, target);
  1413. if(damage+damage2 <= 0)
  1414. return 0;
  1415. return status_fix_damage(src,target,damage+damage2,clif_damage(target,target,tick,sdelay,ddelay,damage,div,type,damage2));
  1416. }
  1417. /*==========================================
  1418. * 見た目のサイズを変更する
  1419. *------------------------------------------*/
  1420. int unit_changeviewsize(struct block_list *bl,short size)
  1421. {
  1422. nullpo_retr(0, bl);
  1423. size=(size<0)?-1:(size>0)?1:0;
  1424. if(bl->type == BL_PC) {
  1425. ((TBL_PC*)bl)->state.size=size;
  1426. } else if(bl->type == BL_MOB) {
  1427. ((TBL_MOB*)bl)->special_state.size=size;
  1428. } else
  1429. return 0;
  1430. if(size!=0)
  1431. clif_misceffect2(bl,421+size);
  1432. return 0;
  1433. }
  1434. /*==========================================
  1435. * Removes a bl/ud from the map.
  1436. * Returns 1 on success. 0 if it couldn't be removed or the bl was free'd
  1437. * if clrtype is 1 (death), appropiate cleanup is performed.
  1438. * Otherwise it is assumed bl is being warped.
  1439. * On-Kill specific stuff is not performed here, look at status_damage for that.
  1440. *------------------------------------------*/
  1441. int unit_remove_map_(struct block_list *bl, int clrtype, const char* file, int line, const char* func)
  1442. {
  1443. struct unit_data *ud = unit_bl2ud(bl);
  1444. struct status_change *sc = status_get_sc(bl);
  1445. nullpo_retr(0, ud);
  1446. if(bl->prev == NULL)
  1447. return 0; //Already removed?
  1448. map_freeblock_lock();
  1449. ud->target = 0; //Unlock walk/attack target.
  1450. if (ud->walktimer != -1)
  1451. unit_stop_walking(bl,0);
  1452. if (ud->attacktimer != -1)
  1453. unit_stop_attack(bl);
  1454. if (ud->skilltimer != -1)
  1455. unit_skillcastcancel(bl,0);
  1456. // Do not reset can-act delay. [Skotlex]
  1457. ud->attackabletime = ud->canmove_tick /*= ud->canact_tick*/ = gettick();
  1458. if(sc && sc->count ) { //map-change/warp dispells.
  1459. status_change_end(bl,SC_BLADESTOP,-1);
  1460. status_change_end(bl,SC_BASILICA,-1);
  1461. status_change_end(bl,SC_ANKLE,-1);
  1462. status_change_end(bl,SC_TRICKDEAD,-1);
  1463. status_change_end(bl,SC_BLADESTOP,-1);
  1464. status_change_end(bl,SC_RUN,-1);
  1465. skill_stop_dancing(bl);
  1466. status_change_end(bl,SC_WARM,-1);
  1467. status_change_end(bl,SC_DEVOTION,-1);
  1468. status_change_end(bl,SC_MARIONETTE,-1);
  1469. status_change_end(bl,SC_MARIONETTE2,-1);
  1470. status_change_end(bl,SC_CLOSECONFINE,-1);
  1471. status_change_end(bl,SC_CLOSECONFINE2,-1);
  1472. status_change_end(bl,SC_HIDING,-1);
  1473. status_change_end(bl,SC_CLOAKING,-1);
  1474. status_change_end(bl,SC_CHASEWALK,-1);
  1475. if (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF)
  1476. status_change_end(bl,SC_GOSPEL,-1);
  1477. status_change_end(bl,SC_CHANGE,-1);
  1478. status_change_end(bl,SC_MIRACLE,-1);
  1479. }
  1480. if (bl->type&BL_CHAR) {
  1481. skill_unit_move(bl,gettick(),4);
  1482. skill_cleartimerskill(bl);
  1483. }
  1484. switch( bl->type )
  1485. {
  1486. case BL_PC:
  1487. {
  1488. struct map_session_data *sd = (struct map_session_data*)bl;
  1489. //Leave/reject all invitations.
  1490. if(sd->chatID)
  1491. chat_leavechat(sd,0);
  1492. if(sd->trade_partner)
  1493. trade_tradecancel(sd);
  1494. if(sd->vender_id)
  1495. vending_closevending(sd);
  1496. if(sd->state.storage_flag == 1)
  1497. storage_storage_quit(sd,0);
  1498. else if (sd->state.storage_flag == 2)
  1499. storage_guild_storage_quit(sd,0);
  1500. sd->state.storage_flag = 0; //Force close it when being warped.
  1501. if(sd->party_invite>0)
  1502. party_reply_invite(sd,sd->party_invite_account,0);
  1503. if(sd->guild_invite>0)
  1504. guild_reply_invite(sd,sd->guild_invite,0);
  1505. if(sd->guild_alliance>0)
  1506. guild_reply_reqalliance(sd,sd->guild_alliance_account,0);
  1507. if(sd->menuskill_id)
  1508. sd->menuskill_id = sd->menuskill_val = 0;
  1509. sd->npc_shopid = 0;
  1510. sd->adopt_invite = 0;
  1511. if(sd->pvp_timer!=-1) {
  1512. delete_timer(sd->pvp_timer,pc_calc_pvprank_timer);
  1513. sd->pvp_timer = INVALID_TIMER;
  1514. sd->pvp_rank = 0;
  1515. }
  1516. if(sd->duel_group > 0)
  1517. duel_leave(sd->duel_group, sd);
  1518. if(pc_issit(sd)) {
  1519. pc_setstand(sd);
  1520. skill_sit(sd,0);
  1521. }
  1522. party_send_dot_remove(sd);//minimap dot fix [Kevin]
  1523. guild_send_dot_remove(sd);
  1524. if( map[bl->m].users <= 0 || sd->state.debug_remove_map )
  1525. {// 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]
  1526. if( sd->debug_file == NULL || !(sd->state.debug_remove_map) )
  1527. {
  1528. sd->debug_file = "";
  1529. sd->debug_line = 0;
  1530. sd->debug_func = "";
  1531. }
  1532. ShowDebug("unit_remove_map: unexpected state when removing player AID/CID:%d/%d"
  1533. " (active=%d connect_new=%d rewarp=%d changemap=%d debug_remove_map=%d)"
  1534. " from map=%s (users=%d)."
  1535. " Previous call from %s:%d(%s), current call from %s:%d(%s)."
  1536. " Please report this!!!\n",
  1537. sd->status.account_id, sd->status.char_id,
  1538. sd->state.active, sd->state.connect_new, sd->state.rewarp, sd->state.changemap, sd->state.debug_remove_map,
  1539. map[bl->m].name, map[bl->m].users,
  1540. sd->debug_file, sd->debug_line, sd->debug_func, file, line, func);
  1541. }
  1542. else
  1543. if (--map[bl->m].users == 0 && battle_config.dynamic_mobs) //[Skotlex]
  1544. map_removemobs(bl->m);
  1545. sd->state.debug_remove_map = 1; // temporary state to track double remove_map's [FlavioJS]
  1546. sd->debug_file = file;
  1547. sd->debug_line = line;
  1548. sd->debug_func = func;
  1549. break;
  1550. }
  1551. case BL_MOB:
  1552. {
  1553. struct mob_data *md = (struct mob_data*)bl;
  1554. md->target_id=0;
  1555. md->attacked_id=0;
  1556. md->state.skillstate= MSS_IDLE;
  1557. break;
  1558. }
  1559. case BL_PET:
  1560. {
  1561. struct pet_data *pd = (struct pet_data*)bl;
  1562. if( pd->pet.intimate <= 0 && !(pd->msd && !pd->msd->state.active) )
  1563. { //If logging out, this is deleted on unit_free
  1564. clif_clearunit_area(bl,clrtype);
  1565. map_delblock(bl);
  1566. unit_free(bl,0);
  1567. map_freeblock_unlock();
  1568. return 0;
  1569. }
  1570. break;
  1571. }
  1572. case BL_HOM:
  1573. {
  1574. struct homun_data *hd = (struct homun_data *)bl;
  1575. ud->canact_tick = ud->canmove_tick; //It appears HOM do reset the can-act tick.
  1576. if( !hd->homunculus.intimacy && !(hd->master && !hd->master->state.active) )
  1577. { //If logging out, this is deleted on unit_free
  1578. clif_emotion(bl, 28) ; //sob
  1579. clif_clearunit_area(bl,clrtype);
  1580. map_delblock(bl);
  1581. unit_free(bl,0);
  1582. map_freeblock_unlock();
  1583. return 0;
  1584. }
  1585. break;
  1586. }
  1587. case BL_MER:
  1588. {
  1589. struct mercenary_data *md = (struct mercenary_data *)bl;
  1590. ud->canact_tick = ud->canmove_tick;
  1591. if( !md->mercenary.remain_life_time && !(md->master && !md->master->state.active) )
  1592. {
  1593. clif_clearunit_area(bl,clrtype);
  1594. map_delblock(bl);
  1595. unit_free(bl,0);
  1596. map_freeblock_unlock();
  1597. return 0;
  1598. }
  1599. break;
  1600. }
  1601. default: ;// do nothing
  1602. }
  1603. clif_clearunit_area(bl,clrtype);
  1604. map_delblock(bl);
  1605. map_freeblock_unlock();
  1606. return 1;
  1607. }
  1608. void unit_remove_map_pc(struct map_session_data *sd, int clrtype)
  1609. {
  1610. unit_remove_map(&sd->bl,clrtype);
  1611. if (clrtype == 3) clrtype = 0; //3 is the warp from logging out, but pets/homunc need to just 'vanish' instead of showing the warping out animation.
  1612. if(sd->pd)
  1613. unit_remove_map(&sd->pd->bl, clrtype);
  1614. if(merc_is_hom_active(sd->hd))
  1615. unit_remove_map(&sd->hd->bl, clrtype);
  1616. if(sd->md)
  1617. unit_remove_map(&sd->md->bl, clrtype);
  1618. }
  1619. void unit_free_pc(struct map_session_data *sd)
  1620. {
  1621. if (sd->pd) unit_free(&sd->pd->bl,0);
  1622. if (sd->hd) unit_free(&sd->hd->bl,0);
  1623. if (sd->md) unit_free(&sd->md->bl,0);
  1624. unit_free(&sd->bl,3);
  1625. }
  1626. /*==========================================
  1627. * Function to free all related resources to the bl
  1628. * if unit is on map, it is removed using the clrtype specified
  1629. * If clrtype is <0, no saving is performed. This is only for non-authed
  1630. * objects that shouldn't be on a map yet.
  1631. *------------------------------------------*/
  1632. int unit_free(struct block_list *bl, int clrtype)
  1633. {
  1634. struct unit_data *ud = unit_bl2ud( bl );
  1635. nullpo_retr(0, ud);
  1636. map_freeblock_lock();
  1637. if( bl->prev ) //Players are supposed to logout with a "warp" effect.
  1638. unit_remove_map(bl, clrtype);
  1639. switch( bl->type )
  1640. {
  1641. case BL_PC:
  1642. {
  1643. struct map_session_data *sd = (struct map_session_data*)bl;
  1644. if( status_isdead(bl) )
  1645. pc_setrestartvalue(sd,2);
  1646. pc_delinvincibletimer(sd);
  1647. pc_autoscript_clear(sd->autoscript, ARRAYLENGTH(sd->autoscript));
  1648. pc_autoscript_clear(sd->autoscript2, ARRAYLENGTH(sd->autoscript2));
  1649. if( sd->followtimer != -1 )
  1650. pc_stop_following(sd);
  1651. if( sd->duel_invite > 0 )
  1652. duel_reject(sd->duel_invite, sd);
  1653. // Notify friends that this char logged out. [Skotlex]
  1654. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
  1655. party_send_logout(sd);
  1656. guild_send_memberinfoshort(sd,0);
  1657. pc_cleareventtimer(sd);
  1658. pc_delspiritball(sd,sd->spiritball,1);
  1659. if( sd->reg )
  1660. { //Double logout already freed pointer fix... [Skotlex]
  1661. aFree(sd->reg);
  1662. sd->reg = NULL;
  1663. sd->reg_num = 0;
  1664. }
  1665. if( sd->regstr )
  1666. {
  1667. int i;
  1668. for( i = 0; i < sd->regstr_num; ++i )
  1669. if( sd->regstr[i].data )
  1670. aFree(sd->regstr[i].data);
  1671. aFree(sd->regstr);
  1672. sd->regstr = NULL;
  1673. sd->regstr_num = 0;
  1674. }
  1675. //Tell the script to end, not delete it, it will free itself when necessary [Kevin]
  1676. if( sd->st )
  1677. {
  1678. sd->st->rid = 0;
  1679. sd->st->state = END;
  1680. }
  1681. break;
  1682. }
  1683. case BL_PET:
  1684. {
  1685. struct pet_data *pd = (struct pet_data*)bl;
  1686. struct map_session_data *sd = pd->msd;
  1687. pet_hungry_timer_delete(pd);
  1688. if( pd->a_skill )
  1689. {
  1690. aFree(pd->a_skill);
  1691. pd->a_skill = NULL;
  1692. }
  1693. if( pd->s_skill )
  1694. {
  1695. if (pd->s_skill->timer != -1) {
  1696. if (pd->s_skill->id)
  1697. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  1698. else
  1699. delete_timer(pd->s_skill->timer, pet_heal_timer);
  1700. }
  1701. aFree(pd->s_skill);
  1702. pd->s_skill = NULL;
  1703. }
  1704. if( pd->recovery )
  1705. {
  1706. if(pd->recovery->timer != -1)
  1707. delete_timer(pd->recovery->timer, pet_recovery_timer);
  1708. aFree(pd->recovery);
  1709. pd->recovery = NULL;
  1710. }
  1711. if( pd->bonus )
  1712. {
  1713. if (pd->bonus->timer != -1)
  1714. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  1715. aFree(pd->bonus);
  1716. pd->bonus = NULL;
  1717. }
  1718. if( pd->loot )
  1719. {
  1720. pet_lootitem_drop(pd,sd);
  1721. if (pd->loot->item)
  1722. aFree(pd->loot->item);
  1723. aFree (pd->loot);
  1724. pd->loot = NULL;
  1725. }
  1726. if( clrtype >= 0 )
  1727. {
  1728. if( pd->pet.intimate > 0 )
  1729. intif_save_petdata(pd->pet.account_id,&pd->pet);
  1730. else
  1731. { //Remove pet.
  1732. intif_delete_petdata(pd->pet.pet_id);
  1733. if (sd) sd->status.pet_id = 0;
  1734. }
  1735. }
  1736. if( sd )
  1737. sd->pd = NULL;
  1738. break;
  1739. }
  1740. case BL_MOB:
  1741. {
  1742. struct mob_data *md = (struct mob_data*)bl;
  1743. if( md->deletetimer != -1 )
  1744. {
  1745. delete_timer(md->deletetimer,mob_timer_delete);
  1746. md->deletetimer = INVALID_TIMER;
  1747. }
  1748. if( md->lootitem )
  1749. {
  1750. aFree(md->lootitem);
  1751. md->lootitem=NULL;
  1752. }
  1753. if( md->guardian_data )
  1754. {
  1755. struct guild_castle* gc = md->guardian_data->castle;
  1756. if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
  1757. {
  1758. gc->guardian[md->guardian_data->number].id = 0;
  1759. }
  1760. else
  1761. {
  1762. int i;
  1763. ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == md->bl.id);
  1764. if( i < gc->temp_guardians_max )
  1765. gc->temp_guardians[i] = 0;
  1766. }
  1767. aFree(md->guardian_data);
  1768. md->guardian_data = NULL;
  1769. }
  1770. if( md->spawn )
  1771. {
  1772. md->spawn->active--;
  1773. if( !md->spawn->state.dynamic )
  1774. {// permanently remove the mob
  1775. if( --md->spawn->num == 0 )
  1776. {// Last freed mob is responsible for deallocating the group's spawn data.
  1777. aFree(md->spawn);
  1778. md->spawn = NULL;
  1779. }
  1780. }
  1781. }
  1782. if( md->base_status)
  1783. {
  1784. aFree(md->base_status);
  1785. md->base_status = NULL;
  1786. }
  1787. if( mob_is_clone(md->class_) )
  1788. mob_clone_delete(md->class_);
  1789. break;
  1790. }
  1791. case BL_HOM:
  1792. {
  1793. struct homun_data *hd = (TBL_HOM*)bl;
  1794. struct map_session_data *sd = hd->master;
  1795. merc_hom_hungry_timer_delete(hd);
  1796. if( clrtype >= 0 )
  1797. {
  1798. if( hd->homunculus.intimacy > 0 )
  1799. merc_save(hd);
  1800. else
  1801. {
  1802. intif_homunculus_requestdelete(hd->homunculus.hom_id);
  1803. if( sd )
  1804. sd->status.hom_id = 0;
  1805. }
  1806. }
  1807. if( sd )
  1808. sd->hd = NULL;
  1809. break;
  1810. }
  1811. case BL_MER:
  1812. {
  1813. struct mercenary_data *md = (TBL_MER*)bl;
  1814. struct map_session_data *sd = md->master;
  1815. if( clrtype >= 0 )
  1816. {
  1817. if( md->mercenary.remain_life_time > 0 )
  1818. mercenary_save(md);
  1819. else
  1820. {
  1821. intif_mercenary_delete(md->mercenary.mercenary_id);
  1822. if( sd )
  1823. sd->status.mer_id = 0;
  1824. }
  1825. }
  1826. if( sd )
  1827. sd->md = NULL;
  1828. merc_contract_stop(md);
  1829. break;
  1830. }
  1831. }
  1832. skill_clear_unitgroup(bl);
  1833. status_change_clear(bl,1);
  1834. map_deliddb(bl);
  1835. if( bl->type != BL_PC ) //Players are handled by map_quit
  1836. map_freeblock(bl);
  1837. map_freeblock_unlock();
  1838. return 0;
  1839. }
  1840. int do_init_unit(void)
  1841. {
  1842. add_timer_func_list(unit_attack_timer, "unit_attack_timer");
  1843. add_timer_func_list(unit_walktoxy_timer,"unit_walktoxy_timer");
  1844. add_timer_func_list(unit_walktobl_sub, "unit_walktobl_sub");
  1845. add_timer_func_list(unit_delay_walktoxy_timer,"unit_delay_walktoxy_timer");
  1846. return 0;
  1847. }
  1848. int do_final_unit(void)
  1849. {
  1850. // nothing to do
  1851. return 0;
  1852. }