viewing paste skill.c | Plain text

Posted on the
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3.  
  4. #include "../common/cbasetypes.h"
  5. #include "../common/timer.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/malloc.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/strlib.h"
  10. #include "../common/utils.h"
  11. #include "../common/ers.h"
  12.  
  13. #include "skill.h"
  14. #include "map.h"
  15. #include "path.h"
  16. #include "clif.h"
  17. #include "pc.h"
  18. #include "status.h"
  19. #include "pet.h"
  20. #include "homunculus.h"
  21. #include "mercenary.h"
  22. #include "mob.h"
  23. #include "npc.h"
  24. #include "battle.h"
  25. #include "battleground.h"
  26. #include "party.h"
  27. #include "itemdb.h"
  28. #include "script.h"
  29. #include "intif.h"
  30. #include "log.h"
  31. #include "chrif.h"
  32. #include "guild.h"
  33. #include "date.h"
  34. #include "unit.h"
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <time.h>
  40.  
  41.  
  42. #define SKILLUNITTIMER_INTERVAL	100
  43.  
  44. // ranges reserved for mapping skill ids to skilldb offsets
  45. #define GD_SKILLRANGEMIN 900
  46. #define GD_SKILLRANGEMAX (GD_SKILLRANGEMIN+MAX_GUILDSKILL)
  47. #define MC_SKILLRANGEMIN 800
  48. #define MC_SKILLRANGEMAX (MC_SKILLRANGEMIN+MAX_MERCSKILL)
  49. #define HM_SKILLRANGEMIN 700
  50. #define HM_SKILLRANGEMAX (HM_SKILLRANGEMIN+MAX_HOMUNSKILL)
  51.  
  52. static struct eri *skill_unit_ers = NULL; //For handling skill_unit's [Skotlex]
  53. static struct eri *skill_timer_ers = NULL; //For handling skill_timerskills [Skotlex]
  54.  
  55. DBMap* skillunit_db = NULL; // int id -> struct skill_unit*
  56.  
  57. DBMap* skilldb_name2id = NULL;
  58. struct s_skill_db skill_db[MAX_SKILL_DB];
  59. struct s_skill_produce_db skill_produce_db[MAX_SKILL_PRODUCE_DB];
  60. struct s_skill_arrow_db skill_arrow_db[MAX_SKILL_ARROW_DB];
  61. struct s_skill_abra_db skill_abra_db[MAX_SKILL_ABRA_DB];
  62.  
  63. struct s_skill_unit_layout skill_unit_layout[MAX_SKILL_UNIT_LAYOUT];
  64. int firewall_unit_pos;
  65. int icewall_unit_pos;
  66.  
  67. //Since only mob-casted splash skills can hit ice-walls
  68. static inline int splash_target(struct block_list* bl)
  69. {
  70. 	return ( bl->type == BL_MOB ) ? BL_SKILL|BL_CHAR : BL_CHAR;
  71. }
  72.  
  73. /// Returns the id of the skill, or 0 if not found.
  74. int skill_name2id(const char* name)
  75. {
  76. 	if( name == NULL )
  77. 		return 0;
  78.  
  79. 	return (int)strdb_get(skilldb_name2id, name);
  80. }
  81.  
  82. /// Maps skill ids to skill db offsets.
  83. /// Returns the skill's array index, or 0 (Unknown Skill).
  84. int skill_get_index( int id )
  85. {
  86. 	// avoid ranges reserved for mapping guild/homun/mercenary skills
  87. 	if( (id >= GD_SKILLRANGEMIN && id <= GD_SKILLRANGEMAX)
  88. 	||  (id >= HM_SKILLRANGEMIN && id <= HM_SKILLRANGEMAX)
  89. 	||  (id >= MC_SKILLRANGEMIN && id <= MC_SKILLRANGEMAX) )
  90. 		return 0;
  91.  
  92. 	// map skill id to skill db index
  93. 	if( id >= GD_SKILLBASE )
  94. 		id = GD_SKILLRANGEMIN + id - GD_SKILLBASE;
  95. 	else
  96. 	if( id >= MC_SKILLBASE )
  97. 		id = MC_SKILLRANGEMIN + id - MC_SKILLBASE;
  98. 	else
  99. 	if( id >= HM_SKILLBASE )
  100. 		id = HM_SKILLRANGEMIN + id - HM_SKILLBASE;
  101. 	else
  102. 		; // identity
  103.  
  104. 	// validate result
  105. 	if( id <= 0 || id >= MAX_SKILL_DB )
  106. 		return 0;
  107.  
  108. 	return id;
  109. }
  110.  
  111. const char* skill_get_name( int id )
  112. {
  113. 	return skill_db[skill_get_index(id)].name;
  114. }
  115.  
  116. const char* skill_get_desc( int id )
  117. {
  118. 	return skill_db[skill_get_index(id)].desc;
  119. }
  120.  
  121. // out of bounds error checking [celest]
  122. static void skill_chk(int* id, int  lv)
  123. {
  124. 	*id = skill_get_index(*id); // checks/adjusts id
  125. 	if( lv <= 0 || lv > MAX_SKILL_LEVEL ) *id = 0;
  126. }
  127.  
  128. #define skill_get(var,id,lv) { skill_chk(&id,lv); if(!id) return 0; return var; }
  129.  
  130. // Skill DB
  131. int	skill_get_hit( int id )               { skill_get (skill_db[id].hit, id, 1); }
  132. int	skill_get_inf( int id )               { skill_get (skill_db[id].inf, id, 1); }
  133. int	skill_get_ele( int id , int lv )      { skill_get (skill_db[id].element[lv-1], id, lv); }
  134. int	skill_get_nk( int id )                { skill_get (skill_db[id].nk, id, 1); }
  135. int	skill_get_max( int id )               { skill_get (skill_db[id].max, id, 1); }
  136. int	skill_get_range( int id , int lv )    { skill_get (skill_db[id].range[lv-1], id, lv); }
  137. int	skill_get_splash( int id , int lv )   { skill_chk (&id, lv); return (skill_db[id].splash[lv-1]>=0?skill_db[id].splash[lv-1]:AREA_SIZE); }
  138. int	skill_get_hp( int id ,int lv )        { skill_get (skill_db[id].hp[lv-1], id, lv); }
  139. int	skill_get_sp( int id ,int lv )        { skill_get (skill_db[id].sp[lv-1], id, lv); }
  140. int	skill_get_hp_rate(int id, int lv )    { skill_get (skill_db[id].hp_rate[lv-1], id, lv); }
  141. int	skill_get_sp_rate(int id, int lv )    { skill_get (skill_db[id].sp_rate[lv-1], id, lv); }
  142. int	skill_get_state(int id)               { skill_get (skill_db[id].state, id, 1); }
  143. int	skill_get_spiritball(int id, int lv)  { skill_get (skill_db[id].spiritball[lv-1], id, lv); }
  144. int	skill_get_itemid(int id, int idx)     { skill_get (skill_db[id].itemid[idx], id, 1); }
  145. int	skill_get_itemqty(int id, int idx)    { skill_get (skill_db[id].amount[idx], id, 1); }
  146. int	skill_get_zeny( int id ,int lv )      { skill_get (skill_db[id].zeny[lv-1], id, lv); }
  147. int	skill_get_num( int id ,int lv )       { skill_get (skill_db[id].num[lv-1], id, lv); }
  148. int	skill_get_cast( int id ,int lv )      { skill_get (skill_db[id].cast[lv-1], id, lv); }
  149. int	skill_get_delay( int id ,int lv )     { skill_get (skill_db[id].delay[lv-1], id, lv); }
  150. int	skill_get_walkdelay( int id ,int lv ) { skill_get (skill_db[id].walkdelay[lv-1], id, lv); }
  151. int	skill_get_time( int id ,int lv )      { skill_get (skill_db[id].upkeep_time[lv-1], id, lv); }
  152. int	skill_get_time2( int id ,int lv )     { skill_get (skill_db[id].upkeep_time2[lv-1], id, lv); }
  153. int	skill_get_castdef( int id )           { skill_get (skill_db[id].cast_def_rate, id, 1); }
  154. int	skill_get_weapontype( int id )        { skill_get (skill_db[id].weapon, id, 1); }
  155. int	skill_get_ammotype( int id )          { skill_get (skill_db[id].ammo, id, 1); }
  156. int	skill_get_ammo_qty( int id, int lv )  { skill_get (skill_db[id].ammo_qty[lv-1], id, lv); }
  157. int	skill_get_inf2( int id )              { skill_get (skill_db[id].inf2, id, 1); }
  158. int	skill_get_castcancel( int id )        { skill_get (skill_db[id].castcancel, id, 1); }
  159. int	skill_get_maxcount( int id ,int lv )  { skill_get (skill_db[id].maxcount[lv-1], id, lv); }
  160. int	skill_get_blewcount( int id ,int lv ) { skill_get (skill_db[id].blewcount[lv-1], id, lv); }
  161. int	skill_get_mhp( int id ,int lv )       { skill_get (skill_db[id].mhp[lv-1], id, lv); }
  162. int	skill_get_castnodex( int id ,int lv ) { skill_get (skill_db[id].castnodex[lv-1], id, lv); }
  163. int	skill_get_delaynodex( int id ,int lv ){ skill_get (skill_db[id].delaynodex[lv-1], id, lv); }
  164. int	skill_get_nocast ( int id )           { skill_get (skill_db[id].nocast, id, 1); }
  165. int	skill_get_type( int id )              { skill_get (skill_db[id].skill_type, id, 1); }
  166. int	skill_get_unit_id ( int id, int flag ){ skill_get (skill_db[id].unit_id[flag], id, 1); }
  167. int	skill_get_unit_interval( int id )     { skill_get (skill_db[id].unit_interval, id, 1); }
  168. int	skill_get_unit_range( int id, int lv ){ skill_get (skill_db[id].unit_range[lv-1], id, lv); }
  169. int	skill_get_unit_target( int id )       { skill_get (skill_db[id].unit_target&BCT_ALL, id, 1); }
  170. int	skill_get_unit_bl_target( int id )    { skill_get (skill_db[id].unit_target&BL_ALL, id, 1); }
  171. int	skill_get_unit_flag( int id )         { skill_get (skill_db[id].unit_flag, id, 1); }
  172. int	skill_get_unit_layout_type( int id ,int lv ){ skill_get (skill_db[id].unit_layout_type[lv-1], id, lv); }
  173.  
  174. int skill_tree_get_max(int id, int b_class)
  175. {
  176. 	int i;
  177. 	b_class = pc_class2idx(b_class);
  178.  
  179. 	ARR_FIND( 0, MAX_SKILL_TREE, i, skill_tree[b_class][i].id == 0 || skill_tree[b_class][i].id == id );
  180. 	if( i < MAX_SKILL_TREE && skill_tree[b_class][i].id == id )
  181. 		return skill_tree[b_class][i].max;
  182. 	else
  183. 		return skill_get_max(id);
  184. }
  185.  
  186. int skill_frostjoke_scream(struct block_list *bl,va_list ap);
  187. int skill_attack_area(struct block_list *bl,va_list ap);
  188. struct skill_unit_group *skill_locate_element_field(struct block_list *bl); // [Skotlex]
  189. int skill_graffitiremover(struct block_list *bl, va_list ap); // [Valaris]
  190. int skill_greed(struct block_list *bl, va_list ap);
  191. static int skill_cell_overlap(struct block_list *bl, va_list ap);
  192. static int skill_trap_splash(struct block_list *bl, va_list ap);
  193. struct skill_unit_group_tickset *skill_unitgrouptickset_search(struct block_list *bl,struct skill_unit_group *sg,int tick);
  194. static int skill_unit_onplace(struct skill_unit *src,struct block_list *bl,unsigned int tick);
  195. static int skill_unit_onleft(int skill_id, struct block_list *bl,unsigned int tick);
  196. static int skill_unit_effect(struct block_list *bl,va_list ap);
  197.  
  198. int enchant_eff[5] = { 10, 14, 17, 19, 20 };
  199. int deluge_eff[5] = { 5, 9, 12, 14, 15 };
  200.  
  201. int skill_get_casttype (int id)
  202. {
  203. 	int inf = skill_get_inf(id);
  204. 	if (inf&(INF_GROUND_SKILL))
  205. 		return CAST_GROUND;
  206. 	if (inf&INF_SUPPORT_SKILL)
  207. 		return CAST_NODAMAGE;
  208. 	if (inf&INF_SELF_SKILL) {
  209. 		if(skill_get_inf2(id)&INF2_NO_TARGET_SELF)
  210. 			return CAST_DAMAGE; //Combo skill.
  211. 		return CAST_NODAMAGE;
  212. 	}
  213. 	if (skill_get_nk(id)&NK_NO_DAMAGE)
  214. 		return CAST_NODAMAGE;
  215. 	return CAST_DAMAGE;
  216. };
  217.  
  218. //Returns actual skill range taking into account attack range and AC_OWL [Skotlex]
  219. int skill_get_range2 (struct block_list *bl, int id, int lv)
  220. {
  221. 	int range;
  222. 	if( bl->type == BL_MOB && battle_config.mob_ai&0x400 )
  223. 		return 9; //Mobs have a range of 9 regardless of skill used.
  224.  
  225. 	range = skill_get_range(id, lv);
  226.  
  227. 	if( range < 0 )
  228. 	{
  229. 		if( battle_config.use_weapon_skill_range&bl->type )
  230. 			return status_get_range(bl);
  231. 		range *=-1;
  232. 	}
  233.  
  234. 	//TODO: Find a way better than hardcoding the list of skills affected by AC_VULTURE
  235. 	switch( id )
  236. 	{
  237. 	case AC_SHOWER:			case MA_SHOWER:
  238. 	case AC_DOUBLE:			case MA_DOUBLE:
  239. 	case HT_BLITZBEAT:
  240. 	case AC_CHARGEARROW:
  241. 	case MA_CHARGEARROW:
  242. 	case SN_FALCONASSAULT:
  243. 	case HT_POWER:
  244. 		if( bl->type == BL_PC )
  245. 			range += pc_checkskill((TBL_PC*)bl, AC_VULTURE);
  246. 		else
  247. 			range += 10; //Assume level 10?
  248. 		break;
  249. 	// added to allow GS skills to be effected by the range of Snake Eyes [Reddozen]
  250. 	case GS_RAPIDSHOWER:
  251. 	case GS_PIERCINGSHOT:
  252. 	case GS_FULLBUSTER:
  253. 	case GS_SPREADATTACK:
  254. 	case GS_GROUNDDRIFT:
  255. 		if (bl->type == BL_PC)
  256. 			range += pc_checkskill((TBL_PC*)bl, GS_SNAKEEYE);
  257. 		else
  258. 			range += 10; //Assume level 10?
  259. 		break;
  260. 	case NJ_KIRIKAGE:
  261. 		if (bl->type == BL_PC)
  262. 			range = skill_get_range(NJ_SHADOWJUMP,pc_checkskill((TBL_PC*)bl,NJ_SHADOWJUMP));
  263. 		break;
  264. 	}
  265.  
  266. 	if( !range && bl->type != BL_PC )
  267. 		return 9; // Enable non players to use self skills on others. [Skotlex]
  268. 	return range;
  269. }
  270.  
  271. int skill_calc_heal(struct block_list *src, struct block_list *target, int skill_id, int skill_lv, bool heal)
  272. {
  273. 	int skill, hp;
  274. 	struct map_session_data *sd = BL_CAST(BL_PC, src);
  275. 	struct map_session_data *tsd = BL_CAST(BL_PC, target);
  276. 	struct status_change* sc;
  277.  
  278. 	switch( skill_id )
  279. 	{
  280. 	case BA_APPLEIDUN:
  281. 		hp = 30+5*skill_lv+5*(status_get_vit(src)/10); // HP recovery
  282. 		if( sd )
  283. 			hp += 5*pc_checkskill(sd,BA_MUSICALLESSON);
  284. 		break;
  285. 	case PR_SANCTUARY:
  286. 		hp = (skill_lv>6)?777:skill_lv*100;
  287. 		break;
  288. 	case NPC_EVILLAND:
  289. 		hp = (skill_lv>6)?666:skill_lv*100;
  290. 		break;
  291. 	default:
  292. 		if (skill_lv >= battle_config.max_heal_lv)
  293. 			return battle_config.max_heal;
  294.  
  295. 		hp = ( status_get_lv(src)+status_get_int(src) )/8 *(4+ skill_lv*8);
  296. 		if( sd && ((skill = pc_checkskill(sd, HP_MEDITATIO)) > 0) )
  297. 			hp += hp * skill * 2 / 100;
  298. 		else if( src->type == BL_HOM && (skill = merc_hom_checkskill(((TBL_HOM*)src), HLIF_BRAIN)) > 0 )
  299. 			hp += hp * skill * 2 / 100;
  300. 		break;
  301. 	}
  302.  
  303. 	if( ( (target && target->type == BL_MER) || !heal ) && skill_id != NPC_EVILLAND )
  304. 		hp >>= 1;
  305.  
  306. 	if( sd && (skill = pc_skillheal_bonus(sd, skill_id)) )
  307. 		hp += hp*skill/100;
  308.  
  309. 	if( tsd && (skill = pc_skillheal2_bonus(tsd, skill_id)) )
  310. 		hp += hp*skill/100;
  311.  
  312. 	sc = status_get_sc(target);
  313. 	if( sc && sc->count )
  314. 	{
  315. 		if( sc->data[SC_CRITICALWOUND] && heal ) // Critical Wound has no effect on offensive heal. [Inkfish]
  316. 			hp -= hp * sc->data[SC_CRITICALWOUND]->val2/100;
  317. 		if( sc->data[SC_INCHEALRATE] && skill_id != NPC_EVILLAND && skill_id != BA_APPLEIDUN )
  318. 			hp += hp * sc->data[SC_INCHEALRATE]->val1/100; // Only affects Heal, Sanctuary and PotionPitcher.(like bHealPower) [Inkfish]
  319. 	}
  320.  
  321. 	return hp;
  322. }
  323.  
  324. // Making plagiarize check its own function [Aru]
  325. int can_copy (struct map_session_data *sd, int skillid, struct block_list* bl)
  326. {
  327. 	// Never copy NPC/Wedding Skills
  328. 	if (skill_get_inf2(skillid)&(INF2_NPC_SKILL|INF2_WEDDING_SKILL))
  329. 		return 0;
  330.  
  331. 	// High-class skills
  332. 	if((skillid >= LK_AURABLADE && skillid <= ASC_CDP) || (skillid >= ST_PRESERVE && skillid <= CR_CULTIVATION))
  333. 	{
  334. 		if(battle_config.copyskill_restrict == 2)
  335. 			return 0;
  336. 		else if(battle_config.copyskill_restrict)
  337. 			return (sd->status.class_ == JOB_STALKER);
  338. 	}
  339.  
  340. 	//Added so plagarize can't copy agi/bless if you're undead since it damages you
  341. 	if ((skillid == AL_INCAGI || skillid == AL_BLESSING || 
  342. 		skillid == CASH_BLESSING || skillid == CASH_INCAGI || 
  343. 		skillid == MER_INCAGI || skillid == MER_BLESSING))
  344. 		return 0;
  345.  
  346. 	return 1;
  347. }
  348.  
  349. // [MouseJstr] - skill ok to cast? and when?
  350. int skillnotok (int skillid, struct map_session_data *sd)
  351. {
  352. 	int i,m;
  353. 	nullpo_retr (1, sd);
  354. 	m = sd->bl.m;
  355. 	i = skill_get_index(skillid);
  356.  
  357. 	if (i == 0)
  358. 		return 1; // invalid skill id
  359.  
  360. 	if (battle_config.gm_skilluncond && pc_isGM(sd) >= battle_config.gm_skilluncond)
  361. 		return 0; // GMs can do any damn thing they want
  362.  
  363. 	if( skillid == AL_TELEPORT && sd->skillitem == skillid && sd->skillitemlv > 2 )
  364. 		return 0; // Teleport lv 3 bypasses this check.[Inkfish]
  365.  
  366. 	if (sd->blockskill[i] > 0)
  367. 		return 1;
  368.  
  369. 	// Check skill restrictions [Celest]
  370. 	if(!map_flag_vs(m) && skill_get_nocast (skillid) & 1)
  371. 		return 1;
  372. 	if(map[m].flag.pvp && skill_get_nocast (skillid) & 2)
  373. 		return 1;
  374. 	if(map_flag_gvg(m) && skill_get_nocast (skillid) & 4)
  375. 		return 1;
  376. 	if(map[m].flag.battleground && skill_get_nocast (skillid) & 8)
  377. 		return 1;
  378. 	if(map[m].flag.restricted && map[m].zone && skill_get_nocast (skillid) & (8*map[m].zone))
  379. 		return 1;
  380.  
  381. 	switch (skillid) {
  382. 		case AL_WARP:
  383. 			if(map[m].flag.nowarp) {
  384. 				clif_skill_teleportmessage(sd,0);
  385. 				return 1;
  386. 			}
  387. 			return 0;
  388. 		case AL_TELEPORT:
  389. 			if(map[m].flag.noteleport) {
  390. 				clif_skill_teleportmessage(sd,0);
  391. 				return 1;
  392. 			}
  393. 			return 0; // gonna be checked in 'skill_castend_nodamage_id'
  394. 		case WE_CALLPARTNER:
  395. 		case WE_CALLPARENT:
  396. 		case WE_CALLBABY:
  397. 			if (map[m].flag.nomemo) {
  398. 				clif_skill_teleportmessage(sd,1);
  399. 				return 1;
  400. 			}
  401. 			break;
  402. 		case MC_VENDING:
  403. 		case MC_IDENTIFY:
  404. 		case ALL_BUYING_STORE:
  405. 			return 0; // always allowed
  406. 		case WZ_ICEWALL:
  407. 			// noicewall flag [Valaris]
  408. 			if (map[m].flag.noicewall) {
  409. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  410. 				return 1;
  411. 			}
  412. 			break;
  413. 		case GD_EMERGENCYCALL:
  414. 			if (
  415. 				!(battle_config.emergency_call&((agit_flag || agit2_flag)?2:1)) ||
  416. 				!(battle_config.emergency_call&(map[m].flag.gvg || map[m].flag.gvg_castle?8:4)) ||
  417. 				(battle_config.emergency_call&16 && map[m].flag.nowarpto && !map[m].flag.gvg_castle)
  418. 			)	{
  419. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  420. 				return 1;
  421. 			}
  422. 			break;
  423. 	}
  424. 	return (map[m].flag.noskill);
  425. }
  426.  
  427. int skillnotok_hom(int skillid, struct homun_data *hd)
  428. {
  429. 	int i = skill_get_index(skillid);
  430. 	nullpo_retr(1,hd);
  431.  
  432. 	if (i == 0)
  433. 		return 1; // invalid skill id
  434.  
  435. 	if (hd->blockskill[i] > 0)
  436. 		return 1;
  437.  
  438. 	//Use master's criteria.
  439. 	return skillnotok(skillid, hd->master);
  440. }
  441.  
  442. int skillnotok_mercenary(int skillid, struct mercenary_data *md)
  443. {
  444. 	int i = skill_get_index(skillid);
  445. 	nullpo_retr(1,md);
  446.  
  447. 	if( i == 0 )
  448. 		return 1; // Invalid Skill ID
  449. 	if( md->blockskill[i] > 0 )
  450. 		return 1;
  451.  
  452. 	return skillnotok(skillid, md->master);
  453. }
  454.  
  455. struct s_skill_unit_layout* skill_get_unit_layout (int skillid, int skilllv, struct block_list* src, int x, int y)
  456. {
  457. 	int pos = skill_get_unit_layout_type(skillid,skilllv);
  458. 	int dir;
  459.  
  460. 	if (pos < -1 || pos >= MAX_SKILL_UNIT_LAYOUT) {
  461. 		ShowError("skill_get_unit_layout: unsupported layout type %d for skill %d (level %d)\n", pos, skillid, skilllv);
  462. 		pos = cap_value(pos, 0, MAX_SQUARE_LAYOUT); // cap to nearest square layout
  463. 	}
  464.  
  465. 	if (pos != -1) // simple single-definition layout
  466. 		return &skill_unit_layout[pos];
  467.  
  468. 	dir = (src->x == x && src->y == y) ? 6 : map_calc_dir(src,x,y); // 6 - default aegis direction
  469.  
  470. 	if (skillid == MG_FIREWALL)
  471. 		return &skill_unit_layout [firewall_unit_pos + dir];
  472. 	else if (skillid == WZ_ICEWALL)
  473. 		return &skill_unit_layout [icewall_unit_pos + dir];
  474.  
  475. 	ShowError("skill_get_unit_layout: unknown unit layout for skill %d (level %d)\n", skillid, skilllv);
  476. 	return &skill_unit_layout[0]; // default 1x1 layout
  477. }
  478.  
  479. /*==========================================
  480.  *
  481.  *------------------------------------------*/
  482. int skill_additional_effect (struct block_list* src, struct block_list *bl, int skillid, int skilllv, int attack_type, int dmg_lv, unsigned int tick)
  483. {
  484. 	struct map_session_data *sd, *dstsd;
  485. 	struct mob_data *md, *dstmd;
  486. 	struct status_data *sstatus, *tstatus;
  487. 	struct status_change *sc, *tsc;
  488.  
  489. 	enum sc_type status;
  490. 	int skill;
  491. 	int rate;
  492.  
  493. 	nullpo_ret(src);
  494. 	nullpo_ret(bl);
  495.  
  496. 	if(skillid < 0) return 0;
  497. 	if(skillid > 0 && skilllv <= 0) return 0;	// don't forget auto attacks! - celest
  498.  
  499. 	if( dmg_lv < ATK_BLOCK ) // Don't apply effect if miss.
  500. 		return 0;
  501.  
  502. 	sd = BL_CAST(BL_PC, src);
  503. 	md = BL_CAST(BL_MOB, src);
  504. 	dstsd = BL_CAST(BL_PC, bl);
  505. 	dstmd = BL_CAST(BL_MOB, bl);
  506.  
  507. 	sc = status_get_sc(src);
  508. 	tsc = status_get_sc(bl);
  509. 	sstatus = status_get_status_data(src);
  510. 	tstatus = status_get_status_data(bl);
  511. 	if (!tsc) //skill additional effect is about adding effects to the target...
  512. 		//So if the target can't be inflicted with statuses, this is pointless.
  513. 		return 0;
  514.  
  515. 	if( sd )
  516. 	{ // These statuses would be applied anyway even if the damage was blocked by some skills. [Inkfish]
  517. 		if( skillid != WS_CARTTERMINATION && skillid != AM_DEMONSTRATION && skillid != CR_REFLECTSHIELD && skillid != MS_REFLECTSHIELD && skillid != ASC_BREAKER )
  518. 		{ // Trigger status effects
  519. 			enum sc_type type;
  520. 			int i;
  521. 			for( i = 0; i < ARRAYLENGTH(sd->addeff) && sd->addeff[i].flag; i++ )
  522. 			{
  523. 				rate = sd->addeff[i].rate;
  524. 				if( attack_type&BF_LONG ) // Any ranged physical attack takes status arrows into account (Grimtooth...) [DracoRPG]
  525. 					rate += sd->addeff[i].arrow_rate;
  526. 				if( !rate ) continue;
  527.  
  528. 				if( (sd->addeff[i].flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)) != (ATF_WEAPON|ATF_MAGIC|ATF_MISC) ) 
  529. 				{ // Trigger has attack type consideration. 
  530. 					if( (sd->addeff[i].flag&ATF_WEAPON && attack_type&BF_WEAPON) ||
  531. 						(sd->addeff[i].flag&ATF_MAGIC && attack_type&BF_MAGIC) ||
  532. 						(sd->addeff[i].flag&ATF_MISC && attack_type&BF_MISC) ) ;
  533. 					else
  534. 						continue; 
  535. 				}
  536.  
  537. 				if( (sd->addeff[i].flag&(ATF_LONG|ATF_SHORT)) != (ATF_LONG|ATF_SHORT) )
  538. 				{ // Trigger has range consideration.
  539. 					if((sd->addeff[i].flag&ATF_LONG && !(attack_type&BF_LONG)) ||
  540. 						(sd->addeff[i].flag&ATF_SHORT && !(attack_type&BF_SHORT)))
  541. 						continue; //Range Failed.
  542. 				}
  543.  
  544. 				type =  sd->addeff[i].id;
  545. 				skill = skill_get_time2(status_sc2skill(type),7);
  546.  
  547. 				if (sd->addeff[i].flag&ATF_TARGET)
  548. 					status_change_start(bl,type,rate,7,0,0,0,skill,0);
  549.  
  550. 				if (sd->addeff[i].flag&ATF_SELF)
  551. 					status_change_start(src,type,rate,7,0,0,0,skill,0);
  552. 			}
  553. 		}
  554.  
  555. 		if( skillid )
  556. 		{ // Trigger status effects on skills
  557. 			enum sc_type type;
  558. 			int i;
  559. 			for( i = 0; i < ARRAYLENGTH(sd->addeff3) && sd->addeff3[i].skill; i++ )
  560. 			{
  561. 				if( skillid != sd->addeff3[i].skill || !sd->addeff3[i].rate )
  562. 					continue;
  563. 				type = sd->addeff3[i].id;
  564. 				skill = skill_get_time2(status_sc2skill(type),7);
  565.  
  566. 				if( sd->addeff3[i].target&ATF_TARGET )
  567. 					status_change_start(bl,type,sd->addeff3[i].rate,7,0,0,0,skill,0);
  568. 				if( sd->addeff3[i].target&ATF_SELF )
  569. 					status_change_start(src,type,sd->addeff3[i].rate,7,0,0,0,skill,0);
  570. 			}
  571. 		}
  572. 	}
  573.  
  574. 	if( dmg_lv < ATK_DEF ) // no damage, return;
  575. 		return 0;
  576.  
  577. 	switch(skillid)
  578. 	{
  579. 	case 0: // Normal attacks (no skill used)
  580. 	{
  581. 		if( attack_type&BF_SKILL )
  582. 			break; // If a normal attack is a skill, it's splash damage. [Inkfish]
  583. 		if(sd) {
  584. 			// Automatic trigger of Blitz Beat
  585. 			if (pc_isfalcon(sd) && sd->status.weapon == W_BOW && (skill=pc_checkskill(sd,HT_BLITZBEAT))>0 &&
  586. 				rand()%1000 <= sstatus->luk*10/3+1 ) {
  587. 				rate=(sd->status.job_level+9)/10;
  588. 				skill_castend_damage_id(src,bl,HT_BLITZBEAT,(skill<rate)?skill:rate,tick,SD_LEVEL);
  589. 			}
  590. 			// Gank
  591. 			if(dstmd && sd->status.weapon != W_BOW &&
  592. 				(skill=pc_checkskill(sd,RG_SNATCHER)) > 0 &&
  593. 				(skill*15 + 55) + pc_checkskill(sd,TF_STEAL)*10 > rand()%1000) {
  594. 				if(pc_steal_item(sd,bl,pc_checkskill(sd,TF_STEAL)))
  595. 					clif_skill_nodamage(src,bl,TF_STEAL,skill,1);
  596. 				else
  597. 					clif_skill_fail(sd,RG_SNATCHER,USESKILL_FAIL_LEVEL,0);
  598. 			}
  599. 			// Chance to trigger Taekwon kicks [Dralnu]
  600. 			if(sc && !sc->data[SC_COMBO]) {
  601. 				if(sc->data[SC_READYSTORM] &&
  602. 					sc_start(src,SC_COMBO, 15, TK_STORMKICK,
  603. 						(2000 - 4*sstatus->agi - 2*sstatus->dex)))
  604. 					; //Stance triggered
  605. 				else if(sc->data[SC_READYDOWN] &&
  606. 					sc_start(src,SC_COMBO, 15, TK_DOWNKICK,
  607. 						(2000 - 4*sstatus->agi - 2*sstatus->dex)))
  608. 					; //Stance triggered
  609. 				else if(sc->data[SC_READYTURN] &&
  610. 					sc_start(src,SC_COMBO, 15, TK_TURNKICK,
  611. 						(2000 - 4*sstatus->agi - 2*sstatus->dex)))
  612. 					; //Stance triggered
  613. 				else if(sc->data[SC_READYCOUNTER])
  614. 				{	//additional chance from SG_FRIEND [Komurka]
  615. 					rate = 20;
  616. 					if (sc->data[SC_SKILLRATE_UP] && sc->data[SC_SKILLRATE_UP]->val1 == TK_COUNTER) {
  617. 						rate += rate*sc->data[SC_SKILLRATE_UP]->val2/100;
  618. 						status_change_end(src, SC_SKILLRATE_UP, INVALID_TIMER);
  619. 					}
  620. 					sc_start4(src,SC_COMBO, rate, TK_COUNTER, bl->id,0,0,
  621. 						(2000 - 4*sstatus->agi - 2*sstatus->dex));
  622. 				}
  623. 			}
  624. 		}
  625.  
  626. 		if (sc) {
  627. 			struct status_change_entry *sce;
  628. 			// Enchant Poison gives a chance to poison attacked enemies
  629. 			if((sce=sc->data[SC_ENCPOISON])) //Don't use sc_start since chance comes in 1/10000 rate.
  630. 				status_change_start(bl,SC_POISON,sce->val2, sce->val1,0,0,0,
  631. 					skill_get_time2(AS_ENCHANTPOISON,sce->val1),0);
  632. 			// Enchant Deadly Poison gives a chance to deadly poison attacked enemies
  633. 			if((sce=sc->data[SC_EDP]))
  634. 				sc_start4(bl,SC_DPOISON,sce->val2, sce->val1,0,0,0,
  635. 					skill_get_time2(ASC_EDP,sce->val1));
  636. 		}
  637. 	}
  638. 	break;
  639.  
  640. 	case SM_BASH:
  641. 		if( sd && skilllv > 5 && pc_checkskill(sd,SM_FATALBLOW)>0 ){
  642. 			//TODO: How much % per base level it actually is?
  643. 			sc_start(bl,SC_STUN,(5*(skilllv-5)+(int)sd->status.base_level/10),
  644. 				skilllv,skill_get_time2(SM_FATALBLOW,skilllv));
  645. 		}
  646. 		break;
  647.  
  648. 	case MER_CRASH:
  649. 		sc_start(bl,SC_STUN,(6*skilllv),skilllv,skill_get_time2(skillid,skilllv));
  650. 		break;
  651.  
  652. 	case AS_VENOMKNIFE:
  653. 		if (sd) //Poison chance must be that of Envenom. [Skotlex]
  654. 			skilllv = pc_checkskill(sd, TF_POISON);
  655. 	case TF_POISON:
  656. 	case AS_SPLASHER:
  657. 		if(!sc_start(bl,SC_POISON,(4*skilllv+10),skilllv,skill_get_time2(skillid,skilllv))
  658. 			&&	sd && skillid==TF_POISON
  659. 		)
  660. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  661. 		break;
  662.  
  663. 	case AS_SONICBLOW:
  664. 		sc_start(bl,SC_STUN,(2*skilllv+10),skilllv,skill_get_time2(skillid,skilllv));
  665. 		break;
  666.  
  667. 	case WZ_FIREPILLAR:
  668. 		unit_set_walkdelay(bl, tick, skill_get_time2(skillid, skilllv), 1);
  669. 		break;
  670.  
  671. 	case MG_FROSTDIVER:
  672. 	case WZ_FROSTNOVA:
  673. 		sc_start(bl,SC_FREEZE,skilllv*3+35,skilllv,skill_get_time2(skillid,skilllv));
  674. 		break;
  675.  
  676. 	case WZ_STORMGUST:
  677. 		 //Tharis pointed out that this is normal freeze chance with a base of 300%
  678. 		if(tsc->sg_counter >= 3 &&
  679. 			sc_start(bl,SC_FREEZE,300,skilllv,skill_get_time2(skillid,skilllv)))
  680. 			tsc->sg_counter = 0;
  681. 		break;
  682.  
  683. 	case WZ_METEOR:
  684. 		sc_start(bl,SC_STUN,3*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  685. 		break;
  686.  
  687. 	case WZ_VERMILION:
  688. 		sc_start(bl,SC_BLIND,4*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  689. 		break;
  690.  
  691. 	case HT_FREEZINGTRAP:
  692. 	case MA_FREEZINGTRAP:
  693. 		sc_start(bl,SC_FREEZE,(3*skilllv+35),skilllv,skill_get_time2(skillid,skilllv));
  694. 		break;
  695.  
  696. 	case HT_FLASHER:
  697. 		sc_start(bl,SC_BLIND,(10*skilllv+30),skilllv,skill_get_time2(skillid,skilllv));
  698. 		break;
  699.  
  700. 	case HT_LANDMINE:
  701. 	case MA_LANDMINE:
  702. 		sc_start(bl,SC_STUN,(5*skilllv+30),skilllv,skill_get_time2(skillid,skilllv));
  703. 		break;
  704.  
  705. 	case HT_SHOCKWAVE:
  706. 		status_percent_damage(src, bl, 0, 15*skilllv+5, false);
  707. 		break;
  708.  
  709. 	case HT_SANDMAN:
  710. 	case MA_SANDMAN:
  711. 		sc_start(bl,SC_SLEEP,(10*skilllv+40),skilllv,skill_get_time2(skillid,skilllv));
  712. 		break;
  713.  
  714. 	case TF_SPRINKLESAND:
  715. 		sc_start(bl,SC_BLIND,20,skilllv,skill_get_time2(skillid,skilllv));
  716. 		break;
  717.  
  718. 	case TF_THROWSTONE:
  719. 		sc_start(bl,SC_STUN,3,skilllv,skill_get_time(skillid,skilllv));
  720. 		sc_start(bl,SC_BLIND,3,skilllv,skill_get_time2(skillid,skilllv));
  721. 		break;
  722.  
  723. 	case NPC_DARKCROSS:
  724. 	case CR_HOLYCROSS:
  725. 		sc_start(bl,SC_BLIND,3*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  726. 		break;
  727.  
  728. 	case CR_GRANDCROSS:
  729. 	case NPC_GRANDDARKNESS:
  730. 		//Chance to cause blind status vs demon and undead element, but not against players
  731. 		if(!dstsd && (battle_check_undead(tstatus->race,tstatus->def_ele) || tstatus->race == RC_DEMON))
  732. 			sc_start(bl,SC_BLIND,100,skilllv,skill_get_time2(skillid,skilllv));
  733. 		attack_type |= BF_WEAPON;
  734. 		break;
  735.  
  736. 	case AM_ACIDTERROR:
  737. 		sc_start(bl,SC_BLEEDING,(skilllv*3),skilllv,skill_get_time2(skillid,skilllv));
  738. 		if (skill_break_equip(bl, EQP_ARMOR, 100*skill_get_time(skillid,skilllv), BCT_ENEMY))
  739. 			clif_emotion(bl,E_OMG);
  740. 		break;
  741.  
  742. 	case AM_DEMONSTRATION:
  743. 		skill_break_equip(bl, EQP_WEAPON, 100*skilllv, BCT_ENEMY);
  744. 		break;
  745.  
  746. 	case CR_SHIELDCHARGE:
  747. 		sc_start(bl,SC_STUN,(15+skilllv*5),skilllv,skill_get_time2(skillid,skilllv));
  748. 		break;
  749.  
  750. 	case PA_PRESSURE:
  751. 		status_percent_damage(src, bl, 0, 15+5*skilllv, false);
  752. 		break;
  753.  
  754. 	case RG_RAID:
  755. 		sc_start(bl,SC_STUN,(10+3*skilllv),skilllv,skill_get_time(skillid,skilllv));
  756. 		sc_start(bl,SC_BLIND,(10+3*skilllv),skilllv,skill_get_time2(skillid,skilllv));
  757. 		break;
  758.  
  759. 	case BA_FROSTJOKER:
  760. 		sc_start(bl,SC_FREEZE,(15+5*skilllv),skilllv,skill_get_time2(skillid,skilllv));
  761. 		break;
  762.  
  763. 	case DC_SCREAM:
  764. 		sc_start(bl,SC_STUN,(25+5*skilllv),skilllv,skill_get_time2(skillid,skilllv));
  765. 		break;
  766.  
  767. 	case BD_LULLABY:
  768. 		sc_start(bl,SC_SLEEP,15,skilllv,skill_get_time2(skillid,skilllv));
  769. 		break;
  770.  
  771. 	case DC_UGLYDANCE:
  772. 		rate = 5+5*skilllv;
  773. 		if(sd && (skill=pc_checkskill(sd,DC_DANCINGLESSON)))
  774. 		    rate += 5+skill;
  775. 		status_zap(bl, 0, rate);
  776.   		break;
  777. 	case SL_STUN:
  778. 		if (tstatus->size==1) //Only stuns mid-sized mobs.
  779. 			sc_start(bl,SC_STUN,(30+10*skilllv),skilllv,skill_get_time(skillid,skilllv));
  780. 		break;
  781.  
  782. 	case NPC_PETRIFYATTACK:
  783. 		sc_start4(bl,status_skill2sc(skillid),50+10*skilllv,
  784. 			skilllv,0,0,skill_get_time(skillid,skilllv),
  785. 			skill_get_time2(skillid,skilllv));
  786. 		break;
  787. 	case NPC_CURSEATTACK:
  788. 	case NPC_SLEEPATTACK:
  789. 	case NPC_BLINDATTACK:
  790. 	case NPC_POISON:
  791. 	case NPC_SILENCEATTACK:
  792. 	case NPC_STUNATTACK:
  793. 	case NPC_HELLPOWER:
  794. 		sc_start(bl,status_skill2sc(skillid),50+10*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  795. 		break;
  796. 	case NPC_ACIDBREATH:
  797. 	case NPC_ICEBREATH:
  798. 		sc_start(bl,status_skill2sc(skillid),70,skilllv,skill_get_time2(skillid,skilllv));
  799. 		break;
  800. 	case NPC_BLEEDING:
  801. 		sc_start(bl,SC_BLEEDING,(20*skilllv),skilllv,skill_get_time2(skillid,skilllv));
  802. 		break;
  803. 	case NPC_MENTALBREAKER:
  804. 	{	//Based on observations by Tharis, Mental Breaker should do SP damage
  805. 	  	//equal to Matk*skLevel.
  806. 		rate = sstatus->matk_min;
  807. 		if (rate < sstatus->matk_max)
  808. 			rate += rand()%(sstatus->matk_max - sstatus->matk_min);
  809. 		rate*=skilllv;
  810. 		status_zap(bl, 0, rate);
  811. 		break;
  812. 	}
  813. 	// Equipment breaking monster skills [Celest]
  814. 	case NPC_WEAPONBRAKER:
  815. 		skill_break_equip(bl, EQP_WEAPON, 150*skilllv, BCT_ENEMY);
  816. 		break;
  817. 	case NPC_ARMORBRAKE:
  818. 		skill_break_equip(bl, EQP_ARMOR, 150*skilllv, BCT_ENEMY);
  819. 		break;
  820. 	case NPC_HELMBRAKE:
  821. 		skill_break_equip(bl, EQP_HELM, 150*skilllv, BCT_ENEMY);
  822. 		break;
  823. 	case NPC_SHIELDBRAKE:
  824. 		skill_break_equip(bl, EQP_SHIELD, 150*skilllv, BCT_ENEMY);
  825. 		break;
  826.  
  827. 	case CH_TIGERFIST:
  828. 		sc_start(bl,SC_STOP,(10+skilllv*10),0,skill_get_time2(skillid,skilllv));
  829. 		break;
  830.  
  831. 	case LK_SPIRALPIERCE:
  832. 	case ML_SPIRALPIERCE:
  833. 		sc_start(bl,SC_STOP,(15+skilllv*5),0,skill_get_time2(skillid,skilllv));
  834. 		break;
  835.  
  836. 	case ST_REJECTSWORD:
  837. 		sc_start(bl,SC_AUTOCOUNTER,(skilllv*15),skilllv,skill_get_time(skillid,skilllv));
  838. 		break;
  839.  
  840. 	case PF_FOGWALL:
  841. 		if (src != bl && !tsc->data[SC_DELUGE])
  842. 			status_change_start(bl,SC_BLIND,10000,skilllv,0,0,0,skill_get_time2(skillid,skilllv),8);
  843. 		break;
  844.  
  845. 	case LK_HEADCRUSH: //Headcrush has chance of causing Bleeding status, except on demon and undead element
  846. 		if (!(battle_check_undead(tstatus->race, tstatus->def_ele) || tstatus->race == RC_DEMON))
  847. 			sc_start(bl, SC_BLEEDING,50, skilllv, skill_get_time2(skillid,skilllv));
  848. 		break;
  849.  
  850. 	case LK_JOINTBEAT:
  851. 		status = status_skill2sc(skillid);
  852. 		if (tsc->jb_flag) {
  853. 			sc_start2(bl,status,(5*skilllv+5),skilllv,tsc->jb_flag&BREAK_FLAGS,skill_get_time2(skillid,skilllv));
  854. 			tsc->jb_flag = 0;
  855. 		}
  856. 		break;
  857. 	case ASC_METEORASSAULT:
  858. 		//Any enemies hit by this skill will receive Stun, Darkness, or external bleeding status ailment with a 5%+5*SkillLV% chance.
  859. 		switch(rand()%3) {
  860. 			case 0:
  861. 				sc_start(bl,SC_BLIND,(5+skilllv*5),skilllv,skill_get_time2(skillid,1));
  862. 				break;
  863. 			case 1:
  864. 				sc_start(bl,SC_STUN,(5+skilllv*5),skilllv,skill_get_time2(skillid,2));
  865. 				break;
  866. 			default:
  867. 				sc_start(bl,SC_BLEEDING,(5+skilllv*5),skilllv,skill_get_time2(skillid,3));
  868.   		}
  869. 		break;
  870.  
  871. 	case HW_NAPALMVULCAN:
  872. 		sc_start(bl,SC_CURSE,5*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  873. 		break;
  874.  
  875. 	case WS_CARTTERMINATION:	// Cart termination
  876. 		sc_start(bl,SC_STUN,5*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  877. 		break;
  878.  
  879. 	case CR_ACIDDEMONSTRATION:
  880. 		skill_break_equip(bl, EQP_WEAPON|EQP_ARMOR, 100*skilllv, BCT_ENEMY);
  881. 		break;
  882.  
  883. 	case TK_DOWNKICK:
  884. 		sc_start(bl,SC_STUN,100,skilllv,skill_get_time2(skillid,skilllv));
  885. 		break;
  886.  
  887. 	case TK_JUMPKICK:
  888. 		if( dstsd && dstsd->class_ != MAPID_SOUL_LINKER && !tsc->data[SC_PRESERVE] )
  889. 		{// debuff the following statuses
  890. 			status_change_end(bl, SC_SPIRIT, INVALID_TIMER);
  891. 			status_change_end(bl, SC_ADRENALINE2, INVALID_TIMER);
  892. 			status_change_end(bl, SC_KAITE, INVALID_TIMER);
  893. 			status_change_end(bl, SC_KAAHI, INVALID_TIMER);
  894. 			status_change_end(bl, SC_ONEHAND, INVALID_TIMER);
  895. 			status_change_end(bl, SC_ASPDPOTION2, INVALID_TIMER);
  896. 		}
  897. 		break;
  898. 	case TK_TURNKICK:
  899. 	case MO_BALKYOUNG: //Note: attack_type is passed as BF_WEAPON for the actual target, BF_MISC for the splash-affected mobs.
  900. 		if(attack_type&BF_MISC) //70% base stun chance...
  901. 			sc_start(bl,SC_STUN,70,skilllv,skill_get_time2(skillid,skilllv));
  902. 		break;
  903. 	case GS_BULLSEYE: //0.1% coma rate.
  904. 		if(tstatus->race == RC_BRUTE || tstatus->race == RC_DEMIHUMAN)
  905. 			status_change_start(bl,SC_COMA,10,skilllv,0,0,0,0,0);
  906. 		break;
  907. 	case GS_PIERCINGSHOT:
  908. 		sc_start(bl,SC_BLEEDING,(skilllv*3),skilllv,skill_get_time2(skillid,skilllv));
  909. 		break;
  910. 	case NJ_HYOUSYOURAKU:
  911. 		sc_start(bl,SC_FREEZE,(10+10*skilllv),skilllv,skill_get_time2(skillid,skilllv));
  912. 		break;
  913. 	case GS_FLING:
  914. 		sc_start(bl,SC_FLING,100, sd?sd->spiritball_old:5,skill_get_time(skillid,skilllv));
  915. 		break;
  916. 	case GS_DISARM:
  917. 		rate = 3*skilllv;
  918. 		if (sstatus->dex > tstatus->dex)
  919. 			rate += (sstatus->dex - tstatus->dex)/5; //TODO: Made up formula
  920. 		skill_strip_equip(bl, EQP_WEAPON, rate, skilllv, skill_get_time(skillid,skilllv));
  921. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  922. 		break;
  923. 	case NPC_EVILLAND:
  924. 		sc_start(bl,SC_BLIND,5*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  925. 		break;
  926. 	case NPC_HELLJUDGEMENT:
  927. 		sc_start(bl,SC_CURSE,100,skilllv,skill_get_time2(skillid,skilllv));
  928. 		break;
  929. 	case NPC_CRITICALWOUND:
  930. 		sc_start(bl,SC_CRITICALWOUND,100,skilllv,skill_get_time2(skillid,skilllv));
  931. 		break;
  932. 	}
  933.  
  934. 	if (md && battle_config.summons_trigger_autospells && md->master_id && md->special_state.ai)
  935. 	{	//Pass heritage to Master for status causing effects. [Skotlex]
  936. 		sd = map_id2sd(md->master_id);
  937. 		src = sd?&sd->bl:src;
  938. 	}
  939.  
  940. 	if( attack_type&BF_WEAPON )
  941. 	{ // Coma, Breaking Equipment
  942. 		if( sd && sd->special_state.bonus_coma )
  943. 		{
  944. 			rate  = sd->weapon_coma_ele[tstatus->def_ele];
  945. 			rate += sd->weapon_coma_race[tstatus->race];
  946. 			rate += sd->weapon_coma_race[tstatus->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
  947. 			if (rate)
  948. 				status_change_start(bl, SC_COMA, rate, 0, 0, 0, 0, 0, 0);
  949. 		}
  950. 		if( sd && battle_config.equip_self_break_rate )
  951. 		{	// Self weapon breaking
  952. 			rate = battle_config.equip_natural_break_rate;
  953. 			if( sc )
  954. 			{
  955. 				if(sc->data[SC_OVERTHRUST])
  956. 					rate += 10;
  957. 				if(sc->data[SC_MAXOVERTHRUST])
  958. 					rate += 10;
  959. 			}
  960. 			if( rate )
  961. 				skill_break_equip(src, EQP_WEAPON, rate, BCT_SELF);
  962. 		}	
  963. 		if( battle_config.equip_skill_break_rate && skillid != WS_CARTTERMINATION && skillid != ITM_TOMAHAWK )
  964. 		{	// Cart Termination/Tomahawk won't trigger breaking data. Why? No idea, go ask Gravity.
  965. 			// Target weapon breaking
  966. 			rate = 0;
  967. 			if( sd )
  968. 				rate += sd->break_weapon_rate;
  969. 			if( sc && sc->data[SC_MELTDOWN] )
  970. 				rate += sc->data[SC_MELTDOWN]->val2;
  971. 			if( rate )
  972. 				skill_break_equip(bl, EQP_WEAPON, rate, BCT_ENEMY);
  973.  
  974. 			// Target armor breaking
  975. 			rate = 0;
  976. 			if( sd )
  977. 				rate += sd->break_armor_rate;
  978. 			if( sc && sc->data[SC_MELTDOWN] )
  979. 				rate += sc->data[SC_MELTDOWN]->val3;
  980. 			if( rate )
  981. 				skill_break_equip(bl, EQP_ARMOR, rate, BCT_ENEMY);
  982. 		}
  983. 	}
  984.  
  985. 	// Autospell when attacking
  986. 	if( sd && !status_isdead(bl) && sd->autospell[0].id )
  987. 	{
  988. 		struct block_list *tbl;
  989. 		struct unit_data *ud;
  990. 		int i, skilllv;
  991.  
  992. 		for (i = 0; i < ARRAYLENGTH(sd->autospell) && sd->autospell[i].id; i++) {
  993.  
  994. 			if(!(sd->autospell[i].flag&attack_type&BF_WEAPONMASK &&
  995. 				 sd->autospell[i].flag&attack_type&BF_RANGEMASK &&
  996. 				 sd->autospell[i].flag&attack_type&BF_SKILLMASK))
  997. 				continue; // one or more trigger conditions were not fulfilled
  998.  
  999. 			skill = (sd->autospell[i].id > 0) ? sd->autospell[i].id : -sd->autospell[i].id;
  1000.  
  1001. 			if (skillnotok(skill, sd))
  1002. 				continue;
  1003.  
  1004. 			skilllv = sd->autospell[i].lv?sd->autospell[i].lv:1;
  1005. 			if (skilllv < 0) skilllv = 1+rand()%(-skilllv);
  1006.  
  1007. 			rate = (!sd->state.arrow_atk) ? sd->autospell[i].rate : sd->autospell[i].rate / 2;
  1008.  
  1009. 			if (rand()%1000 >= rate)
  1010. 				continue;
  1011.  
  1012. 			tbl = (sd->autospell[i].id < 0) ? src : bl;
  1013.  
  1014. 			if( battle_config.autospell_check_range &&
  1015. 				!battle_check_range(src, tbl, skill_get_range2(src, skill,skilllv) + (skill == RG_CLOSECONFINE?0:1)) )
  1016. 				continue;
  1017.  
  1018. 			if (skill == AS_SONICBLOW)
  1019. 				pc_stop_attack(sd); //Special case, Sonic Blow autospell should stop the player attacking.
  1020.  
  1021. 			sd->state.autocast = 1;
  1022. 			skill_consume_requirement(sd,skill,skilllv,1);
  1023. 			switch (skill_get_casttype(skill)) {
  1024. 				case CAST_GROUND:
  1025. 					skill_castend_pos2(src, tbl->x, tbl->y, skill, skilllv, tick, 0);
  1026. 					break;
  1027. 				case CAST_NODAMAGE:
  1028. 					skill_castend_nodamage_id(src, tbl, skill, skilllv, tick, 0);
  1029. 					break;
  1030. 				case CAST_DAMAGE:
  1031. 					skill_castend_damage_id(src, tbl, skill, skilllv, tick, 0);
  1032. 					break;
  1033. 			}
  1034. 			sd->state.autocast = 0;
  1035. 			//Set canact delay. [Skotlex]
  1036. 			ud = unit_bl2ud(src);
  1037. 			if (ud) {
  1038. 				rate = skill_delayfix(src, skill, skilllv);
  1039. 				if (DIFF_TICK(ud->canact_tick, tick + rate) < 0){
  1040. 					ud->canact_tick = tick+rate;
  1041. 					if ( battle_config.display_status_timers && sd )
  1042. 						clif_status_change(src, SI_ACTIONDELAY, 1, rate);
  1043. 				}
  1044. 			}
  1045. 		}
  1046. 	}
  1047.  
  1048. 	//Autobonus when attacking
  1049. 	if( sd && sd->autobonus[0].rate )
  1050. 	{
  1051. 		int i;
  1052. 		for( i = 0; i < ARRAYLENGTH(sd->autobonus); i++ )
  1053. 		{
  1054. 			if( rand()%1000 >= sd->autobonus[i].rate )
  1055. 				continue;
  1056. 			if( sd->autobonus[i].active != INVALID_TIMER )
  1057. 				continue;
  1058. 			if(!(sd->autobonus[i].atk_type&attack_type&BF_WEAPONMASK &&
  1059. 				 sd->autobonus[i].atk_type&attack_type&BF_RANGEMASK &&
  1060. 				 sd->autobonus[i].atk_type&attack_type&BF_SKILLMASK))
  1061. 				continue; // one or more trigger conditions were not fulfilled
  1062. 			pc_exeautobonus(sd,&sd->autobonus[i]);
  1063. 		}
  1064. 	}
  1065.  
  1066. 	//Polymorph
  1067. 	if(sd && sd->classchange && attack_type&BF_WEAPON &&
  1068. 		dstmd && !(tstatus->mode&MD_BOSS) &&
  1069. 		(rand()%10000 < sd->classchange))
  1070. 	{
  1071. 		struct mob_db *mob;
  1072. 		int class_;
  1073. 		skill = 0;
  1074. 		do {
  1075. 			do {
  1076. 				class_ = rand() % MAX_MOB_DB;
  1077. 			} while (!mobdb_checkid(class_));
  1078.  
  1079. 			rate = rand() % 1000000;
  1080. 			mob = mob_db(class_);
  1081. 		} while (
  1082. 			(mob->status.mode&(MD_BOSS|MD_PLANT) || mob->summonper[0] <= rate) &&
  1083. 		  	(skill++) < 2000);
  1084. 		if (skill < 2000)
  1085. 			mob_class_change(dstmd,class_);
  1086. 	}
  1087.  
  1088. 	return 0;
  1089. }
  1090.  
  1091. int skill_onskillusage(struct map_session_data *sd, struct block_list *bl, int skillid, unsigned int tick)
  1092. {
  1093. 	int skill, skilllv, i;
  1094. 	struct block_list *tbl;
  1095.  
  1096. 	if( sd == NULL || skillid <= 0 )
  1097. 		return 0;
  1098.  
  1099. 	for( i = 0; i < ARRAYLENGTH(sd->autospell3) && sd->autospell3[i].flag; i++ )
  1100. 	{
  1101. 		if( sd->autospell3[i].flag != skillid )
  1102. 			continue;
  1103.  
  1104. 		if( sd->autospell3[i].lock )
  1105. 			continue;  // autospell already being executed
  1106.  
  1107. 		skill = (sd->autospell3[i].id > 0) ? sd->autospell3[i].id : -sd->autospell3[i].id;
  1108. 		if( skillnotok(skill, sd) )
  1109. 			continue;
  1110.  
  1111. 		skilllv = sd->autospell3[i].lv ? sd->autospell3[i].lv : 1;
  1112. 		if( skilllv < 0 ) skilllv = 1 + rand()%(-skilllv);
  1113.  
  1114. 		if( sd->autospell3[i].id >= 0 && bl == NULL )
  1115. 			continue; // No target
  1116. 		if( rand()%1000 >= sd->autospell3[i].rate )
  1117. 			continue;
  1118. 		tbl = (sd->autospell3[i].id < 0) ? &sd->bl : bl;
  1119.  
  1120. 		if( battle_config.autospell_check_range &&
  1121. 			!battle_check_range(&sd->bl, tbl, skill_get_range2(&sd->bl, skill,skilllv) + (skill == RG_CLOSECONFINE?0:1)) )
  1122. 			continue;
  1123.  
  1124. 		sd->state.autocast = 1;
  1125. 		sd->autospell3[i].lock = true;
  1126. 		skill_consume_requirement(sd,skill,skilllv,1);
  1127. 		switch( skill_get_casttype(skill) )
  1128. 		{
  1129. 			case CAST_GROUND:   skill_castend_pos2(&sd->bl, tbl->x, tbl->y, skill, skilllv, tick, 0); break;
  1130. 			case CAST_NODAMAGE: skill_castend_nodamage_id(&sd->bl, tbl, skill, skilllv, tick, 0); break;
  1131. 			case CAST_DAMAGE:   skill_castend_damage_id(&sd->bl, tbl, skill, skilllv, tick, 0); break;
  1132. 		}
  1133. 		sd->autospell3[i].lock = false;
  1134. 		sd->state.autocast = 0;
  1135. 	}
  1136.  
  1137. 	if( sd && sd->autobonus3[0].rate )
  1138. 	{
  1139. 		for( i = 0; i < ARRAYLENGTH(sd->autobonus3); i++ )
  1140. 		{
  1141. 			if( rand()%1000 >= sd->autobonus3[i].rate )
  1142. 				continue;
  1143. 			if( sd->autobonus3[i].active != INVALID_TIMER )
  1144. 				continue;
  1145. 			if( sd->autobonus3[i].atk_type != skillid )
  1146. 				continue;
  1147. 			pc_exeautobonus(sd,&sd->autobonus3[i]);
  1148. 		}
  1149. 	}
  1150.  
  1151. 	return 1;
  1152. }
  1153.  
  1154. /* Splitted off from skill_additional_effect, which is never called when the
  1155.  * attack skill kills the enemy. Place in this function counter status effects
  1156.  * when using skills (eg: Asura's sp regen penalty, or counter-status effects
  1157.  * from cards) that will take effect on the source, not the target. [Skotlex]
  1158.  * Note: Currently this function only applies to Extremity Fist and BF_WEAPON
  1159.  * type of skills, so not every instance of skill_additional_effect needs a call
  1160.  * to this one.
  1161.  */
  1162. int skill_counter_additional_effect (struct block_list* src, struct block_list *bl, int skillid, int skilllv, int attack_type, unsigned int tick)
  1163. {
  1164. 	int rate;
  1165. 	struct map_session_data *sd=NULL;
  1166. 	struct map_session_data *dstsd=NULL;
  1167. 	struct status_change *tsc;
  1168.  
  1169. 	nullpo_ret(src);
  1170. 	nullpo_ret(bl);
  1171.  
  1172. 	if(skillid < 0) return 0;
  1173. 	if(skillid > 0 && skilllv <= 0) return 0;	// don't forget auto attacks! - celest
  1174.  
  1175. 	tsc = status_get_sc(bl);
  1176. 	if (tsc && !tsc->count)
  1177. 		tsc = NULL;
  1178.  
  1179. 	sd = BL_CAST(BL_PC, src);
  1180. 	dstsd = BL_CAST(BL_PC, bl);
  1181.  
  1182. 	if(dstsd && attack_type&BF_WEAPON)
  1183. 	{	//Counter effects.
  1184. 		enum sc_type type;
  1185. 		int i, time;
  1186. 		for(i=0; i < ARRAYLENGTH(dstsd->addeff2) && dstsd->addeff2[i].flag; i++)
  1187. 		{
  1188. 			rate = dstsd->addeff2[i].rate;
  1189. 			if (attack_type&BF_LONG)
  1190. 				rate+=dstsd->addeff2[i].arrow_rate;
  1191. 			if (!rate) continue;
  1192.  
  1193. 			if ((dstsd->addeff2[i].flag&(ATF_LONG|ATF_SHORT)) != (ATF_LONG|ATF_SHORT))
  1194. 			{	//Trigger has range consideration.
  1195. 				if((dstsd->addeff2[i].flag&ATF_LONG && !(attack_type&BF_LONG)) ||
  1196. 					(dstsd->addeff2[i].flag&ATF_SHORT && !(attack_type&BF_SHORT)))
  1197. 					continue; //Range Failed.
  1198. 			}
  1199. 			type = dstsd->addeff2[i].id;
  1200. 			time = skill_get_time2(status_sc2skill(type),7);
  1201.  
  1202. 			if (dstsd->addeff2[i].flag&ATF_TARGET)
  1203. 				status_change_start(src,type,rate,7,0,0,0,time,0);
  1204.  
  1205. 			if (dstsd->addeff2[i].flag&ATF_SELF && !status_isdead(bl))
  1206. 				status_change_start(bl,type,rate,7,0,0,0,time,0);
  1207. 		}
  1208. 	}
  1209.  
  1210. 	switch(skillid){
  1211. 	case MO_EXTREMITYFIST:
  1212. 		sc_start(src,status_skill2sc(skillid),100,skilllv,skill_get_time2(skillid,skilllv));
  1213. 		break;
  1214. 	case GS_FULLBUSTER:
  1215. 		sc_start(src,SC_BLIND,2*skilllv,skilllv,skill_get_time2(skillid,skilllv));
  1216. 		break;
  1217. 	case HFLI_SBR44:	//[orn]
  1218. 	case HVAN_EXPLOSION:
  1219. 		if(src->type == BL_HOM){
  1220. 			TBL_HOM *hd = (TBL_HOM*)src;
  1221. 			hd->homunculus.intimacy = 200;
  1222. 			if (hd->master)
  1223. 				clif_send_homdata(hd->master,SP_INTIMATE,hd->homunculus.intimacy/100);
  1224. 		}
  1225. 		break;
  1226. 	case CR_GRANDCROSS:
  1227. 	case NPC_GRANDDARKNESS:
  1228. 		attack_type |= BF_WEAPON;
  1229. 		break;
  1230. 	}
  1231.  
  1232. 	if(sd && (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR &&
  1233. 		rand()%10000 < battle_config.sg_miracle_skill_ratio)	//SG_MIRACLE [Komurka]
  1234. 		sc_start(src,SC_MIRACLE,100,1,battle_config.sg_miracle_skill_duration);
  1235.  
  1236. 	if(sd && skillid && attack_type&BF_MAGIC && status_isdead(bl) &&
  1237. 	 	!(skill_get_inf(skillid)&(INF_GROUND_SKILL|INF_SELF_SKILL)) &&
  1238. 		(rate=pc_checkskill(sd,HW_SOULDRAIN))>0
  1239. 	){	//Soul Drain should only work on targetted spells [Skotlex]
  1240. 		if (pc_issit(sd)) pc_setstand(sd); //Character stuck in attacking animation while 'sitting' fix. [Skotlex]
  1241. 		clif_skill_nodamage(src,bl,HW_SOULDRAIN,rate,1);
  1242. 		status_heal(src, 0, status_get_lv(bl)*(95+15*rate)/100, 2);
  1243. 	}
  1244.  
  1245. 	if( sd && status_isdead(bl) )
  1246. 	{
  1247. 		int sp = 0, hp = 0;
  1248. 		if( attack_type&BF_WEAPON )
  1249. 		{
  1250. 			sp += sd->sp_gain_value;
  1251. 			sp += sd->sp_gain_race[status_get_race(bl)];
  1252. 			sp += sd->sp_gain_race[is_boss(bl)?RC_BOSS:RC_NONBOSS];
  1253. 			hp += sd->hp_gain_value;
  1254. 		}
  1255. 		if( attack_type&BF_MAGIC )
  1256. 		{
  1257. 			sp += sd->magic_sp_gain_value;
  1258. 			hp += sd->magic_hp_gain_value;
  1259. 		}
  1260. 		if( hp || sp )
  1261. 			status_heal(src, hp, sp, battle_config.show_hp_sp_gain?2:0);
  1262. 	}
  1263.  
  1264. 	// Trigger counter-spells to retaliate against damage causing skills.
  1265. 	if(dstsd && !status_isdead(bl) && dstsd->autospell2[0].id &&
  1266. 		!(skillid && skill_get_nk(skillid)&NK_NO_DAMAGE))
  1267. 	{
  1268. 		struct block_list *tbl;
  1269. 		struct unit_data *ud;
  1270. 		int i, skillid, skilllv, rate;
  1271.  
  1272. 		for (i = 0; i < ARRAYLENGTH(dstsd->autospell2) && dstsd->autospell2[i].id; i++) {
  1273.  
  1274. 			if(!(dstsd->autospell2[i].flag&attack_type&BF_WEAPONMASK &&
  1275. 				 dstsd->autospell2[i].flag&attack_type&BF_RANGEMASK &&
  1276. 				 dstsd->autospell2[i].flag&attack_type&BF_SKILLMASK))
  1277. 				continue; // one or more trigger conditions were not fulfilled
  1278.  
  1279. 			skillid = (dstsd->autospell2[i].id > 0) ? dstsd->autospell2[i].id : -dstsd->autospell2[i].id;
  1280. 			skilllv = dstsd->autospell2[i].lv?dstsd->autospell2[i].lv:1;
  1281. 			if (skilllv < 0) skilllv = 1+rand()%(-skilllv);
  1282.  
  1283. 			rate = dstsd->autospell2[i].rate;
  1284. 			if (attack_type&BF_LONG)
  1285. 				 rate>>=1;
  1286.  
  1287. 			if (skillnotok(skillid, dstsd))
  1288. 				continue;
  1289. 			if (rand()%1000 >= rate)
  1290. 				continue;
  1291.  
  1292. 			tbl = (dstsd->autospell2[i].id < 0) ? bl : src;
  1293.  
  1294. 			if( !battle_check_range(src, tbl, skill_get_range2(src, skillid,skilllv) + (skillid == RG_CLOSECONFINE?0:1)) && battle_config.autospell_check_range )
  1295. 				continue;
  1296.  
  1297. 			dstsd->state.autocast = 1;
  1298. 			skill_consume_requirement(dstsd,skillid,skilllv,1);
  1299. 			switch (skill_get_casttype(skillid)) {
  1300. 				case CAST_GROUND:
  1301. 					skill_castend_pos2(bl, tbl->x, tbl->y, skillid, skilllv, tick, 0);
  1302. 					break;
  1303. 				case CAST_NODAMAGE:
  1304. 					skill_castend_nodamage_id(bl, tbl, skillid, skilllv, tick, 0);
  1305. 					break;
  1306. 				case CAST_DAMAGE:
  1307. 					skill_castend_damage_id(bl, tbl, skillid, skilllv, tick, 0);
  1308. 					break;
  1309. 			}
  1310. 			dstsd->state.autocast = 0;
  1311. 			//Set canact delay. [Skotlex]
  1312. 			ud = unit_bl2ud(bl);
  1313. 			if (ud) {
  1314. 				rate = skill_delayfix(bl, skillid, skilllv);
  1315. 				if (DIFF_TICK(ud->canact_tick, tick + rate) < 0){
  1316. 					ud->canact_tick = tick+rate;
  1317. 					if ( battle_config.display_status_timers && dstsd )
  1318. 						clif_status_change(bl, SI_ACTIONDELAY, 1, rate);
  1319. 				}
  1320. 			}
  1321. 		}
  1322. 	}
  1323.  
  1324. 	//Autobonus when attacked
  1325. 	if( dstsd && !status_isdead(bl) && dstsd->autobonus2[0].rate && !(skillid && skill_get_nk(skillid)&NK_NO_DAMAGE) )
  1326. 	{
  1327. 		int i;
  1328. 		for( i = 0; i < ARRAYLENGTH(dstsd->autobonus2); i++ )
  1329. 		{
  1330. 			if( rand()%1000 >= dstsd->autobonus2[i].rate )
  1331. 				continue;
  1332. 			if( dstsd->autobonus2[i].active != INVALID_TIMER )
  1333. 				continue;
  1334. 			if(!(dstsd->autobonus2[i].atk_type&attack_type&BF_WEAPONMASK &&
  1335. 				 dstsd->autobonus2[i].atk_type&attack_type&BF_RANGEMASK &&
  1336. 				 dstsd->autobonus2[i].atk_type&attack_type&BF_SKILLMASK))
  1337. 				continue; // one or more trigger conditions were not fulfilled
  1338. 			pc_exeautobonus(dstsd,&dstsd->autobonus2[i]);
  1339. 		}
  1340. 	}
  1341.  
  1342. 	return 0;
  1343. }
  1344. /*=========================================================================
  1345.  Breaks equipment. On-non players causes the corresponding strip effect.
  1346.  - rate goes from 0 to 10000 (100.00%)
  1347.  - flag is a BCT_ flag to indicate which type of adjustment should be used
  1348.    (BCT_ENEMY/BCT_PARTY/BCT_SELF) are the valid values.
  1349. --------------------------------------------------------------------------*/
  1350. int skill_break_equip (struct block_list *bl, unsigned short where, int rate, int flag)
  1351. {
  1352. 	const int where_list[4]     = {EQP_WEAPON, EQP_ARMOR, EQP_SHIELD, EQP_HELM};
  1353. 	const enum sc_type scatk[4] = {SC_STRIPWEAPON, SC_STRIPARMOR, SC_STRIPSHIELD, SC_STRIPHELM};
  1354. 	const enum sc_type scdef[4] = {SC_CP_WEAPON, SC_CP_ARMOR, SC_CP_SHIELD, SC_CP_HELM};
  1355. 	struct status_change *sc = status_get_sc(bl);
  1356. 	int i,j;
  1357. 	TBL_PC *sd;
  1358. 	sd = BL_CAST(BL_PC, bl);
  1359. 	if (sc && !sc->count)
  1360. 		sc = NULL;
  1361.  
  1362. 	if (sd) {
  1363. 		if (sd->unbreakable_equip)
  1364. 			where &= ~sd->unbreakable_equip;
  1365. 		if (sd->unbreakable)
  1366. 			rate -= rate*sd->unbreakable/100;
  1367. 		if (where&EQP_WEAPON) {
  1368. 			switch (sd->status.weapon) {
  1369. 				case W_FIST:	//Bare fists should not break :P
  1370. 				case W_1HAXE:
  1371. 				case W_2HAXE:
  1372. 				case W_MACE: // Axes and Maces can't be broken [DracoRPG]
  1373. 				case W_2HMACE:
  1374. 				case W_STAFF:
  1375. 				case W_2HSTAFF:
  1376. 				case W_BOOK: //Rods and Books can't be broken [Skotlex]
  1377. 				case W_HUUMA:
  1378. 					where &= ~EQP_WEAPON;
  1379. 			}
  1380. 		}
  1381. 	}
  1382. 	if (flag&BCT_ENEMY) {
  1383. 		if (battle_config.equip_skill_break_rate != 100)
  1384. 			rate = rate*battle_config.equip_skill_break_rate/100;
  1385. 	} else if (flag&(BCT_PARTY|BCT_SELF)) {
  1386. 		if (battle_config.equip_self_break_rate != 100)
  1387. 			rate = rate*battle_config.equip_self_break_rate/100;
  1388. 	}
  1389.  
  1390. 	for (i = 0; i < 4; i++) {
  1391. 		if (where&where_list[i]) {
  1392. 			if (sc && sc->count && sc->data[scdef[i]])
  1393. 				where&=~where_list[i];
  1394. 			else if (rand()%10000 >= rate)
  1395. 				where&=~where_list[i];
  1396. 			else if (!sd && !(status_get_mode(bl)&MD_BOSS)) //Cause Strip effect.
  1397. 				sc_start(bl,scatk[i],100,0,skill_get_time(status_sc2skill(scatk[i]),1));
  1398. 		}
  1399. 	}
  1400. 	if (!where) //Nothing to break.
  1401. 		return 0;
  1402. 	if (sd) {
  1403. 		for (i = 0; i < EQI_MAX; i++) {
  1404. 			j = sd->equip_index[i];
  1405. 			if (j < 0 || sd->status.inventory[j].attribute == 1 || !sd->inventory_data[j])
  1406. 				continue;
  1407. 			flag = 0;
  1408. 			switch(i) {
  1409. 				case EQI_HEAD_TOP: //Upper Head
  1410. 					flag = (where&EQP_HELM);
  1411. 					break;
  1412. 				case EQI_ARMOR: //Body
  1413. 					flag = (where&EQP_ARMOR);
  1414. 					break;
  1415. 				case EQI_HAND_R: //Left/Right hands
  1416. 				case EQI_HAND_L:
  1417. 					flag = (
  1418. 						(where&EQP_WEAPON && sd->inventory_data[j]->type == IT_WEAPON) ||
  1419. 						(where&EQP_SHIELD && sd->inventory_data[j]->type == IT_ARMOR));
  1420. 					break;
  1421. 				case EQI_SHOES:
  1422. 					flag = (where&EQP_SHOES);
  1423. 					break;
  1424. 				case EQI_GARMENT:
  1425. 					flag = (where&EQP_GARMENT);
  1426. 					break;
  1427. 				default:
  1428. 					continue;
  1429. 			}
  1430. 			if (flag) {
  1431. 				sd->status.inventory[j].attribute = 1;
  1432. 				pc_unequipitem(sd, j, 3);
  1433. 			}
  1434. 		}
  1435. 		clif_equiplist(sd);
  1436. 	}
  1437.  
  1438. 	return where; //Return list of pieces broken.
  1439. }
  1440.  
  1441. int skill_strip_equip(struct block_list *bl, unsigned short where, int rate, int lv, int time)
  1442. {
  1443. 	struct status_change *sc;
  1444. 	const int pos[4]             = {EQP_WEAPON, EQP_SHIELD, EQP_ARMOR, EQP_HELM};
  1445. 	const enum sc_type sc_atk[4] = {SC_STRIPWEAPON, SC_STRIPSHIELD, SC_STRIPARMOR, SC_STRIPHELM};
  1446. 	const enum sc_type sc_def[4] = {SC_CP_WEAPON, SC_CP_SHIELD, SC_CP_ARMOR, SC_CP_HELM};
  1447. 	int i;
  1448.  
  1449. 	if (rand()%100 >= rate)
  1450. 		return 0;
  1451.  
  1452. 	sc = status_get_sc(bl);
  1453. 	if (!sc)
  1454. 		return 0;
  1455.  
  1456. 	for (i = 0; i < ARRAYLENGTH(pos); i++) {
  1457. 		if (where&pos[i] && sc->data[sc_def[i]])
  1458. 			where&=~pos[i];
  1459. 	}
  1460. 	if (!where) return 0;
  1461.  
  1462. 	for (i = 0; i < ARRAYLENGTH(pos); i++) {
  1463. 		if (where&pos[i] && !sc_start(bl, sc_atk[i], 100, lv, time))
  1464. 			where&=~pos[i];
  1465. 	}
  1466. 	return where?1:0;
  1467. }
  1468.  
  1469.  
  1470. /*=========================================================================
  1471.  Used to knock back players, monsters, traps, etc
  1472.  - 'count' is the number of squares to knock back
  1473.  - 'direction' indicates the way OPPOSITE to the knockback direction (or -1 for default behavior)
  1474.  - if 'flag&0x1', position update packets must not be sent.
  1475.  -------------------------------------------------------------------------*/
  1476. int skill_blown(struct block_list* src, struct block_list* target, int count, int direction, int flag)
  1477. {
  1478. 	int dx = 0, dy = 0;
  1479. 	struct skill_unit* su = NULL;
  1480.  
  1481. 	nullpo_ret(src);
  1482.  
  1483. 	if (src != target && (map_flag_gvg(target->m) || map[target->m].flag.battleground))
  1484. 		return 0; //No knocking back in WoE
  1485. 	if (count == 0)
  1486. 		return 0; //Actual knockback distance is 0.
  1487.  
  1488. 	switch (target->type)
  1489. 	{
  1490. 		case BL_MOB:
  1491. 		{
  1492. 			struct mob_data* md = BL_CAST(BL_MOB, target);
  1493. 			if( md->class_ == MOBID_EMPERIUM )
  1494. 				return 0;
  1495. 			if(src != target && is_boss(target)) //Bosses can't be knocked-back
  1496. 				return 0;
  1497. 		}
  1498. 			break;
  1499. 		case BL_PC:
  1500. 		{
  1501. 			struct map_session_data *sd = BL_CAST(BL_PC, target);
  1502. 			if( sd->sc.data[SC_BASILICA] && sd->sc.data[SC_BASILICA]->val4 == sd->bl.id && !is_boss(src))
  1503. 				return 0; // Basilica caster can't be knocked-back by normal monsters.
  1504. 			if( src != target && sd->special_state.no_knockback )
  1505. 				return 0;
  1506. 		}
  1507. 			break;
  1508. 		case BL_SKILL:
  1509. 			su = (struct skill_unit *)target;
  1510. 			if( su->group->unit_id == UNT_ANKLESNARE )
  1511. 				return 0; // ankle snare cannot be knocked back
  1512. 			break;
  1513. 	}
  1514.  
  1515. 	if (direction == -1) // <optimized>: do the computation here instead of outside
  1516. 		direction = map_calc_dir(target, src->x, src->y); // direction from src to target, reversed
  1517.  
  1518. 	if (direction >= 0 && direction < 8)
  1519. 	{	// take the reversed 'direction' and reverse it
  1520. 		dx = -dirx[direction];
  1521. 		dy = -diry[direction];
  1522. 	}
  1523.  
  1524. 	return unit_blown(target, dx, dy, count, flag&0x1);
  1525. }
  1526.  
  1527.  
  1528. //Checks if 'bl' should reflect back a spell cast by 'src'.
  1529. //type is the type of magic attack: 0: indirect (aoe), 1: direct (targetted)
  1530. static int skill_magic_reflect(struct block_list* src, struct block_list* bl, int type)
  1531. {
  1532. 	struct status_change *sc = status_get_sc(bl);
  1533. 	struct map_session_data* sd = BL_CAST(BL_PC, bl);
  1534.  
  1535. 	// item-based reflection
  1536. 	if( sd && sd->magic_damage_return && type && rand()%100 < sd->magic_damage_return )
  1537. 		return 1;
  1538.  
  1539. 	if( is_boss(src) )
  1540. 		return 0;
  1541.  
  1542. 	// status-based reflection
  1543. 	if( !sc || sc->count == 0 )
  1544. 		return 0;
  1545.  
  1546. 	if( sc->data[SC_MAGICMIRROR] && rand()%100 < sc->data[SC_MAGICMIRROR]->val2 )
  1547. 		return 1;
  1548.  
  1549. 	if( sc->data[SC_KAITE] && (src->type == BL_PC || status_get_lv(src) <= 80) )
  1550. 	{// Kaite only works against non-players if they are low-level.
  1551. 		clif_specialeffect(bl, 438, AREA);
  1552. 		if( --sc->data[SC_KAITE]->val2 <= 0 )
  1553. 			status_change_end(bl, SC_KAITE, INVALID_TIMER);
  1554. 		return 2;
  1555. 	}
  1556.  
  1557. 	return 0;
  1558. }
  1559.  
  1560. /*
  1561.  * =========================================================================
  1562.  * Does a skill attack with the given properties.
  1563.  * src is the master behind the attack (player/mob/pet)
  1564.  * dsrc is the actual originator of the damage, can be the same as src, or a BL_SKILL
  1565.  * bl is the target to be attacked.
  1566.  * flag can hold a bunch of information:
  1567.  * flag&0xFFF is passed to the underlying battle_calc_attack for processing
  1568.  *      (usually holds number of targets, or just 1 for simple splash attacks)
  1569.  * flag&0x1000 is used to tag that this is a splash-attack (so the damage
  1570.  *      packet shouldn't display a skill animation)
  1571.  * flag&0x2000 is used to signal that the skilllv should be passed as -1 to the
  1572.  *      client (causes player characters to not scream skill name)
  1573.  *-------------------------------------------------------------------------*/
  1574. int skill_attack (int attack_type, struct block_list* src, struct block_list *dsrc, struct block_list *bl, int skillid, int skilllv, unsigned int tick, int flag)
  1575. {
  1576. 	struct Damage dmg;
  1577. 	struct status_data *sstatus, *tstatus;
  1578. 	struct status_change *sc;
  1579. 	struct map_session_data *sd, *tsd;
  1580. 	int type,damage,rdamage=0;
  1581.  
  1582. 	if(skillid > 0 && skilllv <= 0) return 0;
  1583.  
  1584. 	nullpo_ret(src);	//Source is the master behind the attack (player/mob/pet)
  1585. 	nullpo_ret(dsrc); //dsrc is the actual originator of the damage, can be the same as src, or a skill casted by src.
  1586. 	nullpo_ret(bl); //Target to be attacked.
  1587.  
  1588. 	if (src != dsrc) {
  1589. 		//When caster is not the src of attack, this is a ground skill, and as such, do the relevant target checking. [Skotlex]
  1590. 		if (!status_check_skilluse(battle_config.skill_caster_check?src:NULL, bl, skillid, 2))
  1591. 			return 0;
  1592. 	} else if ((flag&SD_ANIMATION) && skill_get_nk(skillid)&NK_SPLASH) {
  1593. 		//Note that splash attacks often only check versus the targetted mob, those around the splash area normally don't get checked for being hidden/cloaked/etc. [Skotlex]
  1594. 		if (!status_check_skilluse(src, bl, skillid, 2))
  1595. 			return 0;
  1596. 	}
  1597.  
  1598. 	sd = BL_CAST(BL_PC, src);
  1599. 	tsd = BL_CAST(BL_PC, bl);
  1600.  
  1601. 	sstatus = status_get_status_data(src);
  1602. 	tstatus = status_get_status_data(bl);
  1603. 	sc= status_get_sc(bl);
  1604. 	if (sc && !sc->count) sc = NULL; //Don't need it.
  1605.  
  1606. 	// Is this check really needed? FrostNova won't hurt you if you step right where the caster is?
  1607. 	if(skillid == WZ_FROSTNOVA && dsrc->x == bl->x && dsrc->y == bl->y)
  1608. 		return 0;
  1609. 	 //Trick Dead protects you from damage, but not from buffs and the like, hence it's placed here.
  1610. 	if (sc && sc->data[SC_TRICKDEAD] && !(sstatus->mode&MD_BOSS))
  1611. 		return 0;
  1612.  
  1613. 	dmg = battle_calc_attack(attack_type,src,bl,skillid,skilllv,flag&0xFFF);
  1614.  
  1615. 	//Skotlex: Adjusted to the new system
  1616. 	if(src->type==BL_PET)
  1617. 	{ // [Valaris]
  1618. 		struct pet_data *pd = (TBL_PET*)src;
  1619. 		if (pd->a_skill && pd->a_skill->div_ && pd->a_skill->id == skillid)
  1620. 		{
  1621. 			int element = skill_get_ele(skillid, skilllv);
  1622. 			if (skillid == -1)
  1623. 				element = sstatus->rhw.ele;
  1624. 			if (element != ELE_NEUTRAL || !(battle_config.attack_attr_none&BL_PET))
  1625. 				dmg.damage=battle_attr_fix(src, bl, skilllv, element, tstatus->def_ele, tstatus->ele_lv);
  1626. 			else
  1627. 				dmg.damage= skilllv;
  1628. 			dmg.damage2=0;
  1629. 			dmg.div_= pd->a_skill->div_;
  1630. 		}
  1631. 	}
  1632.  
  1633. 	if( dmg.flag&BF_MAGIC && ( skillid != NPC_EARTHQUAKE || (battle_config.eq_single_target_reflectable && (flag&0xFFF) == 1) ) )
  1634. 	{ // Earthquake on multiple targets is not counted as a target skill. [Inkfish]
  1635. 		if( (dmg.damage || dmg.damage2) && (type = skill_magic_reflect(src, bl, src==dsrc)) )
  1636. 		{	//Magic reflection, switch caster/target
  1637. 			struct block_list *tbl = bl;
  1638. 			bl = src;
  1639. 			src = tbl;
  1640. 			sd = BL_CAST(BL_PC, src);
  1641. 			tsd = BL_CAST(BL_PC, bl);
  1642. 			sc = status_get_sc(bl);
  1643. 			if (sc && !sc->count)
  1644. 				sc = NULL; //Don't need it.
  1645.  
  1646. 			//Spirit of Wizard blocks Kaite's reflection
  1647. 			if( type == 2 && sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_WIZARD )
  1648. 			{	//Consume one Fragment per hit of the casted skill? [Skotlex]
  1649. 			  	type = tsd?pc_search_inventory (tsd, 7321):0;
  1650. 				if (type >= 0) {
  1651. 					if ( tsd ) pc_delitem(tsd, type, 1, 0, 1);
  1652. 					dmg.damage = dmg.damage2 = 0;
  1653. 					dmg.dmg_lv = ATK_MISS;
  1654. 					sc->data[SC_SPIRIT]->val3 = skillid;
  1655. 					sc->data[SC_SPIRIT]->val4 = dsrc->id;
  1656. 				}
  1657. 			}
  1658. 		}
  1659.  
  1660. 		if(sc && sc->data[SC_MAGICROD] && src == dsrc) {
  1661. 			int sp = skill_get_sp(skillid,skilllv);
  1662. 			dmg.damage = dmg.damage2 = 0;
  1663. 			dmg.dmg_lv = ATK_MISS; //This will prevent skill additional effect from taking effect. [Skotlex]
  1664. 			sp = sp * sc->data[SC_MAGICROD]->val2 / 100;
  1665. 			if(skillid == WZ_WATERBALL && skilllv > 1)
  1666. 				sp = sp/((skilllv|1)*(skilllv|1)); //Estimate SP cost of a single water-ball
  1667. 			status_heal(bl, 0, sp, 2);
  1668. 			clif_skill_nodamage(bl,bl,SA_MAGICROD,sc->data[SC_MAGICROD]->val1,1);
  1669. 		}
  1670. 	}
  1671.  
  1672. 	damage = dmg.damage + dmg.damage2;
  1673.  
  1674. 	if( (skillid == AL_INCAGI || skillid == AL_BLESSING || 
  1675. 		skillid == CASH_BLESSING || skillid == CASH_INCAGI ||
  1676. 		skillid == MER_INCAGI || skillid == MER_BLESSING) && tsd->sc.data[SC_CHANGEUNDEAD] )
  1677. 		damage = 1;
  1678.  
  1679. 	if( damage > 0 && dmg.flag&BF_WEAPON && src != bl && ( src == dsrc || ( dsrc->type == BL_SKILL && ( skillid == SG_SUN_WARM || skillid == SG_MOON_WARM || skillid == SG_STAR_WARM ) ) )
  1680. 		&& skillid != WS_CARTTERMINATION )
  1681. 		rdamage = battle_calc_return_damage(bl, damage, dmg.flag);
  1682.  
  1683. 	//Skill hit type
  1684. 	type=(skillid==0)?5:skill_get_hit(skillid);
  1685.  
  1686. 	if(damage < dmg.div_
  1687. 		//Only skills that knockback even when they miss. [Skotlex]
  1688. 		&& skillid != CH_PALMSTRIKE)
  1689. 		dmg.blewcount = 0;
  1690.  
  1691. 	if(skillid == CR_GRANDCROSS||skillid == NPC_GRANDDARKNESS) {
  1692. 		if(battle_config.gx_disptype) dsrc = src;
  1693. 		if(src == bl) type = 4;
  1694. 		else flag|=SD_ANIMATION;
  1695. 	}
  1696. 	if(skillid == NJ_TATAMIGAESHI) {
  1697. 		dsrc = src; //For correct knockback.
  1698. 		flag|=SD_ANIMATION;
  1699. 	}
  1700.  
  1701. 	if(sd) {
  1702. 		int flag = 0; //Used to signal if this skill can be combo'ed later on.
  1703. 		struct status_change_entry *sce;
  1704. 		if ((sce = sd->sc.data[SC_COMBO]))
  1705. 		{	//End combo state after skill is invoked. [Skotlex]
  1706. 			switch (skillid) {
  1707. 			case TK_TURNKICK:
  1708. 			case TK_STORMKICK:
  1709. 			case TK_DOWNKICK:
  1710. 			case TK_COUNTER:
  1711. 				if (pc_famerank(sd->status.char_id,MAPID_TAEKWON))
  1712. 			  	{	//Extend combo time.
  1713. 					sce->val1 = skillid; //Update combo-skill
  1714. 					sce->val3 = skillid;
  1715. 					delete_timer(sce->timer, status_change_timer);
  1716. 					sce->timer = add_timer(tick+sce->val4, status_change_timer, src->id, SC_COMBO);
  1717. 					break;
  1718. 				}
  1719. 				unit_cancel_combo(src); // Cancel combo wait
  1720. 				break;
  1721. 			default:
  1722. 				if( src == dsrc ) // Ground skills are exceptions. [Inkfish]
  1723. 					status_change_end(src, SC_COMBO, INVALID_TIMER);
  1724. 			}
  1725. 		}
  1726. 		switch(skillid)
  1727. 		{
  1728. 			case MO_TRIPLEATTACK:
  1729. 				if (pc_checkskill(sd, MO_CHAINCOMBO) > 0)
  1730. 					flag=1;
  1731. 				break;
  1732. 			case MO_CHAINCOMBO:
  1733. 				if(pc_checkskill(sd, MO_COMBOFINISH) > 0 && sd->spiritball > 0)
  1734. 					flag=1;
  1735. 				break;
  1736. 			case MO_COMBOFINISH:
  1737. 				if (sd->status.party_id>0) //bonus from SG_FRIEND [Komurka]
  1738. 					party_skill_check(sd, sd->status.party_id, MO_COMBOFINISH, skilllv);
  1739. 				if (pc_checkskill(sd, CH_TIGERFIST) > 0 && sd->spiritball > 0)
  1740. 					flag=1;
  1741. 			case CH_TIGERFIST:
  1742. 				if (!flag && pc_checkskill(sd, CH_CHAINCRUSH) > 0 && sd->spiritball > 1)
  1743. 					flag=1;
  1744. 			case CH_CHAINCRUSH:
  1745. 				if (!flag && pc_checkskill(sd, MO_EXTREMITYFIST) > 0 && sd->spiritball > 0 && sd->sc.data[SC_EXPLOSIONSPIRITS])
  1746. 					flag=1;
  1747. 				break;
  1748. 			case AC_DOUBLE:
  1749. 				if( (tstatus->race == RC_BRUTE || tstatus->race == RC_INSECT) && pc_checkskill(sd, HT_POWER))
  1750. 				{
  1751. 					//TODO: This code was taken from Triple Blows, is this even how it should be? [Skotlex]
  1752. 					sc_start2(src,SC_COMBO,100,HT_POWER,bl->id,2000);
  1753. 					clif_combo_delay(src,2000);
  1754. 				}
  1755. 				break;
  1756. 			case TK_COUNTER:
  1757. 			{	//bonus from SG_FRIEND [Komurka]
  1758. 				int level;
  1759. 				if(sd->status.party_id>0 && (level = pc_checkskill(sd,SG_FRIEND)))
  1760. 					party_skill_check(sd, sd->status.party_id, TK_COUNTER,level);
  1761. 			}
  1762. 				break;
  1763. 			case SL_STIN:
  1764. 			case SL_STUN:
  1765. 				if (skilllv >= 7 && !sd->sc.data[SC_SMA])
  1766. 					sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA, skilllv));
  1767. 				break;
  1768. 			case GS_FULLBUSTER:
  1769. 				//Can't attack nor use items until skill's delay expires. [Skotlex]
  1770. 				sd->ud.attackabletime = sd->canuseitem_tick = sd->ud.canact_tick;
  1771. 				break;
  1772. 		}	//Switch End
  1773. 		if (flag) { //Possible to chain
  1774. 			flag = DIFF_TICK(sd->ud.canact_tick, tick);
  1775. 			if (flag < 1) flag = 1;
  1776. 			sc_start2(src,SC_COMBO,100,skillid,bl->id,flag);
  1777. 			clif_combo_delay(src, flag);
  1778. 		}
  1779. 	}
  1780.  
  1781. 	//Display damage.
  1782. 	switch( skillid )
  1783. 	{
  1784. 	case PA_GOSPEL: //Should look like Holy Cross [Skotlex]
  1785. 		dmg.dmotion = clif_skill_damage(dsrc,bl,tick,dmg.amotion,dmg.dmotion, damage, dmg.div_, CR_HOLYCROSS, -1, 5);
  1786. 		break;
  1787. 	//Skills that need be passed as a normal attack for the client to display correctly.
  1788. 	case HVAN_EXPLOSION:
  1789. 	case NPC_SELFDESTRUCTION:
  1790. 		if(src->type==BL_PC)
  1791. 			dmg.blewcount = 10;
  1792. 		dmg.amotion = 0; //Disable delay or attack will do no damage since source is dead by the time it takes effect. [Skotlex]
  1793. 		// fall through
  1794. 	case KN_AUTOCOUNTER:
  1795. 	case NPC_CRITICALSLASH:
  1796. 	case TF_DOUBLE:
  1797. 	case GS_CHAINACTION:
  1798. 		dmg.dmotion = clif_damage(src,bl,tick,dmg.amotion,dmg.dmotion,damage,dmg.div_,dmg.type,dmg.damage2);
  1799. 		break;
  1800.  
  1801. 	case AS_SPLASHER:
  1802. 		if( flag&SD_ANIMATION ) // the surrounding targets
  1803. 			dmg.dmotion = clif_skill_damage(dsrc,bl,tick, dmg.amotion, dmg.dmotion, damage, dmg.div_, skillid, -1, 5); // needs -1 as skill level
  1804. 		else // the central target doesn't display an animation
  1805. 			dmg.dmotion = clif_skill_damage(dsrc,bl,tick, dmg.amotion, dmg.dmotion, damage, dmg.div_, skillid, -2, 5); // needs -2(!) as skill level
  1806. 		break;
  1807.  
  1808. 	default:
  1809. 		if( flag&SD_ANIMATION && dmg.div_ < 2 ) //Disabling skill animation doesn't works on multi-hit.
  1810. 			type = 5;
  1811. 		dmg.dmotion = clif_skill_damage(dsrc,bl,tick, dmg.amotion, dmg.dmotion, damage, dmg.div_, skillid, flag&SD_LEVEL?-1:skilllv, type);
  1812. 		break;
  1813. 	}
  1814.  
  1815. 	map_freeblock_lock();
  1816.  
  1817. 	if(damage > 0 && dmg.flag&BF_SKILL && tsd
  1818. 		&& pc_checkskill(tsd,RG_PLAGIARISM)
  1819. 	  	&& (!sc || !sc->data[SC_PRESERVE])
  1820. 		&& damage < tsd->battle_status.hp)
  1821. 	{	//Updated to not be able to copy skills if the blow will kill you. [Skotlex]
  1822. 		if ((tsd->status.skill[skillid].id == 0 || tsd->status.skill[skillid].flag == SKILL_FLAG_PLAGIARIZED) &&
  1823. 			can_copy(tsd,skillid,bl))	// Split all the check into their own function [Aru]
  1824. 		{
  1825. 			int lv = skilllv;
  1826. 			if (tsd->cloneskill_id && tsd->status.skill[tsd->cloneskill_id].flag == SKILL_FLAG_PLAGIARIZED){
  1827. 				tsd->status.skill[tsd->cloneskill_id].id = 0;
  1828. 				tsd->status.skill[tsd->cloneskill_id].lv = 0;
  1829. 				tsd->status.skill[tsd->cloneskill_id].flag = 0;
  1830. 				clif_deleteskill(tsd,tsd->cloneskill_id);
  1831. 			}
  1832.  
  1833. 			if ((type = pc_checkskill(tsd,RG_PLAGIARISM)) < lv)
  1834. 				lv = type;
  1835.  
  1836. 			tsd->cloneskill_id = skillid;
  1837. 			pc_setglobalreg(tsd, "CLONE_SKILL", skillid);
  1838. 			pc_setglobalreg(tsd, "CLONE_SKILL_LV", lv);
  1839.  
  1840. 			tsd->status.skill[skillid].id = skillid;
  1841. 			tsd->status.skill[skillid].lv = lv;
  1842. 			tsd->status.skill[skillid].flag = SKILL_FLAG_PLAGIARIZED;
  1843. 			clif_addskill(tsd,skillid);
  1844. 		}
  1845. 	}
  1846. 	if( skillid != WZ_SIGHTRASHER &&
  1847. 		skillid != WZ_SIGHTBLASTER &&
  1848. 		skillid != AC_SHOWER && skillid != MA_SHOWER &&
  1849. 		skillid != SM_MAGNUM && skillid != MS_MAGNUM &&
  1850. 		bl->type == BL_SKILL && damage > 0 )
  1851. 	{
  1852. 		struct skill_unit* su = (struct skill_unit*)bl;
  1853. 		if (su->group && skill_get_inf2(su->group->skill_id)&INF2_TRAP)
  1854. 			damage = 0; //Sight rasher, blaster, and arrow shower may dmg traps. [Kevin]
  1855. 	}
  1856.  
  1857. 	if (dmg.dmg_lv >= ATK_MISS && (type = skill_get_walkdelay(skillid, skilllv)) > 0)
  1858. 	{	//Skills with can't walk delay also stop normal attacking for that
  1859. 		//duration when the attack connects. [Skotlex]
  1860. 		struct unit_data *ud = unit_bl2ud(src);
  1861. 		if (ud && DIFF_TICK(ud->attackabletime, tick + type) < 0)
  1862. 			ud->attackabletime = tick + type;
  1863. 	}
  1864.  
  1865. 	if( !dmg.amotion )
  1866. 	{ //Instant damage
  1867. 		if( !sc || !sc->data[SC_DEVOTION] )
  1868. 			status_fix_damage(src,bl,damage,dmg.dmotion); //Deal damage before knockback to allow stuff like firewall+storm gust combo.
  1869. 		if( !status_isdead(bl) )
  1870. 			skill_additional_effect(src,bl,skillid,skilllv,dmg.flag,dmg.dmg_lv,tick);
  1871. 		if( damage > 0 ) //Counter status effects [Skotlex]
  1872. 			skill_counter_additional_effect(src,bl,skillid,skilllv,dmg.flag,tick);
  1873. 	}
  1874.  
  1875. 	//Only knockback if it's still alive, otherwise a "ghost" is left behind. [Skotlex]
  1876. 	//Reflected spells do not bounce back (bl == dsrc since it only happens for direct skills)
  1877. 	if (dmg.blewcount > 0 && bl!=dsrc && !status_isdead(bl))
  1878. 	{
  1879. 		int direction = -1; // default
  1880. 		switch(skillid)
  1881. 		{
  1882. 			case MG_FIREWALL:  direction = unit_getdir(bl); break; // backwards
  1883. 			case WZ_STORMGUST: direction = rand()%8;        break; // randomly
  1884. 			case PR_SANCTUARY: direction = unit_getdir(bl); break; // backwards
  1885. 		}
  1886. 		skill_blown(dsrc,bl,dmg.blewcount,direction,0);
  1887. 	}
  1888.  
  1889. 	//Delayed damage must be dealt after the knockback (it needs to know actual position of target)
  1890. 	if (dmg.amotion)
  1891. 		battle_delay_damage(tick, dmg.amotion,src,bl,dmg.flag,skillid,skilllv,damage,dmg.dmg_lv,dmg.dmotion);
  1892.  
  1893. 	if( sc && sc->data[SC_DEVOTION] && skillid != PA_PRESSURE )
  1894. 	{
  1895. 		struct status_change_entry *sce = sc->data[SC_DEVOTION];
  1896. 		struct block_list *d_bl = map_id2bl(sce->val1);
  1897.  
  1898. 		if( d_bl && (
  1899. 			(d_bl->type == BL_MER && ((TBL_MER*)d_bl)->master && ((TBL_MER*)d_bl)->master->bl.id == bl->id) ||
  1900. 			(d_bl->type == BL_PC && ((TBL_PC*)d_bl)->devotion[sce->val2] == bl->id)
  1901. 			) && check_distance_bl(bl, d_bl, sce->val3) )
  1902. 		{
  1903. 			clif_damage(d_bl, d_bl, gettick(), 0, 0, damage, 0, 0, 0);
  1904. 			status_fix_damage(NULL, d_bl, damage, 0);
  1905. 		}
  1906. 		else
  1907. 			status_change_end(bl, SC_DEVOTION, INVALID_TIMER);
  1908. 	}
  1909.  
  1910. 	if(skillid == RG_INTIMIDATE && damage > 0 && !(tstatus->mode&MD_BOSS)) {
  1911. 		int rate = 50 + skilllv * 5;
  1912. 		rate = rate + (status_get_lv(src) - status_get_lv(bl));
  1913. 		if(rand()%100 < rate)
  1914. 			skill_addtimerskill(src,tick + 800,bl->id,0,0,skillid,skilllv,0,flag);
  1915. 	}
  1916.  
  1917. 	if(skillid == CR_GRANDCROSS || skillid == NPC_GRANDDARKNESS)
  1918. 		dmg.flag |= BF_WEAPON;
  1919.  
  1920. 	if( sd && dmg.flag&BF_WEAPON && src != bl && ( src == dsrc || ( dsrc->type == BL_SKILL && ( skillid == SG_SUN_WARM || skillid == SG_MOON_WARM || skillid == SG_STAR_WARM ) ) )  && damage > 0 )
  1921. 	{
  1922. 		if (battle_config.left_cardfix_to_right)
  1923. 			battle_drain(sd, bl, dmg.damage, dmg.damage, tstatus->race, tstatus->mode&MD_BOSS);
  1924. 		else
  1925. 			battle_drain(sd, bl, dmg.damage, dmg.damage2, tstatus->race, tstatus->mode&MD_BOSS);
  1926. 	}
  1927.  
  1928. 	if( rdamage > 0 )
  1929. 	{
  1930. 		if( dmg.amotion )
  1931. 			battle_delay_damage(tick, dmg.amotion,bl,src,0,0,0,rdamage,ATK_DEF,0);
  1932. 		else
  1933. 			status_fix_damage(bl,src,rdamage,0);
  1934. 		clif_damage(src,src,tick, dmg.amotion,0,rdamage,dmg.div_>1?dmg.div_:1,4,0);
  1935. 		//Use Reflect Shield to signal this kind of skill trigger. [Skotlex]
  1936. 		if( tsd && src != bl )
  1937. 			battle_drain(tsd, src, rdamage, rdamage, sstatus->race, is_boss(src));
  1938. 		skill_additional_effect(bl, src, CR_REFLECTSHIELD, 1, BF_WEAPON|BF_SHORT|BF_NORMAL,ATK_DEF,tick);
  1939. 	}
  1940.  
  1941. 	if (!(flag&2) &&
  1942. 		(
  1943. 			skillid == MG_COLDBOLT || skillid == MG_FIREBOLT || skillid == MG_LIGHTNINGBOLT
  1944. 		) &&
  1945. 		(sc = status_get_sc(src)) &&
  1946. 		sc->data[SC_DOUBLECAST] &&
  1947. 		rand() % 100 < sc->data[SC_DOUBLECAST]->val2)
  1948. 	{
  1949. //		skill_addtimerskill(src, tick + dmg.div_*dmg.amotion, bl->id, 0, 0, skillid, skilllv, BF_MAGIC, flag|2);
  1950. 		skill_addtimerskill(src, tick + dmg.amotion, bl->id, 0, 0, skillid, skilllv, BF_MAGIC, flag|2);
  1951. 	}
  1952.  
  1953. 	map_freeblock_unlock();
  1954.  
  1955. 	return damage;
  1956. }
  1957.  
  1958. /*==========================================
  1959.  * ƒXƒLƒ‹”Í??U?—p(map_foreachinarea‚©‚çŒÄ‚΂ê‚é)
  1960.  * flag‚ɂ‚¢‚Ä?F16?i?‚ðŠm”F
  1961.  * MSB <- 00fTffff ->LSB
  1962.  *	T	=ƒ^?ƒQƒbƒg‘I?—p(BCT_*)
  1963.  *  ffff=Ž©—R‚ÉŽg—p‰Â”\
  1964.  *  0	=—\–ñ?B0‚ɌŒè
  1965.  *------------------------------------------*/
  1966. static int skill_area_temp[8];
  1967. typedef int (*SkillFunc)(struct block_list *, struct block_list *, int, int, unsigned int, int);
  1968. int skill_area_sub (struct block_list *bl, va_list ap)
  1969. {
  1970. 	struct block_list *src;
  1971. 	int skill_id,skill_lv,flag;
  1972. 	unsigned int tick;
  1973. 	SkillFunc func;
  1974.  
  1975. 	nullpo_ret(bl);
  1976.  
  1977. 	src=va_arg(ap,struct block_list *);
  1978. 	skill_id=va_arg(ap,int);
  1979. 	skill_lv=va_arg(ap,int);
  1980. 	tick=va_arg(ap,unsigned int);
  1981. 	flag=va_arg(ap,int);
  1982. 	func=va_arg(ap,SkillFunc);
  1983.  
  1984. 	if(battle_check_target(src,bl,flag) > 0)
  1985. 	{
  1986. 		// several splash skills need this initial dummy packet to display correctly
  1987. 		if (flag&SD_PREAMBLE && skill_area_temp[2] == 0)
  1988. 			clif_skill_damage(src,bl,tick, status_get_amotion(src), 0, -30000, 1, skill_id, skill_lv, 6);
  1989.  
  1990. 		if (flag&(SD_SPLASH|SD_PREAMBLE))
  1991. 			skill_area_temp[2]++;
  1992.  
  1993. 		return func(src,bl,skill_id,skill_lv,tick,flag);
  1994. 	}
  1995. 	return 0;
  1996. }
  1997.  
  1998. static int skill_check_unit_range_sub (struct block_list *bl, va_list ap)
  1999. {
  2000. 	struct skill_unit *unit;
  2001. 	int skillid,g_skillid;
  2002.  
  2003. 	unit = (struct skill_unit *)bl;
  2004.  
  2005. 	if(bl->prev == NULL || bl->type != BL_SKILL)
  2006. 		return 0;
  2007.  
  2008. 	if(!unit->alive)
  2009. 		return 0;
  2010.  
  2011. 	skillid = va_arg(ap,int);
  2012. 	g_skillid = unit->group->skill_id;
  2013.  
  2014. 	switch (skillid)
  2015. 	{
  2016. 		case MG_SAFETYWALL:
  2017. 		case AL_PNEUMA:
  2018. 			if(g_skillid != MG_SAFETYWALL && g_skillid != AL_PNEUMA)
  2019. 				return 0;
  2020. 			break;
  2021. 		case AL_WARP:
  2022. 		case HT_SKIDTRAP:
  2023. 		case MA_SKIDTRAP:
  2024. 		case HT_LANDMINE:
  2025. 		case MA_LANDMINE:
  2026. 		case HT_ANKLESNARE:
  2027. 		case HT_SHOCKWAVE:
  2028. 		case HT_SANDMAN:
  2029. 		case MA_SANDMAN:
  2030. 		case HT_FLASHER:
  2031. 		case HT_FREEZINGTRAP:
  2032. 		case MA_FREEZINGTRAP:
  2033. 		case HT_BLASTMINE:
  2034. 		case HT_CLAYMORETRAP:
  2035. 		case HT_TALKIEBOX:
  2036. 		case HP_BASILICA:
  2037. 			//Non stackable on themselves and traps (including venom dust which does not has the trap inf2 set)
  2038. 			if (skillid != g_skillid && !(skill_get_inf2(g_skillid)&INF2_TRAP) && g_skillid != AS_VENOMDUST)
  2039. 				return 0;
  2040. 			break;
  2041. 		default: //Avoid stacking with same kind of trap. [Skotlex]
  2042. 			if (g_skillid != skillid)
  2043. 				return 0;
  2044. 			break;
  2045. 	}
  2046.  
  2047. 	return 1;
  2048. }
  2049.  
  2050. static int skill_check_unit_range (struct block_list *bl, int x, int y, int skillid, int skilllv)
  2051. {
  2052. 	//Non players do not check for the skill's splash-trigger area.
  2053. 	int range = bl->type==BL_PC?skill_get_unit_range(skillid, skilllv):0;
  2054. 	int layout_type = skill_get_unit_layout_type(skillid,skilllv);
  2055. 	if (layout_type==-1 || layout_type>MAX_SQUARE_LAYOUT) {
  2056. 		ShowError("skill_check_unit_range: unsupported layout type %d for skill %d\n",layout_type,skillid);
  2057. 		return 0;
  2058. 	}
  2059.  
  2060. 	range += layout_type;
  2061. 	return map_foreachinarea(skill_check_unit_range_sub,bl->m,x-range,y-range,x+range,y+range,BL_SKILL,skillid);
  2062. }
  2063.  
  2064. static int skill_check_unit_range2_sub (struct block_list *bl, va_list ap)
  2065. {
  2066. 	int skillid;
  2067.  
  2068. 	if(bl->prev == NULL)
  2069. 		return 0;
  2070.  
  2071. 	skillid = va_arg(ap,int);
  2072.  
  2073. 	if( status_isdead(bl) && skillid != AL_WARP )
  2074. 		return 0;
  2075.  
  2076. 	if( skillid == HP_BASILICA && bl->type == BL_PC )
  2077. 		return 0;
  2078.  
  2079. 	if( skillid == AM_DEMONSTRATION && bl->type == BL_MOB && ((TBL_MOB*)bl)->class_ == MOBID_EMPERIUM )
  2080. 		return 0; //Allow casting Bomb/Demonstration Right under emperium [Skotlex]
  2081. 	return 1;
  2082. }
  2083.  
  2084. static int skill_check_unit_range2 (struct block_list *bl, int x, int y, int skillid, int skilllv)
  2085. {
  2086. 	int range, type;
  2087.  
  2088. 	switch (skillid) {	// to be expanded later
  2089. 	case WZ_ICEWALL:
  2090. 		range = 2;
  2091. 		break;
  2092. 	default:
  2093. 		{
  2094. 			int layout_type = skill_get_unit_layout_type(skillid,skilllv);
  2095. 			if (layout_type==-1 || layout_type>MAX_SQUARE_LAYOUT) {
  2096. 				ShowError("skill_check_unit_range2: unsupported layout type %d for skill %d\n",layout_type,skillid);
  2097. 				return 0;
  2098. 			}
  2099. 			range = skill_get_unit_range(skillid,skilllv) + layout_type;
  2100. 		}
  2101. 		break;
  2102. 	}
  2103.  
  2104. 	// if the caster is a monster/NPC, only check for players
  2105. 	// otherwise just check characters
  2106. 	if (bl->type == BL_PC)
  2107. 		type = BL_CHAR;
  2108. 	else
  2109. 		type = BL_PC;
  2110.  
  2111. 	return map_foreachinarea(skill_check_unit_range2_sub, bl->m,
  2112. 		x - range, y - range, x + range, y + range,
  2113. 		type, skillid);
  2114. }
  2115.  
  2116. int skill_guildaura_sub (struct map_session_data* sd, int id, int strvit, int agidex)
  2117. {
  2118. 	if(id == sd->bl.id && battle_config.guild_aura&16)
  2119. 		return 0;  // Do not affect guild leader
  2120.  
  2121. 	if (sd->sc.data[SC_GUILDAURA]) {
  2122. 		struct status_change_entry *sce = sd->sc.data[SC_GUILDAURA];
  2123. 		if (sce->val3 != strvit || sce->val4 != agidex) {
  2124. 			sce->val3 = strvit;
  2125. 			sce->val4 = agidex;
  2126. 			status_calc_bl(&sd->bl, StatusChangeFlagTable[SC_GUILDAURA]);
  2127. 		}
  2128. 		return 0;
  2129. 	}
  2130. 	sc_start4(&sd->bl, SC_GUILDAURA,100, 1, id, strvit, agidex, 1000);
  2131. 	return 1;
  2132. }
  2133.  
  2134. /*==========================================
  2135.  * Checks that you have the requirements for casting a skill for homunculus/mercenary.
  2136.  * Flag:
  2137.  * &1: finished casting the skill (invoke hp/sp/item consumption)
  2138.  * &2: picked menu entry (Warp Portal, Teleport and other menu based skills)
  2139.  *------------------------------------------*/
  2140. static int skill_check_condition_mercenary(struct block_list *bl, int skill, int lv, int type)
  2141. {
  2142. 	struct status_data *status;
  2143. 	struct map_session_data *sd = NULL;
  2144. 	int i, j, hp, sp, hp_rate, sp_rate, state, mhp;
  2145. 	int itemid[MAX_SKILL_ITEM_REQUIRE],amount[ARRAYLENGTH(itemid)],index[ARRAYLENGTH(itemid)];
  2146.  
  2147. 	if( lv < 1 || lv > MAX_SKILL_LEVEL )
  2148. 		return 0;
  2149. 	nullpo_ret(bl);
  2150.  
  2151. 	switch( bl->type )
  2152. 	{
  2153. 		case BL_HOM: sd = ((TBL_HOM*)bl)->master; break;
  2154. 		case BL_MER: sd = ((TBL_MER*)bl)->master; break;
  2155. 	}
  2156.  
  2157. 	status = status_get_status_data(bl);
  2158. 	if( (j = skill_get_index(skill)) == 0 )
  2159. 		return 0;
  2160.  
  2161. 	// Requeriments
  2162. 	for( i = 0; i < ARRAYLENGTH(itemid); i++ )
  2163. 	{
  2164. 		itemid[i] = skill_db[j].itemid[i];
  2165. 		amount[i] = skill_db[j].amount[i];
  2166. 	}
  2167. 	hp = skill_db[j].hp[lv-1];
  2168. 	sp = skill_db[j].sp[lv-1];
  2169. 	hp_rate = skill_db[j].hp_rate[lv-1];
  2170. 	sp_rate = skill_db[j].sp_rate[lv-1];
  2171. 	state = skill_db[j].state;
  2172. 	if( (mhp = skill_db[j].mhp[lv-1]) > 0 )
  2173. 		hp += (status->max_hp * mhp) / 100;
  2174. 	if( hp_rate > 0 )
  2175. 		hp += (status->hp * hp_rate) / 100;
  2176. 	else
  2177. 		hp += (status->max_hp * (-hp_rate)) / 100;
  2178. 	if( sp_rate > 0 )
  2179. 		sp += (status->sp * sp_rate) / 100;
  2180. 	else
  2181. 		sp += (status->max_sp * (-sp_rate)) / 100;
  2182.  
  2183. 	if( bl->type == BL_HOM )
  2184. 	{ // Intimacy Requeriments
  2185. 		struct homun_data *hd = BL_CAST(BL_HOM, bl);
  2186. 		switch( skill )
  2187. 		{
  2188. 			case HFLI_SBR44:
  2189. 				if( hd->homunculus.intimacy <= 200 )
  2190. 					return 0;
  2191. 				break;
  2192. 			case HVAN_EXPLOSION:
  2193. 				if( hd->homunculus.intimacy < (unsigned int)battle_config.hvan_explosion_intimate )
  2194. 					return 0;
  2195. 				break;
  2196. 		}
  2197. 	}
  2198.  
  2199. 	if( !(type&2) )
  2200. 	{
  2201. 		if( hp > 0 && status->hp <= (unsigned int)hp )
  2202. 		{
  2203. 			clif_skill_fail(sd, skill, USESKILL_FAIL_HP_INSUFFICIENT, 0);
  2204. 			return 0;
  2205. 		}
  2206. 		if( sp > 0 && status->sp <= (unsigned int)sp )
  2207. 		{
  2208. 			clif_skill_fail(sd, skill, USESKILL_FAIL_SP_INSUFFICIENT, 0);
  2209. 			return 0;
  2210. 		}
  2211. 	}
  2212.  
  2213. 	if( !type )
  2214. 		switch( state )
  2215. 		{
  2216. 			case ST_MOVE_ENABLE:
  2217. 				if( !unit_can_move(bl) )
  2218. 				{
  2219. 					clif_skill_fail(sd, skill, USESKILL_FAIL_LEVEL, 0);
  2220. 					return 0;
  2221. 				}
  2222. 				break;
  2223. 		}
  2224. 	if( !(type&1) )
  2225. 		return 1;
  2226.  
  2227. 	// Check item existences
  2228. 	for( i = 0; i < ARRAYLENGTH(itemid); i++ )
  2229. 	{
  2230. 		index[i] = -1;
  2231. 		if( itemid[i] < 1 ) continue; // No item
  2232. 		index[i] = pc_search_inventory(sd, itemid[i]);
  2233. 		if( index[i] < 0 || sd->status.inventory[index[i]].amount < amount[i] )
  2234. 		{
  2235. 			clif_skill_fail(sd, skill, USESKILL_FAIL_LEVEL, 0);
  2236. 			return 0;
  2237. 		}
  2238. 	}
  2239.  
  2240. 	// Consume items
  2241. 	for( i = 0; i < ARRAYLENGTH(itemid); i++ )
  2242. 	{
  2243. 		if( index[i] >= 0 ) pc_delitem(sd, index[i], amount[i], 0, 1);
  2244. 	}
  2245.  
  2246. 	if( type&2 )
  2247. 		return 1;
  2248.  
  2249. 	if( sp || hp )
  2250. 		status_zap(bl, hp, sp);
  2251.  
  2252. 	return 1;
  2253. }
  2254.  
  2255. /*==========================================
  2256.  *
  2257.  *------------------------------------------*/
  2258. int skill_area_sub_count (struct block_list *src, struct block_list *target, int skillid, int skilllv, unsigned int tick, int flag)
  2259. {
  2260. 	return 1;
  2261. }
  2262.  
  2263. /*==========================================
  2264.  *
  2265.  *------------------------------------------*/
  2266. static int skill_timerskill(int tid, unsigned int tick, int id, intptr_t data)
  2267. {
  2268. 	struct block_list *src = map_id2bl(id),*target;
  2269. 	struct unit_data *ud = unit_bl2ud(src);
  2270. 	struct skill_timerskill *skl = NULL;
  2271. 	int range;
  2272.  
  2273. 	nullpo_ret(src);
  2274. 	nullpo_ret(ud);
  2275. 	skl = ud->skilltimerskill[data];
  2276. 	nullpo_ret(skl);
  2277. 	ud->skilltimerskill[data] = NULL;
  2278.  
  2279. 	do {
  2280. 		if(src->prev == NULL)
  2281. 			break;
  2282. 		if(skl->target_id) {
  2283. 			target = map_id2bl(skl->target_id);
  2284. 			if( skl->skill_id == RG_INTIMIDATE && (!target || target->prev == NULL || !check_distance_bl(src,target,AREA_SIZE)) )
  2285. 				target = src; //Required since it has to warp.
  2286. 			if(target == NULL)
  2287. 				break;
  2288. 			if(target->prev == NULL)
  2289. 				break;
  2290. 			if(src->m != target->m)
  2291. 				break;
  2292. 			if(status_isdead(src))
  2293. 				break;
  2294. 			if(status_isdead(target) && skl->skill_id != RG_INTIMIDATE && skl->skill_id != WZ_WATERBALL)
  2295. 				break;
  2296.  
  2297. 			switch(skl->skill_id) {
  2298. 				case RG_INTIMIDATE:
  2299. 					if (unit_warp(src,-1,-1,-1,CLR_TELEPORT) == 0) {
  2300. 						short x,y;
  2301. 						map_search_freecell(src, 0, &x, &y, 1, 1, 0);
  2302. 						if (target != src && !status_isdead(target))
  2303. 							unit_warp(target, -1, x, y, CLR_TELEPORT);
  2304. 					}
  2305. 					break;
  2306. 				case BA_FROSTJOKER:
  2307. 				case DC_SCREAM:
  2308. 					range= skill_get_splash(skl->skill_id, skl->skill_lv);
  2309. 					map_foreachinarea(skill_frostjoke_scream,skl->map,skl->x-range,skl->y-range,
  2310. 						skl->x+range,skl->y+range,BL_CHAR,src,skl->skill_id,skl->skill_lv,tick);
  2311. 					break;
  2312. 				case NPC_EARTHQUAKE:
  2313. 					skill_area_temp[0] = map_foreachinrange(skill_area_sub, src, skill_get_splash(skl->skill_id, skl->skill_lv), BL_CHAR, src, skl->skill_id, skl->skill_lv, tick, BCT_ENEMY, skill_area_sub_count);
  2314. 					skill_area_temp[1] = src->id;
  2315. 					skill_area_temp[2] = 0;
  2316. 					map_foreachinrange(skill_area_sub, src, skill_get_splash(skl->skill_id, skl->skill_lv), splash_target(src), src, skl->skill_id, skl->skill_lv, tick, skl->flag, skill_castend_damage_id);
  2317. 					if( skl->type > 1 )
  2318. 						skill_addtimerskill(src,tick+250,src->id,0,0,skl->skill_id,skl->skill_lv,skl->type-1,skl->flag);
  2319. 					break;
  2320. 				case WZ_WATERBALL:
  2321. 					if (!status_isdead(target))
  2322. 						skill_attack(BF_MAGIC,src,src,target,skl->skill_id,skl->skill_lv,tick,skl->flag);
  2323. 					if (skl->type>1 && !status_isdead(target)) {
  2324. 						skill_addtimerskill(src,tick+125,target->id,0,0,skl->skill_id,skl->skill_lv,skl->type-1,skl->flag);
  2325. 					} else {
  2326. 						struct status_change *sc = status_get_sc(src);
  2327. 						if(sc) {
  2328. 							status_change_end(src, SC_MAGICPOWER, INVALID_TIMER);
  2329. 							if(sc->data[SC_SPIRIT] &&
  2330. 								sc->data[SC_SPIRIT]->val2 == SL_WIZARD &&
  2331. 								sc->data[SC_SPIRIT]->val3 == skl->skill_id)
  2332. 								sc->data[SC_SPIRIT]->val3 = 0; //Clear bounced spell check.
  2333. 						}
  2334. 					}
  2335. 					break;
  2336. 				default:
  2337. 					skill_attack(skl->type,src,src,target,skl->skill_id,skl->skill_lv,tick,skl->flag);
  2338. 					break;
  2339. 			}
  2340. 		}
  2341. 		else {
  2342. 			if(src->m != skl->map)
  2343. 				break;
  2344. 			switch( skl->skill_id )
  2345. 			{
  2346. 				case WZ_METEOR:
  2347. 					if( skl->type >= 0 )
  2348. 					{
  2349. 						int x = skl->type>>16, y = skl->type&0xFFFF;
  2350. 						if( path_search_long(NULL, src->m, src->x, src->y, x, y, CELL_CHKWALL) )
  2351. 							skill_unitsetting(src,skl->skill_id,skl->skill_lv,x,y,skl->flag);
  2352. 						if( path_search_long(NULL, src->m, src->x, src->y, skl->x, skl->y, CELL_CHKWALL) )
  2353. 							clif_skill_poseffect(src,skl->skill_id,skl->skill_lv,skl->x,skl->y,tick);
  2354. 					}
  2355. 					else if( path_search_long(NULL, src->m, src->x, src->y, skl->x, skl->y, CELL_CHKWALL) )
  2356. 						skill_unitsetting(src,skl->skill_id,skl->skill_lv,skl->x,skl->y,skl->flag);
  2357. 					break;
  2358. 			}
  2359. 		}
  2360. 	} while (0);
  2361. 	//Free skl now that it is no longer needed.
  2362. 	ers_free(skill_timer_ers, skl);
  2363. 	return 0;
  2364. }
  2365.  
  2366. /*==========================================
  2367.  *
  2368.  *------------------------------------------*/
  2369. int skill_addtimerskill (struct block_list *src, unsigned int tick, int target, int x,int y, int skill_id, int skill_lv, int type, int flag)
  2370. {
  2371. 	int i;
  2372. 	struct unit_data *ud;
  2373. 	nullpo_retr(1, src);
  2374. 	ud = unit_bl2ud(src);
  2375. 	nullpo_retr(1, ud);
  2376.  
  2377. 	ARR_FIND( 0, MAX_SKILLTIMERSKILL, i, ud->skilltimerskill[i] == 0 );
  2378. 	if( i == MAX_SKILLTIMERSKILL ) return 1;
  2379.  
  2380. 	ud->skilltimerskill[i] = ers_alloc(skill_timer_ers, struct skill_timerskill);
  2381. 	ud->skilltimerskill[i]->timer = add_timer(tick, skill_timerskill, src->id, i);
  2382. 	ud->skilltimerskill[i]->src_id = src->id;
  2383. 	ud->skilltimerskill[i]->target_id = target;
  2384. 	ud->skilltimerskill[i]->skill_id = skill_id;
  2385. 	ud->skilltimerskill[i]->skill_lv = skill_lv;
  2386. 	ud->skilltimerskill[i]->map = src->m;
  2387. 	ud->skilltimerskill[i]->x = x;
  2388. 	ud->skilltimerskill[i]->y = y;
  2389. 	ud->skilltimerskill[i]->type = type;
  2390. 	ud->skilltimerskill[i]->flag = flag;
  2391. 	return 0;
  2392. }
  2393.  
  2394. /*==========================================
  2395.  *
  2396.  *------------------------------------------*/
  2397. int skill_cleartimerskill (struct block_list *src)
  2398. {
  2399. 	int i;
  2400. 	struct unit_data *ud;
  2401. 	nullpo_ret(src);
  2402. 	ud = unit_bl2ud(src);
  2403. 	nullpo_ret(ud);
  2404.  
  2405. 	for(i=0;i<MAX_SKILLTIMERSKILL;i++) {
  2406. 		if(ud->skilltimerskill[i]) {
  2407. 			delete_timer(ud->skilltimerskill[i]->timer, skill_timerskill);
  2408. 			ers_free(skill_timer_ers, ud->skilltimerskill[i]);
  2409. 			ud->skilltimerskill[i]=NULL;
  2410. 		}
  2411. 	}
  2412. 	return 1;
  2413. }
  2414.  
  2415. static int skill_reveal_trap (struct block_list *bl, va_list ap)
  2416. {
  2417. 	TBL_SKILL *su = (TBL_SKILL*)bl;
  2418. 	if (su->alive && su->group && skill_get_inf2(su->group->skill_id)&INF2_TRAP)
  2419. 	{	//Reveal trap.
  2420. 		//Change look is not good enough, the client ignores it as an actual trap still. [Skotlex]
  2421. 		//clif_changetraplook(bl, su->group->unit_id);
  2422. 		clif_skill_setunit(su);
  2423. 		return 1;
  2424. 	}
  2425. 	return 0;
  2426. }
  2427.  
  2428. /*==========================================
  2429.  *
  2430.  *
  2431.  *------------------------------------------*/
  2432. int skill_castend_damage_id (struct block_list* src, struct block_list *bl, int skillid, int skilllv, unsigned int tick, int flag)
  2433. {
  2434. 	struct map_session_data *sd = NULL;
  2435. 	struct status_data *tstatus;
  2436. 	struct status_change *sc;
  2437.  
  2438. 	if (skillid > 0 && skilllv <= 0) return 0;
  2439.  
  2440. 	nullpo_retr(1, src);
  2441. 	nullpo_retr(1, bl);
  2442.  
  2443. 	if (src->m != bl->m)
  2444. 		return 1;
  2445.  
  2446. 	if (bl->prev == NULL)
  2447. 		return 1;
  2448.  
  2449. 	sd = BL_CAST(BL_PC, src);
  2450.  
  2451. 	if (status_isdead(bl))
  2452. 		return 1;
  2453.  
  2454. 	if (skillid && skill_get_type(skillid) == BF_MAGIC && status_isimmune(bl) == 100)
  2455. 	{	//GTB makes all targetted magic display miss with a single bolt.
  2456. 		sc_type sct = status_skill2sc(skillid);
  2457. 		if(sct != SC_NONE)
  2458. 			status_change_end(bl, sct, INVALID_TIMER);
  2459. 		clif_skill_damage(src, bl, tick, status_get_amotion(src), status_get_dmotion(bl), 0, 1, skillid, skilllv, skill_get_hit(skillid));
  2460. 		return 1;
  2461. 	}
  2462.  
  2463. 	sc = status_get_sc(src);
  2464. 	if (sc && !sc->count)
  2465. 		sc = NULL; //Unneeded
  2466.  
  2467. 	tstatus = status_get_status_data(bl);
  2468.  
  2469. 	map_freeblock_lock();
  2470.  
  2471. 	switch(skillid)
  2472. 	{
  2473. 	case MER_CRASH:
  2474. 	case SM_BASH:
  2475. 	case MS_BASH:
  2476. 	case MC_MAMMONITE:
  2477. 	case TF_DOUBLE:
  2478. 	case AC_DOUBLE:
  2479. 	case MA_DOUBLE:
  2480. 	case AS_SONICBLOW:
  2481. 	case KN_PIERCE:
  2482. 	case ML_PIERCE:
  2483. 	case KN_SPEARBOOMERANG:
  2484. 	case TF_POISON:
  2485. 	case TF_SPRINKLESAND:
  2486. 	case AC_CHARGEARROW:
  2487. 	case MA_CHARGEARROW:
  2488. 	case RG_INTIMIDATE:
  2489. 	case AM_ACIDTERROR:
  2490. 	case BA_MUSICALSTRIKE:
  2491. 	case DC_THROWARROW:
  2492. 	case BA_DISSONANCE:
  2493. 	case CR_HOLYCROSS:
  2494. 	case NPC_DARKCROSS:
  2495. 	case CR_SHIELDCHARGE:
  2496. 	case CR_SHIELDBOOMERANG:
  2497. 	case NPC_PIERCINGATT:
  2498. 	case NPC_MENTALBREAKER:
  2499. 	case NPC_RANGEATTACK:
  2500. 	case NPC_CRITICALSLASH:
  2501. 	case NPC_COMBOATTACK:
  2502. 	case NPC_GUIDEDATTACK:
  2503. 	case NPC_POISON:
  2504. 	case NPC_RANDOMATTACK:
  2505. 	case NPC_WATERATTACK:
  2506. 	case NPC_GROUNDATTACK:
  2507. 	case NPC_FIREATTACK:
  2508. 	case NPC_WINDATTACK:
  2509. 	case NPC_POISONATTACK:
  2510. 	case NPC_HOLYATTACK:
  2511. 	case NPC_DARKNESSATTACK:
  2512. 	case NPC_TELEKINESISATTACK:
  2513. 	case NPC_UNDEADATTACK:
  2514. 	case NPC_ARMORBRAKE:
  2515. 	case NPC_WEAPONBRAKER:
  2516. 	case NPC_HELMBRAKE:
  2517. 	case NPC_SHIELDBRAKE:
  2518. 	case NPC_BLINDATTACK:
  2519. 	case NPC_SILENCEATTACK:
  2520. 	case NPC_STUNATTACK:
  2521. 	case NPC_PETRIFYATTACK:
  2522. 	case NPC_CURSEATTACK:
  2523. 	case NPC_SLEEPATTACK:
  2524. 	case LK_AURABLADE:
  2525. 	case LK_SPIRALPIERCE:
  2526. 	case ML_SPIRALPIERCE:
  2527. 	case LK_HEADCRUSH:
  2528. 	case CG_ARROWVULCAN:
  2529. 	case HW_MAGICCRASHER:
  2530. 	case ITM_TOMAHAWK:
  2531. 	case MO_TRIPLEATTACK:
  2532. 	case CH_CHAINCRUSH:
  2533. 	case CH_TIGERFIST:
  2534. 	case PA_SHIELDCHAIN:	// Shield Chain
  2535. 	case PA_SACRIFICE:
  2536. 	case WS_CARTTERMINATION:	// Cart Termination
  2537. 	case AS_VENOMKNIFE:
  2538. 	case HT_PHANTASMIC:
  2539. 	case HT_POWER:
  2540. 	case TK_DOWNKICK:
  2541. 	case TK_COUNTER:
  2542. 	case GS_CHAINACTION:
  2543. 	case GS_TRIPLEACTION:
  2544. 	case GS_MAGICALBULLET:
  2545. 	case GS_TRACKING:
  2546. 	case GS_PIERCINGSHOT:
  2547. 	case GS_RAPIDSHOWER:
  2548. 	case GS_DUST:
  2549. 	case GS_DISARM:				// Added disarm. [Reddozen]
  2550. 	case GS_FULLBUSTER:
  2551. 	case NJ_SYURIKEN:
  2552. 	case NJ_KUNAI:
  2553. 	case ASC_BREAKER:
  2554. 	case HFLI_MOON:	//[orn]
  2555. 	case HFLI_SBR44:	//[orn]
  2556. 	case NPC_BLEEDING:
  2557. 	case NPC_CRITICALWOUND:
  2558. 	case NPC_HELLPOWER:
  2559. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2560. 		break;
  2561.  
  2562. 	case LK_JOINTBEAT: // decide the ailment first (affects attack damage and effect)
  2563. 		switch( rand()%6 ){
  2564. 		case 0: flag |= BREAK_ANKLE; break;
  2565. 		case 1: flag |= BREAK_WRIST; break;
  2566. 		case 2: flag |= BREAK_KNEE; break;
  2567. 		case 3: flag |= BREAK_SHOULDER; break;
  2568. 		case 4: flag |= BREAK_WAIST; break;
  2569. 		case 5: flag |= BREAK_NECK; break;
  2570. 		}
  2571. 		//TODO: is there really no cleaner way to do this?
  2572. 		sc = status_get_sc(bl);
  2573. 		if (sc) sc->jb_flag = flag;
  2574. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2575. 		break;
  2576.  
  2577. 	case MO_COMBOFINISH:
  2578. 		if (!(flag&1) && sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_MONK)
  2579. 		{	//Becomes a splash attack when Soul Linked.
  2580. 			map_foreachinrange(skill_area_sub, bl,
  2581. 				skill_get_splash(skillid, skilllv),splash_target(src),
  2582. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  2583. 				skill_castend_damage_id);
  2584. 		} else
  2585. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2586. 		break;
  2587.  
  2588. 	case TK_STORMKICK: // Taekwon kicks [Dralnu]
  2589. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  2590. 		skill_area_temp[1] = 0;
  2591. 		map_foreachinrange(skill_attack_area, src,
  2592. 			skill_get_splash(skillid, skilllv), splash_target(src),
  2593. 			BF_WEAPON, src, src, skillid, skilllv, tick, flag, BCT_ENEMY);
  2594. 		break;
  2595.  
  2596. 	case KN_CHARGEATK:
  2597. 		{
  2598. 		bool path = path_search_long(NULL, src->m, src->x, src->y, bl->x, bl->y,CELL_CHKWALL);
  2599. 		unsigned int dist = distance_bl(src, bl);
  2600. 		unsigned int dir = map_calc_dir(bl, src->x, src->y);
  2601.  
  2602. 		// teleport to target (if not on WoE grounds)
  2603. 		if( !map_flag_gvg(src->m) && !map[src->m].flag.battleground && unit_movepos(src, bl->x, bl->y, 0, 1) )
  2604. 			clif_slide(src, bl->x, bl->y);
  2605.  
  2606. 		// cause damage and knockback if the path to target was a straight one
  2607. 		if( path )
  2608. 		{
  2609. 			skill_attack(BF_WEAPON, src, src, bl, skillid, skilllv, tick, dist);
  2610. 			skill_blown(src, bl, dist, dir, 0);
  2611. 			//HACK: since knockback officially defaults to the left, the client also turns to the left... therefore,
  2612. 			// make the caster look in the direction of the target
  2613. 			unit_setdir(src, (dir+4)%8);
  2614. 		}
  2615.  
  2616. 		}
  2617. 		break;
  2618.  
  2619. 	case TK_JUMPKICK:
  2620. 		if( unit_movepos(src, bl->x, bl->y, 1, 1) )
  2621. 		{
  2622. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2623. 			clif_slide(src,bl->x,bl->y);
  2624. 		}
  2625. 		break;
  2626.  
  2627. 	case SN_SHARPSHOOTING:
  2628. 	case MA_SHARPSHOOTING:
  2629. 	case NJ_KAMAITACHI:
  2630. 		//It won't shoot through walls since on castend there has to be a direct
  2631. 		//line of sight between caster and target.
  2632. 		skill_area_temp[1] = bl->id;
  2633. 		map_foreachinpath (skill_attack_area,src->m,src->x,src->y,bl->x,bl->y,
  2634. 			skill_get_splash(skillid, skilllv),skill_get_maxcount(skillid,skilllv), splash_target(src),
  2635. 			skill_get_type(skillid),src,src,skillid,skilllv,tick,flag,BCT_ENEMY);
  2636. 		break;
  2637.  
  2638. 	case NPC_ACIDBREATH:
  2639. 	case NPC_DARKNESSBREATH:
  2640. 	case NPC_FIREBREATH:
  2641. 	case NPC_ICEBREATH:
  2642. 	case NPC_THUNDERBREATH:
  2643. 		skill_area_temp[1] = bl->id;
  2644. 		map_foreachinpath(skill_attack_area,src->m,src->x,src->y,bl->x,bl->y,
  2645. 			skill_get_splash(skillid, skilllv),skill_get_maxcount(skillid,skilllv), splash_target(src),
  2646. 			skill_get_type(skillid),src,src,skillid,skilllv,tick,flag,BCT_ENEMY);
  2647. 		break;
  2648.  
  2649. 	case MO_INVESTIGATE:
  2650. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2651. 		status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2652. 		break;
  2653. 	case WZ_FIREIVY:
  2654. 	{
  2655. 			int x=0, y=0;
  2656. 			int x2=0, y2=0;
  2657. 			int mm = 0;
  2658. 			mm = src->m
  2659. 			x=src->x;
  2660. 			y=src->y;
  2661. 			x2=bl->x;
  2662. 			y2=bl->y;
  2663. 			/*unit_movepos(src, bl->x, bl->y, 1, 1);
  2664. 			clif_slide(src,bl->x,bl->y);
  2665. 			unit_movepos(bl, x, y, 1, 1);
  2666. 			clif_slide(bl,x,y);*/
  2667. 			unit_warp(src,mm,x2,y2,CLR_TELEPORT);
  2668. 			unit_warp(bl,mm,x,y,CLR_TELEPORT);
  2669. 			clif_specialeffect(bl,34,AREA);
  2670. 			clif_specialeffect(src,34,AREA);
  2671. 	}
  2672. 	break;
  2673. 	case RG_BACKSTAP:
  2674. 		{
  2675. 			int dir = map_calc_dir(src, bl->x, bl->y), t_dir = unit_getdir(bl);
  2676. 			if ((!check_distance_bl(src, bl, 0) && !map_check_dir(dir, t_dir)) || bl->type == BL_SKILL) {
  2677. 				status_change_end(src, SC_HIDING, INVALID_TIMER);
  2678. 				skill_attack(BF_WEAPON, src, src, bl, skillid, skilllv, tick, flag);
  2679. 				dir = dir < 4 ? dir+4 : dir-4; // change direction [Celest]
  2680. 				unit_setdir(bl,dir);
  2681. 			}
  2682. 			else if (sd)
  2683. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  2684. 		}
  2685. 		break;
  2686.  
  2687. 	case MO_FINGEROFFENSIVE:
  2688. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2689. 		if (battle_config.finger_offensive_type && sd) {
  2690. 			int i;
  2691. 			for (i = 1; i < sd->spiritball_old; i++)
  2692. 				skill_addtimerskill(src, tick + i * 200, bl->id, 0, 0, skillid, skilllv, BF_WEAPON, flag);
  2693. 		}
  2694. 		status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2695. 		break;
  2696.  
  2697. 	case MO_CHAINCOMBO:
  2698. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2699. 		status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2700. 		break;
  2701.  
  2702. 	case NJ_ISSEN:
  2703. 		status_change_end(src, SC_NEN, INVALID_TIMER);
  2704. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  2705. 		// fall through
  2706. 	case MO_EXTREMITYFIST:
  2707. 		if( skillid == MO_EXTREMITYFIST )
  2708. 		{
  2709. 			status_change_end(src, SC_EXPLOSIONSPIRITS, INVALID_TIMER);
  2710. 			status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2711. 		}
  2712. 		//Client expects you to move to target regardless of distance
  2713. 		{
  2714. 			struct unit_data *ud = unit_bl2ud(src);
  2715. 			short dx,dy;
  2716. 			int i,speed;
  2717. 			i = skillid == MO_EXTREMITYFIST?1:2; //Move 2 cells for Issen, 1 for Asura
  2718. 			dx = bl->x - src->x;
  2719. 			dy = bl->y - src->y;
  2720. 			if (dx < 0) dx-=i;
  2721. 			else if (dx > 0) dx+=i;
  2722. 			if (dy < 0) dy-=i;
  2723. 			else if (dy > 0) dy+=i;
  2724. 			if (!dx && !dy) dy++;
  2725. 			if (map_getcell(src->m, src->x+dx, src->y+dy, CELL_CHKNOPASS))
  2726. 			{
  2727. 				dx = bl->x;
  2728. 				dy = bl->y;
  2729. 			} else {
  2730. 				dx = src->x + dx;
  2731. 				dy = src->y + dy;
  2732. 			}
  2733.  
  2734. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2735.  
  2736. 			if(unit_walktoxy(src, dx, dy, 2) && ud) {
  2737. 				//Increase can't walk delay to not alter your walk path
  2738. 				ud->canmove_tick = tick;
  2739. 				speed = status_get_speed(src);
  2740. 				for (i = 0; i < ud->walkpath.path_len; i ++)
  2741. 				{
  2742. 					if(ud->walkpath.path[i]&1)
  2743. 						ud->canmove_tick+=7*speed/5;
  2744. 					else
  2745. 						ud->canmove_tick+=speed;
  2746. 				}
  2747. 			}
  2748. 		}
  2749. 		break;
  2750.  
  2751. 	//Splash attack skills.
  2752. 	case AS_GRIMTOOTH:
  2753. 	case MC_CARTREVOLUTION:
  2754. 	case NPC_SPLASHATTACK:
  2755. 		flag |= SD_PREAMBLE; // a fake packet will be sent for the first target to be hit
  2756. 	case AS_SPLASHER:
  2757. 	case SM_MAGNUM:
  2758. 	case MS_MAGNUM:
  2759. 	case HT_BLITZBEAT:
  2760. 	case AC_SHOWER:
  2761. 	case MA_SHOWER:
  2762. 	case MG_NAPALMBEAT:
  2763. 	case MG_FIREBALL:
  2764. 	case RG_RAID:
  2765. 	case HW_NAPALMVULCAN:
  2766. 	case NJ_HUUMA:
  2767. 	case NJ_BAKUENRYU:
  2768. 	case ASC_METEORASSAULT:
  2769. 	case GS_DESPERADO:
  2770. 	case GS_SPREADATTACK:
  2771. 	case NPC_EARTHQUAKE:
  2772. 	case NPC_PULSESTRIKE:
  2773. 	case NPC_HELLJUDGEMENT:
  2774. 	case NPC_VAMPIRE_GIFT:
  2775. 		if( flag&1 )
  2776. 		{	//Recursive invocation
  2777. 			// skill_area_temp[0] holds number of targets in area
  2778. 			// skill_area_temp[1] holds the id of the original target
  2779. 			// skill_area_temp[2] counts how many targets have already been processed
  2780. 			int sflag = skill_area_temp[0] & 0xFFF, heal;
  2781. 			if( flag&SD_LEVEL )
  2782. 				sflag |= SD_LEVEL; // -1 will be used in packets instead of the skill level
  2783. 			if( skill_area_temp[1] != bl->id && !(skill_get_inf2(skillid)&INF2_NPC_SKILL) )
  2784. 				sflag |= SD_ANIMATION; // original target gets no animation (as well as all NPC skills)
  2785.  
  2786. 			heal = skill_attack(skill_get_type(skillid), src, src, bl, skillid, skilllv, tick, sflag);
  2787. 			if( skillid == NPC_VAMPIRE_GIFT && heal > 0 )
  2788. 			{
  2789. 				clif_skill_nodamage(NULL, src, AL_HEAL, heal, 1);
  2790. 				status_heal(src,heal,0,0);
  2791. 			}
  2792. 		}
  2793. 		else
  2794. 		{
  2795. 			if ( skillid == NJ_BAKUENRYU )
  2796. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  2797.  
  2798. 			skill_area_temp[0] = 0;
  2799. 			skill_area_temp[1] = bl->id;
  2800. 			skill_area_temp[2] = 0;
  2801.  
  2802. 			// if skill damage should be split among targets, count them
  2803. 			//SD_LEVEL -> Forced splash damage for Auto Blitz-Beat -> count targets
  2804. 			//special case: Venom Splasher uses a different range for searching than for splashing
  2805. 			if( flag&SD_LEVEL || skill_get_nk(skillid)&NK_SPLASHSPLIT )
  2806. 				skill_area_temp[0] = map_foreachinrange(skill_area_sub, bl, (skillid == AS_SPLASHER)?1:skill_get_splash(skillid, skilllv), BL_CHAR, src, skillid, skilllv, tick, BCT_ENEMY, skill_area_sub_count);
  2807.  
  2808. 			// recursive invocation of skill_castend_damage_id() with flag|1
  2809. 			map_foreachinrange(skill_area_sub, bl, skill_get_splash(skillid, skilllv), splash_target(src), src, skillid, skilllv, tick, flag|BCT_ENEMY|SD_SPLASH|1, skill_castend_damage_id);
  2810.  
  2811. 			//FIXME: Isn't EarthQuake a ground skill after all?
  2812. 			if( skillid == NPC_EARTHQUAKE )
  2813. 				skill_addtimerskill(src,tick+250,src->id,0,0,skillid,skilllv,2,flag|BCT_ENEMY|SD_SPLASH|1);
  2814.  
  2815. 			//FIXME: move this to skill_additional_effect or some such? [ultramage]
  2816. 			if( skillid == SM_MAGNUM || skillid == MS_MAGNUM )
  2817. 			{ // Initiate 10% of your damage becomes fire element.
  2818. 				sc_start4(src,SC_WATK_ELEMENT,100,3,20,0,0,skill_get_time2(skillid, skilllv));
  2819. 				if( sd )
  2820. 					skill_blockpc_start(sd, skillid, skill_get_time(skillid, skilllv));
  2821. 				if( bl->type == BL_MER )
  2822. 					skill_blockmerc_start((TBL_MER*)bl, skillid, skill_get_time(skillid, skilllv));
  2823. 			}
  2824. 		}
  2825. 		break;
  2826.  
  2827. 	case KN_BRANDISHSPEAR:
  2828. 	case ML_BRANDISH:
  2829. 		//Coded apart for it needs the flag passed to the damage calculation.
  2830. 		if (skill_area_temp[1] != bl->id)
  2831. 			skill_attack(skill_get_type(skillid), src, src, bl, skillid, skilllv, tick, flag|SD_ANIMATION);
  2832. 		else
  2833. 			skill_attack(skill_get_type(skillid), src, src, bl, skillid, skilllv, tick, flag);
  2834. 		break;
  2835.  
  2836. 	case KN_BOWLINGBASH:
  2837. 	case MS_BOWLINGBASH:
  2838. 		if(flag&1){
  2839. 			if(bl->id==skill_area_temp[1])
  2840. 				break;
  2841. 			//two hits for 500%
  2842. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,SD_ANIMATION);
  2843. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,SD_ANIMATION);
  2844. 		} else {
  2845. 			int i,c;
  2846. 			c = skill_get_blewcount(skillid,skilllv);
  2847. 			// keep moving target in the direction that src is looking, square by square
  2848. 			for(i=0;i<c;i++){
  2849. 				if (!skill_blown(src,bl,1,(unit_getdir(src)+4)%8,0x1))
  2850. 					break; //Can't knockback
  2851. 				skill_area_temp[0] = map_foreachinrange(skill_area_sub, bl, skill_get_splash(skillid, skilllv), BL_CHAR, src, skillid, skilllv, tick, flag|BCT_ENEMY, skill_area_sub_count);
  2852. 				if( skill_area_temp[0] > 1 ) break; // collision
  2853. 			}
  2854. 			clif_blown(bl); //Update target pos.
  2855. 			if (i!=c) { //Splash
  2856. 				skill_area_temp[1] = bl->id;
  2857. 				map_foreachinrange(skill_area_sub, bl, skill_get_splash(skillid, skilllv), splash_target(src), src, skillid, skilllv, tick, flag|BCT_ENEMY|1, skill_castend_damage_id);
  2858. 			}
  2859. 			//Weirdo dual-hit property, two attacks for 500%
  2860. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,0);
  2861. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,0);
  2862. 		}
  2863. 		break;
  2864.  
  2865. 	case KN_SPEARSTAB:
  2866. 		if(flag&1) {
  2867. 			if (bl->id==skill_area_temp[1])
  2868. 				break;
  2869. 			if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,SD_ANIMATION))
  2870. 				skill_blown(src,bl,skill_area_temp[2],-1,0);
  2871. 		} else {
  2872. 			int x=bl->x,y=bl->y,i,dir;
  2873. 			dir = map_calc_dir(bl,src->x,src->y);
  2874. 			skill_area_temp[1] = bl->id;
  2875. 			skill_area_temp[2] = skill_get_blewcount(skillid,skilllv);
  2876. 			// all the enemies between the caster and the target are hit, as well as the target
  2877. 			if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,0))
  2878. 				skill_blown(src,bl,skill_area_temp[2],-1,0);
  2879. 			for (i=0;i<4;i++) {
  2880. 				map_foreachincell(skill_area_sub,bl->m,x,y,BL_CHAR,
  2881. 					src,skillid,skilllv,tick,flag|BCT_ENEMY|1,skill_castend_damage_id);
  2882. 				x += dirx[dir];
  2883. 				y += diry[dir];
  2884. 			}
  2885. 		}
  2886. 		break;
  2887.  
  2888. 	case TK_TURNKICK:
  2889. 	case MO_BALKYOUNG: //Active part of the attack. Skill-attack [Skotlex]
  2890. 	{
  2891. 		skill_area_temp[1] = bl->id; //NOTE: This is used in skill_castend_nodamage_id to avoid affecting the target.
  2892. 		if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag))
  2893. 			map_foreachinrange(skill_area_sub,bl,
  2894. 				skill_get_splash(skillid, skilllv),BL_CHAR,
  2895. 				src,skillid,skilllv,tick,flag|BCT_ENEMY|1,
  2896. 				skill_castend_nodamage_id);
  2897. 	}
  2898. 		break;
  2899. 	case CH_PALMSTRIKE: //	Palm Strike takes effect 1sec after casting. [Skotlex]
  2900. 	//	clif_skill_nodamage(src,bl,skillid,skilllv,0); //Can't make this one display the correct attack animation delay :/
  2901. 		clif_damage(src,bl,tick,status_get_amotion(src),0,-1,1,4,0); //Display an absorbed damage attack.
  2902. 		skill_addtimerskill(src, tick + 1000, bl->id, 0, 0, skillid, skilllv, BF_WEAPON, flag);
  2903. 		break;
  2904.  
  2905. 	case PR_TURNUNDEAD:
  2906. 	case ALL_RESURRECTION:
  2907. 		if (!battle_check_undead(tstatus->race, tstatus->def_ele))
  2908. 			break;
  2909. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2910. 		break;
  2911.  
  2912. 	case MG_SOULSTRIKE:
  2913. 	case NPC_DARKSTRIKE:
  2914. 	case MG_COLDBOLT:
  2915. 	case MG_FIREBOLT:
  2916. 	case MG_LIGHTNINGBOLT:
  2917. 	case WZ_EARTHSPIKE:
  2918. 	case AL_HEAL:
  2919. 	case AL_HOLYLIGHT:
  2920. 	case WZ_JUPITEL:
  2921. 	case NPC_DARKTHUNDER:
  2922. 	case PR_ASPERSIO:
  2923. 	case MG_FROSTDIVER:
  2924. 	case WZ_SIGHTBLASTER:
  2925. 	case WZ_SIGHTRASHER:
  2926. 	case NJ_KOUENKA:
  2927. 	case NJ_HYOUSENSOU:
  2928. 	case NJ_HUUJIN:
  2929. 	skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2930. 		break;
  2931. 	/*case WZ_FIREIVY: //  extra skill ( for fun dota style )
  2932. 		{
  2933. 			int x, y;
  2934. 			x = src->x; // Get current postion of player
  2935. 			y = src->y; 
  2936.  
  2937. 			if( unit_movepos(src,bl->x,bl->y,0,0) )
  2938. 			{
  2939. 				unit_setdir(src, map_calc_dir(src, bl->x, bl->y));
  2940. 				clif_skill_nodamage(src,src,skillid,skilllv,1); 
  2941. 				clif_slide(src, bl->x, bl->y) ; // player swap to target pos
  2942.  
  2943. 				if( unit_movepos(bl,x,y,0,0) )
  2944. 				{
  2945. 					clif_skill_nodamage(bl,bl,skillid,skilllv,1); 
  2946. 					clif_slide(bl, x, y) ; // target swap to player pos
  2947. 				}
  2948. 			}
  2949. 		}
  2950. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2951. 		break;*/
  2952.  
  2953. 	case NPC_MAGICALATTACK:
  2954. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2955. 		sc_start(src,status_skill2sc(skillid),100,skilllv,skill_get_time(skillid,skilllv));
  2956. 		break;
  2957.  
  2958. 	case HVAN_CAPRICE: //[blackhole89]
  2959. 		{
  2960. 			int ran=rand()%4;
  2961. 			int sid = 0;
  2962. 			switch(ran)
  2963. 			{
  2964. 			case 0: sid=MG_COLDBOLT; break;
  2965. 			case 1: sid=MG_FIREBOLT; break;
  2966. 			case 2: sid=MG_LIGHTNINGBOLT; break;
  2967. 			case 3: sid=WZ_EARTHSPIKE; break;
  2968. 			}
  2969. 			skill_attack(BF_MAGIC,src,src,bl,sid,skilllv,tick,flag|SD_LEVEL);
  2970. 		}
  2971. 		break;
  2972. 	case WZ_WATERBALL:
  2973. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2974. 		{
  2975. 			int range = skilllv / 2;
  2976. 			int maxlv = skill_get_max(skillid); // learnable level
  2977. 			int count = 0;
  2978. 			int x, y;
  2979. 			struct skill_unit* unit;
  2980.  
  2981. 			if( skilllv > maxlv )
  2982. 			{
  2983. 				if( src->type == BL_MOB && skilllv == 10 )
  2984. 					range = 4;
  2985. 				else
  2986. 					range = maxlv / 2;
  2987. 			}
  2988.  
  2989. 			for( y = src->y - range; y <= src->y + range; ++y )
  2990. 				for( x = src->x - range; x <= src->x + range; ++x )
  2991. 				{
  2992. 					if( !map_find_skill_unit_oncell(src,x,y,SA_LANDPROTECTOR,NULL) )
  2993. 					{
  2994. 						if( src->type != BL_PC || map_getcell(src->m,x,y,CELL_CHKWATER) ) // non-players bypass the water requirement
  2995. 							count++; // natural water cell
  2996. 						else if( (unit = map_find_skill_unit_oncell(src,x,y,SA_DELUGE,NULL)) != NULL || (unit = map_find_skill_unit_oncell(src,x,y,NJ_SUITON,NULL)) != NULL )
  2997. 						{
  2998. 							count++; // skill-induced water cell
  2999. 							skill_delunit(unit); // consume cell
  3000. 						}
  3001. 					}
  3002. 				}
  3003.  
  3004. 			if( count > 1 ) // queue the remaining count - 1 timerskill Waterballs
  3005. 				skill_addtimerskill(src,tick+150,bl->id,0,0,skillid,skilllv,count-1,flag);
  3006. 		}
  3007. 		break;
  3008.  
  3009. 	case PR_BENEDICTIO:
  3010. 		//Should attack undead and demons. [Skotlex]
  3011. 		if (battle_check_undead(tstatus->race, tstatus->def_ele) || tstatus->race == RC_DEMON)
  3012. 			skill_attack(BF_MAGIC, src, src, bl, skillid, skilllv, tick, flag);
  3013. 	break;
  3014.  
  3015. 	case SL_SMA:
  3016. 		status_change_end(src, SC_SMA, INVALID_TIMER);
  3017. 	case SL_STIN:
  3018. 	case SL_STUN:
  3019. 		if (sd && !battle_config.allow_es_magic_pc && bl->type != BL_MOB) {
  3020. 			status_change_start(src,SC_STUN,10000,skilllv,0,0,0,500,10);
  3021. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3022. 			break;
  3023. 		}
  3024. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  3025. 		break;
  3026.  
  3027. 	case NPC_DARKBREATH:
  3028. 		clif_emotion(src,E_AG);
  3029. 	case SN_FALCONASSAULT:
  3030. 	case PA_PRESSURE:
  3031. 	case CR_ACIDDEMONSTRATION:
  3032. 	case TF_THROWSTONE:
  3033. 	case NPC_SMOKING:
  3034. 	case GS_FLING:
  3035. 	case NJ_ZENYNAGE:
  3036. 		skill_attack(BF_MISC,src,src,bl,skillid,skilllv,tick,flag);
  3037. 		break;
  3038.  
  3039. 	case HVAN_EXPLOSION:
  3040. 	case NPC_SELFDESTRUCTION:
  3041. 		if (src != bl)
  3042. 			skill_attack(BF_MISC,src,src,bl,skillid,skilllv,tick,flag);
  3043. 		break;
  3044.  
  3045. 	// Celest
  3046. 	case PF_SOULBURN:
  3047. 		if (rand()%100 < (skilllv < 5 ? 30 + skilllv * 10 : 70)) {
  3048. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3049. 			if (skilllv == 5)
  3050. 				skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  3051. 			status_percent_damage(src, bl, 0, 100, false);
  3052. 		} else {
  3053. 			clif_skill_nodamage(src,src,skillid,skilllv,1);
  3054. 			if (skilllv == 5)
  3055. 				skill_attack(BF_MAGIC,src,src,src,skillid,skilllv,tick,flag);
  3056. 			status_percent_damage(src, src, 0, 100, false);
  3057. 		}
  3058. 		if (sd) skill_blockpc_start (sd, skillid, (skilllv < 5 ? 10000: 15000));
  3059. 		break;
  3060.  
  3061. 	case NPC_BLOODDRAIN:
  3062. 	case NPC_ENERGYDRAIN:
  3063. 		{
  3064. 			int heal = skill_attack( (skillid == NPC_BLOODDRAIN) ? BF_WEAPON : BF_MAGIC,
  3065. 					src, src, bl, skillid, skilllv, tick, flag);
  3066. 			if (heal > 0){
  3067. 				clif_skill_nodamage(NULL, src, AL_HEAL, heal, 1);
  3068. 				status_heal(src, heal, 0, 0);
  3069. 			}
  3070. 		}
  3071. 		break;
  3072.  
  3073. 	case GS_BULLSEYE:
  3074. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  3075. 		break;
  3076.  
  3077. 	case NJ_KASUMIKIRI:
  3078. 		if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag) > 0)
  3079. 			sc_start(src,SC_HIDING,100,skilllv,skill_get_time(skillid,skilllv));
  3080. 		break;
  3081. 	case NJ_KIRIKAGE:
  3082. 		if( !map_flag_gvg(src->m) && !map[src->m].flag.battleground )
  3083. 		{	//You don't move on GVG grounds.
  3084. 			short x, y;
  3085. 			map_search_freecell(bl, 0, &x, &y, 1, 1, 0);
  3086. 			if (unit_movepos(src, x, y, 0, 0))
  3087. 				clif_slide(src,src->x,src->y);
  3088. 		}
  3089. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  3090. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  3091. 		break;
  3092. 	case 0:
  3093. 		if(sd) {
  3094. 			if (flag & 3){
  3095. 				if (bl->id != skill_area_temp[1])
  3096. 					skill_attack(BF_WEAPON, src, src, bl, skillid, skilllv, tick, SD_LEVEL|flag);
  3097. 			} else {
  3098. 				skill_area_temp[1] = bl->id;
  3099. 				map_foreachinrange(skill_area_sub, bl,
  3100. 					sd->splash_range, BL_CHAR,
  3101. 					src, skillid, skilllv, tick, flag | BCT_ENEMY | 1,
  3102. 					skill_castend_damage_id);
  3103. 				flag|=1; //Set flag to 1 so ammo is not double-consumed. [Skotlex]
  3104. 			}
  3105. 		}
  3106. 		break;
  3107.  
  3108. 	default:
  3109. 		ShowWarning("skill_castend_damage_id: Unknown skill used:%d\n",skillid);
  3110. 		clif_skill_damage(src, bl, tick, status_get_amotion(src), tstatus->dmotion,
  3111. 			0, abs(skill_get_num(skillid, skilllv)),
  3112. 			skillid, skilllv, skill_get_hit(skillid));
  3113. 		map_freeblock_unlock();
  3114. 		return 1;
  3115. 	}
  3116.  
  3117. 	map_freeblock_unlock();
  3118.  
  3119. 	if( sd && !(flag&1) )
  3120. 	{
  3121. 		if( sd->state.arrow_atk ) //Consume arrow on last invocation to this skill.
  3122. 			battle_consume_ammo(sd, skillid, skilllv);
  3123. 		skill_onskillusage(sd, bl, skillid, tick);
  3124. 		skill_consume_requirement(sd,skillid,skilllv,2);
  3125. 	}
  3126.  
  3127. 	return 0;
  3128. }
  3129.  
  3130. /*==========================================
  3131.  *
  3132.  *------------------------------------------*/
  3133. int skill_castend_nodamage_id (struct block_list *src, struct block_list *bl, int skillid, int skilllv, unsigned int tick, int flag)
  3134. {
  3135. 	struct map_session_data *sd, *dstsd;
  3136. 	struct mob_data *md, *dstmd;
  3137. 	struct homun_data *hd;
  3138. 	struct mercenary_data *mer;
  3139. 	struct status_data *sstatus, *tstatus;
  3140. 	struct status_change *tsc;
  3141. 	struct status_change_entry *tsce;
  3142.  
  3143. 	int i;
  3144. 	enum sc_type type;
  3145.  
  3146. 	if(skillid > 0 && skilllv <= 0) return 0;	// celest
  3147.  
  3148. 	nullpo_retr(1, src);
  3149. 	nullpo_retr(1, bl);
  3150.  
  3151. 	if (src->m != bl->m)
  3152. 		return 1;
  3153.  
  3154. 	sd = BL_CAST(BL_PC, src);
  3155. 	hd = BL_CAST(BL_HOM, src);
  3156. 	md = BL_CAST(BL_MOB, src);
  3157. 	mer = BL_CAST(BL_MER, src);
  3158.  
  3159. 	dstsd = BL_CAST(BL_PC, bl);
  3160. 	dstmd = BL_CAST(BL_MOB, bl);
  3161.  
  3162. 	if(bl->prev == NULL)
  3163. 		return 1;
  3164. 	if(status_isdead(src))
  3165. 		return 1;
  3166.  
  3167. 	if( src != bl && status_isdead(bl) && skillid != ALL_RESURRECTION && skillid != PR_REDEMPTIO && skillid != NPC_WIDESOULDRAIN )
  3168. 		return 1;
  3169.  
  3170. 	tstatus = status_get_status_data(bl);
  3171. 	sstatus = status_get_status_data(src);
  3172.  
  3173. 	//Check for undead skills that convert a no-damage skill into a damage one. [Skotlex]
  3174. 	switch (skillid) {
  3175. 		case HLIF_HEAL:	//[orn]
  3176. 			if (bl->type != BL_HOM) {
  3177. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0) ;
  3178. 	        break ;
  3179. 			}
  3180.  		case AL_HEAL:
  3181. 		case ALL_RESURRECTION:
  3182. 		case PR_ASPERSIO:
  3183. 			//Apparently only player casted skills can be offensive like this.
  3184. 			if (sd && battle_check_undead(tstatus->race,tstatus->def_ele)) {
  3185. 				if (battle_check_target(src, bl, BCT_ENEMY) < 1) {
  3186. 				  	//Offensive heal does not works on non-enemies. [Skotlex]
  3187. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3188. 					return 0;
  3189. 				}
  3190. 				return skill_castend_damage_id (src, bl, skillid, skilllv, tick, flag);
  3191. 			}
  3192. 			break;
  3193. 		case NPC_SMOKING: //Since it is a self skill, this one ends here rather than in damage_id. [Skotlex]
  3194. 			return skill_castend_damage_id (src, bl, skillid, skilllv, tick, flag);
  3195. 		default:
  3196. 			//Skill is actually ground placed.
  3197. 			if (src == bl && skill_get_unit_id(skillid,0))
  3198. 				return skill_castend_pos2(src,bl->x,bl->y,skillid,skilllv,tick,0);
  3199. 	}
  3200.  
  3201. 	type = status_skill2sc(skillid);
  3202. 	tsc = status_get_sc(bl);
  3203. 	tsce = (tsc && type != -1)?tsc->data[type]:NULL;
  3204.  
  3205. 	if (src!=bl && type > -1 &&
  3206. 		(i = skill_get_ele(skillid, skilllv)) > ELE_NEUTRAL &&
  3207. 		skill_get_inf(skillid) != INF_SUPPORT_SKILL &&
  3208. 		battle_attr_fix(NULL, NULL, 100, i, tstatus->def_ele, tstatus->ele_lv) <= 0)
  3209. 		return 1; //Skills that cause an status should be blocked if the target element blocks its element.
  3210.  
  3211. 	map_freeblock_lock();
  3212. 	switch(skillid)
  3213. 	{
  3214. 	case HLIF_HEAL:	//[orn]
  3215. 	case AL_HEAL:
  3216. 		{
  3217. 			int heal = skill_calc_heal(src, bl, skillid, skilllv, true);
  3218. 			int heal_get_jobexp;
  3219.  
  3220. 			if( status_isimmune(bl) || (dstmd && (dstmd->class_ == MOBID_EMPERIUM || mob_is_battleground(dstmd))) )
  3221. 				heal=0;
  3222.  
  3223. 			if( sd && dstsd && sd->status.partner_id == dstsd->status.char_id && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && sd->status.sex == 0 )
  3224. 				heal = heal*2;
  3225.  
  3226. 			if( tsc && tsc->count )
  3227. 			{
  3228. 				if( tsc->data[SC_KAITE] && !(sstatus->mode&MD_BOSS) )
  3229. 				{ //Bounce back heal
  3230. 					if (--tsc->data[SC_KAITE]->val2 <= 0)
  3231. 						status_change_end(bl, SC_KAITE, INVALID_TIMER);
  3232. 					if (src == bl)
  3233. 						heal=0; //When you try to heal yourself under Kaite, the heal is voided.
  3234. 					else {
  3235. 						bl = src;
  3236. 						dstsd = sd;
  3237. 					}
  3238. 				} else
  3239. 				if (tsc->data[SC_BERSERK])
  3240. 					heal = 0; //Needed so that it actually displays 0 when healing.
  3241. 			}
  3242. 			heal_get_jobexp = status_heal(bl,heal,0,0);
  3243. 			clif_skill_nodamage (src, bl, skillid, heal, 1);
  3244.  
  3245. 			if(sd && dstsd && heal > 0 && sd != dstsd && battle_config.heal_exp > 0){
  3246. 				heal_get_jobexp = heal_get_jobexp * battle_config.heal_exp / 100;
  3247. 				if (heal_get_jobexp <= 0)
  3248. 					heal_get_jobexp = 1;
  3249. 				pc_gainexp (sd, bl, 0, heal_get_jobexp, false);
  3250. 			}
  3251. 		}
  3252. 		break;
  3253.  
  3254. 	case WZ_FIREIVY:
  3255. 	{
  3256. 		if (battle_check_target(src, bl, BCT_ENEMY) < 1) 
  3257. 		{
  3258. 			int x=0, y=0;
  3259. 			int x2=0, y2=0;
  3260. 			int mm = 0;
  3261. 			mm = src->m
  3262. 			x=src->x;
  3263. 			y=src->y;
  3264. 			x2=bl->x;
  3265. 			y2=bl->y;
  3266. 			/*unit_movepos(src, bl->x, bl->y, 1, 1);
  3267. 			clif_slide(src,bl->x,bl->y);
  3268. 			unit_movepos(bl, x, y, 1, 1);
  3269. 			clif_slide(bl,x,y);*/
  3270. 			unit_warp(src,mm,x2,y2,CLR_TELEPORT);
  3271. 			unit_warp(bl,mm,x,y,CLR_TELEPORT);
  3272. 			clif_specialeffect(bl,34,AREA);
  3273. 			clif_specialeffect(src,34,AREA);
  3274. 		}
  3275. 		else
  3276. 		return skill_castend_damage_id (src, bl, skillid, skilllv, tick, flag);
  3277. 	}
  3278. 	break;
  3279.  
  3280. 	case PR_REDEMPTIO:
  3281. 		if (sd && !(flag&1)) {
  3282. 			if (sd->status.party_id == 0) {
  3283. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3284. 				break;
  3285. 			}
  3286. 			skill_area_temp[0] = 0;
  3287. 			party_foreachsamemap(skill_area_sub,
  3288. 				sd,skill_get_splash(skillid, skilllv),
  3289. 				src,skillid,skilllv,tick, flag|BCT_PARTY|1,
  3290. 				skill_castend_nodamage_id);
  3291. 			if (skill_area_temp[0] == 0) {
  3292. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3293. 				break;
  3294. 			}
  3295. 			skill_area_temp[0] = 5 - skill_area_temp[0]; // The actual penalty...
  3296. 			if (skill_area_temp[0] > 0 && !map[src->m].flag.noexppenalty) { //Apply penalty
  3297. 				sd->status.base_exp -= min(sd->status.base_exp, pc_nextbaseexp(sd) * skill_area_temp[0] * 2/1000); //0.2% penalty per each.
  3298. 				sd->status.job_exp -= min(sd->status.job_exp, pc_nextjobexp(sd) * skill_area_temp[0] * 2/1000);
  3299. 				clif_updatestatus(sd,SP_BASEEXP);
  3300. 				clif_updatestatus(sd,SP_JOBEXP);
  3301. 			}
  3302. 			status_set_hp(src, 1, 0);
  3303. 			status_set_sp(src, 0, 0);
  3304. 			break;
  3305. 		} else if (status_isdead(bl) && flag&1) { //Revive
  3306. 			skill_area_temp[0]++; //Count it in, then fall-through to the Resurrection code.
  3307. 			skilllv = 3; //Resurrection level 3 is used
  3308. 		} else //Invalid target, skip resurrection.
  3309. 			break;
  3310.  
  3311. 	case ALL_RESURRECTION:
  3312. 		if(sd && (map_flag_gvg(bl->m) || map[bl->m].flag.battleground))
  3313. 		{	//No reviving in WoE grounds!
  3314. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3315. 			break;
  3316. 		}
  3317. 		if (!status_isdead(bl))
  3318. 			break;
  3319. 		{
  3320. 			int per = 0, sper = 0;
  3321. 			if (tsc && tsc->data[SC_HELLPOWER])
  3322. 				break;
  3323.  
  3324. 			if (map[bl->m].flag.pvp && dstsd && dstsd->pvp_point < 0)
  3325. 				break;
  3326.  
  3327. 			switch(skilllv){
  3328. 			case 1: per=10; break;
  3329. 			case 2: per=30; break;
  3330. 			case 3: per=50; break;
  3331. 			case 4: per=80; break;
  3332. 			}
  3333. 			if(dstsd && dstsd->special_state.restart_full_recover)
  3334. 				per = sper = 100;
  3335. 			if (status_revive(bl, per, sper))
  3336. 			{
  3337. 				clif_skill_nodamage(src,bl,ALL_RESURRECTION,skilllv,1); //Both Redemptio and Res show this skill-animation.
  3338. 				if(sd && dstsd && battle_config.resurrection_exp > 0)
  3339. 				{
  3340. 					int exp = 0,jexp = 0;
  3341. 					int lv = dstsd->status.base_level - sd->status.base_level, jlv = dstsd->status.job_level - sd->status.job_level;
  3342. 					if(lv > 0 && pc_nextbaseexp(dstsd)) {
  3343. 						exp = (int)((double)dstsd->status.base_exp * (double)lv * (double)battle_config.resurrection_exp / 1000000.);
  3344. 						if (exp < 1) exp = 1;
  3345. 					}
  3346. 					if(jlv > 0 && pc_nextjobexp(dstsd)) {
  3347. 						jexp = (int)((double)dstsd->status.job_exp * (double)lv * (double)battle_config.resurrection_exp / 1000000.);
  3348. 						if (jexp < 1) jexp = 1;
  3349. 					}
  3350. 					if(exp > 0 || jexp > 0)
  3351. 						pc_gainexp (sd, bl, exp, jexp, false);
  3352. 				}
  3353. 			}
  3354. 		}
  3355. 		break;
  3356.  
  3357. 	case AL_DECAGI:
  3358. 	case MER_DECAGI:
  3359. 		clif_skill_nodamage (src, bl, skillid, skilllv,
  3360. 			sc_start(bl, type, (40 + skilllv * 2 + (status_get_lv(src) + sstatus->int_)/5), skilllv, skill_get_time(skillid,skilllv)));
  3361. 		break;
  3362.  
  3363. 	case AL_CRUCIS:
  3364. 		if (flag&1)
  3365. 			sc_start(bl,type, 23+skilllv*4 +status_get_lv(src) -status_get_lv(bl), skilllv,skill_get_time(skillid,skilllv));
  3366. 		else {
  3367. 			map_foreachinrange(skill_area_sub, src, skill_get_splash(skillid, skilllv), BL_CHAR,
  3368. 				src, skillid, skilllv, tick, flag|BCT_ENEMY|1, skill_castend_nodamage_id);
  3369. 			clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  3370. 		}
  3371. 		break;
  3372.  
  3373. 	case PR_LEXDIVINA:
  3374. 	case MER_LEXDIVINA:
  3375. 		if( tsce )
  3376. 			status_change_end(bl,type, INVALID_TIMER);
  3377. 		else
  3378. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  3379. 		clif_skill_nodamage (src, bl, skillid, skilllv, 1);
  3380. 		break;
  3381.  
  3382. 	case SA_ABRACADABRA:
  3383. 		{
  3384. 			int abra_skillid = 0, abra_skilllv;
  3385. 			do {
  3386. 				i = rand() % MAX_SKILL_ABRA_DB;
  3387. 				abra_skillid = skill_abra_db[i].skillid;
  3388. 			} while (abra_skillid == 0 ||
  3389. 				skill_abra_db[i].req_lv > skilllv || //Required lv for it to appear
  3390. 				rand()%10000 >= skill_abra_db[i].per
  3391. 			);
  3392. 			abra_skilllv = min(skilllv, skill_get_max(abra_skillid));
  3393. 			clif_skill_nodamage (src, bl, skillid, skilllv, 1);
  3394.  
  3395. 			if( sd )
  3396. 			{// player-casted
  3397. 				sd->state.abra_flag = 1;
  3398. 				sd->skillitem = abra_skillid;
  3399. 				sd->skillitemlv = abra_skilllv;
  3400. 				clif_item_skill(sd, abra_skillid, abra_skilllv);
  3401. 			}
  3402. 			else
  3403. 			{// mob-casted
  3404. 				struct unit_data *ud = unit_bl2ud(src);
  3405. 				int inf = skill_get_inf(abra_skillid);
  3406. 				int target_id = 0;
  3407. 				if (!ud) break;
  3408. 				if (inf&INF_SELF_SKILL || inf&INF_SUPPORT_SKILL) {
  3409. 					if (src->type == BL_PET)
  3410. 						bl = (struct block_list*)((TBL_PET*)src)->msd;
  3411. 					if (!bl) bl = src;
  3412. 					unit_skilluse_id(src, bl->id, abra_skillid, abra_skilllv);
  3413. 				} else {	//Assume offensive skills
  3414. 					if (ud->target)
  3415. 						target_id = ud->target;
  3416. 					else switch (src->type) {
  3417. 						case BL_MOB: target_id = ((TBL_MOB*)src)->target_id; break;
  3418. 						case BL_PET: target_id = ((TBL_PET*)src)->target_id; break;
  3419. 					}
  3420. 					if (!target_id)
  3421. 						break;
  3422. 					if (skill_get_casttype(abra_skillid) == CAST_GROUND) {
  3423. 						bl = map_id2bl(target_id);
  3424. 						if (!bl) bl = src;
  3425. 						unit_skilluse_pos(src, bl->x, bl->y, abra_skillid, abra_skilllv);
  3426. 					} else
  3427. 						unit_skilluse_id(src, target_id, abra_skillid, abra_skilllv);
  3428. 				}
  3429. 			}
  3430. 		}
  3431. 		break;
  3432.  
  3433. 	case SA_COMA:
  3434. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3435. 			sc_start(bl,type,100,skilllv,skill_get_time2(skillid,skilllv)));
  3436. 		break;
  3437. 	case SA_FULLRECOVERY:
  3438. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3439. 		if (status_isimmune(bl))
  3440. 			break;
  3441. 		status_percent_heal(bl, 100, 100);
  3442. 		break;
  3443. 	case NPC_ALLHEAL:
  3444. 		{
  3445. 			int heal;
  3446. 			if( status_isimmune(bl) )
  3447. 				break;
  3448. 			heal = status_percent_heal(bl, 100, 0);
  3449. 			clif_skill_nodamage(NULL, bl, AL_HEAL, heal, 1);
  3450. 			if( dstmd )
  3451. 			{ // Reset Damage Logs
  3452. 				memset(dstmd->dmglog, 0, sizeof(dstmd->dmglog));
  3453. 				dstmd->tdmg = 0;
  3454. 			}
  3455. 		}
  3456. 		break;
  3457. 	case SA_SUMMONMONSTER:
  3458. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3459. 		if (sd) mob_once_spawn(sd,src->m,src->x,src->y,"--ja--",-1,1,"");
  3460. 		break;
  3461. 	case SA_LEVELUP:
  3462. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3463. 		if (sd && pc_nextbaseexp(sd)) pc_gainexp(sd, NULL, pc_nextbaseexp(sd) * 10 / 100, 0, false);
  3464. 		break;
  3465. 	case SA_INSTANTDEATH:
  3466. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3467. 		status_set_hp(bl,1,0);
  3468. 		break;
  3469. 	case SA_QUESTION:
  3470. 	case SA_GRAVITY:
  3471. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3472. 		break;
  3473. 	case SA_CLASSCHANGE:
  3474. 	case SA_MONOCELL:
  3475. 		if (dstmd)
  3476. 		{
  3477. 			int class_;
  3478. 			if ( sd && dstmd->status.mode&MD_BOSS )
  3479. 			{
  3480. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3481. 				break;
  3482. 			}
  3483. 			class_ = skillid==SA_MONOCELL?1002:mob_get_random_id(2, 1, 0);
  3484. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3485. 			mob_class_change(dstmd,class_);
  3486. 			if( tsc && dstmd->status.mode&MD_BOSS )
  3487. 			{
  3488. 				const enum sc_type scs[] = { SC_QUAGMIRE, SC_PROVOKE, SC_ROKISWEIL, SC_GRAVITATION, SC_SUITON, SC_STRIPWEAPON, SC_STRIPSHIELD, SC_STRIPARMOR, SC_STRIPHELM, SC_BLADESTOP };
  3489. 				for (i = SC_COMMON_MIN; i <= SC_COMMON_MAX; i++)
  3490. 					if (tsc->data[i]) status_change_end(bl, (sc_type)i, INVALID_TIMER);
  3491. 				for (i = 0; i < ARRAYLENGTH(scs); i++)
  3492. 					if (tsc->data[scs[i]]) status_change_end(bl, scs[i], INVALID_TIMER);
  3493. 			}
  3494. 		}
  3495. 		break;
  3496. 	case SA_DEATH:
  3497. 		if ( sd && dstmd && dstmd->status.mode&MD_BOSS )
  3498. 		{
  3499. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3500. 			break;
  3501. 		}
  3502. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3503. 		status_kill(bl);
  3504. 		break;
  3505. 	case SA_REVERSEORCISH:
  3506. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3507. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv)));
  3508. 		break;
  3509. 	case SA_FORTUNE:
  3510. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3511. 		if(sd) pc_getzeny(sd,status_get_lv(bl)*100);
  3512. 		break;
  3513. 	case SA_TAMINGMONSTER:
  3514. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3515. 		if (sd && dstmd) {
  3516. 			ARR_FIND( 0, MAX_PET_DB, i, dstmd->class_ == pet_db[i].class_ );
  3517. 			if( i < MAX_PET_DB )
  3518. 				pet_catch_process1(sd, dstmd->class_);
  3519. 		}
  3520. 		break;
  3521.  
  3522. 	case CR_PROVIDENCE:
  3523. 		if(sd && dstsd){ //Check they are not another crusader [Skotlex]
  3524. 			if ((dstsd->class_&MAPID_UPPERMASK) == MAPID_CRUSADER) {
  3525. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3526. 				map_freeblock_unlock();
  3527. 				return 1;
  3528. 			}
  3529. 		}
  3530. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3531. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3532. 		break;
  3533.  
  3534. 	case CG_MARIONETTE:
  3535. 		{
  3536. 			struct status_change* sc = status_get_sc(src);
  3537.  
  3538. 			if( sd && dstsd && (dstsd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER && dstsd->status.sex == sd->status.sex )
  3539. 			{// Cannot cast on another bard/dancer-type class of the same gender as caster
  3540. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3541. 				map_freeblock_unlock();
  3542. 				return 1;
  3543. 			}
  3544.  
  3545. 			if( sc && tsc )
  3546. 			{
  3547. 				if( !sc->data[SC_MARIONETTE] && !tsc->data[SC_MARIONETTE2] )
  3548. 				{
  3549. 					sc_start(src,SC_MARIONETTE,100,bl->id,skill_get_time(skillid,skilllv));
  3550. 					sc_start(bl,SC_MARIONETTE2,100,src->id,skill_get_time(skillid,skilllv));
  3551. 					clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3552. 				}
  3553. 				else
  3554. 				if(  sc->data[SC_MARIONETTE ] &&  sc->data[SC_MARIONETTE ]->val1 == bl->id &&
  3555. 					tsc->data[SC_MARIONETTE2] && tsc->data[SC_MARIONETTE2]->val1 == src->id )
  3556. 				{
  3557. 					status_change_end(src, SC_MARIONETTE, INVALID_TIMER);
  3558. 					status_change_end(bl, SC_MARIONETTE2, INVALID_TIMER);
  3559. 				}
  3560. 				else
  3561. 				{
  3562. 					if( sd )
  3563. 						clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3564.  
  3565. 					map_freeblock_unlock();
  3566. 					return 1;
  3567. 				}
  3568. 			}
  3569. 		}
  3570. 		break;
  3571.  
  3572. 	case RG_CLOSECONFINE:
  3573. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3574. 			sc_start4(bl,type,100,skilllv,src->id,0,0,skill_get_time(skillid,skilllv)));
  3575. 		break;
  3576. 	case SA_FLAMELAUNCHER:	// added failure chance and chance to break weapon if turned on [Valaris]
  3577. 	case SA_FROSTWEAPON:
  3578. 	case SA_LIGHTNINGLOADER:
  3579. 	case SA_SEISMICWEAPON:
  3580. 		if (dstsd) {
  3581. 			if(dstsd->status.weapon == W_FIST ||
  3582. 				(dstsd->sc.count && !dstsd->sc.data[type] &&
  3583. 				(	//Allow re-enchanting to lenghten time. [Skotlex]
  3584. 					dstsd->sc.data[SC_FIREWEAPON] ||
  3585. 					dstsd->sc.data[SC_WATERWEAPON] ||
  3586. 					dstsd->sc.data[SC_WINDWEAPON] ||
  3587. 					dstsd->sc.data[SC_EARTHWEAPON] ||
  3588. 					dstsd->sc.data[SC_SHADOWWEAPON] ||
  3589. 					dstsd->sc.data[SC_GHOSTWEAPON] ||
  3590. 					dstsd->sc.data[SC_ENCPOISON]
  3591. 				))
  3592. 				) {
  3593. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3594. 				clif_skill_nodamage(src,bl,skillid,skilllv,0);
  3595. 				break;
  3596. 			}
  3597. 		}
  3598. 		// 100% success rate at lv4 & 5, but lasts longer at lv5
  3599. 		if(!clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,type,(60+skilllv*10),skilllv, skill_get_time(skillid,skilllv)))) {
  3600. 			if (sd)
  3601. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3602. 			if (skill_break_equip(bl, EQP_WEAPON, 10000, BCT_PARTY) && sd && sd != dstsd)
  3603. 				clif_displaymessage(sd->fd,"You broke target's weapon");
  3604. 		}
  3605. 		break;
  3606.  
  3607. 	case PR_ASPERSIO:
  3608. 		if (sd && dstmd) {
  3609. 			clif_skill_nodamage(src,bl,skillid,skilllv,0);
  3610. 			break;
  3611. 		}
  3612. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3613. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3614. 		break;
  3615.  
  3616. 	case ITEM_ENCHANTARMS:
  3617. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3618. 			sc_start2(bl,type,100,skilllv,
  3619. 				skill_get_ele(skillid,skilllv), skill_get_time(skillid,skilllv)));
  3620. 		break;
  3621.  
  3622. 	case TK_SEVENWIND:
  3623. 		switch(skill_get_ele(skillid,skilllv)) {
  3624. 			case ELE_EARTH : type = SC_EARTHWEAPON;  break;
  3625. 			case ELE_WIND  : type = SC_WINDWEAPON;   break;
  3626. 			case ELE_WATER : type = SC_WATERWEAPON;  break;
  3627. 			case ELE_FIRE  : type = SC_FIREWEAPON;   break;
  3628. 			case ELE_GHOST : type = SC_GHOSTWEAPON;  break;
  3629. 			case ELE_DARK  : type = SC_SHADOWWEAPON; break;
  3630. 			case ELE_HOLY  : type = SC_ASPERSIO;     break;
  3631. 		}
  3632. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3633. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3634.  
  3635. 		sc_start(bl,SC_SEVENWIND,100,skilllv,skill_get_time(skillid,skilllv));
  3636.  
  3637. 		break;
  3638.  
  3639. 	case PR_KYRIE:
  3640. 	case MER_KYRIE:	
  3641. 		clif_skill_nodamage(bl,bl,skillid,skilllv,
  3642. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3643. 		break;
  3644. 	//Passive Magnum, should had been casted on yourself.
  3645. 	case SM_MAGNUM:
  3646. 	case MS_MAGNUM:
  3647. 		skill_area_temp[1] = 0;
  3648. 		map_foreachinrange(skill_area_sub, src, skill_get_splash(skillid, skilllv), BL_SKILL|BL_CHAR,
  3649. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1, skill_castend_damage_id);
  3650. 		clif_skill_nodamage (src,src,skillid,skilllv,1);
  3651. 		//Initiate 10% of your damage becomes fire element.
  3652. 		sc_start4(src,SC_WATK_ELEMENT,100,3,20,0,0,skill_get_time2(skillid, skilllv));
  3653. 		if (sd) skill_blockpc_start (sd, skillid, skill_get_time(skillid, skilllv));
  3654. 		break;
  3655.  
  3656. 	case AL_INCAGI:
  3657. 	case AL_BLESSING:
  3658. 	case MER_INCAGI:
  3659. 	case MER_BLESSING:
  3660. 		if (dstsd != NULL && tsc->data[SC_CHANGEUNDEAD]) {
  3661. 			skill_attack(BF_MISC,src,src,bl,skillid,skilllv,tick,flag);
  3662. 			break;
  3663. 		}
  3664. 	case PR_SLOWPOISON:
  3665. 	case PR_IMPOSITIO:
  3666. 	case PR_LEXAETERNA:
  3667. 	case PR_SUFFRAGIUM:
  3668. 	case PR_BENEDICTIO:
  3669. 	case LK_BERSERK:
  3670. 	case MS_BERSERK:
  3671. 	case KN_AUTOCOUNTER:
  3672. 	case KN_TWOHANDQUICKEN:
  3673. 	case KN_ONEHAND:
  3674. 	case MER_QUICKEN:
  3675. 	case CR_SPEARQUICKEN:
  3676. 	case CR_REFLECTSHIELD:
  3677. 	case MS_REFLECTSHIELD:
  3678. 	case AS_POISONREACT:
  3679. 	case MC_LOUD:
  3680. 	case MG_ENERGYCOAT:
  3681. 	case MO_EXPLOSIONSPIRITS:
  3682. 	case MO_STEELBODY:
  3683. 	case MO_BLADESTOP:
  3684. 	case LK_AURABLADE:
  3685. 	case LK_PARRYING:
  3686. 	case MS_PARRYING:
  3687. 	case LK_CONCENTRATION:
  3688. 	case WS_CARTBOOST:
  3689. 	case SN_SIGHT:
  3690. 	case WS_MELTDOWN:
  3691. 	case WS_OVERTHRUSTMAX:
  3692. 	case ST_REJECTSWORD:
  3693. 	case HW_MAGICPOWER:
  3694. 	case PF_MEMORIZE:
  3695. 	case PA_SACRIFICE:
  3696. 	case ASC_EDP:
  3697. 	case PF_DOUBLECASTING:
  3698. 	case SG_SUN_COMFORT:
  3699. 	case SG_MOON_COMFORT:
  3700. 	case SG_STAR_COMFORT:
  3701. 	case NPC_HALLUCINATION:
  3702. 	case GS_MADNESSCANCEL:
  3703. 	case GS_ADJUSTMENT:
  3704. 	case GS_INCREASING:
  3705. 	case NJ_KASUMIKIRI:
  3706. 	case NJ_UTSUSEMI:
  3707. 	case NJ_NEN:
  3708. 	case NPC_DEFENDER:
  3709. 	case NPC_MAGICMIRROR:
  3710. 	case ST_PRESERVE:
  3711. 	case NPC_INVINCIBLE:
  3712. 	case NPC_INVINCIBLEOFF:
  3713. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3714. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3715. 		break;
  3716. 	case NPC_STOP:
  3717. 		if( clif_skill_nodamage(src,bl,skillid,skilllv,
  3718. 			sc_start2(bl,type,100,skilllv,src->id,skill_get_time(skillid,skilllv)) ) )
  3719. 			sc_start2(src,type,100,skilllv,bl->id,skill_get_time(skillid,skilllv));
  3720. 		break;
  3721. 	case HP_ASSUMPTIO:
  3722. 		if( sd && dstmd )
  3723. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3724. 		else
  3725. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  3726. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3727. 		break;
  3728. 	case MG_SIGHT:
  3729. 	case MER_SIGHT:
  3730. 	case AL_RUWACH:
  3731. 	case WZ_SIGHTBLASTER:
  3732. 	case NPC_WIDESIGHT:
  3733. 	case NPC_STONESKIN:
  3734. 	case NPC_ANTIMAGIC:
  3735. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3736. 			sc_start2(bl,type,100,skilllv,skillid,skill_get_time(skillid,skilllv)));
  3737. 		break;
  3738. 	case HLIF_AVOID:
  3739. 	case HAMI_DEFENCE:
  3740. 		i = skill_get_time(skillid,skilllv);
  3741. 		clif_skill_nodamage(bl,bl,skillid,skilllv,sc_start(bl,type,100,skilllv,i)); // Master
  3742. 		clif_skill_nodamage(src,src,skillid,skilllv,sc_start(src,type,100,skilllv,i)); // Homunc
  3743. 		break;
  3744. 	case NJ_BUNSINJYUTSU:
  3745. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3746. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3747. 		status_change_end(bl, SC_NEN, INVALID_TIMER);
  3748. 		break;
  3749. /* Was modified to only affect targetted char.	[Skotlex]
  3750. 	case HP_ASSUMPTIO:
  3751. 		if (flag&1)
  3752. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  3753. 		else
  3754. 		{
  3755. 			map_foreachinrange(skill_area_sub, bl,
  3756. 				skill_get_splash(skillid, skilllv), BL_PC,
  3757. 				src, skillid, skilllv, tick, flag|BCT_ALL|1,
  3758. 				skill_castend_nodamage_id);
  3759. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3760. 		}
  3761. 		break;
  3762. */
  3763. 	case SM_ENDURE:
  3764. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3765. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3766. 		if (sd)
  3767. 			skill_blockpc_start (sd, skillid, skill_get_time2(skillid,skilllv));
  3768. 		break;
  3769.  
  3770. 	case AS_ENCHANTPOISON: // Prevent spamming [Valaris]
  3771. 		if (sd && dstsd && dstsd->sc.count) {
  3772. 			if (dstsd->sc.data[SC_FIREWEAPON] ||
  3773. 				dstsd->sc.data[SC_WATERWEAPON] ||
  3774. 				dstsd->sc.data[SC_WINDWEAPON] ||
  3775. 				dstsd->sc.data[SC_EARTHWEAPON] ||
  3776. 				dstsd->sc.data[SC_SHADOWWEAPON] ||
  3777. 				dstsd->sc.data[SC_GHOSTWEAPON]
  3778. 			//	dstsd->sc.data[SC_ENCPOISON] //People say you should be able to recast to lengthen the timer. [Skotlex]
  3779. 			) {
  3780. 					clif_skill_nodamage(src,bl,skillid,skilllv,0);
  3781. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3782. 					break;
  3783. 			}
  3784. 		}
  3785. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3786. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3787. 		break;
  3788.  
  3789. 	case LK_TENSIONRELAX:
  3790. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3791. 			sc_start4(bl,type,100,skilllv,0,0,skill_get_time2(skillid,skilllv),
  3792. 				skill_get_time(skillid,skilllv)));
  3793. 		break;
  3794.  
  3795. 	case MC_CHANGECART:
  3796. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3797. 		break;
  3798.  
  3799. 	case TK_MISSION:
  3800. 		if (sd) {
  3801. 			int id;
  3802. 			if (sd->mission_mobid && (sd->mission_count || rand()%100)) { //Cannot change target when already have one
  3803. 				clif_mission_info(sd, sd->mission_mobid, sd->mission_count);
  3804. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3805. 				break;
  3806. 			}
  3807. 			id = mob_get_random_id(0,0xE, sd->status.base_level);
  3808. 			if (!id) {
  3809. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3810. 				break;
  3811. 			}
  3812. 			sd->mission_mobid = id;
  3813. 			sd->mission_count = 0;
  3814. 			pc_setglobalreg(sd,"TK_MISSION_ID", id);
  3815. 			clif_mission_info(sd, id, 0);
  3816. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3817. 		}
  3818. 		break;
  3819.  
  3820. 	case AC_CONCENTRATION:
  3821. 		{
  3822. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  3823. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3824. 			map_foreachinrange( status_change_timer_sub, src,
  3825. 				skill_get_splash(skillid, skilllv), BL_CHAR,
  3826. 				src,NULL,type,tick);
  3827. 		}
  3828. 		break;
  3829.  
  3830. 	case SM_PROVOKE:
  3831. 	case SM_SELFPROVOKE:
  3832. 	case MER_PROVOKE:
  3833. 		if( (tstatus->mode&MD_BOSS) || battle_check_undead(tstatus->race,tstatus->def_ele) )
  3834. 		{
  3835. 			map_freeblock_unlock();
  3836. 			return 1;
  3837. 		}
  3838. 		//TODO: How much does base level affects? Dummy value of 1% per level difference used. [Skotlex]
  3839. 		clif_skill_nodamage(src,bl,skillid == SM_SELFPROVOKE ? SM_PROVOKE : skillid,skilllv,
  3840. 			(i = sc_start(bl,type, skillid == SM_SELFPROVOKE ? 100:( 50 + 3*skilllv + status_get_lv(src) - status_get_lv(bl)), skilllv, skill_get_time(skillid,skilllv))));
  3841. 		if( !i )
  3842. 		{
  3843. 			if( sd )
  3844. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3845. 			map_freeblock_unlock();
  3846. 			return 0;
  3847. 		}
  3848. 		unit_skillcastcancel(bl, 2);
  3849.  
  3850. 		if( tsc && tsc->count )
  3851. 		{
  3852. 			status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  3853. 			if( tsc->data[SC_STONE] && tsc->opt1 == OPT1_STONE )
  3854. 				status_change_end(bl, SC_STONE, INVALID_TIMER);
  3855. 			status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  3856. 		}
  3857.  
  3858. 		if( dstmd )
  3859. 		{
  3860. 			dstmd->state.provoke_flag = src->id;
  3861. 			mob_target(dstmd, src, skill_get_range2(src,skillid,skilllv));
  3862. 		}
  3863. 		break;
  3864.  
  3865. 	case ML_DEVOTION:
  3866. 	case CR_DEVOTION:
  3867. 		{
  3868. 			int count, lv;
  3869. 			if( !dstsd || (!sd && !mer) )
  3870. 			{ // Only players can be devoted
  3871. 				if( sd )
  3872. 					clif_skill_fail(sd, skillid, USESKILL_FAIL_LEVEL, 0);
  3873. 				break;
  3874. 			}
  3875.  
  3876. 			if( (lv = status_get_lv(src) - dstsd->status.base_level) < 0 )
  3877. 				lv = -lv;
  3878. 			if( lv > battle_config.devotion_level_difference || // Level difference requeriments
  3879. 				(dstsd->sc.data[type] && dstsd->sc.data[type]->val1 != src->id) || // Cannot Devote a player devoted from another source
  3880. 				(skillid == ML_DEVOTION && (!mer || mer != dstsd->md)) || // Mercenary only can devote owner
  3881. 				(dstsd->class_&MAPID_UPPERMASK) == MAPID_CRUSADER || // Crusader Cannot be devoted
  3882. 				(dstsd->sc.data[SC_HELLPOWER])) // Players affected by SC_HELLPOWERR cannot be devoted.
  3883. 			{
  3884. 				if( sd )
  3885. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3886. 				map_freeblock_unlock();
  3887. 				return 1;
  3888. 			}
  3889.  
  3890. 			i = 0;
  3891. 			count = (sd)? min(skilllv,5) : 1; // Mercenary only can Devote owner
  3892. 			if( sd )
  3893. 			{ // Player Devoting Player
  3894. 				ARR_FIND(0, count, i, sd->devotion[i] == bl->id );
  3895. 				if( i == count )
  3896. 				{
  3897. 					ARR_FIND(0, count, i, sd->devotion[i] == 0 );
  3898. 					if( i == count )
  3899. 					{ // No free slots, skill Fail
  3900. 						clif_skill_fail(sd, skillid, USESKILL_FAIL_LEVEL, 0);
  3901. 						map_freeblock_unlock();
  3902. 						return 1;
  3903. 					}
  3904. 				}
  3905.  
  3906. 				sd->devotion[i] = bl->id;
  3907. 			}
  3908. 			else
  3909. 				mer->devotion_flag = 1; // Mercenary Devoting Owner
  3910.  
  3911. 			clif_skill_nodamage(src, bl, skillid, skilllv,
  3912. 				sc_start4(bl, type, 100, src->id, i, skill_get_range2(src,skillid,skilllv),0, skill_get_time2(skillid, skilllv)));
  3913. 			clif_devotion(src, NULL);
  3914. 		}
  3915. 		break;
  3916.  
  3917. 	case MO_CALLSPIRITS:
  3918. 		if(sd) {
  3919. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3920. 			pc_addspiritball(sd,skill_get_time(skillid,skilllv),skilllv);
  3921. 		}
  3922. 		break;
  3923.  
  3924. 	case CH_SOULCOLLECT:
  3925. 		if(sd) {
  3926. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3927. 			for (i = 0; i < 5; i++)
  3928. 				pc_addspiritball(sd,skill_get_time(skillid,skilllv),5);
  3929. 		}
  3930. 		break;
  3931.  
  3932. 	case MO_KITRANSLATION:
  3933. 		if(dstsd && (dstsd->class_&MAPID_BASEMASK)!=MAPID_GUNSLINGER) {
  3934. 			pc_addspiritball(dstsd,skill_get_time(skillid,skilllv),5);
  3935. 		}
  3936. 		break;
  3937.  
  3938. 	case TK_TURNKICK:
  3939. 	case MO_BALKYOUNG: //Passive part of the attack. Splash knock-back+stun. [Skotlex]
  3940. 		if (skill_area_temp[1] != bl->id) {
  3941. 			skill_blown(src,bl,skill_get_blewcount(skillid,skilllv),-1,0);
  3942. 			skill_additional_effect(src,bl,skillid,skilllv,BF_MISC,ATK_DEF,tick); //Use Misc rather than weapon to signal passive pushback
  3943. 		}
  3944. 		break;
  3945.  
  3946. 	case MO_ABSORBSPIRITS:
  3947. 		i = 0;
  3948. 		if (dstsd && dstsd->spiritball && (sd == dstsd || map_flag_vs(src->m)) && (dstsd->class_&MAPID_BASEMASK)!=MAPID_GUNSLINGER)
  3949. 		{	// split the if for readability, and included gunslingers in the check so that their coins cannot be removed [Reddozen]
  3950. 			i = dstsd->spiritball * 7;
  3951. 			pc_delspiritball(dstsd,dstsd->spiritball,0);
  3952. 		} else if (dstmd && !(tstatus->mode&MD_BOSS) && rand() % 100 < 20)
  3953. 		{	// check if target is a monster and not a Boss, for the 20% chance to absorb 2 SP per monster's level [Reddozen]
  3954. 			i = 2 * dstmd->level;
  3955. 			mob_target(dstmd,src,0);
  3956. 		}
  3957. 		if (i) status_heal(src, 0, i, 3);
  3958. 		clif_skill_nodamage(src,bl,skillid,skilllv,i?1:0);
  3959. 		break;
  3960.  
  3961. 	case AC_MAKINGARROW:
  3962. 		if(sd) {
  3963. 			clif_arrow_create_list(sd);
  3964. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3965. 		}
  3966. 		break;
  3967.  
  3968. 	case AM_PHARMACY:
  3969. 		if(sd) {
  3970. 			clif_skill_produce_mix_list(sd,22);
  3971. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3972. 		}
  3973. 		break;
  3974.  
  3975. 	case SA_CREATECON:
  3976. 		if(sd) {
  3977. 			clif_skill_produce_mix_list(sd,23);
  3978. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3979. 		}
  3980. 		break;
  3981.  
  3982. 	case BS_HAMMERFALL:
  3983. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3984. 			sc_start(bl,SC_STUN,(20 + 10 * skilllv),skilllv,skill_get_time2(skillid,skilllv)));
  3985. 		break;
  3986. 	case RG_RAID:
  3987. 		skill_area_temp[1] = 0;
  3988. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3989. 		map_foreachinrange(skill_area_sub, bl,
  3990. 			skill_get_splash(skillid, skilllv), splash_target(src),
  3991. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  3992. 			skill_castend_damage_id);
  3993. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  3994. 		break;
  3995.  
  3996. 	case ASC_METEORASSAULT:
  3997. 	case GS_SPREADATTACK:
  3998. 		skill_area_temp[1] = 0;
  3999. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4000. 		map_foreachinrange(skill_area_sub, bl, skill_get_splash(skillid, skilllv), splash_target(src), 
  4001. 			src, skillid, skilllv, tick, flag|BCT_ENEMY|SD_SPLASH|1, skill_castend_damage_id);
  4002. 		break;
  4003.  
  4004. 	case NPC_EARTHQUAKE:
  4005. 	case NPC_VAMPIRE_GIFT:
  4006. 	case NPC_HELLJUDGEMENT:
  4007. 	case NPC_PULSESTRIKE:
  4008. 		skill_castend_damage_id(src, src, skillid, skilllv, tick, flag);
  4009. 		break;
  4010.  
  4011. 	case KN_BRANDISHSPEAR:
  4012. 	case ML_BRANDISH:
  4013. 		skill_brandishspear(src, bl, skillid, skilllv, tick, flag);
  4014. 		break;
  4015.  
  4016. 	case WZ_SIGHTRASHER:
  4017. 		//Passive side of the attack.
  4018. 		status_change_end(src, SC_SIGHT, INVALID_TIMER);
  4019. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4020. 		map_foreachinrange(skill_area_sub,src,
  4021. 			skill_get_splash(skillid, skilllv),BL_CHAR|BL_SKILL,
  4022. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  4023. 			skill_castend_damage_id);
  4024. 		break;
  4025.  
  4026. 	case NJ_HYOUSYOURAKU:
  4027. 	case NJ_RAIGEKISAI:
  4028. 	case WZ_FROSTNOVA:
  4029. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4030. 		skill_area_temp[1] = 0;
  4031. 		map_foreachinrange(skill_attack_area, src,
  4032. 			skill_get_splash(skillid, skilllv), splash_target(src),
  4033. 			BF_MAGIC, src, src, skillid, skilllv, tick, flag, BCT_ENEMY);
  4034. 		break;
  4035.  
  4036. 	case HVAN_EXPLOSION:	//[orn]
  4037. 	case NPC_SELFDESTRUCTION:
  4038. 		//Self Destruction hits everyone in range (allies+enemies)
  4039. 		//Except for Summoned Marine spheres on non-versus maps, where it's just enemy.
  4040. 		i = ((!md || md->special_state.ai == 2) && !map_flag_vs(src->m))?
  4041. 			BCT_ENEMY:BCT_ALL;
  4042. 		clif_skill_nodamage(src, src, skillid, -1, 1);
  4043. 		map_delblock(src); //Required to prevent chain-self-destructions hitting back.
  4044. 		map_foreachinrange(skill_area_sub, bl,
  4045. 			skill_get_splash(skillid, skilllv), splash_target(src),
  4046. 			src, skillid, skilllv, tick, flag|i,
  4047. 			skill_castend_damage_id);
  4048. 		map_addblock(src);
  4049. 		status_damage(src, src, sstatus->max_hp,0,0,1);
  4050. 		break;
  4051.  
  4052. 	case AL_ANGELUS:
  4053. 	case PR_MAGNIFICAT:
  4054. 	case PR_GLORIA:
  4055. 	case SN_WINDWALK:
  4056. 	case CASH_BLESSING:
  4057. 	case CASH_INCAGI:
  4058. 	case CASH_ASSUMPTIO:
  4059. 		if( sd == NULL || sd->status.party_id == 0 || (flag & 1) )
  4060. 			clif_skill_nodamage(bl, bl, skillid, skilllv, sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4061. 		else if( sd )
  4062. 			party_foreachsamemap(skill_area_sub, sd, skill_get_splash(skillid, skilllv), src, skillid, skilllv, tick, flag|BCT_PARTY|1, skill_castend_nodamage_id);
  4063. 		break;
  4064. 	case MER_MAGNIFICAT:
  4065. 		if( mer != NULL )
  4066. 		{
  4067. 			clif_skill_nodamage(bl, bl, skillid, skilllv, sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4068. 			if( mer->master && mer->master->status.party_id != 0 && !(flag&1) )
  4069. 				party_foreachsamemap(skill_area_sub, mer->master, skill_get_splash(skillid, skilllv), src, skillid, skilllv, tick, flag|BCT_PARTY|1, skill_castend_nodamage_id);
  4070. 			else if( mer->master && !(flag&1) )
  4071. 				clif_skill_nodamage(src, &mer->master->bl, skillid, skilllv, sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4072. 		}
  4073. 		break;
  4074.  
  4075. 	case BS_ADRENALINE:
  4076. 	case BS_ADRENALINE2:
  4077. 	case BS_WEAPONPERFECT:
  4078. 	case BS_OVERTHRUST:
  4079. 		if (sd == NULL || sd->status.party_id == 0 || (flag & 1)) {
  4080. 			clif_skill_nodamage(bl,bl,skillid,skilllv,
  4081. 				sc_start2(bl,type,100,skilllv,(src == bl)? 1:0,skill_get_time(skillid,skilllv)));
  4082. 		} else if (sd) {
  4083. 			party_foreachsamemap(skill_area_sub,
  4084. 				sd,skill_get_splash(skillid, skilllv),
  4085. 				src,skillid,skilllv,tick, flag|BCT_PARTY|1,
  4086. 				skill_castend_nodamage_id);
  4087. 		}
  4088. 		break;
  4089.  
  4090. 	case BS_MAXIMIZE:
  4091. 	case NV_TRICKDEAD:
  4092. 	case CR_DEFENDER:
  4093. 	case ML_DEFENDER:
  4094. 	case CR_AUTOGUARD:
  4095. 	case ML_AUTOGUARD:
  4096. 	case TK_READYSTORM:
  4097. 	case TK_READYDOWN:
  4098. 	case TK_READYTURN:
  4099. 	case TK_READYCOUNTER:
  4100. 	case TK_DODGE:
  4101. 	case CR_SHRINK:
  4102. 	case SG_FUSION:
  4103. 	case GS_GATLINGFEVER:
  4104. 		if( tsce )
  4105. 		{
  4106. 			clif_skill_nodamage(src,bl,skillid,skilllv,status_change_end(bl, type, INVALID_TIMER));
  4107. 			map_freeblock_unlock();
  4108. 			return 0;
  4109. 		}
  4110. 		clif_skill_nodamage(src,bl,skillid,skilllv,sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4111. 		break;
  4112. 	case SL_KAITE:
  4113. 	case SL_KAAHI:
  4114. 	case SL_KAIZEL:
  4115. 	case SL_KAUPE:
  4116. 		if (sd) {
  4117. 			if (!dstsd || !(
  4118. 				(sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SOULLINKER) ||
  4119. 				(dstsd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER ||
  4120. 				dstsd->status.char_id == sd->status.char_id ||
  4121. 				dstsd->status.char_id == sd->status.partner_id ||
  4122. 				dstsd->status.char_id == sd->status.child
  4123. 			)) {
  4124. 				status_change_start(src,SC_STUN,10000,skilllv,0,0,0,500,8);
  4125. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4126. 				break;
  4127. 			}
  4128. 		}
  4129. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4130. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv)));
  4131. 		break;
  4132. 	case SM_AUTOBERSERK:
  4133. 	case MER_AUTOBERSERK:
  4134. 		if( tsce )
  4135. 			i = status_change_end(bl, type, INVALID_TIMER);
  4136. 		else
  4137. 			i = sc_start(bl,type,100,skilllv,60000);
  4138. 		clif_skill_nodamage(src,bl,skillid,skilllv,i);
  4139. 		break;
  4140. 	case TF_HIDING:
  4141. 	case ST_CHASEWALK:
  4142. 		if (tsce)
  4143. 		{
  4144. 			clif_skill_nodamage(src,bl,skillid,-1,status_change_end(bl, type, INVALID_TIMER)); //Hide skill-scream animation.
  4145. 			map_freeblock_unlock();
  4146. 			return 0;
  4147. 		}
  4148. 		clif_skill_nodamage(src,bl,skillid,-1,sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));		
  4149. 		break;
  4150. 	case TK_RUN:
  4151. 		if (tsce)
  4152. 		{
  4153. 			clif_skill_nodamage(src,bl,skillid,skilllv,status_change_end(bl, type, INVALID_TIMER));
  4154. 			map_freeblock_unlock();
  4155. 			return 0;
  4156. 		}
  4157. 		clif_skill_nodamage(src,bl,skillid,skilllv,sc_start4(bl,type,100,skilllv,unit_getdir(bl),0,0,0));
  4158. 		if (sd) // If the client receives a skill-use packet inmediately before a walkok packet, it will discard the walk packet! [Skotlex]
  4159. 			clif_walkok(sd); // So aegis has to resend the walk ok.
  4160. 		break;
  4161. 	case AS_CLOAKING:
  4162. 		if (tsce)
  4163. 		{
  4164. 			i = status_change_end(bl, type, INVALID_TIMER);
  4165. 			if( i )
  4166. 				clif_skill_nodamage(src,bl,skillid,-1,i);
  4167. 			else if( sd )
  4168. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4169. 			map_freeblock_unlock();
  4170. 			return 0;
  4171. 		}
  4172. 		i = sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  4173. 		if( i )
  4174. 			clif_skill_nodamage(src,bl,skillid,-1,i);
  4175. 		else if( sd )
  4176. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4177. 		break;
  4178.  
  4179. 	case BD_ADAPTATION:
  4180. 		if(tsc && tsc->data[SC_DANCING]){
  4181. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4182. 			status_change_end(bl, SC_DANCING, INVALID_TIMER);
  4183. 		}
  4184. 		break;
  4185.  
  4186. 	case BA_FROSTJOKER:
  4187. 	case DC_SCREAM:
  4188. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4189. 		skill_addtimerskill(src,tick+2000,bl->id,src->x,src->y,skillid,skilllv,0,flag);
  4190.  
  4191. 		if (md) {
  4192. 			// custom hack to make the mob display the skill, because these skills don't show the skill use text themselves
  4193. 			//NOTE: mobs don't have the sprite animation that is used when performing this skill (will cause glitches)
  4194. 			char temp[70];
  4195. 			snprintf(temp, sizeof(temp), "%s : %s !!",md->name,skill_db[skillid].desc);
  4196. 			clif_message(&md->bl,temp);
  4197. 		}
  4198. 		break;
  4199.  
  4200. 	case BA_PANGVOICE:
  4201. 		clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,SC_CONFUSION,50,7,skill_get_time(skillid,skilllv)));
  4202. 		break;
  4203.  
  4204. 	case DC_WINKCHARM:
  4205. 		if( dstsd )
  4206. 			clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,SC_CONFUSION,30,7,skill_get_time2(skillid,skilllv)));
  4207. 		else
  4208. 		if( dstmd )
  4209. 		{
  4210. 			if( status_get_lv(src) > status_get_lv(bl)
  4211. 			&&  (tstatus->race == RC_DEMON || tstatus->race == RC_DEMIHUMAN || tstatus->race == RC_ANGEL)
  4212. 			&&  !(tstatus->mode&MD_BOSS) )
  4213. 				clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,type,70,skilllv,skill_get_time(skillid,skilllv)));
  4214. 			else
  4215. 			{
  4216. 				clif_skill_nodamage(src,bl,skillid,skilllv,0);
  4217. 				if(sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4218. 			}
  4219. 		}
  4220. 		break;
  4221.  
  4222. 	case TF_STEAL:
  4223. 		if(sd) {
  4224. 			if(pc_steal_item(sd,bl,skilllv))
  4225. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4226. 			else
  4227. 				clif_skill_fail(sd,skillid,USESKILL_FAIL,0);
  4228. 		}
  4229. 		break;
  4230.  
  4231. 	case RG_STEALCOIN:
  4232. 		if(sd) {
  4233. 			if(pc_steal_coin(sd,bl))
  4234. 			{
  4235. 				dstmd->state.provoke_flag = src->id;
  4236. 				mob_target(dstmd, src, skill_get_range2(src,skillid,skilllv));
  4237. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4238.  
  4239. 			} 
  4240. 			else
  4241. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4242. 		}
  4243. 		break;
  4244.  
  4245. 	case MG_STONECURSE:
  4246. 		{
  4247. 			if (tstatus->mode&MD_BOSS) {
  4248. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4249. 				break;
  4250. 			}
  4251. 			if(status_isimmune(bl) || !tsc)
  4252. 				break;
  4253.  
  4254. 			if (tsc->data[SC_STONE]) {
  4255. 				status_change_end(bl, SC_STONE, INVALID_TIMER);
  4256. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4257. 				break;
  4258. 			}
  4259. 			if (sc_start4(bl,SC_STONE,(skilllv*4+20),
  4260. 				skilllv, 0, 0, skill_get_time(skillid, skilllv),
  4261. 				skill_get_time2(skillid,skilllv)))
  4262. 					clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4263. 			else if(sd) {
  4264. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4265. 				// Level 6-10 doesn't consume a red gem if it fails [celest]
  4266. 				if (skilllv > 5)
  4267. 				{ // not to consume items
  4268. 					map_freeblock_unlock();
  4269. 					return 0;
  4270. 				}
  4271. 			}
  4272. 		}
  4273. 		break;
  4274.  
  4275. 	case NV_FIRSTAID:
  4276. 		clif_skill_nodamage(src,bl,skillid,5,1);
  4277. 		status_heal(bl,5,0,0);
  4278. 		break;
  4279.  
  4280. 	case AL_CURE:
  4281. 		if(status_isimmune(bl)) {
  4282. 			clif_skill_nodamage(src,bl,skillid,skilllv,0);
  4283. 			break;
  4284. 		}
  4285. 		status_change_end(bl, SC_SILENCE, INVALID_TIMER);
  4286. 		status_change_end(bl, SC_BLIND, INVALID_TIMER);
  4287. 		status_change_end(bl, SC_CONFUSION, INVALID_TIMER);
  4288. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4289. 		break;
  4290.  
  4291. 	case TF_DETOXIFY:
  4292. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4293. 		status_change_end(bl, SC_POISON, INVALID_TIMER);
  4294. 		status_change_end(bl, SC_DPOISON, INVALID_TIMER);
  4295. 		break;
  4296.  
  4297. 	case PR_STRECOVERY:
  4298. 		if(status_isimmune(bl)) {
  4299. 			clif_skill_nodamage(src,bl,skillid,skilllv,0);
  4300. 			break;
  4301. 		}
  4302. 		if (tsc && tsc->opt1) {
  4303. 			status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  4304. 			status_change_end(bl, SC_STONE, INVALID_TIMER);
  4305. 			status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  4306. 			status_change_end(bl, SC_STUN, INVALID_TIMER);
  4307. 		}
  4308. 		//Is this equation really right? It looks so... special.
  4309. 		if(battle_check_undead(tstatus->race,tstatus->def_ele))
  4310. 		{
  4311. 			status_change_start(bl, SC_BLIND,
  4312. 				100*(100-(tstatus->int_/2+tstatus->vit/3+tstatus->luk/10)),
  4313. 				1,0,0,0,
  4314. 				skill_get_time2(skillid, skilllv) * (100-(tstatus->int_+tstatus->vit)/2)/100,10);
  4315. 		}
  4316. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4317. 		if(dstmd)
  4318. 			mob_unlocktarget(dstmd,tick);
  4319. 		break;
  4320.  
  4321. 	// Mercenary Supportive Skills
  4322. 	case MER_BENEDICTION:
  4323. 		status_change_end(bl, SC_CURSE, INVALID_TIMER);
  4324. 		status_change_end(bl, SC_BLIND, INVALID_TIMER);
  4325. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4326. 		break;
  4327. 	case MER_COMPRESS:
  4328. 		status_change_end(bl, SC_BLEEDING, INVALID_TIMER);
  4329. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4330. 		break;
  4331. 	case MER_MENTALCURE:
  4332. 		status_change_end(bl, SC_CONFUSION, INVALID_TIMER);
  4333. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4334. 		break;
  4335. 	case MER_RECUPERATE:
  4336. 		status_change_end(bl, SC_POISON, INVALID_TIMER);
  4337. 		status_change_end(bl, SC_SILENCE, INVALID_TIMER);
  4338. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4339. 		break;
  4340. 	case MER_REGAIN:
  4341. 		status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  4342. 		status_change_end(bl, SC_STUN, INVALID_TIMER);
  4343. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4344. 		break;
  4345. 	case MER_TENDER:
  4346. 		status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  4347. 		status_change_end(bl, SC_STONE, INVALID_TIMER);
  4348. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4349. 		break;
  4350.  
  4351. 	case MER_SCAPEGOAT:
  4352. 		if( mer && mer->master )
  4353. 		{
  4354. 			status_heal(&mer->master->bl, mer->battle_status.hp, 0, 2);
  4355. 			status_damage(src, src, mer->battle_status.max_hp, 0, 0, 1);
  4356. 		}
  4357. 		break;
  4358.  
  4359. 	case MER_ESTIMATION:
  4360. 		if( !mer )
  4361. 			break;
  4362. 		sd = mer->master;
  4363. 	case WZ_ESTIMATION:
  4364. 		if( sd == NULL )
  4365. 			break;
  4366. 		if( dstsd )
  4367. 		{ // Fail on Players
  4368. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4369. 			break;
  4370. 		}
  4371. 		if( dstmd && dstmd->class_ == MOBID_EMPERIUM )
  4372. 			break; // Cannot be Used on Emperium
  4373.  
  4374. 		clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  4375. 		clif_skill_estimation(sd, bl);
  4376. 		if( skillid == MER_ESTIMATION )
  4377. 			sd = NULL;
  4378. 		break;
  4379.  
  4380. 	case BS_REPAIRWEAPON:
  4381. 		if(sd && dstsd)
  4382. 			clif_item_repair_list(sd,dstsd);
  4383. 		break;
  4384.  
  4385. 	case MC_IDENTIFY:
  4386. 		if(sd)
  4387. 			clif_item_identify_list(sd);
  4388. 		break;
  4389.  
  4390. 	// Weapon Refining [Celest]
  4391. 	case WS_WEAPONREFINE:
  4392. 		if(sd)
  4393. 			clif_item_refine_list(sd);
  4394. 		break;
  4395.  
  4396. 	case MC_VENDING:
  4397. 		if(sd)
  4398. 		{	//Prevent vending of GMs with unnecessary Level to trade/drop. [Skotlex]
  4399. 			if ( !pc_can_give_items(pc_isGM(sd)) )
  4400. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4401. 			else
  4402. 				clif_openvendingreq(sd,2+skilllv);
  4403. 		}
  4404. 		break;
  4405.  
  4406. 	case AL_TELEPORT:
  4407. 		if(sd)
  4408. 		{
  4409. 			if (map[bl->m].flag.noteleport && skilllv <= 2) {
  4410. 				clif_skill_teleportmessage(sd,0);
  4411. 				break;
  4412. 			}
  4413. 			if(!battle_config.duel_allow_teleport && sd->duel_group && skilllv <= 2) { // duel restriction [LuzZza]
  4414. 				clif_displaymessage(sd->fd, "Duel: Can't use teleport in duel.");
  4415. 				break;
  4416. 			}
  4417.  
  4418. 			if( sd->state.autocast || ( (sd->skillitem == AL_TELEPORT || battle_config.skip_teleport_lv1_menu) && skilllv == 1 ) || skilllv == 3 )
  4419. 			{
  4420. 				if( skilllv == 1 )
  4421. 					pc_randomwarp(sd,CLR_TELEPORT);
  4422. 				else
  4423. 					pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4424. 				break;
  4425. 			}
  4426.  
  4427. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4428. 			if( skilllv == 1 )
  4429. 				clif_skill_warppoint(sd,skillid,skilllv, (unsigned short)-1,0,0,0);
  4430. 			else
  4431. 				clif_skill_warppoint(sd,skillid,skilllv, (unsigned short)-1,sd->status.save_point.map,0,0);
  4432. 		} else
  4433. 			unit_warp(bl,-1,-1,-1,CLR_TELEPORT);
  4434. 		break;
  4435.  
  4436. 	case NPC_EXPULSION:
  4437. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4438. 		unit_warp(bl,-1,-1,-1,CLR_TELEPORT);
  4439. 		break;
  4440.  
  4441. 	case AL_HOLYWATER:
  4442. 		if(sd) {
  4443. 			if (skill_produce_mix(sd, skillid, 523, 0, 0, 0, 1))
  4444. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4445. 			else
  4446. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4447. 		}
  4448. 		break;
  4449.  
  4450. 	case TF_PICKSTONE:
  4451. 		if(sd) {
  4452. 			int eflag;
  4453. 			struct item item_tmp;
  4454. 			struct block_list tbl;
  4455. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4456. 			memset(&item_tmp,0,sizeof(item_tmp));
  4457. 			memset(&tbl,0,sizeof(tbl)); // [MouseJstr]
  4458. 			item_tmp.nameid = ITEMID_STONE;
  4459. 			item_tmp.identify = 1;
  4460. 			tbl.id = 0;
  4461. 			clif_takeitem(&sd->bl,&tbl);
  4462. 			eflag = pc_additem(sd,&item_tmp,1);
  4463. 			if(eflag) {
  4464. 				clif_additem(sd,0,0,eflag);
  4465. 				map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4466. 			}
  4467. 		}
  4468. 		break;
  4469. 	case ASC_CDP:
  4470.      	if(sd) {
  4471. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4472. 			skill_produce_mix(sd, skillid, 678, 0, 0, 0, 1); //Produce a Deadly Poison Bottle.
  4473. 		}
  4474. 		break;
  4475.  
  4476. 	case RG_STRIPWEAPON:
  4477. 	case RG_STRIPSHIELD:
  4478. 	case RG_STRIPARMOR:
  4479. 	case RG_STRIPHELM:
  4480. 	case ST_FULLSTRIP:
  4481. 	{
  4482. 		unsigned short location = 0;
  4483. 		int d = 0;
  4484.  
  4485. 		//Rate in percent
  4486. 		if ( skillid == ST_FULLSTRIP ) {
  4487. 			i = 5 + 2*skilllv + (sstatus->dex - tstatus->dex)/5;
  4488. 		} else {
  4489. 			i = 5 + 5*skilllv + (sstatus->dex - tstatus->dex)/5;
  4490. 		}
  4491. 		if (i < 5) i = 5; //Minimum rate 5%
  4492.  
  4493. 		//Duration in ms
  4494. 		d = skill_get_time(skillid,skilllv) + (sstatus->dex - tstatus->dex)*500;
  4495. 		if (d < 0) d = 0; //Minimum duration 0ms
  4496.  
  4497. 		switch (skillid) {
  4498. 		case RG_STRIPWEAPON:
  4499. 			location = EQP_WEAPON;
  4500. 			break;
  4501. 		case RG_STRIPSHIELD:
  4502. 			location = EQP_SHIELD;
  4503. 			break;
  4504. 		case RG_STRIPARMOR:
  4505. 			location = EQP_ARMOR;
  4506. 			break;
  4507. 		case RG_STRIPHELM:
  4508. 			location = EQP_HELM;
  4509. 			break;
  4510. 		case ST_FULLSTRIP:
  4511. 			location = EQP_WEAPON|EQP_SHIELD|EQP_ARMOR|EQP_HELM;
  4512. 			break;
  4513. 		}
  4514.  
  4515. 		//Special message when trying to use strip on FCP [Jobbie]
  4516. 		if( sd && skillid == ST_FULLSTRIP && tsc && tsc->data[SC_CP_WEAPON] && tsc->data[SC_CP_HELM] && tsc->data[SC_CP_ARMOR] && tsc->data[SC_CP_SHIELD] )
  4517. 		{
  4518. 			clif_gospel_info(sd, 0x28);
  4519. 			break;
  4520. 		}
  4521.  
  4522. 		//Attempts to strip at rate i and duration d
  4523. 		if( (i = skill_strip_equip(bl, location, i, skilllv, d)) || skillid != ST_FULLSTRIP )
  4524. 			clif_skill_nodamage(src,bl,skillid,skilllv,i); 
  4525.  
  4526. 		//Nothing stripped.
  4527. 		if( sd && !i )
  4528. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4529. 	}
  4530. 		break;
  4531.  
  4532. 	case AM_BERSERKPITCHER:
  4533. 	case AM_POTIONPITCHER:
  4534. 		{
  4535. 			int i,x,hp = 0,sp = 0,bonus=100;
  4536. 			if( dstmd && dstmd->class_ == MOBID_EMPERIUM )
  4537. 			{
  4538. 				map_freeblock_unlock();
  4539. 				return 1;
  4540. 			}
  4541. 			if( sd )
  4542. 			{
  4543. 				x = skilllv%11 - 1;
  4544. 				i = pc_search_inventory(sd,skill_db[skillid].itemid[x]);
  4545. 				if(i < 0 || skill_db[skillid].itemid[x] <= 0)
  4546. 				{
  4547. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4548. 					map_freeblock_unlock();
  4549. 					return 1;
  4550. 				}
  4551. 				if(sd->inventory_data[i] == NULL || sd->status.inventory[i].amount < skill_db[skillid].amount[x])
  4552. 				{
  4553. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4554. 					map_freeblock_unlock();
  4555. 					return 1;
  4556. 				}
  4557. 				if( skillid == AM_BERSERKPITCHER )
  4558. 				{
  4559. 					if( dstsd && dstsd->status.base_level < (unsigned int)sd->inventory_data[i]->elv )
  4560. 					{
  4561. 						clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4562. 						map_freeblock_unlock();
  4563. 						return 1;
  4564. 					}
  4565. 				}
  4566. 				potion_flag = 1;
  4567. 				potion_hp = potion_sp = potion_per_hp = potion_per_sp = 0;
  4568. 				potion_target = bl->id;
  4569. 				run_script(sd->inventory_data[i]->script,0,sd->bl.id,0);
  4570. 				potion_flag = potion_target = 0;
  4571. 				if( sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ALCHEMIST )
  4572. 					bonus += sd->status.base_level;
  4573. 				if( potion_per_hp > 0 || potion_per_sp > 0 )
  4574. 				{
  4575. 					hp = tstatus->max_hp * potion_per_hp / 100;
  4576. 					hp = hp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4577. 					if( dstsd )
  4578. 					{
  4579. 						sp = dstsd->status.max_sp * potion_per_sp / 100;
  4580. 						sp = sp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4581. 					}
  4582. 				}
  4583. 				else
  4584. 				{
  4585. 					if( potion_hp > 0 )
  4586. 					{
  4587. 						hp = potion_hp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4588. 						hp = hp * (100 + (tstatus->vit<<1)) / 100;
  4589. 						if( dstsd )
  4590. 							hp = hp * (100 + pc_checkskill(dstsd,SM_RECOVERY)*10) / 100;
  4591. 					}
  4592. 					if( potion_sp > 0 )
  4593. 					{
  4594. 						sp = potion_sp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4595. 						sp = sp * (100 + (tstatus->int_<<1)) / 100;
  4596. 						if( dstsd )
  4597. 							sp = sp * (100 + pc_checkskill(dstsd,MG_SRECOVERY)*10) / 100;
  4598. 					}
  4599. 				}
  4600.  
  4601. 				if (sd->itemgrouphealrate[IG_POTION]>0)
  4602. 				{
  4603. 					hp += hp * sd->itemgrouphealrate[IG_POTION] / 100;
  4604. 					sp += sp * sd->itemgrouphealrate[IG_POTION] / 100;
  4605. 				}
  4606.  
  4607. 				if( (i = pc_skillheal_bonus(sd, skillid)) )
  4608. 				{
  4609. 					hp += hp * i / 100;
  4610. 					sp += sp * i / 100;
  4611. 				}
  4612. 			}
  4613. 			else
  4614. 			{
  4615. 				hp = (1 + rand()%400) * (100 + skilllv*10) / 100;
  4616. 				hp = hp * (100 + (tstatus->vit<<1)) / 100;
  4617. 				if( dstsd )
  4618. 					hp = hp * (100 + pc_checkskill(dstsd,SM_RECOVERY)*10) / 100;
  4619. 			}
  4620. 			if( dstsd && (i = pc_skillheal2_bonus(dstsd, skillid)) )
  4621. 			{
  4622. 				hp += hp * i / 100;
  4623. 				sp += sp * i / 100;
  4624. 			}
  4625. 			if( tsc && tsc->data[SC_CRITICALWOUND] )
  4626. 			{
  4627. 				hp -= hp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  4628. 				sp -= sp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  4629. 			}
  4630. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4631. 			if( hp > 0 || (skillid == AM_POTIONPITCHER && sp <= 0) )
  4632. 				clif_skill_nodamage(NULL,bl,AL_HEAL,hp,1);
  4633. 			if( sp > 0 )
  4634. 				clif_skill_nodamage(NULL,bl,MG_SRECOVERY,sp,1);
  4635. 			status_heal(bl,hp,sp,0);
  4636. 		}
  4637. 		break;
  4638. 	case AM_CP_WEAPON:
  4639. 	case AM_CP_SHIELD:
  4640. 	case AM_CP_ARMOR:
  4641. 	case AM_CP_HELM:
  4642. 		{
  4643. 			enum sc_type scid = (sc_type)(SC_STRIPWEAPON + (skillid - AM_CP_WEAPON));
  4644. 			status_change_end(bl, scid, INVALID_TIMER);
  4645. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  4646. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4647. 		}
  4648. 		break;
  4649. 	case AM_TWILIGHT1:
  4650. 		if (sd) {
  4651. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4652. 			//Prepare 200 White Potions.
  4653. 			if (!skill_produce_mix(sd, skillid, 504, 0, 0, 0, 200))
  4654. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4655. 		}
  4656. 		break;
  4657. 	case AM_TWILIGHT2:
  4658. 		if (sd) {
  4659. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4660. 			//Prepare 200 Slim White Potions.
  4661. 			if (!skill_produce_mix(sd, skillid, 547, 0, 0, 0, 200))
  4662. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4663. 		}
  4664. 		break;
  4665. 	case AM_TWILIGHT3:
  4666. 		if (sd) {
  4667. 			//check if you can produce all three, if not, then fail:
  4668. 			if (!skill_can_produce_mix(sd,970,-1, 100) //100 Alcohol
  4669. 				|| !skill_can_produce_mix(sd,7136,-1, 50) //50 Acid Bottle
  4670. 				|| !skill_can_produce_mix(sd,7135,-1, 50) //50 Flame Bottle
  4671. 			) {
  4672. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4673. 				break;
  4674. 			}
  4675. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4676. 			skill_produce_mix(sd, skillid, 970, 0, 0, 0, 100);
  4677. 			skill_produce_mix(sd, skillid, 7136, 0, 0, 0, 50);
  4678. 			skill_produce_mix(sd, skillid, 7135, 0, 0, 0, 50);
  4679. 		}
  4680. 		break;
  4681. 	case SA_DISPELL:
  4682. 		if (flag&1 || (i = skill_get_splash(skillid, skilllv)) < 1)
  4683. 		{
  4684. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4685. 			if((dstsd && (dstsd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER)
  4686. 				|| (tsc && tsc->data[SC_SPIRIT] && tsc->data[SC_SPIRIT]->val2 == SL_ROGUE) //Rogue's spirit defends againt dispel.
  4687. 				|| rand()%100 >= 50+10*skilllv)
  4688. 			{
  4689. 				if (sd)
  4690. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4691. 				break;
  4692. 			}
  4693. 			if(status_isimmune(bl) || !tsc || !tsc->count)
  4694. 				break;
  4695. 			for(i=0;i<SC_MAX;i++)
  4696. 			{
  4697. 				if (!tsc->data[i])
  4698. 					continue;
  4699. 				switch (i) {
  4700. 				case SC_WEIGHT50:		case SC_WEIGHT90:		case SC_HALLUCINATION:
  4701. 				case SC_STRIPWEAPON:	case SC_STRIPSHIELD:	case SC_STRIPARMOR:
  4702. 				case SC_STRIPHELM:		case SC_CP_WEAPON:		case SC_CP_SHIELD:
  4703. 				case SC_CP_ARMOR:		case SC_CP_HELM:		case SC_COMBO:
  4704. 				case SC_STRFOOD:		case SC_AGIFOOD:		case SC_VITFOOD:
  4705. 				case SC_INTFOOD:		case SC_DEXFOOD:		case SC_LUKFOOD:
  4706. 				case SC_HITFOOD:		case SC_FLEEFOOD:		case SC_BATKFOOD:
  4707. 				case SC_WATKFOOD:		case SC_MATKFOOD:		case SC_DANCING:
  4708. 				case SC_GUILDAURA:		case SC_EDP:			case SC_AUTOBERSERK:
  4709. 				case SC_CARTBOOST:		case SC_MELTDOWN:		case SC_SAFETYWALL:
  4710. 				case SC_SMA:			case SC_SPEEDUP0:		case SC_NOCHAT:
  4711. 				case SC_ANKLE:			case SC_SPIDERWEB:		case SC_JAILED:
  4712. 				case SC_ITEMBOOST:		case SC_EXPBOOST:		case SC_LIFEINSURANCE:
  4713. 				case SC_BOSSMAPINFO:	case SC_PNEUMA:			case SC_AUTOSPELL:
  4714. 				case SC_INCHITRATE:		case SC_INCATKRATE:		case SC_NEN:
  4715. 				case SC_READYSTORM:		case SC_READYDOWN:		case SC_READYTURN:
  4716. 				case SC_READYCOUNTER:	case SC_DODGE:			case SC_WARM:
  4717. 				case SC_SPEEDUP1:		case SC_AUTOTRADE:		case SC_CRITICALWOUND:
  4718. 				case SC_JEXPBOOST:		case SC_INVINCIBLE:		case SC_INVINCIBLEOFF:
  4719. 				case SC_HELLPOWER:		case SC_MANU_ATK:		case SC_MANU_DEF:
  4720. 				case SC_SPL_ATK:		case SC_SPL_DEF:		case SC_MANU_MATK:
  4721. 				case SC_SPL_MATK:		case SC_RICHMANKIM:		case SC_ETERNALCHAOS:
  4722. 				case SC_DRUMBATTLE:		case SC_NIBELUNGEN:		case SC_ROKISWEIL:
  4723. 				case SC_INTOABYSS:		case SC_SIEGFRIED:		case SC_WHISTLE:
  4724. 				case SC_ASSNCROS:		case SC_POEMBRAGI:		case SC_APPLEIDUN:
  4725. 				case SC_HUMMING:		case SC_DONTFORGETME:	case SC_FORTUNE:
  4726. 				case SC_SERVICE4U:		case SC_FOOD_STR_CASH:	case SC_FOOD_AGI_CASH:
  4727. 				case SC_FOOD_VIT_CASH:	case SC_FOOD_DEX_CASH:	case SC_FOOD_INT_CASH:
  4728. 				case SC_FOOD_LUK_CASH:
  4729. 					continue;
  4730. 				case SC_ASSUMPTIO:
  4731. 					if( bl->type == BL_MOB )
  4732. 						continue;
  4733. 					break;
  4734. 				}
  4735. 				if(i==SC_BERSERK) tsc->data[i]->val2=0; //Mark a dispelled berserk to avoid setting hp to 100 by setting hp penalty to 0.
  4736. 				status_change_end(bl, (sc_type)i, INVALID_TIMER);
  4737. 			}
  4738. 			break;
  4739. 		}
  4740. 		//Affect all targets on splash area.
  4741. 		map_foreachinrange(skill_area_sub, bl, i, BL_CHAR,
  4742. 			src, skillid, skilllv, tick, flag|1,
  4743. 			skill_castend_damage_id);
  4744. 		break;
  4745.  
  4746. 	case TF_BACKSLIDING: //This is the correct implementation as per packet logging information. [Skotlex]
  4747. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4748. 		skill_blown(src,bl,skill_get_blewcount(skillid,skilllv),unit_getdir(bl),0);
  4749. 		break;
  4750.  
  4751. 	case TK_HIGHJUMP:
  4752. 		{
  4753. 			int x,y, dir = unit_getdir(src);
  4754.  
  4755. 		  	//Fails on noteleport maps, except for GvG and BG maps [Skotlex]
  4756. 			if( map[src->m].flag.noteleport &&
  4757. 				!(map[src->m].flag.battleground || map_flag_gvg2(src->m) )
  4758. 			) {
  4759. 				x = src->x;
  4760. 				y = src->y;
  4761. 			} else {
  4762. 				x = src->x + dirx[dir]*skilllv*2;
  4763. 				y = src->y + diry[dir]*skilllv*2;
  4764. 			}
  4765.  
  4766. 			clif_skill_nodamage(src,bl,TK_HIGHJUMP,skilllv,1);
  4767. 			if(!map_count_oncell(src->m,x,y,BL_PC|BL_NPC|BL_MOB) && map_getcell(src->m,x,y,CELL_CHKREACH)) {
  4768. 				clif_slide(src,x,y);
  4769. 				unit_movepos(src, x, y, 1, 0);
  4770. 			}
  4771. 		}
  4772. 		break;
  4773.  
  4774. 	case SA_CASTCANCEL:
  4775. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4776. 		unit_skillcastcancel(src,1);
  4777. 		if(sd) {
  4778. 			int sp = skill_get_sp(sd->skillid_old,sd->skilllv_old);
  4779. 			sp = sp * (90 - (skilllv-1)*20) / 100;
  4780. 			if(sp < 0) sp = 0;
  4781. 			status_zap(src, 0, sp);
  4782. 		}
  4783. 		break;
  4784. 	case SA_SPELLBREAKER:
  4785. 		{
  4786. 			int sp;
  4787. 			if(tsc && tsc->data[SC_MAGICROD]) {
  4788. 				sp = skill_get_sp(skillid,skilllv);
  4789. 				sp = sp * tsc->data[SC_MAGICROD]->val2 / 100;
  4790. 				if(sp < 1) sp = 1;
  4791. 				status_heal(bl,0,sp,2);
  4792. 				clif_skill_nodamage(bl,bl,SA_MAGICROD,tsc->data[SC_MAGICROD]->val1,1);
  4793. 				status_percent_damage(bl, src, 0, -20, false); //20% max SP damage.
  4794. 			} else {
  4795. 				struct unit_data *ud = unit_bl2ud(bl);
  4796. 				int bl_skillid=0,bl_skilllv=0,hp = 0;
  4797. 				if (!ud || ud->skilltimer == INVALID_TIMER)
  4798. 					break; //Nothing to cancel.
  4799. 				bl_skillid = ud->skillid;
  4800. 				bl_skilllv = ud->skilllv;
  4801. 				if (tstatus->mode & MD_BOSS)
  4802. 				{	//Only 10% success chance against bosses. [Skotlex]
  4803. 					if (rand()%100 < 90)
  4804. 					{
  4805. 						if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4806. 						break;
  4807. 					}
  4808. 				} else if (!dstsd || map_flag_vs(bl->m)) //HP damage only on pvp-maps when against players.
  4809. 					hp = tstatus->max_hp/50; //Recover 2% HP [Skotlex]
  4810.  
  4811. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4812. 				unit_skillcastcancel(bl,0);
  4813. 				sp = skill_get_sp(bl_skillid,bl_skilllv);
  4814. 				status_zap(bl, hp, sp);
  4815.  
  4816. 				if (hp && skilllv >= 5)
  4817. 					hp>>=1;	//Recover half damaged HP at level 5 [Skotlex]
  4818. 				else
  4819. 					hp = 0;
  4820.  
  4821. 				if (sp) //Recover some of the SP used
  4822. 					sp = sp*(25*(skilllv-1))/100;
  4823.  
  4824. 				if(hp || sp)
  4825. 					status_heal(src, hp, sp, 2);
  4826. 			}
  4827. 		}
  4828. 		break;
  4829. 	case SA_MAGICROD:
  4830. 		//It activates silently, no use animation.
  4831. 		sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  4832. 		break;
  4833. 	case SA_AUTOSPELL:
  4834. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4835. 		if(sd)
  4836. 			clif_autospell(sd,skilllv);
  4837. 		else {
  4838. 			int maxlv=1,spellid=0;
  4839. 			static const int spellarray[3] = { MG_COLDBOLT,MG_FIREBOLT,MG_LIGHTNINGBOLT };
  4840. 			if(skilllv >= 10) {
  4841. 				spellid = MG_FROSTDIVER;
  4842. //				if (tsc && tsc->data[SC_SPIRIT] && tsc->data[SC_SPIRIT]->val2 == SA_SAGE)
  4843. //					maxlv = 10;
  4844. //				else
  4845. 					maxlv = skilllv - 9;
  4846. 			}
  4847. 			else if(skilllv >=8) {
  4848. 				spellid = MG_FIREBALL;
  4849. 				maxlv = skilllv - 7;
  4850. 			}
  4851. 			else if(skilllv >=5) {
  4852. 				spellid = MG_SOULSTRIKE;
  4853. 				maxlv = skilllv - 4;
  4854. 			}
  4855. 			else if(skilllv >=2) {
  4856. 				int i = rand()%3;
  4857. 				spellid = spellarray[i];
  4858. 				maxlv = skilllv - 1;
  4859. 			}
  4860. 			else if(skilllv > 0) {
  4861. 				spellid = MG_NAPALMBEAT;
  4862. 				maxlv = 3;
  4863. 			}
  4864. 			if(spellid > 0)
  4865. 				sc_start4(src,SC_AUTOSPELL,100,skilllv,spellid,maxlv,0,
  4866. 					skill_get_time(SA_AUTOSPELL,skilllv));
  4867. 		}
  4868. 		break;
  4869.  
  4870. 	case BS_GREED:
  4871. 		if(sd){
  4872. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4873. 			map_foreachinrange(skill_greed,bl,
  4874. 				skill_get_splash(skillid, skilllv),BL_ITEM,bl);
  4875. 		}
  4876. 		break;
  4877.  
  4878. 	case SA_ELEMENTWATER:
  4879. 	case SA_ELEMENTFIRE:
  4880. 	case SA_ELEMENTGROUND:
  4881. 	case SA_ELEMENTWIND:
  4882. 		if(sd && !dstmd) //Only works on monsters.
  4883. 			break;
  4884. 		if(tstatus->mode&MD_BOSS)
  4885. 			break;
  4886. 	case NPC_ATTRICHANGE:
  4887. 	case NPC_CHANGEWATER:
  4888. 	case NPC_CHANGEGROUND:
  4889. 	case NPC_CHANGEFIRE:
  4890. 	case NPC_CHANGEWIND:
  4891. 	case NPC_CHANGEPOISON:
  4892. 	case NPC_CHANGEHOLY:
  4893. 	case NPC_CHANGEDARKNESS:
  4894. 	case NPC_CHANGETELEKINESIS:
  4895. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4896. 			sc_start2(bl, type, 100, skilllv, skill_get_ele(skillid,skilllv),
  4897. 				skill_get_time(skillid, skilllv)));
  4898. 		break;
  4899. 	case NPC_CHANGEUNDEAD:
  4900. 		//This skill should fail if target is wearing bathory/evil druid card [Brainstorm]
  4901. 		//TO-DO This is ugly, fix it
  4902. 		if(tstatus->def_ele==ELE_UNDEAD || tstatus->def_ele==ELE_DARK) break;
  4903. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4904. 			sc_start2(bl, type, 100, skilllv, skill_get_ele(skillid,skilllv),
  4905. 				skill_get_time(skillid, skilllv)));
  4906. 		break;
  4907.  
  4908. 	case NPC_PROVOCATION:
  4909. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4910. 		if (md) mob_unlocktarget(md, tick);
  4911. 		break;
  4912.  
  4913. 	case NPC_KEEPING:
  4914. 	case NPC_BARRIER:
  4915. 		{
  4916. 			int skill_time = skill_get_time(skillid,skilllv);
  4917. 			struct unit_data *ud = unit_bl2ud(bl);
  4918. 			if (clif_skill_nodamage(src,bl,skillid,skilllv,
  4919. 					sc_start(bl,type,100,skilllv,skill_time))
  4920. 			&& ud) {	//Disable attacking/acting/moving for skill's duration.
  4921. 				ud->attackabletime =
  4922. 				ud->canact_tick =
  4923. 				ud->canmove_tick = tick + skill_time;
  4924. 			}
  4925. 		}
  4926. 		break;
  4927.  
  4928. 	case NPC_REBIRTH:
  4929. 		if( md && md->state.rebirth )
  4930. 			break; // only works once
  4931. 		sc_start(bl,type,100,skilllv,-1);
  4932. 		break;
  4933.  
  4934. 	case NPC_DARKBLESSING:
  4935. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4936. 			sc_start2(bl,type,(50+skilllv*5),skilllv,skilllv,skill_get_time2(skillid,skilllv)));
  4937. 		break;
  4938.  
  4939. 	case NPC_LICK:
  4940. 		status_zap(bl, 0, 100);
  4941. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4942. 			sc_start(bl,type,(skilllv*5),skilllv,skill_get_time2(skillid,skilllv)));
  4943. 		break;
  4944.  
  4945. 	case NPC_SUICIDE:
  4946. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4947. 		status_kill(src); //When suiciding, neither exp nor drops is given.
  4948. 		break;
  4949.  
  4950. 	case NPC_SUMMONSLAVE:
  4951. 	case NPC_SUMMONMONSTER:
  4952. 		if(md && md->skillidx >= 0)
  4953. 			mob_summonslave(md,md->db->skill[md->skillidx].val,skilllv,skillid);
  4954. 		break;
  4955.  
  4956. 	case NPC_CALLSLAVE:
  4957. 		mob_warpslave(src,MOB_SLAVEDISTANCE);
  4958. 		break;
  4959.  
  4960. 	case NPC_RANDOMMOVE:
  4961. 		if (md) {
  4962. 			md->next_walktime = tick - 1;
  4963. 			mob_randomwalk(md,tick);
  4964. 		}
  4965. 		break;
  4966.  
  4967. 	case NPC_SPEEDUP:
  4968. 		{
  4969. 			// or does it increase casting rate? just a guess xD
  4970. 			int i = SC_ASPDPOTION0 + skilllv - 1;
  4971. 			if (i > SC_ASPDPOTION3)
  4972. 				i = SC_ASPDPOTION3;
  4973. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  4974. 				sc_start(bl,(sc_type)i,100,skilllv,skilllv * 60000));
  4975. 		}
  4976. 		break;
  4977.  
  4978. 	case NPC_REVENGE:
  4979. 		// not really needed... but adding here anyway ^^
  4980. 		if (md && md->master_id > 0) {
  4981. 			struct block_list *mbl, *tbl;
  4982. 			if ((mbl = map_id2bl(md->master_id)) == NULL ||
  4983. 				(tbl = battle_gettargeted(mbl)) == NULL)
  4984. 				break;
  4985. 			md->state.provoke_flag = tbl->id;
  4986. 			mob_target(md, tbl, sstatus->rhw.range);
  4987. 		}
  4988. 		break;
  4989.  
  4990. 	case NPC_RUN:
  4991. 		{
  4992. 			const int mask[8][2] = {{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
  4993. 			int dir = (bl == src)?unit_getdir(src):map_calc_dir(src,bl->x,bl->y); //If cast on self, run forward, else run away.
  4994. 			unit_stop_attack(src);
  4995. 			//Run skillv tiles overriding the can-move check.
  4996. 			if (unit_walktoxy(src, src->x + skilllv * mask[dir][0], src->y + skilllv * mask[dir][1], 2) && md)
  4997. 				md->state.skillstate = MSS_WALK; //Otherwise it isn't updated in the ai.
  4998. 		}
  4999. 		break;
  5000.  
  5001. 	case NPC_TRANSFORMATION:
  5002. 	case NPC_METAMORPHOSIS:
  5003. 		if(md && md->skillidx >= 0) {
  5004. 			int class_ = mob_random_class (md->db->skill[md->skillidx].val,0);
  5005. 			if (skilllv > 1) //Multiply the rest of mobs. [Skotlex]
  5006. 				mob_summonslave(md,md->db->skill[md->skillidx].val,skilllv-1,skillid);
  5007. 			if (class_) mob_class_change(md, class_);
  5008. 		}
  5009. 		break;
  5010.  
  5011. 	case NPC_EMOTION_ON:
  5012. 	case NPC_EMOTION:
  5013. 		//va[0] is the emotion to use.
  5014. 		//NPC_EMOTION & NPC_EMOTION_ON can change a mob's mode 'permanently' [Skotlex]
  5015. 		//val[1] 'sets' the mode
  5016. 		//val[2] adds to the current mode
  5017. 		//val[3] removes from the current mode
  5018. 		//val[4] if set, asks to delete the previous mode change.
  5019. 		if(md && md->skillidx >= 0 && tsc)
  5020. 		{
  5021. 			clif_emotion(bl, md->db->skill[md->skillidx].val[0]);
  5022. 			if(md->db->skill[md->skillidx].val[4] && tsce)
  5023. 				status_change_end(bl, type, INVALID_TIMER);
  5024.  
  5025. 			if(md->db->skill[md->skillidx].val[1] || md->db->skill[md->skillidx].val[2])
  5026. 				sc_start4(src, type, 100, skilllv,
  5027. 					md->db->skill[md->skillidx].val[1],
  5028. 					md->db->skill[md->skillidx].val[2],
  5029. 					md->db->skill[md->skillidx].val[3],
  5030. 					skill_get_time(skillid, skilllv));
  5031. 		}
  5032. 		break;
  5033.  
  5034. 	case NPC_POWERUP:
  5035. 		sc_start(bl,SC_INCATKRATE,100,200,skill_get_time(skillid, skilllv));
  5036. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5037. 			sc_start(bl,type,100,100,skill_get_time(skillid, skilllv)));
  5038. 		break;
  5039.  
  5040. 	case NPC_AGIUP:
  5041. 		sc_start(bl,SC_SPEEDUP1,100,skilllv,skill_get_time(skillid, skilllv));
  5042. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5043. 			sc_start(bl,type,100,100,skill_get_time(skillid, skilllv)));
  5044. 		break;
  5045.  
  5046. 	case NPC_INVISIBLE:
  5047. 		//Have val4 passed as 6 is for "infinite cloak" (do not end on attack/skill use).
  5048. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5049. 			sc_start4(bl,type,100,skilllv,0,0,6,skill_get_time(skillid,skilllv)));
  5050. 		break;
  5051.  
  5052. 	case NPC_SIEGEMODE:
  5053. 		// not sure what it does
  5054. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5055. 		break;
  5056.  
  5057. 	case WE_MALE:
  5058. 		{
  5059. 			int hp_rate=(skilllv <= 0)? 0:skill_db[skillid].hp_rate[skilllv-1];
  5060. 			int gain_hp= tstatus->max_hp*abs(hp_rate)/100; // The earned is the same % of the target HP than it costed the caster. [Skotlex]
  5061. 			clif_skill_nodamage(src,bl,skillid,status_heal(bl, gain_hp, 0, 0),1);
  5062. 		}
  5063. 		break;
  5064. 	case WE_FEMALE:
  5065. 		{
  5066. 			int sp_rate=(skilllv <= 0)? 0:skill_db[skillid].sp_rate[skilllv-1];
  5067. 			int gain_sp=tstatus->max_sp*abs(sp_rate)/100;// The earned is the same % of the target SP than it costed the caster. [Skotlex]
  5068. 			clif_skill_nodamage(src,bl,skillid,status_heal(bl, 0, gain_sp, 0),1);
  5069. 		}
  5070. 		break;
  5071.  
  5072. 	// parent-baby skills
  5073. 	case WE_BABY:
  5074. 		if(sd){
  5075. 			struct map_session_data *f_sd = pc_get_father(sd);
  5076. 			struct map_session_data *m_sd = pc_get_mother(sd);
  5077. 			// if neither was found
  5078. 			if(!f_sd && !m_sd){
  5079. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5080. 				map_freeblock_unlock();
  5081. 				return 0;
  5082. 			}
  5083. 			status_change_start(bl,SC_STUN,10000,skilllv,0,0,0,skill_get_time2(skillid,skilllv),8);
  5084. 			if (f_sd) sc_start(&f_sd->bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  5085. 			if (m_sd) sc_start(&m_sd->bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  5086. 		}
  5087. 		break;
  5088.  
  5089. 	case PF_HPCONVERSION:
  5090. 		{
  5091. 			int hp, sp;
  5092. 			hp = sstatus->max_hp/10;
  5093. 			sp = hp * 10 * skilllv / 100;
  5094. 			if (!status_charge(src,hp,0)) {
  5095. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5096. 				break;
  5097. 			}
  5098. 			clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  5099. 			status_heal(bl,0,sp,2);
  5100. 		}
  5101. 		break;
  5102.  
  5103. 	case MA_REMOVETRAP:
  5104. 	case HT_REMOVETRAP:
  5105. 		//FIXME: I think clif_skill_fail() is supposed to be sent if it fails below [ultramage]
  5106. 		clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  5107. 		{
  5108. 			struct skill_unit* su;
  5109. 			struct skill_unit_group* sg;
  5110. 			su = BL_CAST(BL_SKILL, bl);
  5111.  
  5112. 			// Mercenaries can remove any trap
  5113. 			// Players can only remove their own traps or traps on Vs maps.
  5114. 			if( su && (sg = su->group) && (src->type == BL_MER || sg->src_id == src->id || map_flag_vs(bl->m)) && (skill_get_inf2(sg->skill_id)&INF2_TRAP) )
  5115. 			{
  5116. 				if( sd && !(sg->unit_id == UNT_USED_TRAPS || (sg->unit_id == UNT_ANKLESNARE && sg->val2 != 0 )) )
  5117. 				{ // prevent picking up expired traps
  5118. 					if( battle_config.skill_removetrap_type )
  5119. 					{ // get back all items used to deploy the trap
  5120. 						for( i = 0; i < 10; i++ )
  5121. 						{
  5122. 							if( skill_db[su->group->skill_id].itemid[i] > 0 )
  5123. 							{
  5124. 								int flag;
  5125. 								struct item item_tmp;
  5126. 								memset(&item_tmp,0,sizeof(item_tmp));
  5127. 								item_tmp.nameid = skill_db[su->group->skill_id].itemid[i];
  5128. 								item_tmp.identify = 1;
  5129. 								if( item_tmp.nameid && (flag=pc_additem(sd,&item_tmp,skill_db[su->group->skill_id].amount[i])) )
  5130. 								{
  5131. 									clif_additem(sd,0,0,flag);
  5132. 									map_addflooritem(&item_tmp,skill_db[su->group->skill_id].amount[i],sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5133. 								}
  5134. 							}
  5135. 						}
  5136. 					}
  5137. 					else
  5138. 					{ // get back 1 trap
  5139. 						struct item item_tmp;
  5140. 						memset(&item_tmp,0,sizeof(item_tmp));
  5141. 						item_tmp.nameid = ITEMID_TRAP;
  5142. 						item_tmp.identify = 1;
  5143. 						if( item_tmp.nameid && (flag=pc_additem(sd,&item_tmp,1)) )
  5144. 						{
  5145. 							clif_additem(sd,0,0,flag);
  5146. 							map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5147. 						}
  5148. 					}
  5149. 				}
  5150. 				skill_delunit(su);
  5151. 			}
  5152. 		}
  5153. 		break;
  5154. 	case HT_SPRINGTRAP:
  5155. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5156. 		{
  5157. 			struct skill_unit *su=NULL;
  5158. 			if((bl->type==BL_SKILL) && (su=(struct skill_unit *)bl) && (su->group) ){
  5159. 				switch(su->group->unit_id){
  5160. 					case UNT_ANKLESNARE:	// ankle snare
  5161. 						if (su->group->val2 != 0)
  5162. 							// if it is already trapping something don't spring it,
  5163. 							// remove trap should be used instead
  5164. 							break;
  5165. 						// otherwise fallthrough to below
  5166. 					case UNT_BLASTMINE:
  5167. 					case UNT_SKIDTRAP:
  5168. 					case UNT_LANDMINE:
  5169. 					case UNT_SHOCKWAVE:
  5170. 					case UNT_SANDMAN:
  5171. 					case UNT_FLASHER:
  5172. 					case UNT_FREEZINGTRAP:
  5173. 					case UNT_CLAYMORETRAP:
  5174. 					case UNT_TALKIEBOX:
  5175. 						su->group->unit_id = UNT_USED_TRAPS;
  5176. 						clif_changetraplook(bl, UNT_USED_TRAPS);
  5177. 						su->group->limit=DIFF_TICK(tick+1500,su->group->tick);
  5178. 						su->limit=DIFF_TICK(tick+1500,su->group->tick);
  5179. 				}
  5180. 			}
  5181. 		}
  5182. 		break;
  5183. 	case BD_ENCORE:
  5184. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5185. 		if(sd)
  5186. 			unit_skilluse_id(src,src->id,sd->skillid_dance,sd->skilllv_dance);
  5187. 		break;
  5188.  
  5189. 	case AS_SPLASHER:
  5190. 		if(tstatus->mode&MD_BOSS || tstatus-> hp > tstatus->max_hp*3/4) {
  5191. 			if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5192. 			map_freeblock_unlock();
  5193. 			return 1;
  5194. 		}
  5195. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5196. 			sc_start4(bl,type,100,skilllv,skillid,src->id,skill_get_time(skillid,skilllv),1000));
  5197. 		if (sd) skill_blockpc_start (sd, skillid, skill_get_time(skillid, skilllv)+3000);
  5198. 		break;
  5199.  
  5200. 	case PF_MINDBREAKER:
  5201. 		{
  5202. 			if(tstatus->mode&MD_BOSS || battle_check_undead(tstatus->race,tstatus->def_ele))
  5203. 			{
  5204. 				map_freeblock_unlock();
  5205. 				return 1;
  5206. 			}
  5207.  
  5208. 			if (tsce)
  5209. 			{	//HelloKitty2 (?) explained that this silently fails when target is
  5210. 				//already inflicted. [Skotlex]
  5211. 				map_freeblock_unlock();
  5212. 				return 1;
  5213. 			}
  5214.  
  5215. 			//Has a 55% + skilllv*5% success chance.
  5216. 			if (!clif_skill_nodamage(src,bl,skillid,skilllv,
  5217. 				sc_start(bl,type,55+5*skilllv,skilllv,skill_get_time(skillid,skilllv))))
  5218. 			{
  5219. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5220. 				map_freeblock_unlock();
  5221. 				return 0;
  5222. 			}
  5223.  
  5224. 			unit_skillcastcancel(bl,0);
  5225.  
  5226. 			if(tsc && tsc->count){
  5227. 				status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  5228. 				if(tsc->data[SC_STONE] && tsc->opt1 == OPT1_STONE)
  5229. 					status_change_end(bl, SC_STONE, INVALID_TIMER);
  5230. 				status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  5231. 			}
  5232.  
  5233. 			if(dstmd)
  5234. 				mob_target(dstmd,src,skill_get_range2(src,skillid,skilllv));
  5235. 		}
  5236. 		break;
  5237.  
  5238. 	case PF_SOULCHANGE:
  5239. 		{
  5240. 			unsigned int sp1 = 0, sp2 = 0;
  5241. 			if (dstmd) {
  5242. 				if (dstmd->state.soul_change_flag) {
  5243. 					if(sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5244. 					break;
  5245. 				}
  5246. 				dstmd->state.soul_change_flag = 1;
  5247. 				sp2 = sstatus->max_sp * 3 /100;
  5248. 				status_heal(src, 0, sp2, 2);
  5249. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5250. 				break;
  5251. 			}
  5252. 			sp1 = sstatus->sp;
  5253. 			sp2 = tstatus->sp;
  5254. 			status_set_sp(src, sp2, 3);
  5255. 			status_set_sp(bl, sp1, 3);
  5256. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5257. 		}
  5258. 		break;
  5259.  
  5260. 	// Slim Pitcher
  5261. 	case CR_SLIMPITCHER:
  5262. 		// Updated to block Slim Pitcher from working on barricades and guardian stones.
  5263. 		if( dstmd && (dstmd->class_ == MOBID_EMPERIUM || (dstmd->class_ >= MOBID_BARRICADE1 && dstmd->class_ <= MOBID_GUARIDAN_STONE2)) )
  5264. 			break;
  5265. 		if (potion_hp || potion_sp) {
  5266. 			int hp = potion_hp, sp = potion_sp;
  5267. 			hp = hp * (100 + (tstatus->vit<<1))/100;
  5268. 			sp = sp * (100 + (tstatus->int_<<1))/100;
  5269. 			if (dstsd) {
  5270. 				if (hp)
  5271. 					hp = hp * (100 + pc_checkskill(dstsd,SM_RECOVERY)*10 + pc_skillheal2_bonus(dstsd, skillid))/100;
  5272. 				if (sp)
  5273. 					sp = sp * (100 + pc_checkskill(dstsd,MG_SRECOVERY)*10 + pc_skillheal2_bonus(dstsd, skillid))/100;
  5274. 			}
  5275. 			if (tsc && tsc->data[SC_CRITICALWOUND])
  5276. 			{
  5277. 				hp -= hp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  5278. 				sp -= sp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  5279. 			}
  5280. 			if(hp > 0)
  5281. 				clif_skill_nodamage(NULL,bl,AL_HEAL,hp,1);
  5282. 			if(sp > 0)
  5283. 				clif_skill_nodamage(NULL,bl,MG_SRECOVERY,sp,1);
  5284. 			status_heal(bl,hp,sp,0);
  5285. 		}
  5286. 		break;
  5287. 	// Full Chemical Protection
  5288. 	case CR_FULLPROTECTION:
  5289. 		{
  5290. 			int i, skilltime;
  5291. 			skilltime = skill_get_time(skillid,skilllv);
  5292. 			if (!tsc) {
  5293. 				clif_skill_nodamage(src,bl,skillid,skilllv,0);
  5294. 				break;
  5295. 			}
  5296. 			for (i=0; i<4; i++) {
  5297. 				status_change_end(bl, (sc_type)(SC_STRIPWEAPON + i), INVALID_TIMER);
  5298. 				sc_start(bl,(sc_type)(SC_CP_WEAPON + i),100,skilllv,skilltime);
  5299. 			}
  5300. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5301. 		}
  5302. 		break;
  5303.  
  5304. 	case RG_CLEANER:	//AppleGirl
  5305. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5306. 		break;
  5307.  
  5308. 	case CG_LONGINGFREEDOM:
  5309. 		{
  5310. 			if (tsc && !tsce && (tsce=tsc->data[SC_DANCING]) && tsce->val4
  5311. 				&& (tsce->val1&0xFFFF) != CG_MOONLIT) //Can't use Longing for Freedom while under Moonlight Petals. [Skotlex]
  5312. 			{
  5313. 				clif_skill_nodamage(src,bl,skillid,skilllv,
  5314. 					sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  5315. 			}
  5316. 		}
  5317. 		break;
  5318.  
  5319. 	case CG_TAROTCARD:
  5320. 		{
  5321. 			int eff, count = -1;
  5322. 			if( rand() % 100 > skilllv * 8 || (dstmd && ((dstmd->guardian_data && dstmd->class_ == MOBID_EMPERIUM) || mob_is_battleground(dstmd))) )
  5323. 			{
  5324. 				if( sd )
  5325. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5326.  
  5327. 				map_freeblock_unlock();
  5328. 				return 0;
  5329. 			}
  5330. 			status_zap(src,0,skill_db[skill_get_index(skillid)].sp[skilllv]); // consume sp only if succeeded [Inkfish]
  5331. 			do {
  5332. 				eff = rand() % 14;
  5333. 				clif_specialeffect(bl, 523 + eff, AREA);
  5334. 				switch (eff)
  5335. 				{
  5336. 				case 0:	// heals SP to 0
  5337. 					status_percent_damage(src, bl, 0, 100, false);
  5338. 					break;
  5339. 				case 1:	// matk halved
  5340. 					sc_start(bl,SC_INCMATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5341. 					break;
  5342. 				case 2:	// all buffs removed
  5343. 					status_change_clear_buffs(bl,1);
  5344. 					break;
  5345. 				case 3:	// 1000 damage, random armor destroyed
  5346. 					{
  5347. 						int where[] = { EQP_ARMOR, EQP_SHIELD, EQP_HELM, EQP_SHOES, EQP_GARMENT };
  5348. 						status_fix_damage(src, bl, 1000, 0);
  5349. 						clif_damage(src,bl,tick,0,0,1000,0,0,0);
  5350. 						if( !status_isdead(bl) )
  5351. 							skill_break_equip(bl, where[rand()%3], 10000, BCT_ENEMY);
  5352. 					}
  5353. 					break;
  5354. 				case 4:	// atk halved
  5355. 					sc_start(bl,SC_INCATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5356. 					break;
  5357. 				case 5:	// 2000HP heal, random teleported
  5358. 					status_heal(src, 2000, 0, 0);
  5359. 					if( !map_flag_vs(bl->m) )
  5360. 						unit_warp(bl, -1,-1,-1, CLR_TELEPORT);
  5361. 					break;
  5362. 				case 6:	// random 2 other effects
  5363. 					if (count == -1)
  5364. 						count = 3;
  5365. 					else
  5366. 						count++; //Should not retrigger this one.
  5367. 					break;
  5368. 				case 7:	// stop freeze or stoned
  5369. 					{
  5370. 						enum sc_type sc[] = { SC_STOP, SC_FREEZE, SC_STONE };
  5371. 						sc_start(bl,sc[rand()%3],100,skilllv,skill_get_time2(skillid,skilllv));
  5372. 					}
  5373. 					break;
  5374. 				case 8:	// curse coma and poison
  5375. 					sc_start(bl,SC_COMA,100,skilllv,skill_get_time2(skillid,skilllv));
  5376. 					sc_start(bl,SC_CURSE,100,skilllv,skill_get_time2(skillid,skilllv));
  5377. 					sc_start(bl,SC_POISON,100,skilllv,skill_get_time2(skillid,skilllv));
  5378. 					break;
  5379. 				case 9:	// confusion
  5380. 					sc_start(bl,SC_CONFUSION,100,skilllv,skill_get_time2(skillid,skilllv));
  5381. 					break;
  5382. 				case 10:	// 6666 damage, atk matk halved, cursed
  5383. 					status_fix_damage(src, bl, 6666, 0);
  5384. 					clif_damage(src,bl,tick,0,0,6666,0,0,0);
  5385. 					sc_start(bl,SC_INCATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5386. 					sc_start(bl,SC_INCMATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5387. 					sc_start(bl,SC_CURSE,skilllv,100,skill_get_time2(skillid,skilllv));
  5388. 					break;
  5389. 				case 11:	// 4444 damage
  5390. 					status_fix_damage(src, bl, 4444, 0);
  5391. 					clif_damage(src,bl,tick,0,0,4444,0,0,0);
  5392. 					break;
  5393. 				case 12:	// stun
  5394. 					sc_start(bl,SC_STUN,100,skilllv,5000);
  5395. 					break;
  5396. 				case 13:	// atk,matk,hit,flee,def reduced
  5397. 					sc_start(bl,SC_INCATKRATE,100,-20,skill_get_time2(skillid,skilllv));
  5398. 					sc_start(bl,SC_INCMATKRATE,100,-20,skill_get_time2(skillid,skilllv));
  5399. 					sc_start(bl,SC_INCHITRATE,100,-20,skill_get_time2(skillid,skilllv));
  5400. 					sc_start(bl,SC_INCFLEERATE,100,-20,skill_get_time2(skillid,skilllv));
  5401. 					sc_start(bl,SC_INCDEFRATE,100,-20,skill_get_time2(skillid,skilllv));
  5402. 					break;
  5403. 				default:
  5404. 					break;
  5405. 				}
  5406. 			} while ((--count) > 0);
  5407. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5408. 		}
  5409. 		break;
  5410.  
  5411. 	case SL_ALCHEMIST:
  5412. 	case SL_ASSASIN:
  5413. 	case SL_BARDDANCER:
  5414. 	case SL_BLACKSMITH:
  5415. 	case SL_CRUSADER:
  5416. 	case SL_HUNTER:
  5417. 	case SL_KNIGHT:
  5418. 	case SL_MONK:
  5419. 	case SL_PRIEST:
  5420. 	case SL_ROGUE:
  5421. 	case SL_SAGE:
  5422. 	case SL_SOULLINKER:
  5423. 	case SL_STAR:
  5424. 	case SL_SUPERNOVICE:
  5425. 	case SL_WIZARD:
  5426. 		//NOTE: here, 'type' has the value of the associated MAPID, not of the SC_SPIRIT constant.
  5427. 		if (sd && !(dstsd && (dstsd->class_&MAPID_UPPERMASK) == type)) {
  5428. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5429. 			break;
  5430. 		}
  5431. 		if (skillid == SL_SUPERNOVICE && dstsd && dstsd->die_counter && !(rand()%100))
  5432. 		{	//Erase death count 1% of the casts
  5433. 			dstsd->die_counter = 0;
  5434. 			pc_setglobalreg(dstsd,"PC_DIE_COUNTER", 0);
  5435. 			clif_specialeffect(bl, 0x152, AREA);
  5436. 			//SC_SPIRIT invokes status_calc_pc for us.
  5437. 		}
  5438. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5439. 			sc_start4(bl,SC_SPIRIT,100,skilllv,skillid,0,0,skill_get_time(skillid,skilllv)));
  5440. 		sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA,skilllv));
  5441. 		break;
  5442. 	case SL_HIGH:
  5443. 		if (sd && !(dstsd && (dstsd->class_&JOBL_UPPER) && !(dstsd->class_&JOBL_2) && dstsd->status.base_level < 70)) {
  5444. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5445. 			break;
  5446. 		}
  5447. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5448. 			sc_start4(bl,type,100,skilllv,skillid,0,0,skill_get_time(skillid,skilllv)));
  5449. 		sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA,skilllv));
  5450. 		break;
  5451.  
  5452. 	case SL_SWOO:
  5453. 		if (tsce) {
  5454. 			sc_start(src,SC_STUN,100,skilllv,10000);
  5455. 			break;
  5456. 		}
  5457. 	case SL_SKA: // [marquis007]
  5458. 	case SL_SKE:
  5459. 		if (sd && !battle_config.allow_es_magic_pc && bl->type != BL_MOB) {
  5460. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5461. 			status_change_start(src,SC_STUN,10000,skilllv,0,0,0,500,10);
  5462. 			break;
  5463. 		}
  5464. 		clif_skill_nodamage(src,bl,skillid,skilllv,sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  5465. 		if (skillid == SL_SKE)
  5466. 			sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA,skilllv));
  5467. 		break;
  5468.  
  5469. 	// New guild skills [Celest]
  5470. 	case GD_BATTLEORDER:
  5471. 		if(flag&1) {
  5472. 			if (status_get_guild_id(src) == status_get_guild_id(bl))
  5473. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv));
  5474. 		} else if (status_get_guild_id(src)) {
  5475. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5476. 			map_foreachinrange(skill_area_sub, src,
  5477. 				skill_get_splash(skillid, skilllv), BL_PC,
  5478. 				src,skillid,skilllv,tick, flag|BCT_GUILD|1,
  5479. 				skill_castend_nodamage_id);
  5480. 			if (sd)
  5481. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5482. 		}
  5483. 		break;
  5484. 	case GD_REGENERATION:
  5485. 		if(flag&1) {
  5486. 			if (status_get_guild_id(src) == status_get_guild_id(bl))
  5487. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv));
  5488. 		} else if (status_get_guild_id(src)) {
  5489. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5490. 			map_foreachinrange(skill_area_sub, src,
  5491. 				skill_get_splash(skillid, skilllv), BL_PC,
  5492. 				src,skillid,skilllv,tick, flag|BCT_GUILD|1,
  5493. 				skill_castend_nodamage_id);
  5494. 			if (sd)
  5495. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5496. 		}
  5497. 		break;
  5498. 	case GD_RESTORE:
  5499. 		if(flag&1) {
  5500. 			if (status_get_guild_id(src) == status_get_guild_id(bl))
  5501. 				clif_skill_nodamage(src,bl,AL_HEAL,status_percent_heal(bl,90,90),1);
  5502. 		} else if (status_get_guild_id(src)) {
  5503. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5504. 			map_foreachinrange(skill_area_sub, src,
  5505. 				skill_get_splash(skillid, skilllv), BL_PC,
  5506. 				src,skillid,skilllv,tick, flag|BCT_GUILD|1,
  5507. 				skill_castend_nodamage_id);
  5508. 			if (sd)
  5509. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5510. 		}
  5511. 		break;
  5512. 	case GD_EMERGENCYCALL:
  5513. 		{
  5514. 			int dx[9]={-1, 1, 0, 0,-1, 1,-1, 1, 0};
  5515. 			int dy[9]={ 0, 0, 1,-1, 1,-1,-1, 1, 0};
  5516. 			int j = 0;
  5517. 			struct guild *g = NULL;
  5518. 			// i don't know if it actually summons in a circle, but oh well. ;P
  5519. 			g = sd?sd->state.gmaster_flag:guild_search(status_get_guild_id(src));
  5520. 			if (!g)
  5521. 				break;
  5522. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5523. 			for(i = 0; i < g->max_member; i++, j++) {
  5524. 				if (j>8) j=0;
  5525. 				if ((dstsd = g->member[i].sd) != NULL && sd != dstsd && !dstsd->state.autotrade) {
  5526. 					if (map[dstsd->bl.m].flag.nowarp && !map_flag_gvg2(dstsd->bl.m))
  5527. 						continue;
  5528. 					if(map_getcell(src->m,src->x+dx[j],src->y+dy[j],CELL_CHKNOREACH))
  5529. 						dx[j] = dy[j] = 0;
  5530. 					pc_setpos(dstsd, map_id2index(src->m), src->x+dx[j], src->y+dy[j], CLR_RESPAWN);
  5531. 				}
  5532. 			}
  5533. 			if (sd)
  5534. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5535. 		}
  5536. 		break;
  5537.  
  5538. 	case SG_FEEL:
  5539. 		//AuronX reported you CAN memorize the same map as all three. [Skotlex]
  5540. 		if (sd) {
  5541. 			if(!sd->feel_map[skilllv-1].index)
  5542. 				clif_feel_req(sd->fd,sd, skilllv);
  5543. 			else
  5544. 				clif_feel_info(sd, skilllv-1, 1);
  5545. 		}
  5546. 		break;
  5547.  
  5548. 	case SG_HATE:
  5549. 		if (sd) {
  5550. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5551. 			if (!pc_set_hate_mob(sd, skilllv-1, bl))
  5552. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5553. 		}
  5554. 		break;
  5555.  
  5556. 	case GS_GLITTERING:
  5557. 		if(sd) {
  5558. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5559. 			if(rand()%100 < (20+10*skilllv))
  5560. 				pc_addspiritball(sd,skill_get_time(skillid,skilllv),10);
  5561. 			else if(sd->spiritball > 0)
  5562. 				pc_delspiritball(sd,1,0);
  5563. 		}
  5564. 		break;
  5565.  
  5566. 	case GS_CRACKER:
  5567. 		if (!dstsd)	// according to latest patch, should not work on players [Reddozen]
  5568. 		{
  5569. 			i =65 -5*distance_bl(src,bl); //Base rate
  5570. 			if (i < 30) i = 30;
  5571. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5572. 			sc_start(bl,SC_STUN, i,skilllv,skill_get_time2(skillid,skilllv));
  5573. 		}
  5574. 		else if (sd)
  5575. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5576. 		break;
  5577.  
  5578. 	case AM_CALLHOMUN:	//[orn]
  5579. 		if (sd && !merc_call_homunculus(sd))
  5580. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5581. 		break;
  5582.  
  5583. 	case AM_REST:
  5584. 		if (sd)
  5585. 		{
  5586. 			if (merc_hom_vaporize(sd,1))
  5587. 				clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  5588. 			else
  5589. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5590. 		}
  5591. 		break;
  5592.  
  5593. 	case HAMI_CASTLE:	//[orn]
  5594. 		if(rand()%100 < 20*skilllv && src != bl)
  5595. 		{
  5596. 			int x,y;
  5597. 			x = src->x;
  5598. 			y = src->y;
  5599. 			if (hd)
  5600. 				skill_blockhomun_start(hd, skillid, skill_get_time2(skillid,skilllv));
  5601.  
  5602. 			if (unit_movepos(src,bl->x,bl->y,0,0)) {
  5603. 				clif_skill_nodamage(src,src,skillid,skilllv,1); // Homunc
  5604. 				clif_slide(src,bl->x,bl->y) ;
  5605. 				if (unit_movepos(bl,x,y,0,0))
  5606. 				{
  5607. 					clif_skill_nodamage(bl,bl,skillid,skilllv,1); // Master
  5608. 					clif_slide(bl,x,y) ;
  5609. 				}
  5610.  
  5611. 				//TODO: Shouldn't also players and the like switch targets?
  5612. 				map_foreachinrange(skill_chastle_mob_changetarget,src,
  5613. 					AREA_SIZE, BL_MOB, bl, src);
  5614. 			}
  5615. 		}
  5616. 		// Failed
  5617. 		else if (hd && hd->master)
  5618. 			clif_skill_fail(hd->master, skillid, USESKILL_FAIL_LEVEL, 0);
  5619. 		else if (sd)
  5620. 			clif_skill_fail(sd, skillid, USESKILL_FAIL_LEVEL, 0);
  5621. 		break;
  5622. 	case HVAN_CHAOTIC:	//[orn]
  5623. 		{
  5624. 			static const int per[5][2]={{20,50},{50,60},{25,75},{60,64},{34,67}};
  5625. 			int rnd = rand()%100;
  5626. 			i = (skilllv-1)%5;
  5627. 			if(rnd<per[i][0]) //Self
  5628. 				bl = src;
  5629. 			else if(rnd<per[i][1]) //Master
  5630. 				bl = battle_get_master(src);
  5631. 			else //Enemy
  5632. 				bl = map_id2bl(battle_gettarget(src));
  5633.  
  5634. 			if (!bl) bl = src;
  5635. 			i = skill_calc_heal(src, bl, skillid, 1+rand()%skilllv, true);
  5636. 			//Eh? why double skill packet?
  5637. 			clif_skill_nodamage(src,bl,AL_HEAL,i,1);
  5638. 			clif_skill_nodamage(src,bl,skillid,i,1);
  5639. 			status_heal(bl, i, 0, 0);
  5640. 		}
  5641. 		break;
  5642. 	//Homun single-target support skills [orn]
  5643. 	case HAMI_BLOODLUST:
  5644. 	case HFLI_FLEET:
  5645. 	case HFLI_SPEED:
  5646. 	case HLIF_CHANGE:
  5647. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5648. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  5649. 		if (hd)
  5650. 			skill_blockhomun_start(hd, skillid, skill_get_time2(skillid,skilllv));
  5651. 		break;
  5652.  
  5653. 	case NPC_DRAGONFEAR:
  5654. 		if (flag&1) {
  5655. 			const enum sc_type sc[] = { SC_STUN, SC_SILENCE, SC_CONFUSION, SC_BLEEDING };
  5656. 			int j;
  5657. 			j = i = rand()%ARRAYLENGTH(sc);
  5658. 			while ( !sc_start(bl,sc[i],100,skilllv,skill_get_time2(skillid,i+1)) ) {
  5659. 				i++;
  5660. 				if ( i == ARRAYLENGTH(sc) )
  5661. 					i = 0;
  5662. 				if (i == j)
  5663. 					break;
  5664. 			}
  5665. 			break;
  5666. 		}
  5667. 	case NPC_WIDEBLEEDING:
  5668. 	case NPC_WIDECONFUSE:
  5669. 	case NPC_WIDECURSE:
  5670. 	case NPC_WIDEFREEZE:
  5671. 	case NPC_WIDESLEEP:
  5672. 	case NPC_WIDESILENCE:
  5673. 	case NPC_WIDESTONE:
  5674. 	case NPC_WIDESTUN:
  5675. 	case NPC_SLOWCAST:
  5676. 	case NPC_WIDEHELLDIGNITY:
  5677. 		if (flag&1)
  5678. 			sc_start(bl,type,100,skilllv,skill_get_time2(skillid,skilllv));
  5679. 		else {
  5680. 			skill_area_temp[2] = 0; //For SD_PREAMBLE
  5681. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5682. 			map_foreachinrange(skill_area_sub, bl,
  5683. 				skill_get_splash(skillid, skilllv),BL_CHAR,
  5684. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|SD_PREAMBLE|1,
  5685. 				skill_castend_nodamage_id);
  5686. 		}
  5687. 		break;
  5688. 	case NPC_WIDESOULDRAIN:
  5689. 		if (flag&1)
  5690. 			status_percent_damage(src,bl,0,((skilllv-1)%5+1)*20,false);
  5691. 		else {
  5692. 			skill_area_temp[2] = 0; //For SD_PREAMBLE
  5693. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5694. 			map_foreachinrange(skill_area_sub, bl,
  5695. 				skill_get_splash(skillid, skilllv),BL_CHAR,
  5696. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|SD_PREAMBLE|1,
  5697. 				skill_castend_nodamage_id);
  5698. 		}
  5699. 		break;
  5700. 	case NPC_TALK:
  5701. 	case ALL_WEWISH:
  5702. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5703. 		break;
  5704. 	case ALL_BUYING_STORE:
  5705. 		if( sd )
  5706. 		{// players only, skill allows 5 buying slots
  5707. 			clif_skill_nodamage(src, bl, skillid, skilllv, buyingstore_setup(sd, MAX_BUYINGSTORE_SLOTS));
  5708. 		}
  5709. 		break;
  5710. 	default:
  5711. 		ShowWarning("skill_castend_nodamage_id: Unknown skill used:%d\n",skillid);
  5712. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5713. 		map_freeblock_unlock();
  5714. 		return 1;
  5715. 	}
  5716.  
  5717. 	if (dstmd) { //Mob skill event for no damage skills (damage ones are handled in battle_calc_damage) [Skotlex]
  5718. 		mob_log_damage(dstmd, src, 0); //Log interaction (counts as 'attacker' for the exp bonus)
  5719. 		mobskill_event(dstmd, src, tick, MSC_SKILLUSED|(skillid<<16));
  5720. 	}
  5721.  
  5722. 	if( sd && !(flag&1) )
  5723. 	{
  5724. 		if( sd->state.arrow_atk ) //Consume arrow on last invocation to this skill.
  5725. 			battle_consume_ammo(sd, skillid, skilllv);
  5726. 		skill_onskillusage(sd, bl, skillid, tick);
  5727. 		skill_consume_requirement(sd,skillid,skilllv,2);
  5728. 	}
  5729.  
  5730. 	map_freeblock_unlock();
  5731. 	return 0;
  5732. }
  5733.  
  5734. /*==========================================
  5735.  *
  5736.  *------------------------------------------*/
  5737. int skill_castend_id(int tid, unsigned int tick, int id, intptr_t data)
  5738. {
  5739. 	struct block_list *target, *src;
  5740. 	struct map_session_data *sd;
  5741. 	struct mob_data *md;
  5742. 	struct unit_data *ud;
  5743. 	struct status_change *sc = NULL;
  5744. 	int inf,inf2,flag = 0;
  5745.  
  5746. 	src = map_id2bl(id);
  5747. 	if( src == NULL )
  5748. 	{
  5749. 		ShowDebug("skill_castend_id: src == NULL (tid=%d, id=%d)\n", tid, id);
  5750. 		return 0;// not found
  5751. 	}
  5752.  
  5753. 	ud = unit_bl2ud(src);
  5754. 	if( ud == NULL )
  5755. 	{
  5756. 		ShowDebug("skill_castend_id: ud == NULL (tid=%d, id=%d)\n", tid, id);
  5757. 		return 0;// ???
  5758. 	}
  5759.  
  5760. 	sd = BL_CAST(BL_PC,  src);
  5761. 	md = BL_CAST(BL_MOB, src);
  5762.  
  5763. 	if( src->prev == NULL ) {
  5764. 		ud->skilltimer = INVALID_TIMER;
  5765. 		return 0;
  5766. 	}
  5767.  
  5768. 	if(ud->skillid != SA_CASTCANCEL )
  5769. 	{// otherwise handled in unit_skillcastcancel()
  5770. 		if( ud->skilltimer != tid ) {
  5771. 			ShowError("skill_castend_id: Timer mismatch %d!=%d!\n", ud->skilltimer, tid);
  5772. 			ud->skilltimer = INVALID_TIMER;
  5773. 			return 0;
  5774. 		}
  5775.  
  5776. 		if( sd && ud->skilltimer != INVALID_TIMER && pc_checkskill(sd,SA_FREECAST) > 0 )
  5777. 		{// restore original walk speed
  5778. 			ud->skilltimer = INVALID_TIMER;
  5779. 			status_calc_bl(&sd->bl, SCB_SPEED);
  5780. 		}
  5781.  
  5782. 		ud->skilltimer = INVALID_TIMER;
  5783. 	}
  5784.  
  5785. 	if (ud->skilltarget == id)
  5786. 		target = src;
  5787. 	else
  5788. 		target = map_id2bl(ud->skilltarget);
  5789.  
  5790. 	// Use a do so that you can break out of it when the skill fails.
  5791. 	do {
  5792. 		if(!target || target->prev==NULL) break;
  5793.  
  5794. 		if(src->m != target->m || status_isdead(src)) break;
  5795.  
  5796. 		switch (ud->skillid) {
  5797. 			//These should become skill_castend_pos
  5798. 			case WE_CALLPARTNER:
  5799. 				if(sd) clif_callpartner(sd);
  5800. 			case WE_CALLPARENT:
  5801. 			case WE_CALLBABY:
  5802. 			case AM_RESURRECTHOMUN:
  5803. 			case PF_SPIDERWEB:
  5804. 				//Find a random spot to place the skill. [Skotlex]
  5805. 				inf2 = skill_get_splash(ud->skillid, ud->skilllv);
  5806. 				ud->skillx = target->x + inf2;
  5807. 				ud->skilly = target->y + inf2;
  5808. 				if (inf2 && !map_random_dir(target, &ud->skillx, &ud->skilly)) {
  5809. 					ud->skillx = target->x;
  5810. 					ud->skilly = target->y;
  5811. 				}
  5812. 				ud->skilltimer=tid;
  5813. 				return skill_castend_pos(tid,tick,id,data);
  5814. 		}
  5815.  
  5816. 		if(ud->skillid == RG_BACKSTAP) {
  5817. 			int dir = map_calc_dir(src,target->x,target->y),t_dir = unit_getdir(target);
  5818. 			if(check_distance_bl(src, target, 0) || map_check_dir(dir,t_dir)) {
  5819. 				break;
  5820. 			}
  5821. 		}
  5822.  
  5823. 		if( ud->skillid == PR_TURNUNDEAD )
  5824. 		{
  5825. 			struct status_data *tstatus = status_get_status_data(target);
  5826. 			if( !battle_check_undead(tstatus->race, tstatus->def_ele) )
  5827. 				break;
  5828. 		}
  5829.  
  5830. 		if( ud->skillid == PR_LEXDIVINA || ud->skillid == MER_LEXDIVINA )
  5831. 		{
  5832. 			sc = status_get_sc(target);
  5833. 			if( battle_check_target(src,target, BCT_ENEMY) <= 0 && (!sc || !sc->data[SC_SILENCE]) )
  5834. 			{ //If it's not an enemy, and not silenced, you can't use the skill on them. [Skotlex]
  5835. 				clif_skill_nodamage (src, target, ud->skillid, ud->skilllv, 0);
  5836. 				break;
  5837. 			}
  5838. 		}
  5839. 		else
  5840. 		{ // Check target validity.
  5841. 			inf = skill_get_inf(ud->skillid);
  5842. 			inf2 = skill_get_inf2(ud->skillid);
  5843.  
  5844. 			if(inf&INF_ATTACK_SKILL ||
  5845. 				(inf&INF_SELF_SKILL && inf2&INF2_NO_TARGET_SELF)) //Combo skills
  5846. 				inf = BCT_ENEMY; //Offensive skill.
  5847. 			else if(inf2&INF2_NO_ENEMY)
  5848. 				inf = BCT_NOENEMY;
  5849. 			else
  5850. 				inf = 0;
  5851.  
  5852. 			if(inf2 & (INF2_PARTY_ONLY|INF2_GUILD_ONLY) && src != target)
  5853. 			{
  5854. 				inf |=
  5855. 					(inf2&INF2_PARTY_ONLY?BCT_PARTY:0)|
  5856. 					(inf2&INF2_GUILD_ONLY?BCT_GUILD:0);
  5857. 				//Remove neutral targets (but allow enemy if skill is designed to be so)
  5858. 				inf &= ~BCT_NEUTRAL;
  5859. 			}
  5860.  
  5861. 			if( ud->skillid >= SL_SKE && ud->skillid <= SL_SKA && target->type == BL_MOB )
  5862. 			{
  5863. 				if( ((TBL_MOB*)target)->class_ == MOBID_EMPERIUM )
  5864. 					break;
  5865. 			}
  5866. 			else
  5867. 			if (inf && battle_check_target(src, target, inf) <= 0)
  5868. 				break;
  5869.  
  5870. 			if(inf&BCT_ENEMY && (sc = status_get_sc(target)) &&
  5871. 				sc->data[SC_FOGWALL] &&
  5872. 				rand()%100 < 75)
  5873. 		  	{	//Fogwall makes all offensive-type targetted skills fail at 75%
  5874. 				if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  5875. 				break;
  5876. 			}
  5877. 		}
  5878.  
  5879. 		//Avoid doing double checks for instant-cast skills.
  5880. 		if (tid != INVALID_TIMER && !status_check_skilluse(src, target, ud->skillid, 1))
  5881. 			break;
  5882.  
  5883. 		if(md) {
  5884. 			md->last_thinktime=tick +MIN_MOBTHINKTIME;
  5885. 			if(md->skillidx >= 0 && md->db->skill[md->skillidx].emotion >= 0)
  5886. 				clif_emotion(src, md->db->skill[md->skillidx].emotion);
  5887. 		}
  5888.  
  5889. 		if(src != target && battle_config.skill_add_range &&
  5890. 			!check_distance_bl(src, target, skill_get_range2(src,ud->skillid,ud->skilllv)+battle_config.skill_add_range))
  5891. 		{
  5892. 			if (sd) {
  5893. 				clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  5894. 				if(battle_config.skill_out_range_consume) //Consume items anyway. [Skotlex]
  5895. 					skill_consume_requirement(sd,ud->skillid,ud->skilllv,3);
  5896. 			}
  5897. 			break;
  5898. 		}
  5899.  
  5900. 		if( sd )
  5901. 		{
  5902. 			if( !skill_check_condition_castend(sd, ud->skillid, ud->skilllv) )
  5903. 				break;
  5904. 			else
  5905. 				skill_consume_requirement(sd,ud->skillid,ud->skilllv,1);
  5906. 		}
  5907.  
  5908. 		if( (src->type == BL_MER || src->type == BL_HOM) && !skill_check_condition_mercenary(src, ud->skillid, ud->skilllv, 1) )
  5909. 			break;
  5910.  
  5911. 		if (ud->state.running && ud->skillid == TK_JUMPKICK)
  5912. 			flag = 1;
  5913.  
  5914. 		if (ud->walktimer != INVALID_TIMER && ud->skillid != TK_RUN)
  5915. 			unit_stop_walking(src,1);
  5916.  
  5917. 		if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  5918. 			ud->canact_tick = tick + skill_delayfix(src, ud->skillid, ud->skilllv); //Tests show wings don't overwrite the delay but skill scrolls do. [Inkfish]
  5919. 		if( battle_config.display_status_timers && sd )
  5920. 			clif_status_change(src, SI_ACTIONDELAY, 1, skill_delayfix(src, ud->skillid, ud->skilllv));
  5921. 		if( sd )
  5922. 		{
  5923. 			switch( ud->skillid )
  5924. 			{
  5925. 			case GS_DESPERADO:
  5926. 				sd->canequip_tick = tick + skill_get_time(ud->skillid, ud->skilllv);
  5927. 				break;
  5928. 			case CR_GRANDCROSS:
  5929. 			case NPC_GRANDDARKNESS:
  5930. 				if( (sc = status_get_sc(src)) && sc->data[SC_STRIPSHIELD] )
  5931. 				{
  5932. 					const struct TimerData *timer = get_timer(sc->data[SC_STRIPSHIELD]->timer);
  5933. 					if( timer && timer->func == status_change_timer && DIFF_TICK(timer->tick,gettick()+skill_get_time(ud->skillid, ud->skilllv)) > 0 )
  5934. 						break;
  5935. 				}
  5936. 				sc_start2(src, SC_STRIPSHIELD, 100, 0, 1, skill_get_time(ud->skillid, ud->skilllv));
  5937. 				break;
  5938. 			}
  5939. 		}
  5940. 		if (skill_get_state(ud->skillid) != ST_MOVE_ENABLE)
  5941. 			unit_set_walkdelay(src, tick, battle_config.default_walk_delay+skill_get_walkdelay(ud->skillid, ud->skilllv), 1);
  5942.  
  5943. 		if(battle_config.skill_log && battle_config.skill_log&src->type)
  5944. 			ShowInfo("Type %d, ID %d skill castend id [id =%d, lv=%d, target ID %d]\n",
  5945. 				src->type, src->id, ud->skillid, ud->skilllv, target->id);
  5946.  
  5947. 		map_freeblock_lock();
  5948. 		if (skill_get_casttype(ud->skillid) == CAST_NODAMAGE)
  5949. 			skill_castend_nodamage_id(src,target,ud->skillid,ud->skilllv,tick,flag);
  5950. 		else
  5951. 			skill_castend_damage_id(src,target,ud->skillid,ud->skilllv,tick,flag);
  5952.  
  5953. 		sc = status_get_sc(src);
  5954. 		if(sc && sc->count) {
  5955. 		  	if(sc->data[SC_MAGICPOWER] &&
  5956. 				ud->skillid != HW_MAGICPOWER && ud->skillid != WZ_WATERBALL)
  5957. 				status_change_end(src, SC_MAGICPOWER, INVALID_TIMER);
  5958. 			if(sc->data[SC_SPIRIT] &&
  5959. 				sc->data[SC_SPIRIT]->val2 == SL_WIZARD &&
  5960. 				sc->data[SC_SPIRIT]->val3 == ud->skillid &&
  5961. 			  	ud->skillid != WZ_WATERBALL)
  5962. 				sc->data[SC_SPIRIT]->val3 = 0; //Clear bounced spell check.
  5963.  
  5964. 			if( sc->data[SC_DANCING] && skill_get_inf2(ud->skillid)&INF2_SONG_DANCE && sd )
  5965. 				skill_blockpc_start(sd,BD_ADAPTATION,3000);
  5966. 		}
  5967.  
  5968. 		if( sd && ud->skillid != SA_ABRACADABRA ) // Hocus-Pocus has just set the data so leave it as it is.[Inkfish]
  5969. 			sd->skillitem = sd->skillitemlv = 0;
  5970.  
  5971. 		if (ud->skilltimer == INVALID_TIMER) {
  5972. 			if(md) md->skillidx = -1;
  5973. 			else ud->skillid = 0; //mobs can't clear this one as it is used for skill condition 'afterskill'
  5974. 			ud->skilllv = ud->skilltarget = 0;
  5975. 		}
  5976. 		map_freeblock_unlock();
  5977. 		return 1;
  5978. 	} while(0);
  5979.  
  5980. 	//Skill failed.
  5981. 	if (ud->skillid == MO_EXTREMITYFIST && sd && !(sc && sc->data[SC_FOGWALL]))
  5982.   	{	//When Asura fails... (except when it fails from Fog of Wall)
  5983. 		//Consume SP/spheres
  5984. 		skill_consume_requirement(sd,ud->skillid, ud->skilllv,1);
  5985. 		status_set_sp(src, 0, 0);
  5986. 		sc = &sd->sc;
  5987. 		if (sc->count)
  5988. 		{	//End states
  5989. 			status_change_end(src, SC_EXPLOSIONSPIRITS, INVALID_TIMER);
  5990. 			status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  5991. 		}
  5992. 		if (target && target->m == src->m)
  5993. 		{	//Move character to target anyway.
  5994. 			int dx,dy;
  5995. 			dx = target->x - src->x;
  5996. 			dy = target->y - src->y;
  5997. 			if(dx > 0) dx++;
  5998. 			else if(dx < 0) dx--;
  5999. 			if (dy > 0) dy++;
  6000. 			else if(dy < 0) dy--;
  6001.  
  6002. 			if (unit_movepos(src, src->x+dx, src->y+dy, 1, 1))
  6003. 			{	//Display movement + animation.
  6004. 				clif_slide(src,src->x,src->y);
  6005. 				clif_skill_damage(src,target,tick,sd->battle_status.amotion,0,0,1,ud->skillid, ud->skilllv, 5);
  6006. 			}
  6007. 			clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6008. 		}
  6009. 	}
  6010.  
  6011. 	ud->skillid = ud->skilllv = ud->skilltarget = 0;
  6012. 	if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  6013. 		ud->canact_tick = tick;
  6014. 	//You can't place a skill failed packet here because it would be
  6015. 	//sent in ALL cases, even cases where skill_check_condition fails
  6016. 	//which would lead to double 'skill failed' messages u.u [Skotlex]
  6017. 	if(sd)
  6018. 		sd->skillitem = sd->skillitemlv = 0;
  6019. 	else if(md)
  6020. 		md->skillidx = -1;
  6021. 	return 0;
  6022. }
  6023.  
  6024. /*==========================================
  6025.  *
  6026.  *------------------------------------------*/
  6027. int skill_castend_pos(int tid, unsigned int tick, int id, intptr_t data)
  6028. {
  6029. 	struct block_list* src = map_id2bl(id);
  6030. 	int maxcount;
  6031. 	struct map_session_data *sd;
  6032. 	struct unit_data *ud = unit_bl2ud(src);
  6033. 	struct mob_data *md;
  6034.  
  6035. 	nullpo_ret(ud);
  6036.  
  6037. 	sd = BL_CAST(BL_PC , src);
  6038. 	md = BL_CAST(BL_MOB, src);
  6039.  
  6040. 	if( src->prev == NULL ) {
  6041. 		ud->skilltimer = INVALID_TIMER;
  6042. 		return 0;
  6043. 	}
  6044.  
  6045. 	if( ud->skilltimer != tid )
  6046. 	{
  6047. 		ShowError("skill_castend_pos: Timer mismatch %d!=%d\n", ud->skilltimer, tid);
  6048. 		ud->skilltimer = INVALID_TIMER;
  6049. 		return 0;
  6050. 	}
  6051.  
  6052. 	if( sd && ud->skilltimer != INVALID_TIMER && pc_checkskill(sd,SA_FREECAST) > 0 )
  6053. 	{// restore original walk speed
  6054. 		ud->skilltimer = INVALID_TIMER;
  6055. 		status_calc_bl(&sd->bl, SCB_SPEED);
  6056. 	}
  6057. 	ud->skilltimer = INVALID_TIMER;
  6058.  
  6059. 	do {
  6060. 		if( status_isdead(src) )
  6061. 			break;
  6062.  
  6063. 		if( !(src->type&battle_config.skill_reiteration) &&
  6064. 			skill_get_unit_flag(ud->skillid)&UF_NOREITERATION &&
  6065. 			skill_check_unit_range(src,ud->skillx,ud->skilly,ud->skillid,ud->skilllv)
  6066. 		  )
  6067. 		{
  6068. 			if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6069. 			break;
  6070. 		}
  6071. 		if( src->type&battle_config.skill_nofootset &&
  6072. 			skill_get_unit_flag(ud->skillid)&UF_NOFOOTSET &&
  6073. 			skill_check_unit_range2(src,ud->skillx,ud->skilly,ud->skillid,ud->skilllv)
  6074. 		  )
  6075. 		{
  6076. 			if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6077. 			break;
  6078. 		}
  6079. 		if( src->type&battle_config.land_skill_limit &&
  6080. 			(maxcount = skill_get_maxcount(ud->skillid, ud->skilllv)) > 0
  6081. 		  ) {
  6082. 			int i;
  6083. 			for(i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i] && maxcount;i++) {
  6084. 				if(ud->skillunit[i]->skill_id == ud->skillid)
  6085. 					maxcount--;
  6086. 			}
  6087. 			if( maxcount == 0 )
  6088. 			{
  6089. 				if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6090. 				break;
  6091. 			}
  6092. 		}
  6093.  
  6094. 		if(tid != INVALID_TIMER)
  6095. 		{	//Avoid double checks on instant cast skills. [Skotlex]
  6096. 			if (!status_check_skilluse(src, NULL, ud->skillid, 1))
  6097. 				break;
  6098. 			if(battle_config.skill_add_range &&
  6099. 				!check_distance_blxy(src, ud->skillx, ud->skilly, skill_get_range2(src,ud->skillid,ud->skilllv)+battle_config.skill_add_range)) {
  6100. 				if (sd && battle_config.skill_out_range_consume) //Consume items anyway.
  6101. 					skill_consume_requirement(sd,ud->skillid,ud->skilllv,3);
  6102. 				break;
  6103. 			}
  6104. 		}
  6105.  
  6106. 		if( sd )
  6107. 		{
  6108. 			if( !skill_check_condition_castend(sd, ud->skillid, ud->skilllv) )
  6109. 				break;
  6110. 			else
  6111. 				skill_consume_requirement(sd,ud->skillid,ud->skilllv,1);
  6112. 		}
  6113.  
  6114. 		if( (src->type == BL_MER || src->type == BL_HOM) && !skill_check_condition_mercenary(src, ud->skillid, ud->skilllv, 1) )
  6115. 			break;
  6116.  
  6117. 		if(md) {
  6118. 			md->last_thinktime=tick +MIN_MOBTHINKTIME;
  6119. 			if(md->skillidx >= 0 && md->db->skill[md->skillidx].emotion >= 0)
  6120. 				clif_emotion(src, md->db->skill[md->skillidx].emotion);
  6121. 		}
  6122.  
  6123. 		if(battle_config.skill_log && battle_config.skill_log&src->type)
  6124. 			ShowInfo("Type %d, ID %d skill castend pos [id =%d, lv=%d, (%d,%d)]\n",
  6125. 				src->type, src->id, ud->skillid, ud->skilllv, ud->skillx, ud->skilly);
  6126.  
  6127. 		if (ud->walktimer != INVALID_TIMER)
  6128. 			unit_stop_walking(src,1);
  6129.  
  6130. 		if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  6131. 			ud->canact_tick = tick + skill_delayfix(src, ud->skillid, ud->skilllv);
  6132. 		if( battle_config.display_status_timers && sd )
  6133. 			clif_status_change(src, SI_ACTIONDELAY, 1, skill_delayfix(src, ud->skillid, ud->skilllv));
  6134. //		if( sd )
  6135. //		{
  6136. //			switch( ud->skillid )
  6137. //			{
  6138. //			case ????:
  6139. //				sd->canequip_tick = tick + ????;
  6140. //				break;
  6141. //			}
  6142. //		}
  6143. 		unit_set_walkdelay(src, tick, battle_config.default_walk_delay+skill_get_walkdelay(ud->skillid, ud->skilllv), 1);
  6144.  
  6145. 		map_freeblock_lock();
  6146. 		skill_castend_pos2(src,ud->skillx,ud->skilly,ud->skillid,ud->skilllv,tick,0);
  6147.  
  6148. 		if( sd && sd->skillitem != AL_WARP ) // Warp-Portal thru items will clear data in skill_castend_map. [Inkfish]
  6149. 			sd->skillitem = sd->skillitemlv = 0;
  6150.  
  6151. 		if (ud->skilltimer == INVALID_TIMER) {
  6152. 			if (md) md->skillidx = -1;
  6153. 			else ud->skillid = 0; //Non mobs can't clear this one as it is used for skill condition 'afterskill'
  6154. 			ud->skilllv = ud->skillx = ud->skilly = 0;
  6155. 		}
  6156.  
  6157. 		map_freeblock_unlock();
  6158. 		return 1;
  6159. 	} while(0);
  6160.  
  6161. 	if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  6162. 		ud->canact_tick = tick;
  6163. 	ud->skillid = ud->skilllv = 0;
  6164. 	if(sd)
  6165. 		sd->skillitem = sd->skillitemlv = 0;
  6166. 	else if(md)
  6167. 		md->skillidx  = -1;
  6168. 	return 0;
  6169.  
  6170. }
  6171.  
  6172. /*==========================================
  6173.  *
  6174.  *------------------------------------------*/
  6175. int skill_castend_pos2(struct block_list* src, int x, int y, int skillid, int skilllv, unsigned int tick, int flag)
  6176. {
  6177. 	struct map_session_data* sd;
  6178. 	struct status_change* sc;
  6179. 	struct status_change_entry *sce;
  6180. 	struct skill_unit_group* sg;
  6181. 	enum sc_type type;
  6182. 	int i;
  6183.  
  6184. 	//if(skilllv <= 0) return 0;
  6185. 	if(skillid > 0 && skilllv <= 0) return 0;	// celest
  6186.  
  6187. 	nullpo_ret(src);
  6188.  
  6189. 	if(status_isdead(src))
  6190. 		return 0;
  6191.  
  6192. 	sd = BL_CAST(BL_PC, src);
  6193.  
  6194. 	sc = status_get_sc(src);
  6195. 	type = status_skill2sc(skillid);
  6196. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  6197.  
  6198. 	switch (skillid) { //Skill effect.
  6199. 		case WZ_METEOR:
  6200. 		case MO_BODYRELOCATION:
  6201. 		case CR_CULTIVATION:
  6202. 		case HW_GANBANTEIN:
  6203. 			break; //Effect is displayed on respective switch case.
  6204. 		default:
  6205. 			if(skill_get_inf(skillid)&INF_SELF_SKILL)
  6206. 				clif_skill_nodamage(src,src,skillid,skilllv,1);
  6207. 			else
  6208. 				clif_skill_poseffect(src,skillid,skilllv,x,y,tick);
  6209. 	}
  6210.  
  6211. 	switch(skillid)
  6212. 	{
  6213. 	case PR_BENEDICTIO:
  6214. 		skill_area_temp[1] = src->id;
  6215. 		i = skill_get_splash(skillid, skilllv);
  6216. 		map_foreachinarea(skill_area_sub,
  6217. 			src->m, x-i, y-i, x+i, y+i, BL_PC,
  6218. 			src, skillid, skilllv, tick, flag|BCT_ALL|1,
  6219. 			skill_castend_nodamage_id);
  6220. 		map_foreachinarea(skill_area_sub,
  6221. 			src->m, x-i, y-i, x+i, y+i, BL_CHAR,
  6222. 			src, skillid, skilllv, tick, flag|BCT_ENEMY|1,
  6223. 			skill_castend_damage_id);
  6224. 		break;
  6225.  
  6226. 	case BS_HAMMERFALL:
  6227. 		i = skill_get_splash(skillid, skilllv);
  6228. 		map_foreachinarea (skill_area_sub,
  6229. 			src->m, x-i, y-i, x+i, y+i, BL_CHAR,
  6230. 			src, skillid, skilllv, tick, flag|BCT_ENEMY|2,
  6231. 			skill_castend_nodamage_id);
  6232. 		break;
  6233.  
  6234. 	case HT_DETECTING:
  6235. 		i = skill_get_splash(skillid, skilllv);
  6236. 		map_foreachinarea( status_change_timer_sub,
  6237. 			src->m, x-i, y-i, x+i,y+i,BL_CHAR,
  6238. 			src,NULL,SC_SIGHT,tick);
  6239. 		if(battle_config.traps_setting&1)
  6240. 		map_foreachinarea( skill_reveal_trap,
  6241. 			src->m, x-i, y-i, x+i,y+i,BL_SKILL);
  6242. 		break;
  6243.  
  6244. 	case SA_VOLCANO:
  6245. 	case SA_DELUGE:
  6246. 	case SA_VIOLENTGALE:
  6247. 	{	//Does not consumes if the skill is already active. [Skotlex]
  6248. 		struct skill_unit_group *sg;
  6249. 		if ((sg= skill_locate_element_field(src)) != NULL && ( sg->skill_id == SA_VOLCANO || sg->skill_id == SA_DELUGE || sg->skill_id == SA_VIOLENTGALE ))
  6250. 		{
  6251. 			if (sg->limit - DIFF_TICK(gettick(), sg->tick) > 0)
  6252. 			{
  6253. 				skill_unitsetting(src,skillid,skilllv,x,y,0);
  6254. 				return 0; // not to consume items
  6255. 			}
  6256. 			else
  6257. 				sg->limit = 0; //Disable it.
  6258. 		}
  6259. 		skill_unitsetting(src,skillid,skilllv,x,y,0);
  6260. 		break;
  6261. 	}
  6262. 	case MG_SAFETYWALL:
  6263. 	case MG_FIREWALL:
  6264. 	case MG_THUNDERSTORM:
  6265. 	case AL_PNEUMA:
  6266. 	case WZ_ICEWALL:
  6267. 	case WZ_FIREPILLAR:
  6268. 	case WZ_QUAGMIRE:
  6269. 	case WZ_VERMILION:
  6270. 	case WZ_STORMGUST:
  6271. 	case WZ_HEAVENDRIVE:
  6272. 	case PR_SANCTUARY:
  6273. 	case PR_MAGNUS:
  6274. 	case CR_GRANDCROSS:
  6275. 	case NPC_GRANDDARKNESS:
  6276. 	case HT_SKIDTRAP:
  6277. 	case MA_SKIDTRAP:
  6278. 	case HT_LANDMINE:
  6279. 	case MA_LANDMINE:
  6280. 	case HT_ANKLESNARE:
  6281. 	case HT_SHOCKWAVE:
  6282. 	case HT_SANDMAN:
  6283. 	case MA_SANDMAN:
  6284. 	case HT_FLASHER:
  6285. 	case HT_FREEZINGTRAP:
  6286. 	case MA_FREEZINGTRAP:
  6287. 	case HT_BLASTMINE:
  6288. 	case HT_CLAYMORETRAP:
  6289. 	case AS_VENOMDUST:
  6290. 	case AM_DEMONSTRATION:
  6291. 	case PF_FOGWALL:
  6292. 	case PF_SPIDERWEB:
  6293. 	case HT_TALKIEBOX:
  6294. 	case WE_CALLPARTNER:
  6295. 	case WE_CALLPARENT:
  6296. 	case WE_CALLBABY:
  6297. 	case AC_SHOWER:	//Ground-placed skill implementation.
  6298. 	case MA_SHOWER:
  6299. 	case SA_LANDPROTECTOR:
  6300. 	case BD_LULLABY:
  6301. 	case BD_RICHMANKIM:
  6302. 	case BD_ETERNALCHAOS:
  6303. 	case BD_DRUMBATTLEFIELD:
  6304. 	case BD_RINGNIBELUNGEN:
  6305. 	case BD_ROKISWEIL:
  6306. 	case BD_INTOABYSS:
  6307. 	case BD_SIEGFRIED:
  6308. 	case BA_DISSONANCE:
  6309. 	case BA_POEMBRAGI:
  6310. 	case BA_WHISTLE:
  6311. 	case BA_ASSASSINCROSS:
  6312. 	case BA_APPLEIDUN:
  6313. 	case DC_UGLYDANCE:
  6314. 	case DC_HUMMING:
  6315. 	case DC_DONTFORGETME:
  6316. 	case DC_FORTUNEKISS:
  6317. 	case DC_SERVICEFORYOU:
  6318. 	case CG_MOONLIT:
  6319. 	case GS_DESPERADO:
  6320. 	case NJ_KAENSIN:
  6321. 	case NJ_BAKUENRYU:
  6322. 	case NJ_SUITON:
  6323. 	case NJ_HYOUSYOURAKU:
  6324. 	case NJ_RAIGEKISAI:
  6325. 	case NJ_KAMAITACHI:
  6326. 	case NPC_EVILLAND:
  6327. 		flag|=1;//Set flag to 1 to prevent deleting ammo (it will be deleted on group-delete).
  6328. 	case GS_GROUNDDRIFT: //Ammo should be deleted right away.
  6329. 		skill_unitsetting(src,skillid,skilllv,x,y,0);
  6330. 		break;
  6331. 	case RG_GRAFFITI:			/* Graffiti [Valaris] */
  6332. 		skill_clear_unitgroup(src);
  6333. 		skill_unitsetting(src,skillid,skilllv,x,y,0);
  6334. 		flag|=1;
  6335. 		break;
  6336. 	case HP_BASILICA:
  6337. 		if( sc->data[SC_BASILICA] )
  6338. 			status_change_end(src, SC_BASILICA, INVALID_TIMER); // Cancel Basilica
  6339. 		else
  6340. 		{ // Create Basilica. Start SC on caster. Unit timer start SC on others.
  6341. 			skill_clear_unitgroup(src);
  6342. 			if( skill_unitsetting(src,skillid,skilllv,x,y,0) )
  6343. 				sc_start4(src,type,100,skilllv,0,0,src->id,skill_get_time(skillid,skilllv));
  6344. 			flag|=1;
  6345. 		}
  6346. 		break;
  6347. 	case CG_HERMODE:
  6348. 		skill_clear_unitgroup(src);
  6349. 		if ((sg = skill_unitsetting(src,skillid,skilllv,x,y,0)))
  6350. 			sc_start4(src,SC_DANCING,100,
  6351. 				skillid,0,skilllv,sg->group_id,skill_get_time(skillid,skilllv));
  6352. 		flag|=1;
  6353. 		break;
  6354. 	case RG_CLEANER: // [Valaris]
  6355. 		i = skill_get_splash(skillid, skilllv);
  6356. 		map_foreachinarea(skill_graffitiremover,src->m,x-i,y-i,x+i,y+i,BL_SKILL);
  6357. 		break;
  6358.  
  6359. 	case WZ_METEOR:
  6360. 		{
  6361. 			int flag = 0, area = skill_get_splash(skillid, skilllv);
  6362. 			short tmpx = 0, tmpy = 0, x1 = 0, y1 = 0;
  6363.  
  6364. 			if( sc && sc->data[SC_MAGICPOWER] )
  6365. 				flag = flag|2; //Store the magic power flag for future use. [Skotlex]
  6366.  
  6367. 			for( i = 0; i < 2 + (skilllv>>1); i++ )
  6368. 			{
  6369. 				// Creates a random Cell in the Splash Area
  6370. 				tmpx = x - area + rand()%(area * 2 + 1);
  6371. 				tmpy = y - area + rand()%(area * 2 + 1);
  6372.  
  6373. 				if( i == 0 && path_search_long(NULL, src->m, src->x, src->y, tmpx, tmpy, CELL_CHKWALL) )
  6374. 					clif_skill_poseffect(src,skillid,skilllv,tmpx,tmpy,tick);
  6375.  
  6376. 				if( i > 0 )
  6377. 					skill_addtimerskill(src,tick+i*1000,0,tmpx,tmpy,skillid,skilllv,(x1<<16)|y1,flag&2); //Only pass the Magic Power flag
  6378.  
  6379. 				x1 = tmpx;
  6380. 				y1 = tmpy;
  6381. 			}
  6382.  
  6383. 			skill_addtimerskill(src,tick+i*1000,0,tmpx,tmpy,skillid,skilllv,-1,flag&2);
  6384. 		}
  6385. 		break;
  6386.  
  6387. 	case AL_WARP:
  6388. 		if(sd)
  6389. 		{
  6390. 			clif_skill_warppoint(sd, skillid, skilllv, sd->status.save_point.map,
  6391. 				(skilllv >= 2) ? sd->status.memo_point[0].map : 0,
  6392. 				(skilllv >= 3) ? sd->status.memo_point[1].map : 0,
  6393. 				(skilllv >= 4) ? sd->status.memo_point[2].map : 0
  6394. 			);
  6395. 		}
  6396. 		return 0; // not to consume item.
  6397.  
  6398. 	case MO_BODYRELOCATION:
  6399. 		if (unit_movepos(src, x, y, 1, 1)) {
  6400. 			clif_skill_poseffect(src,skillid,skilllv,src->x,src->y,tick);
  6401. //			clif_slide(src, src->x, src->y); //Poseffect is the one that makes the char snap on the client...
  6402. 			//if (sd) skill_blockpc_start (sd, MO_EXTREMITYFIST, 2000);
  6403. 		}
  6404. 		break;
  6405. 	case NJ_SHADOWJUMP:
  6406. 		if( !map_flag_gvg(src->m) && !map[src->m].flag.battleground )
  6407. 		{	//You don't move on GVG grounds.
  6408. 			unit_movepos(src, x, y, 1, 0);
  6409. 			clif_slide(src,x,y);
  6410. 		}
  6411. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  6412. 		break;
  6413. 	case AM_SPHEREMINE:
  6414. 	case AM_CANNIBALIZE:
  6415. 		{
  6416. 			int summons[5] = { 1589, 1579, 1575, 1555, 1590 };
  6417. 			//int summons[5] = { 1020, 1068, 1118, 1500, 1368 };
  6418. 			int class_ = skillid==AM_SPHEREMINE?1142:summons[skilllv-1];
  6419. 			struct mob_data *md;
  6420.  
  6421. 			// Correct info, don't change any of this! [celest]
  6422. 			md = mob_once_spawn_sub(src, src->m, x, y, status_get_name(src),class_,"");
  6423. 			if (md) {
  6424. 				md->master_id = src->id;
  6425. 				md->special_state.ai = skillid==AM_SPHEREMINE?2:3;
  6426. 				if( md->deletetimer != INVALID_TIMER )
  6427. 					delete_timer(md->deletetimer, mob_timer_delete);
  6428. 				md->deletetimer = add_timer (gettick() + skill_get_time(skillid,skilllv), mob_timer_delete, md->bl.id, 0);
  6429. 				mob_spawn (md); //Now it is ready for spawning.
  6430. 			}
  6431. 		}
  6432. 		break;
  6433.  
  6434. 	// Slim Pitcher [Celest]
  6435. 	case CR_SLIMPITCHER:
  6436. 		if (sd) {
  6437. 			int i = skilllv%11 - 1;
  6438. 			int j = pc_search_inventory(sd,skill_db[skillid].itemid[i]);
  6439. 			if( j < 0 || skill_db[skillid].itemid[i] <= 0 || sd->inventory_data[j] == NULL || sd->status.inventory[j].amount < skill_db[skillid].amount[i] )
  6440. 			{
  6441. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6442. 				return 1;
  6443. 			}
  6444. 			potion_flag = 1;
  6445. 			potion_hp = 0;
  6446. 			potion_sp = 0;
  6447. 			run_script(sd->inventory_data[j]->script,0,sd->bl.id,0);
  6448. 			potion_flag = 0;
  6449. 			//Apply skill bonuses
  6450. 			i = pc_checkskill(sd,CR_SLIMPITCHER)*10
  6451. 				+ pc_checkskill(sd,AM_POTIONPITCHER)*10
  6452. 				+ pc_checkskill(sd,AM_LEARNINGPOTION)*5
  6453. 				+ pc_skillheal_bonus(sd, skillid);
  6454.  
  6455. 			potion_hp = potion_hp * (100+i)/100;
  6456. 			potion_sp = potion_sp * (100+i)/100;
  6457.  
  6458. 			if(potion_hp > 0 || potion_sp > 0) {
  6459. 				i = skill_get_splash(skillid, skilllv);
  6460. 				map_foreachinarea(skill_area_sub,
  6461. 					src->m,x-i,y-i,x+i,y+i,BL_CHAR,
  6462. 					src,skillid,skilllv,tick,flag|BCT_PARTY|BCT_GUILD|1,
  6463. 					skill_castend_nodamage_id);
  6464. 			}
  6465. 		} else {
  6466. 			int i = skilllv%11 - 1;
  6467. 			struct item_data *item;
  6468. 			i = skill_db[skillid].itemid[i];
  6469. 			item = itemdb_search(i);
  6470. 			potion_flag = 1;
  6471. 			potion_hp = 0;
  6472. 			potion_sp = 0;
  6473. 			run_script(item->script,0,src->id,0);
  6474. 			potion_flag = 0;
  6475. 			i = skill_get_max(CR_SLIMPITCHER)*10;
  6476.  
  6477. 			potion_hp = potion_hp * (100+i)/100;
  6478. 			potion_sp = potion_sp * (100+i)/100;
  6479.  
  6480. 			if(potion_hp > 0 || potion_sp > 0) {
  6481. 				i = skill_get_splash(skillid, skilllv);
  6482. 				map_foreachinarea(skill_area_sub,
  6483. 					src->m,x-i,y-i,x+i,y+i,BL_CHAR,
  6484. 					src,skillid,skilllv,tick,flag|BCT_PARTY|BCT_GUILD|1,
  6485. 						skill_castend_nodamage_id);
  6486. 			}
  6487. 		}
  6488. 		break;
  6489.  
  6490. 	case HW_GANBANTEIN:
  6491. 		if (rand()%100 < 80) {
  6492. 			int dummy = 1;
  6493. 			clif_skill_poseffect(src,skillid,skilllv,x,y,tick);
  6494. 			i = skill_get_splash(skillid, skilllv);
  6495. 			map_foreachinarea(skill_cell_overlap, src->m, x-i, y-i, x+i, y+i, BL_SKILL, HW_GANBANTEIN, &dummy, src);
  6496. 		} else {
  6497. 			if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6498. 			return 1;
  6499. 		}
  6500. 		break;
  6501.  
  6502. 	case HW_GRAVITATION:
  6503. 		if ((sg = skill_unitsetting(src,skillid,skilllv,x,y,0)))
  6504. 			sc_start4(src,type,100,skilllv,0,BCT_SELF,sg->group_id,skill_get_time(skillid,skilllv));
  6505. 		flag|=1;
  6506. 		break;
  6507.  
  6508. 	// Plant Cultivation [Celest]
  6509. 	case CR_CULTIVATION:
  6510. 		if (sd) {
  6511. 			int i;
  6512. 			if( map_count_oncell(src->m,x,y,BL_CHAR) > 0 )
  6513. 			{
  6514. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6515. 				return 1;
  6516. 			}
  6517. 			clif_skill_poseffect(src,skillid,skilllv,x,y,tick);
  6518. 			if (rand()%100 < 50) {
  6519. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6520. 			} else {
  6521. 				TBL_MOB* md = mob_once_spawn_sub(src, src->m, x, y, "--ja--",(skilllv < 2 ? 1084+rand()%2 : 1078+rand()%6),"");
  6522. 				if (!md) break;
  6523. 				if ((i = skill_get_time(skillid, skilllv)) > 0)
  6524. 				{
  6525. 					if( md->deletetimer != INVALID_TIMER )
  6526. 						delete_timer(md->deletetimer, mob_timer_delete);
  6527. 					md->deletetimer = add_timer (tick + i, mob_timer_delete, md->bl.id, 0);
  6528. 				}
  6529. 				mob_spawn (md);
  6530. 			}
  6531. 		}
  6532. 		break;
  6533.  
  6534. 	case SG_SUN_WARM:
  6535. 	case SG_MOON_WARM:
  6536. 	case SG_STAR_WARM:
  6537. 		skill_clear_unitgroup(src);
  6538. 		if ((sg = skill_unitsetting(src,skillid,skilllv,src->x,src->y,0)))
  6539. 			sc_start4(src,type,100,skilllv,0,0,sg->group_id,skill_get_time(skillid,skilllv));
  6540. 		flag|=1;
  6541. 		break;
  6542.  
  6543. 	case PA_GOSPEL:
  6544. 		if (sce && sce->val4 == BCT_SELF)
  6545. 		{
  6546. 			status_change_end(src, SC_GOSPEL, INVALID_TIMER);
  6547. 			return 0;
  6548. 		}
  6549. 		else
  6550. 	  	{
  6551. 			sg = skill_unitsetting(src,skillid,skilllv,src->x,src->y,0);
  6552. 			if (!sg) break;
  6553. 			if (sce)
  6554. 				status_change_end(src, type, INVALID_TIMER); //Was under someone else's Gospel. [Skotlex]
  6555. 			sc_start4(src,type,100,skilllv,0,sg->group_id,BCT_SELF,skill_get_time(skillid,skilllv));
  6556. 			clif_skill_poseffect(src, skillid, skilllv, 0, 0, tick); // PA_GOSPEL music packet
  6557. 		}
  6558. 		break;
  6559. 	case NJ_TATAMIGAESHI:
  6560. 		if (skill_unitsetting(src,skillid,skilllv,src->x,src->y,0))
  6561. 			sc_start(src,type,100,skilllv,skill_get_time2(skillid,skilllv));
  6562. 		break;
  6563.  
  6564. 	case AM_RESURRECTHOMUN:	//[orn]
  6565. 		if (sd)
  6566. 		{
  6567. 			if (!merc_resurrect_homunculus(sd, 20*skilllv, x, y))
  6568. 			{
  6569. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6570. 				break;
  6571. 			}
  6572. 		}
  6573. 		break;
  6574.  
  6575. 	default:
  6576. 		ShowWarning("skill_castend_pos2: Unknown skill used:%d\n",skillid);
  6577. 		return 1;
  6578. 	}
  6579.  
  6580. 	status_change_end(src, SC_MAGICPOWER, INVALID_TIMER);
  6581.  
  6582. 	if( sd )
  6583. 	{
  6584. 		if( sd->state.arrow_atk && !(flag&1) ) //Consume arrow if a ground skill was not invoked. [Skotlex]
  6585. 			battle_consume_ammo(sd, skillid, skilllv);
  6586. 		skill_onskillusage(sd, NULL, skillid, tick);
  6587. 		skill_consume_requirement(sd,skillid,skilllv,2);
  6588. 	}
  6589.  
  6590. 	return 0;
  6591. }
  6592.  
  6593. /*==========================================
  6594.  *
  6595.  *------------------------------------------*/
  6596. int skill_castend_map (struct map_session_data *sd, short skill_num, const char *map)
  6597. {
  6598. 	nullpo_ret(sd);
  6599.  
  6600. //Simplify skill_failed code.
  6601. #define skill_failed(sd) { sd->menuskill_id = sd->menuskill_val = 0; }
  6602. 	if(skill_num != sd->menuskill_id)
  6603. 		return 0;
  6604.  
  6605. 	if( sd->bl.prev == NULL || pc_isdead(sd) ) {
  6606. 		skill_failed(sd);
  6607. 		return 0;
  6608. 	}
  6609.  
  6610. 	if(sd->sc.opt1 || sd->sc.option&OPTION_HIDE ) {
  6611. 		skill_failed(sd);
  6612. 		return 0;
  6613. 	}
  6614. 	if(sd->sc.count && (
  6615. 		sd->sc.data[SC_SILENCE] ||
  6616. 		sd->sc.data[SC_ROKISWEIL] ||
  6617. 		sd->sc.data[SC_AUTOCOUNTER] ||
  6618. 		sd->sc.data[SC_STEELBODY] ||
  6619. 		sd->sc.data[SC_DANCING] ||
  6620. 		sd->sc.data[SC_BERSERK] ||
  6621. 		sd->sc.data[SC_BASILICA] ||
  6622. 		sd->sc.data[SC_MARIONETTE]
  6623. 	 )) {
  6624. 		skill_failed(sd);
  6625. 		return 0;
  6626. 	}
  6627.  
  6628. 	pc_stop_attack(sd);
  6629. 	pc_stop_walking(sd,0);
  6630.  
  6631. 	if(battle_config.skill_log && battle_config.skill_log&BL_PC)
  6632. 		ShowInfo("PC %d skill castend skill =%d map=%s\n",sd->bl.id,skill_num,map);
  6633.  
  6634. 	if(strcmp(map,"cancel")==0) {
  6635. 		skill_failed(sd);
  6636. 		return 0;
  6637. 	}
  6638.  
  6639. 	switch(skill_num)
  6640. 	{
  6641. 	case AL_TELEPORT:
  6642. 		if(strcmp(map,"Random")==0)
  6643. 			pc_randomwarp(sd,CLR_TELEPORT);
  6644. 		else if (sd->menuskill_val > 1) //Need lv2 to be able to warp here.
  6645. 			pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  6646. 		break;
  6647.  
  6648. 	case AL_WARP:
  6649. 		{
  6650. 			const struct point *p[4];
  6651. 			struct skill_unit_group *group;
  6652. 			int i, lv, wx, wy;
  6653. 			int maxcount=0;
  6654. 			int x,y;
  6655. 			unsigned short mapindex;
  6656.  
  6657. 			mapindex  = mapindex_name2id((char*)map);
  6658. 			if(!mapindex) { //Given map not found?
  6659. 				clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  6660. 				skill_failed(sd);
  6661. 				return 0;
  6662. 			}
  6663. 			p[0] = &sd->status.save_point;
  6664. 			p[1] = &sd->status.memo_point[0];
  6665. 			p[2] = &sd->status.memo_point[1];
  6666. 			p[3] = &sd->status.memo_point[2];
  6667.  
  6668. 			if((maxcount = skill_get_maxcount(skill_num, sd->menuskill_val)) > 0) {
  6669. 				for(i=0;i<MAX_SKILLUNITGROUP && sd->ud.skillunit[i] && maxcount;i++) {
  6670. 					if(sd->ud.skillunit[i]->skill_id == skill_num)
  6671. 						maxcount--;
  6672. 				}
  6673. 				if(!maxcount) {
  6674. 					clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  6675. 					skill_failed(sd);
  6676. 					return 0;
  6677. 				}
  6678. 			}
  6679.  
  6680. 			lv = sd->skillitem==skill_num?sd->skillitemlv:pc_checkskill(sd,skill_num);
  6681. 			wx = sd->menuskill_val>>16;
  6682. 			wy = sd->menuskill_val&0xffff;
  6683.  
  6684. 			if( lv <= 0 ) return 0;
  6685. 			if( lv > 4 ) lv = 4; // crash prevention
  6686.  
  6687. 			// check if the chosen map exists in the memo list
  6688. 			ARR_FIND( 0, lv, i, mapindex == p[i]->map );
  6689. 			if( i < lv ) {
  6690. 				x=p[i]->x;
  6691. 				y=p[i]->y;
  6692. 			} else {
  6693. 				skill_failed(sd);
  6694. 				return 0;
  6695. 			}
  6696.  
  6697. 			if(!skill_check_condition_castend(sd, sd->menuskill_id, lv))
  6698. 			{  // This checks versus skillid/skilllv...
  6699. 				skill_failed(sd);
  6700. 				return 0;
  6701. 			}
  6702.  
  6703. 			skill_consume_requirement(sd,sd->menuskill_id,lv,2);
  6704. 			sd->skillitem = sd->skillitemlv = 0; // Clear data that's skipped in 'skill_castend_pos' [Inkfish]
  6705.  
  6706. 			if((group=skill_unitsetting(&sd->bl,skill_num,lv,wx,wy,0))==NULL) {
  6707. 				skill_failed(sd);
  6708. 				return 0;
  6709. 			}
  6710.  
  6711. 			// record the destination coordinates
  6712. 			group->val2 = (x<<16)|y;
  6713. 			group->val3 = mapindex;
  6714. 		}
  6715. 		break;
  6716. 	}
  6717.  
  6718. 	sd->menuskill_id = sd->menuskill_val = 0;
  6719. 	return 0;
  6720. #undef skill_failed
  6721. }
  6722.  
  6723. /// transforms 'target' skill unit into dissonance (if conditions are met)
  6724. static int skill_dance_overlap_sub(struct block_list* bl, va_list ap)
  6725. {
  6726. 	struct skill_unit* target = (struct skill_unit*)bl;
  6727. 	struct skill_unit* src = va_arg(ap, struct skill_unit*);
  6728. 	int flag = va_arg(ap, int);
  6729.  
  6730. 	if (src == target)
  6731. 		return 0;
  6732. 	if (!target->group || !(target->group->state.song_dance&0x1))
  6733. 		return 0;
  6734. 	if (!(target->val2 & src->val2 & ~UF_ENSEMBLE)) //They don't match (song + dance) is valid.
  6735. 		return 0;
  6736.  
  6737. 	if (flag) //Set dissonance
  6738. 		target->val2 |= UF_ENSEMBLE; //Add ensemble to signal this unit is overlapping.
  6739. 	else //Remove dissonance
  6740. 		target->val2 &= ~UF_ENSEMBLE;
  6741.  
  6742. 	clif_skill_setunit(target); //Update look of affected cell.
  6743.  
  6744. 	return 1;
  6745. }
  6746.  
  6747. //Does the song/dance overlapping -> dissonance check. [Skotlex]
  6748. //When flag is 0, this unit is about to be removed, cancel the dissonance effect
  6749. //When 1, this unit has been positioned, so start the cancel effect.
  6750. int skill_dance_overlap(struct skill_unit* unit, int flag)
  6751. {
  6752. 	if (!unit || !unit->group || !(unit->group->state.song_dance&0x1))
  6753. 		return 0;
  6754. 	if (!flag && !(unit->val2&UF_ENSEMBLE))
  6755. 		return 0; //Nothing to remove, this unit is not overlapped.
  6756.  
  6757. 	if (unit->val1 != unit->group->skill_id)
  6758. 	{	//Reset state
  6759. 		unit->val1 = unit->group->skill_id;
  6760. 		unit->val2 &= ~UF_ENSEMBLE;
  6761. 	}
  6762.  
  6763. 	return map_foreachincell(skill_dance_overlap_sub, unit->bl.m,unit->bl.x,unit->bl.y,BL_SKILL, unit,flag);
  6764. }
  6765.  
  6766. /*==========================================
  6767.  * Converts this group information so that it is handled as a Dissonance or Ugly Dance cell.
  6768.  * Flag: 0 - Convert, 1 - Revert.
  6769.  *------------------------------------------*/
  6770. static bool skill_dance_switch(struct skill_unit* unit, int flag)
  6771. {
  6772. 	static int prevflag = 1;  // by default the backup is empty
  6773. 	static struct skill_unit_group backup;
  6774. 	struct skill_unit_group* group = unit->group;
  6775.  
  6776. 	// val2&UF_ENSEMBLE is a hack to indicate dissonance
  6777. 	if ( !(group->state.song_dance&0x1 && unit->val2&UF_ENSEMBLE) )
  6778. 		return false;
  6779.  
  6780. 	if( flag == prevflag )
  6781. 	{// protection against attempts to read an empty backup / write to a full backup
  6782. 		ShowError("skill_dance_switch: Attempted to %s (skill_id=%d, skill_lv=%d, src_id=%d).\n",
  6783. 			flag ? "read an empty backup" : "write to a full backup",
  6784. 			group->skill_id, group->skill_lv, group->src_id);
  6785. 		return false;
  6786. 	}
  6787. 	prevflag = flag;
  6788.  
  6789. 	if( !flag )
  6790. 	{	//Transform
  6791. 		int skillid = unit->val2&UF_SONG ? BA_DISSONANCE : DC_UGLYDANCE;
  6792.  
  6793. 		// backup
  6794. 		backup.skill_id    = group->skill_id;
  6795. 		backup.skill_lv    = group->skill_lv;
  6796. 		backup.unit_id     = group->unit_id;
  6797. 		backup.target_flag = group->target_flag;
  6798. 		backup.bl_flag     = group->bl_flag;
  6799. 		backup.interval    = group->interval;
  6800.  
  6801. 		// replace
  6802. 		group->skill_id    = skillid;
  6803. 		group->skill_lv    = 1;
  6804. 		group->unit_id     = skill_get_unit_id(skillid,0);
  6805. 		group->target_flag = skill_get_unit_target(skillid);
  6806. 		group->bl_flag     = skill_get_unit_bl_target(skillid);
  6807. 		group->interval    = skill_get_unit_interval(skillid);
  6808. 	}
  6809. 	else
  6810. 	{	//Restore
  6811. 		group->skill_id    = backup.skill_id;
  6812. 		group->skill_lv    = backup.skill_lv;
  6813. 		group->unit_id     = backup.unit_id;
  6814. 		group->target_flag = backup.target_flag;
  6815. 		group->bl_flag     = backup.bl_flag;
  6816. 		group->interval    = backup.interval;
  6817. 	}
  6818.  
  6819. 	return true;
  6820. }
  6821.  
  6822. /*==========================================
  6823.  * Initializes and sets a ground skill.
  6824.  * flag&1 is used to determine when the skill 'morphs' (Warp portal becomes active, or Fire Pillar becomes active)
  6825.  * flag&2 is used to determine if this skill was casted with Magic Power active.
  6826.  *------------------------------------------*/
  6827. struct skill_unit_group* skill_unitsetting (struct block_list *src, short skillid, short skilllv, short x, short y, int flag)
  6828. {
  6829. 	struct skill_unit_group *group;
  6830. 	int i,limit,val1=0,val2=0,val3=0;
  6831. 	int target,interval,range,unit_flag;
  6832. 	struct s_skill_unit_layout *layout;
  6833. 	struct map_session_data *sd;
  6834. 	struct status_data *status;
  6835. 	struct status_change *sc;
  6836. 	int active_flag=1;
  6837. 	int subunt=0;
  6838.  
  6839. 	nullpo_retr(NULL, src);
  6840.  
  6841. 	limit = skill_get_time(skillid,skilllv);
  6842. 	range = skill_get_unit_range(skillid,skilllv);
  6843. 	interval = skill_get_unit_interval(skillid);
  6844. 	target = skill_get_unit_target(skillid);
  6845. 	unit_flag = skill_get_unit_flag(skillid);
  6846. 	layout = skill_get_unit_layout(skillid,skilllv,src,x,y);
  6847.  
  6848. 	sd = BL_CAST(BL_PC, src);
  6849. 	status = status_get_status_data(src);
  6850. 	sc = status_get_sc(src);	// for traps, firewall and fogwall - celest
  6851.  
  6852. 	switch( skillid )
  6853. 	{
  6854. 	case MG_SAFETYWALL:
  6855. 		val2=skilllv+1;
  6856. 		break;
  6857. 	case MG_FIREWALL:
  6858. 		if(sc && sc->data[SC_VIOLENTGALE])
  6859. 			limit = limit*3/2;
  6860. 		val2=4+skilllv;
  6861. 		break;
  6862.  
  6863. 	case AL_WARP:
  6864. 		val1=skilllv+6;
  6865. 		if(!(flag&1))
  6866. 			limit=2000;
  6867. 		else // previous implementation (not used anymore)
  6868. 		{	//Warp Portal morphing to active mode, extract relevant data from src. [Skotlex]
  6869. 			if( src->type != BL_SKILL ) return NULL;
  6870. 			group = ((TBL_SKILL*)src)->group;
  6871. 			src = map_id2bl(group->src_id);
  6872. 			if( !src ) return NULL;
  6873. 			val2 = group->val2; //Copy the (x,y) position you warp to
  6874. 			val3 = group->val3; //as well as the mapindex to warp to.
  6875. 		}
  6876. 		break;
  6877. 	case HP_BASILICA:
  6878. 		val1 = src->id; // Store caster id.
  6879. 		break;
  6880.  
  6881. 	case PR_SANCTUARY:
  6882. 	case NPC_EVILLAND:
  6883. 		val1=(skilllv+3)*2;
  6884. 		break;
  6885.  
  6886. 	case WZ_FIREPILLAR:
  6887. 		if((flag&1)!=0)
  6888. 			limit=1000;
  6889. 		val1=skilllv+2;
  6890. 		break;
  6891. 	case WZ_QUAGMIRE:	//The target changes to "all" if used in a gvg map. [Skotlex]
  6892. 	case AM_DEMONSTRATION:
  6893. 		if (map_flag_vs(src->m) && battle_config.vs_traps_bctall
  6894. 			&& (src->type&battle_config.vs_traps_bctall))
  6895. 			target = BCT_ALL;
  6896. 		break;
  6897. 	case HT_SHOCKWAVE:
  6898. 		val1=skilllv*15+10;
  6899. 	case HT_SANDMAN:
  6900. 	case MA_SANDMAN:
  6901. 	case HT_CLAYMORETRAP:
  6902. 	case HT_SKIDTRAP:
  6903. 	case MA_SKIDTRAP:
  6904. 	case HT_LANDMINE:
  6905. 	case MA_LANDMINE:
  6906. 	case HT_ANKLESNARE:
  6907. 	case HT_FLASHER:
  6908. 	case HT_FREEZINGTRAP:
  6909. 	case MA_FREEZINGTRAP:
  6910. 	case HT_BLASTMINE:
  6911. 		if( map_flag_gvg(src->m) || map[src->m].flag.battleground )
  6912. 			limit *= 4; // longer trap times in WOE [celest]
  6913. 		if( battle_config.vs_traps_bctall && map_flag_vs(src->m) && (src->type&battle_config.vs_traps_bctall) )
  6914. 			target = BCT_ALL;
  6915. 		break;
  6916.  
  6917. 	case SA_LANDPROTECTOR:
  6918. 	case SA_VOLCANO:
  6919. 	case SA_DELUGE:
  6920. 	case SA_VIOLENTGALE:
  6921. 	{
  6922. 		struct skill_unit_group *old_sg;
  6923. 		if ((old_sg = skill_locate_element_field(src)) != NULL)
  6924. 		{	//HelloKitty confirmed that these are interchangeable,
  6925. 			//so you can change element and not consume gemstones.
  6926. 			if ((
  6927. 				old_sg->skill_id == SA_VOLCANO ||
  6928. 				old_sg->skill_id == SA_DELUGE ||
  6929. 				old_sg->skill_id == SA_VIOLENTGALE
  6930. 			) && old_sg->limit > 0)
  6931. 			{	//Use the previous limit (minus the elapsed time) [Skotlex]
  6932. 				limit = old_sg->limit - DIFF_TICK(gettick(), old_sg->tick);
  6933. 				if (limit < 0)	//This can happen...
  6934. 					limit = skill_get_time(skillid,skilllv);
  6935. 			}
  6936. 			skill_clear_group(src,1);
  6937. 		}
  6938. 		break;
  6939. 	}
  6940.  
  6941. 	case BA_DISSONANCE:
  6942. 	case DC_UGLYDANCE:
  6943. 		val1 = 10;	//FIXME: This value is not used anywhere, what is it for? [Skotlex]
  6944. 		break;
  6945. 	case BA_WHISTLE:
  6946. 		val1 = skilllv +status->agi/10; // Flee increase
  6947. 		val2 = ((skilllv+1)/2)+status->luk/10; // Perfect dodge increase
  6948. 		if(sd){
  6949. 			val1 += pc_checkskill(sd,BA_MUSICALLESSON);
  6950. 			val2 += pc_checkskill(sd,BA_MUSICALLESSON);
  6951. 		}
  6952. 		break;
  6953. 	case DC_HUMMING:
  6954.         val1 = 2*skilllv+status->dex/10; // Hit increase
  6955. 		if(sd)
  6956. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON);
  6957. 		break;
  6958. 	case BA_POEMBRAGI:
  6959. 		val1 = 3*skilllv+status->dex/10; // Casting time reduction
  6960. 		//For some reason at level 10 the base delay reduction is 50%.
  6961. 		val2 = (skilllv<10?3*skilllv:50)+status->int_/5; // After-cast delay reduction
  6962. 		if(sd){
  6963. 			val1 += 2*pc_checkskill(sd,BA_MUSICALLESSON);
  6964. 			val2 += 2*pc_checkskill(sd,BA_MUSICALLESSON);
  6965. 		}
  6966. 		break;
  6967. 	case DC_DONTFORGETME:
  6968. 		val1 = status->dex/10 + 3*skilllv + 5; // ASPD decrease
  6969. 		val2 = status->agi/10 + 3*skilllv + 5; // Movement speed adjustment.
  6970. 		if(sd){
  6971. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON);
  6972. 			val2 += pc_checkskill(sd,DC_DANCINGLESSON);
  6973. 		}
  6974. 		break;
  6975. 	case BA_APPLEIDUN:
  6976. 		val1 = 5+2*skilllv+status->vit/10; // MaxHP percent increase
  6977. 		if(sd)
  6978. 			val1 += pc_checkskill(sd,BA_MUSICALLESSON);
  6979. 		break;
  6980. 	case DC_SERVICEFORYOU:
  6981. 		val1 = 15+skilllv+(status->int_/10); // MaxSP percent increase TO-DO: this INT bonus value is guessed
  6982. 		val2 = 20+3*skilllv+(status->int_/10); // SP cost reduction
  6983. 		if(sd){
  6984. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON); //TO-DO This bonus value is guessed
  6985. 			val2 += pc_checkskill(sd,DC_DANCINGLESSON); //TO-DO Should be half this value
  6986. 		}
  6987. 		break;
  6988. 	case BA_ASSASSINCROSS:
  6989. 		val1 = 100+10*skilllv+status->agi; // ASPD increase
  6990. 		if(sd)
  6991. 			val1 += 5*pc_checkskill(sd,BA_MUSICALLESSON);
  6992. 		break;
  6993. 	case DC_FORTUNEKISS:
  6994. 		val1 = 10+skilllv+(status->luk/10); // Critical increase
  6995. 		if(sd)
  6996. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON);
  6997. 		val1*=10; //Because every 10 crit is an actual cri point.
  6998. 		break;
  6999. 	case BD_DRUMBATTLEFIELD:
  7000. 		val1 = (skilllv+1)*25;	//Watk increase
  7001. 		val2 = (skilllv+1)*2;	//Def increase
  7002. 		break;
  7003. 	case BD_RINGNIBELUNGEN:
  7004. 		val1 = (skilllv+2)*25;	//Watk increase
  7005. 		break;
  7006. 	case BD_RICHMANKIM:
  7007. 		val1 = 25 + 11*skilllv; //Exp increase bonus.
  7008. 		break;
  7009. 	case BD_SIEGFRIED:
  7010. 		val1 = 55 + skilllv*5;	//Elemental Resistance
  7011. 		val2 = skilllv*10;	//Status ailment resistance
  7012. 		break;
  7013. 	case WE_CALLPARTNER:
  7014. 		if (sd) val1 = sd->status.partner_id;
  7015. 		break;
  7016. 	case WE_CALLPARENT:
  7017. 		if (sd) {
  7018. 			val1 = sd->status.father;
  7019. 		 	val2 = sd->status.mother;
  7020. 		}
  7021. 		break;
  7022. 	case WE_CALLBABY:
  7023. 		if (sd) val1 = sd->status.child;
  7024. 		break;
  7025. 	case NJ_KAENSIN:
  7026. 		skill_clear_group(src, 1); //Delete previous Kaensins/Suitons
  7027. 		val2 = (skilllv+1)/2 + 4;
  7028. 		break;
  7029. 	case NJ_SUITON:
  7030. 		skill_clear_group(src, 1);
  7031. 		break;
  7032.  
  7033. 	case GS_GROUNDDRIFT:
  7034. 		{
  7035. 		int element[5]={ELE_WIND,ELE_DARK,ELE_POISON,ELE_WATER,ELE_FIRE};
  7036.  
  7037. 		val1 = status->rhw.ele;
  7038. 		if (!val1)
  7039. 			val1=element[rand()%5];
  7040.  
  7041. 		switch (val1)
  7042. 		{
  7043. 			case ELE_FIRE:
  7044. 				subunt++;
  7045. 			case ELE_WATER:
  7046. 				subunt++;
  7047. 			case ELE_POISON:
  7048. 				subunt++;
  7049. 			case ELE_DARK:
  7050. 				subunt++;
  7051. 			case ELE_WIND:
  7052. 				break;
  7053. 			default:
  7054. 				subunt=rand()%5;
  7055. 				break;
  7056. 		}
  7057.  
  7058. 		break;
  7059. 		}
  7060. 	}
  7061.  
  7062. 	nullpo_retr(NULL, group=skill_initunitgroup(src,layout->count,skillid,skilllv,skill_get_unit_id(skillid,flag&1)+subunt, limit, interval));
  7063. 	group->val1=val1;
  7064. 	group->val2=val2;
  7065. 	group->val3=val3;
  7066. 	group->target_flag=target;
  7067. 	group->bl_flag= skill_get_unit_bl_target(skillid);
  7068. 	group->state.magic_power = (flag&2 || (sc && sc->data[SC_MAGICPOWER])); //Store the magic power flag. [Skotlex]
  7069. 	group->state.ammo_consume = (sd && sd->state.arrow_atk && skillid != GS_GROUNDDRIFT); //Store if this skill needs to consume ammo.
  7070. 	group->state.song_dance = (unit_flag&(UF_DANCE|UF_SONG)?1:0)|(unit_flag&UF_ENSEMBLE?2:0); //Signals if this is a song/dance/duet
  7071.  
  7072.   	//if tick is greater than current, do not invoke onplace function just yet. [Skotlex]
  7073. 	if (DIFF_TICK(group->tick, gettick()) > SKILLUNITTIMER_INTERVAL)
  7074. 		active_flag = 0;
  7075.  
  7076. 	if(skillid==HT_TALKIEBOX || skillid==RG_GRAFFITI){
  7077. 		group->valstr=(char *) aMalloc(MESSAGE_SIZE*sizeof(char));
  7078. 		if (sd)
  7079. 			safestrncpy(group->valstr, sd->message, MESSAGE_SIZE);
  7080. 		else //Eh... we have to write something here... even though mobs shouldn't use this. [Skotlex]
  7081. 			safestrncpy(group->valstr, "Boo!", MESSAGE_SIZE);
  7082. 	}
  7083.  
  7084. 	if (group->state.song_dance) {
  7085. 		if(sd){
  7086. 			sd->skillid_dance = skillid;
  7087. 			sd->skilllv_dance = skilllv;
  7088. 		}
  7089. 		if (
  7090. 			sc_start4(src, SC_DANCING, 100, skillid, group->group_id, skilllv,
  7091. 				(group->state.song_dance&2?BCT_SELF:0), limit+1000) &&
  7092. 			sd && group->state.song_dance&2 && skillid != CG_HERMODE //Hermod is a encore with a warp!
  7093. 		)
  7094. 			skill_check_pc_partner(sd, skillid, &skilllv, 1, 1);
  7095. 	}
  7096.  
  7097. 	limit = group->limit;
  7098. 	for( i = 0; i < layout->count; i++ )
  7099. 	{
  7100. 		struct skill_unit *unit;
  7101. 		int ux = x + layout->dx[i];
  7102. 		int uy = y + layout->dy[i];
  7103. 		int val1 = skilllv;
  7104. 		int val2 = 0;
  7105. 		int alive = 1;
  7106.  
  7107. 		if( !group->state.song_dance && !map_getcell(src->m,ux,uy,CELL_CHKREACH) )
  7108. 			continue; // don't place skill units on walls (except for songs/dances/encores)
  7109. 		if( battle_config.skill_wall_check && skill_get_unit_flag(skillid)&UF_PATHCHECK && !path_search_long(NULL,src->m,ux,uy,x,y,CELL_CHKWALL) )
  7110. 			continue; // no path between cell and center of casting.
  7111.  
  7112. 		switch( skillid )
  7113. 		{
  7114. 		case MG_FIREWALL:
  7115. 		case NJ_KAENSIN:
  7116. 			val2=group->val2;
  7117. 			break;
  7118. 		case WZ_ICEWALL:
  7119. 			val1 = (skilllv <= 1) ? 500 : 200 + 200*skilllv;
  7120. 			val2 = map_getcell(src->m, ux, uy, CELL_GETTYPE);
  7121. 			break;
  7122. 		case HT_LANDMINE:
  7123. 		case MA_LANDMINE:
  7124. 		case HT_ANKLESNARE:
  7125. 		case HT_SHOCKWAVE:
  7126. 		case HT_SANDMAN:
  7127. 		case MA_SANDMAN:
  7128. 		case HT_FLASHER:
  7129. 		case HT_FREEZINGTRAP:
  7130. 		case MA_FREEZINGTRAP:
  7131. 		case HT_TALKIEBOX:
  7132. 		case HT_SKIDTRAP:
  7133. 		case MA_SKIDTRAP:
  7134. 			val1 = 3500;
  7135. 			break;
  7136. 		case GS_DESPERADO:
  7137. 			val1 = abs(layout->dx[i]);
  7138. 			val2 = abs(layout->dy[i]);
  7139. 			if (val1 < 2 || val2 < 2) { //Nearby cross, linear decrease with no diagonals
  7140. 				if (val2 > val1) val1 = val2;
  7141. 				if (val1) val1--;
  7142. 				val1 = 36 -12*val1;
  7143. 			} else //Diagonal edges
  7144. 				val1 = 28 -4*val1 -4*val2;
  7145. 			if (val1 < 1) val1 = 1;
  7146. 			val2 = 0;
  7147. 			break;
  7148. 		default:
  7149. 			if (group->state.song_dance&0x1)
  7150. 				val2 = unit_flag&(UF_DANCE|UF_SONG); //Store whether this is a song/dance
  7151. 			break;
  7152. 		}
  7153.  
  7154. 		if( range <= 0 )
  7155. 			map_foreachincell(skill_cell_overlap,src->m,ux,uy,BL_SKILL,skillid,&alive, src);
  7156. 		if( !alive )
  7157. 			continue;
  7158.  
  7159. 		nullpo_retr(NULL, unit=skill_initunit(group,i,ux,uy,val1,val2));
  7160. 		unit->limit=limit;
  7161. 		unit->range=range;
  7162.  
  7163. 		if (skillid == PF_FOGWALL && alive == 2)
  7164. 		{	//Double duration of cells on top of Deluge/Suiton
  7165. 			unit->limit *= 2;
  7166. 			group->limit = unit->limit;
  7167. 		}
  7168.  
  7169. 		// execute on all targets standing on this cell
  7170. 		if (range==0 && active_flag)
  7171. 			map_foreachincell(skill_unit_effect,unit->bl.m,unit->bl.x,unit->bl.y,group->bl_flag,&unit->bl,gettick(),1);
  7172. 	}
  7173.  
  7174. 	if (!group->alive_count)
  7175. 	{	//No cells? Something that was blocked completely by Land Protector?
  7176. 		skill_delunitgroup(group);
  7177. 		return NULL;
  7178. 	}
  7179.  
  7180.  
  7181. 	if (skillid == NJ_TATAMIGAESHI) //Store number of tiles.
  7182. 		group->val1 = group->alive_count;
  7183.  
  7184. 	return group;
  7185. }
  7186.  
  7187. /*==========================================
  7188.  *
  7189.  *------------------------------------------*/
  7190. static int skill_unit_onplace (struct skill_unit *src, struct block_list *bl, unsigned int tick)
  7191. {
  7192. 	struct skill_unit_group *sg;
  7193. 	struct block_list *ss;
  7194. 	struct status_change *sc;
  7195. 	struct status_change_entry *sce;
  7196. 	enum sc_type type;
  7197. 	int skillid;
  7198.  
  7199. 	nullpo_ret(src);
  7200. 	nullpo_ret(bl);
  7201.  
  7202. 	if(bl->prev==NULL || !src->alive || status_isdead(bl))
  7203. 		return 0;
  7204.  
  7205. 	nullpo_ret(sg=src->group);
  7206. 	nullpo_ret(ss=map_id2bl(sg->src_id));
  7207.  
  7208. 	if( skill_get_type(sg->skill_id) == BF_MAGIC && map_getcell(bl->m, bl->x, bl->y, CELL_CHKLANDPROTECTOR) && sg->skill_id != SA_LANDPROTECTOR )
  7209. 		return 0; //AoE skills are ineffective. [Skotlex]
  7210.  
  7211. 	sc = status_get_sc(bl);
  7212.  
  7213. 	if (sc && sc->option&OPTION_HIDE && sg->skill_id != WZ_HEAVENDRIVE)
  7214. 		return 0; //Hidden characters are immune to AoE skills except Heaven's Drive. [Skotlex]
  7215.  
  7216. 	type = status_skill2sc(sg->skill_id);
  7217. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  7218. 	skillid = sg->skill_id; //In case the group is deleted, we need to return the correct skill id, still.
  7219. 	switch (sg->unit_id)
  7220. 	{
  7221. 	case UNT_SPIDERWEB:
  7222. 		if( sc && sc->data[SC_SPIDERWEB] && sc->data[SC_SPIDERWEB]->val1 > 0 )
  7223. 		{ // If you are fiberlocked and can't move, it will only increase your fireweakness level. [Inkfish]
  7224. 			sc->data[SC_SPIDERWEB]->val2++;
  7225. 			break;
  7226. 		}
  7227. 		else if( sc )
  7228. 		{
  7229. 			int sec = skill_get_time2(sg->skill_id,sg->skill_lv);
  7230. 			if( status_change_start(bl,type,10000,sg->skill_lv,1,sg->group_id,0,sec,8) )
  7231. 			{
  7232. 				const struct TimerData* td = sc->data[type]?get_timer(sc->data[type]->timer):NULL; 
  7233. 				if( td )
  7234. 					sec = DIFF_TICK(td->tick, tick);
  7235. 				map_moveblock(bl, src->bl.x, src->bl.y, tick);
  7236. 				clif_fixpos(bl);
  7237. 				sg->val2 = bl->id;
  7238. 			}
  7239. 			else
  7240. 				sec = 3000; //Couldn't trap it?
  7241. 			sg->limit = DIFF_TICK(tick,sg->tick)+sec;
  7242. 		}
  7243. 		break;
  7244. 	case UNT_SAFETYWALL:
  7245. 		if (!sce)
  7246. 			sc_start4(bl,type,100,sg->skill_lv,sg->group_id,sg->group_id,0,sg->limit);
  7247. 		break;
  7248.  
  7249. 	case UNT_PNEUMA:
  7250. 		if (!sce)
  7251. 			sc_start4(bl,type,100,sg->skill_lv,sg->group_id,0,0,sg->limit);
  7252. 		break;
  7253.  
  7254. 	case UNT_WARP_WAITING:
  7255. 		if(bl->type==BL_PC){
  7256. 			struct map_session_data *sd = (struct map_session_data *)bl;
  7257. 			if((!sd->chatID || battle_config.chat_warpportal)
  7258. 				&& sd->ud.to_x == src->bl.x && sd->ud.to_y == src->bl.y)
  7259. 			{
  7260. 				int x = sg->val2>>16;
  7261. 				int y = sg->val2&0xffff;
  7262. 				unsigned short m = sg->val3;
  7263.  
  7264. 				if( --sg->val1 <= 0 )
  7265. 					skill_delunitgroup(sg);
  7266.  
  7267. 				pc_setpos(sd,m,x,y,CLR_TELEPORT);
  7268. 				sg = src->group; // avoid dangling pointer (pc_setpos can cause deletion of 'sg')
  7269. 			}
  7270. 		} else
  7271. 		if(bl->type == BL_MOB && battle_config.mob_warp&2)
  7272. 		{
  7273. 			int m = map_mapindex2mapid(sg->val3);
  7274. 			if (m < 0) break; //Map not available on this map-server.
  7275. 			unit_warp(bl,m,sg->val2>>16,sg->val2&0xffff,CLR_TELEPORT);
  7276. 		}
  7277. 		break;
  7278.  
  7279. 	case UNT_QUAGMIRE:
  7280. 		if(!sce)
  7281. 			sc_start4(bl,type,100,sg->skill_lv,sg->group_id,0,0,sg->limit);
  7282. 		break;
  7283.  
  7284. 	case UNT_VOLCANO:
  7285. 	case UNT_DELUGE:
  7286. 	case UNT_VIOLENTGALE:
  7287. 		if(!sce)
  7288. 			sc_start(bl,type,100,sg->skill_lv,sg->limit);
  7289. 		break;
  7290.  
  7291. 	case UNT_SUITON:
  7292. 		if(!sce)
  7293. 			sc_start4(bl,type,100,sg->skill_lv,
  7294. 			map_flag_vs(bl->m) || battle_check_target(&src->bl,bl,BCT_ENEMY)>0?1:0, //Send val3 =1 to reduce agi.
  7295. 			0,0,sg->limit);
  7296. 		break;
  7297.  
  7298. 	case UNT_HERMODE:
  7299. 		if (sg->src_id!=bl->id && battle_check_target(&src->bl,bl,BCT_PARTY|BCT_GUILD) > 0)
  7300. 			status_change_clear_buffs(bl,1); //Should dispell only allies.
  7301. 	case UNT_RICHMANKIM:
  7302. 	case UNT_ETERNALCHAOS:
  7303. 	case UNT_DRUMBATTLEFIELD:
  7304. 	case UNT_RINGNIBELUNGEN:
  7305. 	case UNT_ROKISWEIL:
  7306. 	case UNT_INTOABYSS:
  7307. 	case UNT_SIEGFRIED:
  7308. 		 //Needed to check when a dancer/bard leaves their ensemble area.
  7309. 		if (sg->src_id==bl->id && !(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_BARDDANCER))
  7310. 			return skillid;
  7311. 		if (!sce)
  7312. 			sc_start4(bl,type,100,sg->skill_lv,sg->val1,sg->val2,0,sg->limit);
  7313. 		break;
  7314. 	case UNT_WHISTLE:
  7315. 	case UNT_ASSASSINCROSS:
  7316. 	case UNT_POEMBRAGI:
  7317. 	case UNT_APPLEIDUN:
  7318. 	case UNT_HUMMING:
  7319. 	case UNT_DONTFORGETME:
  7320. 	case UNT_FORTUNEKISS:
  7321. 	case UNT_SERVICEFORYOU:
  7322. 		if (sg->src_id==bl->id && !(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_BARDDANCER))
  7323. 			return 0;
  7324. 		if (!sc) return 0;
  7325. 		if (!sce)
  7326. 			sc_start4(bl,type,100,sg->skill_lv,sg->val1,sg->val2,0,sg->limit);
  7327. 		else if (sce->val4 == 1) {
  7328. 			//Readjust timers since the effect will not last long.
  7329. 			sce->val4 = 0;
  7330. 			delete_timer(sce->timer, status_change_timer);
  7331. 			sce->timer = add_timer(tick+sg->limit, status_change_timer, bl->id, type);
  7332. 		}
  7333. 		break;
  7334.  
  7335. 	case UNT_FOGWALL:
  7336. 		if (!sce)
  7337. 		{
  7338. 			sc_start4(bl, type, 100, sg->skill_lv, sg->val1, sg->val2, sg->group_id, sg->limit);
  7339. 			if (battle_check_target(&src->bl,bl,BCT_ENEMY)>0)
  7340. 				skill_additional_effect (ss, bl, sg->skill_id, sg->skill_lv, BF_MISC, ATK_DEF, tick);
  7341. 		}
  7342. 		break;
  7343.  
  7344. 	case UNT_GRAVITATION:
  7345. 		if (!sce)
  7346. 			sc_start4(bl,type,100,sg->skill_lv,0,BCT_ENEMY,sg->group_id,sg->limit);
  7347. 		break;
  7348.  
  7349. // officially, icewall has no problems existing on occupied cells [ultramage]
  7350. //	case UNT_ICEWALL: //Destroy the cell. [Skotlex]
  7351. //		src->val1 = 0;
  7352. //		if(src->limit + sg->tick > tick + 700)
  7353. //			src->limit = DIFF_TICK(tick+700,sg->tick);
  7354. //		break;
  7355.  
  7356. 	case UNT_MOONLIT:
  7357. 		//Knockback out of area if affected char isn't in Moonlit effect
  7358. 		if (sc && sc->data[SC_DANCING] && (sc->data[SC_DANCING]->val1&0xFFFF) == CG_MOONLIT)
  7359. 			break;
  7360. 		if (ss == bl) //Also needed to prevent infinite loop crash.
  7361. 			break;
  7362. 		skill_blown(ss,bl,skill_get_blewcount(sg->skill_id,sg->skill_lv),unit_getdir(bl),0);
  7363. 		break;
  7364. 	}
  7365. 	return skillid;
  7366. }
  7367.  
  7368. /*==========================================
  7369.  *
  7370.  *------------------------------------------*/
  7371. int skill_unit_onplace_timer (struct skill_unit *src, struct block_list *bl, unsigned int tick)
  7372. {
  7373. 	struct skill_unit_group *sg;
  7374. 	struct block_list *ss;
  7375. 	TBL_PC* tsd;
  7376. 	struct status_data *tstatus, *sstatus;
  7377. 	struct status_change *tsc, *sc;
  7378. 	struct skill_unit_group_tickset *ts;
  7379. 	enum sc_type type;
  7380. 	int skillid;
  7381. 	int diff=0;
  7382.  
  7383. 	nullpo_ret(src);
  7384. 	nullpo_ret(bl);
  7385.  
  7386. 	if (bl->prev==NULL || !src->alive || status_isdead(bl))
  7387. 		return 0;
  7388.  
  7389. 	nullpo_ret(sg=src->group);
  7390. 	nullpo_ret(ss=map_id2bl(sg->src_id));
  7391. 	tsd = BL_CAST(BL_PC, bl);
  7392. 	tsc = status_get_sc(bl);
  7393. 	tstatus = status_get_status_data(bl);
  7394. 	if (sg->state.magic_power)  //For magic power.
  7395. 	{
  7396. 		sc = status_get_sc(ss);
  7397. 		sstatus = status_get_status_data(ss);
  7398. 	} else {
  7399. 		sc = NULL;
  7400. 		sstatus = NULL;
  7401. 	}
  7402. 	type = status_skill2sc(sg->skill_id);
  7403. 	skillid = sg->skill_id;
  7404.  
  7405. 	if (sg->interval == -1) {
  7406. 		switch (sg->unit_id) {
  7407. 			case UNT_ANKLESNARE: //These happen when a trap is splash-triggered by multiple targets on the same cell.
  7408. 			case UNT_FIREPILLAR_ACTIVE:
  7409. 				return 0;
  7410. 			default:
  7411. 				ShowError("skill_unit_onplace_timer: interval error (unit id %x)\n", sg->unit_id);
  7412. 				return 0;
  7413. 		}
  7414. 	}
  7415.  
  7416. 	if ((ts = skill_unitgrouptickset_search(bl,sg,tick)))
  7417. 	{	//Not all have it, eg: Traps don't have it even though they can be hit by Heaven's Drive [Skotlex]
  7418. 		diff = DIFF_TICK(tick,ts->tick);
  7419. 		if (diff < 0)
  7420. 			return 0;
  7421. 		ts->tick = tick+sg->interval;
  7422.  
  7423. 		if ((skillid==CR_GRANDCROSS || skillid==NPC_GRANDDARKNESS) && !battle_config.gx_allhit)
  7424. 			ts->tick += sg->interval*(map_count_oncell(bl->m,bl->x,bl->y,BL_CHAR)-1);
  7425. 	}
  7426. 	//Temporarily set magic power to have it take effect. [Skotlex]
  7427. 	if (sg->state.magic_power && sc && !sc->data[SC_MAGICPOWER])
  7428. 	{	//Store previous values.
  7429. 		swap(sstatus->matk_min, sc->mp_matk_min);
  7430. 		swap(sstatus->matk_max, sc->mp_matk_max);
  7431. 		//Note to NOT return from the function until this is unset!
  7432. 	}
  7433.  
  7434. 	switch (sg->unit_id)
  7435. 	{
  7436. 		case UNT_FIREWALL:
  7437. 		case UNT_KAEN:
  7438. 		{
  7439. 			int count=0;
  7440. 			const int x = bl->x, y = bl->y;
  7441.  
  7442. 			//Take into account these hit more times than the timer interval can handle.
  7443. 			do
  7444. 				skill_attack(BF_MAGIC,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick+count*sg->interval,0);
  7445. 			while(--src->val2 && x == bl->x && y == bl->y &&
  7446. 				++count < SKILLUNITTIMER_INTERVAL/sg->interval && !status_isdead(bl));
  7447.  
  7448. 			if (src->val2<=0)
  7449. 				skill_delunit(src);
  7450. 		}
  7451. 		break;
  7452.  
  7453. 		case UNT_SANCTUARY:
  7454. 			if( battle_check_undead(tstatus->race, tstatus->def_ele) || tstatus->race==RC_DEMON )
  7455. 			{ //Only damage enemies with offensive Sanctuary. [Skotlex]
  7456. 				if( battle_check_target(&src->bl,bl,BCT_ENEMY) > 0 && skill_attack(BF_MAGIC, ss, &src->bl, bl, sg->skill_id, sg->skill_lv, tick, 0) )
  7457. 					sg->val1 -= 2; // reduce healing count if this was meant for damaging [hekate]
  7458. 			}
  7459. 			else
  7460. 			{
  7461. 				int heal = skill_calc_heal(ss,bl,sg->skill_id,sg->skill_lv,true);
  7462. 				struct mob_data *md = BL_CAST(BL_MOB, bl);
  7463. 				if( md && mob_is_battleground(md) )
  7464. 					break;
  7465. 				if( tstatus->hp >= tstatus->max_hp )
  7466. 					break;
  7467. 				if( status_isimmune(bl) )
  7468. 					heal = 0;
  7469. 				clif_skill_nodamage(&src->bl, bl, AL_HEAL, heal, 1);
  7470. 				status_heal(bl, heal, 0, 0);
  7471. 				if( diff >= 500 )
  7472. 					sg->val1--;
  7473. 			}
  7474. 			if( sg->val1 <= 0 )
  7475. 				skill_delunitgroup(sg);
  7476. 			break;
  7477.  
  7478. 		case UNT_EVILLAND:
  7479. 			//Will heal demon and undead element monsters, but not players.
  7480. 			if ((bl->type == BL_PC) || (!battle_check_undead(tstatus->race, tstatus->def_ele) && tstatus->race!=RC_DEMON))
  7481. 			{	//Damage enemies
  7482. 				if(battle_check_target(&src->bl,bl,BCT_ENEMY)>0)
  7483. 					skill_attack(BF_MISC, ss, &src->bl, bl, sg->skill_id, sg->skill_lv, tick, 0);
  7484. 			} else {
  7485. 				int heal = skill_calc_heal(ss,bl,sg->skill_id,sg->skill_lv,true);
  7486. 				if (tstatus->hp >= tstatus->max_hp)
  7487. 					break;
  7488. 				if (status_isimmune(bl))
  7489. 					heal = 0;
  7490. 				clif_skill_nodamage(&src->bl, bl, AL_HEAL, heal, 1);
  7491. 				status_heal(bl, heal, 0, 0);
  7492. 			}
  7493. 			break;
  7494.  
  7495. 		case UNT_MAGNUS:
  7496. 			if (!battle_check_undead(tstatus->race,tstatus->def_ele) && tstatus->race!=RC_DEMON)
  7497. 				break;
  7498. 			skill_attack(BF_MAGIC,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7499. 			break;
  7500.  
  7501. 		case UNT_DUMMYSKILL:
  7502. 			switch (sg->skill_id)
  7503. 			{
  7504. 				case SG_SUN_WARM: //SG skills [Komurka]
  7505. 				case SG_MOON_WARM:
  7506. 				case SG_STAR_WARM:
  7507. 				{
  7508. 					int count = 0;
  7509. 					const int x = bl->x, y = bl->y;
  7510.  
  7511. 					//If target isn't knocked back it should hit every "interval" ms [Playtester]
  7512. 					do
  7513. 					{
  7514. 						if( bl->type == BL_PC )
  7515. 							status_zap(bl, 0, 15); // sp damage to players
  7516. 						else // mobs
  7517. 						if( status_charge(ss, 0, 2) ) // costs 2 SP per hit
  7518. 						{
  7519. 							if( !skill_attack(BF_WEAPON,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick+count*sg->interval,0) )
  7520. 								status_charge(ss, 0, 8); //costs additional 8 SP if miss
  7521. 						}
  7522. 						else
  7523. 						{ //should end when out of sp.
  7524. 							sg->limit = DIFF_TICK(tick,sg->tick);
  7525. 							break;
  7526. 						}
  7527. 					} while( x == bl->x && y == bl->y &&
  7528. 						++count < SKILLUNITTIMER_INTERVAL/sg->interval && !status_isdead(bl) );
  7529. 				}
  7530. 				break;
  7531. 				case WZ_STORMGUST: //SG counter does not reset per stormgust. IE: One hit from a SG and two hits from another will freeze you.
  7532. 					if (tsc) 
  7533. 						tsc->sg_counter++; //SG hit counter.
  7534. 					if (skill_attack(skill_get_type(sg->skill_id),ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0) <= 0 && tsc)
  7535. 						tsc->sg_counter=0; //Attack absorbed.
  7536. 				break;
  7537.  
  7538. 				case GS_DESPERADO:
  7539. 					if (rand()%100 < src->val1)
  7540. 						skill_attack(BF_WEAPON,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7541. 				break;
  7542.  
  7543. 				default:
  7544. 					skill_attack(skill_get_type(sg->skill_id),ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7545. 			}
  7546. 			break;
  7547.  
  7548. 		case UNT_FIREPILLAR_WAITING:
  7549. 			skill_unitsetting(ss,sg->skill_id,sg->skill_lv,src->bl.x,src->bl.y,1);
  7550. 			skill_delunit(src);
  7551. 			break;
  7552.  
  7553. 		case UNT_SKIDTRAP:
  7554. 			{
  7555. 				skill_blown(&src->bl,bl,skill_get_blewcount(sg->skill_id,sg->skill_lv),unit_getdir(bl),0);
  7556. 				sg->unit_id = UNT_USED_TRAPS;
  7557. 				clif_changetraplook(&src->bl, UNT_USED_TRAPS);
  7558. 				sg->limit=DIFF_TICK(tick,sg->tick)+1500;
  7559. 			}
  7560. 			break;
  7561.  
  7562. 		case UNT_ANKLESNARE:
  7563. 			if( sg->val2 == 0 && tsc )
  7564. 			{
  7565. 				int sec = skill_get_time2(sg->skill_id,sg->skill_lv);
  7566. 				if( status_change_start(bl,type,10000,sg->skill_lv,sg->group_id,0,0,sec, 8) )
  7567. 				{
  7568. 					const struct TimerData* td = tsc->data[type]?get_timer(tsc->data[type]->timer):NULL; 
  7569. 					if( td )
  7570. 						sec = DIFF_TICK(td->tick, tick);
  7571. 					unit_movepos(bl, src->bl.x, src->bl.y, 0, 0);
  7572. 					clif_fixpos(bl);
  7573. 					sg->val2 = bl->id;
  7574. 				}
  7575. 				else
  7576. 					sec = 3000; //Couldn't trap it?
  7577. 				clif_skillunit_update(&src->bl);
  7578. 				sg->limit = DIFF_TICK(tick,sg->tick)+sec;
  7579. 				sg->interval = -1;
  7580. 				src->range = 0;
  7581. 			}
  7582. 			break;
  7583.  
  7584. 		case UNT_VENOMDUST:
  7585. 			if(tsc && !tsc->data[type])
  7586. 				status_change_start(bl,type,10000,sg->skill_lv,sg->group_id,0,0,skill_get_time2(sg->skill_id,sg->skill_lv),8);
  7587. 			break;
  7588.  
  7589. 		case UNT_LANDMINE:
  7590. 		case UNT_CLAYMORETRAP:
  7591. 		case UNT_BLASTMINE:
  7592. 		case UNT_SHOCKWAVE:
  7593. 		case UNT_SANDMAN:
  7594. 		case UNT_FLASHER:
  7595. 		case UNT_FREEZINGTRAP:
  7596. 		case UNT_FIREPILLAR_ACTIVE:
  7597. 			map_foreachinrange(skill_trap_splash,&src->bl, skill_get_splash(sg->skill_id, sg->skill_lv), sg->bl_flag, &src->bl,tick);
  7598. 			if (sg->unit_id != UNT_FIREPILLAR_ACTIVE)
  7599. 				clif_changetraplook(&src->bl, sg->unit_id==UNT_LANDMINE?UNT_FIREPILLAR_ACTIVE:UNT_USED_TRAPS);
  7600. 			src->range = -1; //Disable range so it does not invoke a for each in area again.
  7601. 			sg->limit=DIFF_TICK(tick,sg->tick)+1500;
  7602. 			break;
  7603.  
  7604. 		case UNT_TALKIEBOX:
  7605. 			if (sg->src_id == bl->id)
  7606. 				break;
  7607. 			if (sg->val2 == 0){
  7608. 				clif_talkiebox(&src->bl, sg->valstr);
  7609. 				sg->unit_id = UNT_USED_TRAPS;
  7610. 				clif_changetraplook(&src->bl, UNT_USED_TRAPS);
  7611. 				sg->limit = DIFF_TICK(tick, sg->tick) + 5000;
  7612. 				sg->val2 = -1;
  7613. 			}
  7614. 			break;
  7615.  
  7616. 		case UNT_LULLABY:
  7617. 			if (ss->id == bl->id)
  7618. 				break;
  7619. 			skill_additional_effect(ss, bl, sg->skill_id, sg->skill_lv, BF_LONG|BF_SKILL|BF_MISC, ATK_DEF, tick);
  7620. 			break;
  7621.  
  7622. 		case UNT_UGLYDANCE:	//Ugly Dance [Skotlex]
  7623. 			if (ss->id != bl->id)
  7624. 				skill_additional_effect(ss, bl, sg->skill_id, sg->skill_lv, BF_LONG|BF_SKILL|BF_MISC, ATK_DEF, tick);
  7625. 			break;
  7626.  
  7627. 		case UNT_DISSONANCE:
  7628. 			skill_attack(BF_MISC, ss, &src->bl, bl, sg->skill_id, sg->skill_lv, tick, 0);
  7629. 			break;
  7630.  
  7631. 		case UNT_APPLEIDUN: //Apple of Idun [Skotlex]
  7632. 		{
  7633. 			int heal;
  7634. 			if( sg->src_id == bl->id && !(tsc && tsc->data[SC_SPIRIT] && tsc->data[SC_SPIRIT]->val2 == SL_BARDDANCER) )
  7635. 				break; // affects self only when soullinked
  7636. 			heal = skill_calc_heal(ss,bl,sg->skill_id, sg->skill_lv, true);
  7637. 			clif_skill_nodamage(&src->bl, bl, AL_HEAL, heal, 1);
  7638. 			status_heal(bl, heal, 0, 0);
  7639. 			break;
  7640. 		}
  7641.  
  7642.  		case UNT_TATAMIGAESHI:
  7643. 		case UNT_DEMONSTRATION:
  7644. 			skill_attack(BF_WEAPON,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7645. 			break;
  7646.  
  7647. 		case UNT_GOSPEL:
  7648. 			if (rand()%100 > sg->skill_lv*10 || ss == bl)
  7649. 				break;
  7650. 			if (battle_check_target(ss,bl,BCT_PARTY)>0)
  7651. 			{ // Support Effect only on party, not guild
  7652. 				int heal;
  7653. 				int i = rand()%13; // Positive buff count
  7654. 				int time = skill_get_time2(sg->skill_id, sg->skill_lv); //Duration
  7655. 				switch (i)
  7656. 				{
  7657. 					case 0: // Heal 1~9999 HP
  7658. 						heal = rand() %9999+1;
  7659. 						clif_skill_nodamage(ss,bl,AL_HEAL,heal,1);
  7660. 						status_heal(bl,heal,0,0);
  7661. 						break;
  7662. 					case 1: // End all negative status
  7663. 						status_change_clear_buffs(bl,2);
  7664. 						if (tsd) clif_gospel_info(tsd, 0x15);
  7665. 						break;
  7666. 					case 2: // Immunity to all status
  7667. 						sc_start(bl,SC_SCRESIST,100,100,time);
  7668. 						if (tsd) clif_gospel_info(tsd, 0x16);
  7669. 						break;
  7670. 					case 3: // MaxHP +100%
  7671. 						sc_start(bl,SC_INCMHPRATE,100,100,time);
  7672. 						if (tsd) clif_gospel_info(tsd, 0x17);
  7673. 						break;
  7674. 					case 4: // MaxSP +100%
  7675. 						sc_start(bl,SC_INCMSPRATE,100,100,time);
  7676. 						if (tsd) clif_gospel_info(tsd, 0x18);
  7677. 						break;
  7678. 					case 5: // All stats +20
  7679. 						sc_start(bl,SC_INCALLSTATUS,100,20,time);
  7680. 						if (tsd) clif_gospel_info(tsd, 0x19);
  7681. 						break;
  7682. 					case 6: // Level 10 Blessing
  7683. 						sc_start(bl,SC_BLESSING,100,10,time);
  7684. 						break;
  7685. 					case 7: // Level 10 Increase AGI
  7686. 						sc_start(bl,SC_INCREASEAGI,100,10,time);
  7687. 						break;
  7688. 					case 8: // Enchant weapon with Holy element
  7689. 						sc_start(bl,SC_ASPERSIO,100,1,time);
  7690. 						if (tsd) clif_gospel_info(tsd, 0x1c);
  7691. 						break;
  7692. 					case 9: // Enchant armor with Holy element
  7693. 						sc_start(bl,SC_BENEDICTIO,100,1,time);
  7694. 						if (tsd) clif_gospel_info(tsd, 0x1d);
  7695. 						break;
  7696. 					case 10: // DEF +25%
  7697. 						sc_start(bl,SC_INCDEFRATE,100,25,time);
  7698. 						if (tsd) clif_gospel_info(tsd, 0x1e);
  7699. 						break;
  7700. 					case 11: // ATK +100%
  7701. 						sc_start(bl,SC_INCATKRATE,100,100,time);
  7702. 						if (tsd) clif_gospel_info(tsd, 0x1f);
  7703. 						break;
  7704. 					case 12: // HIT/Flee +50
  7705. 						sc_start(bl,SC_INCHIT,100,50,time);
  7706. 						sc_start(bl,SC_INCFLEE,100,50,time);
  7707. 						if (tsd) clif_gospel_info(tsd, 0x20);
  7708. 						break;
  7709. 				}
  7710. 			}
  7711. 			else if (battle_check_target(&src->bl,bl,BCT_ENEMY)>0)
  7712. 			{ // Offensive Effect
  7713. 				int i = rand()%9; // Negative buff count
  7714. 				int time = skill_get_time2(sg->skill_id, sg->skill_lv);
  7715. 				switch (i)
  7716. 				{
  7717. 					case 0: // Deal 1~9999 damage
  7718. 						skill_attack(BF_MISC,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7719. 						break;
  7720. 					case 1: // Curse
  7721. 						sc_start(bl,SC_CURSE,100,1,time);
  7722. 						break;
  7723. 					case 2: // Blind
  7724. 						sc_start(bl,SC_BLIND,100,1,time);
  7725. 						break;
  7726. 					case 3: // Poison
  7727. 						sc_start(bl,SC_POISON,100,1,time);
  7728. 						break;
  7729. 					case 4: // Level 10 Provoke
  7730. 						sc_start(bl,SC_PROVOKE,100,10,time);
  7731. 						break;
  7732. 					case 5: // DEF -100%
  7733. 						sc_start(bl,SC_INCDEFRATE,100,-100,time);
  7734. 						break;
  7735. 					case 6: // ATK -100%
  7736. 						sc_start(bl,SC_INCATKRATE,100,-100,time);
  7737. 						break;
  7738. 					case 7: // Flee -100%
  7739. 						sc_start(bl,SC_INCFLEERATE,100,-100,time);
  7740. 						break;
  7741. 					case 8: // Speed/ASPD -25%
  7742. 						sc_start4(bl,SC_GOSPEL,100,1,0,0,BCT_ENEMY,time);
  7743. 						break;
  7744. 				}
  7745. 			}
  7746. 			break;
  7747.  
  7748. 		case UNT_BASILICA:
  7749. 			{
  7750. 				int i = battle_check_target(&src->bl, bl, BCT_ENEMY);
  7751. 				if( i > 0 && !(status_get_mode(bl)&MD_BOSS) )
  7752. 				{ // knock-back any enemy except Boss
  7753. 					skill_blown(&src->bl, bl, 2, unit_getdir(bl), 0);
  7754. 					clif_fixpos(bl);
  7755. 				}
  7756.  
  7757. 				if( sg->src_id != bl->id && i <= 0 )
  7758. 					sc_start4(bl, type, 100, 0, 0, 0, src->bl.id, sg->interval + 100);
  7759. 			}
  7760. 			break;
  7761.  
  7762. 		case UNT_GRAVITATION:
  7763. 			skill_attack(skill_get_type(sg->skill_id),ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7764. 			break;
  7765.  
  7766. 		case UNT_GROUNDDRIFT_WIND:
  7767. 		case UNT_GROUNDDRIFT_DARK:
  7768. 		case UNT_GROUNDDRIFT_POISON:
  7769. 		case UNT_GROUNDDRIFT_WATER:
  7770. 		case UNT_GROUNDDRIFT_FIRE:
  7771. 			map_foreachinrange(skill_trap_splash,&src->bl,
  7772. 				skill_get_splash(sg->skill_id, sg->skill_lv), sg->bl_flag,
  7773. 				&src->bl,tick);
  7774. 			sg->unit_id = UNT_USED_TRAPS;
  7775. 			//clif_changetraplook(&src->bl, UNT_FIREPILLAR_ACTIVE);
  7776. 			sg->limit=DIFF_TICK(tick,sg->tick)+1500;
  7777. 			break;
  7778. 	}
  7779.  
  7780. 	if (sg->state.magic_power && sc && !sc->data[SC_MAGICPOWER])
  7781. 	{	//Unset magic power.
  7782. 		swap(sstatus->matk_min, sc->mp_matk_min);
  7783. 		swap(sstatus->matk_max, sc->mp_matk_max);
  7784. 	}
  7785.  
  7786. 	if (bl->type == BL_MOB && ss != bl)
  7787. 		mobskill_event((TBL_MOB*)bl, ss, tick, MSC_SKILLUSED|(skillid<<16));
  7788.  
  7789. 	return skillid;
  7790. }
  7791. /*==========================================
  7792.  * Triggered when a char steps out of a skill cell
  7793.  *------------------------------------------*/
  7794. int skill_unit_onout (struct skill_unit *src, struct block_list *bl, unsigned int tick)
  7795. {
  7796. 	struct skill_unit_group *sg;
  7797. 	struct status_change *sc;
  7798. 	struct status_change_entry *sce;
  7799. 	enum sc_type type;
  7800.  
  7801. 	nullpo_ret(src);
  7802. 	nullpo_ret(bl);
  7803. 	nullpo_ret(sg=src->group);
  7804. 	sc = status_get_sc(bl);
  7805. 	type = status_skill2sc(sg->skill_id);
  7806. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  7807.  
  7808. 	if( bl->prev==NULL ||
  7809. 		(status_isdead(bl) && sg->unit_id != UNT_ANKLESNARE && sg->unit_id != UNT_SPIDERWEB) ) //Need to delete the trap if the source died.
  7810. 		return 0;
  7811.  
  7812. 	switch(sg->unit_id){
  7813. 	case UNT_SAFETYWALL:
  7814. 	case UNT_PNEUMA:
  7815. 		if (sce)
  7816. 			status_change_end(bl, type, INVALID_TIMER);
  7817. 		break;
  7818.  
  7819. 	case UNT_BASILICA:
  7820. 		if( sce && sce->val4 == src->bl.id )
  7821. 			status_change_end(bl, type, INVALID_TIMER);
  7822. 		break;
  7823.  
  7824. 	case UNT_HERMODE:	//Clear Hermode if the owner moved.
  7825. 		if (sce && sce->val3 == BCT_SELF && sce->val4 == sg->group_id)
  7826. 			status_change_end(bl, type, INVALID_TIMER);
  7827. 		break;
  7828.  
  7829. 	case UNT_SPIDERWEB:
  7830. 		{
  7831. 			struct block_list *target = map_id2bl(sg->val2);
  7832. 			if (target && target==bl)
  7833. 			{
  7834. 				if (sce && sce->val3 == sg->group_id)
  7835. 					status_change_end(bl, type, INVALID_TIMER);
  7836. 				sg->limit = DIFF_TICK(tick,sg->tick)+1000;
  7837. 			}
  7838. 			break;
  7839. 		}
  7840. 	}
  7841. 	return sg->skill_id;
  7842. }
  7843.  
  7844. /*==========================================
  7845.  * Triggered when a char steps out of a skill group (entirely) [Skotlex]
  7846.  *------------------------------------------*/
  7847. static int skill_unit_onleft (int skill_id, struct block_list *bl, unsigned int tick)
  7848. {
  7849. 	struct status_change *sc;
  7850. 	struct status_change_entry *sce;
  7851. 	enum sc_type type;
  7852.  
  7853. 	sc = status_get_sc(bl);
  7854. 	if (sc && !sc->count)
  7855. 		sc = NULL;
  7856.  
  7857. 	type = status_skill2sc(skill_id);
  7858. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  7859.  
  7860. 	switch (skill_id)
  7861. 	{
  7862. 		case WZ_QUAGMIRE:
  7863. 			if (bl->type==BL_MOB)
  7864. 				break;
  7865. 			if (sce)
  7866. 				status_change_end(bl, type, INVALID_TIMER);
  7867. 			break;
  7868.  
  7869. 		case BD_LULLABY:
  7870. 		case BD_RICHMANKIM:
  7871. 		case BD_ETERNALCHAOS:
  7872. 		case BD_DRUMBATTLEFIELD:
  7873. 		case BD_RINGNIBELUNGEN:
  7874. 		case BD_ROKISWEIL:
  7875. 		case BD_INTOABYSS:
  7876. 		case BD_SIEGFRIED:
  7877. 			if(sc && sc->data[SC_DANCING] && (sc->data[SC_DANCING]->val1&0xFFFF) == skill_id)
  7878. 			{	//Check if you just stepped out of your ensemble skill to cancel dancing. [Skotlex]
  7879. 				//We don't check for SC_LONGING because someone could always have knocked you back and out of the song/dance.
  7880. 				//FIXME: This code is not perfect, it doesn't checks for the real ensemble's owner,
  7881. 				//it only checks if you are doing the same ensemble. So if there's two chars doing an ensemble
  7882. 				//which overlaps, by stepping outside of the other parther's ensemble will cause you to cancel
  7883. 				//your own. Let's pray that scenario is pretty unlikely and noone will complain too much about it.
  7884. 				status_change_end(bl, SC_DANCING, INVALID_TIMER);
  7885. 			}
  7886. 		case MG_SAFETYWALL:
  7887. 		case AL_PNEUMA:
  7888. 		case SA_VOLCANO:
  7889. 		case SA_DELUGE:
  7890. 		case SA_VIOLENTGALE:
  7891. 		case CG_HERMODE:
  7892. 		case HW_GRAVITATION:
  7893. 		case NJ_SUITON:
  7894. 			if (sce)
  7895. 				status_change_end(bl, type, INVALID_TIMER);
  7896. 			break;
  7897.  
  7898. 		case BA_POEMBRAGI:
  7899. 		case BA_WHISTLE:
  7900. 		case BA_ASSASSINCROSS:
  7901. 		case BA_APPLEIDUN:
  7902. 		case DC_HUMMING:
  7903. 		case DC_DONTFORGETME:
  7904. 		case DC_FORTUNEKISS:
  7905. 		case DC_SERVICEFORYOU:
  7906. 			if (sce)
  7907. 			{
  7908. 				delete_timer(sce->timer, status_change_timer);
  7909. 				//NOTE: It'd be nice if we could get the skill_lv for a more accurate extra time, but alas...
  7910. 				//not possible on our current implementation.
  7911. 				sce->val4 = 1; //Store the fact that this is a "reduced" duration effect.
  7912. 				sce->timer = add_timer(tick+skill_get_time2(skill_id,1), status_change_timer, bl->id, type);
  7913. 			}
  7914. 			break;
  7915. 		case PF_FOGWALL:
  7916. 			if (sce)
  7917. 			{
  7918. 				status_change_end(bl, type, INVALID_TIMER);
  7919. 				if ((sce=sc->data[SC_BLIND]))
  7920. 				{
  7921. 					if (bl->type == BL_PC) //Players get blind ended inmediately, others have it still for 30 secs. [Skotlex]
  7922. 						status_change_end(bl, SC_BLIND, INVALID_TIMER);
  7923. 					else {
  7924. 						delete_timer(sce->timer, status_change_timer);
  7925. 						sce->timer = add_timer(30000+tick, status_change_timer, bl->id, SC_BLIND);
  7926. 					}
  7927. 				}
  7928. 			}
  7929. 			break;
  7930. 	}
  7931.  
  7932. 	return skill_id;
  7933. }
  7934.  
  7935. /*==========================================
  7936.  * Invoked when a unit cell has been placed/removed/deleted.
  7937.  * flag values:
  7938.  * flag&1: Invoke onplace function (otherwise invoke onout)
  7939.  * flag&4: Invoke a onleft call (the unit might be scheduled for deletion)
  7940.  *------------------------------------------*/
  7941. static int skill_unit_effect (struct block_list* bl, va_list ap)
  7942. {
  7943. 	struct skill_unit* unit = va_arg(ap,struct skill_unit*);
  7944. 	struct skill_unit_group* group = unit->group;
  7945. 	unsigned int tick = va_arg(ap,unsigned int);
  7946. 	unsigned int flag = va_arg(ap,unsigned int);
  7947. 	int skill_id;
  7948. 	bool dissonance;
  7949.  
  7950. 	if( (!unit->alive && !(flag&4)) || bl->prev == NULL )
  7951. 		return 0;
  7952.  
  7953. 	nullpo_ret(group);
  7954.  
  7955. 	dissonance = skill_dance_switch(unit, 0);
  7956.  
  7957. 	//Necessary in case the group is deleted after calling on_place/on_out [Skotlex]
  7958. 	skill_id = group->skill_id;
  7959.  
  7960. 	//Target-type check.
  7961. 	if( !(group->bl_flag&bl->type && battle_check_target(&unit->bl,bl,group->target_flag)>0) )
  7962. 	{
  7963. 		if( flag&4 && group->src_id == bl->id && group->state.song_dance&0x2 )
  7964. 			skill_unit_onleft(skill_id, bl, tick);//Ensemble check to terminate it.
  7965. 	}
  7966. 	else
  7967. 	{
  7968. 		if( flag&1 )
  7969. 			skill_unit_onplace(unit,bl,tick);
  7970. 		else
  7971. 			skill_unit_onout(unit,bl,tick);
  7972.  
  7973. 		if( flag&4 )
  7974. 	  		skill_unit_onleft(skill_id, bl, tick);
  7975. 	}
  7976.  
  7977. 	if( dissonance ) skill_dance_switch(unit, 1);
  7978.  
  7979. 	return 0;
  7980. }
  7981.  
  7982. /*==========================================
  7983.  *
  7984.  *------------------------------------------*/
  7985. int skill_unit_ondamaged (struct skill_unit *src, struct block_list *bl, int damage, unsigned int tick)
  7986. {
  7987. 	struct skill_unit_group *sg;
  7988.  
  7989. 	nullpo_ret(src);
  7990. 	nullpo_ret(sg=src->group);
  7991.  
  7992. 	switch( sg->unit_id )
  7993. 	{
  7994. 	case UNT_SKIDTRAP:
  7995. 	case UNT_LANDMINE:
  7996. 	case UNT_SHOCKWAVE:
  7997. 	case UNT_SANDMAN:
  7998. 	case UNT_FLASHER:
  7999. 	case UNT_FREEZINGTRAP:
  8000. 	case UNT_TALKIEBOX:
  8001. 	case UNT_ANKLESNARE:
  8002. 	case UNT_ICEWALL:
  8003. 		src->val1-=damage;
  8004. 		break;
  8005. 	case UNT_BLASTMINE:
  8006. 	case UNT_CLAYMORETRAP:
  8007. 		skill_blown(bl, &src->bl, 2, -1, 0);
  8008. 		break;
  8009. 	default:
  8010. 		damage = 0;
  8011. 		break;
  8012. 	}
  8013. 	return damage;
  8014. }
  8015.  
  8016. /*==========================================
  8017.  *
  8018.  *------------------------------------------*/
  8019. static int skill_check_condition_char_sub (struct block_list *bl, va_list ap)
  8020. {
  8021. 	int *c, skillid;
  8022. 	struct block_list *src;
  8023. 	struct map_session_data *sd;
  8024. 	struct map_session_data *tsd;
  8025. 	int *p_sd;	//Contains the list of characters found.
  8026.  
  8027. 	nullpo_ret(bl);
  8028. 	nullpo_ret(tsd=(struct map_session_data*)bl);
  8029. 	nullpo_ret(src=va_arg(ap,struct block_list *));
  8030. 	nullpo_ret(sd=(struct map_session_data*)src);
  8031.  
  8032. 	c=va_arg(ap,int *);
  8033. 	p_sd = va_arg(ap, int *);
  8034. 	skillid = va_arg(ap,int);
  8035.  
  8036. 	if ((skillid != PR_BENEDICTIO && *c >=1) || *c >=2)
  8037. 		return 0; //Partner found for ensembles, or the two companions for Benedictio. [Skotlex]
  8038.  
  8039. 	if (bl == src)
  8040. 		return 0;
  8041.  
  8042. 	if(pc_isdead(tsd))
  8043. 		return 0;
  8044.  
  8045. 	if (tsd->sc.data[SC_SILENCE] || tsd->sc.opt1)
  8046. 		return 0;
  8047.  
  8048. 	switch(skillid)
  8049. 	{
  8050. 		case PR_BENEDICTIO:
  8051. 		{
  8052. 			int dir = map_calc_dir(&sd->bl,tsd->bl.x,tsd->bl.y);
  8053. 			dir = (unit_getdir(&sd->bl) + dir)%8; //This adjusts dir to account for the direction the sd is facing.
  8054. 			if ((tsd->class_&MAPID_BASEMASK) == MAPID_ACOLYTE && (dir == 2 || dir == 6) //Must be standing to the left/right of Priest.
  8055. 				&& sd->status.sp >= 10)
  8056. 				p_sd[(*c)++]=tsd->bl.id;
  8057. 			return 1;
  8058. 		}
  8059. 		default: //Warning: Assuming Ensemble Dance/Songs for code speed. [Skotlex]
  8060. 			{
  8061. 				int skilllv;
  8062. 				if(pc_issit(tsd) || !unit_can_move(&tsd->bl))
  8063. 					return 0;
  8064. 				if (sd->status.sex != tsd->status.sex &&
  8065. 						(tsd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER &&
  8066. 						(skilllv = pc_checkskill(tsd, skillid)) > 0 &&
  8067. 						(tsd->weapontype1==W_MUSICAL || tsd->weapontype1==W_WHIP) &&
  8068. 						sd->status.party_id && tsd->status.party_id &&
  8069. 						sd->status.party_id == tsd->status.party_id &&
  8070. 						!tsd->sc.data[SC_DANCING])
  8071. 				{
  8072. 					p_sd[(*c)++]=tsd->bl.id;
  8073. 					return skilllv;
  8074. 				} else {
  8075. 					return 0;
  8076. 				}
  8077. 			}
  8078. 			break;
  8079. 	}
  8080. 	return 0;
  8081. }
  8082.  
  8083. /*==========================================
  8084.  * Checks and stores partners for ensemble skills [Skotlex]
  8085.  *------------------------------------------*/
  8086. int skill_check_pc_partner (struct map_session_data *sd, short skill_id, short* skill_lv, int range, int cast_flag)
  8087. {
  8088. 	static int c=0;
  8089. 	static int p_sd[2] = { 0, 0 };
  8090. 	int i;
  8091.  
  8092. 	if (!battle_config.player_skill_partner_check ||
  8093. 		(battle_config.gm_skilluncond && pc_isGM(sd) >= battle_config.gm_skilluncond))
  8094. 		return 99; //As if there were infinite partners.
  8095.  
  8096. 	if (cast_flag)
  8097. 	{	//Execute the skill on the partners.
  8098. 		struct map_session_data* tsd;
  8099. 		switch (skill_id)
  8100. 		{
  8101. 			case PR_BENEDICTIO:
  8102. 				for (i = 0; i < c; i++)
  8103. 				{
  8104. 					if ((tsd = map_id2sd(p_sd[i])) != NULL)
  8105. 						status_charge(&tsd->bl, 0, 10);
  8106. 				}
  8107. 				return c;
  8108. 			default: //Warning: Assuming Ensemble skills here (for speed)
  8109. 				if (c > 0 && sd->sc.data[SC_DANCING] && (tsd = map_id2sd(p_sd[0])) != NULL)
  8110. 				{
  8111. 					sd->sc.data[SC_DANCING]->val4 = tsd->bl.id;
  8112. 					sc_start4(&tsd->bl,SC_DANCING,100,skill_id,sd->sc.data[SC_DANCING]->val2,*skill_lv,sd->bl.id,skill_get_time(skill_id,*skill_lv)+1000);
  8113. 					clif_skill_nodamage(&tsd->bl, &sd->bl, skill_id, *skill_lv, 1);
  8114. 					tsd->skillid_dance = skill_id;
  8115. 					tsd->skilllv_dance = *skill_lv;
  8116. 				}
  8117. 				return c;
  8118. 		}
  8119. 	}
  8120.  
  8121. 	//Else: new search for partners.
  8122. 	c = 0;
  8123. 	memset (p_sd, 0, sizeof(p_sd));
  8124. 	i = map_foreachinrange(skill_check_condition_char_sub, &sd->bl, range, BL_PC, &sd->bl, &c, &p_sd, skill_id);
  8125.  
  8126. 	if (skill_id != PR_BENEDICTIO) //Apply the average lv to encore skills.
  8127. 		*skill_lv = (i+(*skill_lv))/(c+1); //I know c should be one, but this shows how it could be used for the average of n partners.
  8128. 	return c;
  8129. }
  8130.  
  8131. /*==========================================
  8132.  *
  8133.  *------------------------------------------*/
  8134. static int skill_check_condition_mob_master_sub (struct block_list *bl, va_list ap)
  8135. {
  8136. 	int *c,src_id,mob_class,skill;
  8137. 	struct mob_data *md;
  8138.  
  8139. 	md=(struct mob_data*)bl;
  8140. 	src_id=va_arg(ap,int);
  8141. 	mob_class=va_arg(ap,int);
  8142. 	skill=va_arg(ap,int);
  8143. 	c=va_arg(ap,int *);
  8144.  
  8145. 	if( md->master_id != src_id || md->special_state.ai != (unsigned)(skill == AM_SPHEREMINE?2:3) )
  8146. 		return 0; //Non alchemist summoned mobs have nothing to do here.
  8147.  
  8148. 	if(md->class_==mob_class)
  8149. 		(*c)++;
  8150.  
  8151. 	return 1;
  8152. }
  8153.  
  8154. /*==========================================
  8155.  * Determines if a given skill should be made to consume ammo
  8156.  * when used by the player. [Skotlex]
  8157.  *------------------------------------------*/
  8158. int skill_isammotype (struct map_session_data *sd, int skill)
  8159. {
  8160. 	return (
  8161. 		battle_config.arrow_decrement==2 &&
  8162. 		(sd->status.weapon == W_BOW || (sd->status.weapon >= W_REVOLVER && sd->status.weapon <= W_GRENADE)) &&
  8163. 		skill != HT_PHANTASMIC &&
  8164. 		skill_get_type(skill) == BF_WEAPON &&
  8165. 	  	!(skill_get_nk(skill)&NK_NO_DAMAGE) &&
  8166. 		!skill_get_spiritball(skill,1) //Assume spirit spheres are used as ammo instead.
  8167. 	);
  8168. }
  8169.  
  8170. int skill_check_condition_castbegin(struct map_session_data* sd, short skill, short lv)
  8171. {
  8172. 	struct status_data *status;
  8173. 	struct status_change *sc;
  8174. 	struct skill_condition require;
  8175. 	int i;
  8176.  
  8177. 	nullpo_ret(sd);
  8178.  
  8179. 	if (lv <= 0 || sd->chatID) return 0;
  8180.  
  8181. 	if( battle_config.gm_skilluncond && pc_isGM(sd)>= battle_config.gm_skilluncond && sd->skillitem != skill )
  8182. 	{	//GMs don't override the skillItem check, otherwise they can use items without them being consumed! [Skotlex]	
  8183. 		sd->state.arrow_atk = skill_get_ammotype(skill)?1:0; //Need to do arrow state check.
  8184. 		sd->spiritball_old = sd->spiritball; //Need to do Spiritball check.
  8185. 		return 1;
  8186. 	}
  8187.  
  8188. 	if( sd->menuskill_id == AM_PHARMACY )
  8189. 	{
  8190. 		switch( skill )
  8191. 		{
  8192. 		case AM_PHARMACY:
  8193. 		case AC_MAKINGARROW:
  8194. 		case BS_REPAIRWEAPON:
  8195. 		case AM_TWILIGHT1:
  8196. 		case AM_TWILIGHT2:
  8197. 		case AM_TWILIGHT3:
  8198. 			return 0;
  8199. 		}
  8200. 	}
  8201.  
  8202. 	status = &sd->battle_status;
  8203. 	sc = &sd->sc;
  8204. 	if( !sc->count )
  8205. 		sc = NULL;
  8206.  
  8207. 	if( sd->skillitem == skill )
  8208. 	{
  8209. 		if( sd->state.abra_flag ) // Hocus-Pocus was used. [Inkfish]
  8210. 			sd->state.abra_flag = 0;
  8211. 		else
  8212. 		{ // When a target was selected, consume items that were skipped in pc_use_item [Skotlex]
  8213. 			if( (i = sd->itemindex) == -1 ||
  8214. 				sd->status.inventory[i].nameid != sd->itemid ||
  8215. 				sd->inventory_data[i] == NULL ||
  8216. 				!sd->inventory_data[i]->flag.delay_consume ||
  8217. 				sd->status.inventory[i].amount < 1
  8218. 				)
  8219. 			{	//Something went wrong, item exploit?
  8220. 				sd->itemid = sd->itemindex = -1;
  8221. 				return 0;
  8222. 			}
  8223. 			//Consume
  8224. 			sd->itemid = sd->itemindex = -1;
  8225. 			if( skill == WZ_EARTHSPIKE && sc && sc->data[SC_EARTHSCROLL] && rand()%100 > sc->data[SC_EARTHSCROLL]->val2 ) // [marquis007]
  8226. 				; //Do not consume item.
  8227. 			else if( sd->status.inventory[i].expire_time == 0 )
  8228. 				pc_delitem(sd,i,1,0,0); // Rental usable items are not consumed until expiration
  8229. 		}
  8230. 		return 1;
  8231. 	}
  8232.  
  8233. 	if( pc_is90overweight(sd) )
  8234. 	{
  8235. 		clif_skill_fail(sd,skill,USESKILL_FAIL_WEIGHTOVER,0);
  8236. 		return 0;
  8237. 	}
  8238.  
  8239. 	switch( skill )
  8240. 	{ // Turn off check.
  8241. 	case BS_MAXIMIZE:		case NV_TRICKDEAD:	case TF_HIDING:			case AS_CLOAKING:		case CR_AUTOGUARD:
  8242. 	case ML_AUTOGUARD:		case CR_DEFENDER:	case ML_DEFENDER:		case ST_CHASEWALK:		case PA_GOSPEL:
  8243. 	case CR_SHRINK:			case TK_RUN:		case GS_GATLINGFEVER:	case TK_READYCOUNTER:	case TK_READYDOWN:
  8244. 	case TK_READYSTORM:		case TK_READYTURN:	case SG_FUSION:
  8245. 		if( sc && sc->data[status_skill2sc(skill)] )
  8246. 			return 1;
  8247. 	}
  8248.  
  8249. 	if( lv < 1 || lv > MAX_SKILL_LEVEL )
  8250. 		return 0;
  8251.  
  8252. 	require = skill_get_requirement(sd,skill,lv);
  8253.  
  8254. 	//Can only update state when weapon/arrow info is checked.
  8255. 	sd->state.arrow_atk = require.ammo?1:0;
  8256.  
  8257. 	// perform skill-specific checks (and actions)
  8258. 	switch( skill )
  8259. 	{
  8260. 	case SA_CASTCANCEL:
  8261. 		if(sd->ud.skilltimer == INVALID_TIMER) {
  8262. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8263. 			return 0;
  8264. 		}
  8265. 		break;
  8266. 	case AL_WARP:
  8267. 		if(!battle_config.duel_allow_teleport && sd->duel_group) { // duel restriction [LuzZza]
  8268. 			clif_displaymessage(sd->fd, "Duel: Can't use warp in duel.");
  8269. 			return 0;
  8270. 		}
  8271. 		break;
  8272. 	case MO_CALLSPIRITS:
  8273. 		if(sd->spiritball >= lv) {
  8274. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8275. 			return 0;
  8276. 		}
  8277. 		break;
  8278. 	case MO_FINGEROFFENSIVE:
  8279. 	case GS_FLING:
  8280. 		if( sd->spiritball > 0 && sd->spiritball < require.spiritball )
  8281. 			sd->spiritball_old = require.spiritball = sd->spiritball;
  8282. 		else
  8283. 			sd->spiritball_old = require.spiritball;
  8284. 		break;
  8285. 	case MO_CHAINCOMBO:
  8286. 		if(!sc)
  8287. 			return 0;
  8288. 		if(sc->data[SC_BLADESTOP])
  8289. 			break;
  8290. 		if(sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == MO_TRIPLEATTACK)
  8291. 			break;
  8292. 		return 0;
  8293. 	case MO_COMBOFINISH:
  8294. 		if(!(sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == MO_CHAINCOMBO))
  8295. 			return 0;
  8296. 		break;
  8297. 	case CH_TIGERFIST:
  8298. 		if(!(sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == MO_COMBOFINISH))
  8299. 			return 0;
  8300. 		break;
  8301. 	case CH_CHAINCRUSH:
  8302. 		if(!(sc && sc->data[SC_COMBO]))
  8303. 			return 0;
  8304. 		if(sc->data[SC_COMBO]->val1 != MO_COMBOFINISH && sc->data[SC_COMBO]->val1 != CH_TIGERFIST)
  8305. 			return 0;
  8306. 		break;
  8307. 	case MO_EXTREMITYFIST:
  8308. //		if(sc && sc->data[SC_EXTREMITYFIST]) //To disable Asura during the 5 min skill block uncomment this...
  8309. //			return 0;
  8310. 		if( sc && sc->data[SC_BLADESTOP] )
  8311. 			break;
  8312. 		if( sc && sc->data[SC_COMBO] )
  8313. 		{
  8314. 			switch(sc->data[SC_COMBO]->val1) {
  8315. 				case MO_COMBOFINISH:
  8316. 				case CH_TIGERFIST:
  8317. 				case CH_CHAINCRUSH:
  8318. 					break;
  8319. 				default:
  8320. 					return 0;
  8321. 			}
  8322. 		}
  8323. 		else if( !unit_can_move(&sd->bl) )
  8324. 	  	{	//Placed here as ST_MOVE_ENABLE should not apply if rooted or on a combo. [Skotlex]
  8325. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8326. 			return 0;
  8327. 		}
  8328. 		break;
  8329.  
  8330. 	case TK_MISSION:
  8331. 		if( (sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON )
  8332. 		{// Cannot be used by Non-Taekwon classes
  8333. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8334. 			return 0;
  8335. 		}
  8336. 		break;
  8337.  
  8338. 	case TK_READYCOUNTER:
  8339. 	case TK_READYDOWN:
  8340. 	case TK_READYSTORM:
  8341. 	case TK_READYTURN:
  8342. 	case TK_JUMPKICK:
  8343. 		if( (sd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER )
  8344. 		{// Soul Linkers cannot use this skill
  8345. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8346. 			return 0;
  8347. 		}
  8348. 		break;
  8349.  
  8350. 	case TK_TURNKICK:
  8351. 	case TK_STORMKICK:
  8352. 	case TK_DOWNKICK:
  8353. 	case TK_COUNTER:
  8354. 		if ((sd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER)
  8355. 			return 0; //Anti-Soul Linker check in case you job-changed with Stances active.
  8356. 		if(!(sc && sc->data[SC_COMBO]))
  8357. 			return 0; //Combo needs to be ready
  8358.  
  8359. 		if (sc->data[SC_COMBO]->val3)
  8360. 		{	//Kick chain
  8361. 			//Do not repeat a kick.
  8362. 			if (sc->data[SC_COMBO]->val3 != skill)
  8363. 				break;
  8364. 			status_change_end(&sd->bl, SC_COMBO, INVALID_TIMER);
  8365. 			return 0;
  8366. 		}
  8367. 		if(sc->data[SC_COMBO]->val1 != skill)
  8368. 		{	//Cancel combo wait.
  8369. 			unit_cancel_combo(&sd->bl);
  8370. 			return 0;
  8371. 		}
  8372. 		break; //Combo ready.
  8373. 	case BD_ADAPTATION:
  8374. 		{
  8375. 			int time;
  8376. 			if(!(sc && sc->data[SC_DANCING]))
  8377. 			{
  8378. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8379. 				return 0;
  8380. 			}
  8381. 			time = 1000*(sc->data[SC_DANCING]->val3>>16);
  8382. 			if (skill_get_time(
  8383. 				(sc->data[SC_DANCING]->val1&0xFFFF), //Dance Skill ID
  8384. 				(sc->data[SC_DANCING]->val1>>16)) //Dance Skill LV
  8385. 				- time < skill_get_time2(skill,lv))
  8386. 			{
  8387. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8388. 				return 0;
  8389. 			}
  8390. 		}
  8391. 		break;
  8392.  
  8393. 	case PR_BENEDICTIO:
  8394. 		if (skill_check_pc_partner(sd, skill, &lv, 1, 0) < 2)
  8395. 		{
  8396. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8397. 			return 0;
  8398. 		}
  8399. 		break;
  8400.  
  8401. 	case SL_SMA:
  8402. 		if(!(sc && sc->data[SC_SMA]))
  8403. 			return 0;
  8404. 		break;
  8405.  
  8406. 	case HT_POWER:
  8407. 		if(!(sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == skill))
  8408. 			return 0;
  8409. 		break;
  8410.  
  8411. 	case CG_HERMODE:
  8412. 		if(!npc_check_areanpc(1,sd->bl.m,sd->bl.x,sd->bl.y,skill_get_splash(skill, lv)))
  8413. 		{
  8414. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8415. 			return 0;
  8416. 		}
  8417. 		break;
  8418. 	case CG_MOONLIT: //Check there's no wall in the range+1 area around the caster. [Skotlex]
  8419. 		{
  8420. 			int i,x,y,range = skill_get_splash(skill, lv)+1;
  8421. 			int size = range*2+1;
  8422. 			for (i=0;i<size*size;i++) {
  8423. 				x = sd->bl.x+(i%size-range);
  8424. 				y = sd->bl.y+(i/size-range);
  8425. 				if (map_getcell(sd->bl.m,x,y,CELL_CHKWALL)) {
  8426. 					clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8427. 					return 0;
  8428. 				}
  8429. 			}
  8430. 		}
  8431. 		break;
  8432. 	case PR_REDEMPTIO:
  8433. 		{
  8434. 			int exp;
  8435. 			if( ((exp = pc_nextbaseexp(sd)) > 0 && get_percentage(sd->status.base_exp, exp) < 1) ||
  8436. 				((exp = pc_nextjobexp(sd)) > 0 && get_percentage(sd->status.job_exp, exp) < 1)) {
  8437. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0); //Not enough exp.
  8438. 				return 0;
  8439. 			}
  8440. 			break;
  8441. 		}
  8442. 	case AM_TWILIGHT2:
  8443. 	case AM_TWILIGHT3:
  8444. 		if (!party_skill_check(sd, sd->status.party_id, skill, lv))
  8445. 		{
  8446. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8447. 			return 0;
  8448. 		}
  8449. 		break;
  8450. 	case SG_SUN_WARM:
  8451. 	case SG_MOON_WARM:
  8452. 	case SG_STAR_WARM:
  8453. 		if (sc && sc->data[SC_MIRACLE])
  8454. 			break;
  8455. 		i = skill-SG_SUN_WARM;
  8456. 		if (sd->bl.m == sd->feel_map[i].m)
  8457. 			break;
  8458. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8459. 		return 0;
  8460. 		break;
  8461. 	case SG_SUN_COMFORT:
  8462. 	case SG_MOON_COMFORT:
  8463. 	case SG_STAR_COMFORT:
  8464. 		if (sc && sc->data[SC_MIRACLE])
  8465. 			break;
  8466. 		i = skill-SG_SUN_COMFORT;
  8467. 		if (sd->bl.m == sd->feel_map[i].m &&
  8468. 			(battle_config.allow_skill_without_day || sg_info[i].day_func()))
  8469. 			break;
  8470. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8471. 		return 0;
  8472. 	case SG_FUSION:
  8473. 		if (sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_STAR)
  8474. 			break;
  8475. 		//Auron insists we should implement SP consumption when you are not Soul Linked. [Skotlex]
  8476. 		//Only invoke on skill begin cast (instant cast skill). [Kevin]
  8477. 		if( require.sp > 0 )
  8478. 		{
  8479. 			if (status->sp < (unsigned int)require.sp)
  8480. 				clif_skill_fail(sd,skill,USESKILL_FAIL_SP_INSUFFICIENT,0);
  8481. 			else
  8482. 				status_zap(&sd->bl, 0, require.sp);
  8483. 		}
  8484. 		return 0;
  8485. 	case GD_BATTLEORDER:
  8486. 	case GD_REGENERATION:
  8487. 	case GD_RESTORE:
  8488. 		if (!map_flag_gvg2(sd->bl.m)) {
  8489. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8490. 			return 0;
  8491. 		}
  8492. 	case GD_EMERGENCYCALL:
  8493. 		// other checks were already done in skillnotok()
  8494. 		if (!sd->status.guild_id || !sd->state.gmaster_flag)
  8495. 			return 0;
  8496. 		break;
  8497.  
  8498. 	case GS_GLITTERING:
  8499. 		if(sd->spiritball >= 10) {
  8500. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8501. 			return 0;
  8502. 		}
  8503. 		break;
  8504.  
  8505. 	case NJ_ISSEN:
  8506. 		if (status->hp < 2) {
  8507. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8508. 			return 0;
  8509. 		}
  8510. 	case NJ_BUNSINJYUTSU:
  8511. 		if (!(sc && sc->data[SC_NEN])) {
  8512. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8513. 			return 0;
  8514. 	  	}
  8515. 		break;
  8516.  
  8517. 	case NJ_ZENYNAGE:
  8518. 		if(sd->status.zeny < require.zeny) {
  8519. 			clif_skill_fail(sd,skill,USESKILL_FAIL_MONEY,0);
  8520. 			return 0;
  8521. 		}
  8522. 		break;
  8523. 	case PF_HPCONVERSION:
  8524. 		if (status->sp == status->max_sp)
  8525. 			return 0; //Unusable when at full SP.
  8526. 		break;
  8527. 	case AM_CALLHOMUN: //Can't summon if a hom is already out
  8528. 		if (sd->status.hom_id && sd->hd && !sd->hd->homunculus.vaporize) {
  8529. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8530. 			return 0;
  8531. 		}
  8532. 		break;
  8533. 	case AM_REST: //Can't vapo homun if you don't have an active homunc or it's hp is < 80%
  8534. 		if (!merc_is_hom_active(sd->hd) || sd->hd->battle_status.hp < (sd->hd->battle_status.max_hp*80/100))
  8535. 		{
  8536. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8537. 			return 0;
  8538. 		}
  8539. 		break;
  8540. 	}
  8541.  
  8542. 	switch(require.state) {
  8543. 	case ST_HIDING:
  8544. 		if(!(sc && sc->option&OPTION_HIDE)) {
  8545. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8546. 			return 0;
  8547. 		}
  8548. 		break;
  8549. 	case ST_CLOAKING:
  8550. 		if(!pc_iscloaking(sd)) {
  8551. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8552. 			return 0;
  8553. 		}
  8554. 		break;
  8555. 	case ST_HIDDEN:
  8556. 		if(!pc_ishiding(sd)) {
  8557. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8558. 			return 0;
  8559. 		}
  8560. 		break;
  8561. 	case ST_RIDING:
  8562. 		if(!pc_isriding(sd)) {
  8563. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8564. 			return 0;
  8565. 		}
  8566. 		break;
  8567. 	case ST_FALCON:
  8568. 		if(!pc_isfalcon(sd)) {
  8569. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8570. 			return 0;
  8571. 		}
  8572. 		break;
  8573. 	case ST_CARTBOOST:
  8574. 		if(!(sc && sc->data[SC_CARTBOOST])) {
  8575. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8576. 			return 0;
  8577. 		}
  8578. 	case ST_CART:
  8579. 		if(!pc_iscarton(sd)) {
  8580. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8581. 			return 0;
  8582. 		}
  8583. 		break;
  8584. 	case ST_SHIELD:
  8585. 		if(sd->status.shield <= 0) {
  8586. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8587. 			return 0;
  8588. 		}
  8589. 		break;
  8590. 	case ST_SIGHT:
  8591. 		if(!(sc && sc->data[SC_SIGHT])) {
  8592. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8593. 			return 0;
  8594. 		}
  8595. 		break;
  8596. 	case ST_EXPLOSIONSPIRITS:
  8597. 		if(!(sc && sc->data[SC_EXPLOSIONSPIRITS])) {
  8598. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8599. 			return 0;
  8600. 		}
  8601. 		break;
  8602. 	case ST_RECOV_WEIGHT_RATE:
  8603. 		if(battle_config.natural_heal_weight_rate <= 100 && sd->weight*100/sd->max_weight >= (unsigned int)battle_config.natural_heal_weight_rate) {
  8604. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8605. 			return 0;
  8606. 		}
  8607. 		break;
  8608. 	case ST_MOVE_ENABLE:
  8609. 		if (sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == skill)
  8610. 			sd->ud.canmove_tick = gettick(); //When using a combo, cancel the can't move delay to enable the skill. [Skotlex]
  8611.  
  8612. 		if (!unit_can_move(&sd->bl)) {
  8613. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8614. 			return 0;
  8615. 		}
  8616. 		break;
  8617. 	case ST_WATER:
  8618. 		if (sc && (sc->data[SC_DELUGE] || sc->data[SC_SUITON]))
  8619. 			break;
  8620. 		if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKWATER))
  8621. 			break;
  8622. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8623. 		return 0;
  8624. 	}
  8625.  
  8626. 	if(require.mhp > 0 && get_percentage(status->hp, status->max_hp) > require.mhp) {
  8627. 		//mhp is the max-hp-requirement, that is,
  8628. 		//you must have this % or less of HP to cast it.
  8629. 		clif_skill_fail(sd,skill,USESKILL_FAIL_HP_INSUFFICIENT,0);
  8630. 		return 0;
  8631. 	}
  8632.  
  8633. 	if( require.weapon && !pc_check_weapontype(sd,require.weapon) ) {
  8634. 		clif_skill_fail(sd,skill,USESKILL_FAIL_THIS_WEAPON,0);
  8635. 		return 0;
  8636. 	}
  8637.  
  8638. 	if( require.sp > 0 && status->sp < (unsigned int)require.sp) {
  8639. 		clif_skill_fail(sd,skill,USESKILL_FAIL_SP_INSUFFICIENT,0);
  8640. 		return 0;
  8641. 	}
  8642.  
  8643. 	if( require.zeny > 0 && sd->status.zeny < require.zeny ) {
  8644. 		clif_skill_fail(sd,skill,USESKILL_FAIL_MONEY,0);
  8645. 		return 0;
  8646. 	}
  8647.  
  8648. 	if( require.spiritball > 0 && sd->spiritball < require.spiritball) {
  8649. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8650. 		return 0;
  8651. 	}
  8652.  
  8653. 	return 1;
  8654. }
  8655.  
  8656. int skill_check_condition_castend(struct map_session_data* sd, short skill, short lv)
  8657. {
  8658. 	struct skill_condition require;
  8659. 	struct status_data *status;
  8660. 	int i;
  8661. 	int index[MAX_SKILL_ITEM_REQUIRE];
  8662.  
  8663. 	nullpo_ret(sd);
  8664.  
  8665. 	if( lv <= 0 || sd->chatID )
  8666. 		return 0;
  8667.  
  8668. 	if( battle_config.gm_skilluncond && pc_isGM(sd) >= battle_config.gm_skilluncond && sd->skillitem != skill )
  8669. 	{	//GMs don't override the skillItem check, otherwise they can use items without them being consumed! [Skotlex]	
  8670. 		sd->state.arrow_atk = skill_get_ammotype(skill)?1:0; //Need to do arrow state check.
  8671. 		sd->spiritball_old = sd->spiritball; //Need to do Spiritball check.
  8672. 		return 1;
  8673. 	}
  8674.  
  8675. 	if( sd->menuskill_id == AM_PHARMACY )
  8676. 	{ // Cast start or cast end??
  8677. 		switch( skill )
  8678. 		{
  8679. 		case AM_PHARMACY:
  8680. 		case AC_MAKINGARROW:
  8681. 		case BS_REPAIRWEAPON:
  8682. 		case AM_TWILIGHT1:
  8683. 		case AM_TWILIGHT2:
  8684. 		case AM_TWILIGHT3:
  8685. 			return 0;
  8686. 		}
  8687. 	}
  8688.  
  8689. 	if( sd->skillitem == skill ) // Casting finished (Item skill or Hocus-Pocus)
  8690. 		return 1;
  8691.  
  8692. 	if( pc_is90overweight(sd) )
  8693. 	{
  8694. 		clif_skill_fail(sd,skill,USESKILL_FAIL_WEIGHTOVER,0);
  8695. 		return 0;
  8696. 	}
  8697.  
  8698. 	// perform skill-specific checks (and actions)
  8699. 	switch( skill )
  8700. 	{
  8701. 	case PR_BENEDICTIO:
  8702. 		skill_check_pc_partner(sd, skill, &lv, 1, 1);
  8703. 		break;
  8704. 	case AM_CANNIBALIZE:
  8705. 	case AM_SPHEREMINE:
  8706. 	{
  8707. 		int c=0;
  8708. 		int summons[5] = { 1589, 1579, 1575, 1555, 1590 };
  8709. 		//int summons[5] = { 1020, 1068, 1118, 1500, 1368 };
  8710. 		int maxcount = (skill==AM_CANNIBALIZE)? 6-lv : skill_get_maxcount(skill,lv);
  8711. 		int mob_class = (skill==AM_CANNIBALIZE)? summons[lv-1] :1142;
  8712. 		if(battle_config.land_skill_limit && maxcount>0 && (battle_config.land_skill_limit&BL_PC)) {
  8713. 			i = map_foreachinmap(skill_check_condition_mob_master_sub ,sd->bl.m, BL_MOB, sd->bl.id, mob_class, skill, &c);
  8714. 			if(c >= maxcount ||
  8715. 				(skill==AM_CANNIBALIZE && c != i && battle_config.summon_flora&2))
  8716. 			{	//Fails when: exceed max limit. There are other plant types already out.
  8717. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8718. 				return 0;
  8719. 			}
  8720. 		}
  8721. 		break;
  8722. 	}
  8723. 	}
  8724.  
  8725. 	status = &sd->battle_status;
  8726.  
  8727. 	require = skill_get_requirement(sd,skill,lv);
  8728.  
  8729. 	if( require.hp > 0 && status->hp <= (unsigned int)require.hp) {
  8730. 		clif_skill_fail(sd,skill,USESKILL_FAIL_HP_INSUFFICIENT,0);
  8731. 		return 0;
  8732. 	}
  8733.  
  8734. 	if( require.ammo ) { //Skill requires stuff equipped in the arrow slot.
  8735. 		if((i=sd->equip_index[EQI_AMMO]) < 0 ||
  8736. 			!sd->inventory_data[i] ||
  8737. 			sd->status.inventory[i].amount < require.ammo_qty
  8738. 		) {
  8739. 			clif_arrow_fail(sd,0);
  8740. 			return 0;
  8741. 		}
  8742. 		if (!(require.ammo&1<<sd->inventory_data[i]->look))
  8743. 		{	//Ammo type check. Send the "wrong weapon type" message
  8744. 			//which is the closest we have to wrong ammo type. [Skotlex]
  8745. 			clif_arrow_fail(sd,0); //Haplo suggested we just send the equip-arrows message instead. [Skotlex]
  8746. 			//clif_skill_fail(sd,skill,USESKILL_FAIL_THIS_WEAPON,0);
  8747. 			return 0;
  8748. 		}
  8749. 	}
  8750.  
  8751. 	for( i = 0; i < MAX_SKILL_ITEM_REQUIRE; ++i )
  8752. 	{
  8753. 		if( !require.itemid[i] )
  8754. 			continue;
  8755. 		index[i] = pc_search_inventory(sd,require.itemid[i]);
  8756. 		if( index[i] < 0 || sd->status.inventory[index[i]].amount < require.amount[i] ) {
  8757. 			if( require.itemid[i] == ITEMID_RED_GEMSTONE )
  8758. 				clif_skill_fail(sd,skill,USESKILL_FAIL_REDJAMSTONE,0);// red gemstone required
  8759. 			else if( require.itemid[i] == ITEMID_BLUE_GEMSTONE )
  8760. 				clif_skill_fail(sd,skill,USESKILL_FAIL_BLUEJAMSTONE,0);// blue gemstone required
  8761. 			else
  8762. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8763. 			return 0;
  8764. 		}
  8765. 	}
  8766.  
  8767. 	return 1;
  8768. }
  8769.  
  8770. // type&2: consume items (after skill was used)
  8771. // type&1: consume the others (before skill was used)
  8772. int skill_consume_requirement( struct map_session_data *sd, short skill, short lv, short type)
  8773. {
  8774. 	int n,i;
  8775. 	struct skill_condition req;
  8776.  
  8777. 	nullpo_ret(sd);
  8778.  
  8779. 	req = skill_get_requirement(sd,skill,lv);
  8780.  
  8781. 	if( type&1 )
  8782. 	{
  8783. 		if( skill == CG_TAROTCARD || sd->state.autocast )
  8784. 			req.sp = 0; // TarotCard will consume sp in skill_cast_nodamage_id [Inkfish]
  8785. 		if(req.hp || req.sp)
  8786. 			status_zap(&sd->bl, req.hp, req.sp);
  8787.  
  8788. 		if(req.spiritball > 0)
  8789. 			pc_delspiritball(sd,req.spiritball,0);
  8790.  
  8791. 		if(req.zeny > 0)
  8792. 		{
  8793. 			if( skill == NJ_ZENYNAGE )
  8794. 				req.zeny = 0; //Zeny is reduced on skill_attack.
  8795. 			if( sd->status.zeny < req.zeny )
  8796. 				req.zeny = sd->status.zeny;
  8797. 			pc_payzeny(sd,req.zeny);
  8798. 		}
  8799. 	}
  8800.  
  8801. 	if( type&2 )
  8802. 	{
  8803. 		struct status_change *sc = &sd->sc;
  8804.  
  8805. 		if( !sc->count )
  8806. 			sc = NULL;
  8807.  
  8808. 		for( i = 0; i < MAX_SKILL_ITEM_REQUIRE; ++i )
  8809. 		{
  8810. 			if( !req.itemid[i] )
  8811. 				continue;
  8812.  
  8813. 			if( itemid_isgemstone(req.itemid[i]) && skill != HW_GANBANTEIN && sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_WIZARD )
  8814. 				continue; //Gemstones are checked, but not substracted from inventory.
  8815.  
  8816. 			if( (n = pc_search_inventory(sd,req.itemid[i])) >= 0 )
  8817. 				pc_delitem(sd,n,req.amount[i],0,1);
  8818. 		}
  8819. 	}
  8820.  
  8821. 	return 1;
  8822. }
  8823.  
  8824. struct skill_condition skill_get_requirement(struct map_session_data* sd, short skill, short lv)
  8825. {
  8826. 	struct skill_condition req;
  8827. 	struct status_data *status;
  8828. 	struct status_change *sc;
  8829. 	int i,j,hp_rate,sp_rate;
  8830.  
  8831. 	memset(&req,0,sizeof(req));
  8832.  
  8833. 	if( !sd )
  8834. 		return req;
  8835.  
  8836. 	if( sd->skillitem == skill )
  8837. 		return req; // Item skills and Hocus-Pocus don't have requirements.[Inkfish]
  8838.  
  8839. 	sc = &sd->sc;
  8840. 	if( !sc->count )
  8841. 		sc = NULL;
  8842.  
  8843. 	switch( skill )
  8844. 	{ // Turn off check.
  8845. 	case BS_MAXIMIZE:		case NV_TRICKDEAD:	case TF_HIDING:			case AS_CLOAKING:		case CR_AUTOGUARD:
  8846. 	case ML_AUTOGUARD:		case CR_DEFENDER:	case ML_DEFENDER:		case ST_CHASEWALK:		case PA_GOSPEL:
  8847. 	case CR_SHRINK:			case TK_RUN:		case GS_GATLINGFEVER:	case TK_READYCOUNTER:	case TK_READYDOWN:
  8848. 	case TK_READYSTORM:		case TK_READYTURN:	case SG_FUSION:
  8849. 		if( sc && sc->data[status_skill2sc(skill)] )
  8850. 			return req;
  8851. 	}
  8852.  
  8853. 	j = skill_get_index(skill);
  8854. 	if( j == 0 ) // invalid skill id
  8855.   		return req;
  8856. 	if( lv < 1 || lv > MAX_SKILL_LEVEL )
  8857. 		return req;
  8858.  
  8859. 	status = &sd->battle_status;
  8860.  
  8861. 	req.hp = skill_db[j].hp[lv-1];
  8862. 	hp_rate = skill_db[j].hp_rate[lv-1];
  8863. 	if(hp_rate > 0)
  8864. 		req.hp += (status->hp * hp_rate)/100;
  8865. 	else
  8866. 		req.hp += (status->max_hp * (-hp_rate))/100;
  8867.  
  8868. 	req.sp = skill_db[j].sp[lv-1];
  8869. 	if((sd->skillid_old == BD_ENCORE) && skill == sd->skillid_dance)
  8870. 		req.sp /= 2;
  8871. 	sp_rate = skill_db[j].sp_rate[lv-1];
  8872. 	if(sp_rate > 0)
  8873. 		req.sp += (status->sp * sp_rate)/100;
  8874. 	else
  8875. 		req.sp += (status->max_sp * (-sp_rate))/100;
  8876. 	if( sd->dsprate!=100 )
  8877. 		req.sp = req.sp * sd->dsprate / 100;
  8878.  
  8879. 	req.zeny = skill_db[j].zeny[lv-1];
  8880.  
  8881. 	req.spiritball = skill_db[j].spiritball[lv-1];
  8882.  
  8883. 	req.state = skill_db[j].state;
  8884.  
  8885. 	req.mhp = skill_db[j].mhp[lv-1];
  8886.  
  8887. 	req.weapon = skill_db[j].weapon;
  8888.  
  8889. 	req.ammo_qty = skill_db[j].ammo_qty[lv-1];
  8890. 	if (req.ammo_qty)
  8891. 		req.ammo = skill_db[j].ammo;
  8892.  
  8893. 	if (!req.ammo && skill && skill_isammotype(sd, skill))
  8894. 	{	//Assume this skill is using the weapon, therefore it requires arrows.
  8895. 		req.ammo = 0xFFFFFFFF; //Enable use on all ammo types.
  8896. 		req.ammo_qty = 1;
  8897. 	}
  8898.  
  8899. 	for( i = 0; i < MAX_SKILL_ITEM_REQUIRE; i++ )
  8900. 	{
  8901. 		if( (skill == AM_POTIONPITCHER || skill == CR_SLIMPITCHER || skill == CR_CULTIVATION) && i != lv%11 - 1 )
  8902. 			continue;
  8903.  
  8904. 		switch( skill )
  8905. 		{
  8906. 		case AM_CALLHOMUN:
  8907. 			if (sd->status.hom_id) //Don't delete items when hom is already out.
  8908. 				continue;
  8909. 			break;
  8910. 		case WZ_FIREPILLAR: // celest
  8911. 			if (lv <= 5)	// no gems required at level 1-5
  8912. 				continue;
  8913. 			break;
  8914. 		}
  8915.  
  8916. 		req.itemid[i] = skill_db[j].itemid[i];
  8917. 		req.amount[i] = skill_db[j].amount[i];
  8918.  
  8919. 		if( itemid_isgemstone(req.itemid[i]) && skill != HW_GANBANTEIN )
  8920. 		{
  8921. 			if( sd->special_state.no_gemstone )
  8922. 			{	//Make it substract 1 gem rather than skipping the cost.
  8923. 				if( --req.amount[i] < 1 )
  8924. 					req.itemid[i] = 0;
  8925. 			}
  8926. 			if(sc && sc->data[SC_INTOABYSS])
  8927. 			{
  8928. 				if( skill != SA_ABRACADABRA )
  8929. 					req.itemid[i] = req.amount[i] = 0;
  8930. 				else if( --req.amount[i] < 1 )
  8931. 					req.amount[i] = 1; // Hocus Pocus allways use at least 1 gem
  8932. 			}
  8933. 		}
  8934. 	}
  8935.  
  8936. 	// Check for cost reductions due to skills & SCs
  8937. 	switch(skill) {
  8938. 		case MC_MAMMONITE:
  8939. 			if(pc_checkskill(sd,BS_UNFAIRLYTRICK)>0)
  8940. 				req.zeny -= req.zeny*10/100;
  8941. 			break;
  8942. 		case AL_HOLYLIGHT:
  8943. 			if(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_PRIEST)
  8944. 				req.sp *= 5;
  8945. 			break;
  8946. 		case SL_SMA:
  8947. 		case SL_STUN:
  8948. 		case SL_STIN:
  8949. 		{
  8950. 			int kaina_lv = pc_checkskill(sd,SL_KAINA);
  8951.  
  8952. 			if(kaina_lv==0 || sd->status.base_level<70)
  8953. 				break;
  8954. 			if(sd->status.base_level>=90)
  8955. 				req.sp -= req.sp*7*kaina_lv/100;
  8956. 			else if(sd->status.base_level>=80)
  8957. 				req.sp -= req.sp*5*kaina_lv/100;
  8958. 			else if(sd->status.base_level>=70)
  8959. 				req.sp -= req.sp*3*kaina_lv/100;
  8960. 		}
  8961. 			break;
  8962. 		case MO_TRIPLEATTACK:
  8963. 		case MO_CHAINCOMBO:
  8964. 		case MO_COMBOFINISH:
  8965. 		case CH_TIGERFIST:
  8966. 		case CH_CHAINCRUSH:
  8967. 			if(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_MONK)
  8968. 				req.sp -= req.sp*25/100; //FIXME: Need real data. this is a custom value.
  8969. 			break;
  8970. 		case MO_BODYRELOCATION:
  8971. 			if( sc && sc->data[SC_EXPLOSIONSPIRITS] )
  8972. 				req.spiritball = 0;
  8973. 			break;
  8974. 		case MO_EXTREMITYFIST:
  8975. 			if( sc )
  8976. 			{
  8977. 				if( sc->data[SC_BLADESTOP] )
  8978. 					req.spiritball--;
  8979. 				else if( sc->data[SC_COMBO] )
  8980. 				{
  8981. 					switch( sc->data[SC_COMBO]->val1 )
  8982. 					{
  8983. 						case MO_COMBOFINISH:
  8984. 							req.spiritball = 4;
  8985. 							break;
  8986. 						case CH_TIGERFIST:
  8987. 							req.spiritball = 3;
  8988. 							break;
  8989. 						case CH_CHAINCRUSH:	//It should consume whatever is left as long as it's at least 1.
  8990. 							req.spiritball = sd->spiritball?sd->spiritball:1;
  8991. 							break;
  8992. 					}
  8993. 				}
  8994. 			}
  8995. 			break;
  8996. 	}
  8997.  
  8998. 	return req;
  8999. }
  9000.  
  9001. /*==========================================
  9002.  * Does cast-time reductions based on dex, item bonuses and config setting
  9003.  *------------------------------------------*/
  9004. int skill_castfix (struct block_list *bl, int skill_id, int skill_lv)
  9005. {
  9006. 	int time = skill_get_cast(skill_id, skill_lv);
  9007. 	struct map_session_data *sd;
  9008.  
  9009. 	nullpo_ret(bl);
  9010. 	sd = BL_CAST(BL_PC, bl);
  9011.  
  9012. 	// calculate base cast time (reduced by dex)
  9013. 	if( !(skill_get_castnodex(skill_id, skill_lv)&1) )
  9014. 	{
  9015. 		int scale = battle_config.castrate_dex_scale - status_get_dex(bl);
  9016. 		if( scale > 0 )	// not instant cast
  9017. 			time = time * scale / battle_config.castrate_dex_scale;
  9018. 		else return 0;	// instant cast
  9019. 	}
  9020.  
  9021. 	// calculate cast time reduced by item/card bonuses
  9022. 	if( !(skill_get_castnodex(skill_id, skill_lv)&4) && sd )
  9023. 	{
  9024. 		int i;
  9025. 		if( sd->castrate != 100 )
  9026. 			time = time * sd->castrate / 100;
  9027. 		for( i = 0; i < ARRAYLENGTH(sd->skillcast) && sd->skillcast[i].id; i++ )
  9028. 		{
  9029. 			if( sd->skillcast[i].id == skill_id )
  9030. 			{
  9031. 				time+= time * sd->skillcast[i].val / 100;
  9032. 				break;
  9033. 			}
  9034. 		}
  9035. 	}
  9036.  
  9037. 	// config cast time multiplier
  9038. 	if (battle_config.cast_rate != 100)
  9039. 		time = time * battle_config.cast_rate / 100;
  9040.  
  9041. 	// return final cast time
  9042. 	return (time > 0) ? time : 0;
  9043. }
  9044.  
  9045. /*==========================================
  9046.  * Does cast-time reductions based on sc data.
  9047.  *------------------------------------------*/
  9048. int skill_castfix_sc (struct block_list *bl, int time)
  9049. {
  9050. 	struct status_change *sc = status_get_sc(bl);
  9051.  
  9052. 	if (sc && sc->count) {
  9053. 		if (sc->data[SC_SLOWCAST])
  9054. 			time += time * sc->data[SC_SLOWCAST]->val2 / 100;
  9055. 		if (sc->data[SC_SUFFRAGIUM]) {
  9056. 			time -= time * sc->data[SC_SUFFRAGIUM]->val2 / 100;
  9057. 			status_change_end(bl, SC_SUFFRAGIUM, INVALID_TIMER);
  9058. 		}
  9059. 		if (sc->data[SC_MEMORIZE]) {
  9060. 			time>>=1;
  9061. 			if ((--sc->data[SC_MEMORIZE]->val2) <= 0)
  9062. 				status_change_end(bl, SC_MEMORIZE, INVALID_TIMER);
  9063. 		}
  9064. 		if (sc->data[SC_POEMBRAGI])
  9065. 			time -= time * sc->data[SC_POEMBRAGI]->val2 / 100;
  9066. 	}
  9067. 	return (time > 0) ? time : 0;
  9068. }
  9069.  
  9070. /*==========================================
  9071.  * Does delay reductions based on dex/agi, sc data, item bonuses, ...
  9072.  *------------------------------------------*/
  9073. int skill_delayfix (struct block_list *bl, int skill_id, int skill_lv)
  9074. {
  9075. 	int delaynodex = skill_get_delaynodex(skill_id, skill_lv);
  9076. 	int time = skill_get_delay(skill_id, skill_lv);
  9077. 	struct map_session_data *sd;
  9078. 	struct status_change *sc = status_get_sc(bl);
  9079.  
  9080. 	nullpo_ret(bl);
  9081. 	sd = BL_CAST(BL_PC, bl);
  9082.  
  9083. 	if (skill_id == SA_ABRACADABRA)
  9084. 		return 0; //Will use picked skill's delay.
  9085.  
  9086. 	if (bl->type&battle_config.no_skill_delay)
  9087. 		return battle_config.min_skill_delay_limit;
  9088.  
  9089. 	if (time < 0)
  9090. 		time = -time + status_get_amotion(bl);	// If set to <0, add to attack motion.
  9091.  
  9092. 	// Delay reductions
  9093. 	switch (skill_id)
  9094.   	{	//Monk combo skills have their delay reduced by agi/dex.
  9095. 	case MO_TRIPLEATTACK:
  9096. 	case MO_CHAINCOMBO:
  9097. 	case MO_COMBOFINISH:
  9098. 	case CH_TIGERFIST:
  9099. 	case CH_CHAINCRUSH:
  9100. 		time -= 4*status_get_agi(bl) - 2*status_get_dex(bl);
  9101. 		break;
  9102. 	case HP_BASILICA:
  9103. 		if( sc && !sc->data[SC_BASILICA] )
  9104. 			time = 0; // There is no Delay on Basilica creation, only on cancel
  9105. 		break;
  9106. 	default:
  9107. 		if (battle_config.delay_dependon_dex && !(delaynodex&1))
  9108. 		{	// if skill delay is allowed to be reduced by dex
  9109. 			int scale = battle_config.castrate_dex_scale - status_get_dex(bl);
  9110. 			if (scale > 0)
  9111. 				time = time * scale / battle_config.castrate_dex_scale;
  9112. 			else //To be capped later to minimum.
  9113. 				time = 0;
  9114. 		}
  9115. 		if (battle_config.delay_dependon_agi && !(delaynodex&1))
  9116. 		{	// if skill delay is allowed to be reduced by agi
  9117. 			int scale = battle_config.castrate_dex_scale - status_get_agi(bl);
  9118. 			if (scale > 0)
  9119. 				time = time * scale / battle_config.castrate_dex_scale;
  9120. 			else //To be capped later to minimum.
  9121. 				time = 0;
  9122. 		}
  9123. 	}
  9124.  
  9125. 	if ( sc && sc->data[SC_SPIRIT] )
  9126. 	{
  9127. 		switch (skill_id) {
  9128. 			case CR_SHIELDBOOMERANG:
  9129. 				if (sc->data[SC_SPIRIT]->val2 == SL_CRUSADER)
  9130. 					time /= 2;
  9131. 				break;
  9132. 			case AS_SONICBLOW:
  9133. 				if (!map_flag_gvg(bl->m) && !map[bl->m].flag.battleground && sc->data[SC_SPIRIT]->val2 == SL_ASSASIN)
  9134. 					time /= 2;
  9135. 				break;
  9136. 		}
  9137. 	}
  9138.  
  9139. 	if (!(delaynodex&2))
  9140. 	{
  9141. 		if (sc && sc->count) {
  9142. 			if (sc->data[SC_POEMBRAGI])
  9143. 				time -= time * sc->data[SC_POEMBRAGI]->val3 / 100;
  9144. 		}
  9145. 	}
  9146.  
  9147. 	if( !(delaynodex&4) && sd && sd->delayrate != 100 )
  9148. 		time = time * sd->delayrate / 100;
  9149.  
  9150. 	if (battle_config.delay_rate != 100)
  9151. 		time = time * battle_config.delay_rate / 100;
  9152.  
  9153. 	if (time < status_get_amotion(bl))
  9154. 		time = status_get_amotion(bl); // Delay can never be below amotion [Playtester]
  9155.  
  9156. 	return max(time, battle_config.min_skill_delay_limit);
  9157. }
  9158.  
  9159. /*=========================================
  9160.  *
  9161.  *-----------------------------------------*/
  9162. struct square {
  9163. 	int val1[5];
  9164. 	int val2[5];
  9165. };
  9166.  
  9167. static void skill_brandishspear_first (struct square *tc, int dir, int x, int y)
  9168. {
  9169. 	nullpo_retv(tc);
  9170.  
  9171. 	if(dir == 0){
  9172. 		tc->val1[0]=x-2;
  9173. 		tc->val1[1]=x-1;
  9174. 		tc->val1[2]=x;
  9175. 		tc->val1[3]=x+1;
  9176. 		tc->val1[4]=x+2;
  9177. 		tc->val2[0]=
  9178. 		tc->val2[1]=
  9179. 		tc->val2[2]=
  9180. 		tc->val2[3]=
  9181. 		tc->val2[4]=y-1;
  9182. 	}
  9183. 	else if(dir==2){
  9184. 		tc->val1[0]=
  9185. 		tc->val1[1]=
  9186. 		tc->val1[2]=
  9187. 		tc->val1[3]=
  9188. 		tc->val1[4]=x+1;
  9189. 		tc->val2[0]=y+2;
  9190. 		tc->val2[1]=y+1;
  9191. 		tc->val2[2]=y;
  9192. 		tc->val2[3]=y-1;
  9193. 		tc->val2[4]=y-2;
  9194. 	}
  9195. 	else if(dir==4){
  9196. 		tc->val1[0]=x-2;
  9197. 		tc->val1[1]=x-1;
  9198. 		tc->val1[2]=x;
  9199. 		tc->val1[3]=x+1;
  9200. 		tc->val1[4]=x+2;
  9201. 		tc->val2[0]=
  9202. 		tc->val2[1]=
  9203. 		tc->val2[2]=
  9204. 		tc->val2[3]=
  9205. 		tc->val2[4]=y+1;
  9206. 	}
  9207. 	else if(dir==6){
  9208. 		tc->val1[0]=
  9209. 		tc->val1[1]=
  9210. 		tc->val1[2]=
  9211. 		tc->val1[3]=
  9212. 		tc->val1[4]=x-1;
  9213. 		tc->val2[0]=y+2;
  9214. 		tc->val2[1]=y+1;
  9215. 		tc->val2[2]=y;
  9216. 		tc->val2[3]=y-1;
  9217. 		tc->val2[4]=y-2;
  9218. 	}
  9219. 	else if(dir==1){
  9220. 		tc->val1[0]=x-1;
  9221. 		tc->val1[1]=x;
  9222. 		tc->val1[2]=x+1;
  9223. 		tc->val1[3]=x+2;
  9224. 		tc->val1[4]=x+3;
  9225. 		tc->val2[0]=y-4;
  9226. 		tc->val2[1]=y-3;
  9227. 		tc->val2[2]=y-1;
  9228. 		tc->val2[3]=y;
  9229. 		tc->val2[4]=y+1;
  9230. 	}
  9231. 	else if(dir==3){
  9232. 		tc->val1[0]=x+3;
  9233. 		tc->val1[1]=x+2;
  9234. 		tc->val1[2]=x+1;
  9235. 		tc->val1[3]=x;
  9236. 		tc->val1[4]=x-1;
  9237. 		tc->val2[0]=y-1;
  9238. 		tc->val2[1]=y;
  9239. 		tc->val2[2]=y+1;
  9240. 		tc->val2[3]=y+2;
  9241. 		tc->val2[4]=y+3;
  9242. 	}
  9243. 	else if(dir==5){
  9244. 		tc->val1[0]=x+1;
  9245. 		tc->val1[1]=x;
  9246. 		tc->val1[2]=x-1;
  9247. 		tc->val1[3]=x-2;
  9248. 		tc->val1[4]=x-3;
  9249. 		tc->val2[0]=y+3;
  9250. 		tc->val2[1]=y+2;
  9251. 		tc->val2[2]=y+1;
  9252. 		tc->val2[3]=y;
  9253. 		tc->val2[4]=y-1;
  9254. 	}
  9255. 	else if(dir==7){
  9256. 		tc->val1[0]=x-3;
  9257. 		tc->val1[1]=x-2;
  9258. 		tc->val1[2]=x-1;
  9259. 		tc->val1[3]=x;
  9260. 		tc->val1[4]=x+1;
  9261. 		tc->val2[1]=y;
  9262. 		tc->val2[0]=y+1;
  9263. 		tc->val2[2]=y-1;
  9264. 		tc->val2[3]=y-2;
  9265. 		tc->val2[4]=y-3;
  9266. 	}
  9267.  
  9268. }
  9269.  
  9270. static void skill_brandishspear_dir (struct square* tc, int dir, int are)
  9271. {
  9272. 	int c;
  9273. 	nullpo_retv(tc);
  9274.  
  9275. 	for( c = 0; c < 5; c++ )
  9276. 	{
  9277. 		switch( dir )
  9278. 		{
  9279. 			case 0:                   tc->val2[c]+=are; break;
  9280. 			case 1: tc->val1[c]-=are; tc->val2[c]+=are; break;
  9281. 			case 2: tc->val1[c]-=are;                   break;
  9282. 			case 3: tc->val1[c]-=are; tc->val2[c]-=are; break;
  9283. 			case 4:                   tc->val2[c]-=are; break;
  9284. 			case 5: tc->val1[c]+=are; tc->val2[c]-=are; break;
  9285. 			case 6: tc->val1[c]+=are;                   break;
  9286. 			case 7: tc->val1[c]+=are; tc->val2[c]+=are; break;
  9287. 		}
  9288. 	}
  9289. }
  9290.  
  9291. void skill_brandishspear(struct block_list* src, struct block_list* bl, int skillid, int skilllv, unsigned int tick, int flag)
  9292. {
  9293. 	int c,n=4;
  9294. 	int dir = map_calc_dir(src,bl->x,bl->y);
  9295. 	struct square tc;
  9296. 	int x=bl->x,y=bl->y;
  9297. 	skill_brandishspear_first(&tc,dir,x,y);
  9298. 	skill_brandishspear_dir(&tc,dir,4);
  9299. 	skill_area_temp[1] = bl->id;
  9300.  
  9301. 	if(skilllv > 9){
  9302. 		for(c=1;c<4;c++){
  9303. 			map_foreachincell(skill_area_sub,
  9304. 				bl->m,tc.val1[c],tc.val2[c],BL_CHAR,
  9305. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|n,
  9306. 				skill_castend_damage_id);
  9307. 		}
  9308. 	}
  9309. 	if(skilllv > 6){
  9310. 		skill_brandishspear_dir(&tc,dir,-1);
  9311. 		n--;
  9312. 	}else{
  9313. 		skill_brandishspear_dir(&tc,dir,-2);
  9314. 		n-=2;
  9315. 	}
  9316.  
  9317. 	if(skilllv > 3){
  9318. 		for(c=0;c<5;c++){
  9319. 			map_foreachincell(skill_area_sub,
  9320. 				bl->m,tc.val1[c],tc.val2[c],BL_CHAR,
  9321. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|n,
  9322. 				skill_castend_damage_id);
  9323. 			if(skilllv > 6 && n==3 && c==4){
  9324. 				skill_brandishspear_dir(&tc,dir,-1);
  9325. 				n--;c=-1;
  9326. 			}
  9327. 		}
  9328. 	}
  9329. 	for(c=0;c<10;c++){
  9330. 		if(c==0||c==5) skill_brandishspear_dir(&tc,dir,-1);
  9331. 		map_foreachincell(skill_area_sub,
  9332. 			bl->m,tc.val1[c%5],tc.val2[c%5],BL_CHAR,
  9333. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  9334. 			skill_castend_damage_id);
  9335. 	}
  9336. }
  9337.  
  9338. /*==========================================
  9339.  * Weapon Repair [Celest/DracoRPG]
  9340.  *------------------------------------------*/
  9341. void skill_repairweapon (struct map_session_data *sd, int idx)
  9342. {
  9343. 	int material;
  9344. 	int materials[4] = { 1002, 998, 999, 756 };
  9345. 	struct item *item;
  9346. 	struct map_session_data *target_sd;
  9347.  
  9348. 	nullpo_retv(sd);
  9349. 	target_sd = map_id2sd(sd->menuskill_val);
  9350. 	if (!target_sd) //Failed....
  9351. 		return;
  9352. 	if(idx==0xFFFF) // No item selected ('Cancel' clicked)
  9353. 		return;
  9354. 	if(idx < 0 || idx >= MAX_INVENTORY)
  9355. 		return; //Invalid index??
  9356.  
  9357.    item = &target_sd->status.inventory[idx];
  9358. 	if(item->nameid <= 0 || item->attribute == 0)
  9359. 		return; //Again invalid item....
  9360.  
  9361. 	if(sd!=target_sd && !battle_check_range(&sd->bl,&target_sd->bl,skill_get_range2(&sd->bl, sd->menuskill_id,pc_checkskill(sd, sd->menuskill_id)))){
  9362. 		clif_item_repaireffect(sd,idx,1);
  9363. 		return;
  9364. 	}
  9365.  
  9366. 	if (itemdb_type(item->nameid)==IT_WEAPON)
  9367. 		material = materials [itemdb_wlv(item->nameid)-1]; // Lv1/2/3/4 weapons consume 1 Iron Ore/Iron/Steel/Rough Oridecon
  9368. 	else
  9369. 		material = materials [2]; // Armors consume 1 Steel
  9370. 	if (pc_search_inventory(sd,material) < 0 ) {
  9371. 		clif_skill_fail(sd,sd->menuskill_id,USESKILL_FAIL_LEVEL,0);
  9372. 		return;
  9373. 	}
  9374. 	clif_skill_nodamage(&sd->bl,&target_sd->bl,sd->menuskill_id,1,1);
  9375. 	item->attribute=0;
  9376. 	clif_equiplist(target_sd);
  9377. 	pc_delitem(sd,pc_search_inventory(sd,material),1,0,0);
  9378. 	clif_item_repaireffect(sd,idx,0);
  9379. 	if(sd!=target_sd)
  9380. 		clif_item_repaireffect(target_sd,idx,0);
  9381. }
  9382.  
  9383. /*==========================================
  9384.  * Item Appraisal
  9385.  *------------------------------------------*/
  9386. void skill_identify (struct map_session_data *sd, int idx)
  9387. {
  9388. 	int flag=1;
  9389.  
  9390. 	nullpo_retv(sd);
  9391.  
  9392. 	if(idx >= 0 && idx < MAX_INVENTORY) {
  9393. 		if(sd->status.inventory[idx].nameid > 0 && sd->status.inventory[idx].identify == 0 ){
  9394. 			flag=0;
  9395. 			sd->status.inventory[idx].identify=1;
  9396. 		}
  9397. 	}
  9398. 	clif_item_identified(sd,idx,flag);
  9399. }
  9400.  
  9401. /*==========================================
  9402.  * Weapon Refine [Celest]
  9403.  *------------------------------------------*/
  9404. void skill_weaponrefine (struct map_session_data *sd, int idx)
  9405. {
  9406. 	int i = 0, ep = 0, per;
  9407. 	int material[5] = { 0, 1010, 1011, 984, 984 };
  9408. 	struct item *item;
  9409.  
  9410. 	nullpo_retv(sd);
  9411.  
  9412. 	if (idx >= 0 && idx < MAX_INVENTORY)
  9413. 	{
  9414. 		struct item_data *ditem = sd->inventory_data[idx];
  9415. 		item = &sd->status.inventory[idx];
  9416.  
  9417. 		if(item->nameid > 0 && ditem->type == IT_WEAPON)
  9418. 		{
  9419. 			if( item->refine >= sd->menuskill_val
  9420. 			||  item->refine >= MAX_REFINE		// if it's no longer refineable
  9421. 			||  ditem->flag.no_refine 	// if the item isn't refinable
  9422. 			||  (i = pc_search_inventory(sd, material [ditem->wlv])) < 0 )
  9423. 			{
  9424. 				clif_skill_fail(sd,sd->menuskill_id,USESKILL_FAIL_LEVEL,0);
  9425. 				return;
  9426. 			}
  9427.  
  9428. 			per = percentrefinery [ditem->wlv][(int)item->refine];
  9429. 			per += (((signed int)sd->status.job_level)-50)/2; //Updated per the new kro descriptions. [Skotlex]
  9430.  
  9431. 			pc_delitem(sd, i, 1, 0, 0);
  9432. 			if (per > rand() % 100) {
  9433. 				item->refine++;
  9434. 				if(item->equip) {
  9435. 					ep = item->equip;
  9436. 					pc_unequipitem(sd,idx,3);
  9437. 				}
  9438. 				clif_refine(sd->fd,0,idx,item->refine);
  9439. 				clif_delitem(sd,idx,1,3);
  9440. 				clif_additem(sd,idx,1,0);
  9441. 				if (ep)
  9442. 					pc_equipitem(sd,idx,ep);
  9443. 				clif_misceffect(&sd->bl,3);
  9444. 				if(item->refine == MAX_REFINE &&
  9445. 					item->card[0] == CARD0_FORGE &&
  9446. 					(int)MakeDWord(item->card[2],item->card[3]) == sd->status.char_id)
  9447. 				{ // Fame point system [DracoRPG]
  9448. 					switch(ditem->wlv){
  9449. 						case 1:
  9450. 							pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  9451. 							break;
  9452. 						case 2:
  9453. 							pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  9454. 							break;
  9455. 						case 3:
  9456. 							pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  9457. 							break;
  9458. 					}
  9459. 				}
  9460. 			} else {
  9461. 				item->refine = 0;
  9462. 				if(item->equip)
  9463. 					pc_unequipitem(sd,idx,3);
  9464. 				clif_refine(sd->fd,1,idx,item->refine);
  9465. 				pc_delitem(sd,idx,1,0,2);
  9466. 				clif_misceffect(&sd->bl,2);
  9467. 				clif_emotion(&sd->bl, E_OMG);
  9468. 			}
  9469. 		}
  9470. 	}
  9471. }
  9472.  
  9473. /*==========================================
  9474.  *
  9475.  *------------------------------------------*/
  9476. int skill_autospell (struct map_session_data *sd, int skillid)
  9477. {
  9478. 	int skilllv;
  9479. 	int maxlv=1,lv;
  9480.  
  9481. 	nullpo_ret(sd);
  9482.  
  9483. 	skilllv = sd->menuskill_val;
  9484. 	lv=pc_checkskill(sd,skillid);
  9485.  
  9486. 	if(skilllv <= 0 || !lv) return 0; // Player must learn the skill before doing auto-spell [Lance]
  9487.  
  9488. 	if(skillid==MG_NAPALMBEAT)	maxlv=3;
  9489. 	else if(skillid==MG_COLDBOLT || skillid==MG_FIREBOLT || skillid==MG_LIGHTNINGBOLT){
  9490. 		if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SAGE)
  9491. 			maxlv =20; //Soul Linker bonus. [Skotlex]
  9492. 		else if(skilllv==2) maxlv=1;
  9493. 		else if(skilllv==3) maxlv=2;
  9494. 		else if(skilllv>=4) maxlv=3;
  9495. 	}
  9496. 	else if(skillid==MG_SOULSTRIKE){
  9497. 		if(skilllv==5) maxlv=1;
  9498. 		else if(skilllv==6) maxlv=2;
  9499. 		else if(skilllv>=7) maxlv=3;
  9500. 	}
  9501. 	else if(skillid==MG_FIREBALL){
  9502. 		if(skilllv==8) maxlv=1;
  9503. 		else if(skilllv>=9) maxlv=2;
  9504. 	}
  9505. 	else if(skillid==MG_FROSTDIVER) maxlv=1;
  9506. 	else return 0;
  9507.  
  9508. 	if(maxlv > lv)
  9509. 		maxlv = lv;
  9510.  
  9511. 	sc_start4(&sd->bl,SC_AUTOSPELL,100,skilllv,skillid,maxlv,0,
  9512. 		skill_get_time(SA_AUTOSPELL,skilllv));
  9513. 	return 0;
  9514. }
  9515.  
  9516. /*==========================================
  9517.  * Sitting skills functions.
  9518.  *------------------------------------------*/
  9519. static int skill_sit_count (struct block_list *bl, va_list ap)
  9520. {
  9521. 	struct map_session_data *sd;
  9522. 	int type =va_arg(ap,int);
  9523. 	sd=(struct map_session_data*)bl;
  9524.  
  9525. 	if(!pc_issit(sd))
  9526. 		return 0;
  9527.  
  9528. 	if(type&1 && pc_checkskill(sd,RG_GANGSTER) > 0)
  9529. 		return 1;
  9530.  
  9531. 	if(type&2 && (pc_checkskill(sd,TK_HPTIME) > 0 || pc_checkskill(sd,TK_SPTIME) > 0))
  9532. 		return 1;
  9533.  
  9534. 	return 0;
  9535. }
  9536.  
  9537. static int skill_sit_in (struct block_list *bl, va_list ap)
  9538. {
  9539. 	struct map_session_data *sd;
  9540. 	int type =va_arg(ap,int);
  9541.  
  9542. 	sd=(struct map_session_data*)bl;
  9543.  
  9544. 	if(!pc_issit(sd))
  9545. 		return 0;
  9546.  
  9547. 	if(type&1 && pc_checkskill(sd,RG_GANGSTER) > 0)
  9548. 		sd->state.gangsterparadise=1;
  9549.  
  9550. 	if(type&2 && (pc_checkskill(sd,TK_HPTIME) > 0 || pc_checkskill(sd,TK_SPTIME) > 0 ))
  9551. 	{
  9552. 		sd->state.rest=1;
  9553. 		status_calc_regen(bl, &sd->battle_status, &sd->regen);
  9554. 		status_calc_regen_rate(bl, &sd->regen, &sd->sc);
  9555. 	}
  9556.  
  9557. 	return 0;
  9558. }
  9559.  
  9560. static int skill_sit_out (struct block_list *bl, va_list ap)
  9561. {
  9562. 	struct map_session_data *sd;
  9563. 	int type =va_arg(ap,int);
  9564. 	sd=(struct map_session_data*)bl;
  9565. 	if(sd->state.gangsterparadise && type&1)
  9566. 		sd->state.gangsterparadise=0;
  9567. 	if(sd->state.rest && type&2) {
  9568. 		sd->state.rest=0;
  9569. 		status_calc_regen(bl, &sd->battle_status, &sd->regen);
  9570. 		status_calc_regen_rate(bl, &sd->regen, &sd->sc);
  9571. 	}
  9572. 	return 0;
  9573. }
  9574.  
  9575. int skill_sit (struct map_session_data *sd, int type)
  9576. {
  9577. 	int flag = 0;
  9578. 	int range = 0, lv;
  9579. 	nullpo_ret(sd);
  9580.  
  9581.  
  9582. 	if((lv = pc_checkskill(sd,RG_GANGSTER)) > 0) {
  9583. 		flag|=1;
  9584. 		range = skill_get_splash(RG_GANGSTER, lv);
  9585. 	}
  9586. 	if((lv = pc_checkskill(sd,TK_HPTIME)) > 0) {
  9587. 		flag|=2;
  9588. 		range = skill_get_splash(TK_HPTIME, lv);
  9589. 	}
  9590. 	else if ((lv = pc_checkskill(sd,TK_SPTIME)) > 0) {
  9591. 		flag|=2;
  9592. 		range = skill_get_splash(TK_SPTIME, lv);
  9593. 	}
  9594.  
  9595. 	if (!flag) return 0;
  9596.  
  9597. 	if(type) {
  9598. 		if (map_foreachinrange(skill_sit_count,&sd->bl, range, BL_PC, flag) > 1)
  9599. 			map_foreachinrange(skill_sit_in,&sd->bl, range, BL_PC, flag);
  9600. 	} else {
  9601. 		if (map_foreachinrange(skill_sit_count,&sd->bl, range, BL_PC, flag) < 2)
  9602. 			map_foreachinrange(skill_sit_out,&sd->bl, range, BL_PC, flag);
  9603. 	}
  9604. 	return 0;
  9605. }
  9606.  
  9607. /*==========================================
  9608.  *
  9609.  *------------------------------------------*/
  9610. int skill_frostjoke_scream (struct block_list *bl, va_list ap)
  9611. {
  9612. 	struct block_list *src;
  9613. 	int skillnum,skilllv;
  9614. 	unsigned int tick;
  9615.  
  9616. 	nullpo_ret(bl);
  9617. 	nullpo_ret(src=va_arg(ap,struct block_list*));
  9618.  
  9619. 	skillnum=va_arg(ap,int);
  9620. 	skilllv=va_arg(ap,int);
  9621. 	if(skilllv <= 0) return 0;
  9622. 	tick=va_arg(ap,unsigned int);
  9623.  
  9624. 	if (src == bl || status_isdead(bl))
  9625. 		return 0;
  9626. 	if (bl->type == BL_PC) {
  9627. 		struct map_session_data *sd = (struct map_session_data *)bl;
  9628. 		if (sd && sd->sc.option&OPTION_INVISIBLE)
  9629. 			return 0;
  9630. 	}
  9631. 	//It has been reported that Scream/Joke works the same regardless of woe-setting. [Skotlex]
  9632. 	if(battle_check_target(src,bl,BCT_ENEMY) > 0)
  9633. 		skill_additional_effect(src,bl,skillnum,skilllv,BF_MISC,ATK_DEF,tick);
  9634. 	else if(battle_check_target(src,bl,BCT_PARTY) > 0 && rand()%100 < 10)
  9635. 		skill_additional_effect(src,bl,skillnum,skilllv,BF_MISC,ATK_DEF,tick);
  9636.  
  9637. 	return 0;
  9638. }
  9639.  
  9640. /*==========================================
  9641.  *
  9642.  *------------------------------------------*/
  9643. static void skill_unitsetmapcell (struct skill_unit *src, int skill_num, int skill_lv, cell_t cell, bool flag)
  9644. {
  9645. 	int range = skill_get_unit_range(skill_num,skill_lv);
  9646. 	int x,y;
  9647.  
  9648. 	for( y = src->bl.y - range; y <= src->bl.y + range; ++y )
  9649. 		for( x = src->bl.x - range; x <= src->bl.x + range; ++x )
  9650. 			map_setcell(src->bl.m, x, y, cell, flag);
  9651. }
  9652.  
  9653. /*==========================================
  9654.  *
  9655.  *------------------------------------------*/
  9656. int skill_attack_area (struct block_list *bl, va_list ap)
  9657. {
  9658. 	struct block_list *src,*dsrc;
  9659. 	int atk_type,skillid,skilllv,flag,type;
  9660. 	unsigned int tick;
  9661.  
  9662. 	if(status_isdead(bl))
  9663. 		return 0;
  9664.  
  9665. 	atk_type = va_arg(ap,int);
  9666. 	src=va_arg(ap,struct block_list*);
  9667. 	dsrc=va_arg(ap,struct block_list*);
  9668. 	skillid=va_arg(ap,int);
  9669. 	skilllv=va_arg(ap,int);
  9670. 	tick=va_arg(ap,unsigned int);
  9671. 	flag=va_arg(ap,int);
  9672. 	type=va_arg(ap,int);
  9673.  
  9674.  
  9675. 	if (skill_area_temp[1] == bl->id) //This is the target of the skill, do a full attack and skip target checks.
  9676. 		return skill_attack(atk_type,src,dsrc,bl,skillid,skilllv,tick,flag);
  9677.  
  9678. 	if(battle_check_target(dsrc,bl,type) <= 0 ||
  9679. 		!status_check_skilluse(NULL, bl, skillid, 2))
  9680. 		return 0;
  9681.  
  9682.  
  9683. 	switch (skillid) {
  9684. 	case WZ_FROSTNOVA: //Skills that don't require the animation to be removed
  9685. 	case NPC_ACIDBREATH:
  9686. 	case NPC_DARKNESSBREATH:
  9687. 	case NPC_FIREBREATH:
  9688. 	case NPC_ICEBREATH:
  9689. 	case NPC_THUNDERBREATH:
  9690. 		return skill_attack(atk_type,src,dsrc,bl,skillid,skilllv,tick,flag);
  9691. 	default:
  9692. 		//Area-splash, disable skill animation.
  9693. 		return skill_attack(atk_type,src,dsrc,bl,skillid,skilllv,tick,flag|SD_ANIMATION);
  9694. 	}
  9695. }
  9696. /*==========================================
  9697.  *
  9698.  *------------------------------------------*/
  9699. int skill_clear_group (struct block_list *bl, int flag)
  9700. {
  9701. 	struct unit_data *ud = unit_bl2ud(bl);
  9702. 	struct skill_unit_group *group[MAX_SKILLUNITGROUP];
  9703. 	int i, count=0;
  9704.  
  9705. 	nullpo_ret(bl);
  9706. 	if (!ud) return 0;
  9707.  
  9708. 	//All groups to be deleted are first stored on an array since the array elements shift around when you delete them. [Skotlex]
  9709. 	for (i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i];i++)
  9710. 	{
  9711. 		switch (ud->skillunit[i]->skill_id) {
  9712. 			case SA_DELUGE:
  9713. 			case SA_VOLCANO:
  9714. 			case SA_VIOLENTGALE:
  9715. 			case SA_LANDPROTECTOR:
  9716. 			case NJ_SUITON:
  9717. 			case NJ_KAENSIN:
  9718. 				if (flag&1)
  9719. 					group[count++]= ud->skillunit[i];
  9720. 				break;
  9721. 			default:
  9722. 				if (flag&2 && skill_get_inf2(ud->skillunit[i]->skill_id)&INF2_TRAP)
  9723. 					group[count++]= ud->skillunit[i];
  9724. 				break;
  9725. 		}
  9726.  
  9727. 	}
  9728. 	for (i=0;i<count;i++)
  9729. 		skill_delunitgroup(group[i]);
  9730. 	return count;
  9731. }
  9732.  
  9733. /*==========================================
  9734.  * Returns the first element field found [Skotlex]
  9735.  *------------------------------------------*/
  9736. struct skill_unit_group *skill_locate_element_field(struct block_list *bl)
  9737. {
  9738. 	struct unit_data *ud = unit_bl2ud(bl);
  9739. 	int i;
  9740. 	nullpo_ret(bl);
  9741. 	if (!ud) return NULL;
  9742.  
  9743. 	for (i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i];i++) {
  9744. 		switch (ud->skillunit[i]->skill_id) {
  9745. 			case SA_DELUGE:
  9746. 			case SA_VOLCANO:
  9747. 			case SA_VIOLENTGALE:
  9748. 			case SA_LANDPROTECTOR:
  9749. 			case NJ_SUITON:
  9750. 				return ud->skillunit[i];
  9751. 		}
  9752. 	}
  9753. 	return NULL;
  9754. }
  9755.  
  9756. // for graffiti cleaner [Valaris]
  9757. int skill_graffitiremover (struct block_list *bl, va_list ap)
  9758. {
  9759. 	struct skill_unit *unit=NULL;
  9760.  
  9761. 	nullpo_ret(bl);
  9762. 	nullpo_ret(ap);
  9763.  
  9764. 	if(bl->type!=BL_SKILL || (unit=(struct skill_unit *)bl) == NULL)
  9765. 		return 0;
  9766.  
  9767. 	if((unit->group) && (unit->group->unit_id == UNT_GRAFFITI))
  9768. 		skill_delunit(unit);
  9769.  
  9770. 	return 0;
  9771. }
  9772.  
  9773. int skill_greed (struct block_list *bl, va_list ap)
  9774. {
  9775. 	struct block_list *src;
  9776. 	struct map_session_data *sd=NULL;
  9777. 	struct flooritem_data *fitem=NULL;
  9778.  
  9779. 	nullpo_ret(bl);
  9780. 	nullpo_ret(src = va_arg(ap, struct block_list *));
  9781.  
  9782. 	if(src->type == BL_PC && (sd=(struct map_session_data *)src) && bl->type==BL_ITEM && (fitem=(struct flooritem_data *)bl))
  9783. 		pc_takeitem(sd, fitem);
  9784.  
  9785. 	return 0;
  9786. }
  9787.  
  9788. /*==========================================
  9789.  *
  9790.  *------------------------------------------*/
  9791. static int skill_cell_overlap(struct block_list *bl, va_list ap)
  9792. {
  9793. 	int skillid;
  9794. 	int *alive;
  9795. 	struct skill_unit *unit;
  9796. 	struct block_list *src;
  9797.  
  9798. 	skillid = va_arg(ap,int);
  9799. 	alive = va_arg(ap,int *);
  9800. 	src = va_arg(ap,struct block_list *);
  9801. 	unit = (struct skill_unit *)bl;
  9802.  
  9803. 	if (unit == NULL || unit->group == NULL || (*alive) == 0)
  9804. 		return 0;
  9805.  
  9806. 	switch (skillid)
  9807. 	{
  9808. 		case SA_LANDPROTECTOR:
  9809. 			if( unit->group->skill_id == SA_LANDPROTECTOR )
  9810. 			{	//Check for offensive Land Protector to delete both. [Skotlex]
  9811. 				(*alive) = 0;
  9812. 				skill_delunit(unit);
  9813. 				return 1;
  9814. 			}
  9815. 			if( !(skill_get_inf2(unit->group->skill_id)&(INF2_SONG_DANCE|INF2_TRAP)) )
  9816. 			{	//It deletes everything except songs/dances and traps
  9817. 				skill_delunit(unit);
  9818. 				return 1;
  9819. 			}
  9820. 			break;
  9821. 		case HW_GANBANTEIN:
  9822. 			if( !(unit->group->state.song_dance&0x1) )
  9823. 			{// Don't touch song/dance.
  9824. 				skill_delunit(unit);
  9825. 				return 1;
  9826. 			}
  9827. 			break;
  9828. 		case SA_VOLCANO:
  9829. 		case SA_DELUGE:
  9830. 		case SA_VIOLENTGALE:
  9831. // The official implementation makes them fail to appear when casted on top of ANYTHING
  9832. // but I wonder if they didn't actually meant to fail when casted on top of each other?
  9833. // hence, I leave the alternate implementation here, commented. [Skotlex]
  9834. 			if (unit->range <= 0)
  9835. 			{
  9836. 				(*alive) = 0;
  9837. 				return 1;
  9838. 			}
  9839. /*
  9840. 			switch (unit->group->skill_id)
  9841. 			{	//These cannot override each other.
  9842. 				case SA_VOLCANO:
  9843. 				case SA_DELUGE:
  9844. 				case SA_VIOLENTGALE:
  9845. 					(*alive) = 0;
  9846. 					return 1;
  9847. 			}
  9848. */
  9849. 			break;
  9850. 		case PF_FOGWALL:
  9851. 			switch(unit->group->skill_id)
  9852. 			{
  9853. 				case SA_VOLCANO: //Can't be placed on top of these
  9854. 				case SA_VIOLENTGALE:
  9855. 					(*alive) = 0;
  9856. 					return 1;
  9857. 				case SA_DELUGE:
  9858. 				case NJ_SUITON:
  9859. 				//Cheap 'hack' to notify the calling function that duration should be doubled [Skotlex]
  9860. 					(*alive) = 2;
  9861. 					break;
  9862. 			}
  9863. 			break;
  9864. 		case HP_BASILICA:
  9865. 			if (unit->group->skill_id == HP_BASILICA)
  9866. 			{	//Basilica can't be placed on top of itself to avoid map-cell stacking problems. [Skotlex]
  9867. 				(*alive) = 0;
  9868. 				return 1;
  9869. 			}
  9870. 			break;
  9871. 	}
  9872.  
  9873. 	if (unit->group->skill_id == SA_LANDPROTECTOR &&
  9874. 		!(skill_get_inf2(skillid)&(INF2_SONG_DANCE|INF2_TRAP)))
  9875. 	{	//It deletes everything except songs/dances/traps
  9876. 		(*alive) = 0;
  9877. 		return 1;
  9878. 	}
  9879.  
  9880. 	return 0;
  9881. }
  9882.  
  9883. /*==========================================
  9884.  *
  9885.  *------------------------------------------*/
  9886. int skill_chastle_mob_changetarget(struct block_list *bl,va_list ap)
  9887. {
  9888. 	struct mob_data* md;
  9889. 	struct unit_data*ud = unit_bl2ud(bl);
  9890. 	struct block_list *from_bl;
  9891. 	struct block_list *to_bl;
  9892. 	md = (struct mob_data*)bl;
  9893. 	from_bl = va_arg(ap,struct block_list *);
  9894. 	to_bl = va_arg(ap,struct block_list *);
  9895.  
  9896. 	if(ud && ud->target == from_bl->id)
  9897. 		ud->target = to_bl->id;
  9898.  
  9899. 	if(md->bl.type == BL_MOB && md->target_id == from_bl->id)
  9900. 		md->target_id = to_bl->id;
  9901. 	return 0;
  9902. }
  9903.  
  9904. /*==========================================
  9905.  *
  9906.  *------------------------------------------*/
  9907. static int skill_trap_splash (struct block_list *bl, va_list ap)
  9908. {
  9909. 	struct block_list *src;
  9910. 	int tick;
  9911. 	struct skill_unit *unit;
  9912. 	struct skill_unit_group *sg;
  9913. 	struct block_list *ss;
  9914. 	src = va_arg(ap,struct block_list *);
  9915. 	unit = (struct skill_unit *)src;
  9916. 	tick = va_arg(ap,int);
  9917.  
  9918. 	if( !unit->alive || bl->prev == NULL )
  9919. 		return 0;
  9920.  
  9921. 	nullpo_ret(sg = unit->group);
  9922. 	nullpo_ret(ss = map_id2bl(sg->src_id));
  9923.  
  9924. 	if(battle_check_target(src,bl,BCT_ENEMY) <= 0)
  9925. 		return 0;
  9926.  
  9927. 	switch(sg->unit_id){
  9928. 		case UNT_SHOCKWAVE:
  9929. 		case UNT_SANDMAN:
  9930. 		case UNT_FLASHER:
  9931. 			skill_additional_effect(ss,bl,sg->skill_id,sg->skill_lv,BF_MISC,ATK_DEF,tick);
  9932. 			break;
  9933. 		case UNT_GROUNDDRIFT_WIND:
  9934. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9935. 				sc_start(bl,SC_STUN,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9936. 			break;
  9937. 		case UNT_GROUNDDRIFT_DARK:
  9938. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9939. 				sc_start(bl,SC_BLIND,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9940. 			break;
  9941. 		case UNT_GROUNDDRIFT_POISON:
  9942. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9943. 				sc_start(bl,SC_POISON,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9944. 			break;
  9945. 		case UNT_GROUNDDRIFT_WATER:
  9946. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9947. 				sc_start(bl,SC_FREEZE,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9948. 			break;
  9949. 		case UNT_GROUNDDRIFT_FIRE:
  9950. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9951. 				skill_blown(src,bl,skill_get_blewcount(sg->skill_id,sg->skill_lv),-1,0);
  9952. 			break;
  9953. 		default:
  9954. 			skill_attack(skill_get_type(sg->skill_id),ss,src,bl,sg->skill_id,sg->skill_lv,tick,0);
  9955. 			break;
  9956. 	}
  9957. 	return 1;
  9958. }
  9959.  
  9960. /*==========================================
  9961.  *
  9962.  *------------------------------------------*/
  9963. int skill_enchant_elemental_end (struct block_list *bl, int type)
  9964. {
  9965. 	struct status_change *sc;
  9966. 	const enum sc_type scs[] = { SC_ENCPOISON, SC_ASPERSIO, SC_FIREWEAPON, SC_WATERWEAPON, SC_WINDWEAPON, SC_EARTHWEAPON, SC_SHADOWWEAPON, SC_GHOSTWEAPON, SC_ENCHANTARMS };
  9967. 	int i;
  9968. 	nullpo_ret(bl);
  9969. 	nullpo_ret(sc= status_get_sc(bl));
  9970.  
  9971. 	if (!sc->count) return 0;
  9972.  
  9973. 	for (i = 0; i < ARRAYLENGTH(scs); i++)
  9974. 		if (type != scs[i] && sc->data[scs[i]])
  9975. 			status_change_end(bl, scs[i], INVALID_TIMER);
  9976.  
  9977. 	return 0;
  9978. }
  9979.  
  9980. bool skill_check_cloaking(struct block_list *bl, struct status_change_entry *sce)
  9981. {
  9982. 	static int dx[] = { 0, 1, 0, -1, -1,  1, 1, -1};
  9983. 	static int dy[] = {-1, 0, 1,  0, -1, -1, 1,  1};
  9984. 	bool wall = true;
  9985. 	int i;
  9986.  
  9987. 	if( (bl->type == BL_PC && battle_config.pc_cloak_check_type&1)
  9988. 	||	(bl->type != BL_PC && battle_config.monster_cloak_check_type&1) )
  9989. 	{	//Check for walls.
  9990. 		ARR_FIND( 0, 8, i, map_getcell(bl->m, bl->x+dx[i], bl->y+dy[i], CELL_CHKNOPASS) != 0 );
  9991. 		if( i == 8 )
  9992. 			wall = false;
  9993. 	}
  9994.  
  9995. 	if( sce )
  9996. 	{
  9997. 		if( !wall )
  9998. 		{
  9999. 			if( sce->val1 < 3 ) //End cloaking.
  10000. 				status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  10001. 			else
  10002. 			if( sce->val4&1 )
  10003. 			{	//Remove wall bonus
  10004. 				sce->val4&=~1;
  10005. 				status_calc_bl(bl,SCB_SPEED);
  10006. 			}
  10007. 		}
  10008. 		else
  10009. 		{
  10010. 			if( !(sce->val4&1) )
  10011. 			{	//Add wall speed bonus
  10012. 				sce->val4|=1;
  10013. 				status_calc_bl(bl,SCB_SPEED);
  10014. 			}
  10015. 		}
  10016. 	}
  10017.  
  10018. 	return wall;
  10019. }
  10020.  
  10021. /*==========================================
  10022.  *
  10023.  *------------------------------------------*/
  10024. struct skill_unit *skill_initunit (struct skill_unit_group *group, int idx, int x, int y, int val1, int val2)
  10025. {
  10026. 	struct skill_unit *unit;
  10027.  
  10028. 	nullpo_retr(NULL, group);
  10029. 	nullpo_retr(NULL, group->unit); // crash-protection against poor coding
  10030. 	nullpo_retr(NULL, unit=&group->unit[idx]);
  10031.  
  10032. 	if(!unit->alive)
  10033. 		group->alive_count++;
  10034.  
  10035. 	unit->bl.id=map_get_new_object_id();
  10036. 	unit->bl.type=BL_SKILL;
  10037. 	unit->bl.m=group->map;
  10038. 	unit->bl.x=x;
  10039. 	unit->bl.y=y;
  10040. 	unit->group=group;
  10041. 	unit->alive=1;
  10042. 	unit->val1=val1;
  10043. 	unit->val2=val2;
  10044.  
  10045. 	idb_put(skillunit_db, unit->bl.id, unit);
  10046. 	map_addiddb(&unit->bl);
  10047. 	map_addblock(&unit->bl);
  10048.  
  10049. 	// perform oninit actions
  10050. 	switch (group->skill_id) {
  10051. 	case WZ_ICEWALL:
  10052. 		map_setgatcell(unit->bl.m,unit->bl.x,unit->bl.y,5);
  10053. 		clif_changemapcell(0,unit->bl.m,unit->bl.x,unit->bl.y,5,AREA);
  10054. 		break;
  10055. 	case SA_LANDPROTECTOR:
  10056. 		skill_unitsetmapcell(unit,SA_LANDPROTECTOR,group->skill_lv,CELL_LANDPROTECTOR,true);
  10057. 		break;
  10058. 	case HP_BASILICA:
  10059. 		skill_unitsetmapcell(unit,HP_BASILICA,group->skill_lv,CELL_BASILICA,true);
  10060. 		break;
  10061. 	default:
  10062. 		if (group->state.song_dance&0x1) //Check for dissonance.
  10063. 			skill_dance_overlap(unit, 1);
  10064. 		break;
  10065. 	}
  10066.  
  10067. 	clif_skill_setunit(unit);
  10068.  
  10069. 	return unit;
  10070. }
  10071.  
  10072. /*==========================================
  10073.  *
  10074.  *------------------------------------------*/
  10075. int skill_delunit (struct skill_unit* unit)
  10076. {
  10077. 	struct skill_unit_group *group;
  10078.  
  10079. 	nullpo_ret(unit);
  10080. 	if( !unit->alive )
  10081. 		return 0;
  10082. 	unit->alive=0;
  10083.  
  10084. 	nullpo_ret(group=unit->group);
  10085.  
  10086. 	if( group->state.song_dance&0x1 ) //Cancel dissonance effect.
  10087. 		skill_dance_overlap(unit, 0);
  10088.  
  10089. 	// invoke onout event
  10090. 	if( !unit->range )
  10091. 		map_foreachincell(skill_unit_effect,unit->bl.m,unit->bl.x,unit->bl.y,group->bl_flag,&unit->bl,gettick(),4);
  10092.  
  10093. 	// perform ondelete actions
  10094. 	switch (group->skill_id) {
  10095. 	case HT_ANKLESNARE:
  10096. 		{
  10097. 		struct block_list* target = map_id2bl(group->val2);
  10098. 		if( target )
  10099. 			status_change_end(target, SC_ANKLE, INVALID_TIMER);
  10100. 		}
  10101. 		break;
  10102. 	case WZ_ICEWALL:
  10103. 		map_setgatcell(unit->bl.m,unit->bl.x,unit->bl.y,unit->val2);
  10104. 		clif_changemapcell(0,unit->bl.m,unit->bl.x,unit->bl.y,unit->val2,ALL_SAMEMAP); // hack to avoid clientside cell bug
  10105. 		break;
  10106. 	case SA_LANDPROTECTOR:
  10107. 		skill_unitsetmapcell(unit,SA_LANDPROTECTOR,group->skill_lv,CELL_LANDPROTECTOR,false);
  10108. 		break;
  10109. 	case HP_BASILICA:
  10110. 		skill_unitsetmapcell(unit,HP_BASILICA,group->skill_lv,CELL_BASILICA,false);
  10111. 		break;
  10112. 	}
  10113.  
  10114. 	clif_skill_delunit(unit);
  10115.  
  10116. 	unit->group=NULL;
  10117. 	map_delblock(&unit->bl); // don't free yet
  10118. 	map_deliddb(&unit->bl);
  10119. 	idb_remove(skillunit_db, unit->bl.id);
  10120. 	if(--group->alive_count==0)
  10121. 		skill_delunitgroup(group);
  10122.  
  10123. 	return 0;
  10124. }
  10125. /*==========================================
  10126.  *
  10127.  *------------------------------------------*/
  10128. static DBMap* group_db = NULL;// int group_id -> struct skill_unit_group*
  10129.  
  10130. /// Returns the target skill_unit_group or NULL if not found.
  10131. struct skill_unit_group* skill_id2group(int group_id)
  10132. {
  10133. 	return (struct skill_unit_group*)idb_get(group_db, group_id);
  10134. }
  10135.  
  10136.  
  10137. static int skill_unit_group_newid = MAX_SKILL_DB;
  10138.  
  10139. /// Returns a new group_id that isn't being used in group_db.
  10140. /// Fatal error if nothing is available.
  10141. static int skill_get_new_group_id(void)
  10142. {
  10143. 	if( skill_unit_group_newid >= MAX_SKILL_DB && skill_id2group(skill_unit_group_newid) == NULL )
  10144. 		return skill_unit_group_newid++;// available
  10145. 	{// find next id
  10146. 		int base_id = skill_unit_group_newid;
  10147. 		while( base_id != ++skill_unit_group_newid )
  10148. 		{
  10149. 			if( skill_unit_group_newid < MAX_SKILL_DB )
  10150. 				skill_unit_group_newid = MAX_SKILL_DB;
  10151. 			if( skill_id2group(skill_unit_group_newid) == NULL )
  10152. 				return skill_unit_group_newid++;// available
  10153. 		}
  10154. 		// full loop, nothing available
  10155. 		ShowFatalError("skill_get_new_group_id: All ids are taken. Exiting...");
  10156. 		exit(1);
  10157. 	}
  10158. }
  10159.  
  10160. struct skill_unit_group* skill_initunitgroup (struct block_list* src, int count, short skillid, short skilllv, int unit_id, int limit, int interval)
  10161. {
  10162. 	struct unit_data* ud = unit_bl2ud( src );
  10163. 	struct skill_unit_group* group;
  10164. 	int i;
  10165.  
  10166. 	if(skillid <= 0 || skilllv <= 0) return 0;
  10167.  
  10168. 	nullpo_retr(NULL, src);
  10169. 	nullpo_retr(NULL, ud);
  10170.  
  10171. 	// find a free spot to store the new unit group
  10172. 	ARR_FIND( 0, MAX_SKILLUNITGROUP, i, ud->skillunit[i] == NULL );
  10173. 	if(i == MAX_SKILLUNITGROUP)
  10174. 	{
  10175. 		// array is full, make room by discarding oldest group
  10176. 		int j=0;
  10177. 		unsigned maxdiff=0,x,tick=gettick();
  10178. 		for(i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i];i++)
  10179. 			if((x=DIFF_TICK(tick,ud->skillunit[i]->tick))>maxdiff){
  10180. 				maxdiff=x;
  10181. 				j=i;
  10182. 			}
  10183. 		skill_delunitgroup(ud->skillunit[j]);
  10184. 		//Since elements must have shifted, we use the last slot.
  10185. 		i = MAX_SKILLUNITGROUP-1;
  10186. 	}
  10187.  
  10188. 	group             = ers_alloc(skill_unit_ers, struct skill_unit_group);
  10189. 	group->src_id     = src->id;
  10190. 	group->party_id   = status_get_party_id(src);
  10191. 	group->guild_id   = status_get_guild_id(src);
  10192. 	group->bg_id      = bg_team_get_id(src);
  10193. 	group->group_id   = skill_get_new_group_id();
  10194. 	group->unit       = (struct skill_unit *)aCalloc(count,sizeof(struct skill_unit));
  10195. 	group->unit_count = count;
  10196. 	group->alive_count = 0;
  10197. 	group->val1       = 0;
  10198. 	group->val2       = 0;
  10199. 	group->val3       = 0;
  10200. 	group->skill_id   = skillid;
  10201. 	group->skill_lv   = skilllv;
  10202. 	group->unit_id    = unit_id;
  10203. 	group->map        = src->m;
  10204. 	group->limit      = limit;
  10205. 	group->interval   = interval;
  10206. 	group->tick       = gettick();
  10207. 	group->valstr     = NULL;
  10208.  
  10209. 	ud->skillunit[i] = group;
  10210.  
  10211. 	if (skillid == PR_SANCTUARY) //Sanctuary starts healing +1500ms after casted. [Skotlex]
  10212. 		group->tick += 1500;
  10213.  
  10214. 	idb_put(group_db, group->group_id, group);
  10215. 	return group;
  10216. }
  10217.  
  10218. /*==========================================
  10219.  *
  10220.  *------------------------------------------*/
  10221. int skill_delunitgroup_(struct skill_unit_group *group, const char* file, int line, const char* func)
  10222. {
  10223. 	struct block_list* src;
  10224. 	struct unit_data *ud;
  10225. 	int i,j;
  10226.  
  10227. 	if( group == NULL )
  10228. 	{
  10229. 		ShowDebug("skill_delunitgroup: group is NULL (source=%s:%d, %s)! Please report this! (#3504)\n", file, line, func);
  10230. 		return 0;
  10231. 	}
  10232.  
  10233. 	src=map_id2bl(group->src_id);
  10234. 	ud = unit_bl2ud(src);
  10235. 	if(!src || !ud) {
  10236. 		ShowError("skill_delunitgroup: Group's source not found! (src_id: %d skill_id: %d)\n", group->src_id, group->skill_id);
  10237. 		return 0;
  10238. 	}
  10239. 	if (skill_get_unit_flag(group->skill_id)&(UF_DANCE|UF_SONG|UF_ENSEMBLE))
  10240. 	{
  10241. 		struct status_change* sc = status_get_sc(src);
  10242. 		if (sc && sc->data[SC_DANCING])
  10243. 		{
  10244. 			sc->data[SC_DANCING]->val2 = 0 ; //This prevents status_change_end attempting to redelete the group. [Skotlex]
  10245. 			status_change_end(src, SC_DANCING, INVALID_TIMER);
  10246. 		}
  10247. 	}
  10248.  
  10249. 	// end Gospel's status change on 'src'
  10250. 	// (needs to be done when the group is deleted by other means than skill deactivation)
  10251. 	if (group->unit_id == UNT_GOSPEL) {
  10252. 		struct status_change *sc = status_get_sc(src);
  10253. 		if(sc && sc->data[SC_GOSPEL]) {
  10254. 			sc->data[SC_GOSPEL]->val3 = 0; //Remove reference to this group. [Skotlex]
  10255. 			status_change_end(src, SC_GOSPEL, INVALID_TIMER);
  10256. 		}
  10257. 	}
  10258.  
  10259. 	if (group->skill_id == SG_SUN_WARM ||
  10260. 		group->skill_id == SG_MOON_WARM ||
  10261. 		group->skill_id == SG_STAR_WARM) {
  10262. 		struct status_change *sc = status_get_sc(src);
  10263. 		if(sc && sc->data[SC_WARM]) {
  10264. 			sc->data[SC_WARM]->val4 = 0;
  10265. 			status_change_end(src, SC_WARM, INVALID_TIMER);
  10266. 		}
  10267. 	}
  10268.  
  10269. 	if (src->type==BL_PC && group->state.ammo_consume)
  10270. 		battle_consume_ammo((TBL_PC*)src, group->skill_id, group->skill_lv);
  10271.  
  10272. 	group->alive_count=0;
  10273.  
  10274. 	// remove all unit cells
  10275. 	if(group->unit != NULL)
  10276. 		for( i = 0; i < group->unit_count; i++ )
  10277. 			skill_delunit(&group->unit[i]);
  10278.  
  10279. 	// clear Talkie-box string
  10280. 	if( group->valstr != NULL )
  10281. 	{
  10282. 		aFree(group->valstr);
  10283. 		group->valstr = NULL;
  10284. 	}
  10285.  
  10286. 	idb_remove(group_db, group->group_id);
  10287. 	map_freeblock(&group->unit->bl); // schedules deallocation of whole array (HACK)
  10288. 	group->unit=NULL;
  10289. 	group->group_id=0;
  10290. 	group->unit_count=0;
  10291.  
  10292. 	// locate this group, swap with the last entry and delete it
  10293. 	ARR_FIND( 0, MAX_SKILLUNITGROUP, i, ud->skillunit[i] == group );
  10294. 	ARR_FIND( i, MAX_SKILLUNITGROUP, j, ud->skillunit[j] == NULL ); j--;
  10295. 	if( i < MAX_SKILLUNITGROUP )
  10296. 	{
  10297. 		ud->skillunit[i] = ud->skillunit[j];
  10298. 		ud->skillunit[j] = NULL;
  10299. 		ers_free(skill_unit_ers, group);
  10300. 	}
  10301. 	else
  10302. 		ShowError("skill_delunitgroup: Group not found! (src_id: %d skill_id: %d)\n", group->src_id, group->skill_id);
  10303.  
  10304. 	return 1;
  10305. }
  10306.  
  10307. /*==========================================
  10308.  *
  10309.  *------------------------------------------*/
  10310. int skill_clear_unitgroup (struct block_list *src)
  10311. {
  10312. 	struct unit_data *ud = unit_bl2ud(src);
  10313.  
  10314. 	nullpo_ret(ud);
  10315.  
  10316. 	while (ud->skillunit[0])
  10317. 		skill_delunitgroup(ud->skillunit[0]);
  10318.  
  10319. 	return 1;
  10320. }
  10321.  
  10322. /*==========================================
  10323.  *
  10324.  *------------------------------------------*/
  10325. struct skill_unit_group_tickset *skill_unitgrouptickset_search (struct block_list *bl, struct skill_unit_group *group, int tick)
  10326. {
  10327. 	int i,j=-1,k,s,id;
  10328. 	struct unit_data *ud;
  10329. 	struct skill_unit_group_tickset *set;
  10330.  
  10331. 	nullpo_ret(bl);
  10332. 	if (group->interval==-1)
  10333. 		return NULL;
  10334.  
  10335. 	ud = unit_bl2ud(bl);
  10336. 	if (!ud) return NULL;
  10337.  
  10338. 	set = ud->skillunittick;
  10339.  
  10340. 	if (skill_get_unit_flag(group->skill_id)&UF_NOOVERLAP)
  10341. 		id = s = group->skill_id;
  10342. 	else
  10343. 		id = s = group->group_id;
  10344.  
  10345. 	for (i=0; i<MAX_SKILLUNITGROUPTICKSET; i++) {
  10346. 		k = (i+s) % MAX_SKILLUNITGROUPTICKSET;
  10347. 		if (set[k].id == id)
  10348. 			return &set[k];
  10349. 		else if (j==-1 && (DIFF_TICK(tick,set[k].tick)>0 || set[k].id==0))
  10350. 			j=k;
  10351. 	}
  10352.  
  10353. 	if (j == -1) {
  10354. 		ShowWarning ("skill_unitgrouptickset_search: tickset is full\n");
  10355. 		j = id % MAX_SKILLUNITGROUPTICKSET;
  10356. 	}
  10357.  
  10358. 	set[j].id = id;
  10359. 	set[j].tick = tick;
  10360. 	return &set[j];
  10361. }
  10362.  
  10363. /*==========================================
  10364.  *
  10365.  *------------------------------------------*/
  10366. int skill_unit_timer_sub_onplace (struct block_list* bl, va_list ap)
  10367. {
  10368. 	struct skill_unit* unit = va_arg(ap,struct skill_unit *);
  10369. 	struct skill_unit_group* group = unit->group;
  10370. 	unsigned int tick = va_arg(ap,unsigned int);
  10371.  
  10372. 	if( !unit->alive || bl->prev == NULL )
  10373. 		return 0;
  10374.  
  10375. 	nullpo_ret(group);
  10376.  
  10377. 	if( !(skill_get_inf2(group->skill_id)&(INF2_SONG_DANCE|INF2_TRAP)) && map_getcell(bl->m, bl->x, bl->y, CELL_CHKLANDPROTECTOR) )
  10378. 		return 0; //AoE skills are ineffective. [Skotlex]
  10379.  
  10380. 	if( battle_check_target(&unit->bl,bl,group->target_flag) <= 0 )
  10381. 		return 0;
  10382.  
  10383. 	skill_unit_onplace_timer(unit,bl,tick);
  10384.  
  10385. 	return 1;
  10386. }
  10387.  
  10388. /*==========================================
  10389.  *
  10390.  *------------------------------------------*/
  10391. static int skill_unit_timer_sub (DBKey key, void* data, va_list ap)
  10392. {
  10393. 	struct skill_unit* unit = (struct skill_unit*)data;
  10394. 	struct skill_unit_group* group = unit->group;
  10395. 	unsigned int tick = va_arg(ap,unsigned int);
  10396.   	bool dissonance;
  10397. 	struct block_list* bl = &unit->bl;
  10398.  
  10399. 	if( !unit->alive )
  10400. 		return 0;
  10401.  
  10402. 	nullpo_ret(group);
  10403.  
  10404. 	// check for expiration
  10405. 	if( (DIFF_TICK(tick,group->tick) >= group->limit || DIFF_TICK(tick,group->tick) >= unit->limit) )
  10406. 	{// skill unit expired (inlined from skill_unit_onlimit())
  10407. 		switch( group->unit_id )
  10408. 		{
  10409. 			case UNT_BLASTMINE:
  10410. 			case UNT_GROUNDDRIFT_WIND:
  10411. 			case UNT_GROUNDDRIFT_DARK:
  10412. 			case UNT_GROUNDDRIFT_POISON:
  10413. 			case UNT_GROUNDDRIFT_WATER:
  10414. 			case UNT_GROUNDDRIFT_FIRE:
  10415. 				group->unit_id = UNT_USED_TRAPS;
  10416. 				//clif_changetraplook(bl, UNT_FIREPILLAR_ACTIVE);
  10417. 				group->limit=DIFF_TICK(tick+1500,group->tick);
  10418. 				unit->limit=DIFF_TICK(tick+1500,group->tick);
  10419. 			break;
  10420.  
  10421. 			case UNT_ANKLESNARE:
  10422. 				if( group->val2 > 0 ) {
  10423. 					// Used Trap don't returns back to item
  10424. 					skill_delunit(unit);
  10425. 					break;
  10426. 				}
  10427. 			case UNT_SKIDTRAP:
  10428. 			case UNT_LANDMINE:
  10429. 			case UNT_SHOCKWAVE:
  10430. 			case UNT_SANDMAN:
  10431. 			case UNT_FLASHER:
  10432. 			case UNT_FREEZINGTRAP:
  10433. 			case UNT_CLAYMORETRAP:
  10434. 			case UNT_TALKIEBOX:
  10435. 			{
  10436. 				struct block_list* src;
  10437. 				if( unit->val1 > 0 && (src = map_id2bl(group->src_id)) != NULL && src->type == BL_PC )
  10438. 				{ // revert unit back into a trap
  10439. 					struct item item_tmp;
  10440. 					memset(&item_tmp,0,sizeof(item_tmp));
  10441. 					item_tmp.nameid = ITEMID_TRAP;
  10442. 					item_tmp.identify = 1;
  10443. 					map_addflooritem(&item_tmp,1,bl->m,bl->x,bl->y,0,0,0,0);
  10444. 				}
  10445. 				skill_delunit(unit);
  10446. 			}
  10447. 			break;
  10448.  
  10449. 			case UNT_WARP_ACTIVE:
  10450. 				// warp portal opens (morph to a UNT_WARP_WAITING cell)
  10451. 				group->unit_id = skill_get_unit_id(group->skill_id, 1); // UNT_WARP_WAITING
  10452. 				clif_changelook(&unit->bl, LOOK_BASE, group->unit_id);
  10453. 				// restart timers
  10454. 				group->limit = skill_get_time(group->skill_id,group->skill_lv);
  10455. 				unit->limit = skill_get_time(group->skill_id,group->skill_lv);
  10456. 				// apply effect to all units standing on it
  10457. 				map_foreachincell(skill_unit_effect,unit->bl.m,unit->bl.x,unit->bl.y,group->bl_flag,&unit->bl,gettick(),1);
  10458. 			break;
  10459.  
  10460. 			case UNT_CALLFAMILY:
  10461. 			{
  10462. 				struct map_session_data *sd = NULL;
  10463. 				if(group->val1) {
  10464. 		  			sd = map_charid2sd(group->val1);
  10465. 					group->val1 = 0;
  10466. 					if (sd && !map[sd->bl.m].flag.nowarp)
  10467. 						pc_setpos(sd,map_id2index(unit->bl.m),unit->bl.x,unit->bl.y,CLR_TELEPORT);
  10468. 				}
  10469. 				if(group->val2) {
  10470. 					sd = map_charid2sd(group->val2);
  10471. 					group->val2 = 0;
  10472. 					if (sd && !map[sd->bl.m].flag.nowarp)
  10473. 						pc_setpos(sd,map_id2index(unit->bl.m),unit->bl.x,unit->bl.y,CLR_TELEPORT);
  10474. 				}
  10475. 				skill_delunit(unit);
  10476. 			}
  10477. 			break;
  10478.  
  10479. 			default:
  10480. 				skill_delunit(unit);
  10481. 		}
  10482. 	}
  10483. 	else
  10484. 	{// skill unit is still active
  10485. 		switch( group->unit_id )
  10486. 		{
  10487. 			case UNT_ICEWALL:
  10488. 				// icewall loses 50 hp every second
  10489. 				unit->val1 -= SKILLUNITTIMER_INTERVAL/20; // trap's hp
  10490. 				if( unit->val1 <= 0 && unit->limit + group->tick > tick + 700 )
  10491. 					unit->limit = DIFF_TICK(tick+700,group->tick);
  10492. 				break;
  10493. 			case UNT_SKIDTRAP:
  10494. 			case UNT_LANDMINE:
  10495. 			case UNT_SHOCKWAVE:
  10496. 			case UNT_SANDMAN:
  10497. 			case UNT_FLASHER:
  10498. 			case UNT_FREEZINGTRAP:
  10499. 			case UNT_TALKIEBOX:
  10500. 			case UNT_ANKLESNARE:
  10501. 				if( unit->val1 <= 0 ) {
  10502. 					if( group->unit_id == UNT_ANKLESNARE && group->val2 > 0 )
  10503. 						skill_delunit(unit);
  10504. 					else {
  10505. 						group->unit_id = UNT_USED_TRAPS;
  10506. 						group->limit = DIFF_TICK(tick, group->tick) + 1500;
  10507. 					}
  10508. 				}
  10509. 				break;
  10510. 		}
  10511. 	}
  10512.  
  10513. 	//Don't continue if unit or even group is expired and has been deleted.
  10514. 	if( !group || !unit->alive )
  10515. 		return 0;
  10516.  
  10517. 	dissonance = skill_dance_switch(unit, 0);
  10518.  
  10519. 	if( unit->range >= 0 && group->interval != -1 )
  10520. 	{
  10521. 		if( battle_config.skill_wall_check )
  10522. 			map_foreachinshootrange(skill_unit_timer_sub_onplace, bl, unit->range, group->bl_flag, bl,tick);
  10523. 		else
  10524. 			map_foreachinrange(skill_unit_timer_sub_onplace, bl, unit->range, group->bl_flag, bl,tick);
  10525.  
  10526. 		if(unit->range == -1) //Unit disabled, but it should not be deleted yet.
  10527. 			group->unit_id = UNT_USED_TRAPS;
  10528.  
  10529. 		if( group->unit_id == UNT_TATAMIGAESHI )
  10530. 		{
  10531. 			unit->range = -1; //Disable processed cell.
  10532. 			if (--group->val1 <= 0) // number of live cells
  10533. 	  		{	//All tiles were processed, disable skill.
  10534. 				group->target_flag=BCT_NOONE;
  10535. 				group->bl_flag= BL_NUL;
  10536. 			}
  10537. 		}
  10538. 	}
  10539.  
  10540.   	if( dissonance ) skill_dance_switch(unit, 1);
  10541.  
  10542. 	return 0;
  10543. }
  10544. /*==========================================
  10545.  * Executes on all skill units every SKILLUNITTIMER_INTERVAL miliseconds.
  10546.  *------------------------------------------*/
  10547. int skill_unit_timer(int tid, unsigned int tick, int id, intptr_t data)
  10548. {
  10549. 	map_freeblock_lock();
  10550.  
  10551. 	skillunit_db->foreach(skillunit_db, skill_unit_timer_sub, tick);
  10552.  
  10553. 	map_freeblock_unlock();
  10554.  
  10555. 	return 0;
  10556. }
  10557.  
  10558. static int skill_unit_temp[20];  // temporary storage for tracking skill unit skill ids as players move in/out of them
  10559. /*==========================================
  10560.  *
  10561.  *------------------------------------------*/
  10562. int skill_unit_move_sub (struct block_list* bl, va_list ap)
  10563. {
  10564. 	struct skill_unit* unit = (struct skill_unit *)bl;
  10565. 	struct skill_unit_group* group = unit->group;
  10566.  
  10567. 	struct block_list* target = va_arg(ap,struct block_list*);
  10568. 	unsigned int tick = va_arg(ap,unsigned int);
  10569. 	int flag = va_arg(ap,int);
  10570.  
  10571. 	bool dissonance;
  10572. 	int skill_id;
  10573. 	int i;
  10574.  
  10575. 	nullpo_ret(group);
  10576.  
  10577. 	if( !unit->alive || target->prev == NULL )
  10578. 		return 0;
  10579.  
  10580. 	if( unit->group->skill_id == PF_SPIDERWEB && flag&1 )
  10581. 		return 0; // Fiberlock is never supposed to trigger on skill_unit_move. [Inkfish]
  10582.  
  10583. 	dissonance = skill_dance_switch(unit, 0);
  10584.  
  10585. 	//Necessary in case the group is deleted after calling on_place/on_out [Skotlex]
  10586. 	skill_id = unit->group->skill_id;
  10587.  
  10588. 	if( unit->group->interval != -1 && !(skill_get_unit_flag(skill_id)&UF_DUALMODE) )
  10589. 	{	//Non-dualmode unit skills with a timer don't trigger when walking, so just return
  10590. 		if( dissonance ) skill_dance_switch(unit, 1);
  10591. 		return 0;
  10592. 	}
  10593.  
  10594. 	//Target-type check.
  10595. 	if( !(group->bl_flag&target->type && battle_check_target(&unit->bl,target,group->target_flag) > 0) )
  10596. 	{
  10597. 		if( group->src_id == target->id && group->state.song_dance&0x2 )
  10598. 		{	//Ensemble check to see if they went out/in of the area [Skotlex]
  10599. 			if( flag&1 )
  10600. 			{
  10601. 				if( flag&2 )
  10602. 				{	//Clear this skill id.
  10603. 					ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == skill_id );
  10604. 					if( i < ARRAYLENGTH(skill_unit_temp) )
  10605. 						skill_unit_temp[i] = 0;
  10606. 				}
  10607. 			}
  10608. 			else
  10609. 			{
  10610. 				if( flag&2 )
  10611. 				{	//Store this skill id.
  10612. 					ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == 0 );
  10613. 					if( i < ARRAYLENGTH(skill_unit_temp) )
  10614. 						skill_unit_temp[i] = skill_id;
  10615. 					else
  10616. 						ShowError("skill_unit_move_sub: Reached limit of unit objects per cell!\n");
  10617. 				}
  10618.  
  10619. 			}
  10620.  
  10621. 			if( flag&4 )
  10622. 				skill_unit_onleft(skill_id,target,tick);
  10623. 		}
  10624.  
  10625. 		if( dissonance ) skill_dance_switch(unit, 1);
  10626.  
  10627. 		return 0;
  10628. 	}
  10629. 	else
  10630. 	{
  10631. 		if( flag&1 )
  10632. 		{
  10633. 			int result = skill_unit_onplace(unit,target,tick);
  10634. 			if( flag&2 && result )
  10635. 			{	//Clear skill ids we have stored in onout.
  10636. 				ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == result );
  10637. 				if( i < ARRAYLENGTH(skill_unit_temp) )
  10638. 					skill_unit_temp[i] = 0;
  10639. 			}
  10640. 		}
  10641. 		else
  10642. 		{
  10643. 			int result = skill_unit_onout(unit,target,tick);
  10644. 			if( flag&2 && result )
  10645. 			{	//Store this unit id.
  10646. 				ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == 0 );
  10647. 				if( i < ARRAYLENGTH(skill_unit_temp) )
  10648. 					skill_unit_temp[i] = skill_id;
  10649. 				else
  10650. 					ShowError("skill_unit_move_sub: Reached limit of unit objects per cell!\n");
  10651. 			}
  10652. 		}
  10653.  
  10654. 		//TODO: Normally, this is dangerous since the unit and group could be freed
  10655. 		//inside the onout/onplace functions. Currently it is safe because we know song/dance
  10656. 		//cells do not get deleted within them. [Skotlex]
  10657. 		if( dissonance ) skill_dance_switch(unit, 1);
  10658.  
  10659. 		if( flag&4 )
  10660. 			skill_unit_onleft(skill_id,target,tick);
  10661.  
  10662. 		return 1;
  10663. 	}
  10664. }
  10665.  
  10666. /*==========================================
  10667.  * Invoked when a char has moved and unit cells must be invoked (onplace, onout, onleft)
  10668.  * Flag values:
  10669.  * flag&1: invoke skill_unit_onplace (otherwise invoke skill_unit_onout)
  10670.  * flag&2: this function is being invoked twice as a bl moves, store in memory the affected
  10671.  * units to figure out when they have left a group.
  10672.  * flag&4: Force a onleft event (triggered when the bl is killed, for example)
  10673.  *------------------------------------------*/
  10674. int skill_unit_move (struct block_list *bl, unsigned int tick, int flag)
  10675. {
  10676. 	nullpo_ret(bl);
  10677.  
  10678. 	if( bl->prev == NULL )
  10679. 		return 0;
  10680.  
  10681. 	if( flag&2 && !(flag&1) )
  10682. 	{	//Onout, clear data
  10683. 		memset(skill_unit_temp, 0, sizeof(skill_unit_temp));
  10684. 	}
  10685.  
  10686. 	map_foreachincell(skill_unit_move_sub,bl->m,bl->x,bl->y,BL_SKILL,bl,tick,flag);
  10687.  
  10688. 	if( flag&2 && flag&1 )
  10689. 	{	//Onplace, check any skill units you have left.
  10690. 		int i;
  10691. 		for( i = 0; i < ARRAYLENGTH(skill_unit_temp); i++ )
  10692. 			if( skill_unit_temp[i] )
  10693. 				skill_unit_onleft(skill_unit_temp[i], bl, tick);
  10694. 	}
  10695.  
  10696. 	return 0;
  10697. }
  10698.  
  10699. /*==========================================
  10700.  *
  10701.  *------------------------------------------*/
  10702. int skill_unit_move_unit_group (struct skill_unit_group *group, int m, int dx, int dy)
  10703. {
  10704. 	int i,j;
  10705. 	unsigned int tick = gettick();
  10706. 	int *m_flag;
  10707. 	struct skill_unit *unit1;
  10708. 	struct skill_unit *unit2;
  10709.  
  10710. 	if (group == NULL)
  10711. 		return 0;
  10712. 	if (group->unit_count<=0)
  10713. 		return 0;
  10714. 	if (group->unit==NULL)
  10715. 		return 0;
  10716.  
  10717. 	if (skill_get_unit_flag(group->skill_id)&UF_ENSEMBLE)
  10718. 		return 0; //Ensembles may not be moved around.
  10719.  
  10720. 	if( group->unit_id == UNT_ICEWALL )
  10721. 		return 0; //Icewalls don't get knocked back
  10722.  
  10723. 	m_flag = (int *) aCalloc(group->unit_count, sizeof(int));
  10724. 	//    m_flag
  10725. 	//		0: Neither of the following (skill_unit_onplace & skill_unit_onout are needed)
  10726. 	//		1: Unit will move to a slot that had another unit of the same group (skill_unit_onplace not needed)
  10727. 	//		2: Another unit from same group will end up positioned on this unit (skill_unit_onout not needed)
  10728. 	//		3: Both 1+2.
  10729. 	for(i=0;i<group->unit_count;i++){
  10730. 		unit1=&group->unit[i];
  10731. 		if (!unit1->alive || unit1->bl.m!=m)
  10732. 			continue;
  10733. 		for(j=0;j<group->unit_count;j++){
  10734. 			unit2=&group->unit[j];
  10735. 			if (!unit2->alive)
  10736. 				continue;
  10737. 			if (unit1->bl.x+dx==unit2->bl.x && unit1->bl.y+dy==unit2->bl.y){
  10738. 				m_flag[i] |= 0x1;
  10739. 			}
  10740. 			if (unit1->bl.x-dx==unit2->bl.x && unit1->bl.y-dy==unit2->bl.y){
  10741. 				m_flag[i] |= 0x2;
  10742. 			}
  10743. 		}
  10744. 	}
  10745. 	j = 0;
  10746. 	for (i=0;i<group->unit_count;i++) {
  10747. 		unit1=&group->unit[i];
  10748. 		if (!unit1->alive)
  10749. 			continue;
  10750. 		if (!(m_flag[i]&0x2)) {
  10751. 			if (group->state.song_dance&0x1) //Cancel dissonance effect.
  10752. 				skill_dance_overlap(unit1, 0);
  10753. 			map_foreachincell(skill_unit_effect,unit1->bl.m,unit1->bl.x,unit1->bl.y,group->bl_flag,&unit1->bl,tick,4);
  10754. 		}
  10755. 		//Move Cell using "smart" criteria (avoid useless moving around)
  10756. 		switch(m_flag[i])
  10757. 		{
  10758. 			case 0:
  10759. 			//Cell moves independently, safely move it.
  10760. 				map_moveblock(&unit1->bl, unit1->bl.x+dx, unit1->bl.y+dy, tick);
  10761. 				break;
  10762. 			case 1:
  10763. 			//Cell moves unto another cell, look for a replacement cell that won't collide
  10764. 			//and has no cell moving into it (flag == 2)
  10765. 				for(;j<group->unit_count;j++)
  10766. 				{
  10767. 					if(m_flag[j]!=2 || !group->unit[j].alive)
  10768. 						continue;
  10769. 					//Move to where this cell would had moved.
  10770. 					unit2 = &group->unit[j];
  10771. 					map_moveblock(&unit1->bl, unit2->bl.x+dx, unit2->bl.y+dy, tick);
  10772. 					j++; //Skip this cell as we have used it.
  10773. 					break;
  10774. 				}
  10775. 				break;
  10776. 			case 2:
  10777. 			case 3:
  10778. 				break; //Don't move the cell as a cell will end on this tile anyway.
  10779. 		}
  10780. 		if (!(m_flag[i]&0x2)) { //We only moved the cell in 0-1
  10781. 			if (group->state.song_dance&0x1) //Check for dissonance effect.
  10782. 				skill_dance_overlap(unit1, 1);
  10783. 			clif_skill_setunit(unit1);
  10784. 			map_foreachincell(skill_unit_effect,unit1->bl.m,unit1->bl.x,unit1->bl.y,group->bl_flag,&unit1->bl,tick,1);
  10785. 		}
  10786. 	}
  10787. 	aFree(m_flag);
  10788. 	return 0;
  10789. }
  10790.  
  10791. /*==========================================
  10792.  *
  10793.  *------------------------------------------*/
  10794. int skill_can_produce_mix (struct map_session_data *sd, int nameid, int trigger, int qty)
  10795. {
  10796. 	int i,j;
  10797.  
  10798. 	nullpo_ret(sd);
  10799.  
  10800. 	if(nameid<=0)
  10801. 		return 0;
  10802.  
  10803. 	for(i=0;i<MAX_SKILL_PRODUCE_DB;i++){
  10804. 		if(skill_produce_db[i].nameid == nameid )
  10805. 			break;
  10806. 	}
  10807. 	if( i >= MAX_SKILL_PRODUCE_DB )
  10808. 		return 0;
  10809.  
  10810. 	if( pc_checkadditem(sd, nameid, qty) == ADDITEM_OVERAMOUNT )
  10811. 	{// cannot carry the produced stuff
  10812. 		return 0;
  10813. 	}
  10814.  
  10815. 	if(trigger>=0){
  10816. 		if(trigger>20) { // Non-weapon, non-food item (itemlv must match)
  10817. 			if(skill_produce_db[i].itemlv!=trigger)
  10818. 				return 0;
  10819. 		} else if(trigger>10) { // Food (any item level between 10 and 20 will do)
  10820. 			if(skill_produce_db[i].itemlv<=10 || skill_produce_db[i].itemlv>20)
  10821. 				return 0;
  10822. 		} else { // Weapon (itemlv must be higher or equal)
  10823. 			if(skill_produce_db[i].itemlv>trigger)
  10824. 				return 0;
  10825. 		}
  10826. 	}
  10827. 	if((j=skill_produce_db[i].req_skill)>0 &&
  10828. 		pc_checkskill(sd,j) < skill_produce_db[i].req_skill_lv)
  10829. 		return 0;
  10830.  
  10831. 	for(j=0;j<MAX_PRODUCE_RESOURCE;j++){
  10832. 		int id,x,y;
  10833. 		if( (id=skill_produce_db[i].mat_id[j]) <= 0 )
  10834. 			continue;
  10835. 		if(skill_produce_db[i].mat_amount[j] <= 0) {
  10836. 			if(pc_search_inventory(sd,id) < 0)
  10837. 				return 0;
  10838. 		}
  10839. 		else {
  10840. 			for(y=0,x=0;y<MAX_INVENTORY;y++)
  10841. 				if( sd->status.inventory[y].nameid == id )
  10842. 					x+=sd->status.inventory[y].amount;
  10843. 			if(x<qty*skill_produce_db[i].mat_amount[j])
  10844. 				return 0;
  10845. 		}
  10846. 	}
  10847. 	return i+1;
  10848. }
  10849.  
  10850. /*==========================================
  10851.  *
  10852.  *------------------------------------------*/
  10853. int skill_produce_mix (struct map_session_data *sd, int skill_id, int nameid, int slot1, int slot2, int slot3, int qty)
  10854. {
  10855. 	int slot[3];
  10856. 	int i,sc,ele,idx,equip,wlv,make_per,flag;
  10857. 	int num = -1; // exclude the recipe
  10858. 	struct status_data *status;
  10859.  
  10860. 	nullpo_ret(sd);
  10861. 	status = status_get_status_data(&sd->bl);
  10862.  
  10863. 	if( !(idx=skill_can_produce_mix(sd,nameid,-1, qty)) )
  10864. 		return 0;
  10865. 	idx--;
  10866.  
  10867. 	if (qty < 1)
  10868. 		qty = 1;
  10869.  
  10870. 	if (!skill_id) //A skill can be specified for some override cases.
  10871. 		skill_id = skill_produce_db[idx].req_skill;
  10872.  
  10873. 	slot[0]=slot1;
  10874. 	slot[1]=slot2;
  10875. 	slot[2]=slot3;
  10876.  
  10877. 	for(i=0,sc=0,ele=0;i<3;i++){ //Note that qty should always be one if you are using these!
  10878. 		int j;
  10879. 		if( slot[i]<=0 )
  10880. 			continue;
  10881. 		j = pc_search_inventory(sd,slot[i]);
  10882. 		if(j < 0)
  10883. 			continue;
  10884. 		if(slot[i]==1000){	/* Star Crumb */
  10885. 			pc_delitem(sd,j,1,1,0);
  10886. 			sc++;
  10887. 		}
  10888. 		if(slot[i]>=994 && slot[i]<=997 && ele==0){	/* Flame Heart . . . Great Nature */
  10889. 			static const int ele_table[4]={3,1,4,2};
  10890. 			pc_delitem(sd,j,1,1,0);
  10891. 			ele=ele_table[slot[i]-994];
  10892. 		}
  10893. 	}
  10894.  
  10895. 	for(i=0;i<MAX_PRODUCE_RESOURCE;i++){
  10896. 		int j,id,x;
  10897. 		if( (id=skill_produce_db[idx].mat_id[i]) <= 0 )
  10898. 			continue;
  10899. 		num++;
  10900. 		x=qty*skill_produce_db[idx].mat_amount[i];
  10901. 		do{
  10902. 			int y=0;
  10903. 			j = pc_search_inventory(sd,id);
  10904.  
  10905. 			if(j >= 0){
  10906. 				y = sd->status.inventory[j].amount;
  10907. 				if(y>x)y=x;
  10908. 				pc_delitem(sd,j,y,0,0);
  10909. 			} else
  10910. 				ShowError("skill_produce_mix: material item error\n");
  10911.  
  10912. 			x-=y;
  10913. 		}while( j>=0 && x>0 );
  10914. 	}
  10915.  
  10916. 	if((equip=itemdb_isequip(nameid)))
  10917. 		wlv = itemdb_wlv(nameid);
  10918. 	if(!equip) {
  10919. 		switch(skill_id){
  10920. 			case BS_IRON:
  10921. 			case BS_STEEL:
  10922. 			case BS_ENCHANTEDSTONE:
  10923. 				// Ores & Metals Refining - skill bonuses are straight from kRO website [DracoRPG]
  10924. 				i = pc_checkskill(sd,skill_id);
  10925. 				make_per = sd->status.job_level*20 + status->dex*10 + status->luk*10; //Base chance
  10926. 				switch(nameid){
  10927. 					case 998: // Iron
  10928. 						make_per += 4000+i*500; // Temper Iron bonus: +26/+32/+38/+44/+50
  10929. 						break;
  10930. 					case 999: // Steel
  10931. 						make_per += 3000+i*500; // Temper Steel bonus: +35/+40/+45/+50/+55
  10932. 						break;
  10933. 					case 1000: //Star Crumb
  10934. 						make_per = 100000; // Star Crumbs are 100% success crafting rate? (made 1000% so it succeeds even after penalties) [Skotlex]
  10935. 						break;
  10936. 					default: // Enchanted Stones
  10937. 						make_per += 1000+i*500; // Enchantedstone Craft bonus: +15/+20/+25/+30/+35
  10938. 					break;
  10939. 				}
  10940. 				break;
  10941. 			case ASC_CDP:
  10942. 				make_per = (2000 + 40*status->dex + 20*status->luk);
  10943. 				break;
  10944. 			case AL_HOLYWATER:
  10945. 				make_per = 100000; //100% success
  10946. 				break;
  10947. 			case AM_PHARMACY: // Potion Preparation - reviewed with the help of various Ragnainfo sources [DracoRPG]
  10948. 			case AM_TWILIGHT1:
  10949. 			case AM_TWILIGHT2:
  10950. 			case AM_TWILIGHT3:
  10951. 				make_per = pc_checkskill(sd,AM_LEARNINGPOTION)*50
  10952. 					+ pc_checkskill(sd,AM_PHARMACY)*300 + sd->status.job_level*20
  10953. 					+ (status->int_/2)*10 + status->dex*10+status->luk*10;
  10954. 				if(merc_is_hom_active(sd->hd)) {//Player got a homun
  10955. 					int skill;
  10956. 					if((skill=merc_hom_checkskill(sd->hd,HVAN_INSTRUCT)) > 0) //His homun is a vanil with instruction change
  10957. 						make_per += skill*100; //+1% bonus per level
  10958. 				}
  10959. 				switch(nameid){
  10960. 					case 501: // Red Potion
  10961. 					case 503: // Yellow Potion
  10962. 					case 504: // White Potion
  10963. 						make_per += (1+rand()%100)*10 + 2000;
  10964. 						break;
  10965. 					case 970: // Alcohol
  10966. 						make_per += (1+rand()%100)*10 + 1000;
  10967. 						break;
  10968. 					case 7135: // Bottle Grenade
  10969. 					case 7136: // Acid Bottle
  10970. 					case 7137: // Plant Bottle
  10971. 					case 7138: // Marine Sphere Bottle
  10972. 						make_per += (1+rand()%100)*10;
  10973. 						break;
  10974. 					case 546: // Condensed Yellow Potion
  10975. 						make_per -= (1+rand()%50)*10;
  10976. 						break;
  10977. 					case 547: // Condensed White Potion
  10978. 					case 7139: // Glistening Coat
  10979. 						make_per -= (1+rand()%100)*10;
  10980. 					    break;
  10981. 					//Common items, recieve no bonus or penalty, listed just because they are commonly produced
  10982. 					case 505: // Blue Potion
  10983. 					case 545: // Condensed Red Potion
  10984. 					case 605: // Anodyne
  10985. 					case 606: // Aloevera
  10986. 					default:
  10987. 						break;
  10988. 				}
  10989. 				if(battle_config.pp_rate != 100)
  10990. 					make_per = make_per * battle_config.pp_rate / 100;
  10991. 				break;
  10992. 			case SA_CREATECON: // Elemental Converter Creation
  10993. 				make_per = 100000; // should be 100% success rate
  10994. 				break;
  10995. 			default:
  10996. 				if (sd->menuskill_id ==	AM_PHARMACY &&
  10997. 					sd->menuskill_val > 10 && sd->menuskill_val <= 20)
  10998. 				{	//Assume Cooking Dish
  10999. 					if (sd->menuskill_val >= 15) //Legendary Cooking Set.
  11000. 						make_per = 10000; //100% Success
  11001. 					else
  11002. 						make_per = 1200 * (sd->menuskill_val - 10)
  11003. 							+ 20  * (sd->status.base_level + 1)
  11004. 							+ 20  * (status->dex + 1)
  11005. 							+ 100 * (rand()%(30+5*(sd->cook_mastery/400) - (6+sd->cook_mastery/80)) + (6+sd->cook_mastery/80))
  11006. 							- 400 * (skill_produce_db[idx].itemlv - 11 + 1)
  11007. 							- 10  * (100 - status->luk + 1)
  11008. 							- 500 * (num - 1)
  11009. 							- 100 * (rand()%4 + 1);
  11010. 					break;
  11011. 				}
  11012. 				make_per = 5000;
  11013. 				break;
  11014. 		}
  11015. 	} else { // Weapon Forging - skill bonuses are straight from kRO website, other things from a jRO calculator [DracoRPG]
  11016. 		make_per = 5000 + sd->status.job_level*20 + status->dex*10 + status->luk*10; // Base
  11017. 		make_per += pc_checkskill(sd,skill_id)*500; // Smithing skills bonus: +5/+10/+15
  11018. 		make_per += pc_checkskill(sd,BS_WEAPONRESEARCH)*100 +((wlv >= 3)? pc_checkskill(sd,BS_ORIDEOCON)*100:0); // Weaponry Research bonus: +1/+2/+3/+4/+5/+6/+7/+8/+9/+10, Oridecon Research bonus (custom): +1/+2/+3/+4/+5
  11019. 		make_per -= (ele?2000:0) + sc*1500 + (wlv>1?wlv*1000:0); // Element Stone: -20%, Star Crumb: -15% each, Weapon level malus: -0/-20/-30
  11020. 		if(pc_search_inventory(sd,989) > 0) make_per+= 1000; // Emperium Anvil: +10
  11021. 		else if(pc_search_inventory(sd,988) > 0) make_per+= 500; // Golden Anvil: +5
  11022. 		else if(pc_search_inventory(sd,987) > 0) make_per+= 300; // Oridecon Anvil: +3
  11023. 		else if(pc_search_inventory(sd,986) > 0) make_per+= 0; // Anvil: +0?
  11024. 		if(battle_config.wp_rate != 100)
  11025. 			make_per = make_per * battle_config.wp_rate / 100;
  11026. 	}
  11027.  
  11028. 	if (sd->class_&JOBL_BABY) //if it's a Baby Class
  11029. 		make_per = (make_per * 70) / 100; //Baby penalty is 30%
  11030.  
  11031. 	if(make_per < 1) make_per = 1;
  11032.  
  11033.  
  11034. 	if(rand()%10000 < make_per || qty > 1){ //Success, or crafting multiple items.
  11035. 		struct item tmp_item;
  11036. 		memset(&tmp_item,0,sizeof(tmp_item));
  11037. 		tmp_item.nameid=nameid;
  11038. 		tmp_item.amount=1;
  11039. 		tmp_item.identify=1;
  11040. 		if(equip){
  11041. 			tmp_item.card[0]=CARD0_FORGE;
  11042. 			tmp_item.card[1]=((sc*5)<<8)+ele;
  11043. 			tmp_item.card[2]=GetWord(sd->status.char_id,0); // CharId
  11044. 			tmp_item.card[3]=GetWord(sd->status.char_id,1);
  11045. 		} else {
  11046. 			//Flag is only used on the end, so it can be used here. [Skotlex]
  11047. 			switch (skill_id) {
  11048. 				case BS_DAGGER:
  11049. 				case BS_SWORD:
  11050. 				case BS_TWOHANDSWORD:
  11051. 				case BS_AXE:
  11052. 				case BS_MACE:
  11053. 				case BS_KNUCKLE:
  11054. 				case BS_SPEAR:
  11055. 					flag = battle_config.produce_item_name_input&0x1;
  11056. 					break;
  11057. 				case AM_PHARMACY:
  11058. 				case AM_TWILIGHT1:
  11059. 				case AM_TWILIGHT2:
  11060. 				case AM_TWILIGHT3:
  11061. 					flag = battle_config.produce_item_name_input&0x2;
  11062. 					break;
  11063. 				case AL_HOLYWATER:
  11064. 					flag = battle_config.produce_item_name_input&0x8;
  11065. 					break;
  11066. 				case ASC_CDP:
  11067. 					flag = battle_config.produce_item_name_input&0x10;
  11068. 					break;
  11069. 				default:
  11070. 					flag = battle_config.produce_item_name_input&0x80;
  11071. 					break;
  11072. 			}
  11073. 			if (flag) {
  11074. 				tmp_item.card[0]=CARD0_CREATE;
  11075. 				tmp_item.card[1]=0;
  11076. 				tmp_item.card[2]=GetWord(sd->status.char_id,0); // CharId
  11077. 				tmp_item.card[3]=GetWord(sd->status.char_id,1);
  11078. 			}
  11079. 		}
  11080.  
  11081. //		if(log_config.produce > 0)
  11082. //			log_produce(sd,nameid,slot1,slot2,slot3,1);
  11083. //TODO update PICKLOG
  11084.  
  11085. 		if(equip){
  11086. 			clif_produceeffect(sd,0,nameid);
  11087. 			clif_misceffect(&sd->bl,3);
  11088. 			if(itemdb_wlv(nameid) >= 3 && ((ele? 1 : 0) + sc) >= 3) // Fame point system [DracoRPG]
  11089. 				pc_addfame(sd,10); // Success to forge a lv3 weapon with 3 additional ingredients = +10 fame point
  11090. 		} else {
  11091. 			int fame = 0;
  11092. 			tmp_item.amount = 0;
  11093. 			for (i=0; i< qty; i++)
  11094. 			{	//Apply quantity modifiers.
  11095. 				if (rand()%10000 < make_per || qty == 1)
  11096. 				{ //Success
  11097. 					tmp_item.amount++;
  11098. 					if(nameid < 545 || nameid > 547)
  11099. 						continue;
  11100. 					if(skill_id != AM_PHARMACY &&
  11101. 						skill_id != AM_TWILIGHT1 &&
  11102. 						skill_id != AM_TWILIGHT2 &&
  11103. 						skill_id != AM_TWILIGHT3)
  11104. 						continue;
  11105. 					//Add fame as needed.
  11106. 					switch(++sd->potion_success_counter) {
  11107. 						case 3:
  11108. 							fame+=1; // Success to prepare 3 Condensed Potions in a row
  11109. 							break;
  11110. 						case 5:
  11111. 							fame+=3; // Success to prepare 5 Condensed Potions in a row
  11112. 							break;
  11113. 						case 7:
  11114. 							fame+=10; // Success to prepare 7 Condensed Potions in a row
  11115. 							break;
  11116. 						case 10:
  11117. 							fame+=50; // Success to prepare 10 Condensed Potions in a row
  11118. 							sd->potion_success_counter = 0;
  11119. 							break;
  11120. 					}
  11121. 				} else //Failure
  11122. 					sd->potion_success_counter = 0;
  11123. 			}
  11124. 			if (fame)
  11125. 				pc_addfame(sd,fame);
  11126. 			//Visual effects and the like.
  11127. 			switch (skill_id) {
  11128. 				case AM_PHARMACY:
  11129. 				case AM_TWILIGHT1:
  11130. 				case AM_TWILIGHT2:
  11131. 				case AM_TWILIGHT3:
  11132. 				case ASC_CDP:
  11133. 					clif_produceeffect(sd,2,nameid);
  11134. 					clif_misceffect(&sd->bl,5);
  11135. 					break;
  11136. 				case BS_IRON:
  11137. 				case BS_STEEL:
  11138. 				case BS_ENCHANTEDSTONE:
  11139. 					clif_produceeffect(sd,0,nameid);
  11140. 					clif_misceffect(&sd->bl,3);
  11141. 					break;
  11142. 				default: //Those that don't require a skill?
  11143. 					if( skill_produce_db[idx].itemlv > 10 && skill_produce_db[idx].itemlv <= 20)
  11144. 					{ //Cooking items.
  11145. 						clif_specialeffect(&sd->bl, 608, AREA);
  11146. 						if( sd->cook_mastery < 1999 )
  11147. 							pc_setglobalreg(sd, "COOK_MASTERY",sd->cook_mastery + ( 1 << ( (skill_produce_db[idx].itemlv - 11) / 2 ) ) * 5);
  11148. 					}
  11149. 					break;
  11150. 			}
  11151. 		}
  11152. 		if (tmp_item.amount) { //Success
  11153. 			if((flag = pc_additem(sd,&tmp_item,tmp_item.amount))) {
  11154. 				clif_additem(sd,0,0,flag);
  11155. 				map_addflooritem(&tmp_item,tmp_item.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  11156. 			}
  11157. 			return 1;
  11158. 		}
  11159. 	}
  11160. 	//Failure
  11161. //	if(log_config.produce)
  11162. //		log_produce(sd,nameid,slot1,slot2,slot3,0);
  11163. //TODO update PICKLOG
  11164.  
  11165. 	if(equip){
  11166. 		clif_produceeffect(sd,1,nameid);
  11167. 		clif_misceffect(&sd->bl,2);
  11168. 	} else {
  11169. 		switch (skill_id) {
  11170. 			case ASC_CDP: //25% Damage yourself, and display same effect as failed potion.
  11171. 				status_percent_damage(NULL, &sd->bl, -25, 0, true);
  11172. 			case AM_PHARMACY:
  11173. 			case AM_TWILIGHT1:
  11174. 			case AM_TWILIGHT2:
  11175. 			case AM_TWILIGHT3:
  11176. 				clif_produceeffect(sd,3,nameid);
  11177. 				clif_misceffect(&sd->bl,6);
  11178. 				sd->potion_success_counter = 0; // Fame point system [DracoRPG]
  11179. 				break;
  11180. 			case BS_IRON:
  11181. 			case BS_STEEL:
  11182. 			case BS_ENCHANTEDSTONE:
  11183. 				clif_produceeffect(sd,1,nameid);
  11184. 				clif_misceffect(&sd->bl,2);
  11185. 				break;
  11186. 			default:
  11187. 				if( skill_produce_db[idx].itemlv > 10 && skill_produce_db[idx].itemlv <= 20 )
  11188. 				{ //Cooking items.
  11189. 					clif_specialeffect(&sd->bl, 609, AREA);
  11190. 					if( sd->cook_mastery > 0 )
  11191. 						pc_setglobalreg(sd, "COOK_MASTERY", sd->cook_mastery - ( 1 << ((skill_produce_db[idx].itemlv - 11) / 2) ) - ( ( ( 1 << ((skill_produce_db[idx].itemlv - 11) / 2) ) >> 1 ) * 3 ));
  11192. 				}
  11193. 		}
  11194. 	}
  11195. 	return 0;
  11196. }
  11197.  
  11198. int skill_arrow_create (struct map_session_data *sd, int nameid)
  11199. {
  11200. 	int i,j,flag,index=-1;
  11201. 	struct item tmp_item;
  11202.  
  11203. 	nullpo_ret(sd);
  11204.  
  11205. 	if(nameid <= 0)
  11206. 		return 1;
  11207.  
  11208. 	for(i=0;i<MAX_SKILL_ARROW_DB;i++)
  11209. 		if(nameid == skill_arrow_db[i].nameid) {
  11210. 			index = i;
  11211. 			break;
  11212. 		}
  11213.  
  11214. 	if(index < 0 || (j = pc_search_inventory(sd,nameid)) < 0)
  11215. 		return 1;
  11216.  
  11217. 	pc_delitem(sd,j,1,0,0);
  11218. 	for(i=0;i<MAX_ARROW_RESOURCE;i++) {
  11219. 		memset(&tmp_item,0,sizeof(tmp_item));
  11220. 		tmp_item.identify = 1;
  11221. 		tmp_item.nameid = skill_arrow_db[index].cre_id[i];
  11222. 		tmp_item.amount = skill_arrow_db[index].cre_amount[i];
  11223. 		if(battle_config.produce_item_name_input&0x4) {
  11224. 			tmp_item.card[0]=CARD0_CREATE;
  11225. 			tmp_item.card[1]=0;
  11226. 			tmp_item.card[2]=GetWord(sd->status.char_id,0); // CharId
  11227. 			tmp_item.card[3]=GetWord(sd->status.char_id,1);
  11228. 		}
  11229. 		if(tmp_item.nameid <= 0 || tmp_item.amount <= 0)
  11230. 			continue;
  11231. 		if((flag = pc_additem(sd,&tmp_item,tmp_item.amount))) {
  11232. 			clif_additem(sd,0,0,flag);
  11233. 			map_addflooritem(&tmp_item,tmp_item.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  11234. 		}
  11235. 	}
  11236.  
  11237. 	return 0;
  11238. }
  11239.  
  11240. /*==========================================
  11241.  *
  11242.  *------------------------------------------*/
  11243. int skill_blockpc_end(int tid, unsigned int tick, int id, intptr_t data)
  11244. {
  11245. 	struct map_session_data *sd = map_id2sd(id);
  11246. 	if (data <= 0 || data >= MAX_SKILL)
  11247. 		return 0;
  11248. 	if (!sd) return 0;
  11249. 	if (sd->blockskill[data] != (0x1|(tid&0xFE))) return 0;
  11250. 	sd->blockskill[data] = 0;
  11251. 	return 1;
  11252. }
  11253.  
  11254. int skill_blockpc_start(struct map_session_data *sd, int skillid, int tick)
  11255. {
  11256. 	nullpo_retr (-1, sd);
  11257.  
  11258. 	skillid = skill_get_index(skillid);
  11259. 	if (skillid == 0)
  11260. 		return -1;
  11261.  
  11262. 	if (tick < 1) {
  11263. 		sd->blockskill[skillid] = 0;
  11264. 		return -1;
  11265. 	}
  11266.  
  11267. 	if( battle_config.display_status_timers )
  11268. 		clif_skill_cooldown(sd, skillid, tick);
  11269.  
  11270. 	sd->blockskill[skillid] = 0x1|(0xFE&add_timer(gettick()+tick,skill_blockpc_end,sd->bl.id,skillid));
  11271. 	return 0;
  11272. }
  11273.  
  11274. int skill_blockhomun_end(int tid, unsigned int tick, int id, intptr_t data)	//[orn]
  11275. {
  11276. 	struct homun_data *hd = (TBL_HOM*) map_id2bl(id);
  11277. 	if (data <= 0 || data >= MAX_SKILL)
  11278. 		return 0;
  11279. 	if (hd) hd->blockskill[data] = 0;
  11280.  
  11281. 	return 1;
  11282. }
  11283.  
  11284. int skill_blockhomun_start(struct homun_data *hd, int skillid, int tick)	//[orn]
  11285. {
  11286. 	nullpo_retr (-1, hd);
  11287.  
  11288. 	skillid = skill_get_index(skillid);
  11289. 	if (skillid == 0)
  11290. 		return -1;
  11291.  
  11292. 	if (tick < 1) {
  11293. 		hd->blockskill[skillid] = 0;
  11294. 		return -1;
  11295. 	}
  11296. 	hd->blockskill[skillid] = 1;
  11297. 	return add_timer(gettick() + tick, skill_blockhomun_end, hd->bl.id, skillid);
  11298. }
  11299.  
  11300. int skill_blockmerc_end(int tid, unsigned int tick, int id, intptr_t data)	//[orn]
  11301. {
  11302. 	struct mercenary_data *md = (TBL_MER*)map_id2bl(id);
  11303. 	if( data <= 0 || data >= MAX_SKILL )
  11304. 		return 0;
  11305. 	if( md ) md->blockskill[data] = 0;
  11306.  
  11307. 	return 1;
  11308. }
  11309.  
  11310. int skill_blockmerc_start(struct mercenary_data *md, int skillid, int tick)
  11311. {
  11312. 	nullpo_retr (-1, md);
  11313.  
  11314. 	if( (skillid = skill_get_index(skillid)) == 0 )
  11315. 		return -1;
  11316. 	if( tick < 1 )
  11317. 	{
  11318. 		md->blockskill[skillid] = 0;
  11319. 		return -1;
  11320. 	}
  11321. 	md->blockskill[skillid] = 1;
  11322. 	return add_timer(gettick() + tick, skill_blockmerc_end, md->bl.id, skillid);
  11323. }
  11324.  
  11325. /*
  11326.  *
  11327.  */
  11328. int skill_split_str (char *str, char **val, int num)
  11329. {
  11330. 	int i;
  11331.  
  11332. 	for( i = 0; i < num && str; i++ )
  11333. 	{
  11334. 		val[i] = str;
  11335. 		str = strchr(str,',');
  11336. 		if( str )
  11337. 			*str++=0;
  11338. 	}
  11339.  
  11340. 	return i;
  11341. }
  11342. /*
  11343.  *
  11344.  */
  11345. int skill_split_atoi (char *str, int *val)
  11346. {
  11347. 	int i, j, diff, step = 1;
  11348.  
  11349. 	for (i=0; i<MAX_SKILL_LEVEL; i++) {
  11350. 		if (!str) break;
  11351. 		val[i] = atoi(str);
  11352. 		str = strchr(str,':');
  11353. 		if (str)
  11354. 			*str++=0;
  11355. 	}
  11356. 	if(i==0) //No data found.
  11357. 		return 0;
  11358. 	if(i==1)
  11359. 	{	//Single value, have the whole range have the same value.
  11360. 		for (; i < MAX_SKILL_LEVEL; i++)
  11361. 			val[i] = val[i-1];
  11362. 		return i;
  11363. 	}
  11364. 	//Check for linear change with increasing steps until we reach half of the data acquired.
  11365. 	for (step = 1; step <= i/2; step++)
  11366. 	{
  11367. 		diff = val[i-1] - val[i-step-1];
  11368. 		for(j = i-1; j >= step; j--)
  11369. 			if ((val[j]-val[j-step]) != diff)
  11370. 				break;
  11371.  
  11372. 		if (j>=step) //No match, try next step.
  11373. 			continue;
  11374.  
  11375. 		for(; i < MAX_SKILL_LEVEL; i++)
  11376. 		{	//Apply linear increase
  11377. 			val[i] = val[i-step]+diff;
  11378. 			if (val[i] < 1 && val[i-1] >=0) //Check if we have switched from + to -, cap the decrease to 0 in said cases.
  11379. 			{ val[i] = 1; diff = 0; step = 1; }
  11380. 		}
  11381. 		return i;
  11382. 	}
  11383. 	//Okay.. we can't figure this one out, just fill out the stuff with the previous value.
  11384. 	for (;i<MAX_SKILL_LEVEL; i++)
  11385. 		val[i] = val[i-1];
  11386. 	return i;
  11387. }
  11388.  
  11389. /*
  11390.  *
  11391.  */
  11392. void skill_init_unit_layout (void)
  11393. {
  11394. 	int i,j,size,pos = 0;
  11395.  
  11396. 	memset(skill_unit_layout,0,sizeof(skill_unit_layout));
  11397.  
  11398. 	// standard square layouts go first
  11399. 	for (i=0; i<=MAX_SQUARE_LAYOUT; i++) {
  11400. 		size = i*2+1;
  11401. 		skill_unit_layout[i].count = size*size;
  11402. 		for (j=0; j<size*size; j++) {
  11403. 			skill_unit_layout[i].dx[j] = (j%size-i);
  11404. 			skill_unit_layout[i].dy[j] = (j/size-i);
  11405. 		}
  11406. 	}
  11407.  
  11408. 	// afterwards add special ones
  11409. 	pos = i;
  11410. 	for (i=0;i<MAX_SKILL_DB;i++) {
  11411. 		if (!skill_db[i].unit_id[0] || skill_db[i].unit_layout_type[0] != -1)
  11412. 			continue;
  11413. 		switch (i) {
  11414. 			case MG_FIREWALL:
  11415. 			case WZ_ICEWALL:
  11416. 				// these will be handled later
  11417. 				break;
  11418. 			case PR_SANCTUARY:
  11419. 			case NPC_EVILLAND:
  11420. 			{
  11421. 				static const int dx[] = {
  11422. 					-1, 0, 1,-2,-1, 0, 1, 2,-2,-1,
  11423. 					 0, 1, 2,-2,-1, 0, 1, 2,-1, 0, 1};
  11424. 				static const int dy[]={
  11425. 					-2,-2,-2,-1,-1,-1,-1,-1, 0, 0,
  11426. 					 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2};
  11427. 				skill_unit_layout[pos].count = 21;
  11428. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11429. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11430. 				break;
  11431. 			}
  11432. 			case PR_MAGNUS:
  11433. 			{
  11434. 				static const int dx[] = {
  11435. 					-1, 0, 1,-1, 0, 1,-3,-2,-1, 0,
  11436. 					 1, 2, 3,-3,-2,-1, 0, 1, 2, 3,
  11437. 					-3,-2,-1, 0, 1, 2, 3,-1, 0, 1,-1, 0, 1};
  11438. 				static const int dy[] = {
  11439. 					-3,-3,-3,-2,-2,-2,-1,-1,-1,-1,
  11440. 					-1,-1,-1, 0, 0, 0, 0, 0, 0, 0,
  11441. 					 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3};
  11442. 				skill_unit_layout[pos].count = 33;
  11443. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11444. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11445. 				break;
  11446. 			}
  11447. 			case AS_VENOMDUST:
  11448. 			{
  11449. 				static const int dx[] = {-1, 0, 0, 0, 1};
  11450. 				static const int dy[] = { 0,-1, 0, 1, 0};
  11451. 				skill_unit_layout[pos].count = 5;
  11452. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11453. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11454. 				break;
  11455. 			}
  11456. 			case CR_GRANDCROSS:
  11457. 			case NPC_GRANDDARKNESS:
  11458. 			{
  11459. 				static const int dx[] = {
  11460. 					 0, 0,-1, 0, 1,-2,-1, 0, 1, 2,
  11461. 					-4,-3,-2,-1, 0, 1, 2, 3, 4,-2,
  11462. 					-1, 0, 1, 2,-1, 0, 1, 0, 0};
  11463. 				static const int dy[] = {
  11464. 					-4,-3,-2,-2,-2,-1,-1,-1,-1,-1,
  11465. 					 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  11466. 					 1, 1, 1, 1, 2, 2, 2, 3, 4};
  11467. 				skill_unit_layout[pos].count = 29;
  11468. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11469. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11470. 				break;
  11471. 			}
  11472. 			case PF_FOGWALL:
  11473. 			{
  11474. 				static const int dx[] = {
  11475. 					-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
  11476. 				static const int dy[] = {
  11477. 					-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
  11478. 				skill_unit_layout[pos].count = 15;
  11479. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11480. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11481. 				break;
  11482. 			}
  11483. 			case PA_GOSPEL:
  11484. 			{
  11485. 				static const int dx[] = {
  11486. 					-1, 0, 1,-1, 0, 1,-3,-2,-1, 0,
  11487. 					 1, 2, 3,-3,-2,-1, 0, 1, 2, 3,
  11488. 					-3,-2,-1, 0, 1, 2, 3,-1, 0, 1,
  11489. 					-1, 0, 1};
  11490. 				static const int dy[] = {
  11491. 					-3,-3,-3,-2,-2,-2,-1,-1,-1,-1,
  11492. 					-1,-1,-1, 0, 0, 0, 0, 0, 0, 0,
  11493. 					 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  11494. 					 3, 3, 3};
  11495. 				skill_unit_layout[pos].count = 33;
  11496. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11497. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11498. 				break;
  11499. 			}
  11500. 			case NJ_KAENSIN:
  11501. 			{
  11502. 				static const int dx[] = {-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
  11503. 				static const int dy[] = { 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2};
  11504. 				skill_unit_layout[pos].count = 24;
  11505. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11506. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11507. 				break;
  11508. 			}
  11509. 			case NJ_TATAMIGAESHI:
  11510. 			{
  11511. 				//Level 1 (count 4, cross of 3x3)
  11512. 				static const int dx1[] = {-1, 1, 0, 0};
  11513. 				static const int dy1[] = { 0, 0,-1, 1};
  11514. 				//Level 2-3 (count 8, cross of 5x5)
  11515. 				static const int dx2[] = {-2,-1, 1, 2, 0, 0, 0, 0};
  11516. 				static const int dy2[] = { 0, 0, 0, 0,-2,-1, 1, 2};
  11517. 				//Level 4-5 (count 12, cross of 7x7
  11518. 				static const int dx3[] = {-3,-2,-1, 1, 2, 3, 0, 0, 0, 0, 0, 0};
  11519. 				static const int dy3[] = { 0, 0, 0, 0, 0, 0,-3,-2,-1, 1, 2, 3};
  11520. 				//lv1
  11521. 				j = 0;
  11522. 				skill_unit_layout[pos].count = 4;
  11523. 				memcpy(skill_unit_layout[pos].dx,dx1,sizeof(dx1));
  11524. 				memcpy(skill_unit_layout[pos].dy,dy1,sizeof(dy1));
  11525. 				skill_db[i].unit_layout_type[j] = pos;
  11526. 				//lv2/3
  11527. 				j++;
  11528. 				pos++;
  11529. 				skill_unit_layout[pos].count = 8;
  11530. 				memcpy(skill_unit_layout[pos].dx,dx2,sizeof(dx2));
  11531. 				memcpy(skill_unit_layout[pos].dy,dy2,sizeof(dy2));
  11532. 				skill_db[i].unit_layout_type[j] = pos;
  11533. 				skill_db[i].unit_layout_type[++j] = pos;
  11534. 				//lv4/5
  11535. 				j++;
  11536. 				pos++;
  11537. 				skill_unit_layout[pos].count = 12;
  11538. 				memcpy(skill_unit_layout[pos].dx,dx3,sizeof(dx3));
  11539. 				memcpy(skill_unit_layout[pos].dy,dy3,sizeof(dy3));
  11540. 				skill_db[i].unit_layout_type[j] = pos;
  11541. 				skill_db[i].unit_layout_type[++j] = pos;
  11542. 				//Fill in the rest using lv 5.
  11543. 				for (;j<MAX_SKILL_LEVEL;j++)
  11544. 					skill_db[i].unit_layout_type[j] = pos;
  11545. 				//Skip, this way the check below will fail and continue to the next skill.
  11546. 				pos++;
  11547. 				break;
  11548. 			}
  11549. 			default:
  11550. 				ShowError("unknown unit layout at skill %d\n",i);
  11551. 				break;
  11552. 		}
  11553. 		if (!skill_unit_layout[pos].count)
  11554. 			continue;
  11555. 		for (j=0;j<MAX_SKILL_LEVEL;j++)
  11556. 			skill_db[i].unit_layout_type[j] = pos;
  11557. 		pos++;
  11558. 	}
  11559.  
  11560. 	// firewall and icewall have 8 layouts (direction-dependent)
  11561. 	firewall_unit_pos = pos;
  11562. 	for (i=0;i<8;i++) {
  11563. 		if (i&1) {
  11564. 			skill_unit_layout[pos].count = 5;
  11565. 			if (i&0x2) {
  11566. 				int dx[] = {-1,-1, 0, 0, 1};
  11567. 				int dy[] = { 1, 0, 0,-1,-1};
  11568. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11569. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11570. 			} else {
  11571. 				int dx[] = { 1, 1 ,0, 0,-1};
  11572. 				int dy[] = { 1, 0, 0,-1,-1};
  11573. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11574. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11575. 			}
  11576. 		} else {
  11577. 			skill_unit_layout[pos].count = 3;
  11578. 			if (i%4==0) {
  11579. 				int dx[] = {-1, 0, 1};
  11580. 				int dy[] = { 0, 0, 0};
  11581. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11582. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11583. 			} else {
  11584. 				int dx[] = { 0, 0, 0};
  11585. 				int dy[] = {-1, 0, 1};
  11586. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11587. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11588. 			}
  11589. 		}
  11590. 		pos++;
  11591. 	}
  11592. 	icewall_unit_pos = pos;
  11593. 	for (i=0;i<8;i++) {
  11594. 		skill_unit_layout[pos].count = 5;
  11595. 		if (i&1) {
  11596. 			if (i&0x2) {
  11597. 				int dx[] = {-2,-1, 0, 1, 2};
  11598. 				int dy[] = { 2, 1, 0,-1,-2};
  11599. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11600. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11601. 			} else {
  11602. 				int dx[] = { 2, 1 ,0,-1,-2};
  11603. 				int dy[] = { 2, 1, 0,-1,-2};
  11604. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11605. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11606. 			}
  11607. 		} else {
  11608. 			if (i%4==0) {
  11609. 				int dx[] = {-2,-1, 0, 1, 2};
  11610. 				int dy[] = { 0, 0, 0, 0, 0};
  11611. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11612. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11613. 			} else {
  11614. 				int dx[] = { 0, 0, 0, 0, 0};
  11615. 				int dy[] = {-2,-1, 0, 1, 2};
  11616. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11617. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11618. 			}
  11619. 		}
  11620. 		pos++;
  11621. 	}
  11622. }
  11623.  
  11624. /*==========================================
  11625.  * DB reading.
  11626.  * skill_db.txt
  11627.  * skill_require_db.txt
  11628.  * skill_cast_db.txt
  11629.  * skill_castnodex_db.txt
  11630.  * skill_nocast_db.txt
  11631.  * skill_unit_db.txt
  11632.  * produce_db.txt
  11633.  * create_arrow_db.txt
  11634.  * abra_db.txt
  11635.  *------------------------------------------*/
  11636.  
  11637. static bool skill_parse_row_skilldb(char* split[], int columns, int current)
  11638. {// id,range,hit,inf,element,nk,splash,max,list_num,castcancel,cast_defence_rate,inf2,maxcount,skill_type,blow_count,name,description
  11639. 	int id = atoi(split[0]);
  11640. 	int i;
  11641. 	if( (id >= GD_SKILLRANGEMIN && id <= GD_SKILLRANGEMAX)
  11642. 	||  (id >= HM_SKILLRANGEMIN && id <= HM_SKILLRANGEMAX)
  11643. 	||  (id >= MC_SKILLRANGEMIN && id <= MC_SKILLRANGEMAX) )
  11644. 	{
  11645. 		ShowWarning("skill_parse_row_skilldb: Skill id %d is forbidden (interferes with guild/homun/mercenary skill mapping)!\n", id);
  11646. 		return false;
  11647. 	}
  11648.  
  11649. 	i = skill_get_index(id);
  11650. 	if( !i ) // invalid skill id
  11651. 		return false;
  11652.  
  11653. 	skill_split_atoi(split[1],skill_db[i].range);
  11654. 	skill_db[i].hit = atoi(split[2]);
  11655. 	skill_db[i].inf = atoi(split[3]);
  11656. 	skill_split_atoi(split[4],skill_db[i].element);
  11657. 	skill_db[i].nk = (int)strtol(split[5], NULL, 0);
  11658. 	skill_split_atoi(split[6],skill_db[i].splash);
  11659. 	skill_db[i].max = atoi(split[7]);
  11660. 	skill_split_atoi(split[8],skill_db[i].num);
  11661.  
  11662. 	if( strcmpi(split[9],"yes") == 0 )
  11663. 		skill_db[i].castcancel = 1;
  11664. 	else
  11665. 		skill_db[i].castcancel = 0;
  11666. 	skill_db[i].cast_def_rate = atoi(split[10]);
  11667. 	skill_db[i].inf2 = (int)strtol(split[11], NULL, 0);
  11668. 	skill_split_atoi(split[12],skill_db[i].maxcount);
  11669. 	if( strcmpi(split[13],"weapon") == 0 )
  11670. 		skill_db[i].skill_type = BF_WEAPON;
  11671. 	else if( strcmpi(split[13],"magic") == 0 )
  11672. 		skill_db[i].skill_type = BF_MAGIC;
  11673. 	else if( strcmpi(split[13],"misc") == 0 )
  11674. 		skill_db[i].skill_type = BF_MISC;
  11675. 	else
  11676. 		skill_db[i].skill_type = 0;
  11677. 	skill_split_atoi(split[14],skill_db[i].blewcount);
  11678. 	safestrncpy(skill_db[i].name, trim(split[15]), sizeof(skill_db[i].name));
  11679. 	safestrncpy(skill_db[i].desc, trim(split[16]), sizeof(skill_db[i].desc));
  11680. 	strdb_put(skilldb_name2id, skill_db[i].name, (void*)id);
  11681.  
  11682. 	return true;
  11683. }
  11684.  
  11685. static bool skill_parse_row_requiredb(char* split[], int columns, int current)
  11686. {// SkillID,HPCost,MaxHPTrigger,SPCost,HPRateCost,SPRateCost,ZenyCost,RequiredWeapons,RequiredAmmoTypes,RequiredAmmoAmount,RequiredState,SpiritSphereCost,RequiredItemID1,RequiredItemAmount1,RequiredItemID2,RequiredItemAmount2,RequiredItemID3,RequiredItemAmount3,RequiredItemID4,RequiredItemAmount4,RequiredItemID5,RequiredItemAmount5,RequiredItemID6,RequiredItemAmount6,RequiredItemID7,RequiredItemAmount7,RequiredItemID8,RequiredItemAmount8,RequiredItemID9,RequiredItemAmount9,RequiredItemID10,RequiredItemAmount10
  11687. 	char* p;
  11688. 	int j;
  11689.  
  11690. 	int i = atoi(split[0]);
  11691. 	i = skill_get_index(i);
  11692. 	if( !i ) // invalid skill id
  11693. 		return false;
  11694.  
  11695. 	skill_split_atoi(split[1],skill_db[i].hp);
  11696. 	skill_split_atoi(split[2],skill_db[i].mhp);
  11697. 	skill_split_atoi(split[3],skill_db[i].sp);
  11698. 	skill_split_atoi(split[4],skill_db[i].hp_rate);
  11699. 	skill_split_atoi(split[5],skill_db[i].sp_rate);
  11700. 	skill_split_atoi(split[6],skill_db[i].zeny);
  11701.  
  11702. 	//FIXME: document this
  11703. 	p = split[7];
  11704. 	for( j = 0; j < 32; j++ )
  11705. 	{
  11706. 		int l = atoi(p);
  11707. 		if( l == 99 ) // Any weapon
  11708. 		{
  11709. 			skill_db[i].weapon = 0;
  11710. 			break;
  11711. 		}
  11712. 		else
  11713. 			skill_db[i].weapon |= 1<<l;
  11714. 		p = strchr(p,':');
  11715. 		if(!p)
  11716. 			break;
  11717. 		p++;
  11718. 	}
  11719.  
  11720. 	//FIXME: document this
  11721. 	p = split[8];
  11722. 	for( j = 0; j < 32; j++ )
  11723. 	{
  11724. 		int l = atoi(p);
  11725. 		if( l == 99 ) // Any ammo type
  11726. 		{
  11727. 			skill_db[i].ammo = 0xFFFFFFFF;
  11728. 			break;
  11729. 		}
  11730. 		else if( l ) // 0 stands for no requirement
  11731. 			skill_db[i].ammo |= 1<<l;
  11732. 		p = strchr(p,':');
  11733. 		if( !p )
  11734. 			break;
  11735. 		p++;
  11736. 	}
  11737. 	skill_split_atoi(split[9],skill_db[i].ammo_qty);
  11738.  
  11739. 	if(      strcmpi(split[10],"hiding")==0 ) skill_db[i].state = ST_HIDING;
  11740. 	else if( strcmpi(split[10],"cloaking")==0 ) skill_db[i].state = ST_CLOAKING;
  11741. 	else if( strcmpi(split[10],"hidden")==0 ) skill_db[i].state = ST_HIDDEN;
  11742. 	else if( strcmpi(split[10],"riding")==0 ) skill_db[i].state = ST_RIDING;
  11743. 	else if( strcmpi(split[10],"falcon")==0 ) skill_db[i].state = ST_FALCON;
  11744. 	else if( strcmpi(split[10],"cart")==0 ) skill_db[i].state = ST_CART;
  11745. 	else if( strcmpi(split[10],"shield")==0 ) skill_db[i].state = ST_SHIELD;
  11746. 	else if( strcmpi(split[10],"sight")==0 ) skill_db[i].state = ST_SIGHT;
  11747. 	else if( strcmpi(split[10],"explosionspirits")==0 ) skill_db[i].state = ST_EXPLOSIONSPIRITS;
  11748. 	else if( strcmpi(split[10],"cartboost")==0 ) skill_db[i].state = ST_CARTBOOST;
  11749. 	else if( strcmpi(split[10],"recover_weight_rate")==0 ) skill_db[i].state = ST_RECOV_WEIGHT_RATE;
  11750. 	else if( strcmpi(split[10],"move_enable")==0 ) skill_db[i].state = ST_MOVE_ENABLE;
  11751. 	else if( strcmpi(split[10],"water")==0 ) skill_db[i].state = ST_WATER;
  11752. 	else skill_db[i].state = ST_NONE;
  11753.  
  11754. 	skill_split_atoi(split[11],skill_db[i].spiritball);
  11755. 	for( j = 0; j < MAX_SKILL_ITEM_REQUIRE; j++ ) {
  11756. 		skill_db[i].itemid[j] = atoi(split[12+ 2*j]);
  11757. 		skill_db[i].amount[j] = atoi(split[13+ 2*j]);
  11758. 	}
  11759.  
  11760. 	return true;
  11761. }
  11762.  
  11763. static bool skill_parse_row_castdb(char* split[], int columns, int current)
  11764. {// SkillID,CastingTime,AfterCastActDelay,AfterCastWalkDelay,Duration1,Duration2
  11765. 	int i = atoi(split[0]);
  11766. 	i = skill_get_index(i);
  11767. 	if( !i ) // invalid skill id
  11768. 		return false;
  11769.  
  11770. 	skill_split_atoi(split[1],skill_db[i].cast);
  11771. 	skill_split_atoi(split[2],skill_db[i].delay);
  11772. 	skill_split_atoi(split[3],skill_db[i].walkdelay);
  11773. 	skill_split_atoi(split[4],skill_db[i].upkeep_time);
  11774. 	skill_split_atoi(split[5],skill_db[i].upkeep_time2);
  11775.  
  11776. 	return true;
  11777. }
  11778.  
  11779. static bool skill_parse_row_castnodexdb(char* split[], int columns, int current)
  11780. {// Skill id,Cast,Delay (optional)
  11781. 	int i = atoi(split[0]);
  11782. 	i = skill_get_index(i);
  11783. 	if( !i ) // invalid skill id
  11784. 		return false;
  11785.  
  11786. 	skill_split_atoi(split[1],skill_db[i].castnodex);
  11787. 	if( split[2] ) // optional column
  11788. 		skill_split_atoi(split[2],skill_db[i].delaynodex);
  11789.  
  11790. 	return true;
  11791. }
  11792.  
  11793. static bool skill_parse_row_nocastdb(char* split[], int columns, int current)
  11794. {// SkillID,Flag
  11795. 	int i = atoi(split[0]);
  11796. 	i = skill_get_index(i);
  11797. 	if( !i ) // invalid skill id
  11798. 		return false;
  11799.  
  11800. 	skill_db[i].nocast |= atoi(split[1]);
  11801.  
  11802. 	return true;
  11803. }
  11804.  
  11805. static bool skill_parse_row_unitdb(char* split[], int columns, int current)
  11806. {// ID,unit ID,unit ID 2,layout,range,interval,target,flag
  11807. 	int i = atoi(split[0]);
  11808. 	i = skill_get_index(i);
  11809. 	if( !i ) // invalid skill id
  11810. 		return false;
  11811.  
  11812. 	skill_db[i].unit_id[0] = strtol(split[1],NULL,16);
  11813. 	skill_db[i].unit_id[1] = strtol(split[2],NULL,16);
  11814. 	skill_split_atoi(split[3],skill_db[i].unit_layout_type);
  11815. 	skill_split_atoi(split[4],skill_db[i].unit_range);
  11816. 	skill_db[i].unit_interval = atoi(split[5]);
  11817.  
  11818. 	if( strcmpi(split[6],"noenemy")==0 ) skill_db[i].unit_target = BCT_NOENEMY;
  11819. 	else if( strcmpi(split[6],"friend")==0 ) skill_db[i].unit_target = BCT_NOENEMY;
  11820. 	else if( strcmpi(split[6],"party")==0 ) skill_db[i].unit_target = BCT_PARTY;
  11821. 	else if( strcmpi(split[6],"ally")==0 ) skill_db[i].unit_target = BCT_PARTY|BCT_GUILD;
  11822. 	else if( strcmpi(split[6],"all")==0 ) skill_db[i].unit_target = BCT_ALL;
  11823. 	else if( strcmpi(split[6],"enemy")==0 ) skill_db[i].unit_target = BCT_ENEMY;
  11824. 	else if( strcmpi(split[6],"self")==0 ) skill_db[i].unit_target = BCT_SELF;
  11825. 	else if( strcmpi(split[6],"noone")==0 ) skill_db[i].unit_target = BCT_NOONE;
  11826. 	else skill_db[i].unit_target = strtol(split[6],NULL,16);
  11827.  
  11828. 	skill_db[i].unit_flag = strtol(split[7],NULL,16);
  11829.  
  11830. 	if (skill_db[i].unit_flag&UF_DEFNOTENEMY && battle_config.defnotenemy)
  11831. 		skill_db[i].unit_target = BCT_NOENEMY;
  11832.  
  11833. 	//By default, target just characters.
  11834. 	skill_db[i].unit_target |= BL_CHAR;
  11835. 	if (skill_db[i].unit_flag&UF_NOPC)
  11836. 		skill_db[i].unit_target &= ~BL_PC;
  11837. 	if (skill_db[i].unit_flag&UF_NOMOB)
  11838. 		skill_db[i].unit_target &= ~BL_MOB;
  11839. 	if (skill_db[i].unit_flag&UF_SKILL)
  11840. 		skill_db[i].unit_target |= BL_SKILL;
  11841.  
  11842. 	return true;
  11843. }
  11844.  
  11845. static bool skill_parse_row_producedb(char* split[], int columns, int current)
  11846. {// ProduceItemID,ItemLV,RequireSkill,RequireSkillLv,MaterialID1,MaterialAmount1,......
  11847. 	int x,y;
  11848.  
  11849. 	int i = atoi(split[0]);
  11850. 	if( !i )
  11851. 		return false;
  11852.  
  11853. 	skill_produce_db[current].nameid = i;
  11854. 	skill_produce_db[current].itemlv = atoi(split[1]);
  11855. 	skill_produce_db[current].req_skill = atoi(split[2]);
  11856. 	skill_produce_db[current].req_skill_lv = atoi(split[3]);
  11857.  
  11858. 	for( x = 4, y = 0; x+1 < columns && split[x] && split[x+1] && y < MAX_PRODUCE_RESOURCE; x += 2, y++ )
  11859. 	{
  11860. 		skill_produce_db[current].mat_id[y] = atoi(split[x]);
  11861. 		skill_produce_db[current].mat_amount[y] = atoi(split[x+1]);
  11862. 	}
  11863.  
  11864. 	return true;
  11865. }
  11866.  
  11867. static bool skill_parse_row_createarrowdb(char* split[], int columns, int current)
  11868. {// SourceID,MakeID1,MakeAmount1,...,MakeID5,MakeAmount5
  11869. 	int x,y;
  11870.  
  11871. 	int i = atoi(split[0]);
  11872. 	if( !i )
  11873. 		return false;
  11874.  
  11875. 	skill_arrow_db[current].nameid = i;
  11876.  
  11877. 	for( x = 1, y = 0; x+1 < columns && split[x] && split[x+1] && y < MAX_ARROW_RESOURCE; x += 2, y++ )
  11878. 	{
  11879. 		skill_arrow_db[current].cre_id[y] = atoi(split[x]);
  11880. 		skill_arrow_db[current].cre_amount[y] = atoi(split[x+1]);
  11881. 	}
  11882.  
  11883. 	return true;
  11884. }
  11885.  
  11886. static bool skill_parse_row_abradb(char* split[], int columns, int current)
  11887. {// SkillID,DummyName,RequiredHocusPocusLevel,Rate
  11888. 	int i = atoi(split[0]);
  11889. 	if( !skill_get_index(i) || !skill_get_max(i) )
  11890. 	{
  11891. 		ShowError("abra_db: Invalid skill ID %d\n", i);
  11892. 		return false;
  11893. 	}
  11894. 	if ( !skill_get_inf(i) )
  11895. 	{
  11896. 		ShowError("abra_db: Passive skills cannot be casted (%d/%s)\n", i, skill_get_name(i));
  11897. 		return false;
  11898. 	}
  11899.  
  11900. 	skill_abra_db[current].skillid = i;
  11901. 	skill_abra_db[current].req_lv = atoi(split[2]);
  11902. 	skill_abra_db[current].per = atoi(split[3]);
  11903.  
  11904. 	return true;
  11905. }
  11906.  
  11907. static void skill_readdb(void)
  11908. {
  11909. 	// init skill db structures
  11910. 	db_clear(skilldb_name2id);
  11911. 	memset(skill_db,0,sizeof(skill_db));
  11912. 	memset(skill_produce_db,0,sizeof(skill_produce_db));
  11913. 	memset(skill_arrow_db,0,sizeof(skill_arrow_db));
  11914. 	memset(skill_abra_db,0,sizeof(skill_abra_db));
  11915.  
  11916. 	// load skill databases
  11917. 	safestrncpy(skill_db[0].name, "UNKNOWN_SKILL", sizeof(skill_db[0].name));
  11918. 	safestrncpy(skill_db[0].desc, "Unknown Skill", sizeof(skill_db[0].desc));
  11919. 	sv_readdb(db_path, "skill_db.txt"          , ',',  17, 17, MAX_SKILL_DB, skill_parse_row_skilldb);
  11920. 	sv_readdb(db_path, "skill_require_db.txt"  , ',',  32, 32, MAX_SKILL_DB, skill_parse_row_requiredb);
  11921. 	sv_readdb(db_path, "skill_cast_db.txt"     , ',',   6,  6, MAX_SKILL_DB, skill_parse_row_castdb);
  11922. 	sv_readdb(db_path, "skill_castnodex_db.txt", ',',   2,  3, MAX_SKILL_DB, skill_parse_row_castnodexdb);
  11923. 	sv_readdb(db_path, "skill_nocast_db.txt"   , ',',   2,  2, MAX_SKILL_DB, skill_parse_row_nocastdb);
  11924. 	sv_readdb(db_path, "skill_unit_db.txt"     , ',',   8,  8, MAX_SKILL_DB, skill_parse_row_unitdb);
  11925. 	skill_init_unit_layout();
  11926. 	sv_readdb(db_path, "produce_db.txt"        , ',',   4,  4+2*MAX_PRODUCE_RESOURCE, MAX_SKILL_PRODUCE_DB, skill_parse_row_producedb);
  11927. 	sv_readdb(db_path, "create_arrow_db.txt"   , ',', 1+2,  1+2*MAX_ARROW_RESOURCE, MAX_SKILL_ARROW_DB, skill_parse_row_createarrowdb);
  11928. 	sv_readdb(db_path, "abra_db.txt"           , ',',   4,  4, MAX_SKILL_ABRA_DB, skill_parse_row_abradb);
  11929. }
  11930.  
  11931. void skill_reload (void)
  11932. {
  11933. 	skill_readdb();
  11934. }
  11935.  
  11936. /*==========================================
  11937.  *
  11938.  *------------------------------------------*/
  11939. int do_init_skill (void)
  11940. {
  11941. 	skilldb_name2id = strdb_alloc(DB_OPT_DUP_KEY, 0);
  11942. 	skill_readdb();
  11943.  
  11944. 	group_db = idb_alloc(DB_OPT_BASE);
  11945. 	skillunit_db = idb_alloc(DB_OPT_BASE);
  11946. 	skill_unit_ers = ers_new(sizeof(struct skill_unit_group));
  11947. 	skill_timer_ers  = ers_new(sizeof(struct skill_timerskill));
  11948.  
  11949. 	add_timer_func_list(skill_unit_timer,"skill_unit_timer");
  11950. 	add_timer_func_list(skill_castend_id,"skill_castend_id");
  11951. 	add_timer_func_list(skill_castend_pos,"skill_castend_pos");
  11952. 	add_timer_func_list(skill_timerskill,"skill_timerskill");
  11953. 	add_timer_func_list(skill_blockpc_end, "skill_blockpc_end");
  11954.  
  11955. 	add_timer_interval(gettick()+SKILLUNITTIMER_INTERVAL,skill_unit_timer,0,0,SKILLUNITTIMER_INTERVAL);
  11956.  
  11957. 	return 0;
  11958. }
  11959.  
  11960. int do_final_skill(void)
  11961. {
  11962. 	db_destroy(skilldb_name2id);
  11963. 	db_destroy(group_db);
  11964. 	db_destroy(skillunit_db);
  11965. 	ers_destroy(skill_unit_ers);
  11966. 	ers_destroy(skill_timer_ers);
  11967. 	return 0;
  11968. }
Viewed 240 times, submitted by unknown.