unit.c 68 KB

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