unit.cpp 97 KB

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