unit.c 51 KB

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