unit.c 65 KB

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