unit.c 66 KB

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