unit.c 51 KB

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