unit.c 55 KB

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