unit.cpp 96 KB

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