unit.c 95 KB

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