unit.c 70 KB

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