unit.cpp 110 KB

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