unit.c 60 KB

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