unit.c 50 KB

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