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