unit.cpp 99 KB

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