unit.c 95 KB

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