unit.c 94 KB

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