unit.c 72 KB

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