unit.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. // Copyright (c) jAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. // Merged originally from jA by Skotlex
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "../common/showmsg.h"
  8. #include "../common/timer.h"
  9. #include "../common/nullpo.h"
  10. #include "../common/db.h"
  11. #include "../common/malloc.h"
  12. #include "unit.h"
  13. #include "map.h"
  14. #include "pc.h"
  15. #include "mob.h"
  16. #include "pet.h"
  17. #include "mercenary.h"
  18. #include "skill.h"
  19. #include "clif.h"
  20. #include "npc.h"
  21. #include "guild.h"
  22. #include "status.h"
  23. #include "battle.h"
  24. #include "chat.h"
  25. #include "trade.h"
  26. #include "vending.h"
  27. #include "party.h"
  28. #include "intif.h"
  29. #include "chrif.h"
  30. #include "script.h"
  31. const int dirx[8]={0,-1,-1,-1,0,1,1,1};
  32. const int diry[8]={1,1,0,-1,-1,-1,0,1};
  33. struct unit_data* unit_bl2ud(struct block_list *bl) {
  34. if( bl == NULL) return NULL;
  35. if( bl->type == BL_PC) return &((struct map_session_data*)bl)->ud;
  36. if( bl->type == BL_MOB) return &((struct mob_data*)bl)->ud;
  37. if( bl->type == BL_PET) return &((struct pet_data*)bl)->ud;
  38. if( bl->type == BL_NPC) return &((struct npc_data*)bl)->ud;
  39. if( bl->type == BL_HOM) return &((struct homun_data*)bl)->ud; //[orn]
  40. return NULL;
  41. }
  42. static int unit_walktoxy_timer(int tid,unsigned int tick,int id,int data);
  43. int unit_walktoxy_sub(struct block_list *bl)
  44. {
  45. int i;
  46. struct walkpath_data wpd;
  47. struct unit_data *ud = NULL;
  48. nullpo_retr(1, bl);
  49. ud = unit_bl2ud(bl);
  50. if(ud == NULL) return 0;
  51. if(path_search(&wpd,bl->m,bl->x,bl->y,ud->to_x,ud->to_y,ud->state.walk_easy))
  52. return 0;
  53. memcpy(&ud->walkpath,&wpd,sizeof(wpd));
  54. if (ud->target && ud->chaserange>1) {
  55. //Generally speaking, the walk path is already to an adjacent tile
  56. //so we only need to shorten the path if the range is greater than 1.
  57. int dir;
  58. //Trim the last part of the path to account for range,
  59. //but always move at least one cell when requested to move.
  60. for (i = ud->chaserange*10; i > 0 && ud->walkpath.path_len>1;) {
  61. ud->walkpath.path_len--;
  62. dir = ud->walkpath.path[ud->walkpath.path_len];
  63. if(dir&1)
  64. i-=14;
  65. else
  66. i-=10;
  67. ud->to_x -= dirx[dir];
  68. ud->to_y -= diry[dir];
  69. }
  70. }
  71. ud->state.change_walk_target=0;
  72. if (bl->type == BL_PC)
  73. clif_walkok((TBL_PC*)bl);
  74. clif_move(bl);
  75. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  76. i = -1;
  77. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  78. i = status_get_speed(bl)*14/10;
  79. else
  80. i = status_get_speed(bl);
  81. if( i > 0)
  82. ud->walktimer = add_timer(gettick()+i,unit_walktoxy_timer,bl->id,i);
  83. return 1;
  84. }
  85. static int unit_walktoxy_timer(int tid,unsigned int tick,int id,int data)
  86. {
  87. int i;
  88. int x,y,dx,dy,dir;
  89. struct block_list *bl;
  90. struct map_session_data *sd = NULL;
  91. struct mob_data *md = NULL;
  92. struct unit_data *ud = NULL;
  93. bl=map_id2bl(id);
  94. if(bl == NULL)
  95. return 0;
  96. if( BL_CAST( BL_PC, bl, sd ) ) {
  97. ud = &sd->ud;
  98. } else if( BL_CAST( BL_MOB, bl, md ) ) {
  99. ud = &md->ud;
  100. } else
  101. ud = unit_bl2ud(bl);
  102. if(ud == NULL) return 0;
  103. if(ud->walktimer != tid){
  104. if(battle_config.error_log)
  105. ShowError("unit_walk_timer mismatch %d != %d\n",ud->walktimer,tid);
  106. return 0;
  107. }
  108. ud->walktimer=-1;
  109. if( bl->prev == NULL ) return 0; // block_list から抜けているので移動停止する
  110. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  111. return 0;
  112. //歩いたので息吹のタイマーを初期化
  113. if(sd) {
  114. sd->inchealspirithptick = 0;
  115. sd->inchealspiritsptick = 0;
  116. }
  117. if(ud->walkpath.path[ud->walkpath.path_pos]>=8)
  118. return 1;
  119. x = bl->x;
  120. y = bl->y;
  121. dir = ud->walkpath.path[ud->walkpath.path_pos];
  122. ud->dir = dir;
  123. if (sd)
  124. sd->head_dir = dir;
  125. dx = dirx[(int)dir];
  126. dy = diry[(int)dir];
  127. if(map_getcell(bl->m,x+dx,y+dy,CELL_CHKNOPASS))
  128. return unit_walktoxy_sub(bl);
  129. // バシリカ判定
  130. map_foreachinmovearea(clif_outsight,bl->m,
  131. x-AREA_SIZE,y-AREA_SIZE,x+AREA_SIZE,y+AREA_SIZE,
  132. dx,dy,sd?BL_ALL:BL_PC,bl);
  133. x += dx;
  134. y += dy;
  135. map_moveblock(bl, x, y, tick);
  136. ud->walk_count++; //walked cell counter, to be used for walk-triggered skills. [Skotlex]
  137. ud->walktimer = 1;
  138. map_foreachinmovearea(clif_insight,bl->m,
  139. x-AREA_SIZE,y-AREA_SIZE,x+AREA_SIZE,y+AREA_SIZE,
  140. -dx,-dy,sd?BL_ALL:BL_PC,bl);
  141. ud->walktimer = -1;
  142. if(sd) {
  143. if(map_getcell(bl->m,x,y,CELL_CHKNPC)) {
  144. npc_touch_areanpc(sd,bl->m,x,y);
  145. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  146. return 0;
  147. } else
  148. sd->areanpc_id=0;
  149. if (sd->state.gmaster_flag &&
  150. (battle_config.guild_aura&(agit_flag?2:1)) &&
  151. (battle_config.guild_aura&
  152. (map[bl->m].flag.gvg || map[bl->m].flag.gvg_castle?8:4))
  153. )
  154. { //Guild Aura: Likely needs to be recoded, this method seems inefficient.
  155. struct guild *g = sd->state.gmaster_flag;
  156. int skill, strvit= 0, agidex = 0;
  157. if ((skill = guild_checkskill(g, GD_LEADERSHIP)) > 0) strvit |= (skill&0xFFFF)<<16;
  158. if ((skill = guild_checkskill(g, GD_GLORYWOUNDS)) > 0) strvit |= (skill&0xFFFF);
  159. if ((skill = guild_checkskill(g, GD_SOULCOLD)) > 0) agidex |= (skill&0xFFFF)<<16;
  160. if ((skill = guild_checkskill(g, GD_HAWKEYES)) > 0) agidex |= skill&0xFFFF;
  161. if (strvit || agidex)
  162. map_foreachinrange(skill_guildaura_sub, bl,2, BL_PC,
  163. bl->id, sd->status.guild_id, strvit, agidex);
  164. }
  165. if (
  166. (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR &&
  167. sd->sc.data[SC_MIRACLE].timer==-1 &&
  168. !(ud->walk_count%WALK_SKILL_INTERVAL) &&
  169. rand()%10000 < battle_config.sg_miracle_skill_ratio
  170. ) { //SG_MIRACLE [Komurka]
  171. clif_displaymessage(sd->fd,"[Miracle of the Sun, Moon and Stars]");
  172. sc_start(&sd->bl,SC_MIRACLE,100,1,battle_config.sg_miracle_skill_duration);
  173. }
  174. } else if (md) {
  175. if(battle_config.mob_warp&1 && map_getcell(bl->m,x,y,CELL_CHKNPC) &&
  176. npc_touch_areanpc2(bl)) // Enable mobs to step on warps. [Skotlex]
  177. return 0;
  178. if (md->min_chase > md->db->range2) 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 != -1 &&
  182. !(ud->walk_count%WALK_SKILL_INTERVAL) &&
  183. mobskill_use(md, tick, -1))
  184. {
  185. if (!(ud->skillid == NPC_SELFDESTRUCTION && ud->skilltimer != -1))
  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(bl);
  192. }
  193. }
  194. if(tid == -1) //A directly invoked timer is from battle_stop_walking, therefore the rest is irrelevant.
  195. return 0;
  196. if(ud->state.change_walk_target)
  197. return unit_walktoxy_sub(bl);
  198. ud->walkpath.path_pos++;
  199. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  200. i = -1;
  201. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  202. i = status_get_speed(bl)*14/10;
  203. else
  204. i = status_get_speed(bl);
  205. if(i > 0)
  206. ud->walktimer = add_timer(tick+i,unit_walktoxy_timer,id,i);
  207. else if(ud->state.running) {
  208. //Keep trying to run.
  209. if (!unit_run(bl))
  210. ud->state.running = 0;
  211. } else if (ud->target) {
  212. //Update target trajectory.
  213. struct block_list *tbl = map_id2bl(ud->target);
  214. if (!tbl || !status_check_visibility(bl, tbl)) { //Cancel chase.
  215. ud->to_x = bl->x;
  216. ud->to_y = bl->y;
  217. return 0;
  218. }
  219. if (tbl->m == bl->m && check_distance_bl(bl, tbl, ud->chaserange))
  220. { //Reached destination.
  221. if (ud->state.attack_continue) {
  222. clif_fixpos(bl); //Aegis uses one before every attack, we should
  223. //only need this one for syncing purposes. [Skotlex]
  224. unit_attack(bl, tbl->id, ud->state.attack_continue);
  225. }
  226. } else { //Update chase-path
  227. unit_walktobl(bl, tbl, ud->chaserange, ud->state.walk_easy|(ud->state.attack_continue?2:0));
  228. return 0;
  229. }
  230. } else { //Stopped walking. Update to_x and to_y to current location [Skotlex]
  231. ud->to_x = bl->x;
  232. ud->to_y = bl->y;
  233. if(md && md->nd) // Tell the script engine we've finished walking (for AI pathfinding)
  234. mob_script_callback(md, NULL, CALLBACK_WALKACK);
  235. }
  236. return 0;
  237. }
  238. //Easy parameter: &1 -> 1/2 = easy/hard, &2 -> force walking.
  239. int unit_walktoxy( struct block_list *bl, int x, int y, int easy) {
  240. struct unit_data *ud = NULL;
  241. struct status_change *sc = NULL;
  242. nullpo_retr(0, bl);
  243. if ( status_isdead(bl) ) //[orn]
  244. return 0;
  245. ud = unit_bl2ud(bl);
  246. if( ud == NULL) return 0;
  247. if(!(easy&2) && (!status_get_mode(bl)&MD_CANMOVE || !unit_can_move(bl)))
  248. return 0;
  249. ud->state.walk_easy = easy&1;
  250. ud->target = 0;
  251. ud->to_x = x;
  252. ud->to_y = y;
  253. sc = status_get_sc(bl);
  254. if (sc && sc->count && sc->data[SC_CONFUSION].timer != -1) //Randomize the target position
  255. map_random_dir(bl, &ud->to_x, &ud->to_y);
  256. if(ud->walktimer != -1) {
  257. // 現在歩いている最中の目的地変更なのでマス目の中心に来た時に
  258. // timer関数からunit_walktoxy_subを呼ぶようにする
  259. ud->state.change_walk_target = 1;
  260. return 1;
  261. } else {
  262. return unit_walktoxy_sub(bl);
  263. }
  264. }
  265. static int unit_walktobl_sub(int tid,unsigned int tick,int id,int data)
  266. {
  267. struct block_list *bl = map_id2bl(id);
  268. struct unit_data *ud = bl?unit_bl2ud(bl):NULL;
  269. if (ud && ud->walktimer == -1 && ud->target == data)
  270. {
  271. if (DIFF_TICK(ud->canmove_tick, tick) > 0) //Keep waiting?
  272. add_timer(ud->canmove_tick+1, unit_walktobl_sub, id, data);
  273. else if (unit_can_move(bl))
  274. unit_walktoxy_sub(bl);
  275. }
  276. return 0;
  277. }
  278. // Chases a tbl. If the flag&1, use hard-path seek,
  279. // if flag&2, start attacking upon arrival within range, otherwise just walk to that character.
  280. int unit_walktobl(struct block_list *bl, struct block_list *tbl, int range, int flag) {
  281. struct unit_data *ud = NULL;
  282. struct status_change *sc = NULL;
  283. nullpo_retr(0, bl);
  284. nullpo_retr(0, tbl);
  285. ud = unit_bl2ud(bl);
  286. if( ud == NULL) return 0;
  287. if (!(status_get_mode(bl)&MD_CANMOVE))
  288. return 0;
  289. if (!unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, flag&1, &ud->to_x, &ud->to_y)) {
  290. ud->to_x = bl->x;
  291. ud->to_y = bl->y;
  292. return 0;
  293. }
  294. ud->state.walk_easy = flag&1;
  295. ud->target = tbl->id;
  296. ud->chaserange = range; //Note that if flag&2, this SHOULD be attack-range
  297. ud->state.attack_continue = flag&2?1:0; //Chase to attack.
  298. sc = status_get_sc(bl);
  299. if (sc && sc->count && sc->data[SC_CONFUSION].timer != -1) //Randomize the target position
  300. map_random_dir(bl, &ud->to_x, &ud->to_y);
  301. if(ud->walktimer != -1) {
  302. ud->state.change_walk_target = 1;
  303. return 1;
  304. }
  305. if (DIFF_TICK(ud->canmove_tick, gettick()) > 0)
  306. { //Can't move, wait a bit before invoking the movement.
  307. add_timer(ud->canmove_tick+1, unit_walktobl_sub, bl->id, ud->target);
  308. return 1;
  309. } else if (!unit_can_move(bl))
  310. return 0;
  311. return unit_walktoxy_sub(bl);
  312. }
  313. int unit_run(struct block_list *bl)
  314. {
  315. struct status_change *sc = status_get_sc(bl);
  316. int i,to_x,to_y,dir_x,dir_y;
  317. if (!sc || !sc->count || sc->data[SC_RUN].timer == -1)
  318. return 0;
  319. if (!unit_can_move(bl)) {
  320. if(sc->data[SC_RUN].timer!=-1)
  321. status_change_end(bl,SC_RUN,-1);
  322. return 0;
  323. }
  324. to_x = bl->x;
  325. to_y = bl->y;
  326. dir_x = dirx[sc->data[SC_RUN].val2];
  327. dir_y = diry[sc->data[SC_RUN].val2];
  328. for(i=0;i<AREA_SIZE;i++)
  329. {
  330. if(!map_getcell(bl->m,to_x+dir_x,to_y+dir_y,CELL_CHKPASS))
  331. break;
  332. to_x += dir_x;
  333. to_y += dir_y;
  334. }
  335. if(to_x == bl->x && to_y == bl->y) {
  336. //If you can't run forward, you must be next to a wall, so bounce back. [Skotlex]
  337. status_change_end(bl,SC_RUN,-1);
  338. skill_blown(bl,bl,skill_get_blewcount(TK_RUN,sc->data[SC_RUN].val1)|0x10000);
  339. return 0;
  340. }
  341. unit_walktoxy(bl, to_x, to_y, 1);
  342. return 1;
  343. }
  344. //Instant warp function.
  345. int unit_movepos(struct block_list *bl,int dst_x,int dst_y, int easy, int checkpath)
  346. {
  347. int dx,dy,dir;
  348. struct unit_data *ud = NULL;
  349. struct map_session_data *sd = NULL;
  350. struct walkpath_data wpd;
  351. nullpo_retr(0, bl);
  352. if( BL_CAST( BL_PC, bl, sd ) ) {
  353. ud = &sd->ud;
  354. } else
  355. ud = unit_bl2ud(bl);
  356. if( ud == NULL) return 0;
  357. unit_stop_walking(bl,1);
  358. unit_stop_attack(bl);
  359. if(checkpath && (map_getcell(bl->m,dst_x,dst_y, CELL_CHKNOPASS) || path_search_real(&wpd,bl->m,bl->x,bl->y,dst_x,dst_y,easy, CELL_CHKNOREACH)))
  360. return 0;
  361. dir = map_calc_dir(bl, dst_x,dst_y);
  362. ud->dir = dir;
  363. if(sd) sd->head_dir = dir;
  364. dx = dst_x - bl->x;
  365. dy = dst_y - bl->y;
  366. map_foreachinmovearea(clif_outsight,bl->m,
  367. bl->x-AREA_SIZE,bl->y-AREA_SIZE,bl->x+AREA_SIZE,bl->y+AREA_SIZE,
  368. dx,dy,sd?BL_ALL:BL_PC,bl);
  369. map_moveblock(bl, dst_x, dst_y, gettick());
  370. ud->walktimer = 1;
  371. map_foreachinmovearea(clif_insight,bl->m,
  372. bl->x-AREA_SIZE,bl->y-AREA_SIZE,bl->x+AREA_SIZE,bl->y+AREA_SIZE,
  373. -dx,-dy,sd?BL_ALL:BL_PC,bl);
  374. ud->walktimer = -1;
  375. if(sd) {
  376. if(map_getcell(bl->m,bl->x,bl->y,CELL_CHKNPC)) {
  377. npc_touch_areanpc(sd,bl->m,bl->x,bl->y);
  378. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  379. return 0;
  380. } else
  381. sd->areanpc_id=0;
  382. if(sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0)
  383. { //Check if pet needs to be teleported. [Skotlex]
  384. int flag = 0;
  385. bl = &sd->pd->bl; //Note that bl now points to the pet!
  386. if (!checkpath && path_search(&wpd,bl->m,bl->x,bl->y,dst_x,dst_y,0))
  387. flag = 1;
  388. else if (!check_distance_bl(&sd->bl, bl, AREA_SIZE)) //Too far, teleport.
  389. flag = 2;
  390. if (flag) {
  391. unit_movepos(bl,sd->bl.x,sd->bl.y, 0, 0);
  392. clif_slide(bl,bl->x,bl->y);
  393. }
  394. //If you want to use bl afterwards, uncomment this:
  395. //bl = &sd->bl;
  396. }
  397. }
  398. return 1;
  399. }
  400. int unit_setdir(struct block_list *bl,unsigned char dir)
  401. {
  402. struct unit_data *ud;
  403. nullpo_retr( 0, bl );
  404. ud = unit_bl2ud(bl);
  405. if (!ud) return 0;
  406. ud->dir = dir;
  407. if (bl->type == BL_PC)
  408. ((TBL_PC *)bl)->head_dir = dir;
  409. clif_changed_dir(bl, AREA);
  410. return 0;
  411. }
  412. int unit_getdir(struct block_list *bl)
  413. {
  414. struct unit_data *ud;
  415. nullpo_retr( 0, bl );
  416. ud = unit_bl2ud(bl);
  417. if (!ud) return 0;
  418. return ud->dir;
  419. }
  420. //Warps a unit/ud to a given map/position.
  421. //In the case of players, pc_setpos is used.
  422. //it respects the no warp flags, so it is safe to call this without doing nowarpto/nowarp checks.
  423. int unit_warp(struct block_list *bl,int m,short x,short y,int type)
  424. {
  425. struct unit_data *ud;
  426. nullpo_retr(0, bl);
  427. ud = unit_bl2ud(bl);
  428. if(bl->prev==NULL || !ud)
  429. return 1;
  430. if (type < 0 || type == 1)
  431. //Type 1 is invalid, since you shouldn't warp a bl with the "death"
  432. //animation, it messes up with unit_remove_map! [Skotlex]
  433. return 1;
  434. if( m<0 ) m=bl->m;
  435. switch (bl->type) {
  436. case BL_MOB:
  437. if (map[bl->m].flag.monster_noteleport)
  438. return 1;
  439. if (m != bl->m && map[m].flag.nobranch && battle_config.mob_warp&4)
  440. return 1;
  441. break;
  442. case BL_PC:
  443. if (map[bl->m].flag.noteleport)
  444. return 1;
  445. break;
  446. }
  447. if (x<0 || y<0)
  448. { //Random map position.
  449. if (!map_search_freecell(NULL, m, &x, &y, -1, -1, 1)) {
  450. if(battle_config.error_log)
  451. 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);
  452. return 2;
  453. }
  454. } else if (map_getcell(m,x,y,CELL_CHKNOREACH))
  455. { //Invalid target cell
  456. if(battle_config.error_log)
  457. ShowWarning("unit_warp: Specified non-walkable target cell: %d (%s) at [%d,%d]\n", m, map[m].name, x,y);
  458. if (!map_search_freecell(NULL, m, &x, &y, 4, 4, 1))
  459. { //Can't find a nearby cell
  460. if(battle_config.error_log)
  461. 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);
  462. return 2;
  463. }
  464. }
  465. if (bl->type == BL_PC) //Use pc_setpos
  466. return pc_setpos((TBL_PC*)bl, map[m].index, x, y, type);
  467. if (!unit_remove_map(bl, type))
  468. return 3;
  469. bl->x=ud->to_x=x;
  470. bl->y=ud->to_y=y;
  471. bl->m=m;
  472. map_addblock(bl);
  473. clif_spawn(bl);
  474. skill_unit_move(bl,gettick(),1);
  475. if(bl->type == BL_MOB){
  476. TBL_MOB *md = (TBL_MOB *)bl;
  477. if(md->nd) // Tell the script engine we've warped
  478. mob_script_callback(md, NULL, CALLBACK_WARPACK);
  479. }
  480. return 0;
  481. }
  482. /*==========================================
  483. * 歩行停止
  484. *------------------------------------------
  485. */
  486. int unit_stop_walking(struct block_list *bl,int type)
  487. {
  488. struct unit_data *ud;
  489. struct TimerData *data;
  490. unsigned int tick;
  491. nullpo_retr(0, bl);
  492. ud = unit_bl2ud(bl);
  493. if(!ud || ud->walktimer == -1)
  494. return 0;
  495. //NOTE: We are using timer data after deleting it because we know the
  496. //delete_timer function does not messes with it. If the function's
  497. //behaviour changes in the future, this code could break!
  498. data = get_timer(ud->walktimer);
  499. delete_timer(ud->walktimer, unit_walktoxy_timer);
  500. ud->walktimer = -1;
  501. ud->state.change_walk_target = 0;
  502. tick = gettick();
  503. if ((type&0x02 && !ud->walkpath.path_pos) //Force moving at least one cell.
  504. || (data && DIFF_TICK(data->tick, tick) <= data->data/2)) //Enough time has passed to cover half-cell
  505. {
  506. ud->walkpath.path_len = ud->walkpath.path_pos+1;
  507. unit_walktoxy_timer(-1, tick, bl->id, ud->walkpath.path_pos);
  508. }
  509. if(type&0x01)
  510. clif_fixpos(bl);
  511. ud->walkpath.path_len = 0;
  512. ud->walkpath.path_pos = 0;
  513. ud->to_x = bl->x;
  514. ud->to_y = bl->y;
  515. if(bl->type == BL_PET && type&~0xff)
  516. ud->canmove_tick = gettick() + (type>>8);
  517. if (ud->state.running)
  518. status_change_end(bl, SC_RUN, -1);
  519. return 1;
  520. }
  521. int unit_skilluse_id(struct block_list *src, int target_id, int skill_num, int skill_lv) {
  522. if(skill_num < 0) return 0;
  523. return unit_skilluse_id2(
  524. src, target_id, skill_num, skill_lv,
  525. skill_castfix(src, skill_num, skill_lv),
  526. skill_get_castcancel(skill_num)
  527. );
  528. }
  529. int unit_is_walking(struct block_list *bl)
  530. {
  531. struct unit_data *ud = unit_bl2ud(bl);
  532. nullpo_retr(0, bl);
  533. if(!ud) return 0;
  534. return (ud->walktimer != -1);
  535. }
  536. /*==========================================
  537. * Determines if the bl can move based on status changes. [Skotlex]
  538. *------------------------------------------
  539. */
  540. int unit_can_move(struct block_list *bl)
  541. {
  542. struct map_session_data *sd;
  543. struct unit_data *ud;
  544. struct status_change *sc;
  545. nullpo_retr(0, bl);
  546. ud = unit_bl2ud(bl);
  547. sc = status_get_sc(bl);
  548. BL_CAST(BL_PC, bl, sd);
  549. if (!ud)
  550. return 0;
  551. if (ud->skilltimer != -1 && (!sd || pc_checkskill(sd, SA_FREECAST) <= 0))
  552. return 0;
  553. if (DIFF_TICK(ud->canmove_tick, gettick()) > 0)
  554. return 0;
  555. if (sd && (
  556. pc_issit(sd) ||
  557. sd->state.blockedmove
  558. ))
  559. return 0; //Can't move
  560. if (sc) {
  561. if (sc->opt1 > 0 && sc->opt1 != OPT1_STONEWAIT)
  562. return 0;
  563. if ((sc->option & OPTION_HIDE) && (!sd || pc_checkskill(sd, RG_TUNNELDRIVE) <= 0))
  564. return 0;
  565. if (sc->count && (
  566. sc->data[SC_ANKLE].timer != -1 ||
  567. sc->data[SC_AUTOCOUNTER].timer !=-1 ||
  568. sc->data[SC_TRICKDEAD].timer !=-1 ||
  569. sc->data[SC_BLADESTOP].timer !=-1 ||
  570. sc->data[SC_BLADESTOP_WAIT].timer !=-1 ||
  571. sc->data[SC_SPIDERWEB].timer !=-1 ||
  572. (sc->data[SC_DANCING].timer !=-1 && (
  573. (sc->data[SC_DANCING].val4 && sc->data[SC_LONGING].timer == -1) ||
  574. sc->data[SC_DANCING].val1 == CG_HERMODE //cannot move while Hermod is active.
  575. )) ||
  576. sc->data[SC_MOONLIT].timer != -1 ||
  577. (sc->data[SC_GOSPEL].timer !=-1 && sc->data[SC_GOSPEL].val4 == BCT_SELF) || // cannot move while gospel is in effect
  578. sc->data[SC_STOP].timer != -1 ||
  579. sc->data[SC_CLOSECONFINE].timer != -1 ||
  580. sc->data[SC_CLOSECONFINE2].timer != -1 ||
  581. (sc->data[SC_CLOAKING].timer != -1 && //Need wall at level 1-2
  582. sc->data[SC_CLOAKING].val1 < 3 && !(sc->data[SC_CLOAKING].val4&1))
  583. ))
  584. return 0;
  585. }
  586. return 1;
  587. }
  588. /*==========================================
  589. * Applies walk delay to character, considering that
  590. * if type is 0, this is a damage induced delay: if previous delay is active, do not change it.
  591. * if type is 1, this is a skill induced delay: walk-delay may only be increased, not decreased.
  592. *------------------------------------------
  593. */
  594. int unit_set_walkdelay(struct block_list *bl, unsigned int tick, int delay, int type)
  595. {
  596. struct unit_data *ud = unit_bl2ud(bl);
  597. if (delay <= 0 || !ud) return 0;
  598. if (type) {
  599. if (DIFF_TICK(ud->canmove_tick, tick+delay) > 0)
  600. return 0;
  601. } else {
  602. if (DIFF_TICK(ud->canmove_tick, tick) > 0)
  603. return 0;
  604. }
  605. ud->canmove_tick = tick + delay;
  606. if (ud->walktimer != -1)
  607. { //Stop walking, if chasing, readjust timers.
  608. if (delay == 1)
  609. { //Minimal delay (walk-delay) disabled. Just stop walking.
  610. unit_stop_walking(bl,0);
  611. } else {
  612. unit_stop_walking(bl,2);
  613. if(ud->target)
  614. add_timer(ud->canmove_tick+1, unit_walktobl_sub, bl->id, ud->target);
  615. }
  616. }
  617. return 1;
  618. }
  619. int unit_skilluse_id2(struct block_list *src, int target_id, int skill_num, int skill_lv, int casttime, int castcancel) {
  620. struct unit_data *ud;
  621. struct status_data *tstatus;
  622. struct status_change *sc;
  623. struct map_session_data *sd = NULL;
  624. struct block_list * target = NULL;
  625. unsigned int tick = gettick();
  626. int temp;
  627. nullpo_retr(0, src);
  628. if(status_isdead(src))
  629. return 0; // 死んでいないか
  630. if( BL_CAST( BL_PC, src, sd ) )
  631. ud = &sd->ud;
  632. else
  633. ud = unit_bl2ud(src);
  634. if(ud == NULL) return 0;
  635. sc = status_get_sc(src);
  636. if (sc && !sc->count)
  637. sc = NULL; //Unneeded
  638. //temp: used to signal combo-skills right now.
  639. temp = (target_id == src->id && !(sd && sd->state.skill_flag)
  640. && skill_get_inf(skill_num)&INF_SELF_SKILL
  641. && skill_get_inf2(skill_num)&INF2_NO_TARGET_SELF);
  642. if (temp)
  643. target_id = ud->target; //Auto-select skills. [Skotlex]
  644. if (sd) {
  645. //Target_id checking.
  646. if(skillnotok(skill_num, sd)) // [MouseJstr]
  647. return 0;
  648. switch(skill_num)
  649. { //Check for skills that auto-select target
  650. case MO_CHAINCOMBO:
  651. if (sc && sc->data[SC_BLADESTOP].timer != -1){
  652. if ((target=(struct block_list *)sc->data[SC_BLADESTOP].val4) == NULL)
  653. return 0;
  654. }
  655. break;
  656. case TK_JUMPKICK:
  657. case TK_COUNTER:
  658. case HT_POWER:
  659. if (sc && sc->data[SC_COMBO].timer != -1 && sc->data[SC_COMBO].val1 == skill_num)
  660. target_id = sc->data[SC_COMBO].val2;
  661. break;
  662. case WE_MALE:
  663. case WE_FEMALE:
  664. if (!sd->status.partner_id)
  665. return 0;
  666. target = (struct block_list*)map_charid2sd(sd->status.partner_id);
  667. if (!target) {
  668. clif_skill_fail(sd,skill_num,0,0);
  669. return 0;
  670. }
  671. break;
  672. }
  673. if (target)
  674. target_id = target->id;
  675. }
  676. if (src->type==BL_HOM)
  677. switch(skill_num)
  678. { //Homun-auto-target skills.
  679. case HLIF_HEAL:
  680. case HLIF_AVOID:
  681. case HAMI_DEFENCE:
  682. case HAMI_CASTLE:
  683. target = battle_get_master(src);
  684. if (!target) return 0;
  685. target_id = target->id;
  686. }
  687. if(!target && (target=map_id2bl(target_id)) == NULL )
  688. return 0;
  689. if(src->m != target->m)
  690. return 0; // 同じマップかどうか
  691. if(!src->prev || !target->prev)
  692. return 0; // map 上に存在するか
  693. //Normally not needed because clif.c checks for it, but the at/char/script commands don't! [Skotlex]
  694. if(ud->skilltimer != -1 && skill_num != SA_CASTCANCEL)
  695. return 0;
  696. if(skill_get_inf2(skill_num)&INF2_NO_TARGET_SELF && src->id == target_id)
  697. return 0;
  698. if(!status_check_skilluse(src, target, skill_num, 0))
  699. return 0;
  700. tstatus = status_get_status_data(target);
  701. //直前のスキル状況の記録
  702. if(sd) {
  703. switch(skill_num){
  704. case SA_CASTCANCEL:
  705. if(ud->skillid != skill_num){
  706. sd->skillid_old = ud->skillid;
  707. sd->skilllv_old = ud->skilllv;
  708. break;
  709. }
  710. case BD_ENCORE:
  711. //Prevent using the dance skill if you no longer have the skill in your tree.
  712. if(!sd->skillid_dance || pc_checkskill(sd,sd->skillid_dance)<=0){
  713. clif_skill_fail(sd,skill_num,0,0);
  714. return 0;
  715. }
  716. sd->skillid_old = skill_num;
  717. break;
  718. case BD_LULLABY:
  719. case BD_RICHMANKIM:
  720. case BD_ETERNALCHAOS:
  721. case BD_DRUMBATTLEFIELD:
  722. case BD_RINGNIBELUNGEN:
  723. case BD_ROKISWEIL:
  724. case BD_INTOABYSS:
  725. case BD_SIEGFRIED:
  726. case CG_MOONLIT:
  727. if (battle_config.player_skill_partner_check &&
  728. (!battle_config.gm_skilluncond || pc_isGM(sd) < battle_config.gm_skilluncond) &&
  729. (skill_check_pc_partner(sd, skill_num, &skill_lv, 1, 0) < 1)
  730. ) {
  731. clif_skill_fail(sd,skill_num,0,0);
  732. return 0;
  733. }
  734. break;
  735. }
  736. if (!skill_check_condition(sd, skill_num, skill_lv, 0))
  737. return 0;
  738. }
  739. //TODO: Add type-independant skill_check_condition function.
  740. if (src->type == BL_MOB) {
  741. switch (skill_num) {
  742. case NPC_SUMMONSLAVE:
  743. case NPC_SUMMONMONSTER:
  744. case AL_TELEPORT:
  745. if (((TBL_MOB*)src)->master_id && ((TBL_MOB*)src)->special_state.ai)
  746. return 0;
  747. }
  748. }
  749. //Check range when not using skill on yourself or is a combo-skill during attack
  750. //(these are supposed to always have the same range as your attack)
  751. if(src->id != target_id && (!temp || ud->attacktimer == -1) &&
  752. !battle_check_range(src,target,skill_get_range2(src, skill_num,skill_lv)
  753. +(skill_num==RG_CLOSECONFINE?0:1))) //Close confine is exploitable thanks to this extra range "feature" of the client. [Skotlex]
  754. return 0;
  755. if (!temp) //Stop attack on non-combo skills [Skotlex]
  756. unit_stop_attack(src);
  757. else if(ud->attacktimer != -1) //Elsewise, delay current attack sequence
  758. ud->attackabletime = tick + status_get_adelay(src);
  759. ud->state.skillcastcancel = castcancel;
  760. //temp: Used to signal force cast now.
  761. temp = 0;
  762. switch(skill_num){
  763. case ALL_RESURRECTION:
  764. if(battle_check_undead(tstatus->race,tstatus->def_ele)){
  765. temp=1;
  766. casttime = skill_castfix(src, PR_TURNUNDEAD, skill_lv);
  767. } else if (!status_isdead(target))
  768. return 0; //Can't cast on non-dead characters.
  769. break;
  770. case MO_FINGEROFFENSIVE:
  771. if(sd)
  772. casttime += casttime * ((skill_lv > sd->spiritball)? sd->spiritball:skill_lv);
  773. break;
  774. case MO_EXTREMITYFIST:
  775. if (sc && sc->data[SC_COMBO].timer != -1 &&
  776. (sc->data[SC_COMBO].val1 == MO_COMBOFINISH ||
  777. sc->data[SC_COMBO].val1 == CH_TIGERFIST ||
  778. sc->data[SC_COMBO].val1 == CH_CHAINCRUSH))
  779. casttime = 0;
  780. temp = 1;
  781. break;
  782. case TK_RUN:
  783. if (sc && sc->data[SC_RUN].timer != -1)
  784. casttime = 0;
  785. break;
  786. case SA_MAGICROD:
  787. case SA_SPELLBREAKER:
  788. temp =1;
  789. break;
  790. case KN_CHARGEATK:
  791. //Taken from jA: Casttime is increased by dist/3*100%
  792. casttime = casttime * ((distance_bl(src,target)-1)/3+1);
  793. break;
  794. }
  795. if( casttime>0 || temp){
  796. clif_skillcasting(src, src->id, target_id, 0,0, skill_num,casttime);
  797. if (sd && target->type == BL_MOB)
  798. {
  799. TBL_MOB *md = (TBL_MOB*)target;
  800. mobskill_event(md, src, tick, -1); //Cast targetted skill event.
  801. //temp: used to store mob's mode now.
  802. if (tstatus->mode&MD_CASTSENSOR &&
  803. battle_check_target(target, src, BCT_ENEMY) > 0)
  804. {
  805. switch (md->state.skillstate) {
  806. case MSS_ANGRY:
  807. case MSS_RUSH:
  808. case MSS_FOLLOW:
  809. if (!(tstatus->mode&(MD_AGGRESSIVE|MD_ANGRY)))
  810. break; //Only Aggressive mobs change target while chasing.
  811. case MSS_IDLE:
  812. case MSS_WALK:
  813. md->target_id = src->id;
  814. md->state.aggressive = (temp&MD_ANGRY)?1:0;
  815. md->min_chase = md->db->range3;
  816. }
  817. }
  818. }
  819. }
  820. if( casttime<=0 )
  821. ud->state.skillcastcancel=0;
  822. ud->canact_tick = tick + casttime + 100;
  823. ud->skilltarget = target_id;
  824. ud->skillx = 0;
  825. ud->skilly = 0;
  826. ud->skillid = skill_num;
  827. ud->skilllv = skill_lv;
  828. if(sc && sc->data[SC_CLOAKING].timer != -1 &&
  829. !(sc->data[SC_CLOAKING].val4&2) && skill_num != AS_CLOAKING)
  830. status_change_end(src,SC_CLOAKING,-1);
  831. if(casttime > 0) {
  832. ud->skilltimer = add_timer( tick+casttime, skill_castend_id, src->id, 0 );
  833. if(sd && pc_checkskill(sd,SA_FREECAST))
  834. status_freecast_switch(sd);
  835. else
  836. unit_stop_walking(src,1);
  837. }
  838. else
  839. skill_castend_id(ud->skilltimer,tick,src->id,0);
  840. return 1;
  841. }
  842. int unit_skilluse_pos(struct block_list *src, int skill_x, int skill_y, int skill_num, int skill_lv) {
  843. if(skill_num < 0)
  844. return 0;
  845. return unit_skilluse_pos2(
  846. src, skill_x, skill_y, skill_num, skill_lv,
  847. skill_castfix(src, skill_num, skill_lv),
  848. skill_get_castcancel(skill_num)
  849. );
  850. }
  851. int unit_skilluse_pos2( struct block_list *src, int skill_x, int skill_y, int skill_num, int skill_lv, int casttime, int castcancel) {
  852. struct map_session_data *sd = NULL;
  853. struct unit_data *ud = NULL;
  854. struct status_change *sc;
  855. struct block_list bl;
  856. unsigned int tick = gettick();
  857. nullpo_retr(0, src);
  858. if(!src->prev) return 0; // map 上に存在するか
  859. if(status_isdead(src)) return 0;
  860. if( BL_CAST( BL_PC, src, sd ) ) {
  861. ud = &sd->ud;
  862. } else
  863. ud = unit_bl2ud(src);
  864. if(ud == NULL) return 0;
  865. if(ud->skilltimer != -1) //Normally not needed since clif.c checks for it, but at/char/script commands don't! [Skotlex]
  866. return 0;
  867. sc = status_get_sc(src);
  868. if (sc && !sc->count)
  869. sc = NULL;
  870. if(sd) {
  871. if (skillnotok(skill_num, sd) ||
  872. !skill_check_condition(sd, skill_num, skill_lv,0))
  873. return 0;
  874. }
  875. if (!status_check_skilluse(src, NULL, skill_num, 0))
  876. return 0;
  877. if (map_getcell(src->m, skill_x, skill_y, CELL_CHKNOREACH))
  878. { //prevent casting ground targeted spells on non-walkable areas. [Skotlex]
  879. if (sd) clif_skill_fail(sd,skill_num,0,0);
  880. return 0;
  881. }
  882. /* 射程と障害物チェック */
  883. bl.type = BL_NUL;
  884. bl.m = src->m;
  885. bl.x = skill_x;
  886. bl.y = skill_y;
  887. if(skill_num != TK_HIGHJUMP && skill_num != NJ_SHADOWJUMP &&
  888. !battle_check_range(src,&bl,skill_get_range2(src, skill_num,skill_lv)+1))
  889. return 0;
  890. unit_stop_attack(src);
  891. ud->state.skillcastcancel = castcancel;
  892. if( casttime>0 ) {
  893. unit_stop_walking( src, 1);
  894. clif_skillcasting(src, src->id, 0, skill_x,skill_y, skill_num,casttime);
  895. }
  896. if( casttime<=0 )
  897. ud->state.skillcastcancel=0;
  898. ud->canact_tick = tick + casttime + 100;
  899. ud->skillid = skill_num;
  900. ud->skilllv = skill_lv;
  901. ud->skillx = skill_x;
  902. ud->skilly = skill_y;
  903. ud->skilltarget = 0;
  904. if (sc && sc->data[SC_CLOAKING].timer != -1 &&
  905. !(sc->data[SC_CLOAKING].val4&2))
  906. status_change_end(src,SC_CLOAKING,-1);
  907. if(casttime > 0) {
  908. ud->skilltimer = add_timer( tick+casttime, skill_castend_pos, src->id, 0 );
  909. if(sd && pc_checkskill(sd,SA_FREECAST))
  910. status_freecast_switch(sd);
  911. else
  912. unit_stop_walking(src,1);
  913. }
  914. else {
  915. ud->skilltimer = -1;
  916. skill_castend_pos(ud->skilltimer,tick,src->id,0);
  917. }
  918. return 1;
  919. }
  920. static int unit_attack_timer(int tid,unsigned int tick,int id,int data);
  921. int unit_stop_attack(struct block_list *bl)
  922. {
  923. struct unit_data *ud = unit_bl2ud(bl);
  924. nullpo_retr(0, bl);
  925. if(!ud || ud->attacktimer == -1)
  926. return 0;
  927. delete_timer( ud->attacktimer, unit_attack_timer );
  928. ud->attacktimer = -1;
  929. ud->target = 0;
  930. return 0;
  931. }
  932. //Means current target is unattackable. For now only unlocks mobs.
  933. int unit_unattackable(struct block_list *bl) {
  934. struct unit_data *ud = unit_bl2ud(bl);
  935. if (ud) {
  936. ud->target = 0;
  937. ud->state.attack_continue = 0;
  938. }
  939. if(bl->type == BL_MOB)
  940. mob_unlocktarget((struct mob_data*)bl, gettick()) ;
  941. else if(bl->type == BL_PET)
  942. pet_unlocktarget((struct pet_data*)bl);
  943. return 0;
  944. }
  945. /*==========================================
  946. * 攻撃要求
  947. * typeが1なら継続攻撃
  948. *------------------------------------------
  949. */
  950. int unit_attack(struct block_list *src,int target_id,int type)
  951. {
  952. struct block_list *target;
  953. struct unit_data *ud;
  954. nullpo_retr(0, ud = unit_bl2ud(src));
  955. target=map_id2bl(target_id);
  956. if(target==NULL || status_isdead(target)) {
  957. unit_unattackable(src);
  958. return 1;
  959. }
  960. if(src->type == BL_PC && target->type==BL_NPC) { // monster npcs [Valaris]
  961. npc_click((TBL_PC*)src,(TBL_NPC*)target); // submitted by leinsirk10 [Celest]
  962. return 0;
  963. }
  964. if(battle_check_target(src,target,BCT_ENEMY)<=0 ||
  965. !status_check_skilluse(src, target, 0, 0)
  966. ) {
  967. unit_unattackable(src);
  968. return 1;
  969. }
  970. ud->target = target_id;
  971. ud->state.attack_continue = type;
  972. if (type) //If you re to attack continously, set to auto-case character
  973. ud->chaserange = status_get_range(src);
  974. //Just change target/type. [Skotlex]
  975. if(ud->attacktimer != -1)
  976. return 0;
  977. if(DIFF_TICK(ud->attackabletime, gettick()) > 0)
  978. //Do attack next time it is possible. [Skotlex]
  979. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  980. else //Attack NOW.
  981. unit_attack_timer(-1,gettick(),src->id,0);
  982. return 0;
  983. }
  984. /*==========================================
  985. *
  986. *------------------------------------------
  987. */
  988. int unit_can_reach_pos(struct block_list *bl,int x,int y, int easy)
  989. {
  990. struct walkpath_data wpd;
  991. nullpo_retr(0, bl);
  992. if( bl->x==x && bl->y==y ) // 同じマス
  993. return 1;
  994. // 障害物判定
  995. wpd.path_len=0;
  996. wpd.path_pos=0;
  997. wpd.path_half=0;
  998. return (path_search_real(&wpd,bl->m,bl->x,bl->y,x,y,easy,CELL_CHKNOREACH)!=-1);
  999. }
  1000. /*==========================================
  1001. *
  1002. *------------------------------------------
  1003. */
  1004. int unit_can_reach_bl(struct block_list *bl,struct block_list *tbl, int range, int easy, short *x, short *y)
  1005. {
  1006. struct walkpath_data wpd;
  1007. int i;
  1008. short dx,dy;
  1009. nullpo_retr(0, bl);
  1010. nullpo_retr(0, tbl);
  1011. if( bl->m != tbl->m)
  1012. return 0;
  1013. if( bl->x==tbl->x && bl->y==tbl->y )
  1014. return 1;
  1015. if(range>0 && !check_distance_bl(bl, tbl, range))
  1016. return 0;
  1017. wpd.path_len=0;
  1018. wpd.path_pos=0;
  1019. wpd.path_half=0;
  1020. // It judges whether it can adjoin or not.
  1021. dx=tbl->x - bl->x;
  1022. dy=tbl->y - bl->y;
  1023. dx=(dx>0)?1:((dx<0)?-1:0);
  1024. dy=(dy>0)?1:((dy<0)?-1:0);
  1025. if (map_getcell(tbl->m,tbl->x-dx,tbl->y-dy,CELL_CHKNOREACH))
  1026. { //Look for a suitable cell to place in.
  1027. for(i=0;i<9 && map_getcell(tbl->m,tbl->x-dirx[i],tbl->y-diry[i],CELL_CHKNOREACH);i++);
  1028. if (i==9) return 0; //No valid cells.
  1029. dx = dirx[i];
  1030. dy = diry[i];
  1031. }
  1032. if (x) *x = tbl->x-dx;
  1033. if (y) *y = tbl->y-dy;
  1034. return (path_search_real(&wpd,bl->m,bl->x,bl->y,tbl->x-dx,tbl->y-dy,easy,CELL_CHKNOREACH)!=-1);
  1035. }
  1036. /*==========================================
  1037. * PCの攻撃 (timer関数)
  1038. *------------------------------------------
  1039. */
  1040. static int unit_attack_timer_sub(struct block_list* src, int tid, unsigned int tick)
  1041. {
  1042. struct block_list *target;
  1043. struct unit_data *ud;
  1044. struct status_data *sstatus;
  1045. struct map_session_data *sd = NULL;
  1046. struct mob_data *md = NULL;
  1047. int range;
  1048. if((ud=unit_bl2ud(src))==NULL)
  1049. return 0;
  1050. if(ud->attacktimer != tid){
  1051. if(battle_config.error_log)
  1052. ShowError("unit_attack_timer %d != %d\n",ud->attacktimer,tid);
  1053. return 0;
  1054. }
  1055. BL_CAST( BL_PC , src, sd);
  1056. BL_CAST( BL_MOB, src, md);
  1057. ud->attacktimer=-1;
  1058. target=map_id2bl(ud->target);
  1059. if(src->prev == NULL || target==NULL || target->prev == NULL)
  1060. return 0;
  1061. if(ud->skilltimer != -1 && (!sd || pc_checkskill(sd,SA_FREECAST) <= 0))
  1062. return 0;
  1063. if(src->m != target->m || status_isdead(src) || status_isdead(target) || !status_check_skilluse(src, target, 0, 0))
  1064. return 0;
  1065. sstatus = status_get_status_data(src);
  1066. if(!battle_config.sdelay_attack_enable &&
  1067. DIFF_TICK(ud->canact_tick,tick) > 0 &&
  1068. (!sd || pc_checkskill(sd,SA_FREECAST) <= 0)
  1069. ) {
  1070. if (tid == -1) { //requested attack.
  1071. if(sd) clif_skill_fail(sd,1,4,0);
  1072. return 0;
  1073. }
  1074. //Otherwise, we are in a combo-attack, delay this until your canact time is over. [Skotlex]
  1075. if(ud->state.attack_continue) {
  1076. if (DIFF_TICK(ud->canact_tick, ud->attackabletime) > 0)
  1077. ud->attackabletime = ud->canact_tick;
  1078. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1079. }
  1080. return 1;
  1081. }
  1082. range = sstatus->rhw.range;
  1083. if(!sd || sd->status.weapon != W_BOW) range++; //Dunno why everyone but bows gets this extra range...
  1084. if(unit_is_walking(target)) range++; //Extra range when chasing
  1085. if(!check_distance_bl(src,target,range) ) {
  1086. //Chase if required.
  1087. if(ud->state.attack_continue) {
  1088. if(sd)
  1089. clif_movetoattack(sd,target);
  1090. else
  1091. unit_walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1092. }
  1093. return 1;
  1094. }
  1095. if(!battle_check_range(src,target,range)) {
  1096. //Within range, but no direct line of attack
  1097. if(ud->state.attack_continue) {
  1098. if(ud->chaserange > 2) ud->chaserange-=2;
  1099. unit_walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1100. }
  1101. return 1;
  1102. }
  1103. //Sync packet only for players.
  1104. //Non-players use the sync packet on the walk timer. [Skotlex]
  1105. if (tid == -1 && sd) clif_fixpos(src);
  1106. if(DIFF_TICK(ud->attackabletime,tick) <= 0) {
  1107. if (battle_config.attack_direction_change &&
  1108. (src->type&battle_config.attack_direction_change)) {
  1109. ud->dir = map_calc_dir(src, target->x,target->y );
  1110. if (sd) sd->head_dir = ud->dir;
  1111. }
  1112. if(ud->walktimer != -1)
  1113. unit_stop_walking(src,1);
  1114. if(md) {
  1115. if (mobskill_use(md,tick,-1))
  1116. return 1;
  1117. if (sstatus->mode&MD_ASSIST && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME)
  1118. { // Link monsters nearby [Skotlex]
  1119. md->last_linktime = tick;
  1120. map_foreachinrange(mob_linksearch, src, md->db->range2,
  1121. BL_MOB, md->class_, target, tick);
  1122. }
  1123. }
  1124. if(src->type == BL_PET && pet_attackskill((TBL_PET*)src, target->id))
  1125. return 1;
  1126. map_freeblock_lock();
  1127. ud->attacktarget_lv = battle_weapon_attack(src,target,tick,0);
  1128. if(sd && sd->status.pet_id > 0 && sd->pd && battle_config.pet_attack_support)
  1129. pet_target_check(sd,target,0);
  1130. map_freeblock_unlock();
  1131. ud->attackabletime = tick + sstatus->adelay;
  1132. // You can't move if you can't attack neither.
  1133. unit_set_walkdelay(src, tick, sstatus->amotion, 1);
  1134. }
  1135. if(ud->state.attack_continue)
  1136. ud->attacktimer = add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1137. return 1;
  1138. }
  1139. static int unit_attack_timer(int tid,unsigned int tick,int id,int data) {
  1140. struct block_list *bl;
  1141. bl = map_id2bl(id);
  1142. if(bl && unit_attack_timer_sub(bl, tid, tick) == 0)
  1143. unit_unattackable(bl);
  1144. return 0;
  1145. }
  1146. /*==========================================
  1147. * Cancels an ongoing skill cast.
  1148. * flag&1: Cast-Cancel invoked.
  1149. * flag&2: Cancel only if skill is cancellable.
  1150. *------------------------------------------
  1151. */
  1152. int unit_skillcastcancel(struct block_list *bl,int type)
  1153. {
  1154. struct map_session_data *sd = NULL;
  1155. struct unit_data *ud = unit_bl2ud( bl);
  1156. unsigned int tick=gettick();
  1157. int ret=0, skill;
  1158. nullpo_retr(0, bl);
  1159. if (!ud || ud->skilltimer==-1)
  1160. return 0; //Nothing to cancel.
  1161. BL_CAST(BL_PC, bl, sd);
  1162. if (type&2) {
  1163. //See if it can be cancelled.
  1164. if (!ud->state.skillcastcancel)
  1165. return 0;
  1166. if (sd && (sd->special_state.no_castcancel2 ||
  1167. (sd->special_state.no_castcancel && !map_flag_gvg(bl->m)))) //fixed flags being read the wrong way around [blackhole89]
  1168. return 0;
  1169. }
  1170. ud->canact_tick=tick;
  1171. if(sd && pc_checkskill(sd,SA_FREECAST))
  1172. status_freecast_switch(sd);
  1173. if(type&1 && sd)
  1174. skill = sd->skillid_old;
  1175. else
  1176. skill = ud->skillid;
  1177. if (skill_get_inf(skill) & INF_GROUND_SKILL)
  1178. ret=delete_timer( ud->skilltimer, skill_castend_pos );
  1179. else
  1180. ret=delete_timer( ud->skilltimer, skill_castend_id );
  1181. if(ret<0)
  1182. ShowError("delete timer error : skillid : %d\n",ret);
  1183. if(bl->type==BL_MOB) ((TBL_MOB*)bl)->skillidx = -1;
  1184. ud->skilltimer = -1;
  1185. clif_skillcastcancel(bl);
  1186. return 1;
  1187. }
  1188. // unit_data の初期化処理
  1189. void unit_dataset(struct block_list *bl) {
  1190. struct unit_data *ud;
  1191. nullpo_retv(ud = unit_bl2ud(bl));
  1192. memset( ud, 0, sizeof( struct unit_data) );
  1193. ud->bl = bl;
  1194. ud->walktimer = -1;
  1195. ud->skilltimer = -1;
  1196. ud->attacktimer = -1;
  1197. ud->attackabletime =
  1198. ud->canact_tick =
  1199. ud->canmove_tick = gettick();
  1200. }
  1201. /*==========================================
  1202. * 自分をロックしているユニットの数を数える(foreachclient)
  1203. *------------------------------------------
  1204. */
  1205. static int unit_counttargeted_sub(struct block_list *bl, va_list ap)
  1206. {
  1207. int id, target_lv;
  1208. struct unit_data *ud;
  1209. id = va_arg(ap,int);
  1210. target_lv = va_arg(ap,int);
  1211. if(bl->id == id)
  1212. return 0;
  1213. ud = unit_bl2ud(bl);
  1214. if (ud && ud->target == id && ud->attacktimer != -1 && ud->attacktarget_lv >= target_lv)
  1215. return 1;
  1216. return 0;
  1217. }
  1218. /*==========================================
  1219. *
  1220. *------------------------------------------
  1221. */
  1222. 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)
  1223. {
  1224. nullpo_retr(0, target);
  1225. if(damage+damage2 <= 0)
  1226. return 0;
  1227. return status_fix_damage(src,target,damage+damage2,clif_damage(target,target,tick,sdelay,ddelay,damage,div,type,damage2));
  1228. }
  1229. /*==========================================
  1230. * 自分をロックしている対象の数を返す
  1231. * 戻りは整数で0以上
  1232. *------------------------------------------
  1233. */
  1234. int unit_counttargeted(struct block_list *bl,int target_lv)
  1235. {
  1236. nullpo_retr(0, bl);
  1237. return (map_foreachinrange(unit_counttargeted_sub, bl, AREA_SIZE, BL_CHAR,
  1238. bl->id, target_lv));
  1239. }
  1240. /*==========================================
  1241. * 見た目のサイズを変更する
  1242. *------------------------------------------
  1243. */
  1244. int unit_changeviewsize(struct block_list *bl,short size)
  1245. {
  1246. nullpo_retr(0, bl);
  1247. size=(size<0)?-1:(size>0)?1:0;
  1248. if(bl->type == BL_PC) {
  1249. ((TBL_PC*)bl)->state.size=size;
  1250. } else if(bl->type == BL_MOB) {
  1251. ((TBL_MOB*)bl)->special_state.size=size;
  1252. } else
  1253. return 0;
  1254. if(size!=0)
  1255. clif_misceffect2(bl,421+size);
  1256. return 0;
  1257. }
  1258. /*==========================================
  1259. * Removes a bl/ud from the map.
  1260. * Returns 1 on success. 0 if it couldn't be removed or the bl was free'd
  1261. * if clrtype is 1 (death), appropiate cleanup is performed.
  1262. * Otherwise it is assumed bl is being warped.
  1263. * On-Kill specific stuff is not performed here, look at status_damage for that.
  1264. *------------------------------------------
  1265. */
  1266. int unit_remove_map(struct block_list *bl, int clrtype) {
  1267. struct unit_data *ud = unit_bl2ud(bl);
  1268. struct status_change *sc = status_get_sc(bl);
  1269. nullpo_retr(0, ud);
  1270. if(bl->prev == NULL)
  1271. return 0; //Already removed?
  1272. map_freeblock_lock();
  1273. ud->target = 0; //Unlock walk/attack target.
  1274. if (ud->walktimer != -1)
  1275. unit_stop_walking(bl,0);
  1276. if (ud->attacktimer != -1)
  1277. unit_stop_attack(bl);
  1278. if (ud->skilltimer != -1)
  1279. unit_skillcastcancel(bl,0);
  1280. // Do not reset can-act delay. [Skotlex]
  1281. ud->attackabletime = ud->canmove_tick /*= ud->canact_tick*/ = gettick();
  1282. if(sc && sc->count ) { //map-change/warp dispells.
  1283. if(sc->data[SC_BLADESTOP].timer!=-1)
  1284. status_change_end(bl,SC_BLADESTOP,-1);
  1285. if(sc->data[SC_BASILICA].timer!=-1)
  1286. status_change_end(bl,SC_BASILICA,-1);
  1287. if(sc->data[SC_ANKLE].timer != -1)
  1288. status_change_end(bl, SC_ANKLE, -1);
  1289. if (sc->data[SC_TRICKDEAD].timer != -1)
  1290. status_change_end(bl, SC_TRICKDEAD, -1);
  1291. if (sc->data[SC_BLADESTOP].timer!=-1)
  1292. status_change_end(bl,SC_BLADESTOP,-1);
  1293. if (sc->data[SC_RUN].timer!=-1)
  1294. status_change_end(bl,SC_RUN,-1);
  1295. if (sc->data[SC_DANCING].timer!=-1) // clear dance effect when warping [Valaris]
  1296. skill_stop_dancing(bl);
  1297. if (sc->data[SC_WARM].timer!=-1)
  1298. status_change_end(bl, SC_WARM, -1);
  1299. if (sc->data[SC_DEVOTION].timer!=-1)
  1300. status_change_end(bl,SC_DEVOTION,-1);
  1301. if (sc->data[SC_MARIONETTE].timer!=-1)
  1302. status_change_end(bl,SC_MARIONETTE,-1);
  1303. if (sc->data[SC_MARIONETTE2].timer!=-1)
  1304. status_change_end(bl,SC_MARIONETTE2,-1);
  1305. if (sc->data[SC_CLOSECONFINE].timer!=-1)
  1306. status_change_end(bl,SC_CLOSECONFINE,-1);
  1307. if (sc->data[SC_CLOSECONFINE2].timer!=-1)
  1308. status_change_end(bl,SC_CLOSECONFINE2,-1);
  1309. if (sc->data[SC_HIDING].timer!=-1)
  1310. status_change_end(bl, SC_HIDING, -1);
  1311. if (sc->data[SC_CLOAKING].timer!=-1)
  1312. status_change_end(bl, SC_CLOAKING, -1);
  1313. if (sc->data[SC_CHASEWALK].timer!=-1)
  1314. status_change_end(bl, SC_CHASEWALK, -1);
  1315. if (sc->data[SC_GOSPEL].timer != -1 && sc->data[SC_GOSPEL].val4 == BCT_SELF)
  1316. status_change_end(bl, SC_GOSPEL, -1);
  1317. }
  1318. if (bl->type&BL_CHAR) {
  1319. skill_unit_move(bl,gettick(),4);
  1320. skill_cleartimerskill(bl); // タイマースキルクリア
  1321. }
  1322. if(bl->type == BL_PC) {
  1323. struct map_session_data *sd = (struct map_session_data*)bl;
  1324. //Leave/reject all invitations.
  1325. if(sd->chatID)
  1326. chat_leavechat(sd);
  1327. if(sd->trade_partner)
  1328. trade_tradecancel(sd);
  1329. if(sd->vender_id)
  1330. vending_closevending(sd);
  1331. if(!sd->state.waitingdisconnect)
  1332. { //when quitting, let the final chrif_save handle storage saving.
  1333. if(sd->state.storage_flag == 1)
  1334. storage_storage_quit(sd,0);
  1335. else if (sd->state.storage_flag == 2)
  1336. storage_guild_storage_quit(sd,0);
  1337. }
  1338. if(sd->party_invite>0)
  1339. party_reply_invite(sd,sd->party_invite_account,0);
  1340. if(sd->guild_invite>0)
  1341. guild_reply_invite(sd,sd->guild_invite,0);
  1342. if(sd->guild_alliance>0)
  1343. guild_reply_reqalliance(sd,sd->guild_alliance_account,0);
  1344. pc_delinvincibletimer(sd);
  1345. if(sd->pvp_timer!=-1) {
  1346. delete_timer(sd->pvp_timer,pc_calc_pvprank_timer);
  1347. sd->pvp_timer = -1;
  1348. }
  1349. if(pc_issit(sd)) {
  1350. pc_setstand(sd);
  1351. skill_gangsterparadise(sd,0);
  1352. skill_rest(sd,0);
  1353. }
  1354. party_send_dot_remove(sd);//minimap dot fix [Kevin]
  1355. guild_send_dot_remove(sd);
  1356. } else if(bl->type == BL_MOB) {
  1357. struct mob_data *md = (struct mob_data*)bl;
  1358. md->target_id=0;
  1359. md->attacked_id=0;
  1360. md->state.skillstate= MSS_IDLE;
  1361. } else if (bl->type == BL_PET) {
  1362. struct pet_data *pd = (struct pet_data*)bl;
  1363. if(pd->pet.intimate <= 0) {
  1364. clif_clearchar_area(bl,clrtype);
  1365. map_delblock(bl);
  1366. unit_free(bl,0);
  1367. map_freeblock_unlock();
  1368. return 0;
  1369. }
  1370. } else if (bl->type == BL_HOM) {
  1371. struct homun_data *hd = (struct homun_data *) bl;
  1372. struct map_session_data *sd = hd->master;
  1373. if(!sd || !sd->homunculus.intimacy)
  1374. { //He's going to be deleted.
  1375. clif_emotion(bl, 28) ; //sob
  1376. clif_clearchar_area(bl,clrtype);
  1377. map_delblock(bl);
  1378. unit_free(bl,0);
  1379. map_freeblock_unlock();
  1380. return 0;
  1381. }
  1382. }
  1383. clif_clearchar_area(bl,clrtype);
  1384. map_delblock(bl);
  1385. map_freeblock_unlock();
  1386. return 1;
  1387. }
  1388. /*==========================================
  1389. * Function to free all related resources to the bl
  1390. * if unit is on map, it is removed using the clrtype specified
  1391. *------------------------------------------
  1392. */
  1393. int unit_free(struct block_list *bl, int clrtype) {
  1394. struct unit_data *ud = unit_bl2ud( bl );
  1395. nullpo_retr(0, ud);
  1396. map_freeblock_lock();
  1397. if( bl->prev ) //Players are supposed to logout with a "warp" effect.
  1398. unit_remove_map(bl, clrtype);
  1399. if( bl->type == BL_PC ) {
  1400. struct map_session_data *sd = (struct map_session_data*)bl;
  1401. if(status_isdead(bl))
  1402. pc_setrestartvalue(sd,2);
  1403. //Status that are not saved...
  1404. if(sd->sc.count) {
  1405. if(sd->sc.data[SC_SPURT].timer!=-1)
  1406. status_change_end(bl,SC_SPURT,-1);
  1407. if(sd->sc.data[SC_BERSERK].timer!=-1)
  1408. status_change_end(bl,SC_BERSERK,-1);
  1409. if(sd->sc.data[SC_TRICKDEAD].timer!=-1)
  1410. status_change_end(bl,SC_TRICKDEAD,-1);
  1411. if(sd->sc.data[SC_GUILDAURA].timer!=-1)
  1412. status_change_end(bl,SC_GUILDAURA,-1);
  1413. if (battle_config.debuff_on_logout&1) {
  1414. if(sd->sc.data[SC_ORCISH].timer!=-1)
  1415. status_change_end(bl,SC_ORCISH,-1);
  1416. if(sd->sc.data[SC_STRIPWEAPON].timer!=-1)
  1417. status_change_end(bl,SC_STRIPWEAPON,-1);
  1418. if(sd->sc.data[SC_STRIPARMOR].timer!=-1)
  1419. status_change_end(bl,SC_STRIPARMOR,-1);
  1420. if(sd->sc.data[SC_STRIPSHIELD].timer!=-1)
  1421. status_change_end(bl,SC_STRIPSHIELD,-1);
  1422. if(sd->sc.data[SC_STRIPHELM].timer!=-1)
  1423. status_change_end(bl,SC_STRIPHELM,-1);
  1424. if(sd->sc.data[SC_EXTREMITYFIST].timer!=-1)
  1425. status_change_end(bl,SC_EXTREMITYFIST,-1);
  1426. if(sd->sc.data[SC_EXPLOSIONSPIRITS].timer!=-1)
  1427. status_change_end(bl,SC_EXPLOSIONSPIRITS,-1);
  1428. }
  1429. if (battle_config.debuff_on_logout&2)
  1430. { //Food items are removed on logout.
  1431. if(sd->sc.data[SC_STRFOOD].timer!=-1)
  1432. status_change_end(bl,SC_STRFOOD,-1);
  1433. if(sd->sc.data[SC_AGIFOOD].timer!=-1)
  1434. status_change_end(bl,SC_AGIFOOD,-1);
  1435. if(sd->sc.data[SC_VITFOOD].timer!=-1)
  1436. status_change_end(bl,SC_VITFOOD,-1);
  1437. if(sd->sc.data[SC_INTFOOD].timer!=-1)
  1438. status_change_end(bl,SC_INTFOOD,-1);
  1439. if(sd->sc.data[SC_DEXFOOD].timer!=-1)
  1440. status_change_end(bl,SC_DEXFOOD,-1);
  1441. if(sd->sc.data[SC_LUKFOOD].timer!=-1)
  1442. status_change_end(bl,SC_LUKFOOD,-1);
  1443. if(sd->sc.data[SC_HITFOOD].timer!=-1)
  1444. status_change_end(bl,SC_HITFOOD,-1);
  1445. if(sd->sc.data[SC_FLEEFOOD].timer!=-1)
  1446. status_change_end(bl,SC_FLEEFOOD,-1);
  1447. if(sd->sc.data[SC_BATKFOOD].timer!=-1)
  1448. status_change_end(bl,SC_BATKFOOD,-1);
  1449. if(sd->sc.data[SC_WATKFOOD].timer!=-1)
  1450. status_change_end(bl,SC_WATKFOOD,-1);
  1451. if(sd->sc.data[SC_MATKFOOD].timer!=-1)
  1452. status_change_end(bl,SC_MATKFOOD,-1);
  1453. }
  1454. }
  1455. if (sd->followtimer != -1)
  1456. pc_stop_following(sd);
  1457. // Notify friends that this char logged out. [Skotlex]
  1458. clif_foreachclient(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
  1459. party_send_logout(sd);
  1460. guild_send_memberinfoshort(sd,0);
  1461. pc_cleareventtimer(sd);
  1462. pc_delspiritball(sd,sd->spiritball,1);
  1463. chrif_save_scdata(sd); //Save status changes, then clear'em out from memory. [Skotlex]
  1464. pc_makesavestatus(sd);
  1465. pc_clean_skilltree(sd);
  1466. } else if( bl->type == BL_PET ) {
  1467. struct pet_data *pd = (struct pet_data*)bl;
  1468. struct map_session_data *sd = pd->msd;
  1469. pet_hungry_timer_delete(pd);
  1470. if (pd->a_skill)
  1471. {
  1472. aFree(pd->a_skill);
  1473. pd->a_skill = NULL;
  1474. }
  1475. if (pd->s_skill)
  1476. {
  1477. if (pd->s_skill->timer != -1) {
  1478. if (pd->s_skill->id)
  1479. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  1480. else
  1481. delete_timer(pd->s_skill->timer, pet_heal_timer);
  1482. }
  1483. aFree(pd->s_skill);
  1484. pd->s_skill = NULL;
  1485. }
  1486. if(pd->recovery)
  1487. {
  1488. if(pd->recovery->timer != -1)
  1489. delete_timer(pd->recovery->timer, pet_recovery_timer);
  1490. aFree(pd->recovery);
  1491. pd->recovery = NULL;
  1492. }
  1493. if(pd->bonus)
  1494. {
  1495. if (pd->bonus->timer != -1)
  1496. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  1497. aFree(pd->bonus);
  1498. pd->bonus = NULL;
  1499. }
  1500. if (pd->loot)
  1501. {
  1502. pet_lootitem_drop(pd,sd);
  1503. if (pd->loot->item)
  1504. aFree(pd->loot->item);
  1505. aFree (pd->loot);
  1506. pd->loot = NULL;
  1507. }
  1508. if(pd->pet.intimate > 0)
  1509. intif_save_petdata(pd->pet.account_id,&pd->pet);
  1510. else
  1511. { //Remove pet.
  1512. intif_delete_petdata(pd->pet.pet_id);
  1513. if (sd) sd->status.pet_id = 0;
  1514. }
  1515. if (sd) sd->pd = NULL;
  1516. } else if(bl->type == BL_MOB) {
  1517. struct mob_data *md = (struct mob_data*)bl;
  1518. if(md->deletetimer!=-1) {
  1519. delete_timer(md->deletetimer,mob_timer_delete);
  1520. md->deletetimer=-1;
  1521. }
  1522. if(md->lootitem) {
  1523. aFree(md->lootitem);
  1524. md->lootitem=NULL;
  1525. }
  1526. if (md->guardian_data)
  1527. {
  1528. if (md->guardian_data->number < MAX_GUARDIANS)
  1529. md->guardian_data->castle->guardian[md->guardian_data->number].id = 0;
  1530. aFree(md->guardian_data);
  1531. md->guardian_data = NULL;
  1532. }
  1533. if (md->spawn && md->spawn_n < 0 && --(md->spawn->num) == 0)
  1534. { //Spawning data is not attached to the map, so free it
  1535. //if this is the last mob who is pointing at it.
  1536. aFree(md->spawn);
  1537. md->spawn = NULL;
  1538. }
  1539. if(md->base_status) {
  1540. aFree(md->base_status);
  1541. md->base_status = NULL;
  1542. }
  1543. if(mob_is_clone(md->class_))
  1544. mob_clone_delete(md->class_);
  1545. } else if(bl->type == BL_HOM) {
  1546. struct homun_data *hd = (TBL_HOM*)bl;
  1547. struct map_session_data *sd = hd->master;
  1548. // Desactive timers
  1549. merc_hom_hungry_timer_delete(hd);
  1550. if(sd) {
  1551. if (sd->homunculus.intimacy > 0)
  1552. merc_save(hd);
  1553. else
  1554. {
  1555. intif_homunculus_requestdelete(sd->homunculus.hom_id) ;
  1556. sd->status.hom_id = 0;
  1557. sd->homunculus.hom_id = 0;
  1558. }
  1559. sd->hd = NULL;
  1560. }
  1561. }
  1562. skill_clear_unitgroup(bl);
  1563. status_change_clear(bl,1);
  1564. if (bl->type != BL_PC)
  1565. { //Players are handled by map_quit
  1566. map_deliddb(bl);
  1567. map_freeblock(bl);
  1568. }
  1569. map_freeblock_unlock();
  1570. return 0;
  1571. }
  1572. int do_init_unit(void) {
  1573. add_timer_func_list(unit_attack_timer, "unit_attack_timer");
  1574. add_timer_func_list(unit_walktoxy_timer,"unit_walktoxy_timer");
  1575. add_timer_func_list(unit_walktobl_sub, "unit_walktobl_sub");
  1576. return 0;
  1577. }
  1578. int do_final_unit(void) {
  1579. // nothing to do
  1580. return 0;
  1581. }