unit.c 92 KB

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