unit.c 88 KB

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