unit.c 49 KB

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