unit.c 70 KB

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