unit.c 67 KB

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