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