unit.c 53 KB

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