unit.c 93 KB

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