unit.c 58 KB

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