unit.c 81 KB

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