unit.c 66 KB

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