unit.cpp 106 KB

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