unit.c 54 KB

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