unit.cpp 106 KB

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