unit.cpp 95 KB

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