unit.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  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 "../common/random.h"
  9. #include "unit.h"
  10. #include "map.h"
  11. #include "path.h"
  12. #include "pc.h"
  13. #include "mob.h"
  14. #include "pet.h"
  15. #include "homunculus.h"
  16. #include "instance.h"
  17. #include "mercenary.h"
  18. #include "skill.h"
  19. #include "clif.h"
  20. #include "duel.h"
  21. #include "npc.h"
  22. #include "guild.h"
  23. #include "status.h"
  24. #include "battle.h"
  25. #include "battleground.h"
  26. #include "chat.h"
  27. #include "trade.h"
  28. #include "vending.h"
  29. #include "party.h"
  30. #include "intif.h"
  31. #include "chrif.h"
  32. #include "script.h"
  33. #include "storage.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. const short dirx[8]={0,-1,-1,-1,0,1,1,1};
  38. const short diry[8]={1,1,0,-1,-1,-1,0,1};
  39. struct unit_data* unit_bl2ud(struct block_list *bl)
  40. {
  41. if( bl == NULL) return NULL;
  42. if( bl->type == BL_PC) return &((struct map_session_data*)bl)->ud;
  43. if( bl->type == BL_MOB) return &((struct mob_data*)bl)->ud;
  44. if( bl->type == BL_PET) return &((struct pet_data*)bl)->ud;
  45. if( bl->type == BL_NPC) return &((struct npc_data*)bl)->ud;
  46. if( bl->type == BL_HOM) return &((struct homun_data*)bl)->ud;
  47. if( bl->type == BL_MER) return &((struct mercenary_data*)bl)->ud;
  48. return NULL;
  49. }
  50. static int unit_attack_timer(int tid, unsigned int tick, int id, intptr_t data);
  51. static int unit_walktoxy_timer(int tid, unsigned int tick, int id, intptr_t data);
  52. int unit_walktoxy_sub(struct block_list *bl)
  53. {
  54. int i;
  55. struct walkpath_data wpd;
  56. struct unit_data *ud = NULL;
  57. nullpo_retr(1, bl);
  58. ud = unit_bl2ud(bl);
  59. if(ud == NULL) return 0;
  60. if( !path_search(&wpd,bl->m,bl->x,bl->y,ud->to_x,ud->to_y,ud->state.walk_easy,CELL_CHKNOPASS) )
  61. return 0;
  62. memcpy(&ud->walkpath,&wpd,sizeof(wpd));
  63. if (ud->target && ud->chaserange>1) {
  64. //Generally speaking, the walk path is already to an adjacent tile
  65. //so we only need to shorten the path if the range is greater than 1.
  66. int dir;
  67. //Trim the last part of the path to account for range,
  68. //but always move at least one cell when requested to move.
  69. for (i = ud->chaserange*10; i > 0 && ud->walkpath.path_len>1;) {
  70. ud->walkpath.path_len--;
  71. dir = ud->walkpath.path[ud->walkpath.path_len];
  72. if(dir&1)
  73. i-=14;
  74. else
  75. i-=10;
  76. ud->to_x -= dirx[dir];
  77. ud->to_y -= diry[dir];
  78. }
  79. }
  80. ud->state.change_walk_target=0;
  81. if (bl->type == BL_PC) {
  82. ((TBL_PC *)bl)->head_dir = 0;
  83. clif_walkok((TBL_PC*)bl);
  84. }
  85. clif_move(ud);
  86. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  87. i = -1;
  88. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  89. i = status_get_speed(bl)*14/10;
  90. else
  91. i = status_get_speed(bl);
  92. if( i > 0)
  93. ud->walktimer = add_timer(gettick()+i,unit_walktoxy_timer,bl->id,i);
  94. return 1;
  95. }
  96. static int unit_walktoxy_timer(int tid, unsigned int tick, int id, intptr_t data)
  97. {
  98. int i;
  99. int x,y,dx,dy;
  100. uint8 dir;
  101. struct block_list *bl;
  102. struct map_session_data *sd;
  103. struct mob_data *md;
  104. struct unit_data *ud;
  105. struct mercenary_data *mrd;
  106. bl = map_id2bl(id);
  107. if(bl == NULL)
  108. return 0;
  109. sd = BL_CAST(BL_PC, bl);
  110. md = BL_CAST(BL_MOB, bl);
  111. mrd = BL_CAST(BL_MER, bl);
  112. ud = unit_bl2ud(bl);
  113. if(ud == NULL) return 0;
  114. if(ud->walktimer != tid){
  115. ShowError("unit_walk_timer mismatch %d != %d\n",ud->walktimer,tid);
  116. return 0;
  117. }
  118. ud->walktimer = INVALID_TIMER;
  119. if( bl->prev == NULL ) return 0; // block_list から抜けているので移動停止する
  120. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  121. return 0;
  122. if(ud->walkpath.path[ud->walkpath.path_pos]>=8)
  123. return 1;
  124. x = bl->x;
  125. y = bl->y;
  126. dir = ud->walkpath.path[ud->walkpath.path_pos];
  127. ud->dir = dir;
  128. dx = dirx[(int)dir];
  129. dy = diry[(int)dir];
  130. if(map_getcell(bl->m,x+dx,y+dy,CELL_CHKNOPASS))
  131. return unit_walktoxy_sub(bl);
  132. // バシリカ判定
  133. map_foreachinmovearea(clif_outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
  134. x += dx;
  135. y += dy;
  136. map_moveblock(bl, x, y, tick);
  137. ud->walk_count++; //walked cell counter, to be used for walk-triggered skills. [Skotlex]
  138. if (bl->x != x || bl->y != y || ud->walktimer != INVALID_TIMER)
  139. return 0; //map_moveblock has altered the object beyond what we expected (moved/warped it)
  140. ud->walktimer = -2; // arbitrary non-INVALID_TIMER value to make the clif code send walking packets
  141. map_foreachinmovearea(clif_insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
  142. ud->walktimer = INVALID_TIMER;
  143. if(sd) {
  144. if( sd->touching_id )
  145. npc_touchnext_areanpc(sd,false);
  146. if(map_getcell(bl->m,x,y,CELL_CHKNPC)) {
  147. npc_touch_areanpc(sd,bl->m,x,y);
  148. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  149. return 0;
  150. } else
  151. sd->areanpc_id=0;
  152. if( sd->md && !check_distance_bl(&sd->bl, &sd->md->bl, MAX_MER_DISTANCE) )
  153. {// mercenary is too far from the master so warp the master's position
  154. unit_warp( &sd->md->bl, sd->bl.m, sd->bl.x, sd->bl.y, CLR_TELEPORT );
  155. }
  156. if (sd->state.gmaster_flag &&
  157. (battle_config.guild_aura&((agit_flag || agit2_flag)?2:1)) &&
  158. (battle_config.guild_aura&(map_flag_gvg2(bl->m)?8:4))
  159. )
  160. { //Guild Aura: Likely needs to be recoded, this method seems inefficient.
  161. struct guild *g = sd->state.gmaster_flag;
  162. int skill, strvit= 0, agidex = 0;
  163. if ((skill = guild_checkskill(g, GD_LEADERSHIP)) > 0) strvit |= (skill&0xFFFF)<<16;
  164. if ((skill = guild_checkskill(g, GD_GLORYWOUNDS)) > 0) strvit |= (skill&0xFFFF);
  165. if ((skill = guild_checkskill(g, GD_SOULCOLD)) > 0) agidex |= (skill&0xFFFF)<<16;
  166. if ((skill = guild_checkskill(g, GD_HAWKEYES)) > 0) agidex |= skill&0xFFFF;
  167. if (strvit || agidex)
  168. {// replaced redundant foreachinrange call with smaller and much more efficient iteration
  169. for( i = 0; i < g->max_member; i++ )
  170. {
  171. if( g->member[i].online && g->member[i].sd && sd->bl.m == g->member[i].sd->bl.m && check_distance_bl(&sd->bl, &g->member[i].sd->bl, 2) )
  172. {// perform the aura on the member as appropriate
  173. skill_guildaura_sub(g->member[i].sd, sd->bl.id, strvit, agidex);
  174. }
  175. }
  176. }
  177. }
  178. } else if (md) {
  179. if( map_getcell(bl->m,x,y,CELL_CHKNPC) ) {
  180. if( npc_touch_areanpc2(md) ) return 0; // Warped
  181. } else
  182. md->areanpc_id = 0;
  183. if (md->min_chase > md->db->range3) md->min_chase--;
  184. //Walk skills are triggered regardless of target due to the idle-walk mob state.
  185. //But avoid triggering on stop-walk calls.
  186. if(tid != INVALID_TIMER &&
  187. !(ud->walk_count%WALK_SKILL_INTERVAL) &&
  188. mobskill_use(md, tick, -1))
  189. {
  190. if (!(ud->skillid == NPC_SELFDESTRUCTION && ud->skilltimer != INVALID_TIMER))
  191. { //Skill used, abort walking
  192. clif_fixpos(bl); //Fix position as walk has been cancelled.
  193. return 0;
  194. }
  195. //Resend walk packet for proper Self Destruction display.
  196. clif_move(ud);
  197. }
  198. }
  199. else if( mrd && mrd->master && !check_distance_bl(&mrd->master->bl, bl, MAX_MER_DISTANCE) )
  200. {// mercenary is too far from the master so warp the master's position
  201. unit_warp( bl, mrd->master->bl.id, mrd->master->bl.x, mrd->master->bl.y, CLR_TELEPORT );
  202. }
  203. if(tid == INVALID_TIMER) //A directly invoked timer is from battle_stop_walking, therefore the rest is irrelevant.
  204. return 0;
  205. if(ud->state.change_walk_target)
  206. return unit_walktoxy_sub(bl);
  207. ud->walkpath.path_pos++;
  208. if(ud->walkpath.path_pos>=ud->walkpath.path_len)
  209. i = -1;
  210. else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
  211. i = status_get_speed(bl)*14/10;
  212. else
  213. i = status_get_speed(bl);
  214. if(i > 0) {
  215. ud->walktimer = add_timer(tick+i,unit_walktoxy_timer,id,i);
  216. if( md )
  217. clif_move(ud);
  218. } else if(ud->state.running) {
  219. //Keep trying to run.
  220. if ( !(unit_run(bl) || unit_wugdash(bl,sd)) )
  221. ud->state.running = 0;
  222. }
  223. else if (ud->target) {
  224. //Update target trajectory.
  225. struct block_list *tbl = map_id2bl(ud->target);
  226. if (!tbl || !status_check_visibility(bl, tbl)) { //Cancel chase.
  227. ud->to_x = bl->x;
  228. ud->to_y = bl->y;
  229. if (tbl && bl->type == BL_MOB) //See if the mob can do a warp chase.
  230. mob_warpchase((TBL_MOB*)bl, tbl);
  231. return 0;
  232. }
  233. if (tbl->m == bl->m && check_distance_bl(bl, tbl, ud->chaserange))
  234. { //Reached destination.
  235. if (ud->state.attack_continue)
  236. { //Aegis uses one before every attack, we should
  237. //only need this one for syncing purposes. [Skotlex]
  238. clif_fixpos(bl);
  239. unit_attack(bl, tbl->id, ud->state.attack_continue);
  240. }
  241. } else { //Update chase-path
  242. unit_walktobl(bl, tbl, ud->chaserange, ud->state.walk_easy|(ud->state.attack_continue?2:0));
  243. return 0;
  244. }
  245. }
  246. else { //Stopped walking. Update to_x and to_y to current location [Skotlex]
  247. ud->to_x = bl->x;
  248. ud->to_y = bl->y;
  249. }
  250. return 0;
  251. }
  252. static int unit_delay_walktoxy_timer(int tid, unsigned int tick, int id, intptr_t data)
  253. {
  254. struct block_list *bl = map_id2bl(id);
  255. if (!bl || bl->prev == NULL)
  256. return 0;
  257. unit_walktoxy(bl, (short)((data>>16)&0xffff), (short)(data&0xffff), 0);
  258. return 1;
  259. }
  260. //flag parameter:
  261. //&1 -> 1/0 = easy/hard
  262. //&2 -> force walking
  263. //&4 -> Delay walking if the reason you can't walk is the canwalk delay
  264. int unit_walktoxy( struct block_list *bl, short x, short y, int flag)
  265. {
  266. struct unit_data* ud = NULL;
  267. struct status_change* sc = NULL;
  268. nullpo_ret(bl);
  269. ud = unit_bl2ud(bl);
  270. if( ud == NULL) return 0;
  271. if (flag&4 && DIFF_TICK(ud->canmove_tick, gettick()) > 0 &&
  272. DIFF_TICK(ud->canmove_tick, gettick()) < 2000)
  273. { // Delay walking command. [Skotlex]
  274. add_timer(ud->canmove_tick+1, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF));
  275. return 1;
  276. }
  277. if(!(flag&2) && (!(status_get_mode(bl)&MD_CANMOVE) || !unit_can_move(bl)))
  278. return 0;
  279. ud->state.walk_easy = flag&1;
  280. ud->target = 0;
  281. ud->to_x = x;
  282. ud->to_y = y;
  283. sc = status_get_sc(bl);
  284. if (sc && sc->data[SC_CONFUSION]) //Randomize the target position
  285. map_random_dir(bl, &ud->to_x, &ud->to_y);
  286. if(ud->walktimer != INVALID_TIMER) {
  287. // 現在歩いている最中の目的地変更なのでマス目の中心に来た時に
  288. // timer関数からunit_walktoxy_subを呼ぶようにする
  289. ud->state.change_walk_target = 1;
  290. return 1;
  291. }
  292. if(ud->attacktimer != INVALID_TIMER) {
  293. delete_timer( ud->attacktimer, unit_attack_timer );
  294. ud->attacktimer = INVALID_TIMER;
  295. }
  296. return unit_walktoxy_sub(bl);
  297. }
  298. //To set Mob's CHASE/FOLLOW states (shouldn't be done if there's no path to reach)
  299. static inline void set_mobstate(struct block_list* bl, int flag)
  300. {
  301. struct mob_data* md = BL_CAST(BL_MOB,bl);
  302. if( md && flag )
  303. md->state.skillstate = md->state.aggressive ? MSS_FOLLOW : MSS_RUSH;
  304. }
  305. static int unit_walktobl_sub(int tid, unsigned int tick, int id, intptr_t data)
  306. {
  307. struct block_list *bl = map_id2bl(id);
  308. struct unit_data *ud = bl?unit_bl2ud(bl):NULL;
  309. if (ud && ud->walktimer == INVALID_TIMER && ud->target == data)
  310. {
  311. if (DIFF_TICK(ud->canmove_tick, tick) > 0) //Keep waiting?
  312. add_timer(ud->canmove_tick+1, unit_walktobl_sub, id, data);
  313. else if (unit_can_move(bl))
  314. {
  315. if (unit_walktoxy_sub(bl))
  316. set_mobstate(bl, ud->state.attack_continue);
  317. }
  318. }
  319. return 0;
  320. }
  321. // Chases a tbl. If the flag&1, use hard-path seek,
  322. // if flag&2, start attacking upon arrival within range, otherwise just walk to that character.
  323. int unit_walktobl(struct block_list *bl, struct block_list *tbl, int range, int flag)
  324. {
  325. struct unit_data *ud = NULL;
  326. struct status_change *sc = NULL;
  327. nullpo_ret(bl);
  328. nullpo_ret(tbl);
  329. ud = unit_bl2ud(bl);
  330. if( ud == NULL) return 0;
  331. if (!(status_get_mode(bl)&MD_CANMOVE))
  332. return 0;
  333. if (!unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, flag&1, &ud->to_x, &ud->to_y)) {
  334. ud->to_x = bl->x;
  335. ud->to_y = bl->y;
  336. return 0;
  337. }
  338. ud->state.walk_easy = flag&1;
  339. ud->target = tbl->id;
  340. ud->chaserange = range; //Note that if flag&2, this SHOULD be attack-range
  341. ud->state.attack_continue = flag&2?1:0; //Chase to attack.
  342. sc = status_get_sc(bl);
  343. if (sc && sc->data[SC_CONFUSION]) //Randomize the target position
  344. map_random_dir(bl, &ud->to_x, &ud->to_y);
  345. if(ud->walktimer != INVALID_TIMER) {
  346. ud->state.change_walk_target = 1;
  347. set_mobstate(bl, flag&2);
  348. return 1;
  349. }
  350. if(DIFF_TICK(ud->canmove_tick, gettick()) > 0)
  351. { //Can't move, wait a bit before invoking the movement.
  352. add_timer(ud->canmove_tick+1, unit_walktobl_sub, bl->id, ud->target);
  353. return 1;
  354. }
  355. if(!unit_can_move(bl))
  356. return 0;
  357. if(ud->attacktimer != INVALID_TIMER) {
  358. delete_timer( ud->attacktimer, unit_attack_timer );
  359. ud->attacktimer = INVALID_TIMER;
  360. }
  361. if (unit_walktoxy_sub(bl)) {
  362. set_mobstate(bl, flag&2);
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. #undef set_mobstate
  368. int unit_run(struct block_list *bl)
  369. {
  370. struct status_change *sc = status_get_sc(bl);
  371. short to_x,to_y,dir_x,dir_y;
  372. int lv;
  373. int i;
  374. if (!(sc && sc->data[SC_RUN]))
  375. return 0;
  376. if (!unit_can_move(bl)) {
  377. status_change_end(bl, SC_RUN, INVALID_TIMER);
  378. return 0;
  379. }
  380. lv = sc->data[SC_RUN]->val1;
  381. dir_x = dirx[sc->data[SC_RUN]->val2];
  382. dir_y = diry[sc->data[SC_RUN]->val2];
  383. // determine destination cell
  384. to_x = bl->x;
  385. to_y = bl->y;
  386. for(i=0;i<AREA_SIZE;i++)
  387. {
  388. if(!map_getcell(bl->m,to_x+dir_x,to_y+dir_y,CELL_CHKPASS))
  389. break;
  390. //if sprinting and there's a PC/Mob/NPC, block the path [Kevin]
  391. if(sc->data[SC_RUN] && map_count_oncell(bl->m, to_x+dir_x, to_y+dir_y, BL_PC|BL_MOB|BL_NPC))
  392. break;
  393. to_x += dir_x;
  394. to_y += dir_y;
  395. }
  396. if( (to_x == bl->x && to_y == bl->y ) || (to_x == (bl->x+1) || to_y == (bl->y+1)) || (to_x == (bl->x-1) || to_y == (bl->y-1))) {
  397. //If you can't run forward, you must be next to a wall, so bounce back. [Skotlex]
  398. clif_status_change(bl, SI_BUMP, 1, 0, 0, 0, 0);
  399. //Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
  400. unit_bl2ud(bl)->state.running = 0;
  401. status_change_end(bl, SC_RUN, INVALID_TIMER);
  402. skill_blown(bl,bl,skill_get_blewcount(TK_RUN,lv),unit_getdir(bl),0);
  403. clif_fixpos(bl); //Why is a clif_slide (skill_blown) AND a fixpos needed? Ask Aegis.
  404. clif_status_change(bl, SI_BUMP, 0, 0, 0, 0, 0);
  405. return 0;
  406. }
  407. if (unit_walktoxy(bl, to_x, to_y, 1))
  408. return 1;
  409. //There must be an obstacle nearby. Attempt walking one cell at a time.
  410. do {
  411. to_x -= dir_x;
  412. to_y -= dir_y;
  413. } while (--i > 0 && !unit_walktoxy(bl, to_x, to_y, 1));
  414. if (i==0) {
  415. // copy-paste from above
  416. clif_status_change(bl, SI_BUMP, 1, 0, 0, 0, 0);
  417. //Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
  418. unit_bl2ud(bl)->state.running = 0;
  419. status_change_end(bl, SC_RUN, INVALID_TIMER);
  420. skill_blown(bl,bl,skill_get_blewcount(TK_RUN,lv),unit_getdir(bl),0);
  421. clif_fixpos(bl);
  422. clif_status_change(bl, SI_BUMP, 0, 0, 0, 0, 0);
  423. return 0;
  424. }
  425. return 1;
  426. }
  427. //Exclusive function to Wug Dash state. [Jobbie/3CeAM]
  428. int unit_wugdash(struct block_list *bl, struct map_session_data *sd) {
  429. struct status_change *sc = status_get_sc(bl);
  430. short to_x,to_y,dir_x,dir_y;
  431. int lv;
  432. int i;
  433. if (!(sc && sc->data[SC_WUGDASH]))
  434. return 0;
  435. nullpo_ret(sd);
  436. nullpo_ret(bl);
  437. if (!unit_can_move(bl)) {
  438. status_change_end(bl,SC_WUGDASH,-1);
  439. return 0;
  440. }
  441. lv = sc->data[SC_WUGDASH]->val1;
  442. dir_x = dirx[sc->data[SC_WUGDASH]->val2];
  443. dir_y = diry[sc->data[SC_WUGDASH]->val2];
  444. to_x = bl->x;
  445. to_y = bl->y;
  446. for(i=0;i<AREA_SIZE;i++)
  447. {
  448. if(!map_getcell(bl->m,to_x+dir_x,to_y+dir_y,CELL_CHKPASS))
  449. break;
  450. if(sc->data[SC_WUGDASH] && map_count_oncell(bl->m, to_x+dir_x, to_y+dir_y, BL_PC|BL_MOB|BL_NPC))
  451. break;
  452. to_x += dir_x;
  453. to_y += dir_y;
  454. }
  455. if(to_x == bl->x && to_y == bl->y) {
  456. unit_bl2ud(bl)->state.running = 0;
  457. status_change_end(bl,SC_WUGDASH,-1);
  458. if( sd ){
  459. clif_fixpos(bl);
  460. skill_castend_damage_id(bl, &sd->bl, RA_WUGDASH, lv, gettick(), SD_LEVEL);
  461. }
  462. return 0;
  463. }
  464. if (unit_walktoxy(bl, to_x, to_y, 1))
  465. return 1;
  466. do {
  467. to_x -= dir_x;
  468. to_y -= dir_y;
  469. } while (--i > 0 && !unit_walktoxy(bl, to_x, to_y, 1));
  470. if (i==0) {
  471. unit_bl2ud(bl)->state.running = 0;
  472. status_change_end(bl,SC_WUGDASH,-1);
  473. if( sd ){
  474. clif_fixpos(bl);
  475. skill_castend_damage_id(bl, &sd->bl, RA_WUGDASH, lv, gettick(), SD_LEVEL);
  476. }
  477. return 0;
  478. }
  479. return 1;
  480. }
  481. //Makes bl attempt to run dist cells away from target. Uses hard-paths.
  482. int unit_escape(struct block_list *bl, struct block_list *target, short dist)
  483. {
  484. int dir = map_calc_dir(target, bl->x, bl->y);
  485. while( dist > 0 && map_getcell(bl->m, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], CELL_CHKNOREACH) )
  486. dist--;
  487. return ( dist > 0 && unit_walktoxy(bl, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], 0) );
  488. }
  489. //Instant warp function.
  490. int unit_movepos(struct block_list *bl, short dst_x, short dst_y, int easy, bool checkpath)
  491. {
  492. short dx,dy;
  493. uint8 dir;
  494. struct unit_data *ud = NULL;
  495. struct map_session_data *sd = NULL;
  496. nullpo_ret(bl);
  497. sd = BL_CAST(BL_PC, bl);
  498. ud = unit_bl2ud(bl);
  499. if( ud == NULL) return 0;
  500. unit_stop_walking(bl,1);
  501. unit_stop_attack(bl);
  502. 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)) )
  503. return 0; // unreachable
  504. dir = map_calc_dir(bl, dst_x,dst_y);
  505. ud->dir = dir;
  506. dx = dst_x - bl->x;
  507. dy = dst_y - bl->y;
  508. map_foreachinmovearea(clif_outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
  509. map_moveblock(bl, dst_x, dst_y, gettick());
  510. ud->walktimer = -2; // arbitrary non-INVALID_TIMER value to make the clif code send walking packets
  511. map_foreachinmovearea(clif_insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
  512. ud->walktimer = INVALID_TIMER;
  513. if(sd) {
  514. if( sd->touching_id )
  515. npc_touchnext_areanpc(sd,false);
  516. if(map_getcell(bl->m,bl->x,bl->y,CELL_CHKNPC)) {
  517. npc_touch_areanpc(sd,bl->m,bl->x,bl->y);
  518. if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
  519. return 0;
  520. } else
  521. sd->areanpc_id=0;
  522. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  523. { // Check if pet needs to be teleported. [Skotlex]
  524. int flag = 0;
  525. struct block_list* bl = &sd->pd->bl;
  526. if( !checkpath && !path_search(NULL,bl->m,bl->x,bl->y,dst_x,dst_y,0,CELL_CHKNOPASS) )
  527. flag = 1;
  528. else if (!check_distance_bl(&sd->bl, bl, AREA_SIZE)) //Too far, teleport.
  529. flag = 2;
  530. if( flag )
  531. {
  532. unit_movepos(bl,sd->bl.x,sd->bl.y, 0, 0);
  533. clif_slide(bl,bl->x,bl->y);
  534. }
  535. }
  536. }
  537. return 1;
  538. }
  539. int unit_setdir(struct block_list *bl,unsigned char dir)
  540. {
  541. struct unit_data *ud;
  542. nullpo_ret(bl );
  543. ud = unit_bl2ud(bl);
  544. if (!ud) return 0;
  545. ud->dir = dir;
  546. if (bl->type == BL_PC)
  547. ((TBL_PC *)bl)->head_dir = 0;
  548. clif_changed_dir(bl, AREA);
  549. return 0;
  550. }
  551. uint8 unit_getdir(struct block_list *bl)
  552. {
  553. struct unit_data *ud;
  554. nullpo_ret(bl );
  555. ud = unit_bl2ud(bl);
  556. if (!ud) return 0;
  557. return ud->dir;
  558. }
  559. // Pushes a unit by given amount of cells into given direction. Only
  560. // map cell restrictions are respected.
  561. // flag:
  562. // &1 Do not send position update packets.
  563. int unit_blown(struct block_list* bl, int dx, int dy, int count, int flag)
  564. {
  565. if(count)
  566. {
  567. struct map_session_data* sd;
  568. struct skill_unit* su = NULL;
  569. int nx, ny, result;
  570. sd = BL_CAST(BL_PC, bl);
  571. su = BL_CAST(BL_SKILL, bl);
  572. result = path_blownpos(bl->m, bl->x, bl->y, dx, dy, count);
  573. nx = result>>16;
  574. ny = result&0xffff;
  575. if(!su)
  576. {
  577. unit_stop_walking(bl, 0);
  578. }
  579. dx = nx-bl->x;
  580. dy = ny-bl->y;
  581. if(dx || dy)
  582. {
  583. map_foreachinmovearea(clif_outsight, bl, AREA_SIZE, dx, dy, bl->type == BL_PC ? BL_ALL : BL_PC, bl);
  584. if(su)
  585. {
  586. skill_unit_move_unit_group(su->group, bl->m, dx, dy);
  587. }
  588. else
  589. {
  590. map_moveblock(bl, nx, ny, gettick());
  591. }
  592. map_foreachinmovearea(clif_insight, bl, AREA_SIZE, -dx, -dy, bl->type == BL_PC ? BL_ALL : BL_PC, bl);
  593. if(!(flag&1))
  594. {
  595. clif_blown(bl);
  596. }
  597. if(sd)
  598. {
  599. if(sd->touching_id)
  600. {
  601. npc_touchnext_areanpc(sd, false);
  602. }
  603. if(map_getcell(bl->m, bl->x, bl->y, CELL_CHKNPC))
  604. {
  605. npc_touch_areanpc(sd, bl->m, bl->x, bl->y);
  606. }
  607. else
  608. {
  609. sd->areanpc_id = 0;
  610. }
  611. }
  612. }
  613. count = distance(dx, dy);
  614. }
  615. return count; // return amount of knocked back cells
  616. }
  617. //Warps a unit/ud to a given map/position.
  618. //In the case of players, pc_setpos is used.
  619. //it respects the no warp flags, so it is safe to call this without doing nowarpto/nowarp checks.
  620. int unit_warp(struct block_list *bl,short m,short x,short y,clr_type type)
  621. {
  622. struct unit_data *ud;
  623. nullpo_ret(bl);
  624. ud = unit_bl2ud(bl);
  625. if(bl->prev==NULL || !ud)
  626. return 1;
  627. if (type == CLR_DEAD)
  628. //Type 1 is invalid, since you shouldn't warp a bl with the "death"
  629. //animation, it messes up with unit_remove_map! [Skotlex]
  630. return 1;
  631. if( m<0 ) m=bl->m;
  632. switch (bl->type) {
  633. case BL_MOB:
  634. if (map[bl->m].flag.monster_noteleport && ((TBL_MOB*)bl)->master_id == 0)
  635. return 1;
  636. if (m != bl->m && map[m].flag.nobranch && battle_config.mob_warp&4)
  637. return 1;
  638. break;
  639. case BL_PC:
  640. if (map[bl->m].flag.noteleport)
  641. return 1;
  642. break;
  643. }
  644. if (x<0 || y<0)
  645. { //Random map position.
  646. if (!map_search_freecell(NULL, m, &x, &y, -1, -1, 1)) {
  647. 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);
  648. return 2;
  649. }
  650. } else if (map_getcell(m,x,y,CELL_CHKNOREACH))
  651. { //Invalid target cell
  652. ShowWarning("unit_warp: Specified non-walkable target cell: %d (%s) at [%d,%d]\n", m, map[m].name, x,y);
  653. if (!map_search_freecell(NULL, m, &x, &y, 4, 4, 1))
  654. { //Can't find a nearby cell
  655. 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);
  656. return 2;
  657. }
  658. }
  659. if (bl->type == BL_PC) //Use pc_setpos
  660. return pc_setpos((TBL_PC*)bl, map_id2index(m), x, y, type);
  661. if (!unit_remove_map(bl, type))
  662. return 3;
  663. if (bl->m != m && battle_config.clear_unit_onwarp &&
  664. battle_config.clear_unit_onwarp&bl->type)
  665. skill_clear_unitgroup(bl);
  666. bl->x=ud->to_x=x;
  667. bl->y=ud->to_y=y;
  668. bl->m=m;
  669. map_addblock(bl);
  670. clif_spawn(bl);
  671. skill_unit_move(bl,gettick(),1);
  672. return 0;
  673. }
  674. /*==========================================
  675. * Caused the target object to stop moving.
  676. * Flag values:
  677. * &0x1: Issue a fixpos packet afterwards
  678. * &0x2: Force the unit to move one cell if it hasn't yet
  679. * &0x4: Enable moving to the next cell when unit was already half-way there
  680. * (may cause on-touch/place side-effects, such as a scripted map change)
  681. *------------------------------------------*/
  682. int unit_stop_walking(struct block_list *bl,int type)
  683. {
  684. struct unit_data *ud;
  685. const struct TimerData* td;
  686. unsigned int tick;
  687. nullpo_ret(bl);
  688. ud = unit_bl2ud(bl);
  689. if(!ud || ud->walktimer == INVALID_TIMER)
  690. return 0;
  691. //NOTE: We are using timer data after deleting it because we know the
  692. //delete_timer function does not messes with it. If the function's
  693. //behaviour changes in the future, this code could break!
  694. td = get_timer(ud->walktimer);
  695. delete_timer(ud->walktimer, unit_walktoxy_timer);
  696. ud->walktimer = INVALID_TIMER;
  697. ud->state.change_walk_target = 0;
  698. tick = gettick();
  699. if( (type&0x02 && !ud->walkpath.path_pos) //Force moving at least one cell.
  700. || (type&0x04 && td && DIFF_TICK(td->tick, tick) <= td->data/2) //Enough time has passed to cover half-cell
  701. ) {
  702. ud->walkpath.path_len = ud->walkpath.path_pos+1;
  703. unit_walktoxy_timer(INVALID_TIMER, tick, bl->id, ud->walkpath.path_pos);
  704. }
  705. if(type&0x01)
  706. clif_fixpos(bl);
  707. ud->walkpath.path_len = 0;
  708. ud->walkpath.path_pos = 0;
  709. ud->to_x = bl->x;
  710. ud->to_y = bl->y;
  711. if(bl->type == BL_PET && type&~0xff)
  712. ud->canmove_tick = gettick() + (type>>8);
  713. //Readded, the check in unit_set_walkdelay means dmg during running won't fall through to this place in code [Kevin]
  714. if (ud->state.running) {
  715. status_change_end(bl, SC_RUN, INVALID_TIMER);
  716. status_change_end(bl, SC_WUGDASH, INVALID_TIMER);
  717. }
  718. return 1;
  719. }
  720. int unit_skilluse_id(struct block_list *src, int target_id, short skill_num, short skill_lv)
  721. {
  722. if(skill_num < 0) return 0;
  723. return unit_skilluse_id2(
  724. src, target_id, skill_num, skill_lv,
  725. skill_castfix(src, skill_num, skill_lv),
  726. skill_get_castcancel(skill_num)
  727. );
  728. }
  729. int unit_is_walking(struct block_list *bl)
  730. {
  731. struct unit_data *ud = unit_bl2ud(bl);
  732. nullpo_ret(bl);
  733. if(!ud) return 0;
  734. return (ud->walktimer != INVALID_TIMER);
  735. }
  736. /*==========================================
  737. * Determines if the bl can move based on status changes. [Skotlex]
  738. *------------------------------------------*/
  739. int unit_can_move(struct block_list *bl)
  740. {
  741. struct map_session_data *sd;
  742. struct unit_data *ud;
  743. struct status_change *sc;
  744. nullpo_ret(bl);
  745. ud = unit_bl2ud(bl);
  746. sc = status_get_sc(bl);
  747. sd = BL_CAST(BL_PC, bl);
  748. if (!ud)
  749. return 0;
  750. if (ud->skilltimer != INVALID_TIMER && ud->skillid != LG_EXEEDBREAK && (!sd || !pc_checkskill(sd, SA_FREECAST) || skill_get_inf2(ud->skillid)&INF2_GUILD_SKILL))
  751. return 0; // prevent moving while casting
  752. if (DIFF_TICK(ud->canmove_tick, gettick()) > 0)
  753. return 0;
  754. if (sd && (
  755. pc_issit(sd) ||
  756. sd->state.vending ||
  757. sd->state.buyingstore ||
  758. sd->state.blockedmove
  759. ))
  760. return 0; //Can't move
  761. if (sc) {
  762. if (sc->opt1 > 0 && sc->opt1 != OPT1_STONEWAIT && sc->opt1 != OPT1_BURNING)
  763. return 0;
  764. if ((sc->option & OPTION_HIDE) && (!sd || pc_checkskill(sd, RG_TUNNELDRIVE) <= 0))
  765. return 0;
  766. if (sc->count && (
  767. sc->data[SC_ANKLE]
  768. || sc->data[SC_AUTOCOUNTER]
  769. || sc->data[SC_TRICKDEAD]
  770. || sc->data[SC_BLADESTOP]
  771. || sc->data[SC_BLADESTOP_WAIT]
  772. || (sc->data[SC_SPIDERWEB] && sc->data[SC_SPIDERWEB]->val1)
  773. || (sc->data[SC_DANCING] && sc->data[SC_DANCING]->val4 && (
  774. !sc->data[SC_LONGING] ||
  775. (sc->data[SC_DANCING]->val1&0xFFFF) == CG_MOONLIT ||
  776. (sc->data[SC_DANCING]->val1&0xFFFF) == CG_HERMODE
  777. ))
  778. || (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF) // cannot move while gospel is in effect
  779. || (sc->data[SC_BASILICA] && sc->data[SC_BASILICA]->val4 == bl->id) // Basilica caster cannot move
  780. || sc->data[SC_STOP]
  781. || sc->data[SC_CLOSECONFINE]
  782. || sc->data[SC_CLOSECONFINE2]
  783. || (sc->data[SC_CLOAKING] && //Need wall at level 1-2
  784. sc->data[SC_CLOAKING]->val1 < 3 && !(sc->data[SC_CLOAKING]->val4&1))
  785. || sc->data[SC_MADNESSCANCEL]
  786. || (sc->data[SC_GRAVITATION] && sc->data[SC_GRAVITATION]->val3 == BCT_SELF)
  787. ))
  788. return 0;
  789. }
  790. return 1;
  791. }
  792. /*==========================================
  793. * Resume running after a walk delay
  794. *------------------------------------------*/
  795. int unit_resume_running(int tid, unsigned int tick, int id, intptr_t data)
  796. {
  797. struct unit_data *ud = (struct unit_data *)data;
  798. TBL_PC * sd = map_id2sd(id);
  799. if(sd && pc_isridingwug(sd))
  800. clif_skill_nodamage(ud->bl,ud->bl,RA_WUGDASH,ud->skilllv,
  801. sc_start4(ud->bl,status_skill2sc(RA_WUGDASH),100,ud->skilllv,unit_getdir(ud->bl),0,0,1));
  802. else
  803. clif_skill_nodamage(ud->bl,ud->bl,TK_RUN,ud->skilllv,
  804. sc_start4(ud->bl,status_skill2sc(TK_RUN),100,ud->skilllv,unit_getdir(ud->bl),0,0,0));
  805. if (sd) clif_walkok(sd);
  806. return 0;
  807. }
  808. /*==========================================
  809. * Applies walk delay to character, considering that
  810. * if type is 0, this is a damage induced delay: if previous delay is active, do not change it.
  811. * if type is 1, this is a skill induced delay: walk-delay may only be increased, not decreased.
  812. *------------------------------------------*/
  813. int unit_set_walkdelay(struct block_list *bl, unsigned int tick, int delay, int type)
  814. {
  815. struct unit_data *ud = unit_bl2ud(bl);
  816. if (delay <= 0 || !ud) return 0;
  817. /**
  818. * MvP mobs have no walk delay
  819. **/
  820. if( bl->type == BL_MOB && (((TBL_MOB*)bl)->status.mode&MD_BOSS) )
  821. return 0;
  822. if (type) {
  823. if (DIFF_TICK(ud->canmove_tick, tick+delay) > 0)
  824. return 0;
  825. } else {
  826. //Don't set walk delays when already trapped.
  827. if (!unit_can_move(bl))
  828. return 0;
  829. }
  830. ud->canmove_tick = tick + delay;
  831. if (ud->walktimer != INVALID_TIMER)
  832. { //Stop walking, if chasing, readjust timers.
  833. if (delay == 1)
  834. { //Minimal delay (walk-delay) disabled. Just stop walking.
  835. unit_stop_walking(bl,4);
  836. } else {
  837. //Resume running after can move again [Kevin]
  838. if(ud->state.running)
  839. {
  840. add_timer(ud->canmove_tick, unit_resume_running, bl->id, (intptr_t)ud);
  841. }
  842. else
  843. {
  844. unit_stop_walking(bl,2|4);
  845. if(ud->target)
  846. add_timer(ud->canmove_tick+1, unit_walktobl_sub, bl->id, ud->target);
  847. }
  848. }
  849. }
  850. return 1;
  851. }
  852. int unit_skilluse_id2(struct block_list *src, int target_id, short skill_num, short skill_lv, int casttime, int castcancel)
  853. {
  854. struct unit_data *ud;
  855. struct status_data *tstatus;
  856. struct status_change *sc;
  857. struct map_session_data *sd = NULL;
  858. struct block_list * target = NULL;
  859. unsigned int tick = gettick();
  860. int temp = 0;
  861. nullpo_ret(src);
  862. if(status_isdead(src))
  863. return 0; // 死んでいないか
  864. sd = BL_CAST(BL_PC, src);
  865. ud = unit_bl2ud(src);
  866. if(ud == NULL) return 0;
  867. sc = status_get_sc(src);
  868. if (sc && !sc->count)
  869. sc = NULL; //Unneeded
  870. //temp: used to signal combo-skills right now.
  871. if (sc && sc->data[SC_COMBO] && (sc->data[SC_COMBO]->val1 == skill_num || skill_num == MO_EXTREMITYFIST))
  872. {
  873. if (sc->data[SC_COMBO]->val2)
  874. target_id = sc->data[SC_COMBO]->val2;
  875. else
  876. target_id = ud->target;
  877. temp = 1;
  878. } else
  879. if ( target_id == src->id &&
  880. skill_get_inf(skill_num)&INF_SELF_SKILL &&
  881. skill_get_inf2(skill_num)&INF2_NO_TARGET_SELF )
  882. {
  883. target_id = ud->target; //Auto-select target. [Skotlex]
  884. temp = 1;
  885. }
  886. if (sd) {
  887. //Target_id checking.
  888. if(skillnotok(skill_num, sd)) // [MouseJstr]
  889. return 0;
  890. switch(skill_num)
  891. { //Check for skills that auto-select target
  892. case MO_CHAINCOMBO:
  893. if (sc && sc->data[SC_BLADESTOP]){
  894. if ((target=map_id2bl(sc->data[SC_BLADESTOP]->val4)) == NULL)
  895. return 0;
  896. }
  897. break;
  898. case WE_MALE:
  899. case WE_FEMALE:
  900. if (!sd->status.partner_id)
  901. return 0;
  902. target = (struct block_list*)map_charid2sd(sd->status.partner_id);
  903. if (!target) {
  904. clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  905. return 0;
  906. }
  907. break;
  908. }
  909. if (target)
  910. target_id = target->id;
  911. }
  912. if (src->type==BL_HOM)
  913. switch(skill_num)
  914. { //Homun-auto-target skills.
  915. case HLIF_HEAL:
  916. case HLIF_AVOID:
  917. case HAMI_DEFENCE:
  918. case HAMI_CASTLE:
  919. target = battle_get_master(src);
  920. if (!target) return 0;
  921. target_id = target->id;
  922. }
  923. if( !target ) // choose default target
  924. target = map_id2bl(target_id);
  925. if( !target || src->m != target->m || !src->prev || !target->prev )
  926. return 0;
  927. if( battle_config.ksprotection && sd && mob_ksprotected(src, target) )
  928. return 0;
  929. //Normally not needed because clif.c checks for it, but the at/char/script commands don't! [Skotlex]
  930. if(ud->skilltimer != INVALID_TIMER && skill_num != SA_CASTCANCEL)
  931. return 0;
  932. if(skill_get_inf2(skill_num)&INF2_NO_TARGET_SELF && src->id == target_id)
  933. return 0;
  934. if(!status_check_skilluse(src, target, skill_num, 0))
  935. return 0;
  936. tstatus = status_get_status_data(target);
  937. //直前のスキル状況の記録
  938. if(sd) {
  939. switch(skill_num){
  940. case SA_CASTCANCEL:
  941. if(ud->skillid != skill_num){
  942. sd->skillid_old = ud->skillid;
  943. sd->skilllv_old = ud->skilllv;
  944. }
  945. break;
  946. case BD_ENCORE:
  947. //Prevent using the dance skill if you no longer have the skill in your tree.
  948. if(!sd->skillid_dance || pc_checkskill(sd,sd->skillid_dance)<=0){
  949. clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  950. return 0;
  951. }
  952. sd->skillid_old = skill_num;
  953. break;
  954. case BD_LULLABY:
  955. case BD_RICHMANKIM:
  956. case BD_ETERNALCHAOS:
  957. case BD_DRUMBATTLEFIELD:
  958. case BD_RINGNIBELUNGEN:
  959. case BD_ROKISWEIL:
  960. case BD_INTOABYSS:
  961. case BD_SIEGFRIED:
  962. case CG_MOONLIT:
  963. if (skill_check_pc_partner(sd, skill_num, &skill_lv, 1, 0) < 1)
  964. {
  965. clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  966. return 0;
  967. }
  968. break;
  969. }
  970. if (!skill_check_condition_castbegin(sd, skill_num, skill_lv))
  971. return 0;
  972. }
  973. if( src->type == BL_MOB )
  974. switch( skill_num )
  975. {
  976. case NPC_SUMMONSLAVE:
  977. case NPC_SUMMONMONSTER:
  978. case AL_TELEPORT:
  979. if( ((TBL_MOB*)src)->master_id && ((TBL_MOB*)src)->special_state.ai )
  980. return 0;
  981. }
  982. //Check range when not using skill on yourself or is a combo-skill during attack
  983. //(these are supposed to always have the same range as your attack)
  984. if( src->id != target_id && (!temp || ud->attacktimer == INVALID_TIMER) )
  985. {
  986. if( skill_get_state(ud->skillid) == ST_MOVE_ENABLE )
  987. {
  988. if( !unit_can_reach_bl(src, target, skill_get_range2(src, skill_num,skill_lv) + 1, 1, NULL, NULL) )
  989. return 0; // Walk-path check failed.
  990. }
  991. else if( src->type == BL_MER && skill_num == MA_REMOVETRAP )
  992. {
  993. if( !battle_check_range(battle_get_master(src), target, skill_get_range2(src, skill_num, skill_lv) + 1) )
  994. return 0; // Aegis calc remove trap based on Master position, ignoring mercenary O.O
  995. }
  996. else if( !battle_check_range(src, target, skill_get_range2(src, skill_num,skill_lv) + (skill_num == RG_CLOSECONFINE?0:2)) )
  997. return 0; // Arrow-path check failed.
  998. }
  999. if (!temp) //Stop attack on non-combo skills [Skotlex]
  1000. unit_stop_attack(src);
  1001. else if(ud->attacktimer != INVALID_TIMER) //Elsewise, delay current attack sequence
  1002. ud->attackabletime = tick + status_get_adelay(src);
  1003. ud->state.skillcastcancel = castcancel;
  1004. //temp: Used to signal force cast now.
  1005. temp = 0;
  1006. switch(skill_num){
  1007. case ALL_RESURRECTION:
  1008. if(battle_check_undead(tstatus->race,tstatus->def_ele)) {
  1009. temp = 1;
  1010. } else if (!status_isdead(target))
  1011. return 0; //Can't cast on non-dead characters.
  1012. break;
  1013. case MO_FINGEROFFENSIVE:
  1014. if(sd)
  1015. casttime += casttime * min(skill_lv, sd->spiritball);
  1016. break;
  1017. case MO_EXTREMITYFIST:
  1018. if (sc && sc->data[SC_COMBO] &&
  1019. (sc->data[SC_COMBO]->val1 == MO_COMBOFINISH ||
  1020. sc->data[SC_COMBO]->val1 == CH_TIGERFIST ||
  1021. sc->data[SC_COMBO]->val1 == CH_CHAINCRUSH))
  1022. casttime = 0;
  1023. temp = 1;
  1024. break;
  1025. case SA_SPELLBREAKER:
  1026. temp = 1;
  1027. break;
  1028. case ST_CHASEWALK:
  1029. if (sc && sc->data[SC_CHASEWALK])
  1030. casttime = 0;
  1031. break;
  1032. case TK_RUN:
  1033. if (sc && sc->data[SC_RUN])
  1034. casttime = 0;
  1035. break;
  1036. case HP_BASILICA:
  1037. if( sc && sc->data[SC_BASILICA] )
  1038. casttime = 0; // No Casting time on basilica cancel
  1039. break;
  1040. case KN_CHARGEATK:
  1041. {
  1042. unsigned int k = (distance_bl(src,target)-1)/3; //+100% every 3 cells of distance
  1043. if( k > 2 ) k = 2; // ...but hard-limited to 300%.
  1044. casttime += casttime * k;
  1045. }
  1046. break;
  1047. case GD_EMERGENCYCALL: //Emergency Call double cast when the user has learned Leap [Daegaladh]
  1048. if( sd && pc_checkskill(sd,TK_HIGHJUMP) )
  1049. casttime *= 2;
  1050. break;
  1051. case RA_WUGDASH:
  1052. if (sc && sc->data[SC_WUGDASH])
  1053. casttime = 0;
  1054. }
  1055. // moved here to prevent Suffragium from ending if skill fails
  1056. if (!(skill_get_castnodex(skill_num, skill_lv)&2))
  1057. casttime = skill_castfix_sc(src, casttime, skill_num, skill_lv);
  1058. if( casttime > 0 || temp )
  1059. {
  1060. unit_stop_walking(src,1);
  1061. clif_skillcasting(src, src->id, target_id, 0,0, skill_num, skill_get_ele(skill_num, skill_lv), casttime);
  1062. if (sd && target->type == BL_MOB)
  1063. {
  1064. TBL_MOB *md = (TBL_MOB*)target;
  1065. mobskill_event(md, src, tick, -1); //Cast targetted skill event.
  1066. if (tstatus->mode&(MD_CASTSENSOR_IDLE|MD_CASTSENSOR_CHASE) &&
  1067. battle_check_target(target, src, BCT_ENEMY) > 0)
  1068. {
  1069. switch (md->state.skillstate) {
  1070. case MSS_RUSH:
  1071. case MSS_FOLLOW:
  1072. if (!(tstatus->mode&MD_CASTSENSOR_CHASE))
  1073. break;
  1074. md->target_id = src->id;
  1075. md->state.aggressive = (tstatus->mode&MD_ANGRY)?1:0;
  1076. md->min_chase = md->db->range3;
  1077. break;
  1078. case MSS_IDLE:
  1079. case MSS_WALK:
  1080. if (!(tstatus->mode&MD_CASTSENSOR_IDLE))
  1081. break;
  1082. md->target_id = src->id;
  1083. md->state.aggressive = (tstatus->mode&MD_ANGRY)?1:0;
  1084. md->min_chase = md->db->range3;
  1085. break;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. if( casttime <= 0 )
  1091. ud->state.skillcastcancel = 0;
  1092. if( !sd || sd->skillitem != skill_num || skill_get_cast(skill_num,skill_lv) )
  1093. ud->canact_tick = tick + casttime + 100;
  1094. if( sd )
  1095. {
  1096. switch( skill_num )
  1097. {
  1098. case CG_ARROWVULCAN:
  1099. sd->canequip_tick = tick + casttime;
  1100. break;
  1101. }
  1102. }
  1103. ud->skilltarget = target_id;
  1104. ud->skillx = 0;
  1105. ud->skilly = 0;
  1106. ud->skillid = skill_num;
  1107. ud->skilllv = skill_lv;
  1108. if( sc && sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4) && skill_num != AS_CLOAKING )
  1109. {
  1110. status_change_end(src, SC_CLOAKING, INVALID_TIMER);
  1111. if (!src->prev) return 0; //Warped away!
  1112. }
  1113. if( sc && sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4&4) && skill_num != GC_CLOAKINGEXCEED )
  1114. {
  1115. status_change_end(src,SC_CLOAKINGEXCEED, INVALID_TIMER);
  1116. if (!src->prev) return 0;
  1117. }
  1118. if( casttime > 0 )
  1119. {
  1120. ud->skilltimer = add_timer( tick+casttime, skill_castend_id, src->id, 0 );
  1121. if( sd && (pc_checkskill(sd,SA_FREECAST) > 0 || skill_num == LG_EXEEDBREAK) )
  1122. status_calc_bl(&sd->bl, SCB_SPEED);
  1123. }
  1124. else
  1125. skill_castend_id(ud->skilltimer,tick,src->id,0);
  1126. return 1;
  1127. }
  1128. int unit_skilluse_pos(struct block_list *src, short skill_x, short skill_y, short skill_num, short skill_lv)
  1129. {
  1130. if(skill_num < 0)
  1131. return 0;
  1132. return unit_skilluse_pos2(
  1133. src, skill_x, skill_y, skill_num, skill_lv,
  1134. skill_castfix(src, skill_num, skill_lv),
  1135. skill_get_castcancel(skill_num)
  1136. );
  1137. }
  1138. int unit_skilluse_pos2( struct block_list *src, short skill_x, short skill_y, short skill_num, short skill_lv, int casttime, int castcancel)
  1139. {
  1140. struct map_session_data *sd = NULL;
  1141. struct unit_data *ud = NULL;
  1142. struct status_change *sc;
  1143. struct block_list bl;
  1144. unsigned int tick = gettick();
  1145. nullpo_ret(src);
  1146. if(!src->prev) return 0; // map 上に存在するか
  1147. if(status_isdead(src)) return 0;
  1148. sd = BL_CAST(BL_PC, src);
  1149. ud = unit_bl2ud(src);
  1150. if(ud == NULL) return 0;
  1151. if(ud->skilltimer != INVALID_TIMER) //Normally not needed since clif.c checks for it, but at/char/script commands don't! [Skotlex]
  1152. return 0;
  1153. sc = status_get_sc(src);
  1154. if (sc && !sc->count)
  1155. sc = NULL;
  1156. if( sd )
  1157. {
  1158. if( skillnotok(skill_num, sd) || !skill_check_condition_castbegin(sd, skill_num, skill_lv) )
  1159. return 0;
  1160. /**
  1161. * "WHY IS IT HEREE": pneuma cannot be cancelled past this point, the client displays the animation even,
  1162. * if we cancel it from nodamage_id, so it has to be here for it to not display the animation.
  1163. **/
  1164. if( skill_num == AL_PNEUMA && map_getcell(src->m, skill_x, skill_y, CELL_CHKLANDPROTECTOR) ) {
  1165. clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  1166. return 0;
  1167. }
  1168. }
  1169. if (!status_check_skilluse(src, NULL, skill_num, 0))
  1170. return 0;
  1171. if( map_getcell(src->m, skill_x, skill_y, CELL_CHKWALL) )
  1172. {// can't cast ground targeted spells on wall cells
  1173. if (sd) clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  1174. return 0;
  1175. }
  1176. /* 射程と障害物チェック */
  1177. bl.type = BL_NUL;
  1178. bl.m = src->m;
  1179. bl.x = skill_x;
  1180. bl.y = skill_y;
  1181. if( skill_get_state(ud->skillid) == ST_MOVE_ENABLE )
  1182. {
  1183. if( !unit_can_reach_bl(src, &bl, skill_get_range2(src, skill_num,skill_lv) + 1, 1, NULL, NULL) )
  1184. return 0; //Walk-path check failed.
  1185. }
  1186. else if( !battle_check_range(src, &bl, skill_get_range2(src, skill_num,skill_lv) + 1) )
  1187. return 0; //Arrow-path check failed.
  1188. unit_stop_attack(src);
  1189. // moved here to prevent Suffragium from ending if skill fails
  1190. if (!(skill_get_castnodex(skill_num, skill_lv)&2))
  1191. casttime = skill_castfix_sc(src, casttime, skill_num, skill_lv);
  1192. ud->state.skillcastcancel = castcancel&&casttime>0?1:0;
  1193. if( !sd || sd->skillitem != skill_num || skill_get_cast(skill_num,skill_lv) )
  1194. ud->canact_tick = tick + casttime + 100;
  1195. // if( sd )
  1196. // {
  1197. // switch( skill_num )
  1198. // {
  1199. // case ????:
  1200. // sd->canequip_tick = tick + casttime;
  1201. // }
  1202. // }
  1203. ud->skillid = skill_num;
  1204. ud->skilllv = skill_lv;
  1205. ud->skillx = skill_x;
  1206. ud->skilly = skill_y;
  1207. ud->skilltarget = 0;
  1208. if (sc && sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4))
  1209. {
  1210. status_change_end(src, SC_CLOAKING, INVALID_TIMER);
  1211. if (!src->prev) return 0; //Warped away!
  1212. }
  1213. if (sc && sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4&4))
  1214. {
  1215. status_change_end(src, SC_CLOAKINGEXCEED, INVALID_TIMER);
  1216. if (!src->prev) return 0;
  1217. }
  1218. if( casttime > 0 )
  1219. {
  1220. unit_stop_walking(src,1);
  1221. clif_skillcasting(src, src->id, 0, skill_x, skill_y, skill_num, skill_get_ele(skill_num, skill_lv), casttime);
  1222. ud->skilltimer = add_timer( tick+casttime, skill_castend_pos, src->id, 0 );
  1223. if( (sd && pc_checkskill(sd,SA_FREECAST) > 0) || skill_num == LG_EXEEDBREAK)
  1224. status_calc_bl(&sd->bl, SCB_SPEED);
  1225. }
  1226. else
  1227. {
  1228. ud->skilltimer = INVALID_TIMER;
  1229. skill_castend_pos(ud->skilltimer,tick,src->id,0);
  1230. }
  1231. return 1;
  1232. }
  1233. int unit_stop_attack(struct block_list *bl)
  1234. {
  1235. struct unit_data *ud = unit_bl2ud(bl);
  1236. nullpo_ret(bl);
  1237. if(!ud || ud->attacktimer == INVALID_TIMER)
  1238. return 0;
  1239. delete_timer( ud->attacktimer, unit_attack_timer );
  1240. ud->attacktimer = INVALID_TIMER;
  1241. ud->target = 0;
  1242. return 0;
  1243. }
  1244. //Means current target is unattackable. For now only unlocks mobs.
  1245. int unit_unattackable(struct block_list *bl)
  1246. {
  1247. struct unit_data *ud = unit_bl2ud(bl);
  1248. if (ud) {
  1249. ud->target = 0;
  1250. ud->state.attack_continue = 0;
  1251. }
  1252. if(bl->type == BL_MOB)
  1253. mob_unlocktarget((struct mob_data*)bl, gettick()) ;
  1254. else if(bl->type == BL_PET)
  1255. pet_unlocktarget((struct pet_data*)bl);
  1256. return 0;
  1257. }
  1258. /*==========================================
  1259. * 攻撃要求
  1260. * typeが1なら継続攻撃
  1261. *------------------------------------------*/
  1262. int unit_attack(struct block_list *src,int target_id,int continuous)
  1263. {
  1264. struct block_list *target;
  1265. struct unit_data *ud;
  1266. nullpo_ret(ud = unit_bl2ud(src));
  1267. target = map_id2bl(target_id);
  1268. if( target==NULL || status_isdead(target) )
  1269. {
  1270. unit_unattackable(src);
  1271. return 1;
  1272. }
  1273. if( src->type == BL_PC )
  1274. {
  1275. TBL_PC* sd = (TBL_PC*)src;
  1276. if( target->type == BL_NPC )
  1277. { // monster npcs [Valaris]
  1278. npc_click(sd,(TBL_NPC*)target); // submitted by leinsirk10 [Celest]
  1279. return 0;
  1280. }
  1281. if( pc_is90overweight(sd) )
  1282. { // overweight - stop attacking
  1283. unit_stop_attack(src);
  1284. return 0;
  1285. }
  1286. }
  1287. if( battle_check_target(src,target,BCT_ENEMY) <= 0 || !status_check_skilluse(src, target, 0, 0) )
  1288. {
  1289. unit_unattackable(src);
  1290. return 1;
  1291. }
  1292. ud->target = target_id;
  1293. ud->state.attack_continue = continuous;
  1294. if (continuous) //If you're to attack continously, set to auto-case character
  1295. ud->chaserange = status_get_range(src);
  1296. //Just change target/type. [Skotlex]
  1297. if(ud->attacktimer != INVALID_TIMER)
  1298. return 0;
  1299. //Set Mob's ANGRY/BERSERK states.
  1300. if(src->type == BL_MOB)
  1301. ((TBL_MOB*)src)->state.skillstate = ((TBL_MOB*)src)->state.aggressive?MSS_ANGRY:MSS_BERSERK;
  1302. if(DIFF_TICK(ud->attackabletime, gettick()) > 0)
  1303. //Do attack next time it is possible. [Skotlex]
  1304. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1305. else //Attack NOW.
  1306. unit_attack_timer(INVALID_TIMER, gettick(), src->id, 0);
  1307. return 0;
  1308. }
  1309. //Cancels an ongoing combo, resets attackable time and restarts the
  1310. //attack timer to resume attacking after amotion time. [Skotlex]
  1311. int unit_cancel_combo(struct block_list *bl)
  1312. {
  1313. struct unit_data *ud;
  1314. if (!status_change_end(bl, SC_COMBO, INVALID_TIMER))
  1315. return 0; //Combo wasn't active.
  1316. ud = unit_bl2ud(bl);
  1317. nullpo_ret(ud);
  1318. ud->attackabletime = gettick() + status_get_amotion(bl);
  1319. if (ud->attacktimer == INVALID_TIMER)
  1320. return 1; //Nothing more to do.
  1321. delete_timer(ud->attacktimer, unit_attack_timer);
  1322. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,bl->id,0);
  1323. return 1;
  1324. }
  1325. /*==========================================
  1326. *
  1327. *------------------------------------------*/
  1328. bool unit_can_reach_pos(struct block_list *bl,int x,int y, int easy)
  1329. {
  1330. nullpo_retr(false, bl);
  1331. if( bl->x==x && bl->y==y ) // 同じマス
  1332. return true;
  1333. return path_search(NULL,bl->m,bl->x,bl->y,x,y,easy,CELL_CHKNOREACH);
  1334. }
  1335. /*==========================================
  1336. *
  1337. *------------------------------------------*/
  1338. bool unit_can_reach_bl(struct block_list *bl,struct block_list *tbl, int range, int easy, short *x, short *y)
  1339. {
  1340. int i;
  1341. short dx,dy;
  1342. nullpo_retr(false, bl);
  1343. nullpo_retr(false, tbl);
  1344. if( bl->m != tbl->m)
  1345. return false;
  1346. if( bl->x==tbl->x && bl->y==tbl->y )
  1347. return true;
  1348. if(range>0 && !check_distance_bl(bl, tbl, range))
  1349. return false;
  1350. // It judges whether it can adjoin or not.
  1351. dx=tbl->x - bl->x;
  1352. dy=tbl->y - bl->y;
  1353. dx=(dx>0)?1:((dx<0)?-1:0);
  1354. dy=(dy>0)?1:((dy<0)?-1:0);
  1355. if (map_getcell(tbl->m,tbl->x-dx,tbl->y-dy,CELL_CHKNOPASS))
  1356. { //Look for a suitable cell to place in.
  1357. for(i=0;i<9 && map_getcell(tbl->m,tbl->x-dirx[i],tbl->y-diry[i],CELL_CHKNOPASS);i++);
  1358. if (i==9) return false; //No valid cells.
  1359. dx = dirx[i];
  1360. dy = diry[i];
  1361. }
  1362. if (x) *x = tbl->x-dx;
  1363. if (y) *y = tbl->y-dy;
  1364. return path_search(NULL,bl->m,bl->x,bl->y,tbl->x-dx,tbl->y-dy,easy,CELL_CHKNOREACH);
  1365. }
  1366. /*==========================================
  1367. * Calculates position of Pet/Mercenary/Homunculus
  1368. *------------------------------------------*/
  1369. int unit_calc_pos(struct block_list *bl, int tx, int ty, int dir)
  1370. {
  1371. int dx, dy, x, y, i, k;
  1372. struct unit_data *ud = unit_bl2ud(bl);
  1373. nullpo_ret(ud);
  1374. if( dir < 0 || dir > 7 )
  1375. return 1;
  1376. ud->to_x = tx;
  1377. ud->to_y = ty;
  1378. // 2 cells from Master Position
  1379. dx = -dirx[dir] * 2;
  1380. dy = -diry[dir] * 2;
  1381. x = tx + dx;
  1382. y = ty + dy;
  1383. if( !unit_can_reach_pos(bl, x, y, 0) )
  1384. {
  1385. if( dx > 0 ) x--; else if( dx < 0 ) x++;
  1386. if( dy > 0 ) y--; else if( dy < 0 ) y++;
  1387. if( !unit_can_reach_pos(bl, x, y, 0) )
  1388. {
  1389. for( i = 0; i < 12; i++ )
  1390. {
  1391. k = rnd()%8; // Pick a Random Dir
  1392. dx = -dirx[k] * 2;
  1393. dy = -diry[k] * 2;
  1394. x = tx + dx;
  1395. y = ty + dy;
  1396. if( unit_can_reach_pos(bl, x, y, 0) )
  1397. break;
  1398. else
  1399. {
  1400. if( dx > 0 ) x--; else if( dx < 0 ) x++;
  1401. if( dy > 0 ) y--; else if( dy < 0 ) y++;
  1402. if( unit_can_reach_pos(bl, x, y, 0) )
  1403. break;
  1404. }
  1405. }
  1406. if( i == 12 )
  1407. {
  1408. x = tx; y = tx; // Exactly Master Position
  1409. if( !unit_can_reach_pos(bl, x, y, 0) )
  1410. return 1;
  1411. }
  1412. }
  1413. }
  1414. ud->to_x = x;
  1415. ud->to_y = y;
  1416. return 0;
  1417. }
  1418. /*==========================================
  1419. * PCの攻撃 (timer関数)
  1420. *------------------------------------------*/
  1421. static int unit_attack_timer_sub(struct block_list* src, int tid, unsigned int tick)
  1422. {
  1423. struct block_list *target;
  1424. struct unit_data *ud;
  1425. struct status_data *sstatus;
  1426. struct map_session_data *sd = NULL;
  1427. struct mob_data *md = NULL;
  1428. int range;
  1429. if( (ud=unit_bl2ud(src))==NULL )
  1430. return 0;
  1431. if( ud->attacktimer != tid )
  1432. {
  1433. ShowError("unit_attack_timer %d != %d\n",ud->attacktimer,tid);
  1434. return 0;
  1435. }
  1436. sd = BL_CAST(BL_PC, src);
  1437. md = BL_CAST(BL_MOB, src);
  1438. ud->attacktimer = INVALID_TIMER;
  1439. target=map_id2bl(ud->target);
  1440. if( src == NULL || src->prev == NULL || target==NULL || target->prev == NULL )
  1441. return 0;
  1442. if( status_isdead(src) || status_isdead(target) ||
  1443. battle_check_target(src,target,BCT_ENEMY) <= 0 || !status_check_skilluse(src, target, 0, 0) )
  1444. return 0; // can't attack under these conditions
  1445. if( src->m != target->m )
  1446. {
  1447. if( src->type == BL_MOB && mob_warpchase((TBL_MOB*)src, target) )
  1448. return 1; // Follow up.
  1449. return 0;
  1450. }
  1451. if( ud->skilltimer != INVALID_TIMER && !(sd && pc_checkskill(sd,SA_FREECAST) > 0) )
  1452. return 0; // can't attack while casting
  1453. if( !battle_config.sdelay_attack_enable && DIFF_TICK(ud->canact_tick,tick) > 0 && !(sd && pc_checkskill(sd,SA_FREECAST) > 0) )
  1454. { // attacking when under cast delay has restrictions:
  1455. if( tid == INVALID_TIMER )
  1456. { //requested attack.
  1457. if(sd) clif_skill_fail(sd,1,USESKILL_FAIL_SKILLINTERVAL,0);
  1458. return 0;
  1459. }
  1460. //Otherwise, we are in a combo-attack, delay this until your canact time is over. [Skotlex]
  1461. if( ud->state.attack_continue )
  1462. {
  1463. if( DIFF_TICK(ud->canact_tick, ud->attackabletime) > 0 )
  1464. ud->attackabletime = ud->canact_tick;
  1465. ud->attacktimer=add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1466. }
  1467. return 1;
  1468. }
  1469. sstatus = status_get_status_data(src);
  1470. range = sstatus->rhw.range;
  1471. if( !sd || sd->status.weapon != W_BOW )
  1472. range++; //Dunno why everyone but bows gets this extra range...
  1473. if( unit_is_walking(target) )
  1474. range++; //Extra range when chasing
  1475. if( !check_distance_bl(src,target,range) )
  1476. { //Chase if required.
  1477. if(sd)
  1478. clif_movetoattack(sd,target);
  1479. else if(ud->state.attack_continue)
  1480. unit_walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1481. return 1;
  1482. }
  1483. if( !battle_check_range(src,target,range) )
  1484. {
  1485. //Within range, but no direct line of attack
  1486. if( ud->state.attack_continue )
  1487. {
  1488. if(ud->chaserange > 2) ud->chaserange-=2;
  1489. unit_walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
  1490. }
  1491. return 1;
  1492. }
  1493. //Sync packet only for players.
  1494. //Non-players use the sync packet on the walk timer. [Skotlex]
  1495. if (tid == INVALID_TIMER && sd) clif_fixpos(src);
  1496. if( DIFF_TICK(ud->attackabletime,tick) <= 0 )
  1497. {
  1498. if (battle_config.attack_direction_change && (src->type&battle_config.attack_direction_change)) {
  1499. ud->dir = map_calc_dir(src, target->x,target->y );
  1500. }
  1501. if(ud->walktimer != INVALID_TIMER)
  1502. unit_stop_walking(src,1);
  1503. if(md) {
  1504. if (mobskill_use(md,tick,-1))
  1505. return 1;
  1506. if (sstatus->mode&MD_ASSIST && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME)
  1507. { // Link monsters nearby [Skotlex]
  1508. md->last_linktime = tick;
  1509. map_foreachinrange(mob_linksearch, src, md->db->range2, BL_MOB, md->class_, target, tick);
  1510. }
  1511. }
  1512. if(src->type == BL_PET && pet_attackskill((TBL_PET*)src, target->id))
  1513. return 1;
  1514. map_freeblock_lock();
  1515. ud->attacktarget_lv = battle_weapon_attack(src,target,tick,0);
  1516. if(sd && sd->status.pet_id > 0 && sd->pd && battle_config.pet_attack_support)
  1517. pet_target_check(sd,target,0);
  1518. map_freeblock_unlock();
  1519. /**
  1520. * Applied when you're unable to attack (e.g. out of ammo)
  1521. * We should stop here otherwise timer keeps on and this happens endlessly
  1522. **/
  1523. if( ud->attacktarget_lv == ATK_NONE )
  1524. return 1;
  1525. ud->attackabletime = tick + sstatus->adelay;
  1526. // You can't move if you can't attack neither.
  1527. if (src->type&battle_config.attack_walk_delay)
  1528. unit_set_walkdelay(src, tick, sstatus->amotion, 1);
  1529. }
  1530. if(ud->state.attack_continue)
  1531. ud->attacktimer = add_timer(ud->attackabletime,unit_attack_timer,src->id,0);
  1532. return 1;
  1533. }
  1534. static int unit_attack_timer(int tid, unsigned int tick, int id, intptr_t data)
  1535. {
  1536. struct block_list *bl;
  1537. bl = map_id2bl(id);
  1538. if(bl && unit_attack_timer_sub(bl, tid, tick) == 0)
  1539. unit_unattackable(bl);
  1540. return 0;
  1541. }
  1542. /*==========================================
  1543. * Cancels an ongoing skill cast.
  1544. * flag&1: Cast-Cancel invoked.
  1545. * flag&2: Cancel only if skill is cancellable.
  1546. *------------------------------------------*/
  1547. int unit_skillcastcancel(struct block_list *bl,int type)
  1548. {
  1549. struct map_session_data *sd = NULL;
  1550. struct unit_data *ud = unit_bl2ud( bl);
  1551. unsigned int tick=gettick();
  1552. int ret=0, skill;
  1553. nullpo_ret(bl);
  1554. if (!ud || ud->skilltimer == INVALID_TIMER)
  1555. return 0; //Nothing to cancel.
  1556. sd = BL_CAST(BL_PC, bl);
  1557. if (type&2) {
  1558. //See if it can be cancelled.
  1559. if (!ud->state.skillcastcancel)
  1560. return 0;
  1561. if (sd && (sd->special_state.no_castcancel2 ||
  1562. (sd->special_state.no_castcancel && !map_flag_gvg(bl->m) && !map[bl->m].flag.battleground))) //fixed flags being read the wrong way around [blackhole89]
  1563. return 0;
  1564. }
  1565. ud->canact_tick = tick;
  1566. if(type&1 && sd)
  1567. skill = sd->skillid_old;
  1568. else
  1569. skill = ud->skillid;
  1570. if (skill_get_inf(skill) & INF_GROUND_SKILL)
  1571. ret=delete_timer( ud->skilltimer, skill_castend_pos );
  1572. else
  1573. ret=delete_timer( ud->skilltimer, skill_castend_id );
  1574. if(ret<0)
  1575. ShowError("delete timer error : skillid : %d\n",ret);
  1576. ud->skilltimer = INVALID_TIMER;
  1577. if( sd && pc_checkskill(sd,SA_FREECAST) > 0 )
  1578. status_calc_bl(&sd->bl, SCB_SPEED);
  1579. if( sd )
  1580. {
  1581. switch( skill )
  1582. {
  1583. case CG_ARROWVULCAN:
  1584. sd->canequip_tick = tick;
  1585. break;
  1586. }
  1587. }
  1588. if(bl->type==BL_MOB) ((TBL_MOB*)bl)->skillidx = -1;
  1589. clif_skillcastcancel(bl);
  1590. return 1;
  1591. }
  1592. // unit_data の初期化処理
  1593. void unit_dataset(struct block_list *bl)
  1594. {
  1595. struct unit_data *ud;
  1596. nullpo_retv(ud = unit_bl2ud(bl));
  1597. memset( ud, 0, sizeof( struct unit_data) );
  1598. ud->bl = bl;
  1599. ud->walktimer = INVALID_TIMER;
  1600. ud->skilltimer = INVALID_TIMER;
  1601. ud->attacktimer = INVALID_TIMER;
  1602. ud->attackabletime =
  1603. ud->canact_tick =
  1604. ud->canmove_tick = gettick();
  1605. }
  1606. /*==========================================
  1607. * Returns 1 if this unit is attacking target 'id'
  1608. *------------------------------------------*/
  1609. static int unit_counttargeted_sub(struct block_list* bl, va_list ap)
  1610. {
  1611. int id = va_arg(ap, int);
  1612. int target_lv = va_arg(ap, int); // extra condition
  1613. struct unit_data* ud;
  1614. if(bl->id == id)
  1615. return 0;
  1616. ud = unit_bl2ud(bl);
  1617. if (ud && ud->target == id && ud->attacktimer != INVALID_TIMER && ud->attacktarget_lv >= target_lv)
  1618. return 1;
  1619. return 0;
  1620. }
  1621. /*==========================================
  1622. * Counts the number of units attacking 'bl'
  1623. *------------------------------------------*/
  1624. int unit_counttargeted(struct block_list* bl, int target_lv)
  1625. {
  1626. nullpo_ret(bl);
  1627. return (map_foreachinrange(unit_counttargeted_sub, bl, AREA_SIZE, BL_CHAR, bl->id, target_lv));
  1628. }
  1629. /*==========================================
  1630. *
  1631. *------------------------------------------*/
  1632. 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)
  1633. {
  1634. nullpo_ret(target);
  1635. if(damage+damage2 <= 0)
  1636. return 0;
  1637. return status_fix_damage(src,target,damage+damage2,clif_damage(target,target,tick,sdelay,ddelay,damage,div,type,damage2));
  1638. }
  1639. /*==========================================
  1640. * 見た目のサイズを変更する
  1641. *------------------------------------------*/
  1642. int unit_changeviewsize(struct block_list *bl,short size)
  1643. {
  1644. nullpo_ret(bl);
  1645. size=(size<0)?-1:(size>0)?1:0;
  1646. if(bl->type == BL_PC) {
  1647. ((TBL_PC*)bl)->state.size=size;
  1648. } else if(bl->type == BL_MOB) {
  1649. ((TBL_MOB*)bl)->special_state.size=size;
  1650. } else
  1651. return 0;
  1652. if(size!=0)
  1653. clif_specialeffect(bl,421+size, AREA);
  1654. return 0;
  1655. }
  1656. /*==========================================
  1657. * Removes a bl/ud from the map.
  1658. * Returns 1 on success. 0 if it couldn't be removed or the bl was free'd
  1659. * if clrtype is 1 (death), appropiate cleanup is performed.
  1660. * Otherwise it is assumed bl is being warped.
  1661. * On-Kill specific stuff is not performed here, look at status_damage for that.
  1662. *------------------------------------------*/
  1663. int unit_remove_map_(struct block_list *bl, clr_type clrtype, const char* file, int line, const char* func)
  1664. {
  1665. struct unit_data *ud = unit_bl2ud(bl);
  1666. struct status_change *sc = status_get_sc(bl);
  1667. nullpo_ret(ud);
  1668. if(bl->prev == NULL)
  1669. return 0; //Already removed?
  1670. map_freeblock_lock();
  1671. ud->target = 0; //Unlock walk/attack target.
  1672. if (ud->walktimer != INVALID_TIMER)
  1673. unit_stop_walking(bl,0);
  1674. if (ud->attacktimer != INVALID_TIMER)
  1675. unit_stop_attack(bl);
  1676. if (ud->skilltimer != INVALID_TIMER)
  1677. unit_skillcastcancel(bl,0);
  1678. // Do not reset can-act delay. [Skotlex]
  1679. ud->attackabletime = ud->canmove_tick /*= ud->canact_tick*/ = gettick();
  1680. if(sc && sc->count ) { //map-change/warp dispells.
  1681. status_change_end(bl, SC_BLADESTOP, INVALID_TIMER);
  1682. status_change_end(bl, SC_BASILICA, INVALID_TIMER);
  1683. status_change_end(bl, SC_ANKLE, INVALID_TIMER);
  1684. status_change_end(bl, SC_TRICKDEAD, INVALID_TIMER);
  1685. status_change_end(bl, SC_BLADESTOP_WAIT, INVALID_TIMER);
  1686. status_change_end(bl, SC_RUN, INVALID_TIMER);
  1687. status_change_end(bl, SC_DANCING, INVALID_TIMER);
  1688. status_change_end(bl, SC_WARM, INVALID_TIMER);
  1689. status_change_end(bl, SC_DEVOTION, INVALID_TIMER);
  1690. status_change_end(bl, SC_MARIONETTE, INVALID_TIMER);
  1691. status_change_end(bl, SC_MARIONETTE2, INVALID_TIMER);
  1692. status_change_end(bl, SC_CLOSECONFINE, INVALID_TIMER);
  1693. status_change_end(bl, SC_CLOSECONFINE2, INVALID_TIMER);
  1694. status_change_end(bl, SC_HIDING, INVALID_TIMER);
  1695. status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  1696. status_change_end(bl, SC_CHASEWALK, INVALID_TIMER);
  1697. status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  1698. if (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF)
  1699. status_change_end(bl, SC_GOSPEL, INVALID_TIMER);
  1700. status_change_end(bl, SC_CHANGE, INVALID_TIMER);
  1701. status_change_end(bl, SC_STOP, INVALID_TIMER);
  1702. status_change_end(bl, SC_WUGDASH, INVALID_TIMER);
  1703. }
  1704. if (bl->type&(BL_CHAR|BL_PET)) {
  1705. skill_unit_move(bl,gettick(),4);
  1706. skill_cleartimerskill(bl);
  1707. }
  1708. switch( bl->type )
  1709. {
  1710. case BL_PC:
  1711. {
  1712. struct map_session_data *sd = (struct map_session_data*)bl;
  1713. //Leave/reject all invitations.
  1714. if(sd->chatID)
  1715. chat_leavechat(sd,0);
  1716. if(sd->trade_partner)
  1717. trade_tradecancel(sd);
  1718. vending_closevending(sd);
  1719. buyingstore_close(sd);
  1720. searchstore_close(sd);
  1721. if(sd->state.storage_flag == 1)
  1722. storage_storage_quit(sd,0);
  1723. else if (sd->state.storage_flag == 2)
  1724. storage_guild_storage_quit(sd,0);
  1725. sd->state.storage_flag = 0; //Force close it when being warped.
  1726. if(sd->party_invite>0)
  1727. party_reply_invite(sd,sd->party_invite,0);
  1728. if(sd->guild_invite>0)
  1729. guild_reply_invite(sd,sd->guild_invite,0);
  1730. if(sd->guild_alliance>0)
  1731. guild_reply_reqalliance(sd,sd->guild_alliance_account,0);
  1732. if(sd->menuskill_id)
  1733. sd->menuskill_id = sd->menuskill_val = 0;
  1734. if( sd->touching_id )
  1735. npc_touchnext_areanpc(sd,true);
  1736. sd->npc_shopid = 0;
  1737. sd->adopt_invite = 0;
  1738. if(sd->pvp_timer != INVALID_TIMER) {
  1739. delete_timer(sd->pvp_timer,pc_calc_pvprank_timer);
  1740. sd->pvp_timer = INVALID_TIMER;
  1741. sd->pvp_rank = 0;
  1742. }
  1743. if(sd->duel_group > 0)
  1744. duel_leave(sd->duel_group, sd);
  1745. if(pc_issit(sd)) {
  1746. pc_setstand(sd);
  1747. skill_sit(sd,0);
  1748. }
  1749. party_send_dot_remove(sd);//minimap dot fix [Kevin]
  1750. guild_send_dot_remove(sd);
  1751. bg_send_dot_remove(sd);
  1752. if( map[bl->m].users <= 0 || sd->state.debug_remove_map )
  1753. {// this is only place where map users is decreased, if the mobs were removed too soon then this function was executed too many times [FlavioJS]
  1754. if( sd->debug_file == NULL || !(sd->state.debug_remove_map) )
  1755. {
  1756. sd->debug_file = "";
  1757. sd->debug_line = 0;
  1758. sd->debug_func = "";
  1759. }
  1760. ShowDebug("unit_remove_map: unexpected state when removing player AID/CID:%d/%d"
  1761. " (active=%d connect_new=%d rewarp=%d changemap=%d debug_remove_map=%d)"
  1762. " from map=%s (users=%d)."
  1763. " Previous call from %s:%d(%s), current call from %s:%d(%s)."
  1764. " Please report this!!!\n",
  1765. sd->status.account_id, sd->status.char_id,
  1766. sd->state.active, sd->state.connect_new, sd->state.rewarp, sd->state.changemap, sd->state.debug_remove_map,
  1767. map[bl->m].name, map[bl->m].users,
  1768. sd->debug_file, sd->debug_line, sd->debug_func, file, line, func);
  1769. }
  1770. else
  1771. if (--map[bl->m].users == 0 && battle_config.dynamic_mobs) //[Skotlex]
  1772. map_removemobs(bl->m);
  1773. if( !(sd->sc.option&OPTION_INVISIBLE) )
  1774. {// decrement the number of active pvp players on the map
  1775. --map[bl->m].users_pvp;
  1776. }
  1777. if( map[bl->m].instance_id )
  1778. {
  1779. instance[map[bl->m].instance_id].users--;
  1780. instance_check_idle(map[bl->m].instance_id);
  1781. }
  1782. sd->state.debug_remove_map = 1; // temporary state to track double remove_map's [FlavioJS]
  1783. sd->debug_file = file;
  1784. sd->debug_line = line;
  1785. sd->debug_func = func;
  1786. break;
  1787. }
  1788. case BL_MOB:
  1789. {
  1790. struct mob_data *md = (struct mob_data*)bl;
  1791. // Drop previous target mob_slave_keep_target: no.
  1792. if (!battle_config.mob_slave_keep_target)
  1793. md->target_id=0;
  1794. md->attacked_id=0;
  1795. md->state.skillstate= MSS_IDLE;
  1796. break;
  1797. }
  1798. case BL_PET:
  1799. {
  1800. struct pet_data *pd = (struct pet_data*)bl;
  1801. if( pd->pet.intimate <= 0 && !(pd->msd && !pd->msd->state.active) )
  1802. { //If logging out, this is deleted on unit_free
  1803. clif_clearunit_area(bl,clrtype);
  1804. map_delblock(bl);
  1805. unit_free(bl,CLR_OUTSIGHT);
  1806. map_freeblock_unlock();
  1807. return 0;
  1808. }
  1809. break;
  1810. }
  1811. case BL_HOM:
  1812. {
  1813. struct homun_data *hd = (struct homun_data *)bl;
  1814. ud->canact_tick = ud->canmove_tick; //It appears HOM do reset the can-act tick.
  1815. if( !hd->homunculus.intimacy && !(hd->master && !hd->master->state.active) )
  1816. { //If logging out, this is deleted on unit_free
  1817. clif_emotion(bl, E_SOB);
  1818. clif_clearunit_area(bl,clrtype);
  1819. map_delblock(bl);
  1820. unit_free(bl,CLR_OUTSIGHT);
  1821. map_freeblock_unlock();
  1822. return 0;
  1823. }
  1824. break;
  1825. }
  1826. case BL_MER:
  1827. {
  1828. struct mercenary_data *md = (struct mercenary_data *)bl;
  1829. ud->canact_tick = ud->canmove_tick;
  1830. if( mercenary_get_lifetime(md) <= 0 && !(md->master && !md->master->state.active) )
  1831. {
  1832. clif_clearunit_area(bl,clrtype);
  1833. map_delblock(bl);
  1834. unit_free(bl,CLR_OUTSIGHT);
  1835. map_freeblock_unlock();
  1836. return 0;
  1837. }
  1838. break;
  1839. }
  1840. default: ;// do nothing
  1841. }
  1842. /**
  1843. * BL_MOB is handled by mob_dead unless the monster is not dead.
  1844. **/
  1845. if( bl->type != BL_MOB || !status_isdead(bl) )
  1846. clif_clearunit_area(bl,clrtype);
  1847. map_delblock(bl);
  1848. map_freeblock_unlock();
  1849. return 1;
  1850. }
  1851. void unit_remove_map_pc(struct map_session_data *sd, clr_type clrtype)
  1852. {
  1853. unit_remove_map(&sd->bl,clrtype);
  1854. if (clrtype == CLR_TELEPORT) clrtype = CLR_OUTSIGHT; //CLR_TELEPORT is the warp from logging out, but pets/homunc need to just 'vanish' instead of showing the warping out animation.
  1855. if(sd->pd)
  1856. unit_remove_map(&sd->pd->bl, clrtype);
  1857. if(merc_is_hom_active(sd->hd))
  1858. unit_remove_map(&sd->hd->bl, clrtype);
  1859. if(sd->md)
  1860. unit_remove_map(&sd->md->bl, clrtype);
  1861. }
  1862. void unit_free_pc(struct map_session_data *sd)
  1863. {
  1864. if (sd->pd) unit_free(&sd->pd->bl,CLR_OUTSIGHT);
  1865. if (sd->hd) unit_free(&sd->hd->bl,CLR_OUTSIGHT);
  1866. if (sd->md) unit_free(&sd->md->bl,CLR_OUTSIGHT);
  1867. unit_free(&sd->bl,CLR_TELEPORT);
  1868. }
  1869. /*==========================================
  1870. * Function to free all related resources to the bl
  1871. * if unit is on map, it is removed using the clrtype specified
  1872. *------------------------------------------*/
  1873. int unit_free(struct block_list *bl, clr_type clrtype)
  1874. {
  1875. struct unit_data *ud = unit_bl2ud( bl );
  1876. nullpo_ret(ud);
  1877. map_freeblock_lock();
  1878. if( bl->prev ) //Players are supposed to logout with a "warp" effect.
  1879. unit_remove_map(bl, clrtype);
  1880. switch( bl->type )
  1881. {
  1882. case BL_PC:
  1883. {
  1884. struct map_session_data *sd = (struct map_session_data*)bl;
  1885. if( status_isdead(bl) )
  1886. pc_setrestartvalue(sd,2);
  1887. pc_delinvincibletimer(sd);
  1888. pc_delautobonus(sd,sd->autobonus,ARRAYLENGTH(sd->autobonus),false);
  1889. pc_delautobonus(sd,sd->autobonus2,ARRAYLENGTH(sd->autobonus2),false);
  1890. pc_delautobonus(sd,sd->autobonus3,ARRAYLENGTH(sd->autobonus3),false);
  1891. if( sd->followtimer != INVALID_TIMER )
  1892. pc_stop_following(sd);
  1893. if( sd->duel_invite > 0 )
  1894. duel_reject(sd->duel_invite, sd);
  1895. // Notify friends that this char logged out. [Skotlex]
  1896. map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
  1897. party_send_logout(sd);
  1898. guild_send_memberinfoshort(sd,0);
  1899. pc_cleareventtimer(sd);
  1900. pc_inventory_rental_clear(sd);
  1901. pc_delspiritball(sd,sd->spiritball,1);
  1902. if( sd->reg )
  1903. { //Double logout already freed pointer fix... [Skotlex]
  1904. aFree(sd->reg);
  1905. sd->reg = NULL;
  1906. sd->reg_num = 0;
  1907. }
  1908. if( sd->regstr )
  1909. {
  1910. int i;
  1911. for( i = 0; i < sd->regstr_num; ++i )
  1912. if( sd->regstr[i].data )
  1913. aFree(sd->regstr[i].data);
  1914. aFree(sd->regstr);
  1915. sd->regstr = NULL;
  1916. sd->regstr_num = 0;
  1917. }
  1918. if( sd->st && sd->st->state != RUN )
  1919. {// free attached scripts that are waiting
  1920. script_free_state(sd->st);
  1921. sd->st = NULL;
  1922. sd->npc_id = 0;
  1923. }
  1924. break;
  1925. }
  1926. case BL_PET:
  1927. {
  1928. struct pet_data *pd = (struct pet_data*)bl;
  1929. struct map_session_data *sd = pd->msd;
  1930. pet_hungry_timer_delete(pd);
  1931. if( pd->a_skill )
  1932. {
  1933. aFree(pd->a_skill);
  1934. pd->a_skill = NULL;
  1935. }
  1936. if( pd->s_skill )
  1937. {
  1938. if (pd->s_skill->timer != INVALID_TIMER) {
  1939. if (pd->s_skill->id)
  1940. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  1941. else
  1942. delete_timer(pd->s_skill->timer, pet_heal_timer);
  1943. }
  1944. aFree(pd->s_skill);
  1945. pd->s_skill = NULL;
  1946. }
  1947. if( pd->recovery )
  1948. {
  1949. if(pd->recovery->timer != INVALID_TIMER)
  1950. delete_timer(pd->recovery->timer, pet_recovery_timer);
  1951. aFree(pd->recovery);
  1952. pd->recovery = NULL;
  1953. }
  1954. if( pd->bonus )
  1955. {
  1956. if (pd->bonus->timer != INVALID_TIMER)
  1957. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  1958. aFree(pd->bonus);
  1959. pd->bonus = NULL;
  1960. }
  1961. if( pd->loot )
  1962. {
  1963. pet_lootitem_drop(pd,sd);
  1964. if (pd->loot->item)
  1965. aFree(pd->loot->item);
  1966. aFree (pd->loot);
  1967. pd->loot = NULL;
  1968. }
  1969. if( pd->pet.intimate > 0 )
  1970. intif_save_petdata(pd->pet.account_id,&pd->pet);
  1971. else
  1972. { //Remove pet.
  1973. intif_delete_petdata(pd->pet.pet_id);
  1974. if (sd) sd->status.pet_id = 0;
  1975. }
  1976. if( sd )
  1977. sd->pd = NULL;
  1978. break;
  1979. }
  1980. case BL_MOB:
  1981. {
  1982. struct mob_data *md = (struct mob_data*)bl;
  1983. if( md->spawn_timer != INVALID_TIMER )
  1984. {
  1985. delete_timer(md->spawn_timer,mob_delayspawn);
  1986. md->spawn_timer = INVALID_TIMER;
  1987. }
  1988. if( md->deletetimer != INVALID_TIMER )
  1989. {
  1990. delete_timer(md->deletetimer,mob_timer_delete);
  1991. md->deletetimer = INVALID_TIMER;
  1992. }
  1993. if( md->lootitem )
  1994. {
  1995. aFree(md->lootitem);
  1996. md->lootitem=NULL;
  1997. }
  1998. if( md->guardian_data )
  1999. {
  2000. struct guild_castle* gc = md->guardian_data->castle;
  2001. if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
  2002. {
  2003. gc->guardian[md->guardian_data->number].id = 0;
  2004. }
  2005. else
  2006. {
  2007. int i;
  2008. ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == md->bl.id);
  2009. if( i < gc->temp_guardians_max )
  2010. gc->temp_guardians[i] = 0;
  2011. }
  2012. aFree(md->guardian_data);
  2013. md->guardian_data = NULL;
  2014. }
  2015. if( md->spawn )
  2016. {
  2017. md->spawn->active--;
  2018. if( !md->spawn->state.dynamic )
  2019. {// permanently remove the mob
  2020. if( --md->spawn->num == 0 )
  2021. {// Last freed mob is responsible for deallocating the group's spawn data.
  2022. aFree(md->spawn);
  2023. md->spawn = NULL;
  2024. }
  2025. }
  2026. }
  2027. if( md->base_status)
  2028. {
  2029. aFree(md->base_status);
  2030. md->base_status = NULL;
  2031. }
  2032. if( mob_is_clone(md->class_) )
  2033. mob_clone_delete(md);
  2034. break;
  2035. }
  2036. case BL_HOM:
  2037. {
  2038. struct homun_data *hd = (TBL_HOM*)bl;
  2039. struct map_session_data *sd = hd->master;
  2040. merc_hom_hungry_timer_delete(hd);
  2041. if( hd->homunculus.intimacy > 0 )
  2042. merc_save(hd);
  2043. else
  2044. {
  2045. intif_homunculus_requestdelete(hd->homunculus.hom_id);
  2046. if( sd )
  2047. sd->status.hom_id = 0;
  2048. }
  2049. if( sd )
  2050. sd->hd = NULL;
  2051. break;
  2052. }
  2053. case BL_MER:
  2054. {
  2055. struct mercenary_data *md = (TBL_MER*)bl;
  2056. struct map_session_data *sd = md->master;
  2057. if( mercenary_get_lifetime(md) > 0 )
  2058. mercenary_save(md);
  2059. else
  2060. {
  2061. intif_mercenary_delete(md->mercenary.mercenary_id);
  2062. if( sd )
  2063. sd->status.mer_id = 0;
  2064. }
  2065. if( sd )
  2066. sd->md = NULL;
  2067. merc_contract_stop(md);
  2068. break;
  2069. }
  2070. }
  2071. skill_clear_unitgroup(bl);
  2072. status_change_clear(bl,1);
  2073. map_deliddb(bl);
  2074. if( bl->type != BL_PC ) //Players are handled by map_quit
  2075. map_freeblock(bl);
  2076. map_freeblock_unlock();
  2077. return 0;
  2078. }
  2079. int do_init_unit(void)
  2080. {
  2081. add_timer_func_list(unit_attack_timer, "unit_attack_timer");
  2082. add_timer_func_list(unit_walktoxy_timer,"unit_walktoxy_timer");
  2083. add_timer_func_list(unit_walktobl_sub, "unit_walktobl_sub");
  2084. add_timer_func_list(unit_delay_walktoxy_timer,"unit_delay_walktoxy_timer");
  2085. return 0;
  2086. }
  2087. int do_final_unit(void)
  2088. {
  2089. // nothing to do
  2090. return 0;
  2091. }