viewing paste skill.c | 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. 		x=src->x;
  2657. 		y=src->y;
  2658. 		//unit_movepos(src, bl->x, bl->y, 1, 1);
  2659. 		//clif_slide(src,bl->x,bl->y);
  2660. 		//unit_movepos(bl, x, y, 1, 1);
  2661. 		//clif_slide(bl,x,y);
  2662. 		unit_warp(src,src->m,bl->x,bl->x,CLR_TELEPORT);
  2663. 		unit_warp(bl,bl->m,x,y,CLR_TELEPORT);
  2664. 		clif_specialeffect(bl,34,AREA);
  2665. 		clif_specialeffect(src,34,AREA);
  2666. 	}
  2667. 	break;
  2668. 	case RG_BACKSTAP:
  2669. 		{
  2670. 			int dir = map_calc_dir(src, bl->x, bl->y), t_dir = unit_getdir(bl);
  2671. 			if ((!check_distance_bl(src, bl, 0) && !map_check_dir(dir, t_dir)) || bl->type == BL_SKILL) {
  2672. 				status_change_end(src, SC_HIDING, INVALID_TIMER);
  2673. 				skill_attack(BF_WEAPON, src, src, bl, skillid, skilllv, tick, flag);
  2674. 				dir = dir < 4 ? dir+4 : dir-4; // change direction [Celest]
  2675. 				unit_setdir(bl,dir);
  2676. 			}
  2677. 			else if (sd)
  2678. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  2679. 		}
  2680. 		break;
  2681.  
  2682. 	case MO_FINGEROFFENSIVE:
  2683. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2684. 		if (battle_config.finger_offensive_type && sd) {
  2685. 			int i;
  2686. 			for (i = 1; i < sd->spiritball_old; i++)
  2687. 				skill_addtimerskill(src, tick + i * 200, bl->id, 0, 0, skillid, skilllv, BF_WEAPON, flag);
  2688. 		}
  2689. 		status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2690. 		break;
  2691.  
  2692. 	case MO_CHAINCOMBO:
  2693. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2694. 		status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2695. 		break;
  2696.  
  2697. 	case NJ_ISSEN:
  2698. 		status_change_end(src, SC_NEN, INVALID_TIMER);
  2699. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  2700. 		// fall through
  2701. 	case MO_EXTREMITYFIST:
  2702. 		if( skillid == MO_EXTREMITYFIST )
  2703. 		{
  2704. 			status_change_end(src, SC_EXPLOSIONSPIRITS, INVALID_TIMER);
  2705. 			status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  2706. 		}
  2707. 		//Client expects you to move to target regardless of distance
  2708. 		{
  2709. 			struct unit_data *ud = unit_bl2ud(src);
  2710. 			short dx,dy;
  2711. 			int i,speed;
  2712. 			i = skillid == MO_EXTREMITYFIST?1:2; //Move 2 cells for Issen, 1 for Asura
  2713. 			dx = bl->x - src->x;
  2714. 			dy = bl->y - src->y;
  2715. 			if (dx < 0) dx-=i;
  2716. 			else if (dx > 0) dx+=i;
  2717. 			if (dy < 0) dy-=i;
  2718. 			else if (dy > 0) dy+=i;
  2719. 			if (!dx && !dy) dy++;
  2720. 			if (map_getcell(src->m, src->x+dx, src->y+dy, CELL_CHKNOPASS))
  2721. 			{
  2722. 				dx = bl->x;
  2723. 				dy = bl->y;
  2724. 			} else {
  2725. 				dx = src->x + dx;
  2726. 				dy = src->y + dy;
  2727. 			}
  2728.  
  2729. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  2730.  
  2731. 			if(unit_walktoxy(src, dx, dy, 2) && ud) {
  2732. 				//Increase can't walk delay to not alter your walk path
  2733. 				ud->canmove_tick = tick;
  2734. 				speed = status_get_speed(src);
  2735. 				for (i = 0; i < ud->walkpath.path_len; i ++)
  2736. 				{
  2737. 					if(ud->walkpath.path[i]&1)
  2738. 						ud->canmove_tick+=7*speed/5;
  2739. 					else
  2740. 						ud->canmove_tick+=speed;
  2741. 				}
  2742. 			}
  2743. 		}
  2744. 		break;
  2745.  
  2746. 	//Splash attack skills.
  2747. 	case AS_GRIMTOOTH:
  2748. 	case MC_CARTREVOLUTION:
  2749. 	case NPC_SPLASHATTACK:
  2750. 		flag |= SD_PREAMBLE; // a fake packet will be sent for the first target to be hit
  2751. 	case AS_SPLASHER:
  2752. 	case SM_MAGNUM:
  2753. 	case MS_MAGNUM:
  2754. 	case HT_BLITZBEAT:
  2755. 	case AC_SHOWER:
  2756. 	case MA_SHOWER:
  2757. 	case MG_NAPALMBEAT:
  2758. 	case MG_FIREBALL:
  2759. 	case RG_RAID:
  2760. 	case HW_NAPALMVULCAN:
  2761. 	case NJ_HUUMA:
  2762. 	case NJ_BAKUENRYU:
  2763. 	case ASC_METEORASSAULT:
  2764. 	case GS_DESPERADO:
  2765. 	case GS_SPREADATTACK:
  2766. 	case NPC_EARTHQUAKE:
  2767. 	case NPC_PULSESTRIKE:
  2768. 	case NPC_HELLJUDGEMENT:
  2769. 	case NPC_VAMPIRE_GIFT:
  2770. 		if( flag&1 )
  2771. 		{	//Recursive invocation
  2772. 			// skill_area_temp[0] holds number of targets in area
  2773. 			// skill_area_temp[1] holds the id of the original target
  2774. 			// skill_area_temp[2] counts how many targets have already been processed
  2775. 			int sflag = skill_area_temp[0] & 0xFFF, heal;
  2776. 			if( flag&SD_LEVEL )
  2777. 				sflag |= SD_LEVEL; // -1 will be used in packets instead of the skill level
  2778. 			if( skill_area_temp[1] != bl->id && !(skill_get_inf2(skillid)&INF2_NPC_SKILL) )
  2779. 				sflag |= SD_ANIMATION; // original target gets no animation (as well as all NPC skills)
  2780.  
  2781. 			heal = skill_attack(skill_get_type(skillid), src, src, bl, skillid, skilllv, tick, sflag);
  2782. 			if( skillid == NPC_VAMPIRE_GIFT && heal > 0 )
  2783. 			{
  2784. 				clif_skill_nodamage(NULL, src, AL_HEAL, heal, 1);
  2785. 				status_heal(src,heal,0,0);
  2786. 			}
  2787. 		}
  2788. 		else
  2789. 		{
  2790. 			if ( skillid == NJ_BAKUENRYU )
  2791. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  2792.  
  2793. 			skill_area_temp[0] = 0;
  2794. 			skill_area_temp[1] = bl->id;
  2795. 			skill_area_temp[2] = 0;
  2796.  
  2797. 			// if skill damage should be split among targets, count them
  2798. 			//SD_LEVEL -> Forced splash damage for Auto Blitz-Beat -> count targets
  2799. 			//special case: Venom Splasher uses a different range for searching than for splashing
  2800. 			if( flag&SD_LEVEL || skill_get_nk(skillid)&NK_SPLASHSPLIT )
  2801. 				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);
  2802.  
  2803. 			// recursive invocation of skill_castend_damage_id() with flag|1
  2804. 			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);
  2805.  
  2806. 			//FIXME: Isn't EarthQuake a ground skill after all?
  2807. 			if( skillid == NPC_EARTHQUAKE )
  2808. 				skill_addtimerskill(src,tick+250,src->id,0,0,skillid,skilllv,2,flag|BCT_ENEMY|SD_SPLASH|1);
  2809.  
  2810. 			//FIXME: move this to skill_additional_effect or some such? [ultramage]
  2811. 			if( skillid == SM_MAGNUM || skillid == MS_MAGNUM )
  2812. 			{ // Initiate 10% of your damage becomes fire element.
  2813. 				sc_start4(src,SC_WATK_ELEMENT,100,3,20,0,0,skill_get_time2(skillid, skilllv));
  2814. 				if( sd )
  2815. 					skill_blockpc_start(sd, skillid, skill_get_time(skillid, skilllv));
  2816. 				if( bl->type == BL_MER )
  2817. 					skill_blockmerc_start((TBL_MER*)bl, skillid, skill_get_time(skillid, skilllv));
  2818. 			}
  2819. 		}
  2820. 		break;
  2821.  
  2822. 	case KN_BRANDISHSPEAR:
  2823. 	case ML_BRANDISH:
  2824. 		//Coded apart for it needs the flag passed to the damage calculation.
  2825. 		if (skill_area_temp[1] != bl->id)
  2826. 			skill_attack(skill_get_type(skillid), src, src, bl, skillid, skilllv, tick, flag|SD_ANIMATION);
  2827. 		else
  2828. 			skill_attack(skill_get_type(skillid), src, src, bl, skillid, skilllv, tick, flag);
  2829. 		break;
  2830.  
  2831. 	case KN_BOWLINGBASH:
  2832. 	case MS_BOWLINGBASH:
  2833. 		if(flag&1){
  2834. 			if(bl->id==skill_area_temp[1])
  2835. 				break;
  2836. 			//two hits for 500%
  2837. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,SD_ANIMATION);
  2838. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,SD_ANIMATION);
  2839. 		} else {
  2840. 			int i,c;
  2841. 			c = skill_get_blewcount(skillid,skilllv);
  2842. 			// keep moving target in the direction that src is looking, square by square
  2843. 			for(i=0;i<c;i++){
  2844. 				if (!skill_blown(src,bl,1,(unit_getdir(src)+4)%8,0x1))
  2845. 					break; //Can't knockback
  2846. 				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);
  2847. 				if( skill_area_temp[0] > 1 ) break; // collision
  2848. 			}
  2849. 			clif_blown(bl); //Update target pos.
  2850. 			if (i!=c) { //Splash
  2851. 				skill_area_temp[1] = bl->id;
  2852. 				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);
  2853. 			}
  2854. 			//Weirdo dual-hit property, two attacks for 500%
  2855. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,0);
  2856. 			skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,0);
  2857. 		}
  2858. 		break;
  2859.  
  2860. 	case KN_SPEARSTAB:
  2861. 		if(flag&1) {
  2862. 			if (bl->id==skill_area_temp[1])
  2863. 				break;
  2864. 			if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,SD_ANIMATION))
  2865. 				skill_blown(src,bl,skill_area_temp[2],-1,0);
  2866. 		} else {
  2867. 			int x=bl->x,y=bl->y,i,dir;
  2868. 			dir = map_calc_dir(bl,src->x,src->y);
  2869. 			skill_area_temp[1] = bl->id;
  2870. 			skill_area_temp[2] = skill_get_blewcount(skillid,skilllv);
  2871. 			// all the enemies between the caster and the target are hit, as well as the target
  2872. 			if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,0))
  2873. 				skill_blown(src,bl,skill_area_temp[2],-1,0);
  2874. 			for (i=0;i<4;i++) {
  2875. 				map_foreachincell(skill_area_sub,bl->m,x,y,BL_CHAR,
  2876. 					src,skillid,skilllv,tick,flag|BCT_ENEMY|1,skill_castend_damage_id);
  2877. 				x += dirx[dir];
  2878. 				y += diry[dir];
  2879. 			}
  2880. 		}
  2881. 		break;
  2882.  
  2883. 	case TK_TURNKICK:
  2884. 	case MO_BALKYOUNG: //Active part of the attack. Skill-attack [Skotlex]
  2885. 	{
  2886. 		skill_area_temp[1] = bl->id; //NOTE: This is used in skill_castend_nodamage_id to avoid affecting the target.
  2887. 		if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag))
  2888. 			map_foreachinrange(skill_area_sub,bl,
  2889. 				skill_get_splash(skillid, skilllv),BL_CHAR,
  2890. 				src,skillid,skilllv,tick,flag|BCT_ENEMY|1,
  2891. 				skill_castend_nodamage_id);
  2892. 	}
  2893. 		break;
  2894. 	case CH_PALMSTRIKE: //	Palm Strike takes effect 1sec after casting. [Skotlex]
  2895. 	//	clif_skill_nodamage(src,bl,skillid,skilllv,0); //Can't make this one display the correct attack animation delay :/
  2896. 		clif_damage(src,bl,tick,status_get_amotion(src),0,-1,1,4,0); //Display an absorbed damage attack.
  2897. 		skill_addtimerskill(src, tick + 1000, bl->id, 0, 0, skillid, skilllv, BF_WEAPON, flag);
  2898. 		break;
  2899.  
  2900. 	case PR_TURNUNDEAD:
  2901. 	case ALL_RESURRECTION:
  2902. 		if (!battle_check_undead(tstatus->race, tstatus->def_ele))
  2903. 			break;
  2904. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2905. 		break;
  2906.  
  2907. 	case MG_SOULSTRIKE:
  2908. 	case NPC_DARKSTRIKE:
  2909. 	case MG_COLDBOLT:
  2910. 	case MG_FIREBOLT:
  2911. 	case MG_LIGHTNINGBOLT:
  2912. 	case WZ_EARTHSPIKE:
  2913. 	case AL_HEAL:
  2914. 	case AL_HOLYLIGHT:
  2915. 	case WZ_JUPITEL:
  2916. 	case NPC_DARKTHUNDER:
  2917. 	case PR_ASPERSIO:
  2918. 	case MG_FROSTDIVER:
  2919. 	case WZ_SIGHTBLASTER:
  2920. 	case WZ_SIGHTRASHER:
  2921. 	case NJ_KOUENKA:
  2922. 	case NJ_HYOUSENSOU:
  2923. 	case NJ_HUUJIN:
  2924. 	skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2925. 		break;
  2926. 	/*case WZ_FIREIVY: //  extra skill ( for fun dota style )
  2927. 		{
  2928. 			int x, y;
  2929. 			x = src->x; // Get current postion of player
  2930. 			y = src->y; 
  2931.  
  2932. 			if( unit_movepos(src,bl->x,bl->y,0,0) )
  2933. 			{
  2934. 				unit_setdir(src, map_calc_dir(src, bl->x, bl->y));
  2935. 				clif_skill_nodamage(src,src,skillid,skilllv,1); 
  2936. 				clif_slide(src, bl->x, bl->y) ; // player swap to target pos
  2937.  
  2938. 				if( unit_movepos(bl,x,y,0,0) )
  2939. 				{
  2940. 					clif_skill_nodamage(bl,bl,skillid,skilllv,1); 
  2941. 					clif_slide(bl, x, y) ; // target swap to player pos
  2942. 				}
  2943. 			}
  2944. 		}
  2945. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2946. 		break;*/
  2947.  
  2948. 	case NPC_MAGICALATTACK:
  2949. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2950. 		sc_start(src,status_skill2sc(skillid),100,skilllv,skill_get_time(skillid,skilllv));
  2951. 		break;
  2952.  
  2953. 	case HVAN_CAPRICE: //[blackhole89]
  2954. 		{
  2955. 			int ran=rand()%4;
  2956. 			int sid = 0;
  2957. 			switch(ran)
  2958. 			{
  2959. 			case 0: sid=MG_COLDBOLT; break;
  2960. 			case 1: sid=MG_FIREBOLT; break;
  2961. 			case 2: sid=MG_LIGHTNINGBOLT; break;
  2962. 			case 3: sid=WZ_EARTHSPIKE; break;
  2963. 			}
  2964. 			skill_attack(BF_MAGIC,src,src,bl,sid,skilllv,tick,flag|SD_LEVEL);
  2965. 		}
  2966. 		break;
  2967. 	case WZ_WATERBALL:
  2968. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  2969. 		{
  2970. 			int range = skilllv / 2;
  2971. 			int maxlv = skill_get_max(skillid); // learnable level
  2972. 			int count = 0;
  2973. 			int x, y;
  2974. 			struct skill_unit* unit;
  2975.  
  2976. 			if( skilllv > maxlv )
  2977. 			{
  2978. 				if( src->type == BL_MOB && skilllv == 10 )
  2979. 					range = 4;
  2980. 				else
  2981. 					range = maxlv / 2;
  2982. 			}
  2983.  
  2984. 			for( y = src->y - range; y <= src->y + range; ++y )
  2985. 				for( x = src->x - range; x <= src->x + range; ++x )
  2986. 				{
  2987. 					if( !map_find_skill_unit_oncell(src,x,y,SA_LANDPROTECTOR,NULL) )
  2988. 					{
  2989. 						if( src->type != BL_PC || map_getcell(src->m,x,y,CELL_CHKWATER) ) // non-players bypass the water requirement
  2990. 							count++; // natural water cell
  2991. 						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 )
  2992. 						{
  2993. 							count++; // skill-induced water cell
  2994. 							skill_delunit(unit); // consume cell
  2995. 						}
  2996. 					}
  2997. 				}
  2998.  
  2999. 			if( count > 1 ) // queue the remaining count - 1 timerskill Waterballs
  3000. 				skill_addtimerskill(src,tick+150,bl->id,0,0,skillid,skilllv,count-1,flag);
  3001. 		}
  3002. 		break;
  3003.  
  3004. 	case PR_BENEDICTIO:
  3005. 		//Should attack undead and demons. [Skotlex]
  3006. 		if (battle_check_undead(tstatus->race, tstatus->def_ele) || tstatus->race == RC_DEMON)
  3007. 			skill_attack(BF_MAGIC, src, src, bl, skillid, skilllv, tick, flag);
  3008. 	break;
  3009.  
  3010. 	case SL_SMA:
  3011. 		status_change_end(src, SC_SMA, INVALID_TIMER);
  3012. 	case SL_STIN:
  3013. 	case SL_STUN:
  3014. 		if (sd && !battle_config.allow_es_magic_pc && bl->type != BL_MOB) {
  3015. 			status_change_start(src,SC_STUN,10000,skilllv,0,0,0,500,10);
  3016. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3017. 			break;
  3018. 		}
  3019. 		skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  3020. 		break;
  3021.  
  3022. 	case NPC_DARKBREATH:
  3023. 		clif_emotion(src,E_AG);
  3024. 	case SN_FALCONASSAULT:
  3025. 	case PA_PRESSURE:
  3026. 	case CR_ACIDDEMONSTRATION:
  3027. 	case TF_THROWSTONE:
  3028. 	case NPC_SMOKING:
  3029. 	case GS_FLING:
  3030. 	case NJ_ZENYNAGE:
  3031. 		skill_attack(BF_MISC,src,src,bl,skillid,skilllv,tick,flag);
  3032. 		break;
  3033.  
  3034. 	case HVAN_EXPLOSION:
  3035. 	case NPC_SELFDESTRUCTION:
  3036. 		if (src != bl)
  3037. 			skill_attack(BF_MISC,src,src,bl,skillid,skilllv,tick,flag);
  3038. 		break;
  3039.  
  3040. 	// Celest
  3041. 	case PF_SOULBURN:
  3042. 		if (rand()%100 < (skilllv < 5 ? 30 + skilllv * 10 : 70)) {
  3043. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3044. 			if (skilllv == 5)
  3045. 				skill_attack(BF_MAGIC,src,src,bl,skillid,skilllv,tick,flag);
  3046. 			status_percent_damage(src, bl, 0, 100, false);
  3047. 		} else {
  3048. 			clif_skill_nodamage(src,src,skillid,skilllv,1);
  3049. 			if (skilllv == 5)
  3050. 				skill_attack(BF_MAGIC,src,src,src,skillid,skilllv,tick,flag);
  3051. 			status_percent_damage(src, src, 0, 100, false);
  3052. 		}
  3053. 		if (sd) skill_blockpc_start (sd, skillid, (skilllv < 5 ? 10000: 15000));
  3054. 		break;
  3055.  
  3056. 	case NPC_BLOODDRAIN:
  3057. 	case NPC_ENERGYDRAIN:
  3058. 		{
  3059. 			int heal = skill_attack( (skillid == NPC_BLOODDRAIN) ? BF_WEAPON : BF_MAGIC,
  3060. 					src, src, bl, skillid, skilllv, tick, flag);
  3061. 			if (heal > 0){
  3062. 				clif_skill_nodamage(NULL, src, AL_HEAL, heal, 1);
  3063. 				status_heal(src, heal, 0, 0);
  3064. 			}
  3065. 		}
  3066. 		break;
  3067.  
  3068. 	case GS_BULLSEYE:
  3069. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  3070. 		break;
  3071.  
  3072. 	case NJ_KASUMIKIRI:
  3073. 		if (skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag) > 0)
  3074. 			sc_start(src,SC_HIDING,100,skilllv,skill_get_time(skillid,skilllv));
  3075. 		break;
  3076. 	case NJ_KIRIKAGE:
  3077. 		if( !map_flag_gvg(src->m) && !map[src->m].flag.battleground )
  3078. 		{	//You don't move on GVG grounds.
  3079. 			short x, y;
  3080. 			map_search_freecell(bl, 0, &x, &y, 1, 1, 0);
  3081. 			if (unit_movepos(src, x, y, 0, 0))
  3082. 				clif_slide(src,src->x,src->y);
  3083. 		}
  3084. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  3085. 		skill_attack(BF_WEAPON,src,src,bl,skillid,skilllv,tick,flag);
  3086. 		break;
  3087. 	case 0:
  3088. 		if(sd) {
  3089. 			if (flag & 3){
  3090. 				if (bl->id != skill_area_temp[1])
  3091. 					skill_attack(BF_WEAPON, src, src, bl, skillid, skilllv, tick, SD_LEVEL|flag);
  3092. 			} else {
  3093. 				skill_area_temp[1] = bl->id;
  3094. 				map_foreachinrange(skill_area_sub, bl,
  3095. 					sd->splash_range, BL_CHAR,
  3096. 					src, skillid, skilllv, tick, flag | BCT_ENEMY | 1,
  3097. 					skill_castend_damage_id);
  3098. 				flag|=1; //Set flag to 1 so ammo is not double-consumed. [Skotlex]
  3099. 			}
  3100. 		}
  3101. 		break;
  3102.  
  3103. 	default:
  3104. 		ShowWarning("skill_castend_damage_id: Unknown skill used:%d\n",skillid);
  3105. 		clif_skill_damage(src, bl, tick, status_get_amotion(src), tstatus->dmotion,
  3106. 			0, abs(skill_get_num(skillid, skilllv)),
  3107. 			skillid, skilllv, skill_get_hit(skillid));
  3108. 		map_freeblock_unlock();
  3109. 		return 1;
  3110. 	}
  3111.  
  3112. 	map_freeblock_unlock();
  3113.  
  3114. 	if( sd && !(flag&1) )
  3115. 	{
  3116. 		if( sd->state.arrow_atk ) //Consume arrow on last invocation to this skill.
  3117. 			battle_consume_ammo(sd, skillid, skilllv);
  3118. 		skill_onskillusage(sd, bl, skillid, tick);
  3119. 		skill_consume_requirement(sd,skillid,skilllv,2);
  3120. 	}
  3121.  
  3122. 	return 0;
  3123. }
  3124.  
  3125. /*==========================================
  3126.  *
  3127.  *------------------------------------------*/
  3128. int skill_castend_nodamage_id (struct block_list *src, struct block_list *bl, int skillid, int skilllv, unsigned int tick, int flag)
  3129. {
  3130. 	struct map_session_data *sd, *dstsd;
  3131. 	struct mob_data *md, *dstmd;
  3132. 	struct homun_data *hd;
  3133. 	struct mercenary_data *mer;
  3134. 	struct status_data *sstatus, *tstatus;
  3135. 	struct status_change *tsc;
  3136. 	struct status_change_entry *tsce;
  3137.  
  3138. 	int i;
  3139. 	enum sc_type type;
  3140.  
  3141. 	if(skillid > 0 && skilllv <= 0) return 0;	// celest
  3142.  
  3143. 	nullpo_retr(1, src);
  3144. 	nullpo_retr(1, bl);
  3145.  
  3146. 	if (src->m != bl->m)
  3147. 		return 1;
  3148.  
  3149. 	sd = BL_CAST(BL_PC, src);
  3150. 	hd = BL_CAST(BL_HOM, src);
  3151. 	md = BL_CAST(BL_MOB, src);
  3152. 	mer = BL_CAST(BL_MER, src);
  3153.  
  3154. 	dstsd = BL_CAST(BL_PC, bl);
  3155. 	dstmd = BL_CAST(BL_MOB, bl);
  3156.  
  3157. 	if(bl->prev == NULL)
  3158. 		return 1;
  3159. 	if(status_isdead(src))
  3160. 		return 1;
  3161.  
  3162. 	if( src != bl && status_isdead(bl) && skillid != ALL_RESURRECTION && skillid != PR_REDEMPTIO && skillid != NPC_WIDESOULDRAIN )
  3163. 		return 1;
  3164.  
  3165. 	tstatus = status_get_status_data(bl);
  3166. 	sstatus = status_get_status_data(src);
  3167.  
  3168. 	//Check for undead skills that convert a no-damage skill into a damage one. [Skotlex]
  3169. 	switch (skillid) {
  3170. 		case HLIF_HEAL:	//[orn]
  3171. 			if (bl->type != BL_HOM) {
  3172. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0) ;
  3173. 	        break ;
  3174. 			}
  3175.  		case AL_HEAL:
  3176. 		case ALL_RESURRECTION:
  3177. 		case PR_ASPERSIO:
  3178. 			//Apparently only player casted skills can be offensive like this.
  3179. 			if (sd && battle_check_undead(tstatus->race,tstatus->def_ele)) {
  3180. 				if (battle_check_target(src, bl, BCT_ENEMY) < 1) {
  3181. 				  	//Offensive heal does not works on non-enemies. [Skotlex]
  3182. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3183. 					return 0;
  3184. 				}
  3185. 				return skill_castend_damage_id (src, bl, skillid, skilllv, tick, flag);
  3186. 			}
  3187. 			break;
  3188. 		case NPC_SMOKING: //Since it is a self skill, this one ends here rather than in damage_id. [Skotlex]
  3189. 			return skill_castend_damage_id (src, bl, skillid, skilllv, tick, flag);
  3190. 		default:
  3191. 			//Skill is actually ground placed.
  3192. 			if (src == bl && skill_get_unit_id(skillid,0))
  3193. 				return skill_castend_pos2(src,bl->x,bl->y,skillid,skilllv,tick,0);
  3194. 	}
  3195.  
  3196. 	type = status_skill2sc(skillid);
  3197. 	tsc = status_get_sc(bl);
  3198. 	tsce = (tsc && type != -1)?tsc->data[type]:NULL;
  3199.  
  3200. 	if (src!=bl && type > -1 &&
  3201. 		(i = skill_get_ele(skillid, skilllv)) > ELE_NEUTRAL &&
  3202. 		skill_get_inf(skillid) != INF_SUPPORT_SKILL &&
  3203. 		battle_attr_fix(NULL, NULL, 100, i, tstatus->def_ele, tstatus->ele_lv) <= 0)
  3204. 		return 1; //Skills that cause an status should be blocked if the target element blocks its element.
  3205.  
  3206. 	map_freeblock_lock();
  3207. 	switch(skillid)
  3208. 	{
  3209. 	case HLIF_HEAL:	//[orn]
  3210. 	case AL_HEAL:
  3211. 		{
  3212. 			int heal = skill_calc_heal(src, bl, skillid, skilllv, true);
  3213. 			int heal_get_jobexp;
  3214.  
  3215. 			if( status_isimmune(bl) || (dstmd && (dstmd->class_ == MOBID_EMPERIUM || mob_is_battleground(dstmd))) )
  3216. 				heal=0;
  3217.  
  3218. 			if( sd && dstsd && sd->status.partner_id == dstsd->status.char_id && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && sd->status.sex == 0 )
  3219. 				heal = heal*2;
  3220.  
  3221. 			if( tsc && tsc->count )
  3222. 			{
  3223. 				if( tsc->data[SC_KAITE] && !(sstatus->mode&MD_BOSS) )
  3224. 				{ //Bounce back heal
  3225. 					if (--tsc->data[SC_KAITE]->val2 <= 0)
  3226. 						status_change_end(bl, SC_KAITE, INVALID_TIMER);
  3227. 					if (src == bl)
  3228. 						heal=0; //When you try to heal yourself under Kaite, the heal is voided.
  3229. 					else {
  3230. 						bl = src;
  3231. 						dstsd = sd;
  3232. 					}
  3233. 				} else
  3234. 				if (tsc->data[SC_BERSERK])
  3235. 					heal = 0; //Needed so that it actually displays 0 when healing.
  3236. 			}
  3237. 			heal_get_jobexp = status_heal(bl,heal,0,0);
  3238. 			clif_skill_nodamage (src, bl, skillid, heal, 1);
  3239.  
  3240. 			if(sd && dstsd && heal > 0 && sd != dstsd && battle_config.heal_exp > 0){
  3241. 				heal_get_jobexp = heal_get_jobexp * battle_config.heal_exp / 100;
  3242. 				if (heal_get_jobexp <= 0)
  3243. 					heal_get_jobexp = 1;
  3244. 				pc_gainexp (sd, bl, 0, heal_get_jobexp, false);
  3245. 			}
  3246. 		}
  3247. 		break;
  3248.  
  3249. 	case WZ_FIREIVY:
  3250. 	{
  3251. 		if (battle_check_target(src, bl, BCT_ENEMY) < 1) 
  3252. 		{
  3253. 			int x=0, y=0;
  3254. 			x=src->x;
  3255. 			y=src->y;
  3256. 			/*unit_movepos(src, bl->x, bl->y, 1, 1);
  3257. 			clif_slide(src,bl->x,bl->y);
  3258. 			unit_movepos(bl, x, y, 1, 1);
  3259. 			clif_slide(bl,x,y);*/
  3260. 			unit_warp(src,src->m,bl->x,bl->x,CLR_TELEPORT);
  3261. 			unit_warp(bl,bl->m,x,y,CLR_TELEPORT);
  3262. 			clif_specialeffect(bl,34,AREA);
  3263. 			clif_specialeffect(src,34,AREA);
  3264. 		}
  3265. 		else
  3266. 		return skill_castend_damage_id (src, bl, skillid, skilllv, tick, flag);
  3267. 	}
  3268. 	break;
  3269.  
  3270. 	case PR_REDEMPTIO:
  3271. 		if (sd && !(flag&1)) {
  3272. 			if (sd->status.party_id == 0) {
  3273. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3274. 				break;
  3275. 			}
  3276. 			skill_area_temp[0] = 0;
  3277. 			party_foreachsamemap(skill_area_sub,
  3278. 				sd,skill_get_splash(skillid, skilllv),
  3279. 				src,skillid,skilllv,tick, flag|BCT_PARTY|1,
  3280. 				skill_castend_nodamage_id);
  3281. 			if (skill_area_temp[0] == 0) {
  3282. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3283. 				break;
  3284. 			}
  3285. 			skill_area_temp[0] = 5 - skill_area_temp[0]; // The actual penalty...
  3286. 			if (skill_area_temp[0] > 0 && !map[src->m].flag.noexppenalty) { //Apply penalty
  3287. 				sd->status.base_exp -= min(sd->status.base_exp, pc_nextbaseexp(sd) * skill_area_temp[0] * 2/1000); //0.2% penalty per each.
  3288. 				sd->status.job_exp -= min(sd->status.job_exp, pc_nextjobexp(sd) * skill_area_temp[0] * 2/1000);
  3289. 				clif_updatestatus(sd,SP_BASEEXP);
  3290. 				clif_updatestatus(sd,SP_JOBEXP);
  3291. 			}
  3292. 			status_set_hp(src, 1, 0);
  3293. 			status_set_sp(src, 0, 0);
  3294. 			break;
  3295. 		} else if (status_isdead(bl) && flag&1) { //Revive
  3296. 			skill_area_temp[0]++; //Count it in, then fall-through to the Resurrection code.
  3297. 			skilllv = 3; //Resurrection level 3 is used
  3298. 		} else //Invalid target, skip resurrection.
  3299. 			break;
  3300.  
  3301. 	case ALL_RESURRECTION:
  3302. 		if(sd && (map_flag_gvg(bl->m) || map[bl->m].flag.battleground))
  3303. 		{	//No reviving in WoE grounds!
  3304. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3305. 			break;
  3306. 		}
  3307. 		if (!status_isdead(bl))
  3308. 			break;
  3309. 		{
  3310. 			int per = 0, sper = 0;
  3311. 			if (tsc && tsc->data[SC_HELLPOWER])
  3312. 				break;
  3313.  
  3314. 			if (map[bl->m].flag.pvp && dstsd && dstsd->pvp_point < 0)
  3315. 				break;
  3316.  
  3317. 			switch(skilllv){
  3318. 			case 1: per=10; break;
  3319. 			case 2: per=30; break;
  3320. 			case 3: per=50; break;
  3321. 			case 4: per=80; break;
  3322. 			}
  3323. 			if(dstsd && dstsd->special_state.restart_full_recover)
  3324. 				per = sper = 100;
  3325. 			if (status_revive(bl, per, sper))
  3326. 			{
  3327. 				clif_skill_nodamage(src,bl,ALL_RESURRECTION,skilllv,1); //Both Redemptio and Res show this skill-animation.
  3328. 				if(sd && dstsd && battle_config.resurrection_exp > 0)
  3329. 				{
  3330. 					int exp = 0,jexp = 0;
  3331. 					int lv = dstsd->status.base_level - sd->status.base_level, jlv = dstsd->status.job_level - sd->status.job_level;
  3332. 					if(lv > 0 && pc_nextbaseexp(dstsd)) {
  3333. 						exp = (int)((double)dstsd->status.base_exp * (double)lv * (double)battle_config.resurrection_exp / 1000000.);
  3334. 						if (exp < 1) exp = 1;
  3335. 					}
  3336. 					if(jlv > 0 && pc_nextjobexp(dstsd)) {
  3337. 						jexp = (int)((double)dstsd->status.job_exp * (double)lv * (double)battle_config.resurrection_exp / 1000000.);
  3338. 						if (jexp < 1) jexp = 1;
  3339. 					}
  3340. 					if(exp > 0 || jexp > 0)
  3341. 						pc_gainexp (sd, bl, exp, jexp, false);
  3342. 				}
  3343. 			}
  3344. 		}
  3345. 		break;
  3346.  
  3347. 	case AL_DECAGI:
  3348. 	case MER_DECAGI:
  3349. 		clif_skill_nodamage (src, bl, skillid, skilllv,
  3350. 			sc_start(bl, type, (40 + skilllv * 2 + (status_get_lv(src) + sstatus->int_)/5), skilllv, skill_get_time(skillid,skilllv)));
  3351. 		break;
  3352.  
  3353. 	case AL_CRUCIS:
  3354. 		if (flag&1)
  3355. 			sc_start(bl,type, 23+skilllv*4 +status_get_lv(src) -status_get_lv(bl), skilllv,skill_get_time(skillid,skilllv));
  3356. 		else {
  3357. 			map_foreachinrange(skill_area_sub, src, skill_get_splash(skillid, skilllv), BL_CHAR,
  3358. 				src, skillid, skilllv, tick, flag|BCT_ENEMY|1, skill_castend_nodamage_id);
  3359. 			clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  3360. 		}
  3361. 		break;
  3362.  
  3363. 	case PR_LEXDIVINA:
  3364. 	case MER_LEXDIVINA:
  3365. 		if( tsce )
  3366. 			status_change_end(bl,type, INVALID_TIMER);
  3367. 		else
  3368. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  3369. 		clif_skill_nodamage (src, bl, skillid, skilllv, 1);
  3370. 		break;
  3371.  
  3372. 	case SA_ABRACADABRA:
  3373. 		{
  3374. 			int abra_skillid = 0, abra_skilllv;
  3375. 			do {
  3376. 				i = rand() % MAX_SKILL_ABRA_DB;
  3377. 				abra_skillid = skill_abra_db[i].skillid;
  3378. 			} while (abra_skillid == 0 ||
  3379. 				skill_abra_db[i].req_lv > skilllv || //Required lv for it to appear
  3380. 				rand()%10000 >= skill_abra_db[i].per
  3381. 			);
  3382. 			abra_skilllv = min(skilllv, skill_get_max(abra_skillid));
  3383. 			clif_skill_nodamage (src, bl, skillid, skilllv, 1);
  3384.  
  3385. 			if( sd )
  3386. 			{// player-casted
  3387. 				sd->state.abra_flag = 1;
  3388. 				sd->skillitem = abra_skillid;
  3389. 				sd->skillitemlv = abra_skilllv;
  3390. 				clif_item_skill(sd, abra_skillid, abra_skilllv);
  3391. 			}
  3392. 			else
  3393. 			{// mob-casted
  3394. 				struct unit_data *ud = unit_bl2ud(src);
  3395. 				int inf = skill_get_inf(abra_skillid);
  3396. 				int target_id = 0;
  3397. 				if (!ud) break;
  3398. 				if (inf&INF_SELF_SKILL || inf&INF_SUPPORT_SKILL) {
  3399. 					if (src->type == BL_PET)
  3400. 						bl = (struct block_list*)((TBL_PET*)src)->msd;
  3401. 					if (!bl) bl = src;
  3402. 					unit_skilluse_id(src, bl->id, abra_skillid, abra_skilllv);
  3403. 				} else {	//Assume offensive skills
  3404. 					if (ud->target)
  3405. 						target_id = ud->target;
  3406. 					else switch (src->type) {
  3407. 						case BL_MOB: target_id = ((TBL_MOB*)src)->target_id; break;
  3408. 						case BL_PET: target_id = ((TBL_PET*)src)->target_id; break;
  3409. 					}
  3410. 					if (!target_id)
  3411. 						break;
  3412. 					if (skill_get_casttype(abra_skillid) == CAST_GROUND) {
  3413. 						bl = map_id2bl(target_id);
  3414. 						if (!bl) bl = src;
  3415. 						unit_skilluse_pos(src, bl->x, bl->y, abra_skillid, abra_skilllv);
  3416. 					} else
  3417. 						unit_skilluse_id(src, target_id, abra_skillid, abra_skilllv);
  3418. 				}
  3419. 			}
  3420. 		}
  3421. 		break;
  3422.  
  3423. 	case SA_COMA:
  3424. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3425. 			sc_start(bl,type,100,skilllv,skill_get_time2(skillid,skilllv)));
  3426. 		break;
  3427. 	case SA_FULLRECOVERY:
  3428. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3429. 		if (status_isimmune(bl))
  3430. 			break;
  3431. 		status_percent_heal(bl, 100, 100);
  3432. 		break;
  3433. 	case NPC_ALLHEAL:
  3434. 		{
  3435. 			int heal;
  3436. 			if( status_isimmune(bl) )
  3437. 				break;
  3438. 			heal = status_percent_heal(bl, 100, 0);
  3439. 			clif_skill_nodamage(NULL, bl, AL_HEAL, heal, 1);
  3440. 			if( dstmd )
  3441. 			{ // Reset Damage Logs
  3442. 				memset(dstmd->dmglog, 0, sizeof(dstmd->dmglog));
  3443. 				dstmd->tdmg = 0;
  3444. 			}
  3445. 		}
  3446. 		break;
  3447. 	case SA_SUMMONMONSTER:
  3448. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3449. 		if (sd) mob_once_spawn(sd,src->m,src->x,src->y,"--ja--",-1,1,"");
  3450. 		break;
  3451. 	case SA_LEVELUP:
  3452. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3453. 		if (sd && pc_nextbaseexp(sd)) pc_gainexp(sd, NULL, pc_nextbaseexp(sd) * 10 / 100, 0, false);
  3454. 		break;
  3455. 	case SA_INSTANTDEATH:
  3456. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3457. 		status_set_hp(bl,1,0);
  3458. 		break;
  3459. 	case SA_QUESTION:
  3460. 	case SA_GRAVITY:
  3461. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3462. 		break;
  3463. 	case SA_CLASSCHANGE:
  3464. 	case SA_MONOCELL:
  3465. 		if (dstmd)
  3466. 		{
  3467. 			int class_;
  3468. 			if ( sd && dstmd->status.mode&MD_BOSS )
  3469. 			{
  3470. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3471. 				break;
  3472. 			}
  3473. 			class_ = skillid==SA_MONOCELL?1002:mob_get_random_id(2, 1, 0);
  3474. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3475. 			mob_class_change(dstmd,class_);
  3476. 			if( tsc && dstmd->status.mode&MD_BOSS )
  3477. 			{
  3478. 				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 };
  3479. 				for (i = SC_COMMON_MIN; i <= SC_COMMON_MAX; i++)
  3480. 					if (tsc->data[i]) status_change_end(bl, (sc_type)i, INVALID_TIMER);
  3481. 				for (i = 0; i < ARRAYLENGTH(scs); i++)
  3482. 					if (tsc->data[scs[i]]) status_change_end(bl, scs[i], INVALID_TIMER);
  3483. 			}
  3484. 		}
  3485. 		break;
  3486. 	case SA_DEATH:
  3487. 		if ( sd && dstmd && dstmd->status.mode&MD_BOSS )
  3488. 		{
  3489. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3490. 			break;
  3491. 		}
  3492. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3493. 		status_kill(bl);
  3494. 		break;
  3495. 	case SA_REVERSEORCISH:
  3496. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3497. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv)));
  3498. 		break;
  3499. 	case SA_FORTUNE:
  3500. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3501. 		if(sd) pc_getzeny(sd,status_get_lv(bl)*100);
  3502. 		break;
  3503. 	case SA_TAMINGMONSTER:
  3504. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3505. 		if (sd && dstmd) {
  3506. 			ARR_FIND( 0, MAX_PET_DB, i, dstmd->class_ == pet_db[i].class_ );
  3507. 			if( i < MAX_PET_DB )
  3508. 				pet_catch_process1(sd, dstmd->class_);
  3509. 		}
  3510. 		break;
  3511.  
  3512. 	case CR_PROVIDENCE:
  3513. 		if(sd && dstsd){ //Check they are not another crusader [Skotlex]
  3514. 			if ((dstsd->class_&MAPID_UPPERMASK) == MAPID_CRUSADER) {
  3515. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3516. 				map_freeblock_unlock();
  3517. 				return 1;
  3518. 			}
  3519. 		}
  3520. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3521. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3522. 		break;
  3523.  
  3524. 	case CG_MARIONETTE:
  3525. 		{
  3526. 			struct status_change* sc = status_get_sc(src);
  3527.  
  3528. 			if( sd && dstsd && (dstsd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER && dstsd->status.sex == sd->status.sex )
  3529. 			{// Cannot cast on another bard/dancer-type class of the same gender as caster
  3530. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3531. 				map_freeblock_unlock();
  3532. 				return 1;
  3533. 			}
  3534.  
  3535. 			if( sc && tsc )
  3536. 			{
  3537. 				if( !sc->data[SC_MARIONETTE] && !tsc->data[SC_MARIONETTE2] )
  3538. 				{
  3539. 					sc_start(src,SC_MARIONETTE,100,bl->id,skill_get_time(skillid,skilllv));
  3540. 					sc_start(bl,SC_MARIONETTE2,100,src->id,skill_get_time(skillid,skilllv));
  3541. 					clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3542. 				}
  3543. 				else
  3544. 				if(  sc->data[SC_MARIONETTE ] &&  sc->data[SC_MARIONETTE ]->val1 == bl->id &&
  3545. 					tsc->data[SC_MARIONETTE2] && tsc->data[SC_MARIONETTE2]->val1 == src->id )
  3546. 				{
  3547. 					status_change_end(src, SC_MARIONETTE, INVALID_TIMER);
  3548. 					status_change_end(bl, SC_MARIONETTE2, INVALID_TIMER);
  3549. 				}
  3550. 				else
  3551. 				{
  3552. 					if( sd )
  3553. 						clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3554.  
  3555. 					map_freeblock_unlock();
  3556. 					return 1;
  3557. 				}
  3558. 			}
  3559. 		}
  3560. 		break;
  3561.  
  3562. 	case RG_CLOSECONFINE:
  3563. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3564. 			sc_start4(bl,type,100,skilllv,src->id,0,0,skill_get_time(skillid,skilllv)));
  3565. 		break;
  3566. 	case SA_FLAMELAUNCHER:	// added failure chance and chance to break weapon if turned on [Valaris]
  3567. 	case SA_FROSTWEAPON:
  3568. 	case SA_LIGHTNINGLOADER:
  3569. 	case SA_SEISMICWEAPON:
  3570. 		if (dstsd) {
  3571. 			if(dstsd->status.weapon == W_FIST ||
  3572. 				(dstsd->sc.count && !dstsd->sc.data[type] &&
  3573. 				(	//Allow re-enchanting to lenghten time. [Skotlex]
  3574. 					dstsd->sc.data[SC_FIREWEAPON] ||
  3575. 					dstsd->sc.data[SC_WATERWEAPON] ||
  3576. 					dstsd->sc.data[SC_WINDWEAPON] ||
  3577. 					dstsd->sc.data[SC_EARTHWEAPON] ||
  3578. 					dstsd->sc.data[SC_SHADOWWEAPON] ||
  3579. 					dstsd->sc.data[SC_GHOSTWEAPON] ||
  3580. 					dstsd->sc.data[SC_ENCPOISON]
  3581. 				))
  3582. 				) {
  3583. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3584. 				clif_skill_nodamage(src,bl,skillid,skilllv,0);
  3585. 				break;
  3586. 			}
  3587. 		}
  3588. 		// 100% success rate at lv4 & 5, but lasts longer at lv5
  3589. 		if(!clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,type,(60+skilllv*10),skilllv, skill_get_time(skillid,skilllv)))) {
  3590. 			if (sd)
  3591. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3592. 			if (skill_break_equip(bl, EQP_WEAPON, 10000, BCT_PARTY) && sd && sd != dstsd)
  3593. 				clif_displaymessage(sd->fd,"You broke target's weapon");
  3594. 		}
  3595. 		break;
  3596.  
  3597. 	case PR_ASPERSIO:
  3598. 		if (sd && dstmd) {
  3599. 			clif_skill_nodamage(src,bl,skillid,skilllv,0);
  3600. 			break;
  3601. 		}
  3602. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3603. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3604. 		break;
  3605.  
  3606. 	case ITEM_ENCHANTARMS:
  3607. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3608. 			sc_start2(bl,type,100,skilllv,
  3609. 				skill_get_ele(skillid,skilllv), skill_get_time(skillid,skilllv)));
  3610. 		break;
  3611.  
  3612. 	case TK_SEVENWIND:
  3613. 		switch(skill_get_ele(skillid,skilllv)) {
  3614. 			case ELE_EARTH : type = SC_EARTHWEAPON;  break;
  3615. 			case ELE_WIND  : type = SC_WINDWEAPON;   break;
  3616. 			case ELE_WATER : type = SC_WATERWEAPON;  break;
  3617. 			case ELE_FIRE  : type = SC_FIREWEAPON;   break;
  3618. 			case ELE_GHOST : type = SC_GHOSTWEAPON;  break;
  3619. 			case ELE_DARK  : type = SC_SHADOWWEAPON; break;
  3620. 			case ELE_HOLY  : type = SC_ASPERSIO;     break;
  3621. 		}
  3622. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3623. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3624.  
  3625. 		sc_start(bl,SC_SEVENWIND,100,skilllv,skill_get_time(skillid,skilllv));
  3626.  
  3627. 		break;
  3628.  
  3629. 	case PR_KYRIE:
  3630. 	case MER_KYRIE:	
  3631. 		clif_skill_nodamage(bl,bl,skillid,skilllv,
  3632. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3633. 		break;
  3634. 	//Passive Magnum, should had been casted on yourself.
  3635. 	case SM_MAGNUM:
  3636. 	case MS_MAGNUM:
  3637. 		skill_area_temp[1] = 0;
  3638. 		map_foreachinrange(skill_area_sub, src, skill_get_splash(skillid, skilllv), BL_SKILL|BL_CHAR,
  3639. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1, skill_castend_damage_id);
  3640. 		clif_skill_nodamage (src,src,skillid,skilllv,1);
  3641. 		//Initiate 10% of your damage becomes fire element.
  3642. 		sc_start4(src,SC_WATK_ELEMENT,100,3,20,0,0,skill_get_time2(skillid, skilllv));
  3643. 		if (sd) skill_blockpc_start (sd, skillid, skill_get_time(skillid, skilllv));
  3644. 		break;
  3645.  
  3646. 	case AL_INCAGI:
  3647. 	case AL_BLESSING:
  3648. 	case MER_INCAGI:
  3649. 	case MER_BLESSING:
  3650. 		if (dstsd != NULL && tsc->data[SC_CHANGEUNDEAD]) {
  3651. 			skill_attack(BF_MISC,src,src,bl,skillid,skilllv,tick,flag);
  3652. 			break;
  3653. 		}
  3654. 	case PR_SLOWPOISON:
  3655. 	case PR_IMPOSITIO:
  3656. 	case PR_LEXAETERNA:
  3657. 	case PR_SUFFRAGIUM:
  3658. 	case PR_BENEDICTIO:
  3659. 	case LK_BERSERK:
  3660. 	case MS_BERSERK:
  3661. 	case KN_AUTOCOUNTER:
  3662. 	case KN_TWOHANDQUICKEN:
  3663. 	case KN_ONEHAND:
  3664. 	case MER_QUICKEN:
  3665. 	case CR_SPEARQUICKEN:
  3666. 	case CR_REFLECTSHIELD:
  3667. 	case MS_REFLECTSHIELD:
  3668. 	case AS_POISONREACT:
  3669. 	case MC_LOUD:
  3670. 	case MG_ENERGYCOAT:
  3671. 	case MO_EXPLOSIONSPIRITS:
  3672. 	case MO_STEELBODY:
  3673. 	case MO_BLADESTOP:
  3674. 	case LK_AURABLADE:
  3675. 	case LK_PARRYING:
  3676. 	case MS_PARRYING:
  3677. 	case LK_CONCENTRATION:
  3678. 	case WS_CARTBOOST:
  3679. 	case SN_SIGHT:
  3680. 	case WS_MELTDOWN:
  3681. 	case WS_OVERTHRUSTMAX:
  3682. 	case ST_REJECTSWORD:
  3683. 	case HW_MAGICPOWER:
  3684. 	case PF_MEMORIZE:
  3685. 	case PA_SACRIFICE:
  3686. 	case ASC_EDP:
  3687. 	case PF_DOUBLECASTING:
  3688. 	case SG_SUN_COMFORT:
  3689. 	case SG_MOON_COMFORT:
  3690. 	case SG_STAR_COMFORT:
  3691. 	case NPC_HALLUCINATION:
  3692. 	case GS_MADNESSCANCEL:
  3693. 	case GS_ADJUSTMENT:
  3694. 	case GS_INCREASING:
  3695. 	case NJ_KASUMIKIRI:
  3696. 	case NJ_UTSUSEMI:
  3697. 	case NJ_NEN:
  3698. 	case NPC_DEFENDER:
  3699. 	case NPC_MAGICMIRROR:
  3700. 	case ST_PRESERVE:
  3701. 	case NPC_INVINCIBLE:
  3702. 	case NPC_INVINCIBLEOFF:
  3703. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3704. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3705. 		break;
  3706. 	case NPC_STOP:
  3707. 		if( clif_skill_nodamage(src,bl,skillid,skilllv,
  3708. 			sc_start2(bl,type,100,skilllv,src->id,skill_get_time(skillid,skilllv)) ) )
  3709. 			sc_start2(src,type,100,skilllv,bl->id,skill_get_time(skillid,skilllv));
  3710. 		break;
  3711. 	case HP_ASSUMPTIO:
  3712. 		if( sd && dstmd )
  3713. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3714. 		else
  3715. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  3716. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3717. 		break;
  3718. 	case MG_SIGHT:
  3719. 	case MER_SIGHT:
  3720. 	case AL_RUWACH:
  3721. 	case WZ_SIGHTBLASTER:
  3722. 	case NPC_WIDESIGHT:
  3723. 	case NPC_STONESKIN:
  3724. 	case NPC_ANTIMAGIC:
  3725. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3726. 			sc_start2(bl,type,100,skilllv,skillid,skill_get_time(skillid,skilllv)));
  3727. 		break;
  3728. 	case HLIF_AVOID:
  3729. 	case HAMI_DEFENCE:
  3730. 		i = skill_get_time(skillid,skilllv);
  3731. 		clif_skill_nodamage(bl,bl,skillid,skilllv,sc_start(bl,type,100,skilllv,i)); // Master
  3732. 		clif_skill_nodamage(src,src,skillid,skilllv,sc_start(src,type,100,skilllv,i)); // Homunc
  3733. 		break;
  3734. 	case NJ_BUNSINJYUTSU:
  3735. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3736. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3737. 		status_change_end(bl, SC_NEN, INVALID_TIMER);
  3738. 		break;
  3739. /* Was modified to only affect targetted char.	[Skotlex]
  3740. 	case HP_ASSUMPTIO:
  3741. 		if (flag&1)
  3742. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  3743. 		else
  3744. 		{
  3745. 			map_foreachinrange(skill_area_sub, bl,
  3746. 				skill_get_splash(skillid, skilllv), BL_PC,
  3747. 				src, skillid, skilllv, tick, flag|BCT_ALL|1,
  3748. 				skill_castend_nodamage_id);
  3749. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3750. 		}
  3751. 		break;
  3752. */
  3753. 	case SM_ENDURE:
  3754. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3755. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3756. 		if (sd)
  3757. 			skill_blockpc_start (sd, skillid, skill_get_time2(skillid,skilllv));
  3758. 		break;
  3759.  
  3760. 	case AS_ENCHANTPOISON: // Prevent spamming [Valaris]
  3761. 		if (sd && dstsd && dstsd->sc.count) {
  3762. 			if (dstsd->sc.data[SC_FIREWEAPON] ||
  3763. 				dstsd->sc.data[SC_WATERWEAPON] ||
  3764. 				dstsd->sc.data[SC_WINDWEAPON] ||
  3765. 				dstsd->sc.data[SC_EARTHWEAPON] ||
  3766. 				dstsd->sc.data[SC_SHADOWWEAPON] ||
  3767. 				dstsd->sc.data[SC_GHOSTWEAPON]
  3768. 			//	dstsd->sc.data[SC_ENCPOISON] //People say you should be able to recast to lengthen the timer. [Skotlex]
  3769. 			) {
  3770. 					clif_skill_nodamage(src,bl,skillid,skilllv,0);
  3771. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3772. 					break;
  3773. 			}
  3774. 		}
  3775. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3776. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3777. 		break;
  3778.  
  3779. 	case LK_TENSIONRELAX:
  3780. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3781. 			sc_start4(bl,type,100,skilllv,0,0,skill_get_time2(skillid,skilllv),
  3782. 				skill_get_time(skillid,skilllv)));
  3783. 		break;
  3784.  
  3785. 	case MC_CHANGECART:
  3786. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3787. 		break;
  3788.  
  3789. 	case TK_MISSION:
  3790. 		if (sd) {
  3791. 			int id;
  3792. 			if (sd->mission_mobid && (sd->mission_count || rand()%100)) { //Cannot change target when already have one
  3793. 				clif_mission_info(sd, sd->mission_mobid, sd->mission_count);
  3794. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3795. 				break;
  3796. 			}
  3797. 			id = mob_get_random_id(0,0xE, sd->status.base_level);
  3798. 			if (!id) {
  3799. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3800. 				break;
  3801. 			}
  3802. 			sd->mission_mobid = id;
  3803. 			sd->mission_count = 0;
  3804. 			pc_setglobalreg(sd,"TK_MISSION_ID", id);
  3805. 			clif_mission_info(sd, id, 0);
  3806. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3807. 		}
  3808. 		break;
  3809.  
  3810. 	case AC_CONCENTRATION:
  3811. 		{
  3812. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  3813. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  3814. 			map_foreachinrange( status_change_timer_sub, src,
  3815. 				skill_get_splash(skillid, skilllv), BL_CHAR,
  3816. 				src,NULL,type,tick);
  3817. 		}
  3818. 		break;
  3819.  
  3820. 	case SM_PROVOKE:
  3821. 	case SM_SELFPROVOKE:
  3822. 	case MER_PROVOKE:
  3823. 		if( (tstatus->mode&MD_BOSS) || battle_check_undead(tstatus->race,tstatus->def_ele) )
  3824. 		{
  3825. 			map_freeblock_unlock();
  3826. 			return 1;
  3827. 		}
  3828. 		//TODO: How much does base level affects? Dummy value of 1% per level difference used. [Skotlex]
  3829. 		clif_skill_nodamage(src,bl,skillid == SM_SELFPROVOKE ? SM_PROVOKE : skillid,skilllv,
  3830. 			(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))));
  3831. 		if( !i )
  3832. 		{
  3833. 			if( sd )
  3834. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3835. 			map_freeblock_unlock();
  3836. 			return 0;
  3837. 		}
  3838. 		unit_skillcastcancel(bl, 2);
  3839.  
  3840. 		if( tsc && tsc->count )
  3841. 		{
  3842. 			status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  3843. 			if( tsc->data[SC_STONE] && tsc->opt1 == OPT1_STONE )
  3844. 				status_change_end(bl, SC_STONE, INVALID_TIMER);
  3845. 			status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  3846. 		}
  3847.  
  3848. 		if( dstmd )
  3849. 		{
  3850. 			dstmd->state.provoke_flag = src->id;
  3851. 			mob_target(dstmd, src, skill_get_range2(src,skillid,skilllv));
  3852. 		}
  3853. 		break;
  3854.  
  3855. 	case ML_DEVOTION:
  3856. 	case CR_DEVOTION:
  3857. 		{
  3858. 			int count, lv;
  3859. 			if( !dstsd || (!sd && !mer) )
  3860. 			{ // Only players can be devoted
  3861. 				if( sd )
  3862. 					clif_skill_fail(sd, skillid, USESKILL_FAIL_LEVEL, 0);
  3863. 				break;
  3864. 			}
  3865.  
  3866. 			if( (lv = status_get_lv(src) - dstsd->status.base_level) < 0 )
  3867. 				lv = -lv;
  3868. 			if( lv > battle_config.devotion_level_difference || // Level difference requeriments
  3869. 				(dstsd->sc.data[type] && dstsd->sc.data[type]->val1 != src->id) || // Cannot Devote a player devoted from another source
  3870. 				(skillid == ML_DEVOTION && (!mer || mer != dstsd->md)) || // Mercenary only can devote owner
  3871. 				(dstsd->class_&MAPID_UPPERMASK) == MAPID_CRUSADER || // Crusader Cannot be devoted
  3872. 				(dstsd->sc.data[SC_HELLPOWER])) // Players affected by SC_HELLPOWERR cannot be devoted.
  3873. 			{
  3874. 				if( sd )
  3875. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  3876. 				map_freeblock_unlock();
  3877. 				return 1;
  3878. 			}
  3879.  
  3880. 			i = 0;
  3881. 			count = (sd)? min(skilllv,5) : 1; // Mercenary only can Devote owner
  3882. 			if( sd )
  3883. 			{ // Player Devoting Player
  3884. 				ARR_FIND(0, count, i, sd->devotion[i] == bl->id );
  3885. 				if( i == count )
  3886. 				{
  3887. 					ARR_FIND(0, count, i, sd->devotion[i] == 0 );
  3888. 					if( i == count )
  3889. 					{ // No free slots, skill Fail
  3890. 						clif_skill_fail(sd, skillid, USESKILL_FAIL_LEVEL, 0);
  3891. 						map_freeblock_unlock();
  3892. 						return 1;
  3893. 					}
  3894. 				}
  3895.  
  3896. 				sd->devotion[i] = bl->id;
  3897. 			}
  3898. 			else
  3899. 				mer->devotion_flag = 1; // Mercenary Devoting Owner
  3900.  
  3901. 			clif_skill_nodamage(src, bl, skillid, skilllv,
  3902. 				sc_start4(bl, type, 100, src->id, i, skill_get_range2(src,skillid,skilllv),0, skill_get_time2(skillid, skilllv)));
  3903. 			clif_devotion(src, NULL);
  3904. 		}
  3905. 		break;
  3906.  
  3907. 	case MO_CALLSPIRITS:
  3908. 		if(sd) {
  3909. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3910. 			pc_addspiritball(sd,skill_get_time(skillid,skilllv),skilllv);
  3911. 		}
  3912. 		break;
  3913.  
  3914. 	case CH_SOULCOLLECT:
  3915. 		if(sd) {
  3916. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3917. 			for (i = 0; i < 5; i++)
  3918. 				pc_addspiritball(sd,skill_get_time(skillid,skilllv),5);
  3919. 		}
  3920. 		break;
  3921.  
  3922. 	case MO_KITRANSLATION:
  3923. 		if(dstsd && (dstsd->class_&MAPID_BASEMASK)!=MAPID_GUNSLINGER) {
  3924. 			pc_addspiritball(dstsd,skill_get_time(skillid,skilllv),5);
  3925. 		}
  3926. 		break;
  3927.  
  3928. 	case TK_TURNKICK:
  3929. 	case MO_BALKYOUNG: //Passive part of the attack. Splash knock-back+stun. [Skotlex]
  3930. 		if (skill_area_temp[1] != bl->id) {
  3931. 			skill_blown(src,bl,skill_get_blewcount(skillid,skilllv),-1,0);
  3932. 			skill_additional_effect(src,bl,skillid,skilllv,BF_MISC,ATK_DEF,tick); //Use Misc rather than weapon to signal passive pushback
  3933. 		}
  3934. 		break;
  3935.  
  3936. 	case MO_ABSORBSPIRITS:
  3937. 		i = 0;
  3938. 		if (dstsd && dstsd->spiritball && (sd == dstsd || map_flag_vs(src->m)) && (dstsd->class_&MAPID_BASEMASK)!=MAPID_GUNSLINGER)
  3939. 		{	// split the if for readability, and included gunslingers in the check so that their coins cannot be removed [Reddozen]
  3940. 			i = dstsd->spiritball * 7;
  3941. 			pc_delspiritball(dstsd,dstsd->spiritball,0);
  3942. 		} else if (dstmd && !(tstatus->mode&MD_BOSS) && rand() % 100 < 20)
  3943. 		{	// check if target is a monster and not a Boss, for the 20% chance to absorb 2 SP per monster's level [Reddozen]
  3944. 			i = 2 * dstmd->level;
  3945. 			mob_target(dstmd,src,0);
  3946. 		}
  3947. 		if (i) status_heal(src, 0, i, 3);
  3948. 		clif_skill_nodamage(src,bl,skillid,skilllv,i?1:0);
  3949. 		break;
  3950.  
  3951. 	case AC_MAKINGARROW:
  3952. 		if(sd) {
  3953. 			clif_arrow_create_list(sd);
  3954. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3955. 		}
  3956. 		break;
  3957.  
  3958. 	case AM_PHARMACY:
  3959. 		if(sd) {
  3960. 			clif_skill_produce_mix_list(sd,22);
  3961. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3962. 		}
  3963. 		break;
  3964.  
  3965. 	case SA_CREATECON:
  3966. 		if(sd) {
  3967. 			clif_skill_produce_mix_list(sd,23);
  3968. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3969. 		}
  3970. 		break;
  3971.  
  3972. 	case BS_HAMMERFALL:
  3973. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  3974. 			sc_start(bl,SC_STUN,(20 + 10 * skilllv),skilllv,skill_get_time2(skillid,skilllv)));
  3975. 		break;
  3976. 	case RG_RAID:
  3977. 		skill_area_temp[1] = 0;
  3978. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3979. 		map_foreachinrange(skill_area_sub, bl,
  3980. 			skill_get_splash(skillid, skilllv), splash_target(src),
  3981. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  3982. 			skill_castend_damage_id);
  3983. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  3984. 		break;
  3985.  
  3986. 	case ASC_METEORASSAULT:
  3987. 	case GS_SPREADATTACK:
  3988. 		skill_area_temp[1] = 0;
  3989. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  3990. 		map_foreachinrange(skill_area_sub, bl, skill_get_splash(skillid, skilllv), splash_target(src), 
  3991. 			src, skillid, skilllv, tick, flag|BCT_ENEMY|SD_SPLASH|1, skill_castend_damage_id);
  3992. 		break;
  3993.  
  3994. 	case NPC_EARTHQUAKE:
  3995. 	case NPC_VAMPIRE_GIFT:
  3996. 	case NPC_HELLJUDGEMENT:
  3997. 	case NPC_PULSESTRIKE:
  3998. 		skill_castend_damage_id(src, src, skillid, skilllv, tick, flag);
  3999. 		break;
  4000.  
  4001. 	case KN_BRANDISHSPEAR:
  4002. 	case ML_BRANDISH:
  4003. 		skill_brandishspear(src, bl, skillid, skilllv, tick, flag);
  4004. 		break;
  4005.  
  4006. 	case WZ_SIGHTRASHER:
  4007. 		//Passive side of the attack.
  4008. 		status_change_end(src, SC_SIGHT, INVALID_TIMER);
  4009. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4010. 		map_foreachinrange(skill_area_sub,src,
  4011. 			skill_get_splash(skillid, skilllv),BL_CHAR|BL_SKILL,
  4012. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  4013. 			skill_castend_damage_id);
  4014. 		break;
  4015.  
  4016. 	case NJ_HYOUSYOURAKU:
  4017. 	case NJ_RAIGEKISAI:
  4018. 	case WZ_FROSTNOVA:
  4019. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4020. 		skill_area_temp[1] = 0;
  4021. 		map_foreachinrange(skill_attack_area, src,
  4022. 			skill_get_splash(skillid, skilllv), splash_target(src),
  4023. 			BF_MAGIC, src, src, skillid, skilllv, tick, flag, BCT_ENEMY);
  4024. 		break;
  4025.  
  4026. 	case HVAN_EXPLOSION:	//[orn]
  4027. 	case NPC_SELFDESTRUCTION:
  4028. 		//Self Destruction hits everyone in range (allies+enemies)
  4029. 		//Except for Summoned Marine spheres on non-versus maps, where it's just enemy.
  4030. 		i = ((!md || md->special_state.ai == 2) && !map_flag_vs(src->m))?
  4031. 			BCT_ENEMY:BCT_ALL;
  4032. 		clif_skill_nodamage(src, src, skillid, -1, 1);
  4033. 		map_delblock(src); //Required to prevent chain-self-destructions hitting back.
  4034. 		map_foreachinrange(skill_area_sub, bl,
  4035. 			skill_get_splash(skillid, skilllv), splash_target(src),
  4036. 			src, skillid, skilllv, tick, flag|i,
  4037. 			skill_castend_damage_id);
  4038. 		map_addblock(src);
  4039. 		status_damage(src, src, sstatus->max_hp,0,0,1);
  4040. 		break;
  4041.  
  4042. 	case AL_ANGELUS:
  4043. 	case PR_MAGNIFICAT:
  4044. 	case PR_GLORIA:
  4045. 	case SN_WINDWALK:
  4046. 	case CASH_BLESSING:
  4047. 	case CASH_INCAGI:
  4048. 	case CASH_ASSUMPTIO:
  4049. 		if( sd == NULL || sd->status.party_id == 0 || (flag & 1) )
  4050. 			clif_skill_nodamage(bl, bl, skillid, skilllv, sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4051. 		else if( sd )
  4052. 			party_foreachsamemap(skill_area_sub, sd, skill_get_splash(skillid, skilllv), src, skillid, skilllv, tick, flag|BCT_PARTY|1, skill_castend_nodamage_id);
  4053. 		break;
  4054. 	case MER_MAGNIFICAT:
  4055. 		if( mer != NULL )
  4056. 		{
  4057. 			clif_skill_nodamage(bl, bl, skillid, skilllv, sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4058. 			if( mer->master && mer->master->status.party_id != 0 && !(flag&1) )
  4059. 				party_foreachsamemap(skill_area_sub, mer->master, skill_get_splash(skillid, skilllv), src, skillid, skilllv, tick, flag|BCT_PARTY|1, skill_castend_nodamage_id);
  4060. 			else if( mer->master && !(flag&1) )
  4061. 				clif_skill_nodamage(src, &mer->master->bl, skillid, skilllv, sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4062. 		}
  4063. 		break;
  4064.  
  4065. 	case BS_ADRENALINE:
  4066. 	case BS_ADRENALINE2:
  4067. 	case BS_WEAPONPERFECT:
  4068. 	case BS_OVERTHRUST:
  4069. 		if (sd == NULL || sd->status.party_id == 0 || (flag & 1)) {
  4070. 			clif_skill_nodamage(bl,bl,skillid,skilllv,
  4071. 				sc_start2(bl,type,100,skilllv,(src == bl)? 1:0,skill_get_time(skillid,skilllv)));
  4072. 		} else if (sd) {
  4073. 			party_foreachsamemap(skill_area_sub,
  4074. 				sd,skill_get_splash(skillid, skilllv),
  4075. 				src,skillid,skilllv,tick, flag|BCT_PARTY|1,
  4076. 				skill_castend_nodamage_id);
  4077. 		}
  4078. 		break;
  4079.  
  4080. 	case BS_MAXIMIZE:
  4081. 	case NV_TRICKDEAD:
  4082. 	case CR_DEFENDER:
  4083. 	case ML_DEFENDER:
  4084. 	case CR_AUTOGUARD:
  4085. 	case ML_AUTOGUARD:
  4086. 	case TK_READYSTORM:
  4087. 	case TK_READYDOWN:
  4088. 	case TK_READYTURN:
  4089. 	case TK_READYCOUNTER:
  4090. 	case TK_DODGE:
  4091. 	case CR_SHRINK:
  4092. 	case SG_FUSION:
  4093. 	case GS_GATLINGFEVER:
  4094. 		if( tsce )
  4095. 		{
  4096. 			clif_skill_nodamage(src,bl,skillid,skilllv,status_change_end(bl, type, INVALID_TIMER));
  4097. 			map_freeblock_unlock();
  4098. 			return 0;
  4099. 		}
  4100. 		clif_skill_nodamage(src,bl,skillid,skilllv,sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4101. 		break;
  4102. 	case SL_KAITE:
  4103. 	case SL_KAAHI:
  4104. 	case SL_KAIZEL:
  4105. 	case SL_KAUPE:
  4106. 		if (sd) {
  4107. 			if (!dstsd || !(
  4108. 				(sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SOULLINKER) ||
  4109. 				(dstsd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER ||
  4110. 				dstsd->status.char_id == sd->status.char_id ||
  4111. 				dstsd->status.char_id == sd->status.partner_id ||
  4112. 				dstsd->status.char_id == sd->status.child
  4113. 			)) {
  4114. 				status_change_start(src,SC_STUN,10000,skilllv,0,0,0,500,8);
  4115. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4116. 				break;
  4117. 			}
  4118. 		}
  4119. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4120. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv)));
  4121. 		break;
  4122. 	case SM_AUTOBERSERK:
  4123. 	case MER_AUTOBERSERK:
  4124. 		if( tsce )
  4125. 			i = status_change_end(bl, type, INVALID_TIMER);
  4126. 		else
  4127. 			i = sc_start(bl,type,100,skilllv,60000);
  4128. 		clif_skill_nodamage(src,bl,skillid,skilllv,i);
  4129. 		break;
  4130. 	case TF_HIDING:
  4131. 	case ST_CHASEWALK:
  4132. 		if (tsce)
  4133. 		{
  4134. 			clif_skill_nodamage(src,bl,skillid,-1,status_change_end(bl, type, INVALID_TIMER)); //Hide skill-scream animation.
  4135. 			map_freeblock_unlock();
  4136. 			return 0;
  4137. 		}
  4138. 		clif_skill_nodamage(src,bl,skillid,-1,sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));		
  4139. 		break;
  4140. 	case TK_RUN:
  4141. 		if (tsce)
  4142. 		{
  4143. 			clif_skill_nodamage(src,bl,skillid,skilllv,status_change_end(bl, type, INVALID_TIMER));
  4144. 			map_freeblock_unlock();
  4145. 			return 0;
  4146. 		}
  4147. 		clif_skill_nodamage(src,bl,skillid,skilllv,sc_start4(bl,type,100,skilllv,unit_getdir(bl),0,0,0));
  4148. 		if (sd) // If the client receives a skill-use packet inmediately before a walkok packet, it will discard the walk packet! [Skotlex]
  4149. 			clif_walkok(sd); // So aegis has to resend the walk ok.
  4150. 		break;
  4151. 	case AS_CLOAKING:
  4152. 		if (tsce)
  4153. 		{
  4154. 			i = status_change_end(bl, type, INVALID_TIMER);
  4155. 			if( i )
  4156. 				clif_skill_nodamage(src,bl,skillid,-1,i);
  4157. 			else if( sd )
  4158. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4159. 			map_freeblock_unlock();
  4160. 			return 0;
  4161. 		}
  4162. 		i = sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  4163. 		if( i )
  4164. 			clif_skill_nodamage(src,bl,skillid,-1,i);
  4165. 		else if( sd )
  4166. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4167. 		break;
  4168.  
  4169. 	case BD_ADAPTATION:
  4170. 		if(tsc && tsc->data[SC_DANCING]){
  4171. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4172. 			status_change_end(bl, SC_DANCING, INVALID_TIMER);
  4173. 		}
  4174. 		break;
  4175.  
  4176. 	case BA_FROSTJOKER:
  4177. 	case DC_SCREAM:
  4178. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4179. 		skill_addtimerskill(src,tick+2000,bl->id,src->x,src->y,skillid,skilllv,0,flag);
  4180.  
  4181. 		if (md) {
  4182. 			// custom hack to make the mob display the skill, because these skills don't show the skill use text themselves
  4183. 			//NOTE: mobs don't have the sprite animation that is used when performing this skill (will cause glitches)
  4184. 			char temp[70];
  4185. 			snprintf(temp, sizeof(temp), "%s : %s !!",md->name,skill_db[skillid].desc);
  4186. 			clif_message(&md->bl,temp);
  4187. 		}
  4188. 		break;
  4189.  
  4190. 	case BA_PANGVOICE:
  4191. 		clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,SC_CONFUSION,50,7,skill_get_time(skillid,skilllv)));
  4192. 		break;
  4193.  
  4194. 	case DC_WINKCHARM:
  4195. 		if( dstsd )
  4196. 			clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,SC_CONFUSION,30,7,skill_get_time2(skillid,skilllv)));
  4197. 		else
  4198. 		if( dstmd )
  4199. 		{
  4200. 			if( status_get_lv(src) > status_get_lv(bl)
  4201. 			&&  (tstatus->race == RC_DEMON || tstatus->race == RC_DEMIHUMAN || tstatus->race == RC_ANGEL)
  4202. 			&&  !(tstatus->mode&MD_BOSS) )
  4203. 				clif_skill_nodamage(src,bl,skillid,skilllv, sc_start(bl,type,70,skilllv,skill_get_time(skillid,skilllv)));
  4204. 			else
  4205. 			{
  4206. 				clif_skill_nodamage(src,bl,skillid,skilllv,0);
  4207. 				if(sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4208. 			}
  4209. 		}
  4210. 		break;
  4211.  
  4212. 	case TF_STEAL:
  4213. 		if(sd) {
  4214. 			if(pc_steal_item(sd,bl,skilllv))
  4215. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4216. 			else
  4217. 				clif_skill_fail(sd,skillid,USESKILL_FAIL,0);
  4218. 		}
  4219. 		break;
  4220.  
  4221. 	case RG_STEALCOIN:
  4222. 		if(sd) {
  4223. 			if(pc_steal_coin(sd,bl))
  4224. 			{
  4225. 				dstmd->state.provoke_flag = src->id;
  4226. 				mob_target(dstmd, src, skill_get_range2(src,skillid,skilllv));
  4227. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4228.  
  4229. 			} 
  4230. 			else
  4231. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4232. 		}
  4233. 		break;
  4234.  
  4235. 	case MG_STONECURSE:
  4236. 		{
  4237. 			if (tstatus->mode&MD_BOSS) {
  4238. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4239. 				break;
  4240. 			}
  4241. 			if(status_isimmune(bl) || !tsc)
  4242. 				break;
  4243.  
  4244. 			if (tsc->data[SC_STONE]) {
  4245. 				status_change_end(bl, SC_STONE, INVALID_TIMER);
  4246. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4247. 				break;
  4248. 			}
  4249. 			if (sc_start4(bl,SC_STONE,(skilllv*4+20),
  4250. 				skilllv, 0, 0, skill_get_time(skillid, skilllv),
  4251. 				skill_get_time2(skillid,skilllv)))
  4252. 					clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4253. 			else if(sd) {
  4254. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4255. 				// Level 6-10 doesn't consume a red gem if it fails [celest]
  4256. 				if (skilllv > 5)
  4257. 				{ // not to consume items
  4258. 					map_freeblock_unlock();
  4259. 					return 0;
  4260. 				}
  4261. 			}
  4262. 		}
  4263. 		break;
  4264.  
  4265. 	case NV_FIRSTAID:
  4266. 		clif_skill_nodamage(src,bl,skillid,5,1);
  4267. 		status_heal(bl,5,0,0);
  4268. 		break;
  4269.  
  4270. 	case AL_CURE:
  4271. 		if(status_isimmune(bl)) {
  4272. 			clif_skill_nodamage(src,bl,skillid,skilllv,0);
  4273. 			break;
  4274. 		}
  4275. 		status_change_end(bl, SC_SILENCE, INVALID_TIMER);
  4276. 		status_change_end(bl, SC_BLIND, INVALID_TIMER);
  4277. 		status_change_end(bl, SC_CONFUSION, INVALID_TIMER);
  4278. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4279. 		break;
  4280.  
  4281. 	case TF_DETOXIFY:
  4282. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4283. 		status_change_end(bl, SC_POISON, INVALID_TIMER);
  4284. 		status_change_end(bl, SC_DPOISON, INVALID_TIMER);
  4285. 		break;
  4286.  
  4287. 	case PR_STRECOVERY:
  4288. 		if(status_isimmune(bl)) {
  4289. 			clif_skill_nodamage(src,bl,skillid,skilllv,0);
  4290. 			break;
  4291. 		}
  4292. 		if (tsc && tsc->opt1) {
  4293. 			status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  4294. 			status_change_end(bl, SC_STONE, INVALID_TIMER);
  4295. 			status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  4296. 			status_change_end(bl, SC_STUN, INVALID_TIMER);
  4297. 		}
  4298. 		//Is this equation really right? It looks so... special.
  4299. 		if(battle_check_undead(tstatus->race,tstatus->def_ele))
  4300. 		{
  4301. 			status_change_start(bl, SC_BLIND,
  4302. 				100*(100-(tstatus->int_/2+tstatus->vit/3+tstatus->luk/10)),
  4303. 				1,0,0,0,
  4304. 				skill_get_time2(skillid, skilllv) * (100-(tstatus->int_+tstatus->vit)/2)/100,10);
  4305. 		}
  4306. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4307. 		if(dstmd)
  4308. 			mob_unlocktarget(dstmd,tick);
  4309. 		break;
  4310.  
  4311. 	// Mercenary Supportive Skills
  4312. 	case MER_BENEDICTION:
  4313. 		status_change_end(bl, SC_CURSE, INVALID_TIMER);
  4314. 		status_change_end(bl, SC_BLIND, INVALID_TIMER);
  4315. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4316. 		break;
  4317. 	case MER_COMPRESS:
  4318. 		status_change_end(bl, SC_BLEEDING, INVALID_TIMER);
  4319. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4320. 		break;
  4321. 	case MER_MENTALCURE:
  4322. 		status_change_end(bl, SC_CONFUSION, INVALID_TIMER);
  4323. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4324. 		break;
  4325. 	case MER_RECUPERATE:
  4326. 		status_change_end(bl, SC_POISON, INVALID_TIMER);
  4327. 		status_change_end(bl, SC_SILENCE, INVALID_TIMER);
  4328. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4329. 		break;
  4330. 	case MER_REGAIN:
  4331. 		status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  4332. 		status_change_end(bl, SC_STUN, INVALID_TIMER);
  4333. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4334. 		break;
  4335. 	case MER_TENDER:
  4336. 		status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  4337. 		status_change_end(bl, SC_STONE, INVALID_TIMER);
  4338. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4339. 		break;
  4340.  
  4341. 	case MER_SCAPEGOAT:
  4342. 		if( mer && mer->master )
  4343. 		{
  4344. 			status_heal(&mer->master->bl, mer->battle_status.hp, 0, 2);
  4345. 			status_damage(src, src, mer->battle_status.max_hp, 0, 0, 1);
  4346. 		}
  4347. 		break;
  4348.  
  4349. 	case MER_ESTIMATION:
  4350. 		if( !mer )
  4351. 			break;
  4352. 		sd = mer->master;
  4353. 	case WZ_ESTIMATION:
  4354. 		if( sd == NULL )
  4355. 			break;
  4356. 		if( dstsd )
  4357. 		{ // Fail on Players
  4358. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4359. 			break;
  4360. 		}
  4361. 		if( dstmd && dstmd->class_ == MOBID_EMPERIUM )
  4362. 			break; // Cannot be Used on Emperium
  4363.  
  4364. 		clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  4365. 		clif_skill_estimation(sd, bl);
  4366. 		if( skillid == MER_ESTIMATION )
  4367. 			sd = NULL;
  4368. 		break;
  4369.  
  4370. 	case BS_REPAIRWEAPON:
  4371. 		if(sd && dstsd)
  4372. 			clif_item_repair_list(sd,dstsd);
  4373. 		break;
  4374.  
  4375. 	case MC_IDENTIFY:
  4376. 		if(sd)
  4377. 			clif_item_identify_list(sd);
  4378. 		break;
  4379.  
  4380. 	// Weapon Refining [Celest]
  4381. 	case WS_WEAPONREFINE:
  4382. 		if(sd)
  4383. 			clif_item_refine_list(sd);
  4384. 		break;
  4385.  
  4386. 	case MC_VENDING:
  4387. 		if(sd)
  4388. 		{	//Prevent vending of GMs with unnecessary Level to trade/drop. [Skotlex]
  4389. 			if ( !pc_can_give_items(pc_isGM(sd)) )
  4390. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4391. 			else
  4392. 				clif_openvendingreq(sd,2+skilllv);
  4393. 		}
  4394. 		break;
  4395.  
  4396. 	case AL_TELEPORT:
  4397. 		if(sd)
  4398. 		{
  4399. 			if (map[bl->m].flag.noteleport && skilllv <= 2) {
  4400. 				clif_skill_teleportmessage(sd,0);
  4401. 				break;
  4402. 			}
  4403. 			if(!battle_config.duel_allow_teleport && sd->duel_group && skilllv <= 2) { // duel restriction [LuzZza]
  4404. 				clif_displaymessage(sd->fd, "Duel: Can't use teleport in duel.");
  4405. 				break;
  4406. 			}
  4407.  
  4408. 			if( sd->state.autocast || ( (sd->skillitem == AL_TELEPORT || battle_config.skip_teleport_lv1_menu) && skilllv == 1 ) || skilllv == 3 )
  4409. 			{
  4410. 				if( skilllv == 1 )
  4411. 					pc_randomwarp(sd,CLR_TELEPORT);
  4412. 				else
  4413. 					pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4414. 				break;
  4415. 			}
  4416.  
  4417. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4418. 			if( skilllv == 1 )
  4419. 				clif_skill_warppoint(sd,skillid,skilllv, (unsigned short)-1,0,0,0);
  4420. 			else
  4421. 				clif_skill_warppoint(sd,skillid,skilllv, (unsigned short)-1,sd->status.save_point.map,0,0);
  4422. 		} else
  4423. 			unit_warp(bl,-1,-1,-1,CLR_TELEPORT);
  4424. 		break;
  4425.  
  4426. 	case NPC_EXPULSION:
  4427. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4428. 		unit_warp(bl,-1,-1,-1,CLR_TELEPORT);
  4429. 		break;
  4430.  
  4431. 	case AL_HOLYWATER:
  4432. 		if(sd) {
  4433. 			if (skill_produce_mix(sd, skillid, 523, 0, 0, 0, 1))
  4434. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4435. 			else
  4436. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4437. 		}
  4438. 		break;
  4439.  
  4440. 	case TF_PICKSTONE:
  4441. 		if(sd) {
  4442. 			int eflag;
  4443. 			struct item item_tmp;
  4444. 			struct block_list tbl;
  4445. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4446. 			memset(&item_tmp,0,sizeof(item_tmp));
  4447. 			memset(&tbl,0,sizeof(tbl)); // [MouseJstr]
  4448. 			item_tmp.nameid = ITEMID_STONE;
  4449. 			item_tmp.identify = 1;
  4450. 			tbl.id = 0;
  4451. 			clif_takeitem(&sd->bl,&tbl);
  4452. 			eflag = pc_additem(sd,&item_tmp,1);
  4453. 			if(eflag) {
  4454. 				clif_additem(sd,0,0,eflag);
  4455. 				map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4456. 			}
  4457. 		}
  4458. 		break;
  4459. 	case ASC_CDP:
  4460.      	if(sd) {
  4461. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4462. 			skill_produce_mix(sd, skillid, 678, 0, 0, 0, 1); //Produce a Deadly Poison Bottle.
  4463. 		}
  4464. 		break;
  4465.  
  4466. 	case RG_STRIPWEAPON:
  4467. 	case RG_STRIPSHIELD:
  4468. 	case RG_STRIPARMOR:
  4469. 	case RG_STRIPHELM:
  4470. 	case ST_FULLSTRIP:
  4471. 	{
  4472. 		unsigned short location = 0;
  4473. 		int d = 0;
  4474.  
  4475. 		//Rate in percent
  4476. 		if ( skillid == ST_FULLSTRIP ) {
  4477. 			i = 5 + 2*skilllv + (sstatus->dex - tstatus->dex)/5;
  4478. 		} else {
  4479. 			i = 5 + 5*skilllv + (sstatus->dex - tstatus->dex)/5;
  4480. 		}
  4481. 		if (i < 5) i = 5; //Minimum rate 5%
  4482.  
  4483. 		//Duration in ms
  4484. 		d = skill_get_time(skillid,skilllv) + (sstatus->dex - tstatus->dex)*500;
  4485. 		if (d < 0) d = 0; //Minimum duration 0ms
  4486.  
  4487. 		switch (skillid) {
  4488. 		case RG_STRIPWEAPON:
  4489. 			location = EQP_WEAPON;
  4490. 			break;
  4491. 		case RG_STRIPSHIELD:
  4492. 			location = EQP_SHIELD;
  4493. 			break;
  4494. 		case RG_STRIPARMOR:
  4495. 			location = EQP_ARMOR;
  4496. 			break;
  4497. 		case RG_STRIPHELM:
  4498. 			location = EQP_HELM;
  4499. 			break;
  4500. 		case ST_FULLSTRIP:
  4501. 			location = EQP_WEAPON|EQP_SHIELD|EQP_ARMOR|EQP_HELM;
  4502. 			break;
  4503. 		}
  4504.  
  4505. 		//Special message when trying to use strip on FCP [Jobbie]
  4506. 		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] )
  4507. 		{
  4508. 			clif_gospel_info(sd, 0x28);
  4509. 			break;
  4510. 		}
  4511.  
  4512. 		//Attempts to strip at rate i and duration d
  4513. 		if( (i = skill_strip_equip(bl, location, i, skilllv, d)) || skillid != ST_FULLSTRIP )
  4514. 			clif_skill_nodamage(src,bl,skillid,skilllv,i); 
  4515.  
  4516. 		//Nothing stripped.
  4517. 		if( sd && !i )
  4518. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4519. 	}
  4520. 		break;
  4521.  
  4522. 	case AM_BERSERKPITCHER:
  4523. 	case AM_POTIONPITCHER:
  4524. 		{
  4525. 			int i,x,hp = 0,sp = 0,bonus=100;
  4526. 			if( dstmd && dstmd->class_ == MOBID_EMPERIUM )
  4527. 			{
  4528. 				map_freeblock_unlock();
  4529. 				return 1;
  4530. 			}
  4531. 			if( sd )
  4532. 			{
  4533. 				x = skilllv%11 - 1;
  4534. 				i = pc_search_inventory(sd,skill_db[skillid].itemid[x]);
  4535. 				if(i < 0 || skill_db[skillid].itemid[x] <= 0)
  4536. 				{
  4537. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4538. 					map_freeblock_unlock();
  4539. 					return 1;
  4540. 				}
  4541. 				if(sd->inventory_data[i] == NULL || sd->status.inventory[i].amount < skill_db[skillid].amount[x])
  4542. 				{
  4543. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4544. 					map_freeblock_unlock();
  4545. 					return 1;
  4546. 				}
  4547. 				if( skillid == AM_BERSERKPITCHER )
  4548. 				{
  4549. 					if( dstsd && dstsd->status.base_level < (unsigned int)sd->inventory_data[i]->elv )
  4550. 					{
  4551. 						clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4552. 						map_freeblock_unlock();
  4553. 						return 1;
  4554. 					}
  4555. 				}
  4556. 				potion_flag = 1;
  4557. 				potion_hp = potion_sp = potion_per_hp = potion_per_sp = 0;
  4558. 				potion_target = bl->id;
  4559. 				run_script(sd->inventory_data[i]->script,0,sd->bl.id,0);
  4560. 				potion_flag = potion_target = 0;
  4561. 				if( sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ALCHEMIST )
  4562. 					bonus += sd->status.base_level;
  4563. 				if( potion_per_hp > 0 || potion_per_sp > 0 )
  4564. 				{
  4565. 					hp = tstatus->max_hp * potion_per_hp / 100;
  4566. 					hp = hp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4567. 					if( dstsd )
  4568. 					{
  4569. 						sp = dstsd->status.max_sp * potion_per_sp / 100;
  4570. 						sp = sp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4571. 					}
  4572. 				}
  4573. 				else
  4574. 				{
  4575. 					if( potion_hp > 0 )
  4576. 					{
  4577. 						hp = potion_hp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4578. 						hp = hp * (100 + (tstatus->vit<<1)) / 100;
  4579. 						if( dstsd )
  4580. 							hp = hp * (100 + pc_checkskill(dstsd,SM_RECOVERY)*10) / 100;
  4581. 					}
  4582. 					if( potion_sp > 0 )
  4583. 					{
  4584. 						sp = potion_sp * (100 + pc_checkskill(sd,AM_POTIONPITCHER)*10 + pc_checkskill(sd,AM_LEARNINGPOTION)*5)*bonus/10000;
  4585. 						sp = sp * (100 + (tstatus->int_<<1)) / 100;
  4586. 						if( dstsd )
  4587. 							sp = sp * (100 + pc_checkskill(dstsd,MG_SRECOVERY)*10) / 100;
  4588. 					}
  4589. 				}
  4590.  
  4591. 				if (sd->itemgrouphealrate[IG_POTION]>0)
  4592. 				{
  4593. 					hp += hp * sd->itemgrouphealrate[IG_POTION] / 100;
  4594. 					sp += sp * sd->itemgrouphealrate[IG_POTION] / 100;
  4595. 				}
  4596.  
  4597. 				if( (i = pc_skillheal_bonus(sd, skillid)) )
  4598. 				{
  4599. 					hp += hp * i / 100;
  4600. 					sp += sp * i / 100;
  4601. 				}
  4602. 			}
  4603. 			else
  4604. 			{
  4605. 				hp = (1 + rand()%400) * (100 + skilllv*10) / 100;
  4606. 				hp = hp * (100 + (tstatus->vit<<1)) / 100;
  4607. 				if( dstsd )
  4608. 					hp = hp * (100 + pc_checkskill(dstsd,SM_RECOVERY)*10) / 100;
  4609. 			}
  4610. 			if( dstsd && (i = pc_skillheal2_bonus(dstsd, skillid)) )
  4611. 			{
  4612. 				hp += hp * i / 100;
  4613. 				sp += sp * i / 100;
  4614. 			}
  4615. 			if( tsc && tsc->data[SC_CRITICALWOUND] )
  4616. 			{
  4617. 				hp -= hp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  4618. 				sp -= sp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  4619. 			}
  4620. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4621. 			if( hp > 0 || (skillid == AM_POTIONPITCHER && sp <= 0) )
  4622. 				clif_skill_nodamage(NULL,bl,AL_HEAL,hp,1);
  4623. 			if( sp > 0 )
  4624. 				clif_skill_nodamage(NULL,bl,MG_SRECOVERY,sp,1);
  4625. 			status_heal(bl,hp,sp,0);
  4626. 		}
  4627. 		break;
  4628. 	case AM_CP_WEAPON:
  4629. 	case AM_CP_SHIELD:
  4630. 	case AM_CP_ARMOR:
  4631. 	case AM_CP_HELM:
  4632. 		{
  4633. 			enum sc_type scid = (sc_type)(SC_STRIPWEAPON + (skillid - AM_CP_WEAPON));
  4634. 			status_change_end(bl, scid, INVALID_TIMER);
  4635. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  4636. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  4637. 		}
  4638. 		break;
  4639. 	case AM_TWILIGHT1:
  4640. 		if (sd) {
  4641. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4642. 			//Prepare 200 White Potions.
  4643. 			if (!skill_produce_mix(sd, skillid, 504, 0, 0, 0, 200))
  4644. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4645. 		}
  4646. 		break;
  4647. 	case AM_TWILIGHT2:
  4648. 		if (sd) {
  4649. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4650. 			//Prepare 200 Slim White Potions.
  4651. 			if (!skill_produce_mix(sd, skillid, 547, 0, 0, 0, 200))
  4652. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4653. 		}
  4654. 		break;
  4655. 	case AM_TWILIGHT3:
  4656. 		if (sd) {
  4657. 			//check if you can produce all three, if not, then fail:
  4658. 			if (!skill_can_produce_mix(sd,970,-1, 100) //100 Alcohol
  4659. 				|| !skill_can_produce_mix(sd,7136,-1, 50) //50 Acid Bottle
  4660. 				|| !skill_can_produce_mix(sd,7135,-1, 50) //50 Flame Bottle
  4661. 			) {
  4662. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4663. 				break;
  4664. 			}
  4665. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4666. 			skill_produce_mix(sd, skillid, 970, 0, 0, 0, 100);
  4667. 			skill_produce_mix(sd, skillid, 7136, 0, 0, 0, 50);
  4668. 			skill_produce_mix(sd, skillid, 7135, 0, 0, 0, 50);
  4669. 		}
  4670. 		break;
  4671. 	case SA_DISPELL:
  4672. 		if (flag&1 || (i = skill_get_splash(skillid, skilllv)) < 1)
  4673. 		{
  4674. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4675. 			if((dstsd && (dstsd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER)
  4676. 				|| (tsc && tsc->data[SC_SPIRIT] && tsc->data[SC_SPIRIT]->val2 == SL_ROGUE) //Rogue's spirit defends againt dispel.
  4677. 				|| rand()%100 >= 50+10*skilllv)
  4678. 			{
  4679. 				if (sd)
  4680. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4681. 				break;
  4682. 			}
  4683. 			if(status_isimmune(bl) || !tsc || !tsc->count)
  4684. 				break;
  4685. 			for(i=0;i<SC_MAX;i++)
  4686. 			{
  4687. 				if (!tsc->data[i])
  4688. 					continue;
  4689. 				switch (i) {
  4690. 				case SC_WEIGHT50:		case SC_WEIGHT90:		case SC_HALLUCINATION:
  4691. 				case SC_STRIPWEAPON:	case SC_STRIPSHIELD:	case SC_STRIPARMOR:
  4692. 				case SC_STRIPHELM:		case SC_CP_WEAPON:		case SC_CP_SHIELD:
  4693. 				case SC_CP_ARMOR:		case SC_CP_HELM:		case SC_COMBO:
  4694. 				case SC_STRFOOD:		case SC_AGIFOOD:		case SC_VITFOOD:
  4695. 				case SC_INTFOOD:		case SC_DEXFOOD:		case SC_LUKFOOD:
  4696. 				case SC_HITFOOD:		case SC_FLEEFOOD:		case SC_BATKFOOD:
  4697. 				case SC_WATKFOOD:		case SC_MATKFOOD:		case SC_DANCING:
  4698. 				case SC_GUILDAURA:		case SC_EDP:			case SC_AUTOBERSERK:
  4699. 				case SC_CARTBOOST:		case SC_MELTDOWN:		case SC_SAFETYWALL:
  4700. 				case SC_SMA:			case SC_SPEEDUP0:		case SC_NOCHAT:
  4701. 				case SC_ANKLE:			case SC_SPIDERWEB:		case SC_JAILED:
  4702. 				case SC_ITEMBOOST:		case SC_EXPBOOST:		case SC_LIFEINSURANCE:
  4703. 				case SC_BOSSMAPINFO:	case SC_PNEUMA:			case SC_AUTOSPELL:
  4704. 				case SC_INCHITRATE:		case SC_INCATKRATE:		case SC_NEN:
  4705. 				case SC_READYSTORM:		case SC_READYDOWN:		case SC_READYTURN:
  4706. 				case SC_READYCOUNTER:	case SC_DODGE:			case SC_WARM:
  4707. 				case SC_SPEEDUP1:		case SC_AUTOTRADE:		case SC_CRITICALWOUND:
  4708. 				case SC_JEXPBOOST:		case SC_INVINCIBLE:		case SC_INVINCIBLEOFF:
  4709. 				case SC_HELLPOWER:		case SC_MANU_ATK:		case SC_MANU_DEF:
  4710. 				case SC_SPL_ATK:		case SC_SPL_DEF:		case SC_MANU_MATK:
  4711. 				case SC_SPL_MATK:		case SC_RICHMANKIM:		case SC_ETERNALCHAOS:
  4712. 				case SC_DRUMBATTLE:		case SC_NIBELUNGEN:		case SC_ROKISWEIL:
  4713. 				case SC_INTOABYSS:		case SC_SIEGFRIED:		case SC_WHISTLE:
  4714. 				case SC_ASSNCROS:		case SC_POEMBRAGI:		case SC_APPLEIDUN:
  4715. 				case SC_HUMMING:		case SC_DONTFORGETME:	case SC_FORTUNE:
  4716. 				case SC_SERVICE4U:		case SC_FOOD_STR_CASH:	case SC_FOOD_AGI_CASH:
  4717. 				case SC_FOOD_VIT_CASH:	case SC_FOOD_DEX_CASH:	case SC_FOOD_INT_CASH:
  4718. 				case SC_FOOD_LUK_CASH:
  4719. 					continue;
  4720. 				case SC_ASSUMPTIO:
  4721. 					if( bl->type == BL_MOB )
  4722. 						continue;
  4723. 					break;
  4724. 				}
  4725. 				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.
  4726. 				status_change_end(bl, (sc_type)i, INVALID_TIMER);
  4727. 			}
  4728. 			break;
  4729. 		}
  4730. 		//Affect all targets on splash area.
  4731. 		map_foreachinrange(skill_area_sub, bl, i, BL_CHAR,
  4732. 			src, skillid, skilllv, tick, flag|1,
  4733. 			skill_castend_damage_id);
  4734. 		break;
  4735.  
  4736. 	case TF_BACKSLIDING: //This is the correct implementation as per packet logging information. [Skotlex]
  4737. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4738. 		skill_blown(src,bl,skill_get_blewcount(skillid,skilllv),unit_getdir(bl),0);
  4739. 		break;
  4740.  
  4741. 	case TK_HIGHJUMP:
  4742. 		{
  4743. 			int x,y, dir = unit_getdir(src);
  4744.  
  4745. 		  	//Fails on noteleport maps, except for GvG and BG maps [Skotlex]
  4746. 			if( map[src->m].flag.noteleport &&
  4747. 				!(map[src->m].flag.battleground || map_flag_gvg2(src->m) )
  4748. 			) {
  4749. 				x = src->x;
  4750. 				y = src->y;
  4751. 			} else {
  4752. 				x = src->x + dirx[dir]*skilllv*2;
  4753. 				y = src->y + diry[dir]*skilllv*2;
  4754. 			}
  4755.  
  4756. 			clif_skill_nodamage(src,bl,TK_HIGHJUMP,skilllv,1);
  4757. 			if(!map_count_oncell(src->m,x,y,BL_PC|BL_NPC|BL_MOB) && map_getcell(src->m,x,y,CELL_CHKREACH)) {
  4758. 				clif_slide(src,x,y);
  4759. 				unit_movepos(src, x, y, 1, 0);
  4760. 			}
  4761. 		}
  4762. 		break;
  4763.  
  4764. 	case SA_CASTCANCEL:
  4765. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4766. 		unit_skillcastcancel(src,1);
  4767. 		if(sd) {
  4768. 			int sp = skill_get_sp(sd->skillid_old,sd->skilllv_old);
  4769. 			sp = sp * (90 - (skilllv-1)*20) / 100;
  4770. 			if(sp < 0) sp = 0;
  4771. 			status_zap(src, 0, sp);
  4772. 		}
  4773. 		break;
  4774. 	case SA_SPELLBREAKER:
  4775. 		{
  4776. 			int sp;
  4777. 			if(tsc && tsc->data[SC_MAGICROD]) {
  4778. 				sp = skill_get_sp(skillid,skilllv);
  4779. 				sp = sp * tsc->data[SC_MAGICROD]->val2 / 100;
  4780. 				if(sp < 1) sp = 1;
  4781. 				status_heal(bl,0,sp,2);
  4782. 				clif_skill_nodamage(bl,bl,SA_MAGICROD,tsc->data[SC_MAGICROD]->val1,1);
  4783. 				status_percent_damage(bl, src, 0, -20, false); //20% max SP damage.
  4784. 			} else {
  4785. 				struct unit_data *ud = unit_bl2ud(bl);
  4786. 				int bl_skillid=0,bl_skilllv=0,hp = 0;
  4787. 				if (!ud || ud->skilltimer == INVALID_TIMER)
  4788. 					break; //Nothing to cancel.
  4789. 				bl_skillid = ud->skillid;
  4790. 				bl_skilllv = ud->skilllv;
  4791. 				if (tstatus->mode & MD_BOSS)
  4792. 				{	//Only 10% success chance against bosses. [Skotlex]
  4793. 					if (rand()%100 < 90)
  4794. 					{
  4795. 						if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  4796. 						break;
  4797. 					}
  4798. 				} else if (!dstsd || map_flag_vs(bl->m)) //HP damage only on pvp-maps when against players.
  4799. 					hp = tstatus->max_hp/50; //Recover 2% HP [Skotlex]
  4800.  
  4801. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4802. 				unit_skillcastcancel(bl,0);
  4803. 				sp = skill_get_sp(bl_skillid,bl_skilllv);
  4804. 				status_zap(bl, hp, sp);
  4805.  
  4806. 				if (hp && skilllv >= 5)
  4807. 					hp>>=1;	//Recover half damaged HP at level 5 [Skotlex]
  4808. 				else
  4809. 					hp = 0;
  4810.  
  4811. 				if (sp) //Recover some of the SP used
  4812. 					sp = sp*(25*(skilllv-1))/100;
  4813.  
  4814. 				if(hp || sp)
  4815. 					status_heal(src, hp, sp, 2);
  4816. 			}
  4817. 		}
  4818. 		break;
  4819. 	case SA_MAGICROD:
  4820. 		//It activates silently, no use animation.
  4821. 		sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  4822. 		break;
  4823. 	case SA_AUTOSPELL:
  4824. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4825. 		if(sd)
  4826. 			clif_autospell(sd,skilllv);
  4827. 		else {
  4828. 			int maxlv=1,spellid=0;
  4829. 			static const int spellarray[3] = { MG_COLDBOLT,MG_FIREBOLT,MG_LIGHTNINGBOLT };
  4830. 			if(skilllv >= 10) {
  4831. 				spellid = MG_FROSTDIVER;
  4832. //				if (tsc && tsc->data[SC_SPIRIT] && tsc->data[SC_SPIRIT]->val2 == SA_SAGE)
  4833. //					maxlv = 10;
  4834. //				else
  4835. 					maxlv = skilllv - 9;
  4836. 			}
  4837. 			else if(skilllv >=8) {
  4838. 				spellid = MG_FIREBALL;
  4839. 				maxlv = skilllv - 7;
  4840. 			}
  4841. 			else if(skilllv >=5) {
  4842. 				spellid = MG_SOULSTRIKE;
  4843. 				maxlv = skilllv - 4;
  4844. 			}
  4845. 			else if(skilllv >=2) {
  4846. 				int i = rand()%3;
  4847. 				spellid = spellarray[i];
  4848. 				maxlv = skilllv - 1;
  4849. 			}
  4850. 			else if(skilllv > 0) {
  4851. 				spellid = MG_NAPALMBEAT;
  4852. 				maxlv = 3;
  4853. 			}
  4854. 			if(spellid > 0)
  4855. 				sc_start4(src,SC_AUTOSPELL,100,skilllv,spellid,maxlv,0,
  4856. 					skill_get_time(SA_AUTOSPELL,skilllv));
  4857. 		}
  4858. 		break;
  4859.  
  4860. 	case BS_GREED:
  4861. 		if(sd){
  4862. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4863. 			map_foreachinrange(skill_greed,bl,
  4864. 				skill_get_splash(skillid, skilllv),BL_ITEM,bl);
  4865. 		}
  4866. 		break;
  4867.  
  4868. 	case SA_ELEMENTWATER:
  4869. 	case SA_ELEMENTFIRE:
  4870. 	case SA_ELEMENTGROUND:
  4871. 	case SA_ELEMENTWIND:
  4872. 		if(sd && !dstmd) //Only works on monsters.
  4873. 			break;
  4874. 		if(tstatus->mode&MD_BOSS)
  4875. 			break;
  4876. 	case NPC_ATTRICHANGE:
  4877. 	case NPC_CHANGEWATER:
  4878. 	case NPC_CHANGEGROUND:
  4879. 	case NPC_CHANGEFIRE:
  4880. 	case NPC_CHANGEWIND:
  4881. 	case NPC_CHANGEPOISON:
  4882. 	case NPC_CHANGEHOLY:
  4883. 	case NPC_CHANGEDARKNESS:
  4884. 	case NPC_CHANGETELEKINESIS:
  4885. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4886. 			sc_start2(bl, type, 100, skilllv, skill_get_ele(skillid,skilllv),
  4887. 				skill_get_time(skillid, skilllv)));
  4888. 		break;
  4889. 	case NPC_CHANGEUNDEAD:
  4890. 		//This skill should fail if target is wearing bathory/evil druid card [Brainstorm]
  4891. 		//TO-DO This is ugly, fix it
  4892. 		if(tstatus->def_ele==ELE_UNDEAD || tstatus->def_ele==ELE_DARK) break;
  4893. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4894. 			sc_start2(bl, type, 100, skilllv, skill_get_ele(skillid,skilllv),
  4895. 				skill_get_time(skillid, skilllv)));
  4896. 		break;
  4897.  
  4898. 	case NPC_PROVOCATION:
  4899. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4900. 		if (md) mob_unlocktarget(md, tick);
  4901. 		break;
  4902.  
  4903. 	case NPC_KEEPING:
  4904. 	case NPC_BARRIER:
  4905. 		{
  4906. 			int skill_time = skill_get_time(skillid,skilllv);
  4907. 			struct unit_data *ud = unit_bl2ud(bl);
  4908. 			if (clif_skill_nodamage(src,bl,skillid,skilllv,
  4909. 					sc_start(bl,type,100,skilllv,skill_time))
  4910. 			&& ud) {	//Disable attacking/acting/moving for skill's duration.
  4911. 				ud->attackabletime =
  4912. 				ud->canact_tick =
  4913. 				ud->canmove_tick = tick + skill_time;
  4914. 			}
  4915. 		}
  4916. 		break;
  4917.  
  4918. 	case NPC_REBIRTH:
  4919. 		if( md && md->state.rebirth )
  4920. 			break; // only works once
  4921. 		sc_start(bl,type,100,skilllv,-1);
  4922. 		break;
  4923.  
  4924. 	case NPC_DARKBLESSING:
  4925. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4926. 			sc_start2(bl,type,(50+skilllv*5),skilllv,skilllv,skill_get_time2(skillid,skilllv)));
  4927. 		break;
  4928.  
  4929. 	case NPC_LICK:
  4930. 		status_zap(bl, 0, 100);
  4931. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  4932. 			sc_start(bl,type,(skilllv*5),skilllv,skill_get_time2(skillid,skilllv)));
  4933. 		break;
  4934.  
  4935. 	case NPC_SUICIDE:
  4936. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  4937. 		status_kill(src); //When suiciding, neither exp nor drops is given.
  4938. 		break;
  4939.  
  4940. 	case NPC_SUMMONSLAVE:
  4941. 	case NPC_SUMMONMONSTER:
  4942. 		if(md && md->skillidx >= 0)
  4943. 			mob_summonslave(md,md->db->skill[md->skillidx].val,skilllv,skillid);
  4944. 		break;
  4945.  
  4946. 	case NPC_CALLSLAVE:
  4947. 		mob_warpslave(src,MOB_SLAVEDISTANCE);
  4948. 		break;
  4949.  
  4950. 	case NPC_RANDOMMOVE:
  4951. 		if (md) {
  4952. 			md->next_walktime = tick - 1;
  4953. 			mob_randomwalk(md,tick);
  4954. 		}
  4955. 		break;
  4956.  
  4957. 	case NPC_SPEEDUP:
  4958. 		{
  4959. 			// or does it increase casting rate? just a guess xD
  4960. 			int i = SC_ASPDPOTION0 + skilllv - 1;
  4961. 			if (i > SC_ASPDPOTION3)
  4962. 				i = SC_ASPDPOTION3;
  4963. 			clif_skill_nodamage(src,bl,skillid,skilllv,
  4964. 				sc_start(bl,(sc_type)i,100,skilllv,skilllv * 60000));
  4965. 		}
  4966. 		break;
  4967.  
  4968. 	case NPC_REVENGE:
  4969. 		// not really needed... but adding here anyway ^^
  4970. 		if (md && md->master_id > 0) {
  4971. 			struct block_list *mbl, *tbl;
  4972. 			if ((mbl = map_id2bl(md->master_id)) == NULL ||
  4973. 				(tbl = battle_gettargeted(mbl)) == NULL)
  4974. 				break;
  4975. 			md->state.provoke_flag = tbl->id;
  4976. 			mob_target(md, tbl, sstatus->rhw.range);
  4977. 		}
  4978. 		break;
  4979.  
  4980. 	case NPC_RUN:
  4981. 		{
  4982. 			const int mask[8][2] = {{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
  4983. 			int dir = (bl == src)?unit_getdir(src):map_calc_dir(src,bl->x,bl->y); //If cast on self, run forward, else run away.
  4984. 			unit_stop_attack(src);
  4985. 			//Run skillv tiles overriding the can-move check.
  4986. 			if (unit_walktoxy(src, src->x + skilllv * mask[dir][0], src->y + skilllv * mask[dir][1], 2) && md)
  4987. 				md->state.skillstate = MSS_WALK; //Otherwise it isn't updated in the ai.
  4988. 		}
  4989. 		break;
  4990.  
  4991. 	case NPC_TRANSFORMATION:
  4992. 	case NPC_METAMORPHOSIS:
  4993. 		if(md && md->skillidx >= 0) {
  4994. 			int class_ = mob_random_class (md->db->skill[md->skillidx].val,0);
  4995. 			if (skilllv > 1) //Multiply the rest of mobs. [Skotlex]
  4996. 				mob_summonslave(md,md->db->skill[md->skillidx].val,skilllv-1,skillid);
  4997. 			if (class_) mob_class_change(md, class_);
  4998. 		}
  4999. 		break;
  5000.  
  5001. 	case NPC_EMOTION_ON:
  5002. 	case NPC_EMOTION:
  5003. 		//va[0] is the emotion to use.
  5004. 		//NPC_EMOTION & NPC_EMOTION_ON can change a mob's mode 'permanently' [Skotlex]
  5005. 		//val[1] 'sets' the mode
  5006. 		//val[2] adds to the current mode
  5007. 		//val[3] removes from the current mode
  5008. 		//val[4] if set, asks to delete the previous mode change.
  5009. 		if(md && md->skillidx >= 0 && tsc)
  5010. 		{
  5011. 			clif_emotion(bl, md->db->skill[md->skillidx].val[0]);
  5012. 			if(md->db->skill[md->skillidx].val[4] && tsce)
  5013. 				status_change_end(bl, type, INVALID_TIMER);
  5014.  
  5015. 			if(md->db->skill[md->skillidx].val[1] || md->db->skill[md->skillidx].val[2])
  5016. 				sc_start4(src, type, 100, skilllv,
  5017. 					md->db->skill[md->skillidx].val[1],
  5018. 					md->db->skill[md->skillidx].val[2],
  5019. 					md->db->skill[md->skillidx].val[3],
  5020. 					skill_get_time(skillid, skilllv));
  5021. 		}
  5022. 		break;
  5023.  
  5024. 	case NPC_POWERUP:
  5025. 		sc_start(bl,SC_INCATKRATE,100,200,skill_get_time(skillid, skilllv));
  5026. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5027. 			sc_start(bl,type,100,100,skill_get_time(skillid, skilllv)));
  5028. 		break;
  5029.  
  5030. 	case NPC_AGIUP:
  5031. 		sc_start(bl,SC_SPEEDUP1,100,skilllv,skill_get_time(skillid, skilllv));
  5032. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5033. 			sc_start(bl,type,100,100,skill_get_time(skillid, skilllv)));
  5034. 		break;
  5035.  
  5036. 	case NPC_INVISIBLE:
  5037. 		//Have val4 passed as 6 is for "infinite cloak" (do not end on attack/skill use).
  5038. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5039. 			sc_start4(bl,type,100,skilllv,0,0,6,skill_get_time(skillid,skilllv)));
  5040. 		break;
  5041.  
  5042. 	case NPC_SIEGEMODE:
  5043. 		// not sure what it does
  5044. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5045. 		break;
  5046.  
  5047. 	case WE_MALE:
  5048. 		{
  5049. 			int hp_rate=(skilllv <= 0)? 0:skill_db[skillid].hp_rate[skilllv-1];
  5050. 			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]
  5051. 			clif_skill_nodamage(src,bl,skillid,status_heal(bl, gain_hp, 0, 0),1);
  5052. 		}
  5053. 		break;
  5054. 	case WE_FEMALE:
  5055. 		{
  5056. 			int sp_rate=(skilllv <= 0)? 0:skill_db[skillid].sp_rate[skilllv-1];
  5057. 			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]
  5058. 			clif_skill_nodamage(src,bl,skillid,status_heal(bl, 0, gain_sp, 0),1);
  5059. 		}
  5060. 		break;
  5061.  
  5062. 	// parent-baby skills
  5063. 	case WE_BABY:
  5064. 		if(sd){
  5065. 			struct map_session_data *f_sd = pc_get_father(sd);
  5066. 			struct map_session_data *m_sd = pc_get_mother(sd);
  5067. 			// if neither was found
  5068. 			if(!f_sd && !m_sd){
  5069. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5070. 				map_freeblock_unlock();
  5071. 				return 0;
  5072. 			}
  5073. 			status_change_start(bl,SC_STUN,10000,skilllv,0,0,0,skill_get_time2(skillid,skilllv),8);
  5074. 			if (f_sd) sc_start(&f_sd->bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  5075. 			if (m_sd) sc_start(&m_sd->bl,type,100,skilllv,skill_get_time(skillid,skilllv));
  5076. 		}
  5077. 		break;
  5078.  
  5079. 	case PF_HPCONVERSION:
  5080. 		{
  5081. 			int hp, sp;
  5082. 			hp = sstatus->max_hp/10;
  5083. 			sp = hp * 10 * skilllv / 100;
  5084. 			if (!status_charge(src,hp,0)) {
  5085. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5086. 				break;
  5087. 			}
  5088. 			clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  5089. 			status_heal(bl,0,sp,2);
  5090. 		}
  5091. 		break;
  5092.  
  5093. 	case MA_REMOVETRAP:
  5094. 	case HT_REMOVETRAP:
  5095. 		//FIXME: I think clif_skill_fail() is supposed to be sent if it fails below [ultramage]
  5096. 		clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  5097. 		{
  5098. 			struct skill_unit* su;
  5099. 			struct skill_unit_group* sg;
  5100. 			su = BL_CAST(BL_SKILL, bl);
  5101.  
  5102. 			// Mercenaries can remove any trap
  5103. 			// Players can only remove their own traps or traps on Vs maps.
  5104. 			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) )
  5105. 			{
  5106. 				if( sd && !(sg->unit_id == UNT_USED_TRAPS || (sg->unit_id == UNT_ANKLESNARE && sg->val2 != 0 )) )
  5107. 				{ // prevent picking up expired traps
  5108. 					if( battle_config.skill_removetrap_type )
  5109. 					{ // get back all items used to deploy the trap
  5110. 						for( i = 0; i < 10; i++ )
  5111. 						{
  5112. 							if( skill_db[su->group->skill_id].itemid[i] > 0 )
  5113. 							{
  5114. 								int flag;
  5115. 								struct item item_tmp;
  5116. 								memset(&item_tmp,0,sizeof(item_tmp));
  5117. 								item_tmp.nameid = skill_db[su->group->skill_id].itemid[i];
  5118. 								item_tmp.identify = 1;
  5119. 								if( item_tmp.nameid && (flag=pc_additem(sd,&item_tmp,skill_db[su->group->skill_id].amount[i])) )
  5120. 								{
  5121. 									clif_additem(sd,0,0,flag);
  5122. 									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);
  5123. 								}
  5124. 							}
  5125. 						}
  5126. 					}
  5127. 					else
  5128. 					{ // get back 1 trap
  5129. 						struct item item_tmp;
  5130. 						memset(&item_tmp,0,sizeof(item_tmp));
  5131. 						item_tmp.nameid = ITEMID_TRAP;
  5132. 						item_tmp.identify = 1;
  5133. 						if( item_tmp.nameid && (flag=pc_additem(sd,&item_tmp,1)) )
  5134. 						{
  5135. 							clif_additem(sd,0,0,flag);
  5136. 							map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5137. 						}
  5138. 					}
  5139. 				}
  5140. 				skill_delunit(su);
  5141. 			}
  5142. 		}
  5143. 		break;
  5144. 	case HT_SPRINGTRAP:
  5145. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5146. 		{
  5147. 			struct skill_unit *su=NULL;
  5148. 			if((bl->type==BL_SKILL) && (su=(struct skill_unit *)bl) && (su->group) ){
  5149. 				switch(su->group->unit_id){
  5150. 					case UNT_ANKLESNARE:	// ankle snare
  5151. 						if (su->group->val2 != 0)
  5152. 							// if it is already trapping something don't spring it,
  5153. 							// remove trap should be used instead
  5154. 							break;
  5155. 						// otherwise fallthrough to below
  5156. 					case UNT_BLASTMINE:
  5157. 					case UNT_SKIDTRAP:
  5158. 					case UNT_LANDMINE:
  5159. 					case UNT_SHOCKWAVE:
  5160. 					case UNT_SANDMAN:
  5161. 					case UNT_FLASHER:
  5162. 					case UNT_FREEZINGTRAP:
  5163. 					case UNT_CLAYMORETRAP:
  5164. 					case UNT_TALKIEBOX:
  5165. 						su->group->unit_id = UNT_USED_TRAPS;
  5166. 						clif_changetraplook(bl, UNT_USED_TRAPS);
  5167. 						su->group->limit=DIFF_TICK(tick+1500,su->group->tick);
  5168. 						su->limit=DIFF_TICK(tick+1500,su->group->tick);
  5169. 				}
  5170. 			}
  5171. 		}
  5172. 		break;
  5173. 	case BD_ENCORE:
  5174. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5175. 		if(sd)
  5176. 			unit_skilluse_id(src,src->id,sd->skillid_dance,sd->skilllv_dance);
  5177. 		break;
  5178.  
  5179. 	case AS_SPLASHER:
  5180. 		if(tstatus->mode&MD_BOSS || tstatus-> hp > tstatus->max_hp*3/4) {
  5181. 			if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5182. 			map_freeblock_unlock();
  5183. 			return 1;
  5184. 		}
  5185. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5186. 			sc_start4(bl,type,100,skilllv,skillid,src->id,skill_get_time(skillid,skilllv),1000));
  5187. 		if (sd) skill_blockpc_start (sd, skillid, skill_get_time(skillid, skilllv)+3000);
  5188. 		break;
  5189.  
  5190. 	case PF_MINDBREAKER:
  5191. 		{
  5192. 			if(tstatus->mode&MD_BOSS || battle_check_undead(tstatus->race,tstatus->def_ele))
  5193. 			{
  5194. 				map_freeblock_unlock();
  5195. 				return 1;
  5196. 			}
  5197.  
  5198. 			if (tsce)
  5199. 			{	//HelloKitty2 (?) explained that this silently fails when target is
  5200. 				//already inflicted. [Skotlex]
  5201. 				map_freeblock_unlock();
  5202. 				return 1;
  5203. 			}
  5204.  
  5205. 			//Has a 55% + skilllv*5% success chance.
  5206. 			if (!clif_skill_nodamage(src,bl,skillid,skilllv,
  5207. 				sc_start(bl,type,55+5*skilllv,skilllv,skill_get_time(skillid,skilllv))))
  5208. 			{
  5209. 				if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5210. 				map_freeblock_unlock();
  5211. 				return 0;
  5212. 			}
  5213.  
  5214. 			unit_skillcastcancel(bl,0);
  5215.  
  5216. 			if(tsc && tsc->count){
  5217. 				status_change_end(bl, SC_FREEZE, INVALID_TIMER);
  5218. 				if(tsc->data[SC_STONE] && tsc->opt1 == OPT1_STONE)
  5219. 					status_change_end(bl, SC_STONE, INVALID_TIMER);
  5220. 				status_change_end(bl, SC_SLEEP, INVALID_TIMER);
  5221. 			}
  5222.  
  5223. 			if(dstmd)
  5224. 				mob_target(dstmd,src,skill_get_range2(src,skillid,skilllv));
  5225. 		}
  5226. 		break;
  5227.  
  5228. 	case PF_SOULCHANGE:
  5229. 		{
  5230. 			unsigned int sp1 = 0, sp2 = 0;
  5231. 			if (dstmd) {
  5232. 				if (dstmd->state.soul_change_flag) {
  5233. 					if(sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5234. 					break;
  5235. 				}
  5236. 				dstmd->state.soul_change_flag = 1;
  5237. 				sp2 = sstatus->max_sp * 3 /100;
  5238. 				status_heal(src, 0, sp2, 2);
  5239. 				clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5240. 				break;
  5241. 			}
  5242. 			sp1 = sstatus->sp;
  5243. 			sp2 = tstatus->sp;
  5244. 			status_set_sp(src, sp2, 3);
  5245. 			status_set_sp(bl, sp1, 3);
  5246. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5247. 		}
  5248. 		break;
  5249.  
  5250. 	// Slim Pitcher
  5251. 	case CR_SLIMPITCHER:
  5252. 		// Updated to block Slim Pitcher from working on barricades and guardian stones.
  5253. 		if( dstmd && (dstmd->class_ == MOBID_EMPERIUM || (dstmd->class_ >= MOBID_BARRICADE1 && dstmd->class_ <= MOBID_GUARIDAN_STONE2)) )
  5254. 			break;
  5255. 		if (potion_hp || potion_sp) {
  5256. 			int hp = potion_hp, sp = potion_sp;
  5257. 			hp = hp * (100 + (tstatus->vit<<1))/100;
  5258. 			sp = sp * (100 + (tstatus->int_<<1))/100;
  5259. 			if (dstsd) {
  5260. 				if (hp)
  5261. 					hp = hp * (100 + pc_checkskill(dstsd,SM_RECOVERY)*10 + pc_skillheal2_bonus(dstsd, skillid))/100;
  5262. 				if (sp)
  5263. 					sp = sp * (100 + pc_checkskill(dstsd,MG_SRECOVERY)*10 + pc_skillheal2_bonus(dstsd, skillid))/100;
  5264. 			}
  5265. 			if (tsc && tsc->data[SC_CRITICALWOUND])
  5266. 			{
  5267. 				hp -= hp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  5268. 				sp -= sp * tsc->data[SC_CRITICALWOUND]->val2 / 100;
  5269. 			}
  5270. 			if(hp > 0)
  5271. 				clif_skill_nodamage(NULL,bl,AL_HEAL,hp,1);
  5272. 			if(sp > 0)
  5273. 				clif_skill_nodamage(NULL,bl,MG_SRECOVERY,sp,1);
  5274. 			status_heal(bl,hp,sp,0);
  5275. 		}
  5276. 		break;
  5277. 	// Full Chemical Protection
  5278. 	case CR_FULLPROTECTION:
  5279. 		{
  5280. 			int i, skilltime;
  5281. 			skilltime = skill_get_time(skillid,skilllv);
  5282. 			if (!tsc) {
  5283. 				clif_skill_nodamage(src,bl,skillid,skilllv,0);
  5284. 				break;
  5285. 			}
  5286. 			for (i=0; i<4; i++) {
  5287. 				status_change_end(bl, (sc_type)(SC_STRIPWEAPON + i), INVALID_TIMER);
  5288. 				sc_start(bl,(sc_type)(SC_CP_WEAPON + i),100,skilllv,skilltime);
  5289. 			}
  5290. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5291. 		}
  5292. 		break;
  5293.  
  5294. 	case RG_CLEANER:	//AppleGirl
  5295. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5296. 		break;
  5297.  
  5298. 	case CG_LONGINGFREEDOM:
  5299. 		{
  5300. 			if (tsc && !tsce && (tsce=tsc->data[SC_DANCING]) && tsce->val4
  5301. 				&& (tsce->val1&0xFFFF) != CG_MOONLIT) //Can't use Longing for Freedom while under Moonlight Petals. [Skotlex]
  5302. 			{
  5303. 				clif_skill_nodamage(src,bl,skillid,skilllv,
  5304. 					sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  5305. 			}
  5306. 		}
  5307. 		break;
  5308.  
  5309. 	case CG_TAROTCARD:
  5310. 		{
  5311. 			int eff, count = -1;
  5312. 			if( rand() % 100 > skilllv * 8 || (dstmd && ((dstmd->guardian_data && dstmd->class_ == MOBID_EMPERIUM) || mob_is_battleground(dstmd))) )
  5313. 			{
  5314. 				if( sd )
  5315. 					clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5316.  
  5317. 				map_freeblock_unlock();
  5318. 				return 0;
  5319. 			}
  5320. 			status_zap(src,0,skill_db[skill_get_index(skillid)].sp[skilllv]); // consume sp only if succeeded [Inkfish]
  5321. 			do {
  5322. 				eff = rand() % 14;
  5323. 				clif_specialeffect(bl, 523 + eff, AREA);
  5324. 				switch (eff)
  5325. 				{
  5326. 				case 0:	// heals SP to 0
  5327. 					status_percent_damage(src, bl, 0, 100, false);
  5328. 					break;
  5329. 				case 1:	// matk halved
  5330. 					sc_start(bl,SC_INCMATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5331. 					break;
  5332. 				case 2:	// all buffs removed
  5333. 					status_change_clear_buffs(bl,1);
  5334. 					break;
  5335. 				case 3:	// 1000 damage, random armor destroyed
  5336. 					{
  5337. 						int where[] = { EQP_ARMOR, EQP_SHIELD, EQP_HELM, EQP_SHOES, EQP_GARMENT };
  5338. 						status_fix_damage(src, bl, 1000, 0);
  5339. 						clif_damage(src,bl,tick,0,0,1000,0,0,0);
  5340. 						if( !status_isdead(bl) )
  5341. 							skill_break_equip(bl, where[rand()%3], 10000, BCT_ENEMY);
  5342. 					}
  5343. 					break;
  5344. 				case 4:	// atk halved
  5345. 					sc_start(bl,SC_INCATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5346. 					break;
  5347. 				case 5:	// 2000HP heal, random teleported
  5348. 					status_heal(src, 2000, 0, 0);
  5349. 					if( !map_flag_vs(bl->m) )
  5350. 						unit_warp(bl, -1,-1,-1, CLR_TELEPORT);
  5351. 					break;
  5352. 				case 6:	// random 2 other effects
  5353. 					if (count == -1)
  5354. 						count = 3;
  5355. 					else
  5356. 						count++; //Should not retrigger this one.
  5357. 					break;
  5358. 				case 7:	// stop freeze or stoned
  5359. 					{
  5360. 						enum sc_type sc[] = { SC_STOP, SC_FREEZE, SC_STONE };
  5361. 						sc_start(bl,sc[rand()%3],100,skilllv,skill_get_time2(skillid,skilllv));
  5362. 					}
  5363. 					break;
  5364. 				case 8:	// curse coma and poison
  5365. 					sc_start(bl,SC_COMA,100,skilllv,skill_get_time2(skillid,skilllv));
  5366. 					sc_start(bl,SC_CURSE,100,skilllv,skill_get_time2(skillid,skilllv));
  5367. 					sc_start(bl,SC_POISON,100,skilllv,skill_get_time2(skillid,skilllv));
  5368. 					break;
  5369. 				case 9:	// confusion
  5370. 					sc_start(bl,SC_CONFUSION,100,skilllv,skill_get_time2(skillid,skilllv));
  5371. 					break;
  5372. 				case 10:	// 6666 damage, atk matk halved, cursed
  5373. 					status_fix_damage(src, bl, 6666, 0);
  5374. 					clif_damage(src,bl,tick,0,0,6666,0,0,0);
  5375. 					sc_start(bl,SC_INCATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5376. 					sc_start(bl,SC_INCMATKRATE,100,-50,skill_get_time2(skillid,skilllv));
  5377. 					sc_start(bl,SC_CURSE,skilllv,100,skill_get_time2(skillid,skilllv));
  5378. 					break;
  5379. 				case 11:	// 4444 damage
  5380. 					status_fix_damage(src, bl, 4444, 0);
  5381. 					clif_damage(src,bl,tick,0,0,4444,0,0,0);
  5382. 					break;
  5383. 				case 12:	// stun
  5384. 					sc_start(bl,SC_STUN,100,skilllv,5000);
  5385. 					break;
  5386. 				case 13:	// atk,matk,hit,flee,def reduced
  5387. 					sc_start(bl,SC_INCATKRATE,100,-20,skill_get_time2(skillid,skilllv));
  5388. 					sc_start(bl,SC_INCMATKRATE,100,-20,skill_get_time2(skillid,skilllv));
  5389. 					sc_start(bl,SC_INCHITRATE,100,-20,skill_get_time2(skillid,skilllv));
  5390. 					sc_start(bl,SC_INCFLEERATE,100,-20,skill_get_time2(skillid,skilllv));
  5391. 					sc_start(bl,SC_INCDEFRATE,100,-20,skill_get_time2(skillid,skilllv));
  5392. 					break;
  5393. 				default:
  5394. 					break;
  5395. 				}
  5396. 			} while ((--count) > 0);
  5397. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5398. 		}
  5399. 		break;
  5400.  
  5401. 	case SL_ALCHEMIST:
  5402. 	case SL_ASSASIN:
  5403. 	case SL_BARDDANCER:
  5404. 	case SL_BLACKSMITH:
  5405. 	case SL_CRUSADER:
  5406. 	case SL_HUNTER:
  5407. 	case SL_KNIGHT:
  5408. 	case SL_MONK:
  5409. 	case SL_PRIEST:
  5410. 	case SL_ROGUE:
  5411. 	case SL_SAGE:
  5412. 	case SL_SOULLINKER:
  5413. 	case SL_STAR:
  5414. 	case SL_SUPERNOVICE:
  5415. 	case SL_WIZARD:
  5416. 		//NOTE: here, 'type' has the value of the associated MAPID, not of the SC_SPIRIT constant.
  5417. 		if (sd && !(dstsd && (dstsd->class_&MAPID_UPPERMASK) == type)) {
  5418. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5419. 			break;
  5420. 		}
  5421. 		if (skillid == SL_SUPERNOVICE && dstsd && dstsd->die_counter && !(rand()%100))
  5422. 		{	//Erase death count 1% of the casts
  5423. 			dstsd->die_counter = 0;
  5424. 			pc_setglobalreg(dstsd,"PC_DIE_COUNTER", 0);
  5425. 			clif_specialeffect(bl, 0x152, AREA);
  5426. 			//SC_SPIRIT invokes status_calc_pc for us.
  5427. 		}
  5428. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5429. 			sc_start4(bl,SC_SPIRIT,100,skilllv,skillid,0,0,skill_get_time(skillid,skilllv)));
  5430. 		sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA,skilllv));
  5431. 		break;
  5432. 	case SL_HIGH:
  5433. 		if (sd && !(dstsd && (dstsd->class_&JOBL_UPPER) && !(dstsd->class_&JOBL_2) && dstsd->status.base_level < 70)) {
  5434. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5435. 			break;
  5436. 		}
  5437. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5438. 			sc_start4(bl,type,100,skilllv,skillid,0,0,skill_get_time(skillid,skilllv)));
  5439. 		sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA,skilllv));
  5440. 		break;
  5441.  
  5442. 	case SL_SWOO:
  5443. 		if (tsce) {
  5444. 			sc_start(src,SC_STUN,100,skilllv,10000);
  5445. 			break;
  5446. 		}
  5447. 	case SL_SKA: // [marquis007]
  5448. 	case SL_SKE:
  5449. 		if (sd && !battle_config.allow_es_magic_pc && bl->type != BL_MOB) {
  5450. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5451. 			status_change_start(src,SC_STUN,10000,skilllv,0,0,0,500,10);
  5452. 			break;
  5453. 		}
  5454. 		clif_skill_nodamage(src,bl,skillid,skilllv,sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  5455. 		if (skillid == SL_SKE)
  5456. 			sc_start(src,SC_SMA,100,skilllv,skill_get_time(SL_SMA,skilllv));
  5457. 		break;
  5458.  
  5459. 	// New guild skills [Celest]
  5460. 	case GD_BATTLEORDER:
  5461. 		if(flag&1) {
  5462. 			if (status_get_guild_id(src) == status_get_guild_id(bl))
  5463. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv));
  5464. 		} else if (status_get_guild_id(src)) {
  5465. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5466. 			map_foreachinrange(skill_area_sub, src,
  5467. 				skill_get_splash(skillid, skilllv), BL_PC,
  5468. 				src,skillid,skilllv,tick, flag|BCT_GUILD|1,
  5469. 				skill_castend_nodamage_id);
  5470. 			if (sd)
  5471. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5472. 		}
  5473. 		break;
  5474. 	case GD_REGENERATION:
  5475. 		if(flag&1) {
  5476. 			if (status_get_guild_id(src) == status_get_guild_id(bl))
  5477. 				sc_start(bl,type,100,skilllv,skill_get_time(skillid, skilllv));
  5478. 		} else if (status_get_guild_id(src)) {
  5479. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5480. 			map_foreachinrange(skill_area_sub, src,
  5481. 				skill_get_splash(skillid, skilllv), BL_PC,
  5482. 				src,skillid,skilllv,tick, flag|BCT_GUILD|1,
  5483. 				skill_castend_nodamage_id);
  5484. 			if (sd)
  5485. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5486. 		}
  5487. 		break;
  5488. 	case GD_RESTORE:
  5489. 		if(flag&1) {
  5490. 			if (status_get_guild_id(src) == status_get_guild_id(bl))
  5491. 				clif_skill_nodamage(src,bl,AL_HEAL,status_percent_heal(bl,90,90),1);
  5492. 		} else if (status_get_guild_id(src)) {
  5493. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5494. 			map_foreachinrange(skill_area_sub, src,
  5495. 				skill_get_splash(skillid, skilllv), BL_PC,
  5496. 				src,skillid,skilllv,tick, flag|BCT_GUILD|1,
  5497. 				skill_castend_nodamage_id);
  5498. 			if (sd)
  5499. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5500. 		}
  5501. 		break;
  5502. 	case GD_EMERGENCYCALL:
  5503. 		{
  5504. 			int dx[9]={-1, 1, 0, 0,-1, 1,-1, 1, 0};
  5505. 			int dy[9]={ 0, 0, 1,-1, 1,-1,-1, 1, 0};
  5506. 			int j = 0;
  5507. 			struct guild *g = NULL;
  5508. 			// i don't know if it actually summons in a circle, but oh well. ;P
  5509. 			g = sd?sd->state.gmaster_flag:guild_search(status_get_guild_id(src));
  5510. 			if (!g)
  5511. 				break;
  5512. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5513. 			for(i = 0; i < g->max_member; i++, j++) {
  5514. 				if (j>8) j=0;
  5515. 				if ((dstsd = g->member[i].sd) != NULL && sd != dstsd && !dstsd->state.autotrade) {
  5516. 					if (map[dstsd->bl.m].flag.nowarp && !map_flag_gvg2(dstsd->bl.m))
  5517. 						continue;
  5518. 					if(map_getcell(src->m,src->x+dx[j],src->y+dy[j],CELL_CHKNOREACH))
  5519. 						dx[j] = dy[j] = 0;
  5520. 					pc_setpos(dstsd, map_id2index(src->m), src->x+dx[j], src->y+dy[j], CLR_RESPAWN);
  5521. 				}
  5522. 			}
  5523. 			if (sd)
  5524. 				guild_block_skill(sd,skill_get_time2(skillid,skilllv));
  5525. 		}
  5526. 		break;
  5527.  
  5528. 	case SG_FEEL:
  5529. 		//AuronX reported you CAN memorize the same map as all three. [Skotlex]
  5530. 		if (sd) {
  5531. 			if(!sd->feel_map[skilllv-1].index)
  5532. 				clif_feel_req(sd->fd,sd, skilllv);
  5533. 			else
  5534. 				clif_feel_info(sd, skilllv-1, 1);
  5535. 		}
  5536. 		break;
  5537.  
  5538. 	case SG_HATE:
  5539. 		if (sd) {
  5540. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5541. 			if (!pc_set_hate_mob(sd, skilllv-1, bl))
  5542. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5543. 		}
  5544. 		break;
  5545.  
  5546. 	case GS_GLITTERING:
  5547. 		if(sd) {
  5548. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5549. 			if(rand()%100 < (20+10*skilllv))
  5550. 				pc_addspiritball(sd,skill_get_time(skillid,skilllv),10);
  5551. 			else if(sd->spiritball > 0)
  5552. 				pc_delspiritball(sd,1,0);
  5553. 		}
  5554. 		break;
  5555.  
  5556. 	case GS_CRACKER:
  5557. 		if (!dstsd)	// according to latest patch, should not work on players [Reddozen]
  5558. 		{
  5559. 			i =65 -5*distance_bl(src,bl); //Base rate
  5560. 			if (i < 30) i = 30;
  5561. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5562. 			sc_start(bl,SC_STUN, i,skilllv,skill_get_time2(skillid,skilllv));
  5563. 		}
  5564. 		else if (sd)
  5565. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5566. 		break;
  5567.  
  5568. 	case AM_CALLHOMUN:	//[orn]
  5569. 		if (sd && !merc_call_homunculus(sd))
  5570. 			clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5571. 		break;
  5572.  
  5573. 	case AM_REST:
  5574. 		if (sd)
  5575. 		{
  5576. 			if (merc_hom_vaporize(sd,1))
  5577. 				clif_skill_nodamage(src, bl, skillid, skilllv, 1);
  5578. 			else
  5579. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  5580. 		}
  5581. 		break;
  5582.  
  5583. 	case HAMI_CASTLE:	//[orn]
  5584. 		if(rand()%100 < 20*skilllv && src != bl)
  5585. 		{
  5586. 			int x,y;
  5587. 			x = src->x;
  5588. 			y = src->y;
  5589. 			if (hd)
  5590. 				skill_blockhomun_start(hd, skillid, skill_get_time2(skillid,skilllv));
  5591.  
  5592. 			if (unit_movepos(src,bl->x,bl->y,0,0)) {
  5593. 				clif_skill_nodamage(src,src,skillid,skilllv,1); // Homunc
  5594. 				clif_slide(src,bl->x,bl->y) ;
  5595. 				if (unit_movepos(bl,x,y,0,0))
  5596. 				{
  5597. 					clif_skill_nodamage(bl,bl,skillid,skilllv,1); // Master
  5598. 					clif_slide(bl,x,y) ;
  5599. 				}
  5600.  
  5601. 				//TODO: Shouldn't also players and the like switch targets?
  5602. 				map_foreachinrange(skill_chastle_mob_changetarget,src,
  5603. 					AREA_SIZE, BL_MOB, bl, src);
  5604. 			}
  5605. 		}
  5606. 		// Failed
  5607. 		else if (hd && hd->master)
  5608. 			clif_skill_fail(hd->master, skillid, USESKILL_FAIL_LEVEL, 0);
  5609. 		else if (sd)
  5610. 			clif_skill_fail(sd, skillid, USESKILL_FAIL_LEVEL, 0);
  5611. 		break;
  5612. 	case HVAN_CHAOTIC:	//[orn]
  5613. 		{
  5614. 			static const int per[5][2]={{20,50},{50,60},{25,75},{60,64},{34,67}};
  5615. 			int rnd = rand()%100;
  5616. 			i = (skilllv-1)%5;
  5617. 			if(rnd<per[i][0]) //Self
  5618. 				bl = src;
  5619. 			else if(rnd<per[i][1]) //Master
  5620. 				bl = battle_get_master(src);
  5621. 			else //Enemy
  5622. 				bl = map_id2bl(battle_gettarget(src));
  5623.  
  5624. 			if (!bl) bl = src;
  5625. 			i = skill_calc_heal(src, bl, skillid, 1+rand()%skilllv, true);
  5626. 			//Eh? why double skill packet?
  5627. 			clif_skill_nodamage(src,bl,AL_HEAL,i,1);
  5628. 			clif_skill_nodamage(src,bl,skillid,i,1);
  5629. 			status_heal(bl, i, 0, 0);
  5630. 		}
  5631. 		break;
  5632. 	//Homun single-target support skills [orn]
  5633. 	case HAMI_BLOODLUST:
  5634. 	case HFLI_FLEET:
  5635. 	case HFLI_SPEED:
  5636. 	case HLIF_CHANGE:
  5637. 		clif_skill_nodamage(src,bl,skillid,skilllv,
  5638. 			sc_start(bl,type,100,skilllv,skill_get_time(skillid,skilllv)));
  5639. 		if (hd)
  5640. 			skill_blockhomun_start(hd, skillid, skill_get_time2(skillid,skilllv));
  5641. 		break;
  5642.  
  5643. 	case NPC_DRAGONFEAR:
  5644. 		if (flag&1) {
  5645. 			const enum sc_type sc[] = { SC_STUN, SC_SILENCE, SC_CONFUSION, SC_BLEEDING };
  5646. 			int j;
  5647. 			j = i = rand()%ARRAYLENGTH(sc);
  5648. 			while ( !sc_start(bl,sc[i],100,skilllv,skill_get_time2(skillid,i+1)) ) {
  5649. 				i++;
  5650. 				if ( i == ARRAYLENGTH(sc) )
  5651. 					i = 0;
  5652. 				if (i == j)
  5653. 					break;
  5654. 			}
  5655. 			break;
  5656. 		}
  5657. 	case NPC_WIDEBLEEDING:
  5658. 	case NPC_WIDECONFUSE:
  5659. 	case NPC_WIDECURSE:
  5660. 	case NPC_WIDEFREEZE:
  5661. 	case NPC_WIDESLEEP:
  5662. 	case NPC_WIDESILENCE:
  5663. 	case NPC_WIDESTONE:
  5664. 	case NPC_WIDESTUN:
  5665. 	case NPC_SLOWCAST:
  5666. 	case NPC_WIDEHELLDIGNITY:
  5667. 		if (flag&1)
  5668. 			sc_start(bl,type,100,skilllv,skill_get_time2(skillid,skilllv));
  5669. 		else {
  5670. 			skill_area_temp[2] = 0; //For SD_PREAMBLE
  5671. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5672. 			map_foreachinrange(skill_area_sub, bl,
  5673. 				skill_get_splash(skillid, skilllv),BL_CHAR,
  5674. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|SD_PREAMBLE|1,
  5675. 				skill_castend_nodamage_id);
  5676. 		}
  5677. 		break;
  5678. 	case NPC_WIDESOULDRAIN:
  5679. 		if (flag&1)
  5680. 			status_percent_damage(src,bl,0,((skilllv-1)%5+1)*20,false);
  5681. 		else {
  5682. 			skill_area_temp[2] = 0; //For SD_PREAMBLE
  5683. 			clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5684. 			map_foreachinrange(skill_area_sub, bl,
  5685. 				skill_get_splash(skillid, skilllv),BL_CHAR,
  5686. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|SD_PREAMBLE|1,
  5687. 				skill_castend_nodamage_id);
  5688. 		}
  5689. 		break;
  5690. 	case NPC_TALK:
  5691. 	case ALL_WEWISH:
  5692. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5693. 		break;
  5694. 	case ALL_BUYING_STORE:
  5695. 		if( sd )
  5696. 		{// players only, skill allows 5 buying slots
  5697. 			clif_skill_nodamage(src, bl, skillid, skilllv, buyingstore_setup(sd, MAX_BUYINGSTORE_SLOTS));
  5698. 		}
  5699. 		break;
  5700. 	default:
  5701. 		ShowWarning("skill_castend_nodamage_id: Unknown skill used:%d\n",skillid);
  5702. 		clif_skill_nodamage(src,bl,skillid,skilllv,1);
  5703. 		map_freeblock_unlock();
  5704. 		return 1;
  5705. 	}
  5706.  
  5707. 	if (dstmd) { //Mob skill event for no damage skills (damage ones are handled in battle_calc_damage) [Skotlex]
  5708. 		mob_log_damage(dstmd, src, 0); //Log interaction (counts as 'attacker' for the exp bonus)
  5709. 		mobskill_event(dstmd, src, tick, MSC_SKILLUSED|(skillid<<16));
  5710. 	}
  5711.  
  5712. 	if( sd && !(flag&1) )
  5713. 	{
  5714. 		if( sd->state.arrow_atk ) //Consume arrow on last invocation to this skill.
  5715. 			battle_consume_ammo(sd, skillid, skilllv);
  5716. 		skill_onskillusage(sd, bl, skillid, tick);
  5717. 		skill_consume_requirement(sd,skillid,skilllv,2);
  5718. 	}
  5719.  
  5720. 	map_freeblock_unlock();
  5721. 	return 0;
  5722. }
  5723.  
  5724. /*==========================================
  5725.  *
  5726.  *------------------------------------------*/
  5727. int skill_castend_id(int tid, unsigned int tick, int id, intptr_t data)
  5728. {
  5729. 	struct block_list *target, *src;
  5730. 	struct map_session_data *sd;
  5731. 	struct mob_data *md;
  5732. 	struct unit_data *ud;
  5733. 	struct status_change *sc = NULL;
  5734. 	int inf,inf2,flag = 0;
  5735.  
  5736. 	src = map_id2bl(id);
  5737. 	if( src == NULL )
  5738. 	{
  5739. 		ShowDebug("skill_castend_id: src == NULL (tid=%d, id=%d)\n", tid, id);
  5740. 		return 0;// not found
  5741. 	}
  5742.  
  5743. 	ud = unit_bl2ud(src);
  5744. 	if( ud == NULL )
  5745. 	{
  5746. 		ShowDebug("skill_castend_id: ud == NULL (tid=%d, id=%d)\n", tid, id);
  5747. 		return 0;// ???
  5748. 	}
  5749.  
  5750. 	sd = BL_CAST(BL_PC,  src);
  5751. 	md = BL_CAST(BL_MOB, src);
  5752.  
  5753. 	if( src->prev == NULL ) {
  5754. 		ud->skilltimer = INVALID_TIMER;
  5755. 		return 0;
  5756. 	}
  5757.  
  5758. 	if(ud->skillid != SA_CASTCANCEL )
  5759. 	{// otherwise handled in unit_skillcastcancel()
  5760. 		if( ud->skilltimer != tid ) {
  5761. 			ShowError("skill_castend_id: Timer mismatch %d!=%d!\n", ud->skilltimer, tid);
  5762. 			ud->skilltimer = INVALID_TIMER;
  5763. 			return 0;
  5764. 		}
  5765.  
  5766. 		if( sd && ud->skilltimer != INVALID_TIMER && pc_checkskill(sd,SA_FREECAST) > 0 )
  5767. 		{// restore original walk speed
  5768. 			ud->skilltimer = INVALID_TIMER;
  5769. 			status_calc_bl(&sd->bl, SCB_SPEED);
  5770. 		}
  5771.  
  5772. 		ud->skilltimer = INVALID_TIMER;
  5773. 	}
  5774.  
  5775. 	if (ud->skilltarget == id)
  5776. 		target = src;
  5777. 	else
  5778. 		target = map_id2bl(ud->skilltarget);
  5779.  
  5780. 	// Use a do so that you can break out of it when the skill fails.
  5781. 	do {
  5782. 		if(!target || target->prev==NULL) break;
  5783.  
  5784. 		if(src->m != target->m || status_isdead(src)) break;
  5785.  
  5786. 		switch (ud->skillid) {
  5787. 			//These should become skill_castend_pos
  5788. 			case WE_CALLPARTNER:
  5789. 				if(sd) clif_callpartner(sd);
  5790. 			case WE_CALLPARENT:
  5791. 			case WE_CALLBABY:
  5792. 			case AM_RESURRECTHOMUN:
  5793. 			case PF_SPIDERWEB:
  5794. 				//Find a random spot to place the skill. [Skotlex]
  5795. 				inf2 = skill_get_splash(ud->skillid, ud->skilllv);
  5796. 				ud->skillx = target->x + inf2;
  5797. 				ud->skilly = target->y + inf2;
  5798. 				if (inf2 && !map_random_dir(target, &ud->skillx, &ud->skilly)) {
  5799. 					ud->skillx = target->x;
  5800. 					ud->skilly = target->y;
  5801. 				}
  5802. 				ud->skilltimer=tid;
  5803. 				return skill_castend_pos(tid,tick,id,data);
  5804. 		}
  5805.  
  5806. 		if(ud->skillid == RG_BACKSTAP) {
  5807. 			int dir = map_calc_dir(src,target->x,target->y),t_dir = unit_getdir(target);
  5808. 			if(check_distance_bl(src, target, 0) || map_check_dir(dir,t_dir)) {
  5809. 				break;
  5810. 			}
  5811. 		}
  5812.  
  5813. 		if( ud->skillid == PR_TURNUNDEAD )
  5814. 		{
  5815. 			struct status_data *tstatus = status_get_status_data(target);
  5816. 			if( !battle_check_undead(tstatus->race, tstatus->def_ele) )
  5817. 				break;
  5818. 		}
  5819.  
  5820. 		if( ud->skillid == PR_LEXDIVINA || ud->skillid == MER_LEXDIVINA )
  5821. 		{
  5822. 			sc = status_get_sc(target);
  5823. 			if( battle_check_target(src,target, BCT_ENEMY) <= 0 && (!sc || !sc->data[SC_SILENCE]) )
  5824. 			{ //If it's not an enemy, and not silenced, you can't use the skill on them. [Skotlex]
  5825. 				clif_skill_nodamage (src, target, ud->skillid, ud->skilllv, 0);
  5826. 				break;
  5827. 			}
  5828. 		}
  5829. 		else
  5830. 		{ // Check target validity.
  5831. 			inf = skill_get_inf(ud->skillid);
  5832. 			inf2 = skill_get_inf2(ud->skillid);
  5833.  
  5834. 			if(inf&INF_ATTACK_SKILL ||
  5835. 				(inf&INF_SELF_SKILL && inf2&INF2_NO_TARGET_SELF)) //Combo skills
  5836. 				inf = BCT_ENEMY; //Offensive skill.
  5837. 			else if(inf2&INF2_NO_ENEMY)
  5838. 				inf = BCT_NOENEMY;
  5839. 			else
  5840. 				inf = 0;
  5841.  
  5842. 			if(inf2 & (INF2_PARTY_ONLY|INF2_GUILD_ONLY) && src != target)
  5843. 			{
  5844. 				inf |=
  5845. 					(inf2&INF2_PARTY_ONLY?BCT_PARTY:0)|
  5846. 					(inf2&INF2_GUILD_ONLY?BCT_GUILD:0);
  5847. 				//Remove neutral targets (but allow enemy if skill is designed to be so)
  5848. 				inf &= ~BCT_NEUTRAL;
  5849. 			}
  5850.  
  5851. 			if( ud->skillid >= SL_SKE && ud->skillid <= SL_SKA && target->type == BL_MOB )
  5852. 			{
  5853. 				if( ((TBL_MOB*)target)->class_ == MOBID_EMPERIUM )
  5854. 					break;
  5855. 			}
  5856. 			else
  5857. 			if (inf && battle_check_target(src, target, inf) <= 0)
  5858. 				break;
  5859.  
  5860. 			if(inf&BCT_ENEMY && (sc = status_get_sc(target)) &&
  5861. 				sc->data[SC_FOGWALL] &&
  5862. 				rand()%100 < 75)
  5863. 		  	{	//Fogwall makes all offensive-type targetted skills fail at 75%
  5864. 				if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  5865. 				break;
  5866. 			}
  5867. 		}
  5868.  
  5869. 		//Avoid doing double checks for instant-cast skills.
  5870. 		if (tid != INVALID_TIMER && !status_check_skilluse(src, target, ud->skillid, 1))
  5871. 			break;
  5872.  
  5873. 		if(md) {
  5874. 			md->last_thinktime=tick +MIN_MOBTHINKTIME;
  5875. 			if(md->skillidx >= 0 && md->db->skill[md->skillidx].emotion >= 0)
  5876. 				clif_emotion(src, md->db->skill[md->skillidx].emotion);
  5877. 		}
  5878.  
  5879. 		if(src != target && battle_config.skill_add_range &&
  5880. 			!check_distance_bl(src, target, skill_get_range2(src,ud->skillid,ud->skilllv)+battle_config.skill_add_range))
  5881. 		{
  5882. 			if (sd) {
  5883. 				clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  5884. 				if(battle_config.skill_out_range_consume) //Consume items anyway. [Skotlex]
  5885. 					skill_consume_requirement(sd,ud->skillid,ud->skilllv,3);
  5886. 			}
  5887. 			break;
  5888. 		}
  5889.  
  5890. 		if( sd )
  5891. 		{
  5892. 			if( !skill_check_condition_castend(sd, ud->skillid, ud->skilllv) )
  5893. 				break;
  5894. 			else
  5895. 				skill_consume_requirement(sd,ud->skillid,ud->skilllv,1);
  5896. 		}
  5897.  
  5898. 		if( (src->type == BL_MER || src->type == BL_HOM) && !skill_check_condition_mercenary(src, ud->skillid, ud->skilllv, 1) )
  5899. 			break;
  5900.  
  5901. 		if (ud->state.running && ud->skillid == TK_JUMPKICK)
  5902. 			flag = 1;
  5903.  
  5904. 		if (ud->walktimer != INVALID_TIMER && ud->skillid != TK_RUN)
  5905. 			unit_stop_walking(src,1);
  5906.  
  5907. 		if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  5908. 			ud->canact_tick = tick + skill_delayfix(src, ud->skillid, ud->skilllv); //Tests show wings don't overwrite the delay but skill scrolls do. [Inkfish]
  5909. 		if( battle_config.display_status_timers && sd )
  5910. 			clif_status_change(src, SI_ACTIONDELAY, 1, skill_delayfix(src, ud->skillid, ud->skilllv));
  5911. 		if( sd )
  5912. 		{
  5913. 			switch( ud->skillid )
  5914. 			{
  5915. 			case GS_DESPERADO:
  5916. 				sd->canequip_tick = tick + skill_get_time(ud->skillid, ud->skilllv);
  5917. 				break;
  5918. 			case CR_GRANDCROSS:
  5919. 			case NPC_GRANDDARKNESS:
  5920. 				if( (sc = status_get_sc(src)) && sc->data[SC_STRIPSHIELD] )
  5921. 				{
  5922. 					const struct TimerData *timer = get_timer(sc->data[SC_STRIPSHIELD]->timer);
  5923. 					if( timer && timer->func == status_change_timer && DIFF_TICK(timer->tick,gettick()+skill_get_time(ud->skillid, ud->skilllv)) > 0 )
  5924. 						break;
  5925. 				}
  5926. 				sc_start2(src, SC_STRIPSHIELD, 100, 0, 1, skill_get_time(ud->skillid, ud->skilllv));
  5927. 				break;
  5928. 			}
  5929. 		}
  5930. 		if (skill_get_state(ud->skillid) != ST_MOVE_ENABLE)
  5931. 			unit_set_walkdelay(src, tick, battle_config.default_walk_delay+skill_get_walkdelay(ud->skillid, ud->skilllv), 1);
  5932.  
  5933. 		if(battle_config.skill_log && battle_config.skill_log&src->type)
  5934. 			ShowInfo("Type %d, ID %d skill castend id [id =%d, lv=%d, target ID %d]\n",
  5935. 				src->type, src->id, ud->skillid, ud->skilllv, target->id);
  5936.  
  5937. 		map_freeblock_lock();
  5938. 		if (skill_get_casttype(ud->skillid) == CAST_NODAMAGE)
  5939. 			skill_castend_nodamage_id(src,target,ud->skillid,ud->skilllv,tick,flag);
  5940. 		else
  5941. 			skill_castend_damage_id(src,target,ud->skillid,ud->skilllv,tick,flag);
  5942.  
  5943. 		sc = status_get_sc(src);
  5944. 		if(sc && sc->count) {
  5945. 		  	if(sc->data[SC_MAGICPOWER] &&
  5946. 				ud->skillid != HW_MAGICPOWER && ud->skillid != WZ_WATERBALL)
  5947. 				status_change_end(src, SC_MAGICPOWER, INVALID_TIMER);
  5948. 			if(sc->data[SC_SPIRIT] &&
  5949. 				sc->data[SC_SPIRIT]->val2 == SL_WIZARD &&
  5950. 				sc->data[SC_SPIRIT]->val3 == ud->skillid &&
  5951. 			  	ud->skillid != WZ_WATERBALL)
  5952. 				sc->data[SC_SPIRIT]->val3 = 0; //Clear bounced spell check.
  5953.  
  5954. 			if( sc->data[SC_DANCING] && skill_get_inf2(ud->skillid)&INF2_SONG_DANCE && sd )
  5955. 				skill_blockpc_start(sd,BD_ADAPTATION,3000);
  5956. 		}
  5957.  
  5958. 		if( sd && ud->skillid != SA_ABRACADABRA ) // Hocus-Pocus has just set the data so leave it as it is.[Inkfish]
  5959. 			sd->skillitem = sd->skillitemlv = 0;
  5960.  
  5961. 		if (ud->skilltimer == INVALID_TIMER) {
  5962. 			if(md) md->skillidx = -1;
  5963. 			else ud->skillid = 0; //mobs can't clear this one as it is used for skill condition 'afterskill'
  5964. 			ud->skilllv = ud->skilltarget = 0;
  5965. 		}
  5966. 		map_freeblock_unlock();
  5967. 		return 1;
  5968. 	} while(0);
  5969.  
  5970. 	//Skill failed.
  5971. 	if (ud->skillid == MO_EXTREMITYFIST && sd && !(sc && sc->data[SC_FOGWALL]))
  5972.   	{	//When Asura fails... (except when it fails from Fog of Wall)
  5973. 		//Consume SP/spheres
  5974. 		skill_consume_requirement(sd,ud->skillid, ud->skilllv,1);
  5975. 		status_set_sp(src, 0, 0);
  5976. 		sc = &sd->sc;
  5977. 		if (sc->count)
  5978. 		{	//End states
  5979. 			status_change_end(src, SC_EXPLOSIONSPIRITS, INVALID_TIMER);
  5980. 			status_change_end(src, SC_BLADESTOP, INVALID_TIMER);
  5981. 		}
  5982. 		if (target && target->m == src->m)
  5983. 		{	//Move character to target anyway.
  5984. 			int dx,dy;
  5985. 			dx = target->x - src->x;
  5986. 			dy = target->y - src->y;
  5987. 			if(dx > 0) dx++;
  5988. 			else if(dx < 0) dx--;
  5989. 			if (dy > 0) dy++;
  5990. 			else if(dy < 0) dy--;
  5991.  
  5992. 			if (unit_movepos(src, src->x+dx, src->y+dy, 1, 1))
  5993. 			{	//Display movement + animation.
  5994. 				clif_slide(src,src->x,src->y);
  5995. 				clif_skill_damage(src,target,tick,sd->battle_status.amotion,0,0,1,ud->skillid, ud->skilllv, 5);
  5996. 			}
  5997. 			clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  5998. 		}
  5999. 	}
  6000.  
  6001. 	ud->skillid = ud->skilllv = ud->skilltarget = 0;
  6002. 	if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  6003. 		ud->canact_tick = tick;
  6004. 	//You can't place a skill failed packet here because it would be
  6005. 	//sent in ALL cases, even cases where skill_check_condition fails
  6006. 	//which would lead to double 'skill failed' messages u.u [Skotlex]
  6007. 	if(sd)
  6008. 		sd->skillitem = sd->skillitemlv = 0;
  6009. 	else if(md)
  6010. 		md->skillidx = -1;
  6011. 	return 0;
  6012. }
  6013.  
  6014. /*==========================================
  6015.  *
  6016.  *------------------------------------------*/
  6017. int skill_castend_pos(int tid, unsigned int tick, int id, intptr_t data)
  6018. {
  6019. 	struct block_list* src = map_id2bl(id);
  6020. 	int maxcount;
  6021. 	struct map_session_data *sd;
  6022. 	struct unit_data *ud = unit_bl2ud(src);
  6023. 	struct mob_data *md;
  6024.  
  6025. 	nullpo_ret(ud);
  6026.  
  6027. 	sd = BL_CAST(BL_PC , src);
  6028. 	md = BL_CAST(BL_MOB, src);
  6029.  
  6030. 	if( src->prev == NULL ) {
  6031. 		ud->skilltimer = INVALID_TIMER;
  6032. 		return 0;
  6033. 	}
  6034.  
  6035. 	if( ud->skilltimer != tid )
  6036. 	{
  6037. 		ShowError("skill_castend_pos: Timer mismatch %d!=%d\n", ud->skilltimer, tid);
  6038. 		ud->skilltimer = INVALID_TIMER;
  6039. 		return 0;
  6040. 	}
  6041.  
  6042. 	if( sd && ud->skilltimer != INVALID_TIMER && pc_checkskill(sd,SA_FREECAST) > 0 )
  6043. 	{// restore original walk speed
  6044. 		ud->skilltimer = INVALID_TIMER;
  6045. 		status_calc_bl(&sd->bl, SCB_SPEED);
  6046. 	}
  6047. 	ud->skilltimer = INVALID_TIMER;
  6048.  
  6049. 	do {
  6050. 		if( status_isdead(src) )
  6051. 			break;
  6052.  
  6053. 		if( !(src->type&battle_config.skill_reiteration) &&
  6054. 			skill_get_unit_flag(ud->skillid)&UF_NOREITERATION &&
  6055. 			skill_check_unit_range(src,ud->skillx,ud->skilly,ud->skillid,ud->skilllv)
  6056. 		  )
  6057. 		{
  6058. 			if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6059. 			break;
  6060. 		}
  6061. 		if( src->type&battle_config.skill_nofootset &&
  6062. 			skill_get_unit_flag(ud->skillid)&UF_NOFOOTSET &&
  6063. 			skill_check_unit_range2(src,ud->skillx,ud->skilly,ud->skillid,ud->skilllv)
  6064. 		  )
  6065. 		{
  6066. 			if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6067. 			break;
  6068. 		}
  6069. 		if( src->type&battle_config.land_skill_limit &&
  6070. 			(maxcount = skill_get_maxcount(ud->skillid, ud->skilllv)) > 0
  6071. 		  ) {
  6072. 			int i;
  6073. 			for(i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i] && maxcount;i++) {
  6074. 				if(ud->skillunit[i]->skill_id == ud->skillid)
  6075. 					maxcount--;
  6076. 			}
  6077. 			if( maxcount == 0 )
  6078. 			{
  6079. 				if (sd) clif_skill_fail(sd,ud->skillid,USESKILL_FAIL_LEVEL,0);
  6080. 				break;
  6081. 			}
  6082. 		}
  6083.  
  6084. 		if(tid != INVALID_TIMER)
  6085. 		{	//Avoid double checks on instant cast skills. [Skotlex]
  6086. 			if (!status_check_skilluse(src, NULL, ud->skillid, 1))
  6087. 				break;
  6088. 			if(battle_config.skill_add_range &&
  6089. 				!check_distance_blxy(src, ud->skillx, ud->skilly, skill_get_range2(src,ud->skillid,ud->skilllv)+battle_config.skill_add_range)) {
  6090. 				if (sd && battle_config.skill_out_range_consume) //Consume items anyway.
  6091. 					skill_consume_requirement(sd,ud->skillid,ud->skilllv,3);
  6092. 				break;
  6093. 			}
  6094. 		}
  6095.  
  6096. 		if( sd )
  6097. 		{
  6098. 			if( !skill_check_condition_castend(sd, ud->skillid, ud->skilllv) )
  6099. 				break;
  6100. 			else
  6101. 				skill_consume_requirement(sd,ud->skillid,ud->skilllv,1);
  6102. 		}
  6103.  
  6104. 		if( (src->type == BL_MER || src->type == BL_HOM) && !skill_check_condition_mercenary(src, ud->skillid, ud->skilllv, 1) )
  6105. 			break;
  6106.  
  6107. 		if(md) {
  6108. 			md->last_thinktime=tick +MIN_MOBTHINKTIME;
  6109. 			if(md->skillidx >= 0 && md->db->skill[md->skillidx].emotion >= 0)
  6110. 				clif_emotion(src, md->db->skill[md->skillidx].emotion);
  6111. 		}
  6112.  
  6113. 		if(battle_config.skill_log && battle_config.skill_log&src->type)
  6114. 			ShowInfo("Type %d, ID %d skill castend pos [id =%d, lv=%d, (%d,%d)]\n",
  6115. 				src->type, src->id, ud->skillid, ud->skilllv, ud->skillx, ud->skilly);
  6116.  
  6117. 		if (ud->walktimer != INVALID_TIMER)
  6118. 			unit_stop_walking(src,1);
  6119.  
  6120. 		if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  6121. 			ud->canact_tick = tick + skill_delayfix(src, ud->skillid, ud->skilllv);
  6122. 		if( battle_config.display_status_timers && sd )
  6123. 			clif_status_change(src, SI_ACTIONDELAY, 1, skill_delayfix(src, ud->skillid, ud->skilllv));
  6124. //		if( sd )
  6125. //		{
  6126. //			switch( ud->skillid )
  6127. //			{
  6128. //			case ????:
  6129. //				sd->canequip_tick = tick + ????;
  6130. //				break;
  6131. //			}
  6132. //		}
  6133. 		unit_set_walkdelay(src, tick, battle_config.default_walk_delay+skill_get_walkdelay(ud->skillid, ud->skilllv), 1);
  6134.  
  6135. 		map_freeblock_lock();
  6136. 		skill_castend_pos2(src,ud->skillx,ud->skilly,ud->skillid,ud->skilllv,tick,0);
  6137.  
  6138. 		if( sd && sd->skillitem != AL_WARP ) // Warp-Portal thru items will clear data in skill_castend_map. [Inkfish]
  6139. 			sd->skillitem = sd->skillitemlv = 0;
  6140.  
  6141. 		if (ud->skilltimer == INVALID_TIMER) {
  6142. 			if (md) md->skillidx = -1;
  6143. 			else ud->skillid = 0; //Non mobs can't clear this one as it is used for skill condition 'afterskill'
  6144. 			ud->skilllv = ud->skillx = ud->skilly = 0;
  6145. 		}
  6146.  
  6147. 		map_freeblock_unlock();
  6148. 		return 1;
  6149. 	} while(0);
  6150.  
  6151. 	if( !sd || sd->skillitem != ud->skillid || skill_get_delay(ud->skillid,ud->skilllv) )
  6152. 		ud->canact_tick = tick;
  6153. 	ud->skillid = ud->skilllv = 0;
  6154. 	if(sd)
  6155. 		sd->skillitem = sd->skillitemlv = 0;
  6156. 	else if(md)
  6157. 		md->skillidx  = -1;
  6158. 	return 0;
  6159.  
  6160. }
  6161.  
  6162. /*==========================================
  6163.  *
  6164.  *------------------------------------------*/
  6165. int skill_castend_pos2(struct block_list* src, int x, int y, int skillid, int skilllv, unsigned int tick, int flag)
  6166. {
  6167. 	struct map_session_data* sd;
  6168. 	struct status_change* sc;
  6169. 	struct status_change_entry *sce;
  6170. 	struct skill_unit_group* sg;
  6171. 	enum sc_type type;
  6172. 	int i;
  6173.  
  6174. 	//if(skilllv <= 0) return 0;
  6175. 	if(skillid > 0 && skilllv <= 0) return 0;	// celest
  6176.  
  6177. 	nullpo_ret(src);
  6178.  
  6179. 	if(status_isdead(src))
  6180. 		return 0;
  6181.  
  6182. 	sd = BL_CAST(BL_PC, src);
  6183.  
  6184. 	sc = status_get_sc(src);
  6185. 	type = status_skill2sc(skillid);
  6186. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  6187.  
  6188. 	switch (skillid) { //Skill effect.
  6189. 		case WZ_METEOR:
  6190. 		case MO_BODYRELOCATION:
  6191. 		case CR_CULTIVATION:
  6192. 		case HW_GANBANTEIN:
  6193. 			break; //Effect is displayed on respective switch case.
  6194. 		default:
  6195. 			if(skill_get_inf(skillid)&INF_SELF_SKILL)
  6196. 				clif_skill_nodamage(src,src,skillid,skilllv,1);
  6197. 			else
  6198. 				clif_skill_poseffect(src,skillid,skilllv,x,y,tick);
  6199. 	}
  6200.  
  6201. 	switch(skillid)
  6202. 	{
  6203. 	case PR_BENEDICTIO:
  6204. 		skill_area_temp[1] = src->id;
  6205. 		i = skill_get_splash(skillid, skilllv);
  6206. 		map_foreachinarea(skill_area_sub,
  6207. 			src->m, x-i, y-i, x+i, y+i, BL_PC,
  6208. 			src, skillid, skilllv, tick, flag|BCT_ALL|1,
  6209. 			skill_castend_nodamage_id);
  6210. 		map_foreachinarea(skill_area_sub,
  6211. 			src->m, x-i, y-i, x+i, y+i, BL_CHAR,
  6212. 			src, skillid, skilllv, tick, flag|BCT_ENEMY|1,
  6213. 			skill_castend_damage_id);
  6214. 		break;
  6215.  
  6216. 	case BS_HAMMERFALL:
  6217. 		i = skill_get_splash(skillid, skilllv);
  6218. 		map_foreachinarea (skill_area_sub,
  6219. 			src->m, x-i, y-i, x+i, y+i, BL_CHAR,
  6220. 			src, skillid, skilllv, tick, flag|BCT_ENEMY|2,
  6221. 			skill_castend_nodamage_id);
  6222. 		break;
  6223.  
  6224. 	case HT_DETECTING:
  6225. 		i = skill_get_splash(skillid, skilllv);
  6226. 		map_foreachinarea( status_change_timer_sub,
  6227. 			src->m, x-i, y-i, x+i,y+i,BL_CHAR,
  6228. 			src,NULL,SC_SIGHT,tick);
  6229. 		if(battle_config.traps_setting&1)
  6230. 		map_foreachinarea( skill_reveal_trap,
  6231. 			src->m, x-i, y-i, x+i,y+i,BL_SKILL);
  6232. 		break;
  6233.  
  6234. 	case SA_VOLCANO:
  6235. 	case SA_DELUGE:
  6236. 	case SA_VIOLENTGALE:
  6237. 	{	//Does not consumes if the skill is already active. [Skotlex]
  6238. 		struct skill_unit_group *sg;
  6239. 		if ((sg= skill_locate_element_field(src)) != NULL && ( sg->skill_id == SA_VOLCANO || sg->skill_id == SA_DELUGE || sg->skill_id == SA_VIOLENTGALE ))
  6240. 		{
  6241. 			if (sg->limit - DIFF_TICK(gettick(), sg->tick) > 0)
  6242. 			{
  6243. 				skill_unitsetting(src,skillid,skilllv,x,y,0);
  6244. 				return 0; // not to consume items
  6245. 			}
  6246. 			else
  6247. 				sg->limit = 0; //Disable it.
  6248. 		}
  6249. 		skill_unitsetting(src,skillid,skilllv,x,y,0);
  6250. 		break;
  6251. 	}
  6252. 	case MG_SAFETYWALL:
  6253. 	case MG_FIREWALL:
  6254. 	case MG_THUNDERSTORM:
  6255. 	case AL_PNEUMA:
  6256. 	case WZ_ICEWALL:
  6257. 	case WZ_FIREPILLAR:
  6258. 	case WZ_QUAGMIRE:
  6259. 	case WZ_VERMILION:
  6260. 	case WZ_STORMGUST:
  6261. 	case WZ_HEAVENDRIVE:
  6262. 	case PR_SANCTUARY:
  6263. 	case PR_MAGNUS:
  6264. 	case CR_GRANDCROSS:
  6265. 	case NPC_GRANDDARKNESS:
  6266. 	case HT_SKIDTRAP:
  6267. 	case MA_SKIDTRAP:
  6268. 	case HT_LANDMINE:
  6269. 	case MA_LANDMINE:
  6270. 	case HT_ANKLESNARE:
  6271. 	case HT_SHOCKWAVE:
  6272. 	case HT_SANDMAN:
  6273. 	case MA_SANDMAN:
  6274. 	case HT_FLASHER:
  6275. 	case HT_FREEZINGTRAP:
  6276. 	case MA_FREEZINGTRAP:
  6277. 	case HT_BLASTMINE:
  6278. 	case HT_CLAYMORETRAP:
  6279. 	case AS_VENOMDUST:
  6280. 	case AM_DEMONSTRATION:
  6281. 	case PF_FOGWALL:
  6282. 	case PF_SPIDERWEB:
  6283. 	case HT_TALKIEBOX:
  6284. 	case WE_CALLPARTNER:
  6285. 	case WE_CALLPARENT:
  6286. 	case WE_CALLBABY:
  6287. 	case AC_SHOWER:	//Ground-placed skill implementation.
  6288. 	case MA_SHOWER:
  6289. 	case SA_LANDPROTECTOR:
  6290. 	case BD_LULLABY:
  6291. 	case BD_RICHMANKIM:
  6292. 	case BD_ETERNALCHAOS:
  6293. 	case BD_DRUMBATTLEFIELD:
  6294. 	case BD_RINGNIBELUNGEN:
  6295. 	case BD_ROKISWEIL:
  6296. 	case BD_INTOABYSS:
  6297. 	case BD_SIEGFRIED:
  6298. 	case BA_DISSONANCE:
  6299. 	case BA_POEMBRAGI:
  6300. 	case BA_WHISTLE:
  6301. 	case BA_ASSASSINCROSS:
  6302. 	case BA_APPLEIDUN:
  6303. 	case DC_UGLYDANCE:
  6304. 	case DC_HUMMING:
  6305. 	case DC_DONTFORGETME:
  6306. 	case DC_FORTUNEKISS:
  6307. 	case DC_SERVICEFORYOU:
  6308. 	case CG_MOONLIT:
  6309. 	case GS_DESPERADO:
  6310. 	case NJ_KAENSIN:
  6311. 	case NJ_BAKUENRYU:
  6312. 	case NJ_SUITON:
  6313. 	case NJ_HYOUSYOURAKU:
  6314. 	case NJ_RAIGEKISAI:
  6315. 	case NJ_KAMAITACHI:
  6316. 	case NPC_EVILLAND:
  6317. 		flag|=1;//Set flag to 1 to prevent deleting ammo (it will be deleted on group-delete).
  6318. 	case GS_GROUNDDRIFT: //Ammo should be deleted right away.
  6319. 		skill_unitsetting(src,skillid,skilllv,x,y,0);
  6320. 		break;
  6321. 	case RG_GRAFFITI:			/* Graffiti [Valaris] */
  6322. 		skill_clear_unitgroup(src);
  6323. 		skill_unitsetting(src,skillid,skilllv,x,y,0);
  6324. 		flag|=1;
  6325. 		break;
  6326. 	case HP_BASILICA:
  6327. 		if( sc->data[SC_BASILICA] )
  6328. 			status_change_end(src, SC_BASILICA, INVALID_TIMER); // Cancel Basilica
  6329. 		else
  6330. 		{ // Create Basilica. Start SC on caster. Unit timer start SC on others.
  6331. 			skill_clear_unitgroup(src);
  6332. 			if( skill_unitsetting(src,skillid,skilllv,x,y,0) )
  6333. 				sc_start4(src,type,100,skilllv,0,0,src->id,skill_get_time(skillid,skilllv));
  6334. 			flag|=1;
  6335. 		}
  6336. 		break;
  6337. 	case CG_HERMODE:
  6338. 		skill_clear_unitgroup(src);
  6339. 		if ((sg = skill_unitsetting(src,skillid,skilllv,x,y,0)))
  6340. 			sc_start4(src,SC_DANCING,100,
  6341. 				skillid,0,skilllv,sg->group_id,skill_get_time(skillid,skilllv));
  6342. 		flag|=1;
  6343. 		break;
  6344. 	case RG_CLEANER: // [Valaris]
  6345. 		i = skill_get_splash(skillid, skilllv);
  6346. 		map_foreachinarea(skill_graffitiremover,src->m,x-i,y-i,x+i,y+i,BL_SKILL);
  6347. 		break;
  6348.  
  6349. 	case WZ_METEOR:
  6350. 		{
  6351. 			int flag = 0, area = skill_get_splash(skillid, skilllv);
  6352. 			short tmpx = 0, tmpy = 0, x1 = 0, y1 = 0;
  6353.  
  6354. 			if( sc && sc->data[SC_MAGICPOWER] )
  6355. 				flag = flag|2; //Store the magic power flag for future use. [Skotlex]
  6356.  
  6357. 			for( i = 0; i < 2 + (skilllv>>1); i++ )
  6358. 			{
  6359. 				// Creates a random Cell in the Splash Area
  6360. 				tmpx = x - area + rand()%(area * 2 + 1);
  6361. 				tmpy = y - area + rand()%(area * 2 + 1);
  6362.  
  6363. 				if( i == 0 && path_search_long(NULL, src->m, src->x, src->y, tmpx, tmpy, CELL_CHKWALL) )
  6364. 					clif_skill_poseffect(src,skillid,skilllv,tmpx,tmpy,tick);
  6365.  
  6366. 				if( i > 0 )
  6367. 					skill_addtimerskill(src,tick+i*1000,0,tmpx,tmpy,skillid,skilllv,(x1<<16)|y1,flag&2); //Only pass the Magic Power flag
  6368.  
  6369. 				x1 = tmpx;
  6370. 				y1 = tmpy;
  6371. 			}
  6372.  
  6373. 			skill_addtimerskill(src,tick+i*1000,0,tmpx,tmpy,skillid,skilllv,-1,flag&2);
  6374. 		}
  6375. 		break;
  6376.  
  6377. 	case AL_WARP:
  6378. 		if(sd)
  6379. 		{
  6380. 			clif_skill_warppoint(sd, skillid, skilllv, sd->status.save_point.map,
  6381. 				(skilllv >= 2) ? sd->status.memo_point[0].map : 0,
  6382. 				(skilllv >= 3) ? sd->status.memo_point[1].map : 0,
  6383. 				(skilllv >= 4) ? sd->status.memo_point[2].map : 0
  6384. 			);
  6385. 		}
  6386. 		return 0; // not to consume item.
  6387.  
  6388. 	case MO_BODYRELOCATION:
  6389. 		if (unit_movepos(src, x, y, 1, 1)) {
  6390. 			clif_skill_poseffect(src,skillid,skilllv,src->x,src->y,tick);
  6391. //			clif_slide(src, src->x, src->y); //Poseffect is the one that makes the char snap on the client...
  6392. 			//if (sd) skill_blockpc_start (sd, MO_EXTREMITYFIST, 2000);
  6393. 		}
  6394. 		break;
  6395. 	case NJ_SHADOWJUMP:
  6396. 		if( !map_flag_gvg(src->m) && !map[src->m].flag.battleground )
  6397. 		{	//You don't move on GVG grounds.
  6398. 			unit_movepos(src, x, y, 1, 0);
  6399. 			clif_slide(src,x,y);
  6400. 		}
  6401. 		status_change_end(src, SC_HIDING, INVALID_TIMER);
  6402. 		break;
  6403. 	case AM_SPHEREMINE:
  6404. 	case AM_CANNIBALIZE:
  6405. 		{
  6406. 			int summons[5] = { 1589, 1579, 1575, 1555, 1590 };
  6407. 			//int summons[5] = { 1020, 1068, 1118, 1500, 1368 };
  6408. 			int class_ = skillid==AM_SPHEREMINE?1142:summons[skilllv-1];
  6409. 			struct mob_data *md;
  6410.  
  6411. 			// Correct info, don't change any of this! [celest]
  6412. 			md = mob_once_spawn_sub(src, src->m, x, y, status_get_name(src),class_,"");
  6413. 			if (md) {
  6414. 				md->master_id = src->id;
  6415. 				md->special_state.ai = skillid==AM_SPHEREMINE?2:3;
  6416. 				if( md->deletetimer != INVALID_TIMER )
  6417. 					delete_timer(md->deletetimer, mob_timer_delete);
  6418. 				md->deletetimer = add_timer (gettick() + skill_get_time(skillid,skilllv), mob_timer_delete, md->bl.id, 0);
  6419. 				mob_spawn (md); //Now it is ready for spawning.
  6420. 			}
  6421. 		}
  6422. 		break;
  6423.  
  6424. 	// Slim Pitcher [Celest]
  6425. 	case CR_SLIMPITCHER:
  6426. 		if (sd) {
  6427. 			int i = skilllv%11 - 1;
  6428. 			int j = pc_search_inventory(sd,skill_db[skillid].itemid[i]);
  6429. 			if( j < 0 || skill_db[skillid].itemid[i] <= 0 || sd->inventory_data[j] == NULL || sd->status.inventory[j].amount < skill_db[skillid].amount[i] )
  6430. 			{
  6431. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6432. 				return 1;
  6433. 			}
  6434. 			potion_flag = 1;
  6435. 			potion_hp = 0;
  6436. 			potion_sp = 0;
  6437. 			run_script(sd->inventory_data[j]->script,0,sd->bl.id,0);
  6438. 			potion_flag = 0;
  6439. 			//Apply skill bonuses
  6440. 			i = pc_checkskill(sd,CR_SLIMPITCHER)*10
  6441. 				+ pc_checkskill(sd,AM_POTIONPITCHER)*10
  6442. 				+ pc_checkskill(sd,AM_LEARNINGPOTION)*5
  6443. 				+ pc_skillheal_bonus(sd, skillid);
  6444.  
  6445. 			potion_hp = potion_hp * (100+i)/100;
  6446. 			potion_sp = potion_sp * (100+i)/100;
  6447.  
  6448. 			if(potion_hp > 0 || potion_sp > 0) {
  6449. 				i = skill_get_splash(skillid, skilllv);
  6450. 				map_foreachinarea(skill_area_sub,
  6451. 					src->m,x-i,y-i,x+i,y+i,BL_CHAR,
  6452. 					src,skillid,skilllv,tick,flag|BCT_PARTY|BCT_GUILD|1,
  6453. 					skill_castend_nodamage_id);
  6454. 			}
  6455. 		} else {
  6456. 			int i = skilllv%11 - 1;
  6457. 			struct item_data *item;
  6458. 			i = skill_db[skillid].itemid[i];
  6459. 			item = itemdb_search(i);
  6460. 			potion_flag = 1;
  6461. 			potion_hp = 0;
  6462. 			potion_sp = 0;
  6463. 			run_script(item->script,0,src->id,0);
  6464. 			potion_flag = 0;
  6465. 			i = skill_get_max(CR_SLIMPITCHER)*10;
  6466.  
  6467. 			potion_hp = potion_hp * (100+i)/100;
  6468. 			potion_sp = potion_sp * (100+i)/100;
  6469.  
  6470. 			if(potion_hp > 0 || potion_sp > 0) {
  6471. 				i = skill_get_splash(skillid, skilllv);
  6472. 				map_foreachinarea(skill_area_sub,
  6473. 					src->m,x-i,y-i,x+i,y+i,BL_CHAR,
  6474. 					src,skillid,skilllv,tick,flag|BCT_PARTY|BCT_GUILD|1,
  6475. 						skill_castend_nodamage_id);
  6476. 			}
  6477. 		}
  6478. 		break;
  6479.  
  6480. 	case HW_GANBANTEIN:
  6481. 		if (rand()%100 < 80) {
  6482. 			int dummy = 1;
  6483. 			clif_skill_poseffect(src,skillid,skilllv,x,y,tick);
  6484. 			i = skill_get_splash(skillid, skilllv);
  6485. 			map_foreachinarea(skill_cell_overlap, src->m, x-i, y-i, x+i, y+i, BL_SKILL, HW_GANBANTEIN, &dummy, src);
  6486. 		} else {
  6487. 			if (sd) clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6488. 			return 1;
  6489. 		}
  6490. 		break;
  6491.  
  6492. 	case HW_GRAVITATION:
  6493. 		if ((sg = skill_unitsetting(src,skillid,skilllv,x,y,0)))
  6494. 			sc_start4(src,type,100,skilllv,0,BCT_SELF,sg->group_id,skill_get_time(skillid,skilllv));
  6495. 		flag|=1;
  6496. 		break;
  6497.  
  6498. 	// Plant Cultivation [Celest]
  6499. 	case CR_CULTIVATION:
  6500. 		if (sd) {
  6501. 			int i;
  6502. 			if( map_count_oncell(src->m,x,y,BL_CHAR) > 0 )
  6503. 			{
  6504. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6505. 				return 1;
  6506. 			}
  6507. 			clif_skill_poseffect(src,skillid,skilllv,x,y,tick);
  6508. 			if (rand()%100 < 50) {
  6509. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6510. 			} else {
  6511. 				TBL_MOB* md = mob_once_spawn_sub(src, src->m, x, y, "--ja--",(skilllv < 2 ? 1084+rand()%2 : 1078+rand()%6),"");
  6512. 				if (!md) break;
  6513. 				if ((i = skill_get_time(skillid, skilllv)) > 0)
  6514. 				{
  6515. 					if( md->deletetimer != INVALID_TIMER )
  6516. 						delete_timer(md->deletetimer, mob_timer_delete);
  6517. 					md->deletetimer = add_timer (tick + i, mob_timer_delete, md->bl.id, 0);
  6518. 				}
  6519. 				mob_spawn (md);
  6520. 			}
  6521. 		}
  6522. 		break;
  6523.  
  6524. 	case SG_SUN_WARM:
  6525. 	case SG_MOON_WARM:
  6526. 	case SG_STAR_WARM:
  6527. 		skill_clear_unitgroup(src);
  6528. 		if ((sg = skill_unitsetting(src,skillid,skilllv,src->x,src->y,0)))
  6529. 			sc_start4(src,type,100,skilllv,0,0,sg->group_id,skill_get_time(skillid,skilllv));
  6530. 		flag|=1;
  6531. 		break;
  6532.  
  6533. 	case PA_GOSPEL:
  6534. 		if (sce && sce->val4 == BCT_SELF)
  6535. 		{
  6536. 			status_change_end(src, SC_GOSPEL, INVALID_TIMER);
  6537. 			return 0;
  6538. 		}
  6539. 		else
  6540. 	  	{
  6541. 			sg = skill_unitsetting(src,skillid,skilllv,src->x,src->y,0);
  6542. 			if (!sg) break;
  6543. 			if (sce)
  6544. 				status_change_end(src, type, INVALID_TIMER); //Was under someone else's Gospel. [Skotlex]
  6545. 			sc_start4(src,type,100,skilllv,0,sg->group_id,BCT_SELF,skill_get_time(skillid,skilllv));
  6546. 			clif_skill_poseffect(src, skillid, skilllv, 0, 0, tick); // PA_GOSPEL music packet
  6547. 		}
  6548. 		break;
  6549. 	case NJ_TATAMIGAESHI:
  6550. 		if (skill_unitsetting(src,skillid,skilllv,src->x,src->y,0))
  6551. 			sc_start(src,type,100,skilllv,skill_get_time2(skillid,skilllv));
  6552. 		break;
  6553.  
  6554. 	case AM_RESURRECTHOMUN:	//[orn]
  6555. 		if (sd)
  6556. 		{
  6557. 			if (!merc_resurrect_homunculus(sd, 20*skilllv, x, y))
  6558. 			{
  6559. 				clif_skill_fail(sd,skillid,USESKILL_FAIL_LEVEL,0);
  6560. 				break;
  6561. 			}
  6562. 		}
  6563. 		break;
  6564.  
  6565. 	default:
  6566. 		ShowWarning("skill_castend_pos2: Unknown skill used:%d\n",skillid);
  6567. 		return 1;
  6568. 	}
  6569.  
  6570. 	status_change_end(src, SC_MAGICPOWER, INVALID_TIMER);
  6571.  
  6572. 	if( sd )
  6573. 	{
  6574. 		if( sd->state.arrow_atk && !(flag&1) ) //Consume arrow if a ground skill was not invoked. [Skotlex]
  6575. 			battle_consume_ammo(sd, skillid, skilllv);
  6576. 		skill_onskillusage(sd, NULL, skillid, tick);
  6577. 		skill_consume_requirement(sd,skillid,skilllv,2);
  6578. 	}
  6579.  
  6580. 	return 0;
  6581. }
  6582.  
  6583. /*==========================================
  6584.  *
  6585.  *------------------------------------------*/
  6586. int skill_castend_map (struct map_session_data *sd, short skill_num, const char *map)
  6587. {
  6588. 	nullpo_ret(sd);
  6589.  
  6590. //Simplify skill_failed code.
  6591. #define skill_failed(sd) { sd->menuskill_id = sd->menuskill_val = 0; }
  6592. 	if(skill_num != sd->menuskill_id)
  6593. 		return 0;
  6594.  
  6595. 	if( sd->bl.prev == NULL || pc_isdead(sd) ) {
  6596. 		skill_failed(sd);
  6597. 		return 0;
  6598. 	}
  6599.  
  6600. 	if(sd->sc.opt1 || sd->sc.option&OPTION_HIDE ) {
  6601. 		skill_failed(sd);
  6602. 		return 0;
  6603. 	}
  6604. 	if(sd->sc.count && (
  6605. 		sd->sc.data[SC_SILENCE] ||
  6606. 		sd->sc.data[SC_ROKISWEIL] ||
  6607. 		sd->sc.data[SC_AUTOCOUNTER] ||
  6608. 		sd->sc.data[SC_STEELBODY] ||
  6609. 		sd->sc.data[SC_DANCING] ||
  6610. 		sd->sc.data[SC_BERSERK] ||
  6611. 		sd->sc.data[SC_BASILICA] ||
  6612. 		sd->sc.data[SC_MARIONETTE]
  6613. 	 )) {
  6614. 		skill_failed(sd);
  6615. 		return 0;
  6616. 	}
  6617.  
  6618. 	pc_stop_attack(sd);
  6619. 	pc_stop_walking(sd,0);
  6620.  
  6621. 	if(battle_config.skill_log && battle_config.skill_log&BL_PC)
  6622. 		ShowInfo("PC %d skill castend skill =%d map=%s\n",sd->bl.id,skill_num,map);
  6623.  
  6624. 	if(strcmp(map,"cancel")==0) {
  6625. 		skill_failed(sd);
  6626. 		return 0;
  6627. 	}
  6628.  
  6629. 	switch(skill_num)
  6630. 	{
  6631. 	case AL_TELEPORT:
  6632. 		if(strcmp(map,"Random")==0)
  6633. 			pc_randomwarp(sd,CLR_TELEPORT);
  6634. 		else if (sd->menuskill_val > 1) //Need lv2 to be able to warp here.
  6635. 			pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  6636. 		break;
  6637.  
  6638. 	case AL_WARP:
  6639. 		{
  6640. 			const struct point *p[4];
  6641. 			struct skill_unit_group *group;
  6642. 			int i, lv, wx, wy;
  6643. 			int maxcount=0;
  6644. 			int x,y;
  6645. 			unsigned short mapindex;
  6646.  
  6647. 			mapindex  = mapindex_name2id((char*)map);
  6648. 			if(!mapindex) { //Given map not found?
  6649. 				clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  6650. 				skill_failed(sd);
  6651. 				return 0;
  6652. 			}
  6653. 			p[0] = &sd->status.save_point;
  6654. 			p[1] = &sd->status.memo_point[0];
  6655. 			p[2] = &sd->status.memo_point[1];
  6656. 			p[3] = &sd->status.memo_point[2];
  6657.  
  6658. 			if((maxcount = skill_get_maxcount(skill_num, sd->menuskill_val)) > 0) {
  6659. 				for(i=0;i<MAX_SKILLUNITGROUP && sd->ud.skillunit[i] && maxcount;i++) {
  6660. 					if(sd->ud.skillunit[i]->skill_id == skill_num)
  6661. 						maxcount--;
  6662. 				}
  6663. 				if(!maxcount) {
  6664. 					clif_skill_fail(sd,skill_num,USESKILL_FAIL_LEVEL,0);
  6665. 					skill_failed(sd);
  6666. 					return 0;
  6667. 				}
  6668. 			}
  6669.  
  6670. 			lv = sd->skillitem==skill_num?sd->skillitemlv:pc_checkskill(sd,skill_num);
  6671. 			wx = sd->menuskill_val>>16;
  6672. 			wy = sd->menuskill_val&0xffff;
  6673.  
  6674. 			if( lv <= 0 ) return 0;
  6675. 			if( lv > 4 ) lv = 4; // crash prevention
  6676.  
  6677. 			// check if the chosen map exists in the memo list
  6678. 			ARR_FIND( 0, lv, i, mapindex == p[i]->map );
  6679. 			if( i < lv ) {
  6680. 				x=p[i]->x;
  6681. 				y=p[i]->y;
  6682. 			} else {
  6683. 				skill_failed(sd);
  6684. 				return 0;
  6685. 			}
  6686.  
  6687. 			if(!skill_check_condition_castend(sd, sd->menuskill_id, lv))
  6688. 			{  // This checks versus skillid/skilllv...
  6689. 				skill_failed(sd);
  6690. 				return 0;
  6691. 			}
  6692.  
  6693. 			skill_consume_requirement(sd,sd->menuskill_id,lv,2);
  6694. 			sd->skillitem = sd->skillitemlv = 0; // Clear data that's skipped in 'skill_castend_pos' [Inkfish]
  6695.  
  6696. 			if((group=skill_unitsetting(&sd->bl,skill_num,lv,wx,wy,0))==NULL) {
  6697. 				skill_failed(sd);
  6698. 				return 0;
  6699. 			}
  6700.  
  6701. 			// record the destination coordinates
  6702. 			group->val2 = (x<<16)|y;
  6703. 			group->val3 = mapindex;
  6704. 		}
  6705. 		break;
  6706. 	}
  6707.  
  6708. 	sd->menuskill_id = sd->menuskill_val = 0;
  6709. 	return 0;
  6710. #undef skill_failed
  6711. }
  6712.  
  6713. /// transforms 'target' skill unit into dissonance (if conditions are met)
  6714. static int skill_dance_overlap_sub(struct block_list* bl, va_list ap)
  6715. {
  6716. 	struct skill_unit* target = (struct skill_unit*)bl;
  6717. 	struct skill_unit* src = va_arg(ap, struct skill_unit*);
  6718. 	int flag = va_arg(ap, int);
  6719.  
  6720. 	if (src == target)
  6721. 		return 0;
  6722. 	if (!target->group || !(target->group->state.song_dance&0x1))
  6723. 		return 0;
  6724. 	if (!(target->val2 & src->val2 & ~UF_ENSEMBLE)) //They don't match (song + dance) is valid.
  6725. 		return 0;
  6726.  
  6727. 	if (flag) //Set dissonance
  6728. 		target->val2 |= UF_ENSEMBLE; //Add ensemble to signal this unit is overlapping.
  6729. 	else //Remove dissonance
  6730. 		target->val2 &= ~UF_ENSEMBLE;
  6731.  
  6732. 	clif_skill_setunit(target); //Update look of affected cell.
  6733.  
  6734. 	return 1;
  6735. }
  6736.  
  6737. //Does the song/dance overlapping -> dissonance check. [Skotlex]
  6738. //When flag is 0, this unit is about to be removed, cancel the dissonance effect
  6739. //When 1, this unit has been positioned, so start the cancel effect.
  6740. int skill_dance_overlap(struct skill_unit* unit, int flag)
  6741. {
  6742. 	if (!unit || !unit->group || !(unit->group->state.song_dance&0x1))
  6743. 		return 0;
  6744. 	if (!flag && !(unit->val2&UF_ENSEMBLE))
  6745. 		return 0; //Nothing to remove, this unit is not overlapped.
  6746.  
  6747. 	if (unit->val1 != unit->group->skill_id)
  6748. 	{	//Reset state
  6749. 		unit->val1 = unit->group->skill_id;
  6750. 		unit->val2 &= ~UF_ENSEMBLE;
  6751. 	}
  6752.  
  6753. 	return map_foreachincell(skill_dance_overlap_sub, unit->bl.m,unit->bl.x,unit->bl.y,BL_SKILL, unit,flag);
  6754. }
  6755.  
  6756. /*==========================================
  6757.  * Converts this group information so that it is handled as a Dissonance or Ugly Dance cell.
  6758.  * Flag: 0 - Convert, 1 - Revert.
  6759.  *------------------------------------------*/
  6760. static bool skill_dance_switch(struct skill_unit* unit, int flag)
  6761. {
  6762. 	static int prevflag = 1;  // by default the backup is empty
  6763. 	static struct skill_unit_group backup;
  6764. 	struct skill_unit_group* group = unit->group;
  6765.  
  6766. 	// val2&UF_ENSEMBLE is a hack to indicate dissonance
  6767. 	if ( !(group->state.song_dance&0x1 && unit->val2&UF_ENSEMBLE) )
  6768. 		return false;
  6769.  
  6770. 	if( flag == prevflag )
  6771. 	{// protection against attempts to read an empty backup / write to a full backup
  6772. 		ShowError("skill_dance_switch: Attempted to %s (skill_id=%d, skill_lv=%d, src_id=%d).\n",
  6773. 			flag ? "read an empty backup" : "write to a full backup",
  6774. 			group->skill_id, group->skill_lv, group->src_id);
  6775. 		return false;
  6776. 	}
  6777. 	prevflag = flag;
  6778.  
  6779. 	if( !flag )
  6780. 	{	//Transform
  6781. 		int skillid = unit->val2&UF_SONG ? BA_DISSONANCE : DC_UGLYDANCE;
  6782.  
  6783. 		// backup
  6784. 		backup.skill_id    = group->skill_id;
  6785. 		backup.skill_lv    = group->skill_lv;
  6786. 		backup.unit_id     = group->unit_id;
  6787. 		backup.target_flag = group->target_flag;
  6788. 		backup.bl_flag     = group->bl_flag;
  6789. 		backup.interval    = group->interval;
  6790.  
  6791. 		// replace
  6792. 		group->skill_id    = skillid;
  6793. 		group->skill_lv    = 1;
  6794. 		group->unit_id     = skill_get_unit_id(skillid,0);
  6795. 		group->target_flag = skill_get_unit_target(skillid);
  6796. 		group->bl_flag     = skill_get_unit_bl_target(skillid);
  6797. 		group->interval    = skill_get_unit_interval(skillid);
  6798. 	}
  6799. 	else
  6800. 	{	//Restore
  6801. 		group->skill_id    = backup.skill_id;
  6802. 		group->skill_lv    = backup.skill_lv;
  6803. 		group->unit_id     = backup.unit_id;
  6804. 		group->target_flag = backup.target_flag;
  6805. 		group->bl_flag     = backup.bl_flag;
  6806. 		group->interval    = backup.interval;
  6807. 	}
  6808.  
  6809. 	return true;
  6810. }
  6811.  
  6812. /*==========================================
  6813.  * Initializes and sets a ground skill.
  6814.  * flag&1 is used to determine when the skill 'morphs' (Warp portal becomes active, or Fire Pillar becomes active)
  6815.  * flag&2 is used to determine if this skill was casted with Magic Power active.
  6816.  *------------------------------------------*/
  6817. struct skill_unit_group* skill_unitsetting (struct block_list *src, short skillid, short skilllv, short x, short y, int flag)
  6818. {
  6819. 	struct skill_unit_group *group;
  6820. 	int i,limit,val1=0,val2=0,val3=0;
  6821. 	int target,interval,range,unit_flag;
  6822. 	struct s_skill_unit_layout *layout;
  6823. 	struct map_session_data *sd;
  6824. 	struct status_data *status;
  6825. 	struct status_change *sc;
  6826. 	int active_flag=1;
  6827. 	int subunt=0;
  6828.  
  6829. 	nullpo_retr(NULL, src);
  6830.  
  6831. 	limit = skill_get_time(skillid,skilllv);
  6832. 	range = skill_get_unit_range(skillid,skilllv);
  6833. 	interval = skill_get_unit_interval(skillid);
  6834. 	target = skill_get_unit_target(skillid);
  6835. 	unit_flag = skill_get_unit_flag(skillid);
  6836. 	layout = skill_get_unit_layout(skillid,skilllv,src,x,y);
  6837.  
  6838. 	sd = BL_CAST(BL_PC, src);
  6839. 	status = status_get_status_data(src);
  6840. 	sc = status_get_sc(src);	// for traps, firewall and fogwall - celest
  6841.  
  6842. 	switch( skillid )
  6843. 	{
  6844. 	case MG_SAFETYWALL:
  6845. 		val2=skilllv+1;
  6846. 		break;
  6847. 	case MG_FIREWALL:
  6848. 		if(sc && sc->data[SC_VIOLENTGALE])
  6849. 			limit = limit*3/2;
  6850. 		val2=4+skilllv;
  6851. 		break;
  6852.  
  6853. 	case AL_WARP:
  6854. 		val1=skilllv+6;
  6855. 		if(!(flag&1))
  6856. 			limit=2000;
  6857. 		else // previous implementation (not used anymore)
  6858. 		{	//Warp Portal morphing to active mode, extract relevant data from src. [Skotlex]
  6859. 			if( src->type != BL_SKILL ) return NULL;
  6860. 			group = ((TBL_SKILL*)src)->group;
  6861. 			src = map_id2bl(group->src_id);
  6862. 			if( !src ) return NULL;
  6863. 			val2 = group->val2; //Copy the (x,y) position you warp to
  6864. 			val3 = group->val3; //as well as the mapindex to warp to.
  6865. 		}
  6866. 		break;
  6867. 	case HP_BASILICA:
  6868. 		val1 = src->id; // Store caster id.
  6869. 		break;
  6870.  
  6871. 	case PR_SANCTUARY:
  6872. 	case NPC_EVILLAND:
  6873. 		val1=(skilllv+3)*2;
  6874. 		break;
  6875.  
  6876. 	case WZ_FIREPILLAR:
  6877. 		if((flag&1)!=0)
  6878. 			limit=1000;
  6879. 		val1=skilllv+2;
  6880. 		break;
  6881. 	case WZ_QUAGMIRE:	//The target changes to "all" if used in a gvg map. [Skotlex]
  6882. 	case AM_DEMONSTRATION:
  6883. 		if (map_flag_vs(src->m) && battle_config.vs_traps_bctall
  6884. 			&& (src->type&battle_config.vs_traps_bctall))
  6885. 			target = BCT_ALL;
  6886. 		break;
  6887. 	case HT_SHOCKWAVE:
  6888. 		val1=skilllv*15+10;
  6889. 	case HT_SANDMAN:
  6890. 	case MA_SANDMAN:
  6891. 	case HT_CLAYMORETRAP:
  6892. 	case HT_SKIDTRAP:
  6893. 	case MA_SKIDTRAP:
  6894. 	case HT_LANDMINE:
  6895. 	case MA_LANDMINE:
  6896. 	case HT_ANKLESNARE:
  6897. 	case HT_FLASHER:
  6898. 	case HT_FREEZINGTRAP:
  6899. 	case MA_FREEZINGTRAP:
  6900. 	case HT_BLASTMINE:
  6901. 		if( map_flag_gvg(src->m) || map[src->m].flag.battleground )
  6902. 			limit *= 4; // longer trap times in WOE [celest]
  6903. 		if( battle_config.vs_traps_bctall && map_flag_vs(src->m) && (src->type&battle_config.vs_traps_bctall) )
  6904. 			target = BCT_ALL;
  6905. 		break;
  6906.  
  6907. 	case SA_LANDPROTECTOR:
  6908. 	case SA_VOLCANO:
  6909. 	case SA_DELUGE:
  6910. 	case SA_VIOLENTGALE:
  6911. 	{
  6912. 		struct skill_unit_group *old_sg;
  6913. 		if ((old_sg = skill_locate_element_field(src)) != NULL)
  6914. 		{	//HelloKitty confirmed that these are interchangeable,
  6915. 			//so you can change element and not consume gemstones.
  6916. 			if ((
  6917. 				old_sg->skill_id == SA_VOLCANO ||
  6918. 				old_sg->skill_id == SA_DELUGE ||
  6919. 				old_sg->skill_id == SA_VIOLENTGALE
  6920. 			) && old_sg->limit > 0)
  6921. 			{	//Use the previous limit (minus the elapsed time) [Skotlex]
  6922. 				limit = old_sg->limit - DIFF_TICK(gettick(), old_sg->tick);
  6923. 				if (limit < 0)	//This can happen...
  6924. 					limit = skill_get_time(skillid,skilllv);
  6925. 			}
  6926. 			skill_clear_group(src,1);
  6927. 		}
  6928. 		break;
  6929. 	}
  6930.  
  6931. 	case BA_DISSONANCE:
  6932. 	case DC_UGLYDANCE:
  6933. 		val1 = 10;	//FIXME: This value is not used anywhere, what is it for? [Skotlex]
  6934. 		break;
  6935. 	case BA_WHISTLE:
  6936. 		val1 = skilllv +status->agi/10; // Flee increase
  6937. 		val2 = ((skilllv+1)/2)+status->luk/10; // Perfect dodge increase
  6938. 		if(sd){
  6939. 			val1 += pc_checkskill(sd,BA_MUSICALLESSON);
  6940. 			val2 += pc_checkskill(sd,BA_MUSICALLESSON);
  6941. 		}
  6942. 		break;
  6943. 	case DC_HUMMING:
  6944.         val1 = 2*skilllv+status->dex/10; // Hit increase
  6945. 		if(sd)
  6946. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON);
  6947. 		break;
  6948. 	case BA_POEMBRAGI:
  6949. 		val1 = 3*skilllv+status->dex/10; // Casting time reduction
  6950. 		//For some reason at level 10 the base delay reduction is 50%.
  6951. 		val2 = (skilllv<10?3*skilllv:50)+status->int_/5; // After-cast delay reduction
  6952. 		if(sd){
  6953. 			val1 += 2*pc_checkskill(sd,BA_MUSICALLESSON);
  6954. 			val2 += 2*pc_checkskill(sd,BA_MUSICALLESSON);
  6955. 		}
  6956. 		break;
  6957. 	case DC_DONTFORGETME:
  6958. 		val1 = status->dex/10 + 3*skilllv + 5; // ASPD decrease
  6959. 		val2 = status->agi/10 + 3*skilllv + 5; // Movement speed adjustment.
  6960. 		if(sd){
  6961. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON);
  6962. 			val2 += pc_checkskill(sd,DC_DANCINGLESSON);
  6963. 		}
  6964. 		break;
  6965. 	case BA_APPLEIDUN:
  6966. 		val1 = 5+2*skilllv+status->vit/10; // MaxHP percent increase
  6967. 		if(sd)
  6968. 			val1 += pc_checkskill(sd,BA_MUSICALLESSON);
  6969. 		break;
  6970. 	case DC_SERVICEFORYOU:
  6971. 		val1 = 15+skilllv+(status->int_/10); // MaxSP percent increase TO-DO: this INT bonus value is guessed
  6972. 		val2 = 20+3*skilllv+(status->int_/10); // SP cost reduction
  6973. 		if(sd){
  6974. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON); //TO-DO This bonus value is guessed
  6975. 			val2 += pc_checkskill(sd,DC_DANCINGLESSON); //TO-DO Should be half this value
  6976. 		}
  6977. 		break;
  6978. 	case BA_ASSASSINCROSS:
  6979. 		val1 = 100+10*skilllv+status->agi; // ASPD increase
  6980. 		if(sd)
  6981. 			val1 += 5*pc_checkskill(sd,BA_MUSICALLESSON);
  6982. 		break;
  6983. 	case DC_FORTUNEKISS:
  6984. 		val1 = 10+skilllv+(status->luk/10); // Critical increase
  6985. 		if(sd)
  6986. 			val1 += pc_checkskill(sd,DC_DANCINGLESSON);
  6987. 		val1*=10; //Because every 10 crit is an actual cri point.
  6988. 		break;
  6989. 	case BD_DRUMBATTLEFIELD:
  6990. 		val1 = (skilllv+1)*25;	//Watk increase
  6991. 		val2 = (skilllv+1)*2;	//Def increase
  6992. 		break;
  6993. 	case BD_RINGNIBELUNGEN:
  6994. 		val1 = (skilllv+2)*25;	//Watk increase
  6995. 		break;
  6996. 	case BD_RICHMANKIM:
  6997. 		val1 = 25 + 11*skilllv; //Exp increase bonus.
  6998. 		break;
  6999. 	case BD_SIEGFRIED:
  7000. 		val1 = 55 + skilllv*5;	//Elemental Resistance
  7001. 		val2 = skilllv*10;	//Status ailment resistance
  7002. 		break;
  7003. 	case WE_CALLPARTNER:
  7004. 		if (sd) val1 = sd->status.partner_id;
  7005. 		break;
  7006. 	case WE_CALLPARENT:
  7007. 		if (sd) {
  7008. 			val1 = sd->status.father;
  7009. 		 	val2 = sd->status.mother;
  7010. 		}
  7011. 		break;
  7012. 	case WE_CALLBABY:
  7013. 		if (sd) val1 = sd->status.child;
  7014. 		break;
  7015. 	case NJ_KAENSIN:
  7016. 		skill_clear_group(src, 1); //Delete previous Kaensins/Suitons
  7017. 		val2 = (skilllv+1)/2 + 4;
  7018. 		break;
  7019. 	case NJ_SUITON:
  7020. 		skill_clear_group(src, 1);
  7021. 		break;
  7022.  
  7023. 	case GS_GROUNDDRIFT:
  7024. 		{
  7025. 		int element[5]={ELE_WIND,ELE_DARK,ELE_POISON,ELE_WATER,ELE_FIRE};
  7026.  
  7027. 		val1 = status->rhw.ele;
  7028. 		if (!val1)
  7029. 			val1=element[rand()%5];
  7030.  
  7031. 		switch (val1)
  7032. 		{
  7033. 			case ELE_FIRE:
  7034. 				subunt++;
  7035. 			case ELE_WATER:
  7036. 				subunt++;
  7037. 			case ELE_POISON:
  7038. 				subunt++;
  7039. 			case ELE_DARK:
  7040. 				subunt++;
  7041. 			case ELE_WIND:
  7042. 				break;
  7043. 			default:
  7044. 				subunt=rand()%5;
  7045. 				break;
  7046. 		}
  7047.  
  7048. 		break;
  7049. 		}
  7050. 	}
  7051.  
  7052. 	nullpo_retr(NULL, group=skill_initunitgroup(src,layout->count,skillid,skilllv,skill_get_unit_id(skillid,flag&1)+subunt, limit, interval));
  7053. 	group->val1=val1;
  7054. 	group->val2=val2;
  7055. 	group->val3=val3;
  7056. 	group->target_flag=target;
  7057. 	group->bl_flag= skill_get_unit_bl_target(skillid);
  7058. 	group->state.magic_power = (flag&2 || (sc && sc->data[SC_MAGICPOWER])); //Store the magic power flag. [Skotlex]
  7059. 	group->state.ammo_consume = (sd && sd->state.arrow_atk && skillid != GS_GROUNDDRIFT); //Store if this skill needs to consume ammo.
  7060. 	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
  7061.  
  7062.   	//if tick is greater than current, do not invoke onplace function just yet. [Skotlex]
  7063. 	if (DIFF_TICK(group->tick, gettick()) > SKILLUNITTIMER_INTERVAL)
  7064. 		active_flag = 0;
  7065.  
  7066. 	if(skillid==HT_TALKIEBOX || skillid==RG_GRAFFITI){
  7067. 		group->valstr=(char *) aMalloc(MESSAGE_SIZE*sizeof(char));
  7068. 		if (sd)
  7069. 			safestrncpy(group->valstr, sd->message, MESSAGE_SIZE);
  7070. 		else //Eh... we have to write something here... even though mobs shouldn't use this. [Skotlex]
  7071. 			safestrncpy(group->valstr, "Boo!", MESSAGE_SIZE);
  7072. 	}
  7073.  
  7074. 	if (group->state.song_dance) {
  7075. 		if(sd){
  7076. 			sd->skillid_dance = skillid;
  7077. 			sd->skilllv_dance = skilllv;
  7078. 		}
  7079. 		if (
  7080. 			sc_start4(src, SC_DANCING, 100, skillid, group->group_id, skilllv,
  7081. 				(group->state.song_dance&2?BCT_SELF:0), limit+1000) &&
  7082. 			sd && group->state.song_dance&2 && skillid != CG_HERMODE //Hermod is a encore with a warp!
  7083. 		)
  7084. 			skill_check_pc_partner(sd, skillid, &skilllv, 1, 1);
  7085. 	}
  7086.  
  7087. 	limit = group->limit;
  7088. 	for( i = 0; i < layout->count; i++ )
  7089. 	{
  7090. 		struct skill_unit *unit;
  7091. 		int ux = x + layout->dx[i];
  7092. 		int uy = y + layout->dy[i];
  7093. 		int val1 = skilllv;
  7094. 		int val2 = 0;
  7095. 		int alive = 1;
  7096.  
  7097. 		if( !group->state.song_dance && !map_getcell(src->m,ux,uy,CELL_CHKREACH) )
  7098. 			continue; // don't place skill units on walls (except for songs/dances/encores)
  7099. 		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) )
  7100. 			continue; // no path between cell and center of casting.
  7101.  
  7102. 		switch( skillid )
  7103. 		{
  7104. 		case MG_FIREWALL:
  7105. 		case NJ_KAENSIN:
  7106. 			val2=group->val2;
  7107. 			break;
  7108. 		case WZ_ICEWALL:
  7109. 			val1 = (skilllv <= 1) ? 500 : 200 + 200*skilllv;
  7110. 			val2 = map_getcell(src->m, ux, uy, CELL_GETTYPE);
  7111. 			break;
  7112. 		case HT_LANDMINE:
  7113. 		case MA_LANDMINE:
  7114. 		case HT_ANKLESNARE:
  7115. 		case HT_SHOCKWAVE:
  7116. 		case HT_SANDMAN:
  7117. 		case MA_SANDMAN:
  7118. 		case HT_FLASHER:
  7119. 		case HT_FREEZINGTRAP:
  7120. 		case MA_FREEZINGTRAP:
  7121. 		case HT_TALKIEBOX:
  7122. 		case HT_SKIDTRAP:
  7123. 		case MA_SKIDTRAP:
  7124. 			val1 = 3500;
  7125. 			break;
  7126. 		case GS_DESPERADO:
  7127. 			val1 = abs(layout->dx[i]);
  7128. 			val2 = abs(layout->dy[i]);
  7129. 			if (val1 < 2 || val2 < 2) { //Nearby cross, linear decrease with no diagonals
  7130. 				if (val2 > val1) val1 = val2;
  7131. 				if (val1) val1--;
  7132. 				val1 = 36 -12*val1;
  7133. 			} else //Diagonal edges
  7134. 				val1 = 28 -4*val1 -4*val2;
  7135. 			if (val1 < 1) val1 = 1;
  7136. 			val2 = 0;
  7137. 			break;
  7138. 		default:
  7139. 			if (group->state.song_dance&0x1)
  7140. 				val2 = unit_flag&(UF_DANCE|UF_SONG); //Store whether this is a song/dance
  7141. 			break;
  7142. 		}
  7143.  
  7144. 		if( range <= 0 )
  7145. 			map_foreachincell(skill_cell_overlap,src->m,ux,uy,BL_SKILL,skillid,&alive, src);
  7146. 		if( !alive )
  7147. 			continue;
  7148.  
  7149. 		nullpo_retr(NULL, unit=skill_initunit(group,i,ux,uy,val1,val2));
  7150. 		unit->limit=limit;
  7151. 		unit->range=range;
  7152.  
  7153. 		if (skillid == PF_FOGWALL && alive == 2)
  7154. 		{	//Double duration of cells on top of Deluge/Suiton
  7155. 			unit->limit *= 2;
  7156. 			group->limit = unit->limit;
  7157. 		}
  7158.  
  7159. 		// execute on all targets standing on this cell
  7160. 		if (range==0 && active_flag)
  7161. 			map_foreachincell(skill_unit_effect,unit->bl.m,unit->bl.x,unit->bl.y,group->bl_flag,&unit->bl,gettick(),1);
  7162. 	}
  7163.  
  7164. 	if (!group->alive_count)
  7165. 	{	//No cells? Something that was blocked completely by Land Protector?
  7166. 		skill_delunitgroup(group);
  7167. 		return NULL;
  7168. 	}
  7169.  
  7170.  
  7171. 	if (skillid == NJ_TATAMIGAESHI) //Store number of tiles.
  7172. 		group->val1 = group->alive_count;
  7173.  
  7174. 	return group;
  7175. }
  7176.  
  7177. /*==========================================
  7178.  *
  7179.  *------------------------------------------*/
  7180. static int skill_unit_onplace (struct skill_unit *src, struct block_list *bl, unsigned int tick)
  7181. {
  7182. 	struct skill_unit_group *sg;
  7183. 	struct block_list *ss;
  7184. 	struct status_change *sc;
  7185. 	struct status_change_entry *sce;
  7186. 	enum sc_type type;
  7187. 	int skillid;
  7188.  
  7189. 	nullpo_ret(src);
  7190. 	nullpo_ret(bl);
  7191.  
  7192. 	if(bl->prev==NULL || !src->alive || status_isdead(bl))
  7193. 		return 0;
  7194.  
  7195. 	nullpo_ret(sg=src->group);
  7196. 	nullpo_ret(ss=map_id2bl(sg->src_id));
  7197.  
  7198. 	if( skill_get_type(sg->skill_id) == BF_MAGIC && map_getcell(bl->m, bl->x, bl->y, CELL_CHKLANDPROTECTOR) && sg->skill_id != SA_LANDPROTECTOR )
  7199. 		return 0; //AoE skills are ineffective. [Skotlex]
  7200.  
  7201. 	sc = status_get_sc(bl);
  7202.  
  7203. 	if (sc && sc->option&OPTION_HIDE && sg->skill_id != WZ_HEAVENDRIVE)
  7204. 		return 0; //Hidden characters are immune to AoE skills except Heaven's Drive. [Skotlex]
  7205.  
  7206. 	type = status_skill2sc(sg->skill_id);
  7207. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  7208. 	skillid = sg->skill_id; //In case the group is deleted, we need to return the correct skill id, still.
  7209. 	switch (sg->unit_id)
  7210. 	{
  7211. 	case UNT_SPIDERWEB:
  7212. 		if( sc && sc->data[SC_SPIDERWEB] && sc->data[SC_SPIDERWEB]->val1 > 0 )
  7213. 		{ // If you are fiberlocked and can't move, it will only increase your fireweakness level. [Inkfish]
  7214. 			sc->data[SC_SPIDERWEB]->val2++;
  7215. 			break;
  7216. 		}
  7217. 		else if( sc )
  7218. 		{
  7219. 			int sec = skill_get_time2(sg->skill_id,sg->skill_lv);
  7220. 			if( status_change_start(bl,type,10000,sg->skill_lv,1,sg->group_id,0,sec,8) )
  7221. 			{
  7222. 				const struct TimerData* td = sc->data[type]?get_timer(sc->data[type]->timer):NULL; 
  7223. 				if( td )
  7224. 					sec = DIFF_TICK(td->tick, tick);
  7225. 				map_moveblock(bl, src->bl.x, src->bl.y, tick);
  7226. 				clif_fixpos(bl);
  7227. 				sg->val2 = bl->id;
  7228. 			}
  7229. 			else
  7230. 				sec = 3000; //Couldn't trap it?
  7231. 			sg->limit = DIFF_TICK(tick,sg->tick)+sec;
  7232. 		}
  7233. 		break;
  7234. 	case UNT_SAFETYWALL:
  7235. 		if (!sce)
  7236. 			sc_start4(bl,type,100,sg->skill_lv,sg->group_id,sg->group_id,0,sg->limit);
  7237. 		break;
  7238.  
  7239. 	case UNT_PNEUMA:
  7240. 		if (!sce)
  7241. 			sc_start4(bl,type,100,sg->skill_lv,sg->group_id,0,0,sg->limit);
  7242. 		break;
  7243.  
  7244. 	case UNT_WARP_WAITING:
  7245. 		if(bl->type==BL_PC){
  7246. 			struct map_session_data *sd = (struct map_session_data *)bl;
  7247. 			if((!sd->chatID || battle_config.chat_warpportal)
  7248. 				&& sd->ud.to_x == src->bl.x && sd->ud.to_y == src->bl.y)
  7249. 			{
  7250. 				int x = sg->val2>>16;
  7251. 				int y = sg->val2&0xffff;
  7252. 				unsigned short m = sg->val3;
  7253.  
  7254. 				if( --sg->val1 <= 0 )
  7255. 					skill_delunitgroup(sg);
  7256.  
  7257. 				pc_setpos(sd,m,x,y,CLR_TELEPORT);
  7258. 				sg = src->group; // avoid dangling pointer (pc_setpos can cause deletion of 'sg')
  7259. 			}
  7260. 		} else
  7261. 		if(bl->type == BL_MOB && battle_config.mob_warp&2)
  7262. 		{
  7263. 			int m = map_mapindex2mapid(sg->val3);
  7264. 			if (m < 0) break; //Map not available on this map-server.
  7265. 			unit_warp(bl,m,sg->val2>>16,sg->val2&0xffff,CLR_TELEPORT);
  7266. 		}
  7267. 		break;
  7268.  
  7269. 	case UNT_QUAGMIRE:
  7270. 		if(!sce)
  7271. 			sc_start4(bl,type,100,sg->skill_lv,sg->group_id,0,0,sg->limit);
  7272. 		break;
  7273.  
  7274. 	case UNT_VOLCANO:
  7275. 	case UNT_DELUGE:
  7276. 	case UNT_VIOLENTGALE:
  7277. 		if(!sce)
  7278. 			sc_start(bl,type,100,sg->skill_lv,sg->limit);
  7279. 		break;
  7280.  
  7281. 	case UNT_SUITON:
  7282. 		if(!sce)
  7283. 			sc_start4(bl,type,100,sg->skill_lv,
  7284. 			map_flag_vs(bl->m) || battle_check_target(&src->bl,bl,BCT_ENEMY)>0?1:0, //Send val3 =1 to reduce agi.
  7285. 			0,0,sg->limit);
  7286. 		break;
  7287.  
  7288. 	case UNT_HERMODE:
  7289. 		if (sg->src_id!=bl->id && battle_check_target(&src->bl,bl,BCT_PARTY|BCT_GUILD) > 0)
  7290. 			status_change_clear_buffs(bl,1); //Should dispell only allies.
  7291. 	case UNT_RICHMANKIM:
  7292. 	case UNT_ETERNALCHAOS:
  7293. 	case UNT_DRUMBATTLEFIELD:
  7294. 	case UNT_RINGNIBELUNGEN:
  7295. 	case UNT_ROKISWEIL:
  7296. 	case UNT_INTOABYSS:
  7297. 	case UNT_SIEGFRIED:
  7298. 		 //Needed to check when a dancer/bard leaves their ensemble area.
  7299. 		if (sg->src_id==bl->id && !(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_BARDDANCER))
  7300. 			return skillid;
  7301. 		if (!sce)
  7302. 			sc_start4(bl,type,100,sg->skill_lv,sg->val1,sg->val2,0,sg->limit);
  7303. 		break;
  7304. 	case UNT_WHISTLE:
  7305. 	case UNT_ASSASSINCROSS:
  7306. 	case UNT_POEMBRAGI:
  7307. 	case UNT_APPLEIDUN:
  7308. 	case UNT_HUMMING:
  7309. 	case UNT_DONTFORGETME:
  7310. 	case UNT_FORTUNEKISS:
  7311. 	case UNT_SERVICEFORYOU:
  7312. 		if (sg->src_id==bl->id && !(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_BARDDANCER))
  7313. 			return 0;
  7314. 		if (!sc) return 0;
  7315. 		if (!sce)
  7316. 			sc_start4(bl,type,100,sg->skill_lv,sg->val1,sg->val2,0,sg->limit);
  7317. 		else if (sce->val4 == 1) {
  7318. 			//Readjust timers since the effect will not last long.
  7319. 			sce->val4 = 0;
  7320. 			delete_timer(sce->timer, status_change_timer);
  7321. 			sce->timer = add_timer(tick+sg->limit, status_change_timer, bl->id, type);
  7322. 		}
  7323. 		break;
  7324.  
  7325. 	case UNT_FOGWALL:
  7326. 		if (!sce)
  7327. 		{
  7328. 			sc_start4(bl, type, 100, sg->skill_lv, sg->val1, sg->val2, sg->group_id, sg->limit);
  7329. 			if (battle_check_target(&src->bl,bl,BCT_ENEMY)>0)
  7330. 				skill_additional_effect (ss, bl, sg->skill_id, sg->skill_lv, BF_MISC, ATK_DEF, tick);
  7331. 		}
  7332. 		break;
  7333.  
  7334. 	case UNT_GRAVITATION:
  7335. 		if (!sce)
  7336. 			sc_start4(bl,type,100,sg->skill_lv,0,BCT_ENEMY,sg->group_id,sg->limit);
  7337. 		break;
  7338.  
  7339. // officially, icewall has no problems existing on occupied cells [ultramage]
  7340. //	case UNT_ICEWALL: //Destroy the cell. [Skotlex]
  7341. //		src->val1 = 0;
  7342. //		if(src->limit + sg->tick > tick + 700)
  7343. //			src->limit = DIFF_TICK(tick+700,sg->tick);
  7344. //		break;
  7345.  
  7346. 	case UNT_MOONLIT:
  7347. 		//Knockback out of area if affected char isn't in Moonlit effect
  7348. 		if (sc && sc->data[SC_DANCING] && (sc->data[SC_DANCING]->val1&0xFFFF) == CG_MOONLIT)
  7349. 			break;
  7350. 		if (ss == bl) //Also needed to prevent infinite loop crash.
  7351. 			break;
  7352. 		skill_blown(ss,bl,skill_get_blewcount(sg->skill_id,sg->skill_lv),unit_getdir(bl),0);
  7353. 		break;
  7354. 	}
  7355. 	return skillid;
  7356. }
  7357.  
  7358. /*==========================================
  7359.  *
  7360.  *------------------------------------------*/
  7361. int skill_unit_onplace_timer (struct skill_unit *src, struct block_list *bl, unsigned int tick)
  7362. {
  7363. 	struct skill_unit_group *sg;
  7364. 	struct block_list *ss;
  7365. 	TBL_PC* tsd;
  7366. 	struct status_data *tstatus, *sstatus;
  7367. 	struct status_change *tsc, *sc;
  7368. 	struct skill_unit_group_tickset *ts;
  7369. 	enum sc_type type;
  7370. 	int skillid;
  7371. 	int diff=0;
  7372.  
  7373. 	nullpo_ret(src);
  7374. 	nullpo_ret(bl);
  7375.  
  7376. 	if (bl->prev==NULL || !src->alive || status_isdead(bl))
  7377. 		return 0;
  7378.  
  7379. 	nullpo_ret(sg=src->group);
  7380. 	nullpo_ret(ss=map_id2bl(sg->src_id));
  7381. 	tsd = BL_CAST(BL_PC, bl);
  7382. 	tsc = status_get_sc(bl);
  7383. 	tstatus = status_get_status_data(bl);
  7384. 	if (sg->state.magic_power)  //For magic power.
  7385. 	{
  7386. 		sc = status_get_sc(ss);
  7387. 		sstatus = status_get_status_data(ss);
  7388. 	} else {
  7389. 		sc = NULL;
  7390. 		sstatus = NULL;
  7391. 	}
  7392. 	type = status_skill2sc(sg->skill_id);
  7393. 	skillid = sg->skill_id;
  7394.  
  7395. 	if (sg->interval == -1) {
  7396. 		switch (sg->unit_id) {
  7397. 			case UNT_ANKLESNARE: //These happen when a trap is splash-triggered by multiple targets on the same cell.
  7398. 			case UNT_FIREPILLAR_ACTIVE:
  7399. 				return 0;
  7400. 			default:
  7401. 				ShowError("skill_unit_onplace_timer: interval error (unit id %x)\n", sg->unit_id);
  7402. 				return 0;
  7403. 		}
  7404. 	}
  7405.  
  7406. 	if ((ts = skill_unitgrouptickset_search(bl,sg,tick)))
  7407. 	{	//Not all have it, eg: Traps don't have it even though they can be hit by Heaven's Drive [Skotlex]
  7408. 		diff = DIFF_TICK(tick,ts->tick);
  7409. 		if (diff < 0)
  7410. 			return 0;
  7411. 		ts->tick = tick+sg->interval;
  7412.  
  7413. 		if ((skillid==CR_GRANDCROSS || skillid==NPC_GRANDDARKNESS) && !battle_config.gx_allhit)
  7414. 			ts->tick += sg->interval*(map_count_oncell(bl->m,bl->x,bl->y,BL_CHAR)-1);
  7415. 	}
  7416. 	//Temporarily set magic power to have it take effect. [Skotlex]
  7417. 	if (sg->state.magic_power && sc && !sc->data[SC_MAGICPOWER])
  7418. 	{	//Store previous values.
  7419. 		swap(sstatus->matk_min, sc->mp_matk_min);
  7420. 		swap(sstatus->matk_max, sc->mp_matk_max);
  7421. 		//Note to NOT return from the function until this is unset!
  7422. 	}
  7423.  
  7424. 	switch (sg->unit_id)
  7425. 	{
  7426. 		case UNT_FIREWALL:
  7427. 		case UNT_KAEN:
  7428. 		{
  7429. 			int count=0;
  7430. 			const int x = bl->x, y = bl->y;
  7431.  
  7432. 			//Take into account these hit more times than the timer interval can handle.
  7433. 			do
  7434. 				skill_attack(BF_MAGIC,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick+count*sg->interval,0);
  7435. 			while(--src->val2 && x == bl->x && y == bl->y &&
  7436. 				++count < SKILLUNITTIMER_INTERVAL/sg->interval && !status_isdead(bl));
  7437.  
  7438. 			if (src->val2<=0)
  7439. 				skill_delunit(src);
  7440. 		}
  7441. 		break;
  7442.  
  7443. 		case UNT_SANCTUARY:
  7444. 			if( battle_check_undead(tstatus->race, tstatus->def_ele) || tstatus->race==RC_DEMON )
  7445. 			{ //Only damage enemies with offensive Sanctuary. [Skotlex]
  7446. 				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) )
  7447. 					sg->val1 -= 2; // reduce healing count if this was meant for damaging [hekate]
  7448. 			}
  7449. 			else
  7450. 			{
  7451. 				int heal = skill_calc_heal(ss,bl,sg->skill_id,sg->skill_lv,true);
  7452. 				struct mob_data *md = BL_CAST(BL_MOB, bl);
  7453. 				if( md && mob_is_battleground(md) )
  7454. 					break;
  7455. 				if( tstatus->hp >= tstatus->max_hp )
  7456. 					break;
  7457. 				if( status_isimmune(bl) )
  7458. 					heal = 0;
  7459. 				clif_skill_nodamage(&src->bl, bl, AL_HEAL, heal, 1);
  7460. 				status_heal(bl, heal, 0, 0);
  7461. 				if( diff >= 500 )
  7462. 					sg->val1--;
  7463. 			}
  7464. 			if( sg->val1 <= 0 )
  7465. 				skill_delunitgroup(sg);
  7466. 			break;
  7467.  
  7468. 		case UNT_EVILLAND:
  7469. 			//Will heal demon and undead element monsters, but not players.
  7470. 			if ((bl->type == BL_PC) || (!battle_check_undead(tstatus->race, tstatus->def_ele) && tstatus->race!=RC_DEMON))
  7471. 			{	//Damage enemies
  7472. 				if(battle_check_target(&src->bl,bl,BCT_ENEMY)>0)
  7473. 					skill_attack(BF_MISC, ss, &src->bl, bl, sg->skill_id, sg->skill_lv, tick, 0);
  7474. 			} else {
  7475. 				int heal = skill_calc_heal(ss,bl,sg->skill_id,sg->skill_lv,true);
  7476. 				if (tstatus->hp >= tstatus->max_hp)
  7477. 					break;
  7478. 				if (status_isimmune(bl))
  7479. 					heal = 0;
  7480. 				clif_skill_nodamage(&src->bl, bl, AL_HEAL, heal, 1);
  7481. 				status_heal(bl, heal, 0, 0);
  7482. 			}
  7483. 			break;
  7484.  
  7485. 		case UNT_MAGNUS:
  7486. 			if (!battle_check_undead(tstatus->race,tstatus->def_ele) && tstatus->race!=RC_DEMON)
  7487. 				break;
  7488. 			skill_attack(BF_MAGIC,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7489. 			break;
  7490.  
  7491. 		case UNT_DUMMYSKILL:
  7492. 			switch (sg->skill_id)
  7493. 			{
  7494. 				case SG_SUN_WARM: //SG skills [Komurka]
  7495. 				case SG_MOON_WARM:
  7496. 				case SG_STAR_WARM:
  7497. 				{
  7498. 					int count = 0;
  7499. 					const int x = bl->x, y = bl->y;
  7500.  
  7501. 					//If target isn't knocked back it should hit every "interval" ms [Playtester]
  7502. 					do
  7503. 					{
  7504. 						if( bl->type == BL_PC )
  7505. 							status_zap(bl, 0, 15); // sp damage to players
  7506. 						else // mobs
  7507. 						if( status_charge(ss, 0, 2) ) // costs 2 SP per hit
  7508. 						{
  7509. 							if( !skill_attack(BF_WEAPON,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick+count*sg->interval,0) )
  7510. 								status_charge(ss, 0, 8); //costs additional 8 SP if miss
  7511. 						}
  7512. 						else
  7513. 						{ //should end when out of sp.
  7514. 							sg->limit = DIFF_TICK(tick,sg->tick);
  7515. 							break;
  7516. 						}
  7517. 					} while( x == bl->x && y == bl->y &&
  7518. 						++count < SKILLUNITTIMER_INTERVAL/sg->interval && !status_isdead(bl) );
  7519. 				}
  7520. 				break;
  7521. 				case WZ_STORMGUST: //SG counter does not reset per stormgust. IE: One hit from a SG and two hits from another will freeze you.
  7522. 					if (tsc) 
  7523. 						tsc->sg_counter++; //SG hit counter.
  7524. 					if (skill_attack(skill_get_type(sg->skill_id),ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0) <= 0 && tsc)
  7525. 						tsc->sg_counter=0; //Attack absorbed.
  7526. 				break;
  7527.  
  7528. 				case GS_DESPERADO:
  7529. 					if (rand()%100 < src->val1)
  7530. 						skill_attack(BF_WEAPON,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7531. 				break;
  7532.  
  7533. 				default:
  7534. 					skill_attack(skill_get_type(sg->skill_id),ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7535. 			}
  7536. 			break;
  7537.  
  7538. 		case UNT_FIREPILLAR_WAITING:
  7539. 			skill_unitsetting(ss,sg->skill_id,sg->skill_lv,src->bl.x,src->bl.y,1);
  7540. 			skill_delunit(src);
  7541. 			break;
  7542.  
  7543. 		case UNT_SKIDTRAP:
  7544. 			{
  7545. 				skill_blown(&src->bl,bl,skill_get_blewcount(sg->skill_id,sg->skill_lv),unit_getdir(bl),0);
  7546. 				sg->unit_id = UNT_USED_TRAPS;
  7547. 				clif_changetraplook(&src->bl, UNT_USED_TRAPS);
  7548. 				sg->limit=DIFF_TICK(tick,sg->tick)+1500;
  7549. 			}
  7550. 			break;
  7551.  
  7552. 		case UNT_ANKLESNARE:
  7553. 			if( sg->val2 == 0 && tsc )
  7554. 			{
  7555. 				int sec = skill_get_time2(sg->skill_id,sg->skill_lv);
  7556. 				if( status_change_start(bl,type,10000,sg->skill_lv,sg->group_id,0,0,sec, 8) )
  7557. 				{
  7558. 					const struct TimerData* td = tsc->data[type]?get_timer(tsc->data[type]->timer):NULL; 
  7559. 					if( td )
  7560. 						sec = DIFF_TICK(td->tick, tick);
  7561. 					unit_movepos(bl, src->bl.x, src->bl.y, 0, 0);
  7562. 					clif_fixpos(bl);
  7563. 					sg->val2 = bl->id;
  7564. 				}
  7565. 				else
  7566. 					sec = 3000; //Couldn't trap it?
  7567. 				clif_skillunit_update(&src->bl);
  7568. 				sg->limit = DIFF_TICK(tick,sg->tick)+sec;
  7569. 				sg->interval = -1;
  7570. 				src->range = 0;
  7571. 			}
  7572. 			break;
  7573.  
  7574. 		case UNT_VENOMDUST:
  7575. 			if(tsc && !tsc->data[type])
  7576. 				status_change_start(bl,type,10000,sg->skill_lv,sg->group_id,0,0,skill_get_time2(sg->skill_id,sg->skill_lv),8);
  7577. 			break;
  7578.  
  7579. 		case UNT_LANDMINE:
  7580. 		case UNT_CLAYMORETRAP:
  7581. 		case UNT_BLASTMINE:
  7582. 		case UNT_SHOCKWAVE:
  7583. 		case UNT_SANDMAN:
  7584. 		case UNT_FLASHER:
  7585. 		case UNT_FREEZINGTRAP:
  7586. 		case UNT_FIREPILLAR_ACTIVE:
  7587. 			map_foreachinrange(skill_trap_splash,&src->bl, skill_get_splash(sg->skill_id, sg->skill_lv), sg->bl_flag, &src->bl,tick);
  7588. 			if (sg->unit_id != UNT_FIREPILLAR_ACTIVE)
  7589. 				clif_changetraplook(&src->bl, sg->unit_id==UNT_LANDMINE?UNT_FIREPILLAR_ACTIVE:UNT_USED_TRAPS);
  7590. 			src->range = -1; //Disable range so it does not invoke a for each in area again.
  7591. 			sg->limit=DIFF_TICK(tick,sg->tick)+1500;
  7592. 			break;
  7593.  
  7594. 		case UNT_TALKIEBOX:
  7595. 			if (sg->src_id == bl->id)
  7596. 				break;
  7597. 			if (sg->val2 == 0){
  7598. 				clif_talkiebox(&src->bl, sg->valstr);
  7599. 				sg->unit_id = UNT_USED_TRAPS;
  7600. 				clif_changetraplook(&src->bl, UNT_USED_TRAPS);
  7601. 				sg->limit = DIFF_TICK(tick, sg->tick) + 5000;
  7602. 				sg->val2 = -1;
  7603. 			}
  7604. 			break;
  7605.  
  7606. 		case UNT_LULLABY:
  7607. 			if (ss->id == bl->id)
  7608. 				break;
  7609. 			skill_additional_effect(ss, bl, sg->skill_id, sg->skill_lv, BF_LONG|BF_SKILL|BF_MISC, ATK_DEF, tick);
  7610. 			break;
  7611.  
  7612. 		case UNT_UGLYDANCE:	//Ugly Dance [Skotlex]
  7613. 			if (ss->id != bl->id)
  7614. 				skill_additional_effect(ss, bl, sg->skill_id, sg->skill_lv, BF_LONG|BF_SKILL|BF_MISC, ATK_DEF, tick);
  7615. 			break;
  7616.  
  7617. 		case UNT_DISSONANCE:
  7618. 			skill_attack(BF_MISC, ss, &src->bl, bl, sg->skill_id, sg->skill_lv, tick, 0);
  7619. 			break;
  7620.  
  7621. 		case UNT_APPLEIDUN: //Apple of Idun [Skotlex]
  7622. 		{
  7623. 			int heal;
  7624. 			if( sg->src_id == bl->id && !(tsc && tsc->data[SC_SPIRIT] && tsc->data[SC_SPIRIT]->val2 == SL_BARDDANCER) )
  7625. 				break; // affects self only when soullinked
  7626. 			heal = skill_calc_heal(ss,bl,sg->skill_id, sg->skill_lv, true);
  7627. 			clif_skill_nodamage(&src->bl, bl, AL_HEAL, heal, 1);
  7628. 			status_heal(bl, heal, 0, 0);
  7629. 			break;
  7630. 		}
  7631.  
  7632.  		case UNT_TATAMIGAESHI:
  7633. 		case UNT_DEMONSTRATION:
  7634. 			skill_attack(BF_WEAPON,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7635. 			break;
  7636.  
  7637. 		case UNT_GOSPEL:
  7638. 			if (rand()%100 > sg->skill_lv*10 || ss == bl)
  7639. 				break;
  7640. 			if (battle_check_target(ss,bl,BCT_PARTY)>0)
  7641. 			{ // Support Effect only on party, not guild
  7642. 				int heal;
  7643. 				int i = rand()%13; // Positive buff count
  7644. 				int time = skill_get_time2(sg->skill_id, sg->skill_lv); //Duration
  7645. 				switch (i)
  7646. 				{
  7647. 					case 0: // Heal 1~9999 HP
  7648. 						heal = rand() %9999+1;
  7649. 						clif_skill_nodamage(ss,bl,AL_HEAL,heal,1);
  7650. 						status_heal(bl,heal,0,0);
  7651. 						break;
  7652. 					case 1: // End all negative status
  7653. 						status_change_clear_buffs(bl,2);
  7654. 						if (tsd) clif_gospel_info(tsd, 0x15);
  7655. 						break;
  7656. 					case 2: // Immunity to all status
  7657. 						sc_start(bl,SC_SCRESIST,100,100,time);
  7658. 						if (tsd) clif_gospel_info(tsd, 0x16);
  7659. 						break;
  7660. 					case 3: // MaxHP +100%
  7661. 						sc_start(bl,SC_INCMHPRATE,100,100,time);
  7662. 						if (tsd) clif_gospel_info(tsd, 0x17);
  7663. 						break;
  7664. 					case 4: // MaxSP +100%
  7665. 						sc_start(bl,SC_INCMSPRATE,100,100,time);
  7666. 						if (tsd) clif_gospel_info(tsd, 0x18);
  7667. 						break;
  7668. 					case 5: // All stats +20
  7669. 						sc_start(bl,SC_INCALLSTATUS,100,20,time);
  7670. 						if (tsd) clif_gospel_info(tsd, 0x19);
  7671. 						break;
  7672. 					case 6: // Level 10 Blessing
  7673. 						sc_start(bl,SC_BLESSING,100,10,time);
  7674. 						break;
  7675. 					case 7: // Level 10 Increase AGI
  7676. 						sc_start(bl,SC_INCREASEAGI,100,10,time);
  7677. 						break;
  7678. 					case 8: // Enchant weapon with Holy element
  7679. 						sc_start(bl,SC_ASPERSIO,100,1,time);
  7680. 						if (tsd) clif_gospel_info(tsd, 0x1c);
  7681. 						break;
  7682. 					case 9: // Enchant armor with Holy element
  7683. 						sc_start(bl,SC_BENEDICTIO,100,1,time);
  7684. 						if (tsd) clif_gospel_info(tsd, 0x1d);
  7685. 						break;
  7686. 					case 10: // DEF +25%
  7687. 						sc_start(bl,SC_INCDEFRATE,100,25,time);
  7688. 						if (tsd) clif_gospel_info(tsd, 0x1e);
  7689. 						break;
  7690. 					case 11: // ATK +100%
  7691. 						sc_start(bl,SC_INCATKRATE,100,100,time);
  7692. 						if (tsd) clif_gospel_info(tsd, 0x1f);
  7693. 						break;
  7694. 					case 12: // HIT/Flee +50
  7695. 						sc_start(bl,SC_INCHIT,100,50,time);
  7696. 						sc_start(bl,SC_INCFLEE,100,50,time);
  7697. 						if (tsd) clif_gospel_info(tsd, 0x20);
  7698. 						break;
  7699. 				}
  7700. 			}
  7701. 			else if (battle_check_target(&src->bl,bl,BCT_ENEMY)>0)
  7702. 			{ // Offensive Effect
  7703. 				int i = rand()%9; // Negative buff count
  7704. 				int time = skill_get_time2(sg->skill_id, sg->skill_lv);
  7705. 				switch (i)
  7706. 				{
  7707. 					case 0: // Deal 1~9999 damage
  7708. 						skill_attack(BF_MISC,ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7709. 						break;
  7710. 					case 1: // Curse
  7711. 						sc_start(bl,SC_CURSE,100,1,time);
  7712. 						break;
  7713. 					case 2: // Blind
  7714. 						sc_start(bl,SC_BLIND,100,1,time);
  7715. 						break;
  7716. 					case 3: // Poison
  7717. 						sc_start(bl,SC_POISON,100,1,time);
  7718. 						break;
  7719. 					case 4: // Level 10 Provoke
  7720. 						sc_start(bl,SC_PROVOKE,100,10,time);
  7721. 						break;
  7722. 					case 5: // DEF -100%
  7723. 						sc_start(bl,SC_INCDEFRATE,100,-100,time);
  7724. 						break;
  7725. 					case 6: // ATK -100%
  7726. 						sc_start(bl,SC_INCATKRATE,100,-100,time);
  7727. 						break;
  7728. 					case 7: // Flee -100%
  7729. 						sc_start(bl,SC_INCFLEERATE,100,-100,time);
  7730. 						break;
  7731. 					case 8: // Speed/ASPD -25%
  7732. 						sc_start4(bl,SC_GOSPEL,100,1,0,0,BCT_ENEMY,time);
  7733. 						break;
  7734. 				}
  7735. 			}
  7736. 			break;
  7737.  
  7738. 		case UNT_BASILICA:
  7739. 			{
  7740. 				int i = battle_check_target(&src->bl, bl, BCT_ENEMY);
  7741. 				if( i > 0 && !(status_get_mode(bl)&MD_BOSS) )
  7742. 				{ // knock-back any enemy except Boss
  7743. 					skill_blown(&src->bl, bl, 2, unit_getdir(bl), 0);
  7744. 					clif_fixpos(bl);
  7745. 				}
  7746.  
  7747. 				if( sg->src_id != bl->id && i <= 0 )
  7748. 					sc_start4(bl, type, 100, 0, 0, 0, src->bl.id, sg->interval + 100);
  7749. 			}
  7750. 			break;
  7751.  
  7752. 		case UNT_GRAVITATION:
  7753. 			skill_attack(skill_get_type(sg->skill_id),ss,&src->bl,bl,sg->skill_id,sg->skill_lv,tick,0);
  7754. 			break;
  7755.  
  7756. 		case UNT_GROUNDDRIFT_WIND:
  7757. 		case UNT_GROUNDDRIFT_DARK:
  7758. 		case UNT_GROUNDDRIFT_POISON:
  7759. 		case UNT_GROUNDDRIFT_WATER:
  7760. 		case UNT_GROUNDDRIFT_FIRE:
  7761. 			map_foreachinrange(skill_trap_splash,&src->bl,
  7762. 				skill_get_splash(sg->skill_id, sg->skill_lv), sg->bl_flag,
  7763. 				&src->bl,tick);
  7764. 			sg->unit_id = UNT_USED_TRAPS;
  7765. 			//clif_changetraplook(&src->bl, UNT_FIREPILLAR_ACTIVE);
  7766. 			sg->limit=DIFF_TICK(tick,sg->tick)+1500;
  7767. 			break;
  7768. 	}
  7769.  
  7770. 	if (sg->state.magic_power && sc && !sc->data[SC_MAGICPOWER])
  7771. 	{	//Unset magic power.
  7772. 		swap(sstatus->matk_min, sc->mp_matk_min);
  7773. 		swap(sstatus->matk_max, sc->mp_matk_max);
  7774. 	}
  7775.  
  7776. 	if (bl->type == BL_MOB && ss != bl)
  7777. 		mobskill_event((TBL_MOB*)bl, ss, tick, MSC_SKILLUSED|(skillid<<16));
  7778.  
  7779. 	return skillid;
  7780. }
  7781. /*==========================================
  7782.  * Triggered when a char steps out of a skill cell
  7783.  *------------------------------------------*/
  7784. int skill_unit_onout (struct skill_unit *src, struct block_list *bl, unsigned int tick)
  7785. {
  7786. 	struct skill_unit_group *sg;
  7787. 	struct status_change *sc;
  7788. 	struct status_change_entry *sce;
  7789. 	enum sc_type type;
  7790.  
  7791. 	nullpo_ret(src);
  7792. 	nullpo_ret(bl);
  7793. 	nullpo_ret(sg=src->group);
  7794. 	sc = status_get_sc(bl);
  7795. 	type = status_skill2sc(sg->skill_id);
  7796. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  7797.  
  7798. 	if( bl->prev==NULL ||
  7799. 		(status_isdead(bl) && sg->unit_id != UNT_ANKLESNARE && sg->unit_id != UNT_SPIDERWEB) ) //Need to delete the trap if the source died.
  7800. 		return 0;
  7801.  
  7802. 	switch(sg->unit_id){
  7803. 	case UNT_SAFETYWALL:
  7804. 	case UNT_PNEUMA:
  7805. 		if (sce)
  7806. 			status_change_end(bl, type, INVALID_TIMER);
  7807. 		break;
  7808.  
  7809. 	case UNT_BASILICA:
  7810. 		if( sce && sce->val4 == src->bl.id )
  7811. 			status_change_end(bl, type, INVALID_TIMER);
  7812. 		break;
  7813.  
  7814. 	case UNT_HERMODE:	//Clear Hermode if the owner moved.
  7815. 		if (sce && sce->val3 == BCT_SELF && sce->val4 == sg->group_id)
  7816. 			status_change_end(bl, type, INVALID_TIMER);
  7817. 		break;
  7818.  
  7819. 	case UNT_SPIDERWEB:
  7820. 		{
  7821. 			struct block_list *target = map_id2bl(sg->val2);
  7822. 			if (target && target==bl)
  7823. 			{
  7824. 				if (sce && sce->val3 == sg->group_id)
  7825. 					status_change_end(bl, type, INVALID_TIMER);
  7826. 				sg->limit = DIFF_TICK(tick,sg->tick)+1000;
  7827. 			}
  7828. 			break;
  7829. 		}
  7830. 	}
  7831. 	return sg->skill_id;
  7832. }
  7833.  
  7834. /*==========================================
  7835.  * Triggered when a char steps out of a skill group (entirely) [Skotlex]
  7836.  *------------------------------------------*/
  7837. static int skill_unit_onleft (int skill_id, struct block_list *bl, unsigned int tick)
  7838. {
  7839. 	struct status_change *sc;
  7840. 	struct status_change_entry *sce;
  7841. 	enum sc_type type;
  7842.  
  7843. 	sc = status_get_sc(bl);
  7844. 	if (sc && !sc->count)
  7845. 		sc = NULL;
  7846.  
  7847. 	type = status_skill2sc(skill_id);
  7848. 	sce = (sc && type != -1)?sc->data[type]:NULL;
  7849.  
  7850. 	switch (skill_id)
  7851. 	{
  7852. 		case WZ_QUAGMIRE:
  7853. 			if (bl->type==BL_MOB)
  7854. 				break;
  7855. 			if (sce)
  7856. 				status_change_end(bl, type, INVALID_TIMER);
  7857. 			break;
  7858.  
  7859. 		case BD_LULLABY:
  7860. 		case BD_RICHMANKIM:
  7861. 		case BD_ETERNALCHAOS:
  7862. 		case BD_DRUMBATTLEFIELD:
  7863. 		case BD_RINGNIBELUNGEN:
  7864. 		case BD_ROKISWEIL:
  7865. 		case BD_INTOABYSS:
  7866. 		case BD_SIEGFRIED:
  7867. 			if(sc && sc->data[SC_DANCING] && (sc->data[SC_DANCING]->val1&0xFFFF) == skill_id)
  7868. 			{	//Check if you just stepped out of your ensemble skill to cancel dancing. [Skotlex]
  7869. 				//We don't check for SC_LONGING because someone could always have knocked you back and out of the song/dance.
  7870. 				//FIXME: This code is not perfect, it doesn't checks for the real ensemble's owner,
  7871. 				//it only checks if you are doing the same ensemble. So if there's two chars doing an ensemble
  7872. 				//which overlaps, by stepping outside of the other parther's ensemble will cause you to cancel
  7873. 				//your own. Let's pray that scenario is pretty unlikely and noone will complain too much about it.
  7874. 				status_change_end(bl, SC_DANCING, INVALID_TIMER);
  7875. 			}
  7876. 		case MG_SAFETYWALL:
  7877. 		case AL_PNEUMA:
  7878. 		case SA_VOLCANO:
  7879. 		case SA_DELUGE:
  7880. 		case SA_VIOLENTGALE:
  7881. 		case CG_HERMODE:
  7882. 		case HW_GRAVITATION:
  7883. 		case NJ_SUITON:
  7884. 			if (sce)
  7885. 				status_change_end(bl, type, INVALID_TIMER);
  7886. 			break;
  7887.  
  7888. 		case BA_POEMBRAGI:
  7889. 		case BA_WHISTLE:
  7890. 		case BA_ASSASSINCROSS:
  7891. 		case BA_APPLEIDUN:
  7892. 		case DC_HUMMING:
  7893. 		case DC_DONTFORGETME:
  7894. 		case DC_FORTUNEKISS:
  7895. 		case DC_SERVICEFORYOU:
  7896. 			if (sce)
  7897. 			{
  7898. 				delete_timer(sce->timer, status_change_timer);
  7899. 				//NOTE: It'd be nice if we could get the skill_lv for a more accurate extra time, but alas...
  7900. 				//not possible on our current implementation.
  7901. 				sce->val4 = 1; //Store the fact that this is a "reduced" duration effect.
  7902. 				sce->timer = add_timer(tick+skill_get_time2(skill_id,1), status_change_timer, bl->id, type);
  7903. 			}
  7904. 			break;
  7905. 		case PF_FOGWALL:
  7906. 			if (sce)
  7907. 			{
  7908. 				status_change_end(bl, type, INVALID_TIMER);
  7909. 				if ((sce=sc->data[SC_BLIND]))
  7910. 				{
  7911. 					if (bl->type == BL_PC) //Players get blind ended inmediately, others have it still for 30 secs. [Skotlex]
  7912. 						status_change_end(bl, SC_BLIND, INVALID_TIMER);
  7913. 					else {
  7914. 						delete_timer(sce->timer, status_change_timer);
  7915. 						sce->timer = add_timer(30000+tick, status_change_timer, bl->id, SC_BLIND);
  7916. 					}
  7917. 				}
  7918. 			}
  7919. 			break;
  7920. 	}
  7921.  
  7922. 	return skill_id;
  7923. }
  7924.  
  7925. /*==========================================
  7926.  * Invoked when a unit cell has been placed/removed/deleted.
  7927.  * flag values:
  7928.  * flag&1: Invoke onplace function (otherwise invoke onout)
  7929.  * flag&4: Invoke a onleft call (the unit might be scheduled for deletion)
  7930.  *------------------------------------------*/
  7931. static int skill_unit_effect (struct block_list* bl, va_list ap)
  7932. {
  7933. 	struct skill_unit* unit = va_arg(ap,struct skill_unit*);
  7934. 	struct skill_unit_group* group = unit->group;
  7935. 	unsigned int tick = va_arg(ap,unsigned int);
  7936. 	unsigned int flag = va_arg(ap,unsigned int);
  7937. 	int skill_id;
  7938. 	bool dissonance;
  7939.  
  7940. 	if( (!unit->alive && !(flag&4)) || bl->prev == NULL )
  7941. 		return 0;
  7942.  
  7943. 	nullpo_ret(group);
  7944.  
  7945. 	dissonance = skill_dance_switch(unit, 0);
  7946.  
  7947. 	//Necessary in case the group is deleted after calling on_place/on_out [Skotlex]
  7948. 	skill_id = group->skill_id;
  7949.  
  7950. 	//Target-type check.
  7951. 	if( !(group->bl_flag&bl->type && battle_check_target(&unit->bl,bl,group->target_flag)>0) )
  7952. 	{
  7953. 		if( flag&4 && group->src_id == bl->id && group->state.song_dance&0x2 )
  7954. 			skill_unit_onleft(skill_id, bl, tick);//Ensemble check to terminate it.
  7955. 	}
  7956. 	else
  7957. 	{
  7958. 		if( flag&1 )
  7959. 			skill_unit_onplace(unit,bl,tick);
  7960. 		else
  7961. 			skill_unit_onout(unit,bl,tick);
  7962.  
  7963. 		if( flag&4 )
  7964. 	  		skill_unit_onleft(skill_id, bl, tick);
  7965. 	}
  7966.  
  7967. 	if( dissonance ) skill_dance_switch(unit, 1);
  7968.  
  7969. 	return 0;
  7970. }
  7971.  
  7972. /*==========================================
  7973.  *
  7974.  *------------------------------------------*/
  7975. int skill_unit_ondamaged (struct skill_unit *src, struct block_list *bl, int damage, unsigned int tick)
  7976. {
  7977. 	struct skill_unit_group *sg;
  7978.  
  7979. 	nullpo_ret(src);
  7980. 	nullpo_ret(sg=src->group);
  7981.  
  7982. 	switch( sg->unit_id )
  7983. 	{
  7984. 	case UNT_SKIDTRAP:
  7985. 	case UNT_LANDMINE:
  7986. 	case UNT_SHOCKWAVE:
  7987. 	case UNT_SANDMAN:
  7988. 	case UNT_FLASHER:
  7989. 	case UNT_FREEZINGTRAP:
  7990. 	case UNT_TALKIEBOX:
  7991. 	case UNT_ANKLESNARE:
  7992. 	case UNT_ICEWALL:
  7993. 		src->val1-=damage;
  7994. 		break;
  7995. 	case UNT_BLASTMINE:
  7996. 	case UNT_CLAYMORETRAP:
  7997. 		skill_blown(bl, &src->bl, 2, -1, 0);
  7998. 		break;
  7999. 	default:
  8000. 		damage = 0;
  8001. 		break;
  8002. 	}
  8003. 	return damage;
  8004. }
  8005.  
  8006. /*==========================================
  8007.  *
  8008.  *------------------------------------------*/
  8009. static int skill_check_condition_char_sub (struct block_list *bl, va_list ap)
  8010. {
  8011. 	int *c, skillid;
  8012. 	struct block_list *src;
  8013. 	struct map_session_data *sd;
  8014. 	struct map_session_data *tsd;
  8015. 	int *p_sd;	//Contains the list of characters found.
  8016.  
  8017. 	nullpo_ret(bl);
  8018. 	nullpo_ret(tsd=(struct map_session_data*)bl);
  8019. 	nullpo_ret(src=va_arg(ap,struct block_list *));
  8020. 	nullpo_ret(sd=(struct map_session_data*)src);
  8021.  
  8022. 	c=va_arg(ap,int *);
  8023. 	p_sd = va_arg(ap, int *);
  8024. 	skillid = va_arg(ap,int);
  8025.  
  8026. 	if ((skillid != PR_BENEDICTIO && *c >=1) || *c >=2)
  8027. 		return 0; //Partner found for ensembles, or the two companions for Benedictio. [Skotlex]
  8028.  
  8029. 	if (bl == src)
  8030. 		return 0;
  8031.  
  8032. 	if(pc_isdead(tsd))
  8033. 		return 0;
  8034.  
  8035. 	if (tsd->sc.data[SC_SILENCE] || tsd->sc.opt1)
  8036. 		return 0;
  8037.  
  8038. 	switch(skillid)
  8039. 	{
  8040. 		case PR_BENEDICTIO:
  8041. 		{
  8042. 			int dir = map_calc_dir(&sd->bl,tsd->bl.x,tsd->bl.y);
  8043. 			dir = (unit_getdir(&sd->bl) + dir)%8; //This adjusts dir to account for the direction the sd is facing.
  8044. 			if ((tsd->class_&MAPID_BASEMASK) == MAPID_ACOLYTE && (dir == 2 || dir == 6) //Must be standing to the left/right of Priest.
  8045. 				&& sd->status.sp >= 10)
  8046. 				p_sd[(*c)++]=tsd->bl.id;
  8047. 			return 1;
  8048. 		}
  8049. 		default: //Warning: Assuming Ensemble Dance/Songs for code speed. [Skotlex]
  8050. 			{
  8051. 				int skilllv;
  8052. 				if(pc_issit(tsd) || !unit_can_move(&tsd->bl))
  8053. 					return 0;
  8054. 				if (sd->status.sex != tsd->status.sex &&
  8055. 						(tsd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER &&
  8056. 						(skilllv = pc_checkskill(tsd, skillid)) > 0 &&
  8057. 						(tsd->weapontype1==W_MUSICAL || tsd->weapontype1==W_WHIP) &&
  8058. 						sd->status.party_id && tsd->status.party_id &&
  8059. 						sd->status.party_id == tsd->status.party_id &&
  8060. 						!tsd->sc.data[SC_DANCING])
  8061. 				{
  8062. 					p_sd[(*c)++]=tsd->bl.id;
  8063. 					return skilllv;
  8064. 				} else {
  8065. 					return 0;
  8066. 				}
  8067. 			}
  8068. 			break;
  8069. 	}
  8070. 	return 0;
  8071. }
  8072.  
  8073. /*==========================================
  8074.  * Checks and stores partners for ensemble skills [Skotlex]
  8075.  *------------------------------------------*/
  8076. int skill_check_pc_partner (struct map_session_data *sd, short skill_id, short* skill_lv, int range, int cast_flag)
  8077. {
  8078. 	static int c=0;
  8079. 	static int p_sd[2] = { 0, 0 };
  8080. 	int i;
  8081.  
  8082. 	if (!battle_config.player_skill_partner_check ||
  8083. 		(battle_config.gm_skilluncond && pc_isGM(sd) >= battle_config.gm_skilluncond))
  8084. 		return 99; //As if there were infinite partners.
  8085.  
  8086. 	if (cast_flag)
  8087. 	{	//Execute the skill on the partners.
  8088. 		struct map_session_data* tsd;
  8089. 		switch (skill_id)
  8090. 		{
  8091. 			case PR_BENEDICTIO:
  8092. 				for (i = 0; i < c; i++)
  8093. 				{
  8094. 					if ((tsd = map_id2sd(p_sd[i])) != NULL)
  8095. 						status_charge(&tsd->bl, 0, 10);
  8096. 				}
  8097. 				return c;
  8098. 			default: //Warning: Assuming Ensemble skills here (for speed)
  8099. 				if (c > 0 && sd->sc.data[SC_DANCING] && (tsd = map_id2sd(p_sd[0])) != NULL)
  8100. 				{
  8101. 					sd->sc.data[SC_DANCING]->val4 = tsd->bl.id;
  8102. 					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);
  8103. 					clif_skill_nodamage(&tsd->bl, &sd->bl, skill_id, *skill_lv, 1);
  8104. 					tsd->skillid_dance = skill_id;
  8105. 					tsd->skilllv_dance = *skill_lv;
  8106. 				}
  8107. 				return c;
  8108. 		}
  8109. 	}
  8110.  
  8111. 	//Else: new search for partners.
  8112. 	c = 0;
  8113. 	memset (p_sd, 0, sizeof(p_sd));
  8114. 	i = map_foreachinrange(skill_check_condition_char_sub, &sd->bl, range, BL_PC, &sd->bl, &c, &p_sd, skill_id);
  8115.  
  8116. 	if (skill_id != PR_BENEDICTIO) //Apply the average lv to encore skills.
  8117. 		*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.
  8118. 	return c;
  8119. }
  8120.  
  8121. /*==========================================
  8122.  *
  8123.  *------------------------------------------*/
  8124. static int skill_check_condition_mob_master_sub (struct block_list *bl, va_list ap)
  8125. {
  8126. 	int *c,src_id,mob_class,skill;
  8127. 	struct mob_data *md;
  8128.  
  8129. 	md=(struct mob_data*)bl;
  8130. 	src_id=va_arg(ap,int);
  8131. 	mob_class=va_arg(ap,int);
  8132. 	skill=va_arg(ap,int);
  8133. 	c=va_arg(ap,int *);
  8134.  
  8135. 	if( md->master_id != src_id || md->special_state.ai != (unsigned)(skill == AM_SPHEREMINE?2:3) )
  8136. 		return 0; //Non alchemist summoned mobs have nothing to do here.
  8137.  
  8138. 	if(md->class_==mob_class)
  8139. 		(*c)++;
  8140.  
  8141. 	return 1;
  8142. }
  8143.  
  8144. /*==========================================
  8145.  * Determines if a given skill should be made to consume ammo
  8146.  * when used by the player. [Skotlex]
  8147.  *------------------------------------------*/
  8148. int skill_isammotype (struct map_session_data *sd, int skill)
  8149. {
  8150. 	return (
  8151. 		battle_config.arrow_decrement==2 &&
  8152. 		(sd->status.weapon == W_BOW || (sd->status.weapon >= W_REVOLVER && sd->status.weapon <= W_GRENADE)) &&
  8153. 		skill != HT_PHANTASMIC &&
  8154. 		skill_get_type(skill) == BF_WEAPON &&
  8155. 	  	!(skill_get_nk(skill)&NK_NO_DAMAGE) &&
  8156. 		!skill_get_spiritball(skill,1) //Assume spirit spheres are used as ammo instead.
  8157. 	);
  8158. }
  8159.  
  8160. int skill_check_condition_castbegin(struct map_session_data* sd, short skill, short lv)
  8161. {
  8162. 	struct status_data *status;
  8163. 	struct status_change *sc;
  8164. 	struct skill_condition require;
  8165. 	int i;
  8166.  
  8167. 	nullpo_ret(sd);
  8168.  
  8169. 	if (lv <= 0 || sd->chatID) return 0;
  8170.  
  8171. 	if( battle_config.gm_skilluncond && pc_isGM(sd)>= battle_config.gm_skilluncond && sd->skillitem != skill )
  8172. 	{	//GMs don't override the skillItem check, otherwise they can use items without them being consumed! [Skotlex]	
  8173. 		sd->state.arrow_atk = skill_get_ammotype(skill)?1:0; //Need to do arrow state check.
  8174. 		sd->spiritball_old = sd->spiritball; //Need to do Spiritball check.
  8175. 		return 1;
  8176. 	}
  8177.  
  8178. 	if( sd->menuskill_id == AM_PHARMACY )
  8179. 	{
  8180. 		switch( skill )
  8181. 		{
  8182. 		case AM_PHARMACY:
  8183. 		case AC_MAKINGARROW:
  8184. 		case BS_REPAIRWEAPON:
  8185. 		case AM_TWILIGHT1:
  8186. 		case AM_TWILIGHT2:
  8187. 		case AM_TWILIGHT3:
  8188. 			return 0;
  8189. 		}
  8190. 	}
  8191.  
  8192. 	status = &sd->battle_status;
  8193. 	sc = &sd->sc;
  8194. 	if( !sc->count )
  8195. 		sc = NULL;
  8196.  
  8197. 	if( sd->skillitem == skill )
  8198. 	{
  8199. 		if( sd->state.abra_flag ) // Hocus-Pocus was used. [Inkfish]
  8200. 			sd->state.abra_flag = 0;
  8201. 		else
  8202. 		{ // When a target was selected, consume items that were skipped in pc_use_item [Skotlex]
  8203. 			if( (i = sd->itemindex) == -1 ||
  8204. 				sd->status.inventory[i].nameid != sd->itemid ||
  8205. 				sd->inventory_data[i] == NULL ||
  8206. 				!sd->inventory_data[i]->flag.delay_consume ||
  8207. 				sd->status.inventory[i].amount < 1
  8208. 				)
  8209. 			{	//Something went wrong, item exploit?
  8210. 				sd->itemid = sd->itemindex = -1;
  8211. 				return 0;
  8212. 			}
  8213. 			//Consume
  8214. 			sd->itemid = sd->itemindex = -1;
  8215. 			if( skill == WZ_EARTHSPIKE && sc && sc->data[SC_EARTHSCROLL] && rand()%100 > sc->data[SC_EARTHSCROLL]->val2 ) // [marquis007]
  8216. 				; //Do not consume item.
  8217. 			else if( sd->status.inventory[i].expire_time == 0 )
  8218. 				pc_delitem(sd,i,1,0,0); // Rental usable items are not consumed until expiration
  8219. 		}
  8220. 		return 1;
  8221. 	}
  8222.  
  8223. 	if( pc_is90overweight(sd) )
  8224. 	{
  8225. 		clif_skill_fail(sd,skill,USESKILL_FAIL_WEIGHTOVER,0);
  8226. 		return 0;
  8227. 	}
  8228.  
  8229. 	switch( skill )
  8230. 	{ // Turn off check.
  8231. 	case BS_MAXIMIZE:		case NV_TRICKDEAD:	case TF_HIDING:			case AS_CLOAKING:		case CR_AUTOGUARD:
  8232. 	case ML_AUTOGUARD:		case CR_DEFENDER:	case ML_DEFENDER:		case ST_CHASEWALK:		case PA_GOSPEL:
  8233. 	case CR_SHRINK:			case TK_RUN:		case GS_GATLINGFEVER:	case TK_READYCOUNTER:	case TK_READYDOWN:
  8234. 	case TK_READYSTORM:		case TK_READYTURN:	case SG_FUSION:
  8235. 		if( sc && sc->data[status_skill2sc(skill)] )
  8236. 			return 1;
  8237. 	}
  8238.  
  8239. 	if( lv < 1 || lv > MAX_SKILL_LEVEL )
  8240. 		return 0;
  8241.  
  8242. 	require = skill_get_requirement(sd,skill,lv);
  8243.  
  8244. 	//Can only update state when weapon/arrow info is checked.
  8245. 	sd->state.arrow_atk = require.ammo?1:0;
  8246.  
  8247. 	// perform skill-specific checks (and actions)
  8248. 	switch( skill )
  8249. 	{
  8250. 	case SA_CASTCANCEL:
  8251. 		if(sd->ud.skilltimer == INVALID_TIMER) {
  8252. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8253. 			return 0;
  8254. 		}
  8255. 		break;
  8256. 	case AL_WARP:
  8257. 		if(!battle_config.duel_allow_teleport && sd->duel_group) { // duel restriction [LuzZza]
  8258. 			clif_displaymessage(sd->fd, "Duel: Can't use warp in duel.");
  8259. 			return 0;
  8260. 		}
  8261. 		break;
  8262. 	case MO_CALLSPIRITS:
  8263. 		if(sd->spiritball >= lv) {
  8264. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8265. 			return 0;
  8266. 		}
  8267. 		break;
  8268. 	case MO_FINGEROFFENSIVE:
  8269. 	case GS_FLING:
  8270. 		if( sd->spiritball > 0 && sd->spiritball < require.spiritball )
  8271. 			sd->spiritball_old = require.spiritball = sd->spiritball;
  8272. 		else
  8273. 			sd->spiritball_old = require.spiritball;
  8274. 		break;
  8275. 	case MO_CHAINCOMBO:
  8276. 		if(!sc)
  8277. 			return 0;
  8278. 		if(sc->data[SC_BLADESTOP])
  8279. 			break;
  8280. 		if(sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == MO_TRIPLEATTACK)
  8281. 			break;
  8282. 		return 0;
  8283. 	case MO_COMBOFINISH:
  8284. 		if(!(sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == MO_CHAINCOMBO))
  8285. 			return 0;
  8286. 		break;
  8287. 	case CH_TIGERFIST:
  8288. 		if(!(sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == MO_COMBOFINISH))
  8289. 			return 0;
  8290. 		break;
  8291. 	case CH_CHAINCRUSH:
  8292. 		if(!(sc && sc->data[SC_COMBO]))
  8293. 			return 0;
  8294. 		if(sc->data[SC_COMBO]->val1 != MO_COMBOFINISH && sc->data[SC_COMBO]->val1 != CH_TIGERFIST)
  8295. 			return 0;
  8296. 		break;
  8297. 	case MO_EXTREMITYFIST:
  8298. //		if(sc && sc->data[SC_EXTREMITYFIST]) //To disable Asura during the 5 min skill block uncomment this...
  8299. //			return 0;
  8300. 		if( sc && sc->data[SC_BLADESTOP] )
  8301. 			break;
  8302. 		if( sc && sc->data[SC_COMBO] )
  8303. 		{
  8304. 			switch(sc->data[SC_COMBO]->val1) {
  8305. 				case MO_COMBOFINISH:
  8306. 				case CH_TIGERFIST:
  8307. 				case CH_CHAINCRUSH:
  8308. 					break;
  8309. 				default:
  8310. 					return 0;
  8311. 			}
  8312. 		}
  8313. 		else if( !unit_can_move(&sd->bl) )
  8314. 	  	{	//Placed here as ST_MOVE_ENABLE should not apply if rooted or on a combo. [Skotlex]
  8315. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8316. 			return 0;
  8317. 		}
  8318. 		break;
  8319.  
  8320. 	case TK_MISSION:
  8321. 		if( (sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON )
  8322. 		{// Cannot be used by Non-Taekwon classes
  8323. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8324. 			return 0;
  8325. 		}
  8326. 		break;
  8327.  
  8328. 	case TK_READYCOUNTER:
  8329. 	case TK_READYDOWN:
  8330. 	case TK_READYSTORM:
  8331. 	case TK_READYTURN:
  8332. 	case TK_JUMPKICK:
  8333. 		if( (sd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER )
  8334. 		{// Soul Linkers cannot use this skill
  8335. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8336. 			return 0;
  8337. 		}
  8338. 		break;
  8339.  
  8340. 	case TK_TURNKICK:
  8341. 	case TK_STORMKICK:
  8342. 	case TK_DOWNKICK:
  8343. 	case TK_COUNTER:
  8344. 		if ((sd->class_&MAPID_UPPERMASK) == MAPID_SOUL_LINKER)
  8345. 			return 0; //Anti-Soul Linker check in case you job-changed with Stances active.
  8346. 		if(!(sc && sc->data[SC_COMBO]))
  8347. 			return 0; //Combo needs to be ready
  8348.  
  8349. 		if (sc->data[SC_COMBO]->val3)
  8350. 		{	//Kick chain
  8351. 			//Do not repeat a kick.
  8352. 			if (sc->data[SC_COMBO]->val3 != skill)
  8353. 				break;
  8354. 			status_change_end(&sd->bl, SC_COMBO, INVALID_TIMER);
  8355. 			return 0;
  8356. 		}
  8357. 		if(sc->data[SC_COMBO]->val1 != skill)
  8358. 		{	//Cancel combo wait.
  8359. 			unit_cancel_combo(&sd->bl);
  8360. 			return 0;
  8361. 		}
  8362. 		break; //Combo ready.
  8363. 	case BD_ADAPTATION:
  8364. 		{
  8365. 			int time;
  8366. 			if(!(sc && sc->data[SC_DANCING]))
  8367. 			{
  8368. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8369. 				return 0;
  8370. 			}
  8371. 			time = 1000*(sc->data[SC_DANCING]->val3>>16);
  8372. 			if (skill_get_time(
  8373. 				(sc->data[SC_DANCING]->val1&0xFFFF), //Dance Skill ID
  8374. 				(sc->data[SC_DANCING]->val1>>16)) //Dance Skill LV
  8375. 				- time < skill_get_time2(skill,lv))
  8376. 			{
  8377. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8378. 				return 0;
  8379. 			}
  8380. 		}
  8381. 		break;
  8382.  
  8383. 	case PR_BENEDICTIO:
  8384. 		if (skill_check_pc_partner(sd, skill, &lv, 1, 0) < 2)
  8385. 		{
  8386. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8387. 			return 0;
  8388. 		}
  8389. 		break;
  8390.  
  8391. 	case SL_SMA:
  8392. 		if(!(sc && sc->data[SC_SMA]))
  8393. 			return 0;
  8394. 		break;
  8395.  
  8396. 	case HT_POWER:
  8397. 		if(!(sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == skill))
  8398. 			return 0;
  8399. 		break;
  8400.  
  8401. 	case CG_HERMODE:
  8402. 		if(!npc_check_areanpc(1,sd->bl.m,sd->bl.x,sd->bl.y,skill_get_splash(skill, lv)))
  8403. 		{
  8404. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8405. 			return 0;
  8406. 		}
  8407. 		break;
  8408. 	case CG_MOONLIT: //Check there's no wall in the range+1 area around the caster. [Skotlex]
  8409. 		{
  8410. 			int i,x,y,range = skill_get_splash(skill, lv)+1;
  8411. 			int size = range*2+1;
  8412. 			for (i=0;i<size*size;i++) {
  8413. 				x = sd->bl.x+(i%size-range);
  8414. 				y = sd->bl.y+(i/size-range);
  8415. 				if (map_getcell(sd->bl.m,x,y,CELL_CHKWALL)) {
  8416. 					clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8417. 					return 0;
  8418. 				}
  8419. 			}
  8420. 		}
  8421. 		break;
  8422. 	case PR_REDEMPTIO:
  8423. 		{
  8424. 			int exp;
  8425. 			if( ((exp = pc_nextbaseexp(sd)) > 0 && get_percentage(sd->status.base_exp, exp) < 1) ||
  8426. 				((exp = pc_nextjobexp(sd)) > 0 && get_percentage(sd->status.job_exp, exp) < 1)) {
  8427. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0); //Not enough exp.
  8428. 				return 0;
  8429. 			}
  8430. 			break;
  8431. 		}
  8432. 	case AM_TWILIGHT2:
  8433. 	case AM_TWILIGHT3:
  8434. 		if (!party_skill_check(sd, sd->status.party_id, skill, lv))
  8435. 		{
  8436. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8437. 			return 0;
  8438. 		}
  8439. 		break;
  8440. 	case SG_SUN_WARM:
  8441. 	case SG_MOON_WARM:
  8442. 	case SG_STAR_WARM:
  8443. 		if (sc && sc->data[SC_MIRACLE])
  8444. 			break;
  8445. 		i = skill-SG_SUN_WARM;
  8446. 		if (sd->bl.m == sd->feel_map[i].m)
  8447. 			break;
  8448. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8449. 		return 0;
  8450. 		break;
  8451. 	case SG_SUN_COMFORT:
  8452. 	case SG_MOON_COMFORT:
  8453. 	case SG_STAR_COMFORT:
  8454. 		if (sc && sc->data[SC_MIRACLE])
  8455. 			break;
  8456. 		i = skill-SG_SUN_COMFORT;
  8457. 		if (sd->bl.m == sd->feel_map[i].m &&
  8458. 			(battle_config.allow_skill_without_day || sg_info[i].day_func()))
  8459. 			break;
  8460. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8461. 		return 0;
  8462. 	case SG_FUSION:
  8463. 		if (sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_STAR)
  8464. 			break;
  8465. 		//Auron insists we should implement SP consumption when you are not Soul Linked. [Skotlex]
  8466. 		//Only invoke on skill begin cast (instant cast skill). [Kevin]
  8467. 		if( require.sp > 0 )
  8468. 		{
  8469. 			if (status->sp < (unsigned int)require.sp)
  8470. 				clif_skill_fail(sd,skill,USESKILL_FAIL_SP_INSUFFICIENT,0);
  8471. 			else
  8472. 				status_zap(&sd->bl, 0, require.sp);
  8473. 		}
  8474. 		return 0;
  8475. 	case GD_BATTLEORDER:
  8476. 	case GD_REGENERATION:
  8477. 	case GD_RESTORE:
  8478. 		if (!map_flag_gvg2(sd->bl.m)) {
  8479. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8480. 			return 0;
  8481. 		}
  8482. 	case GD_EMERGENCYCALL:
  8483. 		// other checks were already done in skillnotok()
  8484. 		if (!sd->status.guild_id || !sd->state.gmaster_flag)
  8485. 			return 0;
  8486. 		break;
  8487.  
  8488. 	case GS_GLITTERING:
  8489. 		if(sd->spiritball >= 10) {
  8490. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8491. 			return 0;
  8492. 		}
  8493. 		break;
  8494.  
  8495. 	case NJ_ISSEN:
  8496. 		if (status->hp < 2) {
  8497. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8498. 			return 0;
  8499. 		}
  8500. 	case NJ_BUNSINJYUTSU:
  8501. 		if (!(sc && sc->data[SC_NEN])) {
  8502. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8503. 			return 0;
  8504. 	  	}
  8505. 		break;
  8506.  
  8507. 	case NJ_ZENYNAGE:
  8508. 		if(sd->status.zeny < require.zeny) {
  8509. 			clif_skill_fail(sd,skill,USESKILL_FAIL_MONEY,0);
  8510. 			return 0;
  8511. 		}
  8512. 		break;
  8513. 	case PF_HPCONVERSION:
  8514. 		if (status->sp == status->max_sp)
  8515. 			return 0; //Unusable when at full SP.
  8516. 		break;
  8517. 	case AM_CALLHOMUN: //Can't summon if a hom is already out
  8518. 		if (sd->status.hom_id && sd->hd && !sd->hd->homunculus.vaporize) {
  8519. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8520. 			return 0;
  8521. 		}
  8522. 		break;
  8523. 	case AM_REST: //Can't vapo homun if you don't have an active homunc or it's hp is < 80%
  8524. 		if (!merc_is_hom_active(sd->hd) || sd->hd->battle_status.hp < (sd->hd->battle_status.max_hp*80/100))
  8525. 		{
  8526. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8527. 			return 0;
  8528. 		}
  8529. 		break;
  8530. 	}
  8531.  
  8532. 	switch(require.state) {
  8533. 	case ST_HIDING:
  8534. 		if(!(sc && sc->option&OPTION_HIDE)) {
  8535. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8536. 			return 0;
  8537. 		}
  8538. 		break;
  8539. 	case ST_CLOAKING:
  8540. 		if(!pc_iscloaking(sd)) {
  8541. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8542. 			return 0;
  8543. 		}
  8544. 		break;
  8545. 	case ST_HIDDEN:
  8546. 		if(!pc_ishiding(sd)) {
  8547. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8548. 			return 0;
  8549. 		}
  8550. 		break;
  8551. 	case ST_RIDING:
  8552. 		if(!pc_isriding(sd)) {
  8553. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8554. 			return 0;
  8555. 		}
  8556. 		break;
  8557. 	case ST_FALCON:
  8558. 		if(!pc_isfalcon(sd)) {
  8559. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8560. 			return 0;
  8561. 		}
  8562. 		break;
  8563. 	case ST_CARTBOOST:
  8564. 		if(!(sc && sc->data[SC_CARTBOOST])) {
  8565. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8566. 			return 0;
  8567. 		}
  8568. 	case ST_CART:
  8569. 		if(!pc_iscarton(sd)) {
  8570. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8571. 			return 0;
  8572. 		}
  8573. 		break;
  8574. 	case ST_SHIELD:
  8575. 		if(sd->status.shield <= 0) {
  8576. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8577. 			return 0;
  8578. 		}
  8579. 		break;
  8580. 	case ST_SIGHT:
  8581. 		if(!(sc && sc->data[SC_SIGHT])) {
  8582. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8583. 			return 0;
  8584. 		}
  8585. 		break;
  8586. 	case ST_EXPLOSIONSPIRITS:
  8587. 		if(!(sc && sc->data[SC_EXPLOSIONSPIRITS])) {
  8588. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8589. 			return 0;
  8590. 		}
  8591. 		break;
  8592. 	case ST_RECOV_WEIGHT_RATE:
  8593. 		if(battle_config.natural_heal_weight_rate <= 100 && sd->weight*100/sd->max_weight >= (unsigned int)battle_config.natural_heal_weight_rate) {
  8594. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8595. 			return 0;
  8596. 		}
  8597. 		break;
  8598. 	case ST_MOVE_ENABLE:
  8599. 		if (sc && sc->data[SC_COMBO] && sc->data[SC_COMBO]->val1 == skill)
  8600. 			sd->ud.canmove_tick = gettick(); //When using a combo, cancel the can't move delay to enable the skill. [Skotlex]
  8601.  
  8602. 		if (!unit_can_move(&sd->bl)) {
  8603. 			clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8604. 			return 0;
  8605. 		}
  8606. 		break;
  8607. 	case ST_WATER:
  8608. 		if (sc && (sc->data[SC_DELUGE] || sc->data[SC_SUITON]))
  8609. 			break;
  8610. 		if (map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKWATER))
  8611. 			break;
  8612. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8613. 		return 0;
  8614. 	}
  8615.  
  8616. 	if(require.mhp > 0 && get_percentage(status->hp, status->max_hp) > require.mhp) {
  8617. 		//mhp is the max-hp-requirement, that is,
  8618. 		//you must have this % or less of HP to cast it.
  8619. 		clif_skill_fail(sd,skill,USESKILL_FAIL_HP_INSUFFICIENT,0);
  8620. 		return 0;
  8621. 	}
  8622.  
  8623. 	if( require.weapon && !pc_check_weapontype(sd,require.weapon) ) {
  8624. 		clif_skill_fail(sd,skill,USESKILL_FAIL_THIS_WEAPON,0);
  8625. 		return 0;
  8626. 	}
  8627.  
  8628. 	if( require.sp > 0 && status->sp < (unsigned int)require.sp) {
  8629. 		clif_skill_fail(sd,skill,USESKILL_FAIL_SP_INSUFFICIENT,0);
  8630. 		return 0;
  8631. 	}
  8632.  
  8633. 	if( require.zeny > 0 && sd->status.zeny < require.zeny ) {
  8634. 		clif_skill_fail(sd,skill,USESKILL_FAIL_MONEY,0);
  8635. 		return 0;
  8636. 	}
  8637.  
  8638. 	if( require.spiritball > 0 && sd->spiritball < require.spiritball) {
  8639. 		clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8640. 		return 0;
  8641. 	}
  8642.  
  8643. 	return 1;
  8644. }
  8645.  
  8646. int skill_check_condition_castend(struct map_session_data* sd, short skill, short lv)
  8647. {
  8648. 	struct skill_condition require;
  8649. 	struct status_data *status;
  8650. 	int i;
  8651. 	int index[MAX_SKILL_ITEM_REQUIRE];
  8652.  
  8653. 	nullpo_ret(sd);
  8654.  
  8655. 	if( lv <= 0 || sd->chatID )
  8656. 		return 0;
  8657.  
  8658. 	if( battle_config.gm_skilluncond && pc_isGM(sd) >= battle_config.gm_skilluncond && sd->skillitem != skill )
  8659. 	{	//GMs don't override the skillItem check, otherwise they can use items without them being consumed! [Skotlex]	
  8660. 		sd->state.arrow_atk = skill_get_ammotype(skill)?1:0; //Need to do arrow state check.
  8661. 		sd->spiritball_old = sd->spiritball; //Need to do Spiritball check.
  8662. 		return 1;
  8663. 	}
  8664.  
  8665. 	if( sd->menuskill_id == AM_PHARMACY )
  8666. 	{ // Cast start or cast end??
  8667. 		switch( skill )
  8668. 		{
  8669. 		case AM_PHARMACY:
  8670. 		case AC_MAKINGARROW:
  8671. 		case BS_REPAIRWEAPON:
  8672. 		case AM_TWILIGHT1:
  8673. 		case AM_TWILIGHT2:
  8674. 		case AM_TWILIGHT3:
  8675. 			return 0;
  8676. 		}
  8677. 	}
  8678.  
  8679. 	if( sd->skillitem == skill ) // Casting finished (Item skill or Hocus-Pocus)
  8680. 		return 1;
  8681.  
  8682. 	if( pc_is90overweight(sd) )
  8683. 	{
  8684. 		clif_skill_fail(sd,skill,USESKILL_FAIL_WEIGHTOVER,0);
  8685. 		return 0;
  8686. 	}
  8687.  
  8688. 	// perform skill-specific checks (and actions)
  8689. 	switch( skill )
  8690. 	{
  8691. 	case PR_BENEDICTIO:
  8692. 		skill_check_pc_partner(sd, skill, &lv, 1, 1);
  8693. 		break;
  8694. 	case AM_CANNIBALIZE:
  8695. 	case AM_SPHEREMINE:
  8696. 	{
  8697. 		int c=0;
  8698. 		int summons[5] = { 1589, 1579, 1575, 1555, 1590 };
  8699. 		//int summons[5] = { 1020, 1068, 1118, 1500, 1368 };
  8700. 		int maxcount = (skill==AM_CANNIBALIZE)? 6-lv : skill_get_maxcount(skill,lv);
  8701. 		int mob_class = (skill==AM_CANNIBALIZE)? summons[lv-1] :1142;
  8702. 		if(battle_config.land_skill_limit && maxcount>0 && (battle_config.land_skill_limit&BL_PC)) {
  8703. 			i = map_foreachinmap(skill_check_condition_mob_master_sub ,sd->bl.m, BL_MOB, sd->bl.id, mob_class, skill, &c);
  8704. 			if(c >= maxcount ||
  8705. 				(skill==AM_CANNIBALIZE && c != i && battle_config.summon_flora&2))
  8706. 			{	//Fails when: exceed max limit. There are other plant types already out.
  8707. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8708. 				return 0;
  8709. 			}
  8710. 		}
  8711. 		break;
  8712. 	}
  8713. 	}
  8714.  
  8715. 	status = &sd->battle_status;
  8716.  
  8717. 	require = skill_get_requirement(sd,skill,lv);
  8718.  
  8719. 	if( require.hp > 0 && status->hp <= (unsigned int)require.hp) {
  8720. 		clif_skill_fail(sd,skill,USESKILL_FAIL_HP_INSUFFICIENT,0);
  8721. 		return 0;
  8722. 	}
  8723.  
  8724. 	if( require.ammo ) { //Skill requires stuff equipped in the arrow slot.
  8725. 		if((i=sd->equip_index[EQI_AMMO]) < 0 ||
  8726. 			!sd->inventory_data[i] ||
  8727. 			sd->status.inventory[i].amount < require.ammo_qty
  8728. 		) {
  8729. 			clif_arrow_fail(sd,0);
  8730. 			return 0;
  8731. 		}
  8732. 		if (!(require.ammo&1<<sd->inventory_data[i]->look))
  8733. 		{	//Ammo type check. Send the "wrong weapon type" message
  8734. 			//which is the closest we have to wrong ammo type. [Skotlex]
  8735. 			clif_arrow_fail(sd,0); //Haplo suggested we just send the equip-arrows message instead. [Skotlex]
  8736. 			//clif_skill_fail(sd,skill,USESKILL_FAIL_THIS_WEAPON,0);
  8737. 			return 0;
  8738. 		}
  8739. 	}
  8740.  
  8741. 	for( i = 0; i < MAX_SKILL_ITEM_REQUIRE; ++i )
  8742. 	{
  8743. 		if( !require.itemid[i] )
  8744. 			continue;
  8745. 		index[i] = pc_search_inventory(sd,require.itemid[i]);
  8746. 		if( index[i] < 0 || sd->status.inventory[index[i]].amount < require.amount[i] ) {
  8747. 			if( require.itemid[i] == ITEMID_RED_GEMSTONE )
  8748. 				clif_skill_fail(sd,skill,USESKILL_FAIL_REDJAMSTONE,0);// red gemstone required
  8749. 			else if( require.itemid[i] == ITEMID_BLUE_GEMSTONE )
  8750. 				clif_skill_fail(sd,skill,USESKILL_FAIL_BLUEJAMSTONE,0);// blue gemstone required
  8751. 			else
  8752. 				clif_skill_fail(sd,skill,USESKILL_FAIL_LEVEL,0);
  8753. 			return 0;
  8754. 		}
  8755. 	}
  8756.  
  8757. 	return 1;
  8758. }
  8759.  
  8760. // type&2: consume items (after skill was used)
  8761. // type&1: consume the others (before skill was used)
  8762. int skill_consume_requirement( struct map_session_data *sd, short skill, short lv, short type)
  8763. {
  8764. 	int n,i;
  8765. 	struct skill_condition req;
  8766.  
  8767. 	nullpo_ret(sd);
  8768.  
  8769. 	req = skill_get_requirement(sd,skill,lv);
  8770.  
  8771. 	if( type&1 )
  8772. 	{
  8773. 		if( skill == CG_TAROTCARD || sd->state.autocast )
  8774. 			req.sp = 0; // TarotCard will consume sp in skill_cast_nodamage_id [Inkfish]
  8775. 		if(req.hp || req.sp)
  8776. 			status_zap(&sd->bl, req.hp, req.sp);
  8777.  
  8778. 		if(req.spiritball > 0)
  8779. 			pc_delspiritball(sd,req.spiritball,0);
  8780.  
  8781. 		if(req.zeny > 0)
  8782. 		{
  8783. 			if( skill == NJ_ZENYNAGE )
  8784. 				req.zeny = 0; //Zeny is reduced on skill_attack.
  8785. 			if( sd->status.zeny < req.zeny )
  8786. 				req.zeny = sd->status.zeny;
  8787. 			pc_payzeny(sd,req.zeny);
  8788. 		}
  8789. 	}
  8790.  
  8791. 	if( type&2 )
  8792. 	{
  8793. 		struct status_change *sc = &sd->sc;
  8794.  
  8795. 		if( !sc->count )
  8796. 			sc = NULL;
  8797.  
  8798. 		for( i = 0; i < MAX_SKILL_ITEM_REQUIRE; ++i )
  8799. 		{
  8800. 			if( !req.itemid[i] )
  8801. 				continue;
  8802.  
  8803. 			if( itemid_isgemstone(req.itemid[i]) && skill != HW_GANBANTEIN && sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_WIZARD )
  8804. 				continue; //Gemstones are checked, but not substracted from inventory.
  8805.  
  8806. 			if( (n = pc_search_inventory(sd,req.itemid[i])) >= 0 )
  8807. 				pc_delitem(sd,n,req.amount[i],0,1);
  8808. 		}
  8809. 	}
  8810.  
  8811. 	return 1;
  8812. }
  8813.  
  8814. struct skill_condition skill_get_requirement(struct map_session_data* sd, short skill, short lv)
  8815. {
  8816. 	struct skill_condition req;
  8817. 	struct status_data *status;
  8818. 	struct status_change *sc;
  8819. 	int i,j,hp_rate,sp_rate;
  8820.  
  8821. 	memset(&req,0,sizeof(req));
  8822.  
  8823. 	if( !sd )
  8824. 		return req;
  8825.  
  8826. 	if( sd->skillitem == skill )
  8827. 		return req; // Item skills and Hocus-Pocus don't have requirements.[Inkfish]
  8828.  
  8829. 	sc = &sd->sc;
  8830. 	if( !sc->count )
  8831. 		sc = NULL;
  8832.  
  8833. 	switch( skill )
  8834. 	{ // Turn off check.
  8835. 	case BS_MAXIMIZE:		case NV_TRICKDEAD:	case TF_HIDING:			case AS_CLOAKING:		case CR_AUTOGUARD:
  8836. 	case ML_AUTOGUARD:		case CR_DEFENDER:	case ML_DEFENDER:		case ST_CHASEWALK:		case PA_GOSPEL:
  8837. 	case CR_SHRINK:			case TK_RUN:		case GS_GATLINGFEVER:	case TK_READYCOUNTER:	case TK_READYDOWN:
  8838. 	case TK_READYSTORM:		case TK_READYTURN:	case SG_FUSION:
  8839. 		if( sc && sc->data[status_skill2sc(skill)] )
  8840. 			return req;
  8841. 	}
  8842.  
  8843. 	j = skill_get_index(skill);
  8844. 	if( j == 0 ) // invalid skill id
  8845.   		return req;
  8846. 	if( lv < 1 || lv > MAX_SKILL_LEVEL )
  8847. 		return req;
  8848.  
  8849. 	status = &sd->battle_status;
  8850.  
  8851. 	req.hp = skill_db[j].hp[lv-1];
  8852. 	hp_rate = skill_db[j].hp_rate[lv-1];
  8853. 	if(hp_rate > 0)
  8854. 		req.hp += (status->hp * hp_rate)/100;
  8855. 	else
  8856. 		req.hp += (status->max_hp * (-hp_rate))/100;
  8857.  
  8858. 	req.sp = skill_db[j].sp[lv-1];
  8859. 	if((sd->skillid_old == BD_ENCORE) && skill == sd->skillid_dance)
  8860. 		req.sp /= 2;
  8861. 	sp_rate = skill_db[j].sp_rate[lv-1];
  8862. 	if(sp_rate > 0)
  8863. 		req.sp += (status->sp * sp_rate)/100;
  8864. 	else
  8865. 		req.sp += (status->max_sp * (-sp_rate))/100;
  8866. 	if( sd->dsprate!=100 )
  8867. 		req.sp = req.sp * sd->dsprate / 100;
  8868.  
  8869. 	req.zeny = skill_db[j].zeny[lv-1];
  8870.  
  8871. 	req.spiritball = skill_db[j].spiritball[lv-1];
  8872.  
  8873. 	req.state = skill_db[j].state;
  8874.  
  8875. 	req.mhp = skill_db[j].mhp[lv-1];
  8876.  
  8877. 	req.weapon = skill_db[j].weapon;
  8878.  
  8879. 	req.ammo_qty = skill_db[j].ammo_qty[lv-1];
  8880. 	if (req.ammo_qty)
  8881. 		req.ammo = skill_db[j].ammo;
  8882.  
  8883. 	if (!req.ammo && skill && skill_isammotype(sd, skill))
  8884. 	{	//Assume this skill is using the weapon, therefore it requires arrows.
  8885. 		req.ammo = 0xFFFFFFFF; //Enable use on all ammo types.
  8886. 		req.ammo_qty = 1;
  8887. 	}
  8888.  
  8889. 	for( i = 0; i < MAX_SKILL_ITEM_REQUIRE; i++ )
  8890. 	{
  8891. 		if( (skill == AM_POTIONPITCHER || skill == CR_SLIMPITCHER || skill == CR_CULTIVATION) && i != lv%11 - 1 )
  8892. 			continue;
  8893.  
  8894. 		switch( skill )
  8895. 		{
  8896. 		case AM_CALLHOMUN:
  8897. 			if (sd->status.hom_id) //Don't delete items when hom is already out.
  8898. 				continue;
  8899. 			break;
  8900. 		case WZ_FIREPILLAR: // celest
  8901. 			if (lv <= 5)	// no gems required at level 1-5
  8902. 				continue;
  8903. 			break;
  8904. 		}
  8905.  
  8906. 		req.itemid[i] = skill_db[j].itemid[i];
  8907. 		req.amount[i] = skill_db[j].amount[i];
  8908.  
  8909. 		if( itemid_isgemstone(req.itemid[i]) && skill != HW_GANBANTEIN )
  8910. 		{
  8911. 			if( sd->special_state.no_gemstone )
  8912. 			{	//Make it substract 1 gem rather than skipping the cost.
  8913. 				if( --req.amount[i] < 1 )
  8914. 					req.itemid[i] = 0;
  8915. 			}
  8916. 			if(sc && sc->data[SC_INTOABYSS])
  8917. 			{
  8918. 				if( skill != SA_ABRACADABRA )
  8919. 					req.itemid[i] = req.amount[i] = 0;
  8920. 				else if( --req.amount[i] < 1 )
  8921. 					req.amount[i] = 1; // Hocus Pocus allways use at least 1 gem
  8922. 			}
  8923. 		}
  8924. 	}
  8925.  
  8926. 	// Check for cost reductions due to skills & SCs
  8927. 	switch(skill) {
  8928. 		case MC_MAMMONITE:
  8929. 			if(pc_checkskill(sd,BS_UNFAIRLYTRICK)>0)
  8930. 				req.zeny -= req.zeny*10/100;
  8931. 			break;
  8932. 		case AL_HOLYLIGHT:
  8933. 			if(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_PRIEST)
  8934. 				req.sp *= 5;
  8935. 			break;
  8936. 		case SL_SMA:
  8937. 		case SL_STUN:
  8938. 		case SL_STIN:
  8939. 		{
  8940. 			int kaina_lv = pc_checkskill(sd,SL_KAINA);
  8941.  
  8942. 			if(kaina_lv==0 || sd->status.base_level<70)
  8943. 				break;
  8944. 			if(sd->status.base_level>=90)
  8945. 				req.sp -= req.sp*7*kaina_lv/100;
  8946. 			else if(sd->status.base_level>=80)
  8947. 				req.sp -= req.sp*5*kaina_lv/100;
  8948. 			else if(sd->status.base_level>=70)
  8949. 				req.sp -= req.sp*3*kaina_lv/100;
  8950. 		}
  8951. 			break;
  8952. 		case MO_TRIPLEATTACK:
  8953. 		case MO_CHAINCOMBO:
  8954. 		case MO_COMBOFINISH:
  8955. 		case CH_TIGERFIST:
  8956. 		case CH_CHAINCRUSH:
  8957. 			if(sc && sc->data[SC_SPIRIT] && sc->data[SC_SPIRIT]->val2 == SL_MONK)
  8958. 				req.sp -= req.sp*25/100; //FIXME: Need real data. this is a custom value.
  8959. 			break;
  8960. 		case MO_BODYRELOCATION:
  8961. 			if( sc && sc->data[SC_EXPLOSIONSPIRITS] )
  8962. 				req.spiritball = 0;
  8963. 			break;
  8964. 		case MO_EXTREMITYFIST:
  8965. 			if( sc )
  8966. 			{
  8967. 				if( sc->data[SC_BLADESTOP] )
  8968. 					req.spiritball--;
  8969. 				else if( sc->data[SC_COMBO] )
  8970. 				{
  8971. 					switch( sc->data[SC_COMBO]->val1 )
  8972. 					{
  8973. 						case MO_COMBOFINISH:
  8974. 							req.spiritball = 4;
  8975. 							break;
  8976. 						case CH_TIGERFIST:
  8977. 							req.spiritball = 3;
  8978. 							break;
  8979. 						case CH_CHAINCRUSH:	//It should consume whatever is left as long as it's at least 1.
  8980. 							req.spiritball = sd->spiritball?sd->spiritball:1;
  8981. 							break;
  8982. 					}
  8983. 				}
  8984. 			}
  8985. 			break;
  8986. 	}
  8987.  
  8988. 	return req;
  8989. }
  8990.  
  8991. /*==========================================
  8992.  * Does cast-time reductions based on dex, item bonuses and config setting
  8993.  *------------------------------------------*/
  8994. int skill_castfix (struct block_list *bl, int skill_id, int skill_lv)
  8995. {
  8996. 	int time = skill_get_cast(skill_id, skill_lv);
  8997. 	struct map_session_data *sd;
  8998.  
  8999. 	nullpo_ret(bl);
  9000. 	sd = BL_CAST(BL_PC, bl);
  9001.  
  9002. 	// calculate base cast time (reduced by dex)
  9003. 	if( !(skill_get_castnodex(skill_id, skill_lv)&1) )
  9004. 	{
  9005. 		int scale = battle_config.castrate_dex_scale - status_get_dex(bl);
  9006. 		if( scale > 0 )	// not instant cast
  9007. 			time = time * scale / battle_config.castrate_dex_scale;
  9008. 		else return 0;	// instant cast
  9009. 	}
  9010.  
  9011. 	// calculate cast time reduced by item/card bonuses
  9012. 	if( !(skill_get_castnodex(skill_id, skill_lv)&4) && sd )
  9013. 	{
  9014. 		int i;
  9015. 		if( sd->castrate != 100 )
  9016. 			time = time * sd->castrate / 100;
  9017. 		for( i = 0; i < ARRAYLENGTH(sd->skillcast) && sd->skillcast[i].id; i++ )
  9018. 		{
  9019. 			if( sd->skillcast[i].id == skill_id )
  9020. 			{
  9021. 				time+= time * sd->skillcast[i].val / 100;
  9022. 				break;
  9023. 			}
  9024. 		}
  9025. 	}
  9026.  
  9027. 	// config cast time multiplier
  9028. 	if (battle_config.cast_rate != 100)
  9029. 		time = time * battle_config.cast_rate / 100;
  9030.  
  9031. 	// return final cast time
  9032. 	return (time > 0) ? time : 0;
  9033. }
  9034.  
  9035. /*==========================================
  9036.  * Does cast-time reductions based on sc data.
  9037.  *------------------------------------------*/
  9038. int skill_castfix_sc (struct block_list *bl, int time)
  9039. {
  9040. 	struct status_change *sc = status_get_sc(bl);
  9041.  
  9042. 	if (sc && sc->count) {
  9043. 		if (sc->data[SC_SLOWCAST])
  9044. 			time += time * sc->data[SC_SLOWCAST]->val2 / 100;
  9045. 		if (sc->data[SC_SUFFRAGIUM]) {
  9046. 			time -= time * sc->data[SC_SUFFRAGIUM]->val2 / 100;
  9047. 			status_change_end(bl, SC_SUFFRAGIUM, INVALID_TIMER);
  9048. 		}
  9049. 		if (sc->data[SC_MEMORIZE]) {
  9050. 			time>>=1;
  9051. 			if ((--sc->data[SC_MEMORIZE]->val2) <= 0)
  9052. 				status_change_end(bl, SC_MEMORIZE, INVALID_TIMER);
  9053. 		}
  9054. 		if (sc->data[SC_POEMBRAGI])
  9055. 			time -= time * sc->data[SC_POEMBRAGI]->val2 / 100;
  9056. 	}
  9057. 	return (time > 0) ? time : 0;
  9058. }
  9059.  
  9060. /*==========================================
  9061.  * Does delay reductions based on dex/agi, sc data, item bonuses, ...
  9062.  *------------------------------------------*/
  9063. int skill_delayfix (struct block_list *bl, int skill_id, int skill_lv)
  9064. {
  9065. 	int delaynodex = skill_get_delaynodex(skill_id, skill_lv);
  9066. 	int time = skill_get_delay(skill_id, skill_lv);
  9067. 	struct map_session_data *sd;
  9068. 	struct status_change *sc = status_get_sc(bl);
  9069.  
  9070. 	nullpo_ret(bl);
  9071. 	sd = BL_CAST(BL_PC, bl);
  9072.  
  9073. 	if (skill_id == SA_ABRACADABRA)
  9074. 		return 0; //Will use picked skill's delay.
  9075.  
  9076. 	if (bl->type&battle_config.no_skill_delay)
  9077. 		return battle_config.min_skill_delay_limit;
  9078.  
  9079. 	if (time < 0)
  9080. 		time = -time + status_get_amotion(bl);	// If set to <0, add to attack motion.
  9081.  
  9082. 	// Delay reductions
  9083. 	switch (skill_id)
  9084.   	{	//Monk combo skills have their delay reduced by agi/dex.
  9085. 	case MO_TRIPLEATTACK:
  9086. 	case MO_CHAINCOMBO:
  9087. 	case MO_COMBOFINISH:
  9088. 	case CH_TIGERFIST:
  9089. 	case CH_CHAINCRUSH:
  9090. 		time -= 4*status_get_agi(bl) - 2*status_get_dex(bl);
  9091. 		break;
  9092. 	case HP_BASILICA:
  9093. 		if( sc && !sc->data[SC_BASILICA] )
  9094. 			time = 0; // There is no Delay on Basilica creation, only on cancel
  9095. 		break;
  9096. 	default:
  9097. 		if (battle_config.delay_dependon_dex && !(delaynodex&1))
  9098. 		{	// if skill delay is allowed to be reduced by dex
  9099. 			int scale = battle_config.castrate_dex_scale - status_get_dex(bl);
  9100. 			if (scale > 0)
  9101. 				time = time * scale / battle_config.castrate_dex_scale;
  9102. 			else //To be capped later to minimum.
  9103. 				time = 0;
  9104. 		}
  9105. 		if (battle_config.delay_dependon_agi && !(delaynodex&1))
  9106. 		{	// if skill delay is allowed to be reduced by agi
  9107. 			int scale = battle_config.castrate_dex_scale - status_get_agi(bl);
  9108. 			if (scale > 0)
  9109. 				time = time * scale / battle_config.castrate_dex_scale;
  9110. 			else //To be capped later to minimum.
  9111. 				time = 0;
  9112. 		}
  9113. 	}
  9114.  
  9115. 	if ( sc && sc->data[SC_SPIRIT] )
  9116. 	{
  9117. 		switch (skill_id) {
  9118. 			case CR_SHIELDBOOMERANG:
  9119. 				if (sc->data[SC_SPIRIT]->val2 == SL_CRUSADER)
  9120. 					time /= 2;
  9121. 				break;
  9122. 			case AS_SONICBLOW:
  9123. 				if (!map_flag_gvg(bl->m) && !map[bl->m].flag.battleground && sc->data[SC_SPIRIT]->val2 == SL_ASSASIN)
  9124. 					time /= 2;
  9125. 				break;
  9126. 		}
  9127. 	}
  9128.  
  9129. 	if (!(delaynodex&2))
  9130. 	{
  9131. 		if (sc && sc->count) {
  9132. 			if (sc->data[SC_POEMBRAGI])
  9133. 				time -= time * sc->data[SC_POEMBRAGI]->val3 / 100;
  9134. 		}
  9135. 	}
  9136.  
  9137. 	if( !(delaynodex&4) && sd && sd->delayrate != 100 )
  9138. 		time = time * sd->delayrate / 100;
  9139.  
  9140. 	if (battle_config.delay_rate != 100)
  9141. 		time = time * battle_config.delay_rate / 100;
  9142.  
  9143. 	if (time < status_get_amotion(bl))
  9144. 		time = status_get_amotion(bl); // Delay can never be below amotion [Playtester]
  9145.  
  9146. 	return max(time, battle_config.min_skill_delay_limit);
  9147. }
  9148.  
  9149. /*=========================================
  9150.  *
  9151.  *-----------------------------------------*/
  9152. struct square {
  9153. 	int val1[5];
  9154. 	int val2[5];
  9155. };
  9156.  
  9157. static void skill_brandishspear_first (struct square *tc, int dir, int x, int y)
  9158. {
  9159. 	nullpo_retv(tc);
  9160.  
  9161. 	if(dir == 0){
  9162. 		tc->val1[0]=x-2;
  9163. 		tc->val1[1]=x-1;
  9164. 		tc->val1[2]=x;
  9165. 		tc->val1[3]=x+1;
  9166. 		tc->val1[4]=x+2;
  9167. 		tc->val2[0]=
  9168. 		tc->val2[1]=
  9169. 		tc->val2[2]=
  9170. 		tc->val2[3]=
  9171. 		tc->val2[4]=y-1;
  9172. 	}
  9173. 	else if(dir==2){
  9174. 		tc->val1[0]=
  9175. 		tc->val1[1]=
  9176. 		tc->val1[2]=
  9177. 		tc->val1[3]=
  9178. 		tc->val1[4]=x+1;
  9179. 		tc->val2[0]=y+2;
  9180. 		tc->val2[1]=y+1;
  9181. 		tc->val2[2]=y;
  9182. 		tc->val2[3]=y-1;
  9183. 		tc->val2[4]=y-2;
  9184. 	}
  9185. 	else if(dir==4){
  9186. 		tc->val1[0]=x-2;
  9187. 		tc->val1[1]=x-1;
  9188. 		tc->val1[2]=x;
  9189. 		tc->val1[3]=x+1;
  9190. 		tc->val1[4]=x+2;
  9191. 		tc->val2[0]=
  9192. 		tc->val2[1]=
  9193. 		tc->val2[2]=
  9194. 		tc->val2[3]=
  9195. 		tc->val2[4]=y+1;
  9196. 	}
  9197. 	else if(dir==6){
  9198. 		tc->val1[0]=
  9199. 		tc->val1[1]=
  9200. 		tc->val1[2]=
  9201. 		tc->val1[3]=
  9202. 		tc->val1[4]=x-1;
  9203. 		tc->val2[0]=y+2;
  9204. 		tc->val2[1]=y+1;
  9205. 		tc->val2[2]=y;
  9206. 		tc->val2[3]=y-1;
  9207. 		tc->val2[4]=y-2;
  9208. 	}
  9209. 	else if(dir==1){
  9210. 		tc->val1[0]=x-1;
  9211. 		tc->val1[1]=x;
  9212. 		tc->val1[2]=x+1;
  9213. 		tc->val1[3]=x+2;
  9214. 		tc->val1[4]=x+3;
  9215. 		tc->val2[0]=y-4;
  9216. 		tc->val2[1]=y-3;
  9217. 		tc->val2[2]=y-1;
  9218. 		tc->val2[3]=y;
  9219. 		tc->val2[4]=y+1;
  9220. 	}
  9221. 	else if(dir==3){
  9222. 		tc->val1[0]=x+3;
  9223. 		tc->val1[1]=x+2;
  9224. 		tc->val1[2]=x+1;
  9225. 		tc->val1[3]=x;
  9226. 		tc->val1[4]=x-1;
  9227. 		tc->val2[0]=y-1;
  9228. 		tc->val2[1]=y;
  9229. 		tc->val2[2]=y+1;
  9230. 		tc->val2[3]=y+2;
  9231. 		tc->val2[4]=y+3;
  9232. 	}
  9233. 	else if(dir==5){
  9234. 		tc->val1[0]=x+1;
  9235. 		tc->val1[1]=x;
  9236. 		tc->val1[2]=x-1;
  9237. 		tc->val1[3]=x-2;
  9238. 		tc->val1[4]=x-3;
  9239. 		tc->val2[0]=y+3;
  9240. 		tc->val2[1]=y+2;
  9241. 		tc->val2[2]=y+1;
  9242. 		tc->val2[3]=y;
  9243. 		tc->val2[4]=y-1;
  9244. 	}
  9245. 	else if(dir==7){
  9246. 		tc->val1[0]=x-3;
  9247. 		tc->val1[1]=x-2;
  9248. 		tc->val1[2]=x-1;
  9249. 		tc->val1[3]=x;
  9250. 		tc->val1[4]=x+1;
  9251. 		tc->val2[1]=y;
  9252. 		tc->val2[0]=y+1;
  9253. 		tc->val2[2]=y-1;
  9254. 		tc->val2[3]=y-2;
  9255. 		tc->val2[4]=y-3;
  9256. 	}
  9257.  
  9258. }
  9259.  
  9260. static void skill_brandishspear_dir (struct square* tc, int dir, int are)
  9261. {
  9262. 	int c;
  9263. 	nullpo_retv(tc);
  9264.  
  9265. 	for( c = 0; c < 5; c++ )
  9266. 	{
  9267. 		switch( dir )
  9268. 		{
  9269. 			case 0:                   tc->val2[c]+=are; break;
  9270. 			case 1: tc->val1[c]-=are; tc->val2[c]+=are; break;
  9271. 			case 2: tc->val1[c]-=are;                   break;
  9272. 			case 3: tc->val1[c]-=are; tc->val2[c]-=are; break;
  9273. 			case 4:                   tc->val2[c]-=are; break;
  9274. 			case 5: tc->val1[c]+=are; tc->val2[c]-=are; break;
  9275. 			case 6: tc->val1[c]+=are;                   break;
  9276. 			case 7: tc->val1[c]+=are; tc->val2[c]+=are; break;
  9277. 		}
  9278. 	}
  9279. }
  9280.  
  9281. void skill_brandishspear(struct block_list* src, struct block_list* bl, int skillid, int skilllv, unsigned int tick, int flag)
  9282. {
  9283. 	int c,n=4;
  9284. 	int dir = map_calc_dir(src,bl->x,bl->y);
  9285. 	struct square tc;
  9286. 	int x=bl->x,y=bl->y;
  9287. 	skill_brandishspear_first(&tc,dir,x,y);
  9288. 	skill_brandishspear_dir(&tc,dir,4);
  9289. 	skill_area_temp[1] = bl->id;
  9290.  
  9291. 	if(skilllv > 9){
  9292. 		for(c=1;c<4;c++){
  9293. 			map_foreachincell(skill_area_sub,
  9294. 				bl->m,tc.val1[c],tc.val2[c],BL_CHAR,
  9295. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|n,
  9296. 				skill_castend_damage_id);
  9297. 		}
  9298. 	}
  9299. 	if(skilllv > 6){
  9300. 		skill_brandishspear_dir(&tc,dir,-1);
  9301. 		n--;
  9302. 	}else{
  9303. 		skill_brandishspear_dir(&tc,dir,-2);
  9304. 		n-=2;
  9305. 	}
  9306.  
  9307. 	if(skilllv > 3){
  9308. 		for(c=0;c<5;c++){
  9309. 			map_foreachincell(skill_area_sub,
  9310. 				bl->m,tc.val1[c],tc.val2[c],BL_CHAR,
  9311. 				src,skillid,skilllv,tick, flag|BCT_ENEMY|n,
  9312. 				skill_castend_damage_id);
  9313. 			if(skilllv > 6 && n==3 && c==4){
  9314. 				skill_brandishspear_dir(&tc,dir,-1);
  9315. 				n--;c=-1;
  9316. 			}
  9317. 		}
  9318. 	}
  9319. 	for(c=0;c<10;c++){
  9320. 		if(c==0||c==5) skill_brandishspear_dir(&tc,dir,-1);
  9321. 		map_foreachincell(skill_area_sub,
  9322. 			bl->m,tc.val1[c%5],tc.val2[c%5],BL_CHAR,
  9323. 			src,skillid,skilllv,tick, flag|BCT_ENEMY|1,
  9324. 			skill_castend_damage_id);
  9325. 	}
  9326. }
  9327.  
  9328. /*==========================================
  9329.  * Weapon Repair [Celest/DracoRPG]
  9330.  *------------------------------------------*/
  9331. void skill_repairweapon (struct map_session_data *sd, int idx)
  9332. {
  9333. 	int material;
  9334. 	int materials[4] = { 1002, 998, 999, 756 };
  9335. 	struct item *item;
  9336. 	struct map_session_data *target_sd;
  9337.  
  9338. 	nullpo_retv(sd);
  9339. 	target_sd = map_id2sd(sd->menuskill_val);
  9340. 	if (!target_sd) //Failed....
  9341. 		return;
  9342. 	if(idx==0xFFFF) // No item selected ('Cancel' clicked)
  9343. 		return;
  9344. 	if(idx < 0 || idx >= MAX_INVENTORY)
  9345. 		return; //Invalid index??
  9346.  
  9347.    item = &target_sd->status.inventory[idx];
  9348. 	if(item->nameid <= 0 || item->attribute == 0)
  9349. 		return; //Again invalid item....
  9350.  
  9351. 	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)))){
  9352. 		clif_item_repaireffect(sd,idx,1);
  9353. 		return;
  9354. 	}
  9355.  
  9356. 	if (itemdb_type(item->nameid)==IT_WEAPON)
  9357. 		material = materials [itemdb_wlv(item->nameid)-1]; // Lv1/2/3/4 weapons consume 1 Iron Ore/Iron/Steel/Rough Oridecon
  9358. 	else
  9359. 		material = materials [2]; // Armors consume 1 Steel
  9360. 	if (pc_search_inventory(sd,material) < 0 ) {
  9361. 		clif_skill_fail(sd,sd->menuskill_id,USESKILL_FAIL_LEVEL,0);
  9362. 		return;
  9363. 	}
  9364. 	clif_skill_nodamage(&sd->bl,&target_sd->bl,sd->menuskill_id,1,1);
  9365. 	item->attribute=0;
  9366. 	clif_equiplist(target_sd);
  9367. 	pc_delitem(sd,pc_search_inventory(sd,material),1,0,0);
  9368. 	clif_item_repaireffect(sd,idx,0);
  9369. 	if(sd!=target_sd)
  9370. 		clif_item_repaireffect(target_sd,idx,0);
  9371. }
  9372.  
  9373. /*==========================================
  9374.  * Item Appraisal
  9375.  *------------------------------------------*/
  9376. void skill_identify (struct map_session_data *sd, int idx)
  9377. {
  9378. 	int flag=1;
  9379.  
  9380. 	nullpo_retv(sd);
  9381.  
  9382. 	if(idx >= 0 && idx < MAX_INVENTORY) {
  9383. 		if(sd->status.inventory[idx].nameid > 0 && sd->status.inventory[idx].identify == 0 ){
  9384. 			flag=0;
  9385. 			sd->status.inventory[idx].identify=1;
  9386. 		}
  9387. 	}
  9388. 	clif_item_identified(sd,idx,flag);
  9389. }
  9390.  
  9391. /*==========================================
  9392.  * Weapon Refine [Celest]
  9393.  *------------------------------------------*/
  9394. void skill_weaponrefine (struct map_session_data *sd, int idx)
  9395. {
  9396. 	int i = 0, ep = 0, per;
  9397. 	int material[5] = { 0, 1010, 1011, 984, 984 };
  9398. 	struct item *item;
  9399.  
  9400. 	nullpo_retv(sd);
  9401.  
  9402. 	if (idx >= 0 && idx < MAX_INVENTORY)
  9403. 	{
  9404. 		struct item_data *ditem = sd->inventory_data[idx];
  9405. 		item = &sd->status.inventory[idx];
  9406.  
  9407. 		if(item->nameid > 0 && ditem->type == IT_WEAPON)
  9408. 		{
  9409. 			if( item->refine >= sd->menuskill_val
  9410. 			||  item->refine >= MAX_REFINE		// if it's no longer refineable
  9411. 			||  ditem->flag.no_refine 	// if the item isn't refinable
  9412. 			||  (i = pc_search_inventory(sd, material [ditem->wlv])) < 0 )
  9413. 			{
  9414. 				clif_skill_fail(sd,sd->menuskill_id,USESKILL_FAIL_LEVEL,0);
  9415. 				return;
  9416. 			}
  9417.  
  9418. 			per = percentrefinery [ditem->wlv][(int)item->refine];
  9419. 			per += (((signed int)sd->status.job_level)-50)/2; //Updated per the new kro descriptions. [Skotlex]
  9420.  
  9421. 			pc_delitem(sd, i, 1, 0, 0);
  9422. 			if (per > rand() % 100) {
  9423. 				item->refine++;
  9424. 				if(item->equip) {
  9425. 					ep = item->equip;
  9426. 					pc_unequipitem(sd,idx,3);
  9427. 				}
  9428. 				clif_refine(sd->fd,0,idx,item->refine);
  9429. 				clif_delitem(sd,idx,1,3);
  9430. 				clif_additem(sd,idx,1,0);
  9431. 				if (ep)
  9432. 					pc_equipitem(sd,idx,ep);
  9433. 				clif_misceffect(&sd->bl,3);
  9434. 				if(item->refine == MAX_REFINE &&
  9435. 					item->card[0] == CARD0_FORGE &&
  9436. 					(int)MakeDWord(item->card[2],item->card[3]) == sd->status.char_id)
  9437. 				{ // Fame point system [DracoRPG]
  9438. 					switch(ditem->wlv){
  9439. 						case 1:
  9440. 							pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  9441. 							break;
  9442. 						case 2:
  9443. 							pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  9444. 							break;
  9445. 						case 3:
  9446. 							pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  9447. 							break;
  9448. 					}
  9449. 				}
  9450. 			} else {
  9451. 				item->refine = 0;
  9452. 				if(item->equip)
  9453. 					pc_unequipitem(sd,idx,3);
  9454. 				clif_refine(sd->fd,1,idx,item->refine);
  9455. 				pc_delitem(sd,idx,1,0,2);
  9456. 				clif_misceffect(&sd->bl,2);
  9457. 				clif_emotion(&sd->bl, E_OMG);
  9458. 			}
  9459. 		}
  9460. 	}
  9461. }
  9462.  
  9463. /*==========================================
  9464.  *
  9465.  *------------------------------------------*/
  9466. int skill_autospell (struct map_session_data *sd, int skillid)
  9467. {
  9468. 	int skilllv;
  9469. 	int maxlv=1,lv;
  9470.  
  9471. 	nullpo_ret(sd);
  9472.  
  9473. 	skilllv = sd->menuskill_val;
  9474. 	lv=pc_checkskill(sd,skillid);
  9475.  
  9476. 	if(skilllv <= 0 || !lv) return 0; // Player must learn the skill before doing auto-spell [Lance]
  9477.  
  9478. 	if(skillid==MG_NAPALMBEAT)	maxlv=3;
  9479. 	else if(skillid==MG_COLDBOLT || skillid==MG_FIREBOLT || skillid==MG_LIGHTNINGBOLT){
  9480. 		if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SAGE)
  9481. 			maxlv =20; //Soul Linker bonus. [Skotlex]
  9482. 		else if(skilllv==2) maxlv=1;
  9483. 		else if(skilllv==3) maxlv=2;
  9484. 		else if(skilllv>=4) maxlv=3;
  9485. 	}
  9486. 	else if(skillid==MG_SOULSTRIKE){
  9487. 		if(skilllv==5) maxlv=1;
  9488. 		else if(skilllv==6) maxlv=2;
  9489. 		else if(skilllv>=7) maxlv=3;
  9490. 	}
  9491. 	else if(skillid==MG_FIREBALL){
  9492. 		if(skilllv==8) maxlv=1;
  9493. 		else if(skilllv>=9) maxlv=2;
  9494. 	}
  9495. 	else if(skillid==MG_FROSTDIVER) maxlv=1;
  9496. 	else return 0;
  9497.  
  9498. 	if(maxlv > lv)
  9499. 		maxlv = lv;
  9500.  
  9501. 	sc_start4(&sd->bl,SC_AUTOSPELL,100,skilllv,skillid,maxlv,0,
  9502. 		skill_get_time(SA_AUTOSPELL,skilllv));
  9503. 	return 0;
  9504. }
  9505.  
  9506. /*==========================================
  9507.  * Sitting skills functions.
  9508.  *------------------------------------------*/
  9509. static int skill_sit_count (struct block_list *bl, va_list ap)
  9510. {
  9511. 	struct map_session_data *sd;
  9512. 	int type =va_arg(ap,int);
  9513. 	sd=(struct map_session_data*)bl;
  9514.  
  9515. 	if(!pc_issit(sd))
  9516. 		return 0;
  9517.  
  9518. 	if(type&1 && pc_checkskill(sd,RG_GANGSTER) > 0)
  9519. 		return 1;
  9520.  
  9521. 	if(type&2 && (pc_checkskill(sd,TK_HPTIME) > 0 || pc_checkskill(sd,TK_SPTIME) > 0))
  9522. 		return 1;
  9523.  
  9524. 	return 0;
  9525. }
  9526.  
  9527. static int skill_sit_in (struct block_list *bl, va_list ap)
  9528. {
  9529. 	struct map_session_data *sd;
  9530. 	int type =va_arg(ap,int);
  9531.  
  9532. 	sd=(struct map_session_data*)bl;
  9533.  
  9534. 	if(!pc_issit(sd))
  9535. 		return 0;
  9536.  
  9537. 	if(type&1 && pc_checkskill(sd,RG_GANGSTER) > 0)
  9538. 		sd->state.gangsterparadise=1;
  9539.  
  9540. 	if(type&2 && (pc_checkskill(sd,TK_HPTIME) > 0 || pc_checkskill(sd,TK_SPTIME) > 0 ))
  9541. 	{
  9542. 		sd->state.rest=1;
  9543. 		status_calc_regen(bl, &sd->battle_status, &sd->regen);
  9544. 		status_calc_regen_rate(bl, &sd->regen, &sd->sc);
  9545. 	}
  9546.  
  9547. 	return 0;
  9548. }
  9549.  
  9550. static int skill_sit_out (struct block_list *bl, va_list ap)
  9551. {
  9552. 	struct map_session_data *sd;
  9553. 	int type =va_arg(ap,int);
  9554. 	sd=(struct map_session_data*)bl;
  9555. 	if(sd->state.gangsterparadise && type&1)
  9556. 		sd->state.gangsterparadise=0;
  9557. 	if(sd->state.rest && type&2) {
  9558. 		sd->state.rest=0;
  9559. 		status_calc_regen(bl, &sd->battle_status, &sd->regen);
  9560. 		status_calc_regen_rate(bl, &sd->regen, &sd->sc);
  9561. 	}
  9562. 	return 0;
  9563. }
  9564.  
  9565. int skill_sit (struct map_session_data *sd, int type)
  9566. {
  9567. 	int flag = 0;
  9568. 	int range = 0, lv;
  9569. 	nullpo_ret(sd);
  9570.  
  9571.  
  9572. 	if((lv = pc_checkskill(sd,RG_GANGSTER)) > 0) {
  9573. 		flag|=1;
  9574. 		range = skill_get_splash(RG_GANGSTER, lv);
  9575. 	}
  9576. 	if((lv = pc_checkskill(sd,TK_HPTIME)) > 0) {
  9577. 		flag|=2;
  9578. 		range = skill_get_splash(TK_HPTIME, lv);
  9579. 	}
  9580. 	else if ((lv = pc_checkskill(sd,TK_SPTIME)) > 0) {
  9581. 		flag|=2;
  9582. 		range = skill_get_splash(TK_SPTIME, lv);
  9583. 	}
  9584.  
  9585. 	if (!flag) return 0;
  9586.  
  9587. 	if(type) {
  9588. 		if (map_foreachinrange(skill_sit_count,&sd->bl, range, BL_PC, flag) > 1)
  9589. 			map_foreachinrange(skill_sit_in,&sd->bl, range, BL_PC, flag);
  9590. 	} else {
  9591. 		if (map_foreachinrange(skill_sit_count,&sd->bl, range, BL_PC, flag) < 2)
  9592. 			map_foreachinrange(skill_sit_out,&sd->bl, range, BL_PC, flag);
  9593. 	}
  9594. 	return 0;
  9595. }
  9596.  
  9597. /*==========================================
  9598.  *
  9599.  *------------------------------------------*/
  9600. int skill_frostjoke_scream (struct block_list *bl, va_list ap)
  9601. {
  9602. 	struct block_list *src;
  9603. 	int skillnum,skilllv;
  9604. 	unsigned int tick;
  9605.  
  9606. 	nullpo_ret(bl);
  9607. 	nullpo_ret(src=va_arg(ap,struct block_list*));
  9608.  
  9609. 	skillnum=va_arg(ap,int);
  9610. 	skilllv=va_arg(ap,int);
  9611. 	if(skilllv <= 0) return 0;
  9612. 	tick=va_arg(ap,unsigned int);
  9613.  
  9614. 	if (src == bl || status_isdead(bl))
  9615. 		return 0;
  9616. 	if (bl->type == BL_PC) {
  9617. 		struct map_session_data *sd = (struct map_session_data *)bl;
  9618. 		if (sd && sd->sc.option&OPTION_INVISIBLE)
  9619. 			return 0;
  9620. 	}
  9621. 	//It has been reported that Scream/Joke works the same regardless of woe-setting. [Skotlex]
  9622. 	if(battle_check_target(src,bl,BCT_ENEMY) > 0)
  9623. 		skill_additional_effect(src,bl,skillnum,skilllv,BF_MISC,ATK_DEF,tick);
  9624. 	else if(battle_check_target(src,bl,BCT_PARTY) > 0 && rand()%100 < 10)
  9625. 		skill_additional_effect(src,bl,skillnum,skilllv,BF_MISC,ATK_DEF,tick);
  9626.  
  9627. 	return 0;
  9628. }
  9629.  
  9630. /*==========================================
  9631.  *
  9632.  *------------------------------------------*/
  9633. static void skill_unitsetmapcell (struct skill_unit *src, int skill_num, int skill_lv, cell_t cell, bool flag)
  9634. {
  9635. 	int range = skill_get_unit_range(skill_num,skill_lv);
  9636. 	int x,y;
  9637.  
  9638. 	for( y = src->bl.y - range; y <= src->bl.y + range; ++y )
  9639. 		for( x = src->bl.x - range; x <= src->bl.x + range; ++x )
  9640. 			map_setcell(src->bl.m, x, y, cell, flag);
  9641. }
  9642.  
  9643. /*==========================================
  9644.  *
  9645.  *------------------------------------------*/
  9646. int skill_attack_area (struct block_list *bl, va_list ap)
  9647. {
  9648. 	struct block_list *src,*dsrc;
  9649. 	int atk_type,skillid,skilllv,flag,type;
  9650. 	unsigned int tick;
  9651.  
  9652. 	if(status_isdead(bl))
  9653. 		return 0;
  9654.  
  9655. 	atk_type = va_arg(ap,int);
  9656. 	src=va_arg(ap,struct block_list*);
  9657. 	dsrc=va_arg(ap,struct block_list*);
  9658. 	skillid=va_arg(ap,int);
  9659. 	skilllv=va_arg(ap,int);
  9660. 	tick=va_arg(ap,unsigned int);
  9661. 	flag=va_arg(ap,int);
  9662. 	type=va_arg(ap,int);
  9663.  
  9664.  
  9665. 	if (skill_area_temp[1] == bl->id) //This is the target of the skill, do a full attack and skip target checks.
  9666. 		return skill_attack(atk_type,src,dsrc,bl,skillid,skilllv,tick,flag);
  9667.  
  9668. 	if(battle_check_target(dsrc,bl,type) <= 0 ||
  9669. 		!status_check_skilluse(NULL, bl, skillid, 2))
  9670. 		return 0;
  9671.  
  9672.  
  9673. 	switch (skillid) {
  9674. 	case WZ_FROSTNOVA: //Skills that don't require the animation to be removed
  9675. 	case NPC_ACIDBREATH:
  9676. 	case NPC_DARKNESSBREATH:
  9677. 	case NPC_FIREBREATH:
  9678. 	case NPC_ICEBREATH:
  9679. 	case NPC_THUNDERBREATH:
  9680. 		return skill_attack(atk_type,src,dsrc,bl,skillid,skilllv,tick,flag);
  9681. 	default:
  9682. 		//Area-splash, disable skill animation.
  9683. 		return skill_attack(atk_type,src,dsrc,bl,skillid,skilllv,tick,flag|SD_ANIMATION);
  9684. 	}
  9685. }
  9686. /*==========================================
  9687.  *
  9688.  *------------------------------------------*/
  9689. int skill_clear_group (struct block_list *bl, int flag)
  9690. {
  9691. 	struct unit_data *ud = unit_bl2ud(bl);
  9692. 	struct skill_unit_group *group[MAX_SKILLUNITGROUP];
  9693. 	int i, count=0;
  9694.  
  9695. 	nullpo_ret(bl);
  9696. 	if (!ud) return 0;
  9697.  
  9698. 	//All groups to be deleted are first stored on an array since the array elements shift around when you delete them. [Skotlex]
  9699. 	for (i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i];i++)
  9700. 	{
  9701. 		switch (ud->skillunit[i]->skill_id) {
  9702. 			case SA_DELUGE:
  9703. 			case SA_VOLCANO:
  9704. 			case SA_VIOLENTGALE:
  9705. 			case SA_LANDPROTECTOR:
  9706. 			case NJ_SUITON:
  9707. 			case NJ_KAENSIN:
  9708. 				if (flag&1)
  9709. 					group[count++]= ud->skillunit[i];
  9710. 				break;
  9711. 			default:
  9712. 				if (flag&2 && skill_get_inf2(ud->skillunit[i]->skill_id)&INF2_TRAP)
  9713. 					group[count++]= ud->skillunit[i];
  9714. 				break;
  9715. 		}
  9716.  
  9717. 	}
  9718. 	for (i=0;i<count;i++)
  9719. 		skill_delunitgroup(group[i]);
  9720. 	return count;
  9721. }
  9722.  
  9723. /*==========================================
  9724.  * Returns the first element field found [Skotlex]
  9725.  *------------------------------------------*/
  9726. struct skill_unit_group *skill_locate_element_field(struct block_list *bl)
  9727. {
  9728. 	struct unit_data *ud = unit_bl2ud(bl);
  9729. 	int i;
  9730. 	nullpo_ret(bl);
  9731. 	if (!ud) return NULL;
  9732.  
  9733. 	for (i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i];i++) {
  9734. 		switch (ud->skillunit[i]->skill_id) {
  9735. 			case SA_DELUGE:
  9736. 			case SA_VOLCANO:
  9737. 			case SA_VIOLENTGALE:
  9738. 			case SA_LANDPROTECTOR:
  9739. 			case NJ_SUITON:
  9740. 				return ud->skillunit[i];
  9741. 		}
  9742. 	}
  9743. 	return NULL;
  9744. }
  9745.  
  9746. // for graffiti cleaner [Valaris]
  9747. int skill_graffitiremover (struct block_list *bl, va_list ap)
  9748. {
  9749. 	struct skill_unit *unit=NULL;
  9750.  
  9751. 	nullpo_ret(bl);
  9752. 	nullpo_ret(ap);
  9753.  
  9754. 	if(bl->type!=BL_SKILL || (unit=(struct skill_unit *)bl) == NULL)
  9755. 		return 0;
  9756.  
  9757. 	if((unit->group) && (unit->group->unit_id == UNT_GRAFFITI))
  9758. 		skill_delunit(unit);
  9759.  
  9760. 	return 0;
  9761. }
  9762.  
  9763. int skill_greed (struct block_list *bl, va_list ap)
  9764. {
  9765. 	struct block_list *src;
  9766. 	struct map_session_data *sd=NULL;
  9767. 	struct flooritem_data *fitem=NULL;
  9768.  
  9769. 	nullpo_ret(bl);
  9770. 	nullpo_ret(src = va_arg(ap, struct block_list *));
  9771.  
  9772. 	if(src->type == BL_PC && (sd=(struct map_session_data *)src) && bl->type==BL_ITEM && (fitem=(struct flooritem_data *)bl))
  9773. 		pc_takeitem(sd, fitem);
  9774.  
  9775. 	return 0;
  9776. }
  9777.  
  9778. /*==========================================
  9779.  *
  9780.  *------------------------------------------*/
  9781. static int skill_cell_overlap(struct block_list *bl, va_list ap)
  9782. {
  9783. 	int skillid;
  9784. 	int *alive;
  9785. 	struct skill_unit *unit;
  9786. 	struct block_list *src;
  9787.  
  9788. 	skillid = va_arg(ap,int);
  9789. 	alive = va_arg(ap,int *);
  9790. 	src = va_arg(ap,struct block_list *);
  9791. 	unit = (struct skill_unit *)bl;
  9792.  
  9793. 	if (unit == NULL || unit->group == NULL || (*alive) == 0)
  9794. 		return 0;
  9795.  
  9796. 	switch (skillid)
  9797. 	{
  9798. 		case SA_LANDPROTECTOR:
  9799. 			if( unit->group->skill_id == SA_LANDPROTECTOR )
  9800. 			{	//Check for offensive Land Protector to delete both. [Skotlex]
  9801. 				(*alive) = 0;
  9802. 				skill_delunit(unit);
  9803. 				return 1;
  9804. 			}
  9805. 			if( !(skill_get_inf2(unit->group->skill_id)&(INF2_SONG_DANCE|INF2_TRAP)) )
  9806. 			{	//It deletes everything except songs/dances and traps
  9807. 				skill_delunit(unit);
  9808. 				return 1;
  9809. 			}
  9810. 			break;
  9811. 		case HW_GANBANTEIN:
  9812. 			if( !(unit->group->state.song_dance&0x1) )
  9813. 			{// Don't touch song/dance.
  9814. 				skill_delunit(unit);
  9815. 				return 1;
  9816. 			}
  9817. 			break;
  9818. 		case SA_VOLCANO:
  9819. 		case SA_DELUGE:
  9820. 		case SA_VIOLENTGALE:
  9821. // The official implementation makes them fail to appear when casted on top of ANYTHING
  9822. // but I wonder if they didn't actually meant to fail when casted on top of each other?
  9823. // hence, I leave the alternate implementation here, commented. [Skotlex]
  9824. 			if (unit->range <= 0)
  9825. 			{
  9826. 				(*alive) = 0;
  9827. 				return 1;
  9828. 			}
  9829. /*
  9830. 			switch (unit->group->skill_id)
  9831. 			{	//These cannot override each other.
  9832. 				case SA_VOLCANO:
  9833. 				case SA_DELUGE:
  9834. 				case SA_VIOLENTGALE:
  9835. 					(*alive) = 0;
  9836. 					return 1;
  9837. 			}
  9838. */
  9839. 			break;
  9840. 		case PF_FOGWALL:
  9841. 			switch(unit->group->skill_id)
  9842. 			{
  9843. 				case SA_VOLCANO: //Can't be placed on top of these
  9844. 				case SA_VIOLENTGALE:
  9845. 					(*alive) = 0;
  9846. 					return 1;
  9847. 				case SA_DELUGE:
  9848. 				case NJ_SUITON:
  9849. 				//Cheap 'hack' to notify the calling function that duration should be doubled [Skotlex]
  9850. 					(*alive) = 2;
  9851. 					break;
  9852. 			}
  9853. 			break;
  9854. 		case HP_BASILICA:
  9855. 			if (unit->group->skill_id == HP_BASILICA)
  9856. 			{	//Basilica can't be placed on top of itself to avoid map-cell stacking problems. [Skotlex]
  9857. 				(*alive) = 0;
  9858. 				return 1;
  9859. 			}
  9860. 			break;
  9861. 	}
  9862.  
  9863. 	if (unit->group->skill_id == SA_LANDPROTECTOR &&
  9864. 		!(skill_get_inf2(skillid)&(INF2_SONG_DANCE|INF2_TRAP)))
  9865. 	{	//It deletes everything except songs/dances/traps
  9866. 		(*alive) = 0;
  9867. 		return 1;
  9868. 	}
  9869.  
  9870. 	return 0;
  9871. }
  9872.  
  9873. /*==========================================
  9874.  *
  9875.  *------------------------------------------*/
  9876. int skill_chastle_mob_changetarget(struct block_list *bl,va_list ap)
  9877. {
  9878. 	struct mob_data* md;
  9879. 	struct unit_data*ud = unit_bl2ud(bl);
  9880. 	struct block_list *from_bl;
  9881. 	struct block_list *to_bl;
  9882. 	md = (struct mob_data*)bl;
  9883. 	from_bl = va_arg(ap,struct block_list *);
  9884. 	to_bl = va_arg(ap,struct block_list *);
  9885.  
  9886. 	if(ud && ud->target == from_bl->id)
  9887. 		ud->target = to_bl->id;
  9888.  
  9889. 	if(md->bl.type == BL_MOB && md->target_id == from_bl->id)
  9890. 		md->target_id = to_bl->id;
  9891. 	return 0;
  9892. }
  9893.  
  9894. /*==========================================
  9895.  *
  9896.  *------------------------------------------*/
  9897. static int skill_trap_splash (struct block_list *bl, va_list ap)
  9898. {
  9899. 	struct block_list *src;
  9900. 	int tick;
  9901. 	struct skill_unit *unit;
  9902. 	struct skill_unit_group *sg;
  9903. 	struct block_list *ss;
  9904. 	src = va_arg(ap,struct block_list *);
  9905. 	unit = (struct skill_unit *)src;
  9906. 	tick = va_arg(ap,int);
  9907.  
  9908. 	if( !unit->alive || bl->prev == NULL )
  9909. 		return 0;
  9910.  
  9911. 	nullpo_ret(sg = unit->group);
  9912. 	nullpo_ret(ss = map_id2bl(sg->src_id));
  9913.  
  9914. 	if(battle_check_target(src,bl,BCT_ENEMY) <= 0)
  9915. 		return 0;
  9916.  
  9917. 	switch(sg->unit_id){
  9918. 		case UNT_SHOCKWAVE:
  9919. 		case UNT_SANDMAN:
  9920. 		case UNT_FLASHER:
  9921. 			skill_additional_effect(ss,bl,sg->skill_id,sg->skill_lv,BF_MISC,ATK_DEF,tick);
  9922. 			break;
  9923. 		case UNT_GROUNDDRIFT_WIND:
  9924. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9925. 				sc_start(bl,SC_STUN,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9926. 			break;
  9927. 		case UNT_GROUNDDRIFT_DARK:
  9928. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9929. 				sc_start(bl,SC_BLIND,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9930. 			break;
  9931. 		case UNT_GROUNDDRIFT_POISON:
  9932. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9933. 				sc_start(bl,SC_POISON,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9934. 			break;
  9935. 		case UNT_GROUNDDRIFT_WATER:
  9936. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9937. 				sc_start(bl,SC_FREEZE,5,sg->skill_lv,skill_get_time2(sg->skill_id, sg->skill_lv));
  9938. 			break;
  9939. 		case UNT_GROUNDDRIFT_FIRE:
  9940. 			if(skill_attack(BF_WEAPON,ss,src,bl,sg->skill_id,sg->skill_lv,tick,sg->val1))
  9941. 				skill_blown(src,bl,skill_get_blewcount(sg->skill_id,sg->skill_lv),-1,0);
  9942. 			break;
  9943. 		default:
  9944. 			skill_attack(skill_get_type(sg->skill_id),ss,src,bl,sg->skill_id,sg->skill_lv,tick,0);
  9945. 			break;
  9946. 	}
  9947. 	return 1;
  9948. }
  9949.  
  9950. /*==========================================
  9951.  *
  9952.  *------------------------------------------*/
  9953. int skill_enchant_elemental_end (struct block_list *bl, int type)
  9954. {
  9955. 	struct status_change *sc;
  9956. 	const enum sc_type scs[] = { SC_ENCPOISON, SC_ASPERSIO, SC_FIREWEAPON, SC_WATERWEAPON, SC_WINDWEAPON, SC_EARTHWEAPON, SC_SHADOWWEAPON, SC_GHOSTWEAPON, SC_ENCHANTARMS };
  9957. 	int i;
  9958. 	nullpo_ret(bl);
  9959. 	nullpo_ret(sc= status_get_sc(bl));
  9960.  
  9961. 	if (!sc->count) return 0;
  9962.  
  9963. 	for (i = 0; i < ARRAYLENGTH(scs); i++)
  9964. 		if (type != scs[i] && sc->data[scs[i]])
  9965. 			status_change_end(bl, scs[i], INVALID_TIMER);
  9966.  
  9967. 	return 0;
  9968. }
  9969.  
  9970. bool skill_check_cloaking(struct block_list *bl, struct status_change_entry *sce)
  9971. {
  9972. 	static int dx[] = { 0, 1, 0, -1, -1,  1, 1, -1};
  9973. 	static int dy[] = {-1, 0, 1,  0, -1, -1, 1,  1};
  9974. 	bool wall = true;
  9975. 	int i;
  9976.  
  9977. 	if( (bl->type == BL_PC && battle_config.pc_cloak_check_type&1)
  9978. 	||	(bl->type != BL_PC && battle_config.monster_cloak_check_type&1) )
  9979. 	{	//Check for walls.
  9980. 		ARR_FIND( 0, 8, i, map_getcell(bl->m, bl->x+dx[i], bl->y+dy[i], CELL_CHKNOPASS) != 0 );
  9981. 		if( i == 8 )
  9982. 			wall = false;
  9983. 	}
  9984.  
  9985. 	if( sce )
  9986. 	{
  9987. 		if( !wall )
  9988. 		{
  9989. 			if( sce->val1 < 3 ) //End cloaking.
  9990. 				status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  9991. 			else
  9992. 			if( sce->val4&1 )
  9993. 			{	//Remove wall bonus
  9994. 				sce->val4&=~1;
  9995. 				status_calc_bl(bl,SCB_SPEED);
  9996. 			}
  9997. 		}
  9998. 		else
  9999. 		{
  10000. 			if( !(sce->val4&1) )
  10001. 			{	//Add wall speed bonus
  10002. 				sce->val4|=1;
  10003. 				status_calc_bl(bl,SCB_SPEED);
  10004. 			}
  10005. 		}
  10006. 	}
  10007.  
  10008. 	return wall;
  10009. }
  10010.  
  10011. /*==========================================
  10012.  *
  10013.  *------------------------------------------*/
  10014. struct skill_unit *skill_initunit (struct skill_unit_group *group, int idx, int x, int y, int val1, int val2)
  10015. {
  10016. 	struct skill_unit *unit;
  10017.  
  10018. 	nullpo_retr(NULL, group);
  10019. 	nullpo_retr(NULL, group->unit); // crash-protection against poor coding
  10020. 	nullpo_retr(NULL, unit=&group->unit[idx]);
  10021.  
  10022. 	if(!unit->alive)
  10023. 		group->alive_count++;
  10024.  
  10025. 	unit->bl.id=map_get_new_object_id();
  10026. 	unit->bl.type=BL_SKILL;
  10027. 	unit->bl.m=group->map;
  10028. 	unit->bl.x=x;
  10029. 	unit->bl.y=y;
  10030. 	unit->group=group;
  10031. 	unit->alive=1;
  10032. 	unit->val1=val1;
  10033. 	unit->val2=val2;
  10034.  
  10035. 	idb_put(skillunit_db, unit->bl.id, unit);
  10036. 	map_addiddb(&unit->bl);
  10037. 	map_addblock(&unit->bl);
  10038.  
  10039. 	// perform oninit actions
  10040. 	switch (group->skill_id) {
  10041. 	case WZ_ICEWALL:
  10042. 		map_setgatcell(unit->bl.m,unit->bl.x,unit->bl.y,5);
  10043. 		clif_changemapcell(0,unit->bl.m,unit->bl.x,unit->bl.y,5,AREA);
  10044. 		break;
  10045. 	case SA_LANDPROTECTOR:
  10046. 		skill_unitsetmapcell(unit,SA_LANDPROTECTOR,group->skill_lv,CELL_LANDPROTECTOR,true);
  10047. 		break;
  10048. 	case HP_BASILICA:
  10049. 		skill_unitsetmapcell(unit,HP_BASILICA,group->skill_lv,CELL_BASILICA,true);
  10050. 		break;
  10051. 	default:
  10052. 		if (group->state.song_dance&0x1) //Check for dissonance.
  10053. 			skill_dance_overlap(unit, 1);
  10054. 		break;
  10055. 	}
  10056.  
  10057. 	clif_skill_setunit(unit);
  10058.  
  10059. 	return unit;
  10060. }
  10061.  
  10062. /*==========================================
  10063.  *
  10064.  *------------------------------------------*/
  10065. int skill_delunit (struct skill_unit* unit)
  10066. {
  10067. 	struct skill_unit_group *group;
  10068.  
  10069. 	nullpo_ret(unit);
  10070. 	if( !unit->alive )
  10071. 		return 0;
  10072. 	unit->alive=0;
  10073.  
  10074. 	nullpo_ret(group=unit->group);
  10075.  
  10076. 	if( group->state.song_dance&0x1 ) //Cancel dissonance effect.
  10077. 		skill_dance_overlap(unit, 0);
  10078.  
  10079. 	// invoke onout event
  10080. 	if( !unit->range )
  10081. 		map_foreachincell(skill_unit_effect,unit->bl.m,unit->bl.x,unit->bl.y,group->bl_flag,&unit->bl,gettick(),4);
  10082.  
  10083. 	// perform ondelete actions
  10084. 	switch (group->skill_id) {
  10085. 	case HT_ANKLESNARE:
  10086. 		{
  10087. 		struct block_list* target = map_id2bl(group->val2);
  10088. 		if( target )
  10089. 			status_change_end(target, SC_ANKLE, INVALID_TIMER);
  10090. 		}
  10091. 		break;
  10092. 	case WZ_ICEWALL:
  10093. 		map_setgatcell(unit->bl.m,unit->bl.x,unit->bl.y,unit->val2);
  10094. 		clif_changemapcell(0,unit->bl.m,unit->bl.x,unit->bl.y,unit->val2,ALL_SAMEMAP); // hack to avoid clientside cell bug
  10095. 		break;
  10096. 	case SA_LANDPROTECTOR:
  10097. 		skill_unitsetmapcell(unit,SA_LANDPROTECTOR,group->skill_lv,CELL_LANDPROTECTOR,false);
  10098. 		break;
  10099. 	case HP_BASILICA:
  10100. 		skill_unitsetmapcell(unit,HP_BASILICA,group->skill_lv,CELL_BASILICA,false);
  10101. 		break;
  10102. 	}
  10103.  
  10104. 	clif_skill_delunit(unit);
  10105.  
  10106. 	unit->group=NULL;
  10107. 	map_delblock(&unit->bl); // don't free yet
  10108. 	map_deliddb(&unit->bl);
  10109. 	idb_remove(skillunit_db, unit->bl.id);
  10110. 	if(--group->alive_count==0)
  10111. 		skill_delunitgroup(group);
  10112.  
  10113. 	return 0;
  10114. }
  10115. /*==========================================
  10116.  *
  10117.  *------------------------------------------*/
  10118. static DBMap* group_db = NULL;// int group_id -> struct skill_unit_group*
  10119.  
  10120. /// Returns the target skill_unit_group or NULL if not found.
  10121. struct skill_unit_group* skill_id2group(int group_id)
  10122. {
  10123. 	return (struct skill_unit_group*)idb_get(group_db, group_id);
  10124. }
  10125.  
  10126.  
  10127. static int skill_unit_group_newid = MAX_SKILL_DB;
  10128.  
  10129. /// Returns a new group_id that isn't being used in group_db.
  10130. /// Fatal error if nothing is available.
  10131. static int skill_get_new_group_id(void)
  10132. {
  10133. 	if( skill_unit_group_newid >= MAX_SKILL_DB && skill_id2group(skill_unit_group_newid) == NULL )
  10134. 		return skill_unit_group_newid++;// available
  10135. 	{// find next id
  10136. 		int base_id = skill_unit_group_newid;
  10137. 		while( base_id != ++skill_unit_group_newid )
  10138. 		{
  10139. 			if( skill_unit_group_newid < MAX_SKILL_DB )
  10140. 				skill_unit_group_newid = MAX_SKILL_DB;
  10141. 			if( skill_id2group(skill_unit_group_newid) == NULL )
  10142. 				return skill_unit_group_newid++;// available
  10143. 		}
  10144. 		// full loop, nothing available
  10145. 		ShowFatalError("skill_get_new_group_id: All ids are taken. Exiting...");
  10146. 		exit(1);
  10147. 	}
  10148. }
  10149.  
  10150. struct skill_unit_group* skill_initunitgroup (struct block_list* src, int count, short skillid, short skilllv, int unit_id, int limit, int interval)
  10151. {
  10152. 	struct unit_data* ud = unit_bl2ud( src );
  10153. 	struct skill_unit_group* group;
  10154. 	int i;
  10155.  
  10156. 	if(skillid <= 0 || skilllv <= 0) return 0;
  10157.  
  10158. 	nullpo_retr(NULL, src);
  10159. 	nullpo_retr(NULL, ud);
  10160.  
  10161. 	// find a free spot to store the new unit group
  10162. 	ARR_FIND( 0, MAX_SKILLUNITGROUP, i, ud->skillunit[i] == NULL );
  10163. 	if(i == MAX_SKILLUNITGROUP)
  10164. 	{
  10165. 		// array is full, make room by discarding oldest group
  10166. 		int j=0;
  10167. 		unsigned maxdiff=0,x,tick=gettick();
  10168. 		for(i=0;i<MAX_SKILLUNITGROUP && ud->skillunit[i];i++)
  10169. 			if((x=DIFF_TICK(tick,ud->skillunit[i]->tick))>maxdiff){
  10170. 				maxdiff=x;
  10171. 				j=i;
  10172. 			}
  10173. 		skill_delunitgroup(ud->skillunit[j]);
  10174. 		//Since elements must have shifted, we use the last slot.
  10175. 		i = MAX_SKILLUNITGROUP-1;
  10176. 	}
  10177.  
  10178. 	group             = ers_alloc(skill_unit_ers, struct skill_unit_group);
  10179. 	group->src_id     = src->id;
  10180. 	group->party_id   = status_get_party_id(src);
  10181. 	group->guild_id   = status_get_guild_id(src);
  10182. 	group->bg_id      = bg_team_get_id(src);
  10183. 	group->group_id   = skill_get_new_group_id();
  10184. 	group->unit       = (struct skill_unit *)aCalloc(count,sizeof(struct skill_unit));
  10185. 	group->unit_count = count;
  10186. 	group->alive_count = 0;
  10187. 	group->val1       = 0;
  10188. 	group->val2       = 0;
  10189. 	group->val3       = 0;
  10190. 	group->skill_id   = skillid;
  10191. 	group->skill_lv   = skilllv;
  10192. 	group->unit_id    = unit_id;
  10193. 	group->map        = src->m;
  10194. 	group->limit      = limit;
  10195. 	group->interval   = interval;
  10196. 	group->tick       = gettick();
  10197. 	group->valstr     = NULL;
  10198.  
  10199. 	ud->skillunit[i] = group;
  10200.  
  10201. 	if (skillid == PR_SANCTUARY) //Sanctuary starts healing +1500ms after casted. [Skotlex]
  10202. 		group->tick += 1500;
  10203.  
  10204. 	idb_put(group_db, group->group_id, group);
  10205. 	return group;
  10206. }
  10207.  
  10208. /*==========================================
  10209.  *
  10210.  *------------------------------------------*/
  10211. int skill_delunitgroup_(struct skill_unit_group *group, const char* file, int line, const char* func)
  10212. {
  10213. 	struct block_list* src;
  10214. 	struct unit_data *ud;
  10215. 	int i,j;
  10216.  
  10217. 	if( group == NULL )
  10218. 	{
  10219. 		ShowDebug("skill_delunitgroup: group is NULL (source=%s:%d, %s)! Please report this! (#3504)\n", file, line, func);
  10220. 		return 0;
  10221. 	}
  10222.  
  10223. 	src=map_id2bl(group->src_id);
  10224. 	ud = unit_bl2ud(src);
  10225. 	if(!src || !ud) {
  10226. 		ShowError("skill_delunitgroup: Group's source not found! (src_id: %d skill_id: %d)\n", group->src_id, group->skill_id);
  10227. 		return 0;
  10228. 	}
  10229. 	if (skill_get_unit_flag(group->skill_id)&(UF_DANCE|UF_SONG|UF_ENSEMBLE))
  10230. 	{
  10231. 		struct status_change* sc = status_get_sc(src);
  10232. 		if (sc && sc->data[SC_DANCING])
  10233. 		{
  10234. 			sc->data[SC_DANCING]->val2 = 0 ; //This prevents status_change_end attempting to redelete the group. [Skotlex]
  10235. 			status_change_end(src, SC_DANCING, INVALID_TIMER);
  10236. 		}
  10237. 	}
  10238.  
  10239. 	// end Gospel's status change on 'src'
  10240. 	// (needs to be done when the group is deleted by other means than skill deactivation)
  10241. 	if (group->unit_id == UNT_GOSPEL) {
  10242. 		struct status_change *sc = status_get_sc(src);
  10243. 		if(sc && sc->data[SC_GOSPEL]) {
  10244. 			sc->data[SC_GOSPEL]->val3 = 0; //Remove reference to this group. [Skotlex]
  10245. 			status_change_end(src, SC_GOSPEL, INVALID_TIMER);
  10246. 		}
  10247. 	}
  10248.  
  10249. 	if (group->skill_id == SG_SUN_WARM ||
  10250. 		group->skill_id == SG_MOON_WARM ||
  10251. 		group->skill_id == SG_STAR_WARM) {
  10252. 		struct status_change *sc = status_get_sc(src);
  10253. 		if(sc && sc->data[SC_WARM]) {
  10254. 			sc->data[SC_WARM]->val4 = 0;
  10255. 			status_change_end(src, SC_WARM, INVALID_TIMER);
  10256. 		}
  10257. 	}
  10258.  
  10259. 	if (src->type==BL_PC && group->state.ammo_consume)
  10260. 		battle_consume_ammo((TBL_PC*)src, group->skill_id, group->skill_lv);
  10261.  
  10262. 	group->alive_count=0;
  10263.  
  10264. 	// remove all unit cells
  10265. 	if(group->unit != NULL)
  10266. 		for( i = 0; i < group->unit_count; i++ )
  10267. 			skill_delunit(&group->unit[i]);
  10268.  
  10269. 	// clear Talkie-box string
  10270. 	if( group->valstr != NULL )
  10271. 	{
  10272. 		aFree(group->valstr);
  10273. 		group->valstr = NULL;
  10274. 	}
  10275.  
  10276. 	idb_remove(group_db, group->group_id);
  10277. 	map_freeblock(&group->unit->bl); // schedules deallocation of whole array (HACK)
  10278. 	group->unit=NULL;
  10279. 	group->group_id=0;
  10280. 	group->unit_count=0;
  10281.  
  10282. 	// locate this group, swap with the last entry and delete it
  10283. 	ARR_FIND( 0, MAX_SKILLUNITGROUP, i, ud->skillunit[i] == group );
  10284. 	ARR_FIND( i, MAX_SKILLUNITGROUP, j, ud->skillunit[j] == NULL ); j--;
  10285. 	if( i < MAX_SKILLUNITGROUP )
  10286. 	{
  10287. 		ud->skillunit[i] = ud->skillunit[j];
  10288. 		ud->skillunit[j] = NULL;
  10289. 		ers_free(skill_unit_ers, group);
  10290. 	}
  10291. 	else
  10292. 		ShowError("skill_delunitgroup: Group not found! (src_id: %d skill_id: %d)\n", group->src_id, group->skill_id);
  10293.  
  10294. 	return 1;
  10295. }
  10296.  
  10297. /*==========================================
  10298.  *
  10299.  *------------------------------------------*/
  10300. int skill_clear_unitgroup (struct block_list *src)
  10301. {
  10302. 	struct unit_data *ud = unit_bl2ud(src);
  10303.  
  10304. 	nullpo_ret(ud);
  10305.  
  10306. 	while (ud->skillunit[0])
  10307. 		skill_delunitgroup(ud->skillunit[0]);
  10308.  
  10309. 	return 1;
  10310. }
  10311.  
  10312. /*==========================================
  10313.  *
  10314.  *------------------------------------------*/
  10315. struct skill_unit_group_tickset *skill_unitgrouptickset_search (struct block_list *bl, struct skill_unit_group *group, int tick)
  10316. {
  10317. 	int i,j=-1,k,s,id;
  10318. 	struct unit_data *ud;
  10319. 	struct skill_unit_group_tickset *set;
  10320.  
  10321. 	nullpo_ret(bl);
  10322. 	if (group->interval==-1)
  10323. 		return NULL;
  10324.  
  10325. 	ud = unit_bl2ud(bl);
  10326. 	if (!ud) return NULL;
  10327.  
  10328. 	set = ud->skillunittick;
  10329.  
  10330. 	if (skill_get_unit_flag(group->skill_id)&UF_NOOVERLAP)
  10331. 		id = s = group->skill_id;
  10332. 	else
  10333. 		id = s = group->group_id;
  10334.  
  10335. 	for (i=0; i<MAX_SKILLUNITGROUPTICKSET; i++) {
  10336. 		k = (i+s) % MAX_SKILLUNITGROUPTICKSET;
  10337. 		if (set[k].id == id)
  10338. 			return &set[k];
  10339. 		else if (j==-1 && (DIFF_TICK(tick,set[k].tick)>0 || set[k].id==0))
  10340. 			j=k;
  10341. 	}
  10342.  
  10343. 	if (j == -1) {
  10344. 		ShowWarning ("skill_unitgrouptickset_search: tickset is full\n");
  10345. 		j = id % MAX_SKILLUNITGROUPTICKSET;
  10346. 	}
  10347.  
  10348. 	set[j].id = id;
  10349. 	set[j].tick = tick;
  10350. 	return &set[j];
  10351. }
  10352.  
  10353. /*==========================================
  10354.  *
  10355.  *------------------------------------------*/
  10356. int skill_unit_timer_sub_onplace (struct block_list* bl, va_list ap)
  10357. {
  10358. 	struct skill_unit* unit = va_arg(ap,struct skill_unit *);
  10359. 	struct skill_unit_group* group = unit->group;
  10360. 	unsigned int tick = va_arg(ap,unsigned int);
  10361.  
  10362. 	if( !unit->alive || bl->prev == NULL )
  10363. 		return 0;
  10364.  
  10365. 	nullpo_ret(group);
  10366.  
  10367. 	if( !(skill_get_inf2(group->skill_id)&(INF2_SONG_DANCE|INF2_TRAP)) && map_getcell(bl->m, bl->x, bl->y, CELL_CHKLANDPROTECTOR) )
  10368. 		return 0; //AoE skills are ineffective. [Skotlex]
  10369.  
  10370. 	if( battle_check_target(&unit->bl,bl,group->target_flag) <= 0 )
  10371. 		return 0;
  10372.  
  10373. 	skill_unit_onplace_timer(unit,bl,tick);
  10374.  
  10375. 	return 1;
  10376. }
  10377.  
  10378. /*==========================================
  10379.  *
  10380.  *------------------------------------------*/
  10381. static int skill_unit_timer_sub (DBKey key, void* data, va_list ap)
  10382. {
  10383. 	struct skill_unit* unit = (struct skill_unit*)data;
  10384. 	struct skill_unit_group* group = unit->group;
  10385. 	unsigned int tick = va_arg(ap,unsigned int);
  10386.   	bool dissonance;
  10387. 	struct block_list* bl = &unit->bl;
  10388.  
  10389. 	if( !unit->alive )
  10390. 		return 0;
  10391.  
  10392. 	nullpo_ret(group);
  10393.  
  10394. 	// check for expiration
  10395. 	if( (DIFF_TICK(tick,group->tick) >= group->limit || DIFF_TICK(tick,group->tick) >= unit->limit) )
  10396. 	{// skill unit expired (inlined from skill_unit_onlimit())
  10397. 		switch( group->unit_id )
  10398. 		{
  10399. 			case UNT_BLASTMINE:
  10400. 			case UNT_GROUNDDRIFT_WIND:
  10401. 			case UNT_GROUNDDRIFT_DARK:
  10402. 			case UNT_GROUNDDRIFT_POISON:
  10403. 			case UNT_GROUNDDRIFT_WATER:
  10404. 			case UNT_GROUNDDRIFT_FIRE:
  10405. 				group->unit_id = UNT_USED_TRAPS;
  10406. 				//clif_changetraplook(bl, UNT_FIREPILLAR_ACTIVE);
  10407. 				group->limit=DIFF_TICK(tick+1500,group->tick);
  10408. 				unit->limit=DIFF_TICK(tick+1500,group->tick);
  10409. 			break;
  10410.  
  10411. 			case UNT_ANKLESNARE:
  10412. 				if( group->val2 > 0 ) {
  10413. 					// Used Trap don't returns back to item
  10414. 					skill_delunit(unit);
  10415. 					break;
  10416. 				}
  10417. 			case UNT_SKIDTRAP:
  10418. 			case UNT_LANDMINE:
  10419. 			case UNT_SHOCKWAVE:
  10420. 			case UNT_SANDMAN:
  10421. 			case UNT_FLASHER:
  10422. 			case UNT_FREEZINGTRAP:
  10423. 			case UNT_CLAYMORETRAP:
  10424. 			case UNT_TALKIEBOX:
  10425. 			{
  10426. 				struct block_list* src;
  10427. 				if( unit->val1 > 0 && (src = map_id2bl(group->src_id)) != NULL && src->type == BL_PC )
  10428. 				{ // revert unit back into a trap
  10429. 					struct item item_tmp;
  10430. 					memset(&item_tmp,0,sizeof(item_tmp));
  10431. 					item_tmp.nameid = ITEMID_TRAP;
  10432. 					item_tmp.identify = 1;
  10433. 					map_addflooritem(&item_tmp,1,bl->m,bl->x,bl->y,0,0,0,0);
  10434. 				}
  10435. 				skill_delunit(unit);
  10436. 			}
  10437. 			break;
  10438.  
  10439. 			case UNT_WARP_ACTIVE:
  10440. 				// warp portal opens (morph to a UNT_WARP_WAITING cell)
  10441. 				group->unit_id = skill_get_unit_id(group->skill_id, 1); // UNT_WARP_WAITING
  10442. 				clif_changelook(&unit->bl, LOOK_BASE, group->unit_id);
  10443. 				// restart timers
  10444. 				group->limit = skill_get_time(group->skill_id,group->skill_lv);
  10445. 				unit->limit = skill_get_time(group->skill_id,group->skill_lv);
  10446. 				// apply effect to all units standing on it
  10447. 				map_foreachincell(skill_unit_effect,unit->bl.m,unit->bl.x,unit->bl.y,group->bl_flag,&unit->bl,gettick(),1);
  10448. 			break;
  10449.  
  10450. 			case UNT_CALLFAMILY:
  10451. 			{
  10452. 				struct map_session_data *sd = NULL;
  10453. 				if(group->val1) {
  10454. 		  			sd = map_charid2sd(group->val1);
  10455. 					group->val1 = 0;
  10456. 					if (sd && !map[sd->bl.m].flag.nowarp)
  10457. 						pc_setpos(sd,map_id2index(unit->bl.m),unit->bl.x,unit->bl.y,CLR_TELEPORT);
  10458. 				}
  10459. 				if(group->val2) {
  10460. 					sd = map_charid2sd(group->val2);
  10461. 					group->val2 = 0;
  10462. 					if (sd && !map[sd->bl.m].flag.nowarp)
  10463. 						pc_setpos(sd,map_id2index(unit->bl.m),unit->bl.x,unit->bl.y,CLR_TELEPORT);
  10464. 				}
  10465. 				skill_delunit(unit);
  10466. 			}
  10467. 			break;
  10468.  
  10469. 			default:
  10470. 				skill_delunit(unit);
  10471. 		}
  10472. 	}
  10473. 	else
  10474. 	{// skill unit is still active
  10475. 		switch( group->unit_id )
  10476. 		{
  10477. 			case UNT_ICEWALL:
  10478. 				// icewall loses 50 hp every second
  10479. 				unit->val1 -= SKILLUNITTIMER_INTERVAL/20; // trap's hp
  10480. 				if( unit->val1 <= 0 && unit->limit + group->tick > tick + 700 )
  10481. 					unit->limit = DIFF_TICK(tick+700,group->tick);
  10482. 				break;
  10483. 			case UNT_SKIDTRAP:
  10484. 			case UNT_LANDMINE:
  10485. 			case UNT_SHOCKWAVE:
  10486. 			case UNT_SANDMAN:
  10487. 			case UNT_FLASHER:
  10488. 			case UNT_FREEZINGTRAP:
  10489. 			case UNT_TALKIEBOX:
  10490. 			case UNT_ANKLESNARE:
  10491. 				if( unit->val1 <= 0 ) {
  10492. 					if( group->unit_id == UNT_ANKLESNARE && group->val2 > 0 )
  10493. 						skill_delunit(unit);
  10494. 					else {
  10495. 						group->unit_id = UNT_USED_TRAPS;
  10496. 						group->limit = DIFF_TICK(tick, group->tick) + 1500;
  10497. 					}
  10498. 				}
  10499. 				break;
  10500. 		}
  10501. 	}
  10502.  
  10503. 	//Don't continue if unit or even group is expired and has been deleted.
  10504. 	if( !group || !unit->alive )
  10505. 		return 0;
  10506.  
  10507. 	dissonance = skill_dance_switch(unit, 0);
  10508.  
  10509. 	if( unit->range >= 0 && group->interval != -1 )
  10510. 	{
  10511. 		if( battle_config.skill_wall_check )
  10512. 			map_foreachinshootrange(skill_unit_timer_sub_onplace, bl, unit->range, group->bl_flag, bl,tick);
  10513. 		else
  10514. 			map_foreachinrange(skill_unit_timer_sub_onplace, bl, unit->range, group->bl_flag, bl,tick);
  10515.  
  10516. 		if(unit->range == -1) //Unit disabled, but it should not be deleted yet.
  10517. 			group->unit_id = UNT_USED_TRAPS;
  10518.  
  10519. 		if( group->unit_id == UNT_TATAMIGAESHI )
  10520. 		{
  10521. 			unit->range = -1; //Disable processed cell.
  10522. 			if (--group->val1 <= 0) // number of live cells
  10523. 	  		{	//All tiles were processed, disable skill.
  10524. 				group->target_flag=BCT_NOONE;
  10525. 				group->bl_flag= BL_NUL;
  10526. 			}
  10527. 		}
  10528. 	}
  10529.  
  10530.   	if( dissonance ) skill_dance_switch(unit, 1);
  10531.  
  10532. 	return 0;
  10533. }
  10534. /*==========================================
  10535.  * Executes on all skill units every SKILLUNITTIMER_INTERVAL miliseconds.
  10536.  *------------------------------------------*/
  10537. int skill_unit_timer(int tid, unsigned int tick, int id, intptr_t data)
  10538. {
  10539. 	map_freeblock_lock();
  10540.  
  10541. 	skillunit_db->foreach(skillunit_db, skill_unit_timer_sub, tick);
  10542.  
  10543. 	map_freeblock_unlock();
  10544.  
  10545. 	return 0;
  10546. }
  10547.  
  10548. static int skill_unit_temp[20];  // temporary storage for tracking skill unit skill ids as players move in/out of them
  10549. /*==========================================
  10550.  *
  10551.  *------------------------------------------*/
  10552. int skill_unit_move_sub (struct block_list* bl, va_list ap)
  10553. {
  10554. 	struct skill_unit* unit = (struct skill_unit *)bl;
  10555. 	struct skill_unit_group* group = unit->group;
  10556.  
  10557. 	struct block_list* target = va_arg(ap,struct block_list*);
  10558. 	unsigned int tick = va_arg(ap,unsigned int);
  10559. 	int flag = va_arg(ap,int);
  10560.  
  10561. 	bool dissonance;
  10562. 	int skill_id;
  10563. 	int i;
  10564.  
  10565. 	nullpo_ret(group);
  10566.  
  10567. 	if( !unit->alive || target->prev == NULL )
  10568. 		return 0;
  10569.  
  10570. 	if( unit->group->skill_id == PF_SPIDERWEB && flag&1 )
  10571. 		return 0; // Fiberlock is never supposed to trigger on skill_unit_move. [Inkfish]
  10572.  
  10573. 	dissonance = skill_dance_switch(unit, 0);
  10574.  
  10575. 	//Necessary in case the group is deleted after calling on_place/on_out [Skotlex]
  10576. 	skill_id = unit->group->skill_id;
  10577.  
  10578. 	if( unit->group->interval != -1 && !(skill_get_unit_flag(skill_id)&UF_DUALMODE) )
  10579. 	{	//Non-dualmode unit skills with a timer don't trigger when walking, so just return
  10580. 		if( dissonance ) skill_dance_switch(unit, 1);
  10581. 		return 0;
  10582. 	}
  10583.  
  10584. 	//Target-type check.
  10585. 	if( !(group->bl_flag&target->type && battle_check_target(&unit->bl,target,group->target_flag) > 0) )
  10586. 	{
  10587. 		if( group->src_id == target->id && group->state.song_dance&0x2 )
  10588. 		{	//Ensemble check to see if they went out/in of the area [Skotlex]
  10589. 			if( flag&1 )
  10590. 			{
  10591. 				if( flag&2 )
  10592. 				{	//Clear this skill id.
  10593. 					ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == skill_id );
  10594. 					if( i < ARRAYLENGTH(skill_unit_temp) )
  10595. 						skill_unit_temp[i] = 0;
  10596. 				}
  10597. 			}
  10598. 			else
  10599. 			{
  10600. 				if( flag&2 )
  10601. 				{	//Store this skill id.
  10602. 					ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == 0 );
  10603. 					if( i < ARRAYLENGTH(skill_unit_temp) )
  10604. 						skill_unit_temp[i] = skill_id;
  10605. 					else
  10606. 						ShowError("skill_unit_move_sub: Reached limit of unit objects per cell!\n");
  10607. 				}
  10608.  
  10609. 			}
  10610.  
  10611. 			if( flag&4 )
  10612. 				skill_unit_onleft(skill_id,target,tick);
  10613. 		}
  10614.  
  10615. 		if( dissonance ) skill_dance_switch(unit, 1);
  10616.  
  10617. 		return 0;
  10618. 	}
  10619. 	else
  10620. 	{
  10621. 		if( flag&1 )
  10622. 		{
  10623. 			int result = skill_unit_onplace(unit,target,tick);
  10624. 			if( flag&2 && result )
  10625. 			{	//Clear skill ids we have stored in onout.
  10626. 				ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == result );
  10627. 				if( i < ARRAYLENGTH(skill_unit_temp) )
  10628. 					skill_unit_temp[i] = 0;
  10629. 			}
  10630. 		}
  10631. 		else
  10632. 		{
  10633. 			int result = skill_unit_onout(unit,target,tick);
  10634. 			if( flag&2 && result )
  10635. 			{	//Store this unit id.
  10636. 				ARR_FIND( 0, ARRAYLENGTH(skill_unit_temp), i, skill_unit_temp[i] == 0 );
  10637. 				if( i < ARRAYLENGTH(skill_unit_temp) )
  10638. 					skill_unit_temp[i] = skill_id;
  10639. 				else
  10640. 					ShowError("skill_unit_move_sub: Reached limit of unit objects per cell!\n");
  10641. 			}
  10642. 		}
  10643.  
  10644. 		//TODO: Normally, this is dangerous since the unit and group could be freed
  10645. 		//inside the onout/onplace functions. Currently it is safe because we know song/dance
  10646. 		//cells do not get deleted within them. [Skotlex]
  10647. 		if( dissonance ) skill_dance_switch(unit, 1);
  10648.  
  10649. 		if( flag&4 )
  10650. 			skill_unit_onleft(skill_id,target,tick);
  10651.  
  10652. 		return 1;
  10653. 	}
  10654. }
  10655.  
  10656. /*==========================================
  10657.  * Invoked when a char has moved and unit cells must be invoked (onplace, onout, onleft)
  10658.  * Flag values:
  10659.  * flag&1: invoke skill_unit_onplace (otherwise invoke skill_unit_onout)
  10660.  * flag&2: this function is being invoked twice as a bl moves, store in memory the affected
  10661.  * units to figure out when they have left a group.
  10662.  * flag&4: Force a onleft event (triggered when the bl is killed, for example)
  10663.  *------------------------------------------*/
  10664. int skill_unit_move (struct block_list *bl, unsigned int tick, int flag)
  10665. {
  10666. 	nullpo_ret(bl);
  10667.  
  10668. 	if( bl->prev == NULL )
  10669. 		return 0;
  10670.  
  10671. 	if( flag&2 && !(flag&1) )
  10672. 	{	//Onout, clear data
  10673. 		memset(skill_unit_temp, 0, sizeof(skill_unit_temp));
  10674. 	}
  10675.  
  10676. 	map_foreachincell(skill_unit_move_sub,bl->m,bl->x,bl->y,BL_SKILL,bl,tick,flag);
  10677.  
  10678. 	if( flag&2 && flag&1 )
  10679. 	{	//Onplace, check any skill units you have left.
  10680. 		int i;
  10681. 		for( i = 0; i < ARRAYLENGTH(skill_unit_temp); i++ )
  10682. 			if( skill_unit_temp[i] )
  10683. 				skill_unit_onleft(skill_unit_temp[i], bl, tick);
  10684. 	}
  10685.  
  10686. 	return 0;
  10687. }
  10688.  
  10689. /*==========================================
  10690.  *
  10691.  *------------------------------------------*/
  10692. int skill_unit_move_unit_group (struct skill_unit_group *group, int m, int dx, int dy)
  10693. {
  10694. 	int i,j;
  10695. 	unsigned int tick = gettick();
  10696. 	int *m_flag;
  10697. 	struct skill_unit *unit1;
  10698. 	struct skill_unit *unit2;
  10699.  
  10700. 	if (group == NULL)
  10701. 		return 0;
  10702. 	if (group->unit_count<=0)
  10703. 		return 0;
  10704. 	if (group->unit==NULL)
  10705. 		return 0;
  10706.  
  10707. 	if (skill_get_unit_flag(group->skill_id)&UF_ENSEMBLE)
  10708. 		return 0; //Ensembles may not be moved around.
  10709.  
  10710. 	if( group->unit_id == UNT_ICEWALL )
  10711. 		return 0; //Icewalls don't get knocked back
  10712.  
  10713. 	m_flag = (int *) aCalloc(group->unit_count, sizeof(int));
  10714. 	//    m_flag
  10715. 	//		0: Neither of the following (skill_unit_onplace & skill_unit_onout are needed)
  10716. 	//		1: Unit will move to a slot that had another unit of the same group (skill_unit_onplace not needed)
  10717. 	//		2: Another unit from same group will end up positioned on this unit (skill_unit_onout not needed)
  10718. 	//		3: Both 1+2.
  10719. 	for(i=0;i<group->unit_count;i++){
  10720. 		unit1=&group->unit[i];
  10721. 		if (!unit1->alive || unit1->bl.m!=m)
  10722. 			continue;
  10723. 		for(j=0;j<group->unit_count;j++){
  10724. 			unit2=&group->unit[j];
  10725. 			if (!unit2->alive)
  10726. 				continue;
  10727. 			if (unit1->bl.x+dx==unit2->bl.x && unit1->bl.y+dy==unit2->bl.y){
  10728. 				m_flag[i] |= 0x1;
  10729. 			}
  10730. 			if (unit1->bl.x-dx==unit2->bl.x && unit1->bl.y-dy==unit2->bl.y){
  10731. 				m_flag[i] |= 0x2;
  10732. 			}
  10733. 		}
  10734. 	}
  10735. 	j = 0;
  10736. 	for (i=0;i<group->unit_count;i++) {
  10737. 		unit1=&group->unit[i];
  10738. 		if (!unit1->alive)
  10739. 			continue;
  10740. 		if (!(m_flag[i]&0x2)) {
  10741. 			if (group->state.song_dance&0x1) //Cancel dissonance effect.
  10742. 				skill_dance_overlap(unit1, 0);
  10743. 			map_foreachincell(skill_unit_effect,unit1->bl.m,unit1->bl.x,unit1->bl.y,group->bl_flag,&unit1->bl,tick,4);
  10744. 		}
  10745. 		//Move Cell using "smart" criteria (avoid useless moving around)
  10746. 		switch(m_flag[i])
  10747. 		{
  10748. 			case 0:
  10749. 			//Cell moves independently, safely move it.
  10750. 				map_moveblock(&unit1->bl, unit1->bl.x+dx, unit1->bl.y+dy, tick);
  10751. 				break;
  10752. 			case 1:
  10753. 			//Cell moves unto another cell, look for a replacement cell that won't collide
  10754. 			//and has no cell moving into it (flag == 2)
  10755. 				for(;j<group->unit_count;j++)
  10756. 				{
  10757. 					if(m_flag[j]!=2 || !group->unit[j].alive)
  10758. 						continue;
  10759. 					//Move to where this cell would had moved.
  10760. 					unit2 = &group->unit[j];
  10761. 					map_moveblock(&unit1->bl, unit2->bl.x+dx, unit2->bl.y+dy, tick);
  10762. 					j++; //Skip this cell as we have used it.
  10763. 					break;
  10764. 				}
  10765. 				break;
  10766. 			case 2:
  10767. 			case 3:
  10768. 				break; //Don't move the cell as a cell will end on this tile anyway.
  10769. 		}
  10770. 		if (!(m_flag[i]&0x2)) { //We only moved the cell in 0-1
  10771. 			if (group->state.song_dance&0x1) //Check for dissonance effect.
  10772. 				skill_dance_overlap(unit1, 1);
  10773. 			clif_skill_setunit(unit1);
  10774. 			map_foreachincell(skill_unit_effect,unit1->bl.m,unit1->bl.x,unit1->bl.y,group->bl_flag,&unit1->bl,tick,1);
  10775. 		}
  10776. 	}
  10777. 	aFree(m_flag);
  10778. 	return 0;
  10779. }
  10780.  
  10781. /*==========================================
  10782.  *
  10783.  *------------------------------------------*/
  10784. int skill_can_produce_mix (struct map_session_data *sd, int nameid, int trigger, int qty)
  10785. {
  10786. 	int i,j;
  10787.  
  10788. 	nullpo_ret(sd);
  10789.  
  10790. 	if(nameid<=0)
  10791. 		return 0;
  10792.  
  10793. 	for(i=0;i<MAX_SKILL_PRODUCE_DB;i++){
  10794. 		if(skill_produce_db[i].nameid == nameid )
  10795. 			break;
  10796. 	}
  10797. 	if( i >= MAX_SKILL_PRODUCE_DB )
  10798. 		return 0;
  10799.  
  10800. 	if( pc_checkadditem(sd, nameid, qty) == ADDITEM_OVERAMOUNT )
  10801. 	{// cannot carry the produced stuff
  10802. 		return 0;
  10803. 	}
  10804.  
  10805. 	if(trigger>=0){
  10806. 		if(trigger>20) { // Non-weapon, non-food item (itemlv must match)
  10807. 			if(skill_produce_db[i].itemlv!=trigger)
  10808. 				return 0;
  10809. 		} else if(trigger>10) { // Food (any item level between 10 and 20 will do)
  10810. 			if(skill_produce_db[i].itemlv<=10 || skill_produce_db[i].itemlv>20)
  10811. 				return 0;
  10812. 		} else { // Weapon (itemlv must be higher or equal)
  10813. 			if(skill_produce_db[i].itemlv>trigger)
  10814. 				return 0;
  10815. 		}
  10816. 	}
  10817. 	if((j=skill_produce_db[i].req_skill)>0 &&
  10818. 		pc_checkskill(sd,j) < skill_produce_db[i].req_skill_lv)
  10819. 		return 0;
  10820.  
  10821. 	for(j=0;j<MAX_PRODUCE_RESOURCE;j++){
  10822. 		int id,x,y;
  10823. 		if( (id=skill_produce_db[i].mat_id[j]) <= 0 )
  10824. 			continue;
  10825. 		if(skill_produce_db[i].mat_amount[j] <= 0) {
  10826. 			if(pc_search_inventory(sd,id) < 0)
  10827. 				return 0;
  10828. 		}
  10829. 		else {
  10830. 			for(y=0,x=0;y<MAX_INVENTORY;y++)
  10831. 				if( sd->status.inventory[y].nameid == id )
  10832. 					x+=sd->status.inventory[y].amount;
  10833. 			if(x<qty*skill_produce_db[i].mat_amount[j])
  10834. 				return 0;
  10835. 		}
  10836. 	}
  10837. 	return i+1;
  10838. }
  10839.  
  10840. /*==========================================
  10841.  *
  10842.  *------------------------------------------*/
  10843. int skill_produce_mix (struct map_session_data *sd, int skill_id, int nameid, int slot1, int slot2, int slot3, int qty)
  10844. {
  10845. 	int slot[3];
  10846. 	int i,sc,ele,idx,equip,wlv,make_per,flag;
  10847. 	int num = -1; // exclude the recipe
  10848. 	struct status_data *status;
  10849.  
  10850. 	nullpo_ret(sd);
  10851. 	status = status_get_status_data(&sd->bl);
  10852.  
  10853. 	if( !(idx=skill_can_produce_mix(sd,nameid,-1, qty)) )
  10854. 		return 0;
  10855. 	idx--;
  10856.  
  10857. 	if (qty < 1)
  10858. 		qty = 1;
  10859.  
  10860. 	if (!skill_id) //A skill can be specified for some override cases.
  10861. 		skill_id = skill_produce_db[idx].req_skill;
  10862.  
  10863. 	slot[0]=slot1;
  10864. 	slot[1]=slot2;
  10865. 	slot[2]=slot3;
  10866.  
  10867. 	for(i=0,sc=0,ele=0;i<3;i++){ //Note that qty should always be one if you are using these!
  10868. 		int j;
  10869. 		if( slot[i]<=0 )
  10870. 			continue;
  10871. 		j = pc_search_inventory(sd,slot[i]);
  10872. 		if(j < 0)
  10873. 			continue;
  10874. 		if(slot[i]==1000){	/* Star Crumb */
  10875. 			pc_delitem(sd,j,1,1,0);
  10876. 			sc++;
  10877. 		}
  10878. 		if(slot[i]>=994 && slot[i]<=997 && ele==0){	/* Flame Heart . . . Great Nature */
  10879. 			static const int ele_table[4]={3,1,4,2};
  10880. 			pc_delitem(sd,j,1,1,0);
  10881. 			ele=ele_table[slot[i]-994];
  10882. 		}
  10883. 	}
  10884.  
  10885. 	for(i=0;i<MAX_PRODUCE_RESOURCE;i++){
  10886. 		int j,id,x;
  10887. 		if( (id=skill_produce_db[idx].mat_id[i]) <= 0 )
  10888. 			continue;
  10889. 		num++;
  10890. 		x=qty*skill_produce_db[idx].mat_amount[i];
  10891. 		do{
  10892. 			int y=0;
  10893. 			j = pc_search_inventory(sd,id);
  10894.  
  10895. 			if(j >= 0){
  10896. 				y = sd->status.inventory[j].amount;
  10897. 				if(y>x)y=x;
  10898. 				pc_delitem(sd,j,y,0,0);
  10899. 			} else
  10900. 				ShowError("skill_produce_mix: material item error\n");
  10901.  
  10902. 			x-=y;
  10903. 		}while( j>=0 && x>0 );
  10904. 	}
  10905.  
  10906. 	if((equip=itemdb_isequip(nameid)))
  10907. 		wlv = itemdb_wlv(nameid);
  10908. 	if(!equip) {
  10909. 		switch(skill_id){
  10910. 			case BS_IRON:
  10911. 			case BS_STEEL:
  10912. 			case BS_ENCHANTEDSTONE:
  10913. 				// Ores & Metals Refining - skill bonuses are straight from kRO website [DracoRPG]
  10914. 				i = pc_checkskill(sd,skill_id);
  10915. 				make_per = sd->status.job_level*20 + status->dex*10 + status->luk*10; //Base chance
  10916. 				switch(nameid){
  10917. 					case 998: // Iron
  10918. 						make_per += 4000+i*500; // Temper Iron bonus: +26/+32/+38/+44/+50
  10919. 						break;
  10920. 					case 999: // Steel
  10921. 						make_per += 3000+i*500; // Temper Steel bonus: +35/+40/+45/+50/+55
  10922. 						break;
  10923. 					case 1000: //Star Crumb
  10924. 						make_per = 100000; // Star Crumbs are 100% success crafting rate? (made 1000% so it succeeds even after penalties) [Skotlex]
  10925. 						break;
  10926. 					default: // Enchanted Stones
  10927. 						make_per += 1000+i*500; // Enchantedstone Craft bonus: +15/+20/+25/+30/+35
  10928. 					break;
  10929. 				}
  10930. 				break;
  10931. 			case ASC_CDP:
  10932. 				make_per = (2000 + 40*status->dex + 20*status->luk);
  10933. 				break;
  10934. 			case AL_HOLYWATER:
  10935. 				make_per = 100000; //100% success
  10936. 				break;
  10937. 			case AM_PHARMACY: // Potion Preparation - reviewed with the help of various Ragnainfo sources [DracoRPG]
  10938. 			case AM_TWILIGHT1:
  10939. 			case AM_TWILIGHT2:
  10940. 			case AM_TWILIGHT3:
  10941. 				make_per = pc_checkskill(sd,AM_LEARNINGPOTION)*50
  10942. 					+ pc_checkskill(sd,AM_PHARMACY)*300 + sd->status.job_level*20
  10943. 					+ (status->int_/2)*10 + status->dex*10+status->luk*10;
  10944. 				if(merc_is_hom_active(sd->hd)) {//Player got a homun
  10945. 					int skill;
  10946. 					if((skill=merc_hom_checkskill(sd->hd,HVAN_INSTRUCT)) > 0) //His homun is a vanil with instruction change
  10947. 						make_per += skill*100; //+1% bonus per level
  10948. 				}
  10949. 				switch(nameid){
  10950. 					case 501: // Red Potion
  10951. 					case 503: // Yellow Potion
  10952. 					case 504: // White Potion
  10953. 						make_per += (1+rand()%100)*10 + 2000;
  10954. 						break;
  10955. 					case 970: // Alcohol
  10956. 						make_per += (1+rand()%100)*10 + 1000;
  10957. 						break;
  10958. 					case 7135: // Bottle Grenade
  10959. 					case 7136: // Acid Bottle
  10960. 					case 7137: // Plant Bottle
  10961. 					case 7138: // Marine Sphere Bottle
  10962. 						make_per += (1+rand()%100)*10;
  10963. 						break;
  10964. 					case 546: // Condensed Yellow Potion
  10965. 						make_per -= (1+rand()%50)*10;
  10966. 						break;
  10967. 					case 547: // Condensed White Potion
  10968. 					case 7139: // Glistening Coat
  10969. 						make_per -= (1+rand()%100)*10;
  10970. 					    break;
  10971. 					//Common items, recieve no bonus or penalty, listed just because they are commonly produced
  10972. 					case 505: // Blue Potion
  10973. 					case 545: // Condensed Red Potion
  10974. 					case 605: // Anodyne
  10975. 					case 606: // Aloevera
  10976. 					default:
  10977. 						break;
  10978. 				}
  10979. 				if(battle_config.pp_rate != 100)
  10980. 					make_per = make_per * battle_config.pp_rate / 100;
  10981. 				break;
  10982. 			case SA_CREATECON: // Elemental Converter Creation
  10983. 				make_per = 100000; // should be 100% success rate
  10984. 				break;
  10985. 			default:
  10986. 				if (sd->menuskill_id ==	AM_PHARMACY &&
  10987. 					sd->menuskill_val > 10 && sd->menuskill_val <= 20)
  10988. 				{	//Assume Cooking Dish
  10989. 					if (sd->menuskill_val >= 15) //Legendary Cooking Set.
  10990. 						make_per = 10000; //100% Success
  10991. 					else
  10992. 						make_per = 1200 * (sd->menuskill_val - 10)
  10993. 							+ 20  * (sd->status.base_level + 1)
  10994. 							+ 20  * (status->dex + 1)
  10995. 							+ 100 * (rand()%(30+5*(sd->cook_mastery/400) - (6+sd->cook_mastery/80)) + (6+sd->cook_mastery/80))
  10996. 							- 400 * (skill_produce_db[idx].itemlv - 11 + 1)
  10997. 							- 10  * (100 - status->luk + 1)
  10998. 							- 500 * (num - 1)
  10999. 							- 100 * (rand()%4 + 1);
  11000. 					break;
  11001. 				}
  11002. 				make_per = 5000;
  11003. 				break;
  11004. 		}
  11005. 	} else { // Weapon Forging - skill bonuses are straight from kRO website, other things from a jRO calculator [DracoRPG]
  11006. 		make_per = 5000 + sd->status.job_level*20 + status->dex*10 + status->luk*10; // Base
  11007. 		make_per += pc_checkskill(sd,skill_id)*500; // Smithing skills bonus: +5/+10/+15
  11008. 		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
  11009. 		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
  11010. 		if(pc_search_inventory(sd,989) > 0) make_per+= 1000; // Emperium Anvil: +10
  11011. 		else if(pc_search_inventory(sd,988) > 0) make_per+= 500; // Golden Anvil: +5
  11012. 		else if(pc_search_inventory(sd,987) > 0) make_per+= 300; // Oridecon Anvil: +3
  11013. 		else if(pc_search_inventory(sd,986) > 0) make_per+= 0; // Anvil: +0?
  11014. 		if(battle_config.wp_rate != 100)
  11015. 			make_per = make_per * battle_config.wp_rate / 100;
  11016. 	}
  11017.  
  11018. 	if (sd->class_&JOBL_BABY) //if it's a Baby Class
  11019. 		make_per = (make_per * 70) / 100; //Baby penalty is 30%
  11020.  
  11021. 	if(make_per < 1) make_per = 1;
  11022.  
  11023.  
  11024. 	if(rand()%10000 < make_per || qty > 1){ //Success, or crafting multiple items.
  11025. 		struct item tmp_item;
  11026. 		memset(&tmp_item,0,sizeof(tmp_item));
  11027. 		tmp_item.nameid=nameid;
  11028. 		tmp_item.amount=1;
  11029. 		tmp_item.identify=1;
  11030. 		if(equip){
  11031. 			tmp_item.card[0]=CARD0_FORGE;
  11032. 			tmp_item.card[1]=((sc*5)<<8)+ele;
  11033. 			tmp_item.card[2]=GetWord(sd->status.char_id,0); // CharId
  11034. 			tmp_item.card[3]=GetWord(sd->status.char_id,1);
  11035. 		} else {
  11036. 			//Flag is only used on the end, so it can be used here. [Skotlex]
  11037. 			switch (skill_id) {
  11038. 				case BS_DAGGER:
  11039. 				case BS_SWORD:
  11040. 				case BS_TWOHANDSWORD:
  11041. 				case BS_AXE:
  11042. 				case BS_MACE:
  11043. 				case BS_KNUCKLE:
  11044. 				case BS_SPEAR:
  11045. 					flag = battle_config.produce_item_name_input&0x1;
  11046. 					break;
  11047. 				case AM_PHARMACY:
  11048. 				case AM_TWILIGHT1:
  11049. 				case AM_TWILIGHT2:
  11050. 				case AM_TWILIGHT3:
  11051. 					flag = battle_config.produce_item_name_input&0x2;
  11052. 					break;
  11053. 				case AL_HOLYWATER:
  11054. 					flag = battle_config.produce_item_name_input&0x8;
  11055. 					break;
  11056. 				case ASC_CDP:
  11057. 					flag = battle_config.produce_item_name_input&0x10;
  11058. 					break;
  11059. 				default:
  11060. 					flag = battle_config.produce_item_name_input&0x80;
  11061. 					break;
  11062. 			}
  11063. 			if (flag) {
  11064. 				tmp_item.card[0]=CARD0_CREATE;
  11065. 				tmp_item.card[1]=0;
  11066. 				tmp_item.card[2]=GetWord(sd->status.char_id,0); // CharId
  11067. 				tmp_item.card[3]=GetWord(sd->status.char_id,1);
  11068. 			}
  11069. 		}
  11070.  
  11071. //		if(log_config.produce > 0)
  11072. //			log_produce(sd,nameid,slot1,slot2,slot3,1);
  11073. //TODO update PICKLOG
  11074.  
  11075. 		if(equip){
  11076. 			clif_produceeffect(sd,0,nameid);
  11077. 			clif_misceffect(&sd->bl,3);
  11078. 			if(itemdb_wlv(nameid) >= 3 && ((ele? 1 : 0) + sc) >= 3) // Fame point system [DracoRPG]
  11079. 				pc_addfame(sd,10); // Success to forge a lv3 weapon with 3 additional ingredients = +10 fame point
  11080. 		} else {
  11081. 			int fame = 0;
  11082. 			tmp_item.amount = 0;
  11083. 			for (i=0; i< qty; i++)
  11084. 			{	//Apply quantity modifiers.
  11085. 				if (rand()%10000 < make_per || qty == 1)
  11086. 				{ //Success
  11087. 					tmp_item.amount++;
  11088. 					if(nameid < 545 || nameid > 547)
  11089. 						continue;
  11090. 					if(skill_id != AM_PHARMACY &&
  11091. 						skill_id != AM_TWILIGHT1 &&
  11092. 						skill_id != AM_TWILIGHT2 &&
  11093. 						skill_id != AM_TWILIGHT3)
  11094. 						continue;
  11095. 					//Add fame as needed.
  11096. 					switch(++sd->potion_success_counter) {
  11097. 						case 3:
  11098. 							fame+=1; // Success to prepare 3 Condensed Potions in a row
  11099. 							break;
  11100. 						case 5:
  11101. 							fame+=3; // Success to prepare 5 Condensed Potions in a row
  11102. 							break;
  11103. 						case 7:
  11104. 							fame+=10; // Success to prepare 7 Condensed Potions in a row
  11105. 							break;
  11106. 						case 10:
  11107. 							fame+=50; // Success to prepare 10 Condensed Potions in a row
  11108. 							sd->potion_success_counter = 0;
  11109. 							break;
  11110. 					}
  11111. 				} else //Failure
  11112. 					sd->potion_success_counter = 0;
  11113. 			}
  11114. 			if (fame)
  11115. 				pc_addfame(sd,fame);
  11116. 			//Visual effects and the like.
  11117. 			switch (skill_id) {
  11118. 				case AM_PHARMACY:
  11119. 				case AM_TWILIGHT1:
  11120. 				case AM_TWILIGHT2:
  11121. 				case AM_TWILIGHT3:
  11122. 				case ASC_CDP:
  11123. 					clif_produceeffect(sd,2,nameid);
  11124. 					clif_misceffect(&sd->bl,5);
  11125. 					break;
  11126. 				case BS_IRON:
  11127. 				case BS_STEEL:
  11128. 				case BS_ENCHANTEDSTONE:
  11129. 					clif_produceeffect(sd,0,nameid);
  11130. 					clif_misceffect(&sd->bl,3);
  11131. 					break;
  11132. 				default: //Those that don't require a skill?
  11133. 					if( skill_produce_db[idx].itemlv > 10 && skill_produce_db[idx].itemlv <= 20)
  11134. 					{ //Cooking items.
  11135. 						clif_specialeffect(&sd->bl, 608, AREA);
  11136. 						if( sd->cook_mastery < 1999 )
  11137. 							pc_setglobalreg(sd, "COOK_MASTERY",sd->cook_mastery + ( 1 << ( (skill_produce_db[idx].itemlv - 11) / 2 ) ) * 5);
  11138. 					}
  11139. 					break;
  11140. 			}
  11141. 		}
  11142. 		if (tmp_item.amount) { //Success
  11143. 			if((flag = pc_additem(sd,&tmp_item,tmp_item.amount))) {
  11144. 				clif_additem(sd,0,0,flag);
  11145. 				map_addflooritem(&tmp_item,tmp_item.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  11146. 			}
  11147. 			return 1;
  11148. 		}
  11149. 	}
  11150. 	//Failure
  11151. //	if(log_config.produce)
  11152. //		log_produce(sd,nameid,slot1,slot2,slot3,0);
  11153. //TODO update PICKLOG
  11154.  
  11155. 	if(equip){
  11156. 		clif_produceeffect(sd,1,nameid);
  11157. 		clif_misceffect(&sd->bl,2);
  11158. 	} else {
  11159. 		switch (skill_id) {
  11160. 			case ASC_CDP: //25% Damage yourself, and display same effect as failed potion.
  11161. 				status_percent_damage(NULL, &sd->bl, -25, 0, true);
  11162. 			case AM_PHARMACY:
  11163. 			case AM_TWILIGHT1:
  11164. 			case AM_TWILIGHT2:
  11165. 			case AM_TWILIGHT3:
  11166. 				clif_produceeffect(sd,3,nameid);
  11167. 				clif_misceffect(&sd->bl,6);
  11168. 				sd->potion_success_counter = 0; // Fame point system [DracoRPG]
  11169. 				break;
  11170. 			case BS_IRON:
  11171. 			case BS_STEEL:
  11172. 			case BS_ENCHANTEDSTONE:
  11173. 				clif_produceeffect(sd,1,nameid);
  11174. 				clif_misceffect(&sd->bl,2);
  11175. 				break;
  11176. 			default:
  11177. 				if( skill_produce_db[idx].itemlv > 10 && skill_produce_db[idx].itemlv <= 20 )
  11178. 				{ //Cooking items.
  11179. 					clif_specialeffect(&sd->bl, 609, AREA);
  11180. 					if( sd->cook_mastery > 0 )
  11181. 						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 ));
  11182. 				}
  11183. 		}
  11184. 	}
  11185. 	return 0;
  11186. }
  11187.  
  11188. int skill_arrow_create (struct map_session_data *sd, int nameid)
  11189. {
  11190. 	int i,j,flag,index=-1;
  11191. 	struct item tmp_item;
  11192.  
  11193. 	nullpo_ret(sd);
  11194.  
  11195. 	if(nameid <= 0)
  11196. 		return 1;
  11197.  
  11198. 	for(i=0;i<MAX_SKILL_ARROW_DB;i++)
  11199. 		if(nameid == skill_arrow_db[i].nameid) {
  11200. 			index = i;
  11201. 			break;
  11202. 		}
  11203.  
  11204. 	if(index < 0 || (j = pc_search_inventory(sd,nameid)) < 0)
  11205. 		return 1;
  11206.  
  11207. 	pc_delitem(sd,j,1,0,0);
  11208. 	for(i=0;i<MAX_ARROW_RESOURCE;i++) {
  11209. 		memset(&tmp_item,0,sizeof(tmp_item));
  11210. 		tmp_item.identify = 1;
  11211. 		tmp_item.nameid = skill_arrow_db[index].cre_id[i];
  11212. 		tmp_item.amount = skill_arrow_db[index].cre_amount[i];
  11213. 		if(battle_config.produce_item_name_input&0x4) {
  11214. 			tmp_item.card[0]=CARD0_CREATE;
  11215. 			tmp_item.card[1]=0;
  11216. 			tmp_item.card[2]=GetWord(sd->status.char_id,0); // CharId
  11217. 			tmp_item.card[3]=GetWord(sd->status.char_id,1);
  11218. 		}
  11219. 		if(tmp_item.nameid <= 0 || tmp_item.amount <= 0)
  11220. 			continue;
  11221. 		if((flag = pc_additem(sd,&tmp_item,tmp_item.amount))) {
  11222. 			clif_additem(sd,0,0,flag);
  11223. 			map_addflooritem(&tmp_item,tmp_item.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  11224. 		}
  11225. 	}
  11226.  
  11227. 	return 0;
  11228. }
  11229.  
  11230. /*==========================================
  11231.  *
  11232.  *------------------------------------------*/
  11233. int skill_blockpc_end(int tid, unsigned int tick, int id, intptr_t data)
  11234. {
  11235. 	struct map_session_data *sd = map_id2sd(id);
  11236. 	if (data <= 0 || data >= MAX_SKILL)
  11237. 		return 0;
  11238. 	if (!sd) return 0;
  11239. 	if (sd->blockskill[data] != (0x1|(tid&0xFE))) return 0;
  11240. 	sd->blockskill[data] = 0;
  11241. 	return 1;
  11242. }
  11243.  
  11244. int skill_blockpc_start(struct map_session_data *sd, int skillid, int tick)
  11245. {
  11246. 	nullpo_retr (-1, sd);
  11247.  
  11248. 	skillid = skill_get_index(skillid);
  11249. 	if (skillid == 0)
  11250. 		return -1;
  11251.  
  11252. 	if (tick < 1) {
  11253. 		sd->blockskill[skillid] = 0;
  11254. 		return -1;
  11255. 	}
  11256.  
  11257. 	if( battle_config.display_status_timers )
  11258. 		clif_skill_cooldown(sd, skillid, tick);
  11259.  
  11260. 	sd->blockskill[skillid] = 0x1|(0xFE&add_timer(gettick()+tick,skill_blockpc_end,sd->bl.id,skillid));
  11261. 	return 0;
  11262. }
  11263.  
  11264. int skill_blockhomun_end(int tid, unsigned int tick, int id, intptr_t data)	//[orn]
  11265. {
  11266. 	struct homun_data *hd = (TBL_HOM*) map_id2bl(id);
  11267. 	if (data <= 0 || data >= MAX_SKILL)
  11268. 		return 0;
  11269. 	if (hd) hd->blockskill[data] = 0;
  11270.  
  11271. 	return 1;
  11272. }
  11273.  
  11274. int skill_blockhomun_start(struct homun_data *hd, int skillid, int tick)	//[orn]
  11275. {
  11276. 	nullpo_retr (-1, hd);
  11277.  
  11278. 	skillid = skill_get_index(skillid);
  11279. 	if (skillid == 0)
  11280. 		return -1;
  11281.  
  11282. 	if (tick < 1) {
  11283. 		hd->blockskill[skillid] = 0;
  11284. 		return -1;
  11285. 	}
  11286. 	hd->blockskill[skillid] = 1;
  11287. 	return add_timer(gettick() + tick, skill_blockhomun_end, hd->bl.id, skillid);
  11288. }
  11289.  
  11290. int skill_blockmerc_end(int tid, unsigned int tick, int id, intptr_t data)	//[orn]
  11291. {
  11292. 	struct mercenary_data *md = (TBL_MER*)map_id2bl(id);
  11293. 	if( data <= 0 || data >= MAX_SKILL )
  11294. 		return 0;
  11295. 	if( md ) md->blockskill[data] = 0;
  11296.  
  11297. 	return 1;
  11298. }
  11299.  
  11300. int skill_blockmerc_start(struct mercenary_data *md, int skillid, int tick)
  11301. {
  11302. 	nullpo_retr (-1, md);
  11303.  
  11304. 	if( (skillid = skill_get_index(skillid)) == 0 )
  11305. 		return -1;
  11306. 	if( tick < 1 )
  11307. 	{
  11308. 		md->blockskill[skillid] = 0;
  11309. 		return -1;
  11310. 	}
  11311. 	md->blockskill[skillid] = 1;
  11312. 	return add_timer(gettick() + tick, skill_blockmerc_end, md->bl.id, skillid);
  11313. }
  11314.  
  11315. /*
  11316.  *
  11317.  */
  11318. int skill_split_str (char *str, char **val, int num)
  11319. {
  11320. 	int i;
  11321.  
  11322. 	for( i = 0; i < num && str; i++ )
  11323. 	{
  11324. 		val[i] = str;
  11325. 		str = strchr(str,',');
  11326. 		if( str )
  11327. 			*str++=0;
  11328. 	}
  11329.  
  11330. 	return i;
  11331. }
  11332. /*
  11333.  *
  11334.  */
  11335. int skill_split_atoi (char *str, int *val)
  11336. {
  11337. 	int i, j, diff, step = 1;
  11338.  
  11339. 	for (i=0; i<MAX_SKILL_LEVEL; i++) {
  11340. 		if (!str) break;
  11341. 		val[i] = atoi(str);
  11342. 		str = strchr(str,':');
  11343. 		if (str)
  11344. 			*str++=0;
  11345. 	}
  11346. 	if(i==0) //No data found.
  11347. 		return 0;
  11348. 	if(i==1)
  11349. 	{	//Single value, have the whole range have the same value.
  11350. 		for (; i < MAX_SKILL_LEVEL; i++)
  11351. 			val[i] = val[i-1];
  11352. 		return i;
  11353. 	}
  11354. 	//Check for linear change with increasing steps until we reach half of the data acquired.
  11355. 	for (step = 1; step <= i/2; step++)
  11356. 	{
  11357. 		diff = val[i-1] - val[i-step-1];
  11358. 		for(j = i-1; j >= step; j--)
  11359. 			if ((val[j]-val[j-step]) != diff)
  11360. 				break;
  11361.  
  11362. 		if (j>=step) //No match, try next step.
  11363. 			continue;
  11364.  
  11365. 		for(; i < MAX_SKILL_LEVEL; i++)
  11366. 		{	//Apply linear increase
  11367. 			val[i] = val[i-step]+diff;
  11368. 			if (val[i] < 1 && val[i-1] >=0) //Check if we have switched from + to -, cap the decrease to 0 in said cases.
  11369. 			{ val[i] = 1; diff = 0; step = 1; }
  11370. 		}
  11371. 		return i;
  11372. 	}
  11373. 	//Okay.. we can't figure this one out, just fill out the stuff with the previous value.
  11374. 	for (;i<MAX_SKILL_LEVEL; i++)
  11375. 		val[i] = val[i-1];
  11376. 	return i;
  11377. }
  11378.  
  11379. /*
  11380.  *
  11381.  */
  11382. void skill_init_unit_layout (void)
  11383. {
  11384. 	int i,j,size,pos = 0;
  11385.  
  11386. 	memset(skill_unit_layout,0,sizeof(skill_unit_layout));
  11387.  
  11388. 	// standard square layouts go first
  11389. 	for (i=0; i<=MAX_SQUARE_LAYOUT; i++) {
  11390. 		size = i*2+1;
  11391. 		skill_unit_layout[i].count = size*size;
  11392. 		for (j=0; j<size*size; j++) {
  11393. 			skill_unit_layout[i].dx[j] = (j%size-i);
  11394. 			skill_unit_layout[i].dy[j] = (j/size-i);
  11395. 		}
  11396. 	}
  11397.  
  11398. 	// afterwards add special ones
  11399. 	pos = i;
  11400. 	for (i=0;i<MAX_SKILL_DB;i++) {
  11401. 		if (!skill_db[i].unit_id[0] || skill_db[i].unit_layout_type[0] != -1)
  11402. 			continue;
  11403. 		switch (i) {
  11404. 			case MG_FIREWALL:
  11405. 			case WZ_ICEWALL:
  11406. 				// these will be handled later
  11407. 				break;
  11408. 			case PR_SANCTUARY:
  11409. 			case NPC_EVILLAND:
  11410. 			{
  11411. 				static const int dx[] = {
  11412. 					-1, 0, 1,-2,-1, 0, 1, 2,-2,-1,
  11413. 					 0, 1, 2,-2,-1, 0, 1, 2,-1, 0, 1};
  11414. 				static const int dy[]={
  11415. 					-2,-2,-2,-1,-1,-1,-1,-1, 0, 0,
  11416. 					 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2};
  11417. 				skill_unit_layout[pos].count = 21;
  11418. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11419. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11420. 				break;
  11421. 			}
  11422. 			case PR_MAGNUS:
  11423. 			{
  11424. 				static const int dx[] = {
  11425. 					-1, 0, 1,-1, 0, 1,-3,-2,-1, 0,
  11426. 					 1, 2, 3,-3,-2,-1, 0, 1, 2, 3,
  11427. 					-3,-2,-1, 0, 1, 2, 3,-1, 0, 1,-1, 0, 1};
  11428. 				static const int dy[] = {
  11429. 					-3,-3,-3,-2,-2,-2,-1,-1,-1,-1,
  11430. 					-1,-1,-1, 0, 0, 0, 0, 0, 0, 0,
  11431. 					 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3};
  11432. 				skill_unit_layout[pos].count = 33;
  11433. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11434. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11435. 				break;
  11436. 			}
  11437. 			case AS_VENOMDUST:
  11438. 			{
  11439. 				static const int dx[] = {-1, 0, 0, 0, 1};
  11440. 				static const int dy[] = { 0,-1, 0, 1, 0};
  11441. 				skill_unit_layout[pos].count = 5;
  11442. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11443. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11444. 				break;
  11445. 			}
  11446. 			case CR_GRANDCROSS:
  11447. 			case NPC_GRANDDARKNESS:
  11448. 			{
  11449. 				static const int dx[] = {
  11450. 					 0, 0,-1, 0, 1,-2,-1, 0, 1, 2,
  11451. 					-4,-3,-2,-1, 0, 1, 2, 3, 4,-2,
  11452. 					-1, 0, 1, 2,-1, 0, 1, 0, 0};
  11453. 				static const int dy[] = {
  11454. 					-4,-3,-2,-2,-2,-1,-1,-1,-1,-1,
  11455. 					 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  11456. 					 1, 1, 1, 1, 2, 2, 2, 3, 4};
  11457. 				skill_unit_layout[pos].count = 29;
  11458. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11459. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11460. 				break;
  11461. 			}
  11462. 			case PF_FOGWALL:
  11463. 			{
  11464. 				static const int dx[] = {
  11465. 					-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
  11466. 				static const int dy[] = {
  11467. 					-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
  11468. 				skill_unit_layout[pos].count = 15;
  11469. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11470. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11471. 				break;
  11472. 			}
  11473. 			case PA_GOSPEL:
  11474. 			{
  11475. 				static const int dx[] = {
  11476. 					-1, 0, 1,-1, 0, 1,-3,-2,-1, 0,
  11477. 					 1, 2, 3,-3,-2,-1, 0, 1, 2, 3,
  11478. 					-3,-2,-1, 0, 1, 2, 3,-1, 0, 1,
  11479. 					-1, 0, 1};
  11480. 				static const int dy[] = {
  11481. 					-3,-3,-3,-2,-2,-2,-1,-1,-1,-1,
  11482. 					-1,-1,-1, 0, 0, 0, 0, 0, 0, 0,
  11483. 					 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  11484. 					 3, 3, 3};
  11485. 				skill_unit_layout[pos].count = 33;
  11486. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11487. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11488. 				break;
  11489. 			}
  11490. 			case NJ_KAENSIN:
  11491. 			{
  11492. 				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};
  11493. 				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};
  11494. 				skill_unit_layout[pos].count = 24;
  11495. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11496. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11497. 				break;
  11498. 			}
  11499. 			case NJ_TATAMIGAESHI:
  11500. 			{
  11501. 				//Level 1 (count 4, cross of 3x3)
  11502. 				static const int dx1[] = {-1, 1, 0, 0};
  11503. 				static const int dy1[] = { 0, 0,-1, 1};
  11504. 				//Level 2-3 (count 8, cross of 5x5)
  11505. 				static const int dx2[] = {-2,-1, 1, 2, 0, 0, 0, 0};
  11506. 				static const int dy2[] = { 0, 0, 0, 0,-2,-1, 1, 2};
  11507. 				//Level 4-5 (count 12, cross of 7x7
  11508. 				static const int dx3[] = {-3,-2,-1, 1, 2, 3, 0, 0, 0, 0, 0, 0};
  11509. 				static const int dy3[] = { 0, 0, 0, 0, 0, 0,-3,-2,-1, 1, 2, 3};
  11510. 				//lv1
  11511. 				j = 0;
  11512. 				skill_unit_layout[pos].count = 4;
  11513. 				memcpy(skill_unit_layout[pos].dx,dx1,sizeof(dx1));
  11514. 				memcpy(skill_unit_layout[pos].dy,dy1,sizeof(dy1));
  11515. 				skill_db[i].unit_layout_type[j] = pos;
  11516. 				//lv2/3
  11517. 				j++;
  11518. 				pos++;
  11519. 				skill_unit_layout[pos].count = 8;
  11520. 				memcpy(skill_unit_layout[pos].dx,dx2,sizeof(dx2));
  11521. 				memcpy(skill_unit_layout[pos].dy,dy2,sizeof(dy2));
  11522. 				skill_db[i].unit_layout_type[j] = pos;
  11523. 				skill_db[i].unit_layout_type[++j] = pos;
  11524. 				//lv4/5
  11525. 				j++;
  11526. 				pos++;
  11527. 				skill_unit_layout[pos].count = 12;
  11528. 				memcpy(skill_unit_layout[pos].dx,dx3,sizeof(dx3));
  11529. 				memcpy(skill_unit_layout[pos].dy,dy3,sizeof(dy3));
  11530. 				skill_db[i].unit_layout_type[j] = pos;
  11531. 				skill_db[i].unit_layout_type[++j] = pos;
  11532. 				//Fill in the rest using lv 5.
  11533. 				for (;j<MAX_SKILL_LEVEL;j++)
  11534. 					skill_db[i].unit_layout_type[j] = pos;
  11535. 				//Skip, this way the check below will fail and continue to the next skill.
  11536. 				pos++;
  11537. 				break;
  11538. 			}
  11539. 			default:
  11540. 				ShowError("unknown unit layout at skill %d\n",i);
  11541. 				break;
  11542. 		}
  11543. 		if (!skill_unit_layout[pos].count)
  11544. 			continue;
  11545. 		for (j=0;j<MAX_SKILL_LEVEL;j++)
  11546. 			skill_db[i].unit_layout_type[j] = pos;
  11547. 		pos++;
  11548. 	}
  11549.  
  11550. 	// firewall and icewall have 8 layouts (direction-dependent)
  11551. 	firewall_unit_pos = pos;
  11552. 	for (i=0;i<8;i++) {
  11553. 		if (i&1) {
  11554. 			skill_unit_layout[pos].count = 5;
  11555. 			if (i&0x2) {
  11556. 				int dx[] = {-1,-1, 0, 0, 1};
  11557. 				int dy[] = { 1, 0, 0,-1,-1};
  11558. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11559. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11560. 			} else {
  11561. 				int dx[] = { 1, 1 ,0, 0,-1};
  11562. 				int dy[] = { 1, 0, 0,-1,-1};
  11563. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11564. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11565. 			}
  11566. 		} else {
  11567. 			skill_unit_layout[pos].count = 3;
  11568. 			if (i%4==0) {
  11569. 				int dx[] = {-1, 0, 1};
  11570. 				int dy[] = { 0, 0, 0};
  11571. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11572. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11573. 			} else {
  11574. 				int dx[] = { 0, 0, 0};
  11575. 				int dy[] = {-1, 0, 1};
  11576. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11577. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11578. 			}
  11579. 		}
  11580. 		pos++;
  11581. 	}
  11582. 	icewall_unit_pos = pos;
  11583. 	for (i=0;i<8;i++) {
  11584. 		skill_unit_layout[pos].count = 5;
  11585. 		if (i&1) {
  11586. 			if (i&0x2) {
  11587. 				int dx[] = {-2,-1, 0, 1, 2};
  11588. 				int dy[] = { 2, 1, 0,-1,-2};
  11589. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11590. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11591. 			} else {
  11592. 				int dx[] = { 2, 1 ,0,-1,-2};
  11593. 				int dy[] = { 2, 1, 0,-1,-2};
  11594. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11595. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11596. 			}
  11597. 		} else {
  11598. 			if (i%4==0) {
  11599. 				int dx[] = {-2,-1, 0, 1, 2};
  11600. 				int dy[] = { 0, 0, 0, 0, 0};
  11601. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11602. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11603. 			} else {
  11604. 				int dx[] = { 0, 0, 0, 0, 0};
  11605. 				int dy[] = {-2,-1, 0, 1, 2};
  11606. 				memcpy(skill_unit_layout[pos].dx,dx,sizeof(dx));
  11607. 				memcpy(skill_unit_layout[pos].dy,dy,sizeof(dy));
  11608. 			}
  11609. 		}
  11610. 		pos++;
  11611. 	}
  11612. }
  11613.  
  11614. /*==========================================
  11615.  * DB reading.
  11616.  * skill_db.txt
  11617.  * skill_require_db.txt
  11618.  * skill_cast_db.txt
  11619.  * skill_castnodex_db.txt
  11620.  * skill_nocast_db.txt
  11621.  * skill_unit_db.txt
  11622.  * produce_db.txt
  11623.  * create_arrow_db.txt
  11624.  * abra_db.txt
  11625.  *------------------------------------------*/
  11626.  
  11627. static bool skill_parse_row_skilldb(char* split[], int columns, int current)
  11628. {// id,range,hit,inf,element,nk,splash,max,list_num,castcancel,cast_defence_rate,inf2,maxcount,skill_type,blow_count,name,description
  11629. 	int id = atoi(split[0]);
  11630. 	int i;
  11631. 	if( (id >= GD_SKILLRANGEMIN && id <= GD_SKILLRANGEMAX)
  11632. 	||  (id >= HM_SKILLRANGEMIN && id <= HM_SKILLRANGEMAX)
  11633. 	||  (id >= MC_SKILLRANGEMIN && id <= MC_SKILLRANGEMAX) )
  11634. 	{
  11635. 		ShowWarning("skill_parse_row_skilldb: Skill id %d is forbidden (interferes with guild/homun/mercenary skill mapping)!\n", id);
  11636. 		return false;
  11637. 	}
  11638.  
  11639. 	i = skill_get_index(id);
  11640. 	if( !i ) // invalid skill id
  11641. 		return false;
  11642.  
  11643. 	skill_split_atoi(split[1],skill_db[i].range);
  11644. 	skill_db[i].hit = atoi(split[2]);
  11645. 	skill_db[i].inf = atoi(split[3]);
  11646. 	skill_split_atoi(split[4],skill_db[i].element);
  11647. 	skill_db[i].nk = (int)strtol(split[5], NULL, 0);
  11648. 	skill_split_atoi(split[6],skill_db[i].splash);
  11649. 	skill_db[i].max = atoi(split[7]);
  11650. 	skill_split_atoi(split[8],skill_db[i].num);
  11651.  
  11652. 	if( strcmpi(split[9],"yes") == 0 )
  11653. 		skill_db[i].castcancel = 1;
  11654. 	else
  11655. 		skill_db[i].castcancel = 0;
  11656. 	skill_db[i].cast_def_rate = atoi(split[10]);
  11657. 	skill_db[i].inf2 = (int)strtol(split[11], NULL, 0);
  11658. 	skill_split_atoi(split[12],skill_db[i].maxcount);
  11659. 	if( strcmpi(split[13],"weapon") == 0 )
  11660. 		skill_db[i].skill_type = BF_WEAPON;
  11661. 	else if( strcmpi(split[13],"magic") == 0 )
  11662. 		skill_db[i].skill_type = BF_MAGIC;
  11663. 	else if( strcmpi(split[13],"misc") == 0 )
  11664. 		skill_db[i].skill_type = BF_MISC;
  11665. 	else
  11666. 		skill_db[i].skill_type = 0;
  11667. 	skill_split_atoi(split[14],skill_db[i].blewcount);
  11668. 	safestrncpy(skill_db[i].name, trim(split[15]), sizeof(skill_db[i].name));
  11669. 	safestrncpy(skill_db[i].desc, trim(split[16]), sizeof(skill_db[i].desc));
  11670. 	strdb_put(skilldb_name2id, skill_db[i].name, (void*)id);
  11671.  
  11672. 	return true;
  11673. }
  11674.  
  11675. static bool skill_parse_row_requiredb(char* split[], int columns, int current)
  11676. {// 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
  11677. 	char* p;
  11678. 	int j;
  11679.  
  11680. 	int i = atoi(split[0]);
  11681. 	i = skill_get_index(i);
  11682. 	if( !i ) // invalid skill id
  11683. 		return false;
  11684.  
  11685. 	skill_split_atoi(split[1],skill_db[i].hp);
  11686. 	skill_split_atoi(split[2],skill_db[i].mhp);
  11687. 	skill_split_atoi(split[3],skill_db[i].sp);
  11688. 	skill_split_atoi(split[4],skill_db[i].hp_rate);
  11689. 	skill_split_atoi(split[5],skill_db[i].sp_rate);
  11690. 	skill_split_atoi(split[6],skill_db[i].zeny);
  11691.  
  11692. 	//FIXME: document this
  11693. 	p = split[7];
  11694. 	for( j = 0; j < 32; j++ )
  11695. 	{
  11696. 		int l = atoi(p);
  11697. 		if( l == 99 ) // Any weapon
  11698. 		{
  11699. 			skill_db[i].weapon = 0;
  11700. 			break;
  11701. 		}
  11702. 		else
  11703. 			skill_db[i].weapon |= 1<<l;
  11704. 		p = strchr(p,':');
  11705. 		if(!p)
  11706. 			break;
  11707. 		p++;
  11708. 	}
  11709.  
  11710. 	//FIXME: document this
  11711. 	p = split[8];
  11712. 	for( j = 0; j < 32; j++ )
  11713. 	{
  11714. 		int l = atoi(p);
  11715. 		if( l == 99 ) // Any ammo type
  11716. 		{
  11717. 			skill_db[i].ammo = 0xFFFFFFFF;
  11718. 			break;
  11719. 		}
  11720. 		else if( l ) // 0 stands for no requirement
  11721. 			skill_db[i].ammo |= 1<<l;
  11722. 		p = strchr(p,':');
  11723. 		if( !p )
  11724. 			break;
  11725. 		p++;
  11726. 	}
  11727. 	skill_split_atoi(split[9],skill_db[i].ammo_qty);
  11728.  
  11729. 	if(      strcmpi(split[10],"hiding")==0 ) skill_db[i].state = ST_HIDING;
  11730. 	else if( strcmpi(split[10],"cloaking")==0 ) skill_db[i].state = ST_CLOAKING;
  11731. 	else if( strcmpi(split[10],"hidden")==0 ) skill_db[i].state = ST_HIDDEN;
  11732. 	else if( strcmpi(split[10],"riding")==0 ) skill_db[i].state = ST_RIDING;
  11733. 	else if( strcmpi(split[10],"falcon")==0 ) skill_db[i].state = ST_FALCON;
  11734. 	else if( strcmpi(split[10],"cart")==0 ) skill_db[i].state = ST_CART;
  11735. 	else if( strcmpi(split[10],"shield")==0 ) skill_db[i].state = ST_SHIELD;
  11736. 	else if( strcmpi(split[10],"sight")==0 ) skill_db[i].state = ST_SIGHT;
  11737. 	else if( strcmpi(split[10],"explosionspirits")==0 ) skill_db[i].state = ST_EXPLOSIONSPIRITS;
  11738. 	else if( strcmpi(split[10],"cartboost")==0 ) skill_db[i].state = ST_CARTBOOST;
  11739. 	else if( strcmpi(split[10],"recover_weight_rate")==0 ) skill_db[i].state = ST_RECOV_WEIGHT_RATE;
  11740. 	else if( strcmpi(split[10],"move_enable")==0 ) skill_db[i].state = ST_MOVE_ENABLE;
  11741. 	else if( strcmpi(split[10],"water")==0 ) skill_db[i].state = ST_WATER;
  11742. 	else skill_db[i].state = ST_NONE;
  11743.  
  11744. 	skill_split_atoi(split[11],skill_db[i].spiritball);
  11745. 	for( j = 0; j < MAX_SKILL_ITEM_REQUIRE; j++ ) {
  11746. 		skill_db[i].itemid[j] = atoi(split[12+ 2*j]);
  11747. 		skill_db[i].amount[j] = atoi(split[13+ 2*j]);
  11748. 	}
  11749.  
  11750. 	return true;
  11751. }
  11752.  
  11753. static bool skill_parse_row_castdb(char* split[], int columns, int current)
  11754. {// SkillID,CastingTime,AfterCastActDelay,AfterCastWalkDelay,Duration1,Duration2
  11755. 	int i = atoi(split[0]);
  11756. 	i = skill_get_index(i);
  11757. 	if( !i ) // invalid skill id
  11758. 		return false;
  11759.  
  11760. 	skill_split_atoi(split[1],skill_db[i].cast);
  11761. 	skill_split_atoi(split[2],skill_db[i].delay);
  11762. 	skill_split_atoi(split[3],skill_db[i].walkdelay);
  11763. 	skill_split_atoi(split[4],skill_db[i].upkeep_time);
  11764. 	skill_split_atoi(split[5],skill_db[i].upkeep_time2);
  11765.  
  11766. 	return true;
  11767. }
  11768.  
  11769. static bool skill_parse_row_castnodexdb(char* split[], int columns, int current)
  11770. {// Skill id,Cast,Delay (optional)
  11771. 	int i = atoi(split[0]);
  11772. 	i = skill_get_index(i);
  11773. 	if( !i ) // invalid skill id
  11774. 		return false;
  11775.  
  11776. 	skill_split_atoi(split[1],skill_db[i].castnodex);
  11777. 	if( split[2] ) // optional column
  11778. 		skill_split_atoi(split[2],skill_db[i].delaynodex);
  11779.  
  11780. 	return true;
  11781. }
  11782.  
  11783. static bool skill_parse_row_nocastdb(char* split[], int columns, int current)
  11784. {// SkillID,Flag
  11785. 	int i = atoi(split[0]);
  11786. 	i = skill_get_index(i);
  11787. 	if( !i ) // invalid skill id
  11788. 		return false;
  11789.  
  11790. 	skill_db[i].nocast |= atoi(split[1]);
  11791.  
  11792. 	return true;
  11793. }
  11794.  
  11795. static bool skill_parse_row_unitdb(char* split[], int columns, int current)
  11796. {// ID,unit ID,unit ID 2,layout,range,interval,target,flag
  11797. 	int i = atoi(split[0]);
  11798. 	i = skill_get_index(i);
  11799. 	if( !i ) // invalid skill id
  11800. 		return false;
  11801.  
  11802. 	skill_db[i].unit_id[0] = strtol(split[1],NULL,16);
  11803. 	skill_db[i].unit_id[1] = strtol(split[2],NULL,16);
  11804. 	skill_split_atoi(split[3],skill_db[i].unit_layout_type);
  11805. 	skill_split_atoi(split[4],skill_db[i].unit_range);
  11806. 	skill_db[i].unit_interval = atoi(split[5]);
  11807.  
  11808. 	if( strcmpi(split[6],"noenemy")==0 ) skill_db[i].unit_target = BCT_NOENEMY;
  11809. 	else if( strcmpi(split[6],"friend")==0 ) skill_db[i].unit_target = BCT_NOENEMY;
  11810. 	else if( strcmpi(split[6],"party")==0 ) skill_db[i].unit_target = BCT_PARTY;
  11811. 	else if( strcmpi(split[6],"ally")==0 ) skill_db[i].unit_target = BCT_PARTY|BCT_GUILD;
  11812. 	else if( strcmpi(split[6],"all")==0 ) skill_db[i].unit_target = BCT_ALL;
  11813. 	else if( strcmpi(split[6],"enemy")==0 ) skill_db[i].unit_target = BCT_ENEMY;
  11814. 	else if( strcmpi(split[6],"self")==0 ) skill_db[i].unit_target = BCT_SELF;
  11815. 	else if( strcmpi(split[6],"noone")==0 ) skill_db[i].unit_target = BCT_NOONE;
  11816. 	else skill_db[i].unit_target = strtol(split[6],NULL,16);
  11817.  
  11818. 	skill_db[i].unit_flag = strtol(split[7],NULL,16);
  11819.  
  11820. 	if (skill_db[i].unit_flag&UF_DEFNOTENEMY && battle_config.defnotenemy)
  11821. 		skill_db[i].unit_target = BCT_NOENEMY;
  11822.  
  11823. 	//By default, target just characters.
  11824. 	skill_db[i].unit_target |= BL_CHAR;
  11825. 	if (skill_db[i].unit_flag&UF_NOPC)
  11826. 		skill_db[i].unit_target &= ~BL_PC;
  11827. 	if (skill_db[i].unit_flag&UF_NOMOB)
  11828. 		skill_db[i].unit_target &= ~BL_MOB;
  11829. 	if (skill_db[i].unit_flag&UF_SKILL)
  11830. 		skill_db[i].unit_target |= BL_SKILL;
  11831.  
  11832. 	return true;
  11833. }
  11834.  
  11835. static bool skill_parse_row_producedb(char* split[], int columns, int current)
  11836. {// ProduceItemID,ItemLV,RequireSkill,RequireSkillLv,MaterialID1,MaterialAmount1,......
  11837. 	int x,y;
  11838.  
  11839. 	int i = atoi(split[0]);
  11840. 	if( !i )
  11841. 		return false;
  11842.  
  11843. 	skill_produce_db[current].nameid = i;
  11844. 	skill_produce_db[current].itemlv = atoi(split[1]);
  11845. 	skill_produce_db[current].req_skill = atoi(split[2]);
  11846. 	skill_produce_db[current].req_skill_lv = atoi(split[3]);
  11847.  
  11848. 	for( x = 4, y = 0; x+1 < columns && split[x] && split[x+1] && y < MAX_PRODUCE_RESOURCE; x += 2, y++ )
  11849. 	{
  11850. 		skill_produce_db[current].mat_id[y] = atoi(split[x]);
  11851. 		skill_produce_db[current].mat_amount[y] = atoi(split[x+1]);
  11852. 	}
  11853.  
  11854. 	return true;
  11855. }
  11856.  
  11857. static bool skill_parse_row_createarrowdb(char* split[], int columns, int current)
  11858. {// SourceID,MakeID1,MakeAmount1,...,MakeID5,MakeAmount5
  11859. 	int x,y;
  11860.  
  11861. 	int i = atoi(split[0]);
  11862. 	if( !i )
  11863. 		return false;
  11864.  
  11865. 	skill_arrow_db[current].nameid = i;
  11866.  
  11867. 	for( x = 1, y = 0; x+1 < columns && split[x] && split[x+1] && y < MAX_ARROW_RESOURCE; x += 2, y++ )
  11868. 	{
  11869. 		skill_arrow_db[current].cre_id[y] = atoi(split[x]);
  11870. 		skill_arrow_db[current].cre_amount[y] = atoi(split[x+1]);
  11871. 	}
  11872.  
  11873. 	return true;
  11874. }
  11875.  
  11876. static bool skill_parse_row_abradb(char* split[], int columns, int current)
  11877. {// SkillID,DummyName,RequiredHocusPocusLevel,Rate
  11878. 	int i = atoi(split[0]);
  11879. 	if( !skill_get_index(i) || !skill_get_max(i) )
  11880. 	{
  11881. 		ShowError("abra_db: Invalid skill ID %d\n", i);
  11882. 		return false;
  11883. 	}
  11884. 	if ( !skill_get_inf(i) )
  11885. 	{
  11886. 		ShowError("abra_db: Passive skills cannot be casted (%d/%s)\n", i, skill_get_name(i));
  11887. 		return false;
  11888. 	}
  11889.  
  11890. 	skill_abra_db[current].skillid = i;
  11891. 	skill_abra_db[current].req_lv = atoi(split[2]);
  11892. 	skill_abra_db[current].per = atoi(split[3]);
  11893.  
  11894. 	return true;
  11895. }
  11896.  
  11897. static void skill_readdb(void)
  11898. {
  11899. 	// init skill db structures
  11900. 	db_clear(skilldb_name2id);
  11901. 	memset(skill_db,0,sizeof(skill_db));
  11902. 	memset(skill_produce_db,0,sizeof(skill_produce_db));
  11903. 	memset(skill_arrow_db,0,sizeof(skill_arrow_db));
  11904. 	memset(skill_abra_db,0,sizeof(skill_abra_db));
  11905.  
  11906. 	// load skill databases
  11907. 	safestrncpy(skill_db[0].name, "UNKNOWN_SKILL", sizeof(skill_db[0].name));
  11908. 	safestrncpy(skill_db[0].desc, "Unknown Skill", sizeof(skill_db[0].desc));
  11909. 	sv_readdb(db_path, "skill_db.txt"          , ',',  17, 17, MAX_SKILL_DB, skill_parse_row_skilldb);
  11910. 	sv_readdb(db_path, "skill_require_db.txt"  , ',',  32, 32, MAX_SKILL_DB, skill_parse_row_requiredb);
  11911. 	sv_readdb(db_path, "skill_cast_db.txt"     , ',',   6,  6, MAX_SKILL_DB, skill_parse_row_castdb);
  11912. 	sv_readdb(db_path, "skill_castnodex_db.txt", ',',   2,  3, MAX_SKILL_DB, skill_parse_row_castnodexdb);
  11913. 	sv_readdb(db_path, "skill_nocast_db.txt"   , ',',   2,  2, MAX_SKILL_DB, skill_parse_row_nocastdb);
  11914. 	sv_readdb(db_path, "skill_unit_db.txt"     , ',',   8,  8, MAX_SKILL_DB, skill_parse_row_unitdb);
  11915. 	skill_init_unit_layout();
  11916. 	sv_readdb(db_path, "produce_db.txt"        , ',',   4,  4+2*MAX_PRODUCE_RESOURCE, MAX_SKILL_PRODUCE_DB, skill_parse_row_producedb);
  11917. 	sv_readdb(db_path, "create_arrow_db.txt"   , ',', 1+2,  1+2*MAX_ARROW_RESOURCE, MAX_SKILL_ARROW_DB, skill_parse_row_createarrowdb);
  11918. 	sv_readdb(db_path, "abra_db.txt"           , ',',   4,  4, MAX_SKILL_ABRA_DB, skill_parse_row_abradb);
  11919. }
  11920.  
  11921. void skill_reload (void)
  11922. {
  11923. 	skill_readdb();
  11924. }
  11925.  
  11926. /*==========================================
  11927.  *
  11928.  *------------------------------------------*/
  11929. int do_init_skill (void)
  11930. {
  11931. 	skilldb_name2id = strdb_alloc(DB_OPT_DUP_KEY, 0);
  11932. 	skill_readdb();
  11933.  
  11934. 	group_db = idb_alloc(DB_OPT_BASE);
  11935. 	skillunit_db = idb_alloc(DB_OPT_BASE);
  11936. 	skill_unit_ers = ers_new(sizeof(struct skill_unit_group));
  11937. 	skill_timer_ers  = ers_new(sizeof(struct skill_timerskill));
  11938.  
  11939. 	add_timer_func_list(skill_unit_timer,"skill_unit_timer");
  11940. 	add_timer_func_list(skill_castend_id,"skill_castend_id");
  11941. 	add_timer_func_list(skill_castend_pos,"skill_castend_pos");
  11942. 	add_timer_func_list(skill_timerskill,"skill_timerskill");
  11943. 	add_timer_func_list(skill_blockpc_end, "skill_blockpc_end");
  11944.  
  11945. 	add_timer_interval(gettick()+SKILLUNITTIMER_INTERVAL,skill_unit_timer,0,0,SKILLUNITTIMER_INTERVAL);
  11946.  
  11947. 	return 0;
  11948. }
  11949.  
  11950. int do_final_skill(void)
  11951. {
  11952. 	db_destroy(skilldb_name2id);
  11953. 	db_destroy(group_db);
  11954. 	db_destroy(skillunit_db);
  11955. 	ers_destroy(skill_unit_ers);
  11956. 	ers_destroy(skill_timer_ers);
  11957. 	return 0;
  11958. }
Viewed 1616 times, submitted by Guest.