unit.c 81 KB

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