viewing paste Unknown #11903 | C

Posted on the
  1. // Copyright (c) Hercules Dev Team, licensed under GNU GPL.
  2. // See the LICENSE file
  3. // Portions Copyright (c) Athena Dev Teams
  4.  
  5. #include "../common/cbasetypes.h"
  6. #include "../common/timer.h"
  7. #include "../common/nullpo.h"
  8. #include "../common/malloc.h"
  9. #include "../common/showmsg.h"
  10. #include "../common/ers.h"
  11. #include "../common/random.h"
  12. #include "../common/socket.h"
  13. #include "../common/strlib.h"
  14. #include "../common/utils.h"
  15.  
  16. #include "map.h"
  17. #include "path.h"
  18. #include "pc.h"
  19. #include "status.h"
  20. #include "skill.h"
  21. #include "homunculus.h"
  22. #include "mercenary.h"
  23. #include "elemental.h"
  24. #include "mob.h"
  25. #include "itemdb.h"
  26. #include "clif.h"
  27. #include "pet.h"
  28. #include "guild.h"
  29. #include "party.h"
  30. #include "battle.h"
  31. #include "battleground.h"
  32. #include "chrif.h"
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <math.h>
  38.  
  39. struct Battle_Config battle_config;
  40. struct battle_interface battle_s;
  41.  
  42. int battle_getcurrentskill(struct block_list *bl) { //Returns the current/last skill in use by this bl.
  43. 	struct unit_data *ud;
  44.  
  45. 	if( bl->type == BL_SKILL ) {
  46. 		struct skill_unit * su = (struct skill_unit*)bl;
  47. 		return su->group?su->group->skill_id:0;
  48. 	}
  49.  
  50. 	ud = unit->bl2ud(bl);
  51.  
  52. 	return ud?ud->skill_id:0;
  53. }
  54.  
  55. /*==========================================
  56.  * Get random targetting enemy
  57.  *------------------------------------------*/
  58. int battle_gettargeted_sub(struct block_list *bl, va_list ap) {
  59. 	struct block_list **bl_list;
  60. 	struct unit_data *ud;
  61. 	int target_id;
  62. 	int *c;
  63.  
  64. 	bl_list = va_arg(ap, struct block_list **);
  65. 	c = va_arg(ap, int *);
  66. 	target_id = va_arg(ap, int);
  67.  
  68. 	if (bl->id == target_id)
  69. 		return 0;
  70.  
  71. 	if (*c >= 24)
  72. 		return 0;
  73.  
  74. 	if ( !(ud = unit->bl2ud(bl)) )
  75. 		return 0;
  76.  
  77. 	if (ud->target == target_id || ud->skilltarget == target_id) {
  78. 		bl_list[(*c)++] = bl;
  79. 		return 1;
  80. 	}
  81.  
  82. 	return 0;
  83. }
  84.  
  85. struct block_list* battle_gettargeted(struct block_list *target) {
  86. 	struct block_list *bl_list[24];
  87. 	int c = 0;
  88. 	nullpo_retr(NULL, target);
  89.  
  90. 	memset(bl_list, 0, sizeof(bl_list));
  91. 	map->foreachinrange(battle->get_targeted_sub, target, AREA_SIZE, BL_CHAR, bl_list, &c, target->id);
  92. 	if ( c == 0 )
  93. 		return NULL;
  94. 	if( c > 24 )
  95. 		c = 24;
  96. 	return bl_list[rnd()%c];
  97. }
  98.  
  99.  
  100. //Returns the id of the current targetted character of the passed bl. [Skotlex]
  101. int battle_gettarget(struct block_list* bl) {
  102.  
  103. 	switch (bl->type) {
  104. 		case BL_PC:  return ((struct map_session_data*)bl)->ud.target;
  105. 		case BL_MOB: return ((struct mob_data*)bl)->target_id;
  106. 		case BL_PET: return ((struct pet_data*)bl)->target_id;
  107. 		case BL_HOM: return ((struct homun_data*)bl)->ud.target;
  108. 		case BL_MER: return ((struct mercenary_data*)bl)->ud.target;
  109. 		case BL_ELEM: return ((struct elemental_data*)bl)->ud.target;
  110. 	}
  111.  
  112. 	return 0;
  113. }
  114.  
  115. int battle_getenemy_sub(struct block_list *bl, va_list ap) {
  116. 	struct block_list **bl_list;
  117. 	struct block_list *target;
  118. 	int *c;
  119.  
  120. 	bl_list = va_arg(ap, struct block_list **);
  121. 	c = va_arg(ap, int *);
  122. 	target = va_arg(ap, struct block_list *);
  123.  
  124. 	if (bl->id == target->id)
  125. 		return 0;
  126.  
  127. 	if (*c >= 24)
  128. 		return 0;
  129.  
  130. 	if (status->isdead(bl))
  131. 		return 0;
  132.  
  133. 	if (battle->check_target(target, bl, BCT_ENEMY) > 0) {
  134. 		bl_list[(*c)++] = bl;
  135. 		return 1;
  136. 	}
  137.  
  138. 	return 0;
  139. }
  140.  
  141. // Picks a random enemy of the given type (BL_PC, BL_CHAR, etc) within the range given. [Skotlex]
  142. struct block_list* battle_getenemy(struct block_list *target, int type, int range) {
  143. 	struct block_list *bl_list[24];
  144. 	int c = 0;
  145.  
  146. 	memset(bl_list, 0, sizeof(bl_list));
  147. 	map->foreachinrange(battle->get_enemy_sub, target, range, type, bl_list, &c, target);
  148.  
  149. 	if ( c == 0 )
  150. 		return NULL;
  151.  
  152. 	if( c > 24 )
  153. 		c = 24;
  154.  
  155. 	return bl_list[rnd()%c];
  156. }
  157. int battle_getenemyarea_sub(struct block_list *bl, va_list ap) {
  158. 	struct block_list **bl_list, *src;
  159. 	int *c, ignore_id;
  160.  
  161. 	bl_list = va_arg(ap, struct block_list **);
  162. 	c = va_arg(ap, int *);
  163. 	src = va_arg(ap, struct block_list *);
  164. 	ignore_id = va_arg(ap, int);
  165.  
  166. 	if( bl->id == src->id || bl->id == ignore_id )
  167. 		return 0; // Ignores Caster and a possible pre-target
  168.  
  169. 	if( *c >= 23 )
  170. 		return 0;
  171.  
  172. 	if( status->isdead(bl) )
  173. 		return 0;
  174.  
  175. 	if( battle->check_target(src, bl, BCT_ENEMY) > 0 ) {// Is Enemy!...
  176. 		bl_list[(*c)++] = bl;
  177. 		return 1;
  178. 	}
  179.  
  180. 	return 0;
  181. }
  182.  
  183. // Pick a random enemy
  184. struct block_list* battle_getenemyarea(struct block_list *src, int x, int y, int range, int type, int ignore_id) {
  185. 	struct block_list *bl_list[24];
  186. 	int c = 0;
  187.  
  188. 	memset(bl_list, 0, sizeof(bl_list));
  189. 	map->foreachinarea(battle->get_enemy_area_sub, src->m, x - range, y - range, x + range, y + range, type, bl_list, &c, src, ignore_id);
  190.  
  191. 	if( c == 0 )
  192. 		return NULL;
  193. 	if( c >= 24 )
  194. 		c = 23;
  195.  
  196. 	return bl_list[rnd()%c];
  197. }
  198.  
  199. int battle_delay_damage_sub(int tid, unsigned int tick, int id, intptr_t data) {
  200. 	struct delay_damage *dat = (struct delay_damage *)data;
  201.  
  202. 	if ( dat ) {
  203. 		struct block_list* src = NULL;
  204. 		struct block_list* target = map->id2bl(dat->target_id);
  205.  
  206. 		if( !target || status->isdead(target) ) {/* nothing we can do */
  207. 			if( dat->src_type == BL_PC && ( src = map->id2bl(dat->src_id) ) && --((TBL_PC*)src)->delayed_damage == 0 && ((TBL_PC*)src)->state.hold_recalc ) {
  208. 				((TBL_PC*)src)->state.hold_recalc = 0;
  209. 				status_calc_pc(((TBL_PC*)src),0);
  210. 			}
  211. 			ers_free(battle->delay_damage_ers, dat);
  212. 			return 0;
  213. 		}
  214.  
  215. 		src = map->id2bl(dat->src_id);
  216.  
  217. 		//Check to see if you haven't teleported. [Skotlex]
  218. 		if( src && target->m == src->m
  219. 		 && (target->type != BL_PC || ((TBL_PC*)target)->invincible_timer == INVALID_TIMER)
  220. 		 && check_distance_bl(src, target, dat->distance)
  221. 		) {
  222. 			map->freeblock_lock();
  223. 			status_fix_damage(src, target, dat->damage, dat->delay);
  224. 			if( dat->attack_type && !status->isdead(target) && dat->additional_effects )
  225. 				skill->additional_effect(src,target,dat->skill_id,dat->skill_lv,dat->attack_type,dat->dmg_lv,tick);
  226. 			if( dat->dmg_lv > ATK_BLOCK && dat->attack_type )
  227. 				skill->counter_additional_effect(src,target,dat->skill_id,dat->skill_lv,dat->attack_type,tick);
  228. 			map->freeblock_unlock();
  229. 		} else if( !src && dat->skill_id == CR_REFLECTSHIELD ) {
  230. 			/**
  231. 			 * it was monster reflected damage, and the monster died, we pass the damage to the character as expected
  232. 			 **/
  233. 			map->freeblock_lock();
  234. 			status_fix_damage(target, target, dat->damage, dat->delay);
  235. 			map->freeblock_unlock();
  236. 		}
  237.  
  238. 		if( src && src->type == BL_PC && --((TBL_PC*)src)->delayed_damage == 0 && ((TBL_PC*)src)->state.hold_recalc ) {
  239. 			((TBL_PC*)src)->state.hold_recalc = 0;
  240. 			status_calc_pc(((TBL_PC*)src),0);
  241. 		}
  242. 	}
  243. 	ers_free(battle->delay_damage_ers, dat);
  244. 	return 0;
  245. }
  246.  
  247. int battle_delay_damage (unsigned int tick, int amotion, struct block_list *src, struct block_list *target, int attack_type, uint16 skill_id, uint16 skill_lv, int64 damage, enum damage_lv dmg_lv, int ddelay, bool additional_effects) {
  248. 	struct delay_damage *dat;
  249. 	struct status_change *sc;
  250. 	nullpo_ret(src);
  251. 	nullpo_ret(target);
  252.  
  253. 	sc = status->get_sc(target);
  254.  
  255. 	if( sc && sc->data[SC_DEVOTION] && damage > 0 && skill_id != PA_PRESSURE && skill_id != CR_REFLECTSHIELD )
  256. 		damage = 0;
  257.  
  258. 	if ( !battle_config.delay_battle_damage || amotion <= 1 ) {
  259. 		map->freeblock_lock();
  260. 		status_fix_damage(src, target, damage, ddelay); // We have to seperate here between reflect damage and others [icescope]
  261. 		if( attack_type && !status->isdead(target) && additional_effects )
  262. 			skill->additional_effect(src, target, skill_id, skill_lv, attack_type, dmg_lv, timer->gettick());
  263. 		if( dmg_lv > ATK_BLOCK && attack_type )
  264. 			skill->counter_additional_effect(src, target, skill_id, skill_lv, attack_type, timer->gettick());
  265. 		map->freeblock_unlock();
  266. 		return 0;
  267. 	}
  268. 	dat = ers_alloc(battle->delay_damage_ers, struct delay_damage);
  269. 	dat->src_id = src->id;
  270. 	dat->target_id = target->id;
  271. 	dat->skill_id = skill_id;
  272. 	dat->skill_lv = skill_lv;
  273. 	dat->attack_type = attack_type;
  274. 	dat->damage = damage;
  275. 	dat->dmg_lv = dmg_lv;
  276. 	dat->delay = ddelay;
  277. 	dat->distance = distance_bl(src, target)+10; //Attack should connect regardless unless you teleported.
  278. 	dat->additional_effects = additional_effects;
  279. 	dat->src_type = src->type;
  280. 	if (src->type != BL_PC && amotion > 1000)
  281. 		amotion = 1000; //Aegis places a damage-delay cap of 1 sec to non player attacks. [Skotlex]
  282.  
  283. 	if( src->type == BL_PC ) {
  284. 		((TBL_PC*)src)->delayed_damage++;
  285. 	}
  286.  
  287. 	timer->add(tick+amotion, battle->delay_damage_sub, 0, (intptr_t)dat);
  288.  
  289. 	return 0;
  290. }
  291. int battle_attr_ratio(int atk_elem,int def_type, int def_lv)
  292. {
  293.  
  294. 	if (atk_elem < 0 || atk_elem >= ELE_MAX)
  295. 		return 100;
  296.  
  297. 	if (def_type < 0 || def_type > ELE_MAX || def_lv < 1 || def_lv > 4)
  298. 		return 100;
  299.  
  300. 	return battle->attr_fix_table[def_lv-1][atk_elem][def_type];
  301. }
  302.  
  303. /*==========================================
  304.  * Does attribute fix modifiers.
  305.  * Added passing of the chars so that the status changes can affect it. [Skotlex]
  306.  * Note: Passing src/target == NULL is perfectly valid, it skips SC_ checks.
  307.  *------------------------------------------*/
  308. int64 battle_attr_fix(struct block_list *src, struct block_list *target, int64 damage,int atk_elem,int def_type, int def_lv)
  309. {
  310. 	struct status_change *sc=NULL, *tsc=NULL;
  311. 	int ratio;
  312.  
  313. 	if (src) sc = status->get_sc(src);
  314. 	if (target) tsc = status->get_sc(target);
  315.  
  316. 	if (atk_elem < 0 || atk_elem >= ELE_MAX)
  317. 		atk_elem = rnd()%ELE_MAX;
  318.  
  319. 	if (def_type < 0 || def_type > ELE_MAX ||
  320. 		def_lv < 1 || def_lv > 4) {
  321. 		ShowError("battle_attr_fix: unknown attr type: atk=%d def_type=%d def_lv=%d\n",atk_elem,def_type,def_lv);
  322. 		return damage;
  323. 	}
  324.  
  325. 	ratio = battle->attr_fix_table[def_lv-1][atk_elem][def_type];
  326. 	if (sc && sc->count) {
  327. 		if(sc->data[SC_VOLCANO] && atk_elem == ELE_FIRE)
  328. 			ratio += skill->enchant_eff[sc->data[SC_VOLCANO]->val1-1];
  329. 		if(sc->data[SC_VIOLENTGALE] && atk_elem == ELE_WIND)
  330. 			ratio += skill->enchant_eff[sc->data[SC_VIOLENTGALE]->val1-1];
  331. 		if(sc->data[SC_DELUGE] && atk_elem == ELE_WATER)
  332. 			ratio += skill->enchant_eff[sc->data[SC_DELUGE]->val1-1];
  333. 	}
  334. 	if( target && target->type == BL_SKILL ) {
  335. 		if( atk_elem == ELE_FIRE && battle->get_current_skill(target) == GN_WALLOFTHORN ) {
  336. 			struct skill_unit *su = (struct skill_unit*)target;
  337. 			struct skill_unit_group *sg;
  338. 			struct block_list *src;
  339.  
  340. 			if( !su || !su->alive
  341. 			 || (sg = su->group) == NULL || sg->val3 == -1
  342. 			 || (src = map->id2bl(sg->src_id)) == NULL || status->isdead(src)
  343. 			)
  344. 				return 0;
  345.  
  346. 			if( sg->unit_id != UNT_FIREWALL ) {
  347. 				int x,y;
  348. 				x = sg->val3 >> 16;
  349. 				y = sg->val3 & 0xffff;
  350. 				skill->unitsetting(src,su->group->skill_id,su->group->skill_lv,x,y,1);
  351. 				sg->val3 = -1;
  352. 				sg->limit = DIFF_TICK(timer->gettick(),sg->tick)+300;
  353. 			}
  354. 		}
  355. 	}
  356. 	if( tsc && tsc->count ) { //since an atk can only have one type let's optimise this a bit
  357. 		switch(atk_elem){
  358. 		case ELE_FIRE:
  359. 			if( tsc->data[SC_SPIDERWEB]) {
  360. 				tsc->data[SC_SPIDERWEB]->val1 = 0; // free to move now
  361. 				if( tsc->data[SC_SPIDERWEB]->val2-- > 0 )
  362. 					damage <<= 1; // double damage
  363. 				if( tsc->data[SC_SPIDERWEB]->val2 == 0 )
  364. 					status_change_end(target, SC_SPIDERWEB, INVALID_TIMER);
  365. 			}
  366. 			if( tsc->data[SC_THORNS_TRAP])
  367. 				status_change_end(target, SC_THORNS_TRAP, INVALID_TIMER);
  368. 			if( tsc->data[SC_FIRE_CLOAK_OPTION])
  369. 				damage -= damage * tsc->data[SC_FIRE_CLOAK_OPTION]->val2 / 100;
  370. 			if( tsc->data[SC_COLD] && target->type != BL_MOB)
  371. 				status_change_end(target, SC_COLD, INVALID_TIMER);
  372. 			if( tsc->data[SC_EARTH_INSIGNIA]) damage += damage/2;
  373. 			if( tsc->data[SC_VOLCANIC_ASH]) damage += damage/2; //150%
  374. 			break;
  375. 		case ELE_HOLY:
  376. 			if( tsc->data[SC_ORATIO]) ratio += tsc->data[SC_ORATIO]->val1 * 2;
  377. 			break;
  378. 		case ELE_POISON:
  379. 			if( tsc->data[SC_VENOMIMPRESS]) ratio += tsc->data[SC_VENOMIMPRESS]->val2;
  380. 			break;
  381. 		case ELE_WIND:
  382. 			if( tsc->data[SC_COLD] && target->type != BL_MOB) damage += damage/2;
  383. 			if( tsc->data[SC_WATER_INSIGNIA]) damage += damage/2;
  384. 			break;
  385. 		case ELE_WATER:
  386. 			if( tsc->data[SC_FIRE_INSIGNIA]) damage += damage/2;
  387. 			break;
  388. 		case ELE_EARTH:
  389. 			if( tsc->data[SC_WIND_INSIGNIA]) damage += damage/2;
  390. 			break;
  391. 		}
  392. 	} //end tsc check
  393. 	if( src && src->type == BL_PC ){
  394. 		struct map_session_data *sd = BL_CAST(BL_PC, src);
  395. 		int s;
  396.  
  397. 		ARR_FIND(1, 6, s, sd->charm[s] > 0);
  398.  
  399. 		if( s < 5 && atk_elem == s )
  400. 			ratio += sd->charm[s] * 2; // +2% custom value
  401. 	}
  402. 	if( target && target->type == BL_PC ) {
  403. 		struct map_session_data *tsd = BL_CAST(BL_PC, target);
  404. 		int t;
  405.  
  406. 		ARR_FIND(1, 6, t, tsd->charm[t] > 0);
  407.  
  408. 		if( t < 5 && atk_elem == t )
  409. 			damage -= damage * ( tsd->charm[t] * 3 ) / 100;// -3% custom value
  410. 	}
  411. 	return damage*ratio/100;
  412. }
  413. #ifdef RENEWAL
  414. int64 battle_calc_weapon_damage(struct block_list *src, struct block_list *bl, uint16 skill_id, uint16 skill_lv, struct weapon_atk *watk, int nk, bool n_ele, short s_ele, short s_ele_, int size, int type, int flag, int flag2){ // [malufett]
  415. 	int64 damage, eatk = 0;
  416. 	struct status_change *sc;
  417. 	struct map_session_data *sd;
  418.  
  419. 	if( !src || !bl )
  420. 		return 0;
  421.  
  422. 	sc = status->get_sc(src);
  423. 	sd = BL_CAST(BL_PC, src);
  424.  
  425. 	damage = status->get_weapon_atk(src, watk, flag);
  426.  
  427. 	if( sd ){
  428. 		if( type == EQI_HAND_R )
  429. 			damage = battle->calc_sizefix(sd, damage, EQI_HAND_R, size, flag&8);
  430. 		else
  431. 			damage = battle->calc_sizefix(sd, damage, EQI_HAND_L, size, flag&8);
  432.  
  433. 		if( flag&2 && sd->bonus.arrow_atk )
  434. 			damage += sd->bonus.arrow_atk;
  435.  
  436. 		if( sd->battle_status.equip_atk != 0 )
  437. 			eatk = sd->base_status.equip_atk;
  438. 	}
  439.  
  440. 	if( sc && sc->count ){
  441. 		if( sc->data[SC_ZENKAI] && watk->ele == sc->data[SC_ZENKAI]->val2 )
  442. 			eatk += 200;
  443. 	#ifdef RENEWAL_EDP	
  444. 		if( sc->data[SC_EDP] && skill_id != AS_GRIMTOOTH && skill_id != AS_VENOMKNIFE && skill_id != ASC_BREAKER ){
  445. 			eatk = eatk * sc->data[SC_EDP]->val4 / 100;
  446. 			damage += damage * sc->data[SC_EDP]->val3 / 100;
  447. 		}
  448. 	#endif
  449. 	}
  450.  
  451. 	if( skill_id != ASC_METEORASSAULT ){
  452. 		if( sc && sc->data[SC_SUB_WEAPONPROPERTY] ) // Temporary. [malufett]
  453. 			damage += damage * sc->data[SC_SUB_WEAPONPROPERTY]->val2 / 100;
  454. 	}
  455.  
  456. 	// Temporary. [malufett]
  457. 	damage = battle->calc_elefix(src, bl, skill_id, skill_lv, damage + eatk, nk, n_ele, s_ele, s_ele_, type == EQI_HAND_L, flag);
  458.  
  459. 	/**
  460. 	 * In RE Shield Bommerang takes weapon element only for damage calculation,
  461. 	 * - resist calculation is always against neutral
  462. 	**/
  463. 	if ( skill_id == CR_SHIELDBOOMERANG )
  464. 		s_ele = s_ele_ = ELE_NEUTRAL;
  465.  
  466. 	// attacker side
  467. 	damage = battle->calc_cardfix(BF_WEAPON, src, bl, nk, s_ele, s_ele_, damage, 2|(type == EQI_HAND_L), flag2);
  468.  
  469. 	// target side
  470. 	damage = battle->calc_cardfix(BF_WEAPON, src, bl, nk, s_ele, s_ele_, damage, 0, flag2);
  471.  
  472. 	return damage;
  473. }
  474. #endif
  475. /*==========================================
  476.  * Calculates the standard damage of a normal attack assuming it hits,
  477.  * it calculates nothing extra fancy, is needed for magnum break's WATK_ELEMENT bonus. [Skotlex]
  478.  *------------------------------------------
  479.  * Pass damage2 as NULL to not calc it.
  480.  * Flag values:
  481.  * &1: Critical hit
  482.  * &2: Arrow attack
  483.  * &4: Skill is Magic Crasher
  484.  * &8: Skip target size adjustment (Extremity Fist?)
  485.  *&16: Arrow attack but BOW, REVOLVER, RIFLE, SHOTGUN, GATLING or GRENADE type weapon not equipped (i.e. shuriken, kunai and venom knives not affected by DEX)
  486.  */
  487. #ifdef RENEWAL
  488. int64 battle_calc_base_damage(struct block_list *src, struct block_list *bl, uint16 skill_id, uint16 skill_lv, int nk, bool n_ele, short s_ele, short s_ele_, int type, int flag, int flag2) {
  489. 	int64 damage, batk;
  490. 	struct status_data *st = status->get_status_data(src);
  491.  
  492. 	batk = battle->calc_elefix(src, bl, skill_id, skill_lv, status->calc_batk(bl, status->get_sc(src), st->batk, false), nk, n_ele, ELE_NEUTRAL, ELE_NEUTRAL, false, flag);
  493.  
  494. 	if( type == EQI_HAND_L )
  495. 		damage = batk + 3 * battle->calc_weapon_damage(src, bl, skill_id, skill_lv, &st->lhw, nk, n_ele, s_ele, s_ele_, status_get_size(bl), type, flag, flag2) / 4;
  496. 	else
  497. 		damage = (batk << 1) + battle->calc_weapon_damage(src, bl, skill_id, skill_lv, &st->rhw, nk, n_ele, s_ele, s_ele_, status_get_size(bl), type, flag, flag2);
  498. #else
  499. static int64 battle_calc_base_damage(struct status_data *st, struct weapon_atk *wa, struct status_change *sc, unsigned short t_size, struct map_session_data *sd, int flag) {
  500. 	unsigned int atkmin=0, atkmax=0;
  501. 	short type = 0;
  502. 	int64 damage = 0;
  503.  
  504. 	if (!sd) { //Mobs/Pets
  505. 		if(flag&4) {
  506. 			atkmin = st->matk_min;
  507. 			atkmax = st->matk_max;
  508. 		} else {
  509. 			atkmin = wa->atk;
  510. 			atkmax = wa->atk2;
  511. 		}
  512. 		if (atkmin > atkmax)
  513. 			atkmin = atkmax;
  514. 	} else { //PCs
  515. 		atkmax = wa->atk;
  516. 		type = (wa == &st->lhw)?EQI_HAND_L:EQI_HAND_R;
  517.  
  518. 		if (!(flag&1) || (flag&2)) { //Normal attacks
  519. 			atkmin = st->dex;
  520.  
  521. 			if (sd->equip_index[type] >= 0 && sd->inventory_data[sd->equip_index[type]])
  522. 				atkmin = atkmin*(80 + sd->inventory_data[sd->equip_index[type]]->wlv*20)/100;
  523.  
  524. 			if (atkmin > atkmax)
  525. 				atkmin = atkmax;
  526.  
  527. 			if(flag&2 && !(flag&16)) { //Bows
  528. 				atkmin = atkmin*atkmax/100;
  529. 				if (atkmin > atkmax)
  530. 					atkmax = atkmin;
  531. 			}
  532. 		}
  533. 	}
  534.  
  535. 	if (sc && sc->data[SC_MAXIMIZEPOWER])
  536. 		atkmin = atkmax;
  537.  
  538. 	//Weapon Damage calculation
  539. 	if (!(flag&1))
  540. 		damage = (atkmax>atkmin? rnd()%(atkmax-atkmin):0)+atkmin;
  541. 	else
  542. 		damage = atkmax;
  543.  
  544. 	if (sd) {
  545. 		//rodatazone says the range is 0~arrow_atk-1 for non crit
  546. 		if (flag&2 && sd->bonus.arrow_atk)
  547. 			damage += ( (flag&1) ? sd->bonus.arrow_atk : rnd()%sd->bonus.arrow_atk );
  548.  
  549. 		//SizeFix only for players
  550. 		if (!(sd->special_state.no_sizefix || (flag&8)))
  551. 			damage = damage * ( type == EQI_HAND_L ? sd->left_weapon.atkmods[t_size] : sd->right_weapon.atkmods[t_size] ) / 100;
  552. 	}
  553.  
  554. 	//Finally, add baseatk
  555. 	if(flag&4)
  556. 		damage += st->matk_min;
  557. 	else
  558. 		damage += st->batk;
  559.  
  560. 	//rodatazone says that Overrefine bonuses are part of baseatk
  561. 	//Here we also apply the weapon_atk_rate bonus so it is correctly applied on left/right hands.
  562. 	if(sd) {
  563. 		if (type == EQI_HAND_L) {
  564. 			if(sd->left_weapon.overrefine)
  565. 				damage += rnd()%sd->left_weapon.overrefine+1;
  566. 			if (sd->weapon_atk_rate[sd->weapontype2])
  567. 				damage += damage * sd->weapon_atk_rate[sd->weapontype2] / 100;
  568. 		} else { //Right hand
  569. 			if(sd->right_weapon.overrefine)
  570. 				damage += rnd()%sd->right_weapon.overrefine+1;
  571. 			if (sd->weapon_atk_rate[sd->weapontype1])
  572. 				damage += damage * sd->weapon_atk_rate[sd->weapontype1] / 100;
  573. 		}
  574. 	}
  575. #endif
  576. 	return damage;
  577. }
  578.  
  579. int64 battle_calc_sizefix(struct map_session_data *sd, int64 damage, int type, int size,  bool ignore){
  580. 	//SizeFix only for players
  581. 	if (!(sd->special_state.no_sizefix || (ignore)))
  582. 		damage = damage * ( type == EQI_HAND_L ? sd->left_weapon.atkmods[size] : sd->right_weapon.atkmods[size] ) / 100;
  583. 	return damage;
  584. }
  585.  
  586. /*==========================================
  587.  * Passive skill damages increases
  588.  *------------------------------------------*/
  589. int64 battle_addmastery(struct map_session_data *sd,struct block_list *target,int64 dmg,int type) {
  590. 	int64 damage;
  591. 	struct status_data *st = status->get_status_data(target);
  592. 	int weapon, skill_lv;
  593. 	damage = dmg;
  594.  
  595. 	nullpo_ret(sd);
  596.  
  597. 	if((skill_lv = pc->checkskill(sd,AL_DEMONBANE)) > 0 &&
  598. 		target->type == BL_MOB && //This bonus doesnt work against players.
  599. 		(battle->check_undead(st->race,st->def_ele) || st->race==RC_DEMON) )
  600. 		damage += (int)(skill_lv*(3+sd->status.base_level/20.0));
  601. 		//damage += (skill_lv * 3);
  602. 	if( (skill_lv = pc->checkskill(sd, RA_RANGERMAIN)) > 0 && (st->race == RC_BRUTE || st->race == RC_PLANT || st->race == RC_FISH) )
  603. 		damage += (skill_lv * 5);
  604. 	if( (skill_lv = pc->checkskill(sd,NC_RESEARCHFE)) > 0 && (st->def_ele == ELE_FIRE || st->def_ele == ELE_EARTH) )
  605. 		damage += (skill_lv * 10);
  606. 	if( pc_ismadogear(sd) )
  607. 		damage += 20 + 20 * pc->checkskill(sd, NC_MADOLICENCE);
  608. #ifdef RENEWAL
  609. 	if( (skill_lv = pc->checkskill(sd,BS_WEAPONRESEARCH)) > 0 )
  610. 		damage += (skill_lv * 2);
  611. #endif
  612. 	if((skill_lv = pc->checkskill(sd,HT_BEASTBANE)) > 0 && (st->race==RC_BRUTE || st->race==RC_INSECT) ) {
  613. 		damage += (skill_lv * 4);
  614. 		if (sd->sc.data[SC_SOULLINK] && sd->sc.data[SC_SOULLINK]->val2 == SL_HUNTER)
  615. 			damage += sd->status.str;
  616. 	}
  617.  
  618. 	if(type == 0)
  619. 		weapon = sd->weapontype1;
  620. 	else
  621. 		weapon = sd->weapontype2;
  622. 	switch(weapon) {
  623. 		case W_1HSWORD:
  624. 			#ifdef RENEWAL
  625. 				if((skill_lv = pc->checkskill(sd,AM_AXEMASTERY)) > 0)
  626. 					damage += (skill_lv * 3);
  627. 			#endif
  628. 		case W_DAGGER:
  629. 			if((skill_lv = pc->checkskill(sd,SM_SWORD)) > 0)
  630. 				damage += (skill_lv * 4);
  631. 			if((skill_lv = pc->checkskill(sd,GN_TRAINING_SWORD)) > 0)
  632. 				damage += skill_lv * 10;
  633. 			break;
  634. 		case W_2HSWORD:
  635. 			#ifdef RENEWAL
  636. 				if((skill_lv = pc->checkskill(sd,AM_AXEMASTERY)) > 0)
  637. 					damage += (skill_lv * 3);
  638. 			#endif
  639. 			if((skill_lv = pc->checkskill(sd,SM_TWOHAND)) > 0)
  640. 				damage += (skill_lv * 4);
  641. 			break;
  642. 		case W_1HSPEAR:
  643. 		case W_2HSPEAR:
  644. 			if((skill_lv = pc->checkskill(sd,KN_SPEARMASTERY)) > 0) {
  645. 				if(pc_isridingdragon(sd))
  646. 					damage += (skill_lv * 10);
  647. 				else if(pc_isriding(sd))
  648. 					damage += (skill_lv * 5);
  649. 				else 
  650. 					damage += (skill_lv * 4);
  651. 			}
  652. 			break;
  653. 		case W_1HAXE:
  654. 		case W_2HAXE:
  655. 			if((skill_lv = pc->checkskill(sd,AM_AXEMASTERY)) > 0)
  656. 				damage += (skill_lv * 3);
  657. 			if((skill_lv = pc->checkskill(sd,NC_TRAININGAXE)) > 0)
  658. 				damage += (skill_lv * 5);
  659. 			break;
  660. 		case W_MACE:
  661. 		case W_2HMACE:
  662. 			if((skill_lv = pc->checkskill(sd,PR_MACEMASTERY)) > 0)
  663. 				damage += (skill_lv * 3);
  664. 			if((skill_lv = pc->checkskill(sd,NC_TRAININGAXE)) > 0)
  665. 				damage += (skill_lv * 5);
  666. 			break;
  667. 		case W_FIST:
  668. 			if((skill_lv = pc->checkskill(sd,TK_RUN)) > 0)
  669. 				damage += (skill_lv * 10);
  670. 			// No break, fallthrough to Knuckles
  671. 		case W_KNUCKLE:
  672. 			if((skill_lv = pc->checkskill(sd,MO_IRONHAND)) > 0)
  673. 				damage += (skill_lv * 3);
  674. 			break;
  675. 		case W_MUSICAL:
  676. 			if((skill_lv = pc->checkskill(sd,BA_MUSICALLESSON)) > 0)
  677. 				damage += (skill_lv * 3);
  678. 			break;
  679. 		case W_WHIP:
  680. 			if((skill_lv = pc->checkskill(sd,DC_DANCINGLESSON)) > 0)
  681. 				damage += (skill_lv * 3);
  682. 			break;
  683. 		case W_BOOK:
  684. 			if((skill_lv = pc->checkskill(sd,SA_ADVANCEDBOOK)) > 0)
  685. 				damage += (skill_lv * 3);
  686. 			break;
  687. 		case W_KATAR:
  688. 			if((skill_lv = pc->checkskill(sd,AS_KATAR)) > 0)
  689. 				damage += (skill_lv * 3);
  690. 			break;
  691. 	}
  692.  
  693. 	return damage;
  694. }
  695.  
  696. /*==========================================
  697.  * Calculates ATK masteries.
  698.  *------------------------------------------*/
  699. int64 battle_calc_masteryfix(struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int64 damage, int div, bool left, bool weapon) {
  700. 	int skill2_lv, i;
  701. 	struct status_change *sc;
  702. 	struct map_session_data *sd;
  703. 	struct status_data *tstatus;
  704.  
  705. 	nullpo_ret(src);
  706. 	nullpo_ret(target);
  707.  
  708. 	sc = status->get_sc(src);
  709. 	sd = BL_CAST(BL_PC, src);
  710. 	tstatus = status->get_status_data(target);
  711.  
  712. 	if ( !sd )
  713. 		return damage;
  714.  
  715. 	damage = battle->add_mastery(sd, target, damage, left);
  716.  
  717. 	switch( skill_id ){ // specific skill masteries
  718. 		case MO_INVESTIGATE:
  719. 		case MO_EXTREMITYFIST:
  720. 		case CR_GRANDCROSS:
  721. 		case NJ_ISSEN:
  722. 		case CR_ACIDDEMONSTRATION:
  723. 			return damage;
  724. 		case NJ_SYURIKEN:
  725. 			if( (skill2_lv = pc->checkskill(sd,NJ_TOBIDOUGU)) > 0
  726. #ifndef RENEWAL
  727. 				&& weapon
  728. #endif
  729. 				)
  730. 				damage += 3 * skill2_lv;
  731. 			break;	
  732. #ifndef RENEWAL
  733. 		case NJ_KUNAI:
  734. 			if( weapon )
  735. 				damage += 60;
  736. 			break;
  737. #endif
  738. 		case RA_WUGDASH://(Caster Current Weight x 10 / 8)
  739. 			if( sd->weight )
  740. 				damage += sd->weight / 8 ;
  741. 		case RA_WUGSTRIKE:
  742. 		case RA_WUGBITE:	
  743. 			damage += 30*pc->checkskill(sd, RA_TOOTHOFWUG);
  744. 			break;
  745. 		case HT_FREEZINGTRAP:
  746. 			damage += 40 * pc->checkskill(sd, RA_RESEARCHTRAP);
  747. 			break;
  748. 	}
  749.  
  750. 	if( sc ){ // sc considered as masteries
  751. 		if(sc->data[SC_GN_CARTBOOST])
  752. 			damage += 10 * sc->data[SC_GN_CARTBOOST]->val1;
  753. 		if(sc->data[SC_CAMOUFLAGE])
  754. 			damage += 30 * ( 10 - sc->data[SC_CAMOUFLAGE]->val4 );
  755. #ifdef RENEWAL
  756. 		if(sc->data[SC_NIBELUNGEN] && weapon)
  757. 			damage += sc->data[SC_NIBELUNGEN]->val2;
  758. 		if(sc->data[SC_IMPOSITIO])
  759. 			damage += sc->data[SC_IMPOSITIO]->val2;
  760. 		if(sc->data[SC_DRUMBATTLE]){
  761. 			if(tstatus->size == SZ_SMALL)
  762. 				damage += sc->data[SC_DRUMBATTLE]->val2;
  763. 			else if(tstatus->size == SZ_MEDIUM)
  764. 				damage += 10 * sc->data[SC_DRUMBATTLE]->val1;
  765. 			//else no bonus for large target
  766. 		}
  767. 		if(sc->data[SC_GS_MADNESSCANCEL])
  768. 			damage += 100;
  769. 		if(sc->data[SC_GS_GATLINGFEVER]){
  770. 			if(tstatus->size == SZ_SMALL)
  771. 				damage += 10 * sc->data[SC_GS_GATLINGFEVER]->val1;
  772. 			else if(tstatus->size == SZ_MEDIUM)
  773. 				damage += -5 * sc->data[SC_GS_GATLINGFEVER]->val1;
  774. 			else
  775. 				damage += sc->data[SC_GS_GATLINGFEVER]->val1;
  776. 		}
  777. 		//if(sc->data[SC_SPECIALZONE])
  778. 		//	damage += sc->data[SC_SPECIALZONE]->val2 >> 4;
  779. #endif
  780. 	}
  781.  
  782. 	// general skill masteries
  783. #ifdef RENEWAL
  784. 	if( skill_id == MO_FINGEROFFENSIVE )//The finger offensive spheres on moment of attack do count. [Skotlex]
  785. 		damage += div * sd->spiritball_old * 3;
  786. 	else
  787. 		damage += div * sd->spiritball * 3;
  788. 	if( skill_id != CR_SHIELDBOOMERANG ) // Only Shield boomerang doesn't takes the Star Crumbs bonus.
  789. 		damage += div * (left ? sd->left_weapon.star : sd->right_weapon.star);
  790. 	if( skill_id != MC_CARTREVOLUTION && (skill2_lv=pc->checkskill(sd,BS_HILTBINDING)) > 0 )
  791. 		damage += 4;
  792.  
  793. 	if(sd->status.party_id && (skill2_lv=pc->checkskill(sd,TK_POWER)) > 0) {
  794. 		if( (i = party->foreachsamemap(party->sub_count, sd, 0)) > 1 )
  795. 			damage += 2 * skill2_lv * i * (damage /*+ unknown value*/)  / 100 /*+ unknown value*/;
  796. 	}
  797. #else
  798. 	if( skill_id != ASC_BREAKER && weapon ) // Adv Katar Mastery is does not applies to ASC_BREAKER, but other masteries DO apply >_>
  799. 		if( sd->status.weapon == W_KATAR && (skill2_lv=pc->checkskill(sd,ASC_KATAR)) > 0 )
  800. 			damage += damage * (10 + 2 * skill2_lv) / 100;
  801. #endif
  802.  
  803. 	// percentage factor masteries
  804. 	if ( sc && sc->data[SC_MIRACLE] )
  805. 		i = 2; //Star anger
  806. 	else
  807. 		ARR_FIND(0, MAX_PC_FEELHATE, i, status->get_class(target) == sd->hate_mob[i]);
  808. 	if ( i < MAX_PC_FEELHATE && (skill2_lv=pc->checkskill(sd,pc->sg_info[i].anger_id)) && weapon ) {
  809. 		int ratio = sd->status.base_level + status_get_dex(src) + status_get_luk(src);
  810. 		if ( i == 2 ) ratio += status_get_str(src); //Star Anger
  811. 		if  (skill2_lv < 4 )
  812. 			ratio /= (12 - 3 * skill2_lv);
  813. 		damage += damage * ratio / 100;
  814. 	}
  815.  
  816. 	if( sd->status.class_ == JOB_ARCH_BISHOP_T || sd->status.class_ == JOB_ARCH_BISHOP ){
  817. 		if((skill2_lv = pc->checkskill(sd,AB_EUCHARISTICA)) > 0 &&
  818. 			(tstatus->race == RC_DEMON || tstatus->def_ele == ELE_DARK) )
  819. 			damage += damage * skill2_lv / 100;
  820. 	}
  821.  
  822. 	return damage;
  823. }
  824. /*==========================================
  825.  * Elemental attribute fix.
  826.  *------------------------------------------*/
  827. int64 battle_calc_elefix(struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int64 damage, int nk, int n_ele, int s_ele, int s_ele_, bool left, int flag){
  828. 	struct status_data *tstatus;
  829.  
  830. 	nullpo_ret(src);
  831. 	nullpo_ret(target);
  832.  
  833. 	tstatus = status->get_status_data(target);
  834.  
  835. 	if( (nk&NK_NO_ELEFIX) || n_ele )
  836. 		return damage;
  837.  
  838. 	if( damage > 0 ) {
  839. 		if( left )
  840. 			damage = battle->attr_fix(src, target, damage, s_ele_, tstatus->def_ele, tstatus->ele_lv);
  841. 		else{
  842. 			damage=battle->attr_fix(src, target, damage, s_ele, tstatus->def_ele, tstatus->ele_lv);
  843. 			if( skill_id == MC_CARTREVOLUTION ) //Cart Revolution applies the element fix once more with neutral element
  844. 				damage = battle->attr_fix(src,target,damage,ELE_NEUTRAL,tstatus->def_ele, tstatus->ele_lv);
  845. 			if( skill_id == GS_GROUNDDRIFT ) //Additional 50*lv Neutral damage.
  846. 				damage += battle_attr_fix(src,target,50*skill_lv,ELE_NEUTRAL,tstatus->def_ele, tstatus->ele_lv);
  847. 		}
  848. 	}
  849.  
  850. #ifndef RENEWAL
  851. 	{
  852. 		struct status_data *sstatus;
  853. 		struct status_change *sc;
  854.  
  855. 		sstatus = status->get_status_data(src);
  856. 		sc = status->get_sc(src);
  857.  
  858. 		if( sc && sc->data[SC_SUB_WEAPONPROPERTY] ) { // Descriptions indicate this means adding a percent of a normal attack in another element. [Skotlex]
  859. 			int64 temp = battle->calc_base_damage(sstatus, &sstatus->rhw, sc, tstatus->size, BL_CAST(BL_PC, src), (flag?2:0)) * sc->data[SC_SUB_WEAPONPROPERTY]->val2 / 100;
  860. 			damage += battle->attr_fix(src, target, temp, sc->data[SC_SUB_WEAPONPROPERTY]->val1, tstatus->def_ele, tstatus->ele_lv);
  861. 			if( left ) {
  862. 				temp = battle->calc_base_damage(sstatus, &sstatus->lhw, sc, tstatus->size, BL_CAST(BL_PC, src), (flag?2:0)) * sc->data[SC_SUB_WEAPONPROPERTY]->val2 / 100;
  863. 				damage += battle->attr_fix(src, target, temp, sc->data[SC_SUB_WEAPONPROPERTY]->val1, tstatus->def_ele, tstatus->ele_lv);
  864. 			}
  865. 		}
  866. 	}
  867. #endif
  868. 	return damage;
  869. }
  870. /*==========================================
  871.  * Calculates card bonuses damage adjustments.
  872.  * cflag(cardfix flag):
  873.  *	&1 - calc for left hand.
  874.  *  &2 - atker side cardfix(BF_WEAPON) otherwise target side(BF_WEAPON).
  875.  *------------------------------------------*/
  876. int64 battle_calc_cardfix(int attack_type, struct block_list *src, struct block_list *target, int nk, int s_ele, int s_ele_, int64 damage, int cflag, int wflag){
  877. 	struct map_session_data *sd, *tsd;
  878. 	short cardfix = 1000, t_class, s_class, s_race2, t_race2;
  879. 	struct status_data *sstatus, *tstatus;
  880. 	int i;
  881.  
  882. 	if( !damage )
  883. 		return 0;
  884.  
  885. 	nullpo_ret(src);
  886. 	nullpo_ret(target);
  887.  
  888. 	sd = BL_CAST(BL_PC, src);
  889. 	tsd = BL_CAST(BL_PC, target);
  890. 	t_class = status->get_class(target);
  891. 	s_class = status->get_class(src);
  892. 	sstatus = status->get_status_data(src);
  893. 	tstatus = status->get_status_data(target);
  894. 	s_race2 = status->get_race2(src);
  895.  
  896. 	switch(attack_type){
  897. 		case BF_MAGIC:
  898. 			if ( sd && !(nk&NK_NO_CARDFIX_ATK) ) {
  899. 				cardfix = cardfix * (100 + sd->magic_addrace[tstatus->race]) / 100;
  900. 				if (!(nk&NK_NO_ELEFIX))
  901. 					cardfix = cardfix*(100+sd->magic_addele[tstatus->def_ele]) / 100;
  902. 				cardfix = cardfix * (100 + sd->magic_addsize[tstatus->size]) / 100;
  903. 				cardfix = cardfix * (100 + sd->magic_addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
  904. 				cardfix = cardfix * (100 + sd->magic_atk_ele[s_ele])/100;
  905. 				for(i=0; i< ARRAYLENGTH(sd->add_mdmg) && sd->add_mdmg[i].rate; i++) {
  906. 					if(sd->add_mdmg[i].class_ == t_class) {
  907. 						cardfix = cardfix * (100 + sd->add_mdmg[i].rate) / 100;
  908. 						break;
  909. 					}
  910. 				}
  911. 				if (cardfix != 1000)
  912. 					damage = damage * cardfix / 1000;
  913. 			}
  914.  
  915. 			if( tsd && !(nk&NK_NO_CARDFIX_DEF) )
  916. 			{ // Target cards.
  917. 				if (!(nk&NK_NO_ELEFIX))
  918. 				{
  919. 					int ele_fix = tsd->subele[s_ele];
  920. 					for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++)
  921. 					{
  922. 						if(tsd->subele2[i].ele != s_ele) continue;
  923. 						if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
  924. 							 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
  925. 							 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
  926. 							continue;
  927. 						ele_fix += tsd->subele2[i].rate;
  928. 					}
  929. 					cardfix = cardfix * (100 - ele_fix) / 100;
  930. 				}
  931. 				cardfix = cardfix * (100 - tsd->subsize[sstatus->size]) / 100;
  932. 				cardfix = cardfix * (100 - tsd->subrace2[s_race2]) / 100;
  933. 				cardfix = cardfix * (100 - tsd->subrace[sstatus->race]) / 100;
  934. 				cardfix = cardfix * (100 - tsd->subrace[is_boss(src)?RC_BOSS:RC_NONBOSS]) / 100;
  935. 				if( sstatus->race != RC_DEMIHUMAN )
  936. 					cardfix = cardfix * (100-tsd->subrace[RC_NONDEMIHUMAN]) / 100;
  937.  
  938. 				for(i=0; i < ARRAYLENGTH(tsd->add_mdef) && tsd->add_mdef[i].rate;i++) {
  939. 					if(tsd->add_mdef[i].class_ == s_class) {
  940. 						cardfix = cardfix * (100-tsd->add_mdef[i].rate) / 100;
  941. 						break;
  942. 					}
  943. 				}
  944. 				//It was discovered that ranged defense also counts vs magic! [Skotlex]
  945. 				if ( wflag&BF_SHORT )
  946. 					cardfix = cardfix * ( 100 - tsd->bonus.near_attack_def_rate ) / 100;
  947. 				else
  948. 					cardfix = cardfix * ( 100 - tsd->bonus.long_attack_def_rate ) / 100;
  949.  
  950. 				cardfix = cardfix * ( 100 - tsd->bonus.magic_def_rate ) / 100;
  951.  
  952. 				if( tsd->sc.data[SC_PROTECT_MDEF] )
  953. 					cardfix = cardfix * ( 100 - tsd->sc.data[SC_PROTECT_MDEF]->val1 ) / 100;
  954.  
  955. 				if( cardfix != 1000 )
  956. 					damage = damage * cardfix / 1000;
  957. 			}
  958. 			break;
  959. 		case BF_WEAPON:
  960. 			t_race2 = status->get_race2(target);
  961. 			if( cflag&2 ){
  962. 				if( sd && !(nk&NK_NO_CARDFIX_ATK) ){
  963. 					short cardfix_ = 1000;
  964. 					if( sd->state.arrow_atk ){
  965. 						cardfix = cardfix * (100 + sd->right_weapon.addrace[tstatus->race] + sd->arrow_addrace[tstatus->race]) / 100;
  966. 						if( !(nk&NK_NO_ELEFIX) ){
  967. 							int ele_fix = sd->right_weapon.addele[tstatus->def_ele] + sd->arrow_addele[tstatus->def_ele];
  968. 							for(i = 0; ARRAYLENGTH(sd->right_weapon.addele2) > i && sd->right_weapon.addele2[i].rate != 0; i++){
  969. 								if (sd->right_weapon.addele2[i].ele != tstatus->def_ele) continue;
  970. 								if(!(sd->right_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
  971. 									 sd->right_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
  972. 									 sd->right_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
  973. 										continue;
  974. 								ele_fix += sd->right_weapon.addele2[i].rate;
  975. 							}
  976. 							cardfix = cardfix * (100 + ele_fix) / 100;
  977. 						}
  978. 						cardfix = cardfix * (100 + sd->right_weapon.addsize[tstatus->size]+sd->arrow_addsize[tstatus->size]) / 100;
  979. 						cardfix = cardfix * (100 + sd->right_weapon.addrace2[t_race2]) / 100;
  980. 						cardfix = cardfix * (100 + sd->right_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS] + sd->arrow_addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
  981. 						if( tstatus->race != RC_DEMIHUMAN )
  982. 							cardfix = cardfix * (100 + sd->right_weapon.addrace[RC_NONDEMIHUMAN]+sd->arrow_addrace[RC_NONDEMIHUMAN]) / 100;
  983. 					}else{ // Melee attack
  984. 						if( !battle_config.left_cardfix_to_right ){
  985. 							cardfix=cardfix*(100+sd->right_weapon.addrace[tstatus->race])/100;					
  986. 							if( !(nk&NK_NO_ELEFIX) ){
  987. 								int ele_fix = sd->right_weapon.addele[tstatus->def_ele];
  988. 								for (i = 0; ARRAYLENGTH(sd->right_weapon.addele2) > i && sd->right_weapon.addele2[i].rate != 0; i++) {
  989. 									if (sd->right_weapon.addele2[i].ele != tstatus->def_ele) continue;
  990. 									if(!(sd->right_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
  991. 										 sd->right_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
  992. 										 sd->right_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
  993. 											continue;
  994. 									ele_fix += sd->right_weapon.addele2[i].rate;
  995. 								}
  996. 								cardfix = cardfix * (100+ele_fix) / 100;
  997. 							}
  998. 							cardfix = cardfix * (100+sd->right_weapon.addsize[tstatus->size]) / 100;
  999. 							cardfix = cardfix * (100+sd->right_weapon.addrace2[t_race2]) / 100;
  1000. 							cardfix = cardfix * (100+sd->right_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
  1001. 							if( tstatus->race != RC_DEMIHUMAN )
  1002. 								cardfix = cardfix * (100 + sd->right_weapon.addrace[RC_NONDEMIHUMAN]) / 100;
  1003.  
  1004. 							if( cflag&1 ){
  1005. 								cardfix_ = cardfix_*(100+sd->left_weapon.addrace[tstatus->race])/100;						
  1006. 								if (!(nk&NK_NO_ELEFIX)){
  1007. 									int ele_fix_lh = sd->left_weapon.addele[tstatus->def_ele];							
  1008. 									for (i = 0; ARRAYLENGTH(sd->left_weapon.addele2) > i && sd->left_weapon.addele2[i].rate != 0; i++) {
  1009. 										if (sd->left_weapon.addele2[i].ele != tstatus->def_ele) continue;
  1010. 										if(!(sd->left_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
  1011. 											 sd->left_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
  1012. 											 sd->left_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
  1013. 												continue;
  1014. 										ele_fix_lh += sd->left_weapon.addele2[i].rate;
  1015. 									}
  1016. 									cardfix = cardfix * (100+ele_fix_lh) / 100;
  1017. 								}
  1018. 								cardfix_ = cardfix_ * (100+sd->left_weapon.addsize[tstatus->size]) / 100;
  1019. 								cardfix_ = cardfix_ * (100+sd->left_weapon.addrace2[t_race2]) / 100;
  1020. 								cardfix_ = cardfix_ * (100+sd->left_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
  1021. 								if( tstatus->race != RC_DEMIHUMAN )
  1022. 									cardfix_ = cardfix_*(100+sd->left_weapon.addrace[RC_NONDEMIHUMAN])/100;
  1023. 							}
  1024. 						}else{
  1025. 							int ele_fix = sd->right_weapon.addele[tstatus->def_ele] + sd->left_weapon.addele[tstatus->def_ele];
  1026. 							for (i = 0; ARRAYLENGTH(sd->right_weapon.addele2) > i && sd->right_weapon.addele2[i].rate != 0; i++){
  1027. 								if (sd->right_weapon.addele2[i].ele != tstatus->def_ele) continue;
  1028. 								if(!(sd->right_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
  1029. 									 sd->right_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
  1030. 									 sd->right_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
  1031. 										continue;
  1032. 								ele_fix += sd->right_weapon.addele2[i].rate;
  1033. 							}
  1034. 							for (i = 0; ARRAYLENGTH(sd->left_weapon.addele2) > i && sd->left_weapon.addele2[i].rate != 0; i++){
  1035. 								if (sd->left_weapon.addele2[i].ele != tstatus->def_ele) continue;
  1036. 								if(!(sd->left_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
  1037. 									 sd->left_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
  1038. 									 sd->left_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
  1039. 										continue;
  1040. 								ele_fix += sd->left_weapon.addele2[i].rate;
  1041. 							}
  1042.  
  1043. 							cardfix = cardfix * (100 + sd->right_weapon.addrace[tstatus->race] + sd->left_weapon.addrace[tstatus->race]) / 100;
  1044. 							cardfix = cardfix * (100 + ele_fix) / 100;
  1045. 							cardfix = cardfix * (100 + sd->right_weapon.addsize[tstatus->size] + sd->left_weapon.addsize[tstatus->size])/100;
  1046. 							cardfix = cardfix * (100 + sd->right_weapon.addrace2[t_race2] + sd->left_weapon.addrace2[t_race2])/100;
  1047. 							cardfix = cardfix * (100 + sd->right_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS] + sd->left_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
  1048. 							if( tstatus->race != RC_DEMIHUMAN )
  1049. 								cardfix = cardfix * (100+sd->right_weapon.addrace[RC_NONDEMIHUMAN] + sd->left_weapon.addrace[RC_NONDEMIHUMAN]) / 100;
  1050. 						}
  1051. 					}
  1052.  
  1053. 					for( i = 0; i < ARRAYLENGTH(sd->right_weapon.add_dmg) && sd->right_weapon.add_dmg[i].rate; i++ ){
  1054. 						if( sd->right_weapon.add_dmg[i].class_ == t_class ){
  1055. 							cardfix = cardfix * (100 + sd->right_weapon.add_dmg[i].rate) / 100;
  1056. 							break;
  1057. 						}
  1058. 					}
  1059.  
  1060. 					if( cflag&1 ){
  1061. 						for( i = 0; i < ARRAYLENGTH(sd->left_weapon.add_dmg) && sd->left_weapon.add_dmg[i].rate; i++ ){
  1062. 							if( sd->left_weapon.add_dmg[i].class_ == t_class ){
  1063. 								cardfix_ = cardfix_ * (100 + sd->left_weapon.add_dmg[i].rate) / 100;
  1064. 								break;
  1065. 							}
  1066. 						}
  1067. 					}
  1068. #ifndef RENEWAL
  1069. 					if( wflag&BF_LONG )
  1070. 						cardfix = cardfix * (100 + sd->bonus.long_attack_atk_rate) / 100;
  1071. #endif
  1072. 					if( (cflag&1) && cardfix_ != 1000 )
  1073. 						damage = damage * cardfix_ / 1000;
  1074. 					else if( cardfix != 1000 )
  1075. 						damage = damage * cardfix / 1000;
  1076. 				}
  1077. 			}else{
  1078. 				// Target side
  1079. 				if( tsd && !(nk&NK_NO_CARDFIX_DEF) ){
  1080. 					if( !(nk&NK_NO_ELEFIX) ){
  1081. 						int ele_fix = tsd->subele[s_ele];
  1082. 						for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++)
  1083. 						{
  1084. 							if(tsd->subele2[i].ele != s_ele) continue;
  1085. 							if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
  1086. 								 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
  1087. 								 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
  1088. 								continue;
  1089. 							ele_fix += tsd->subele2[i].rate;
  1090. 						}
  1091. 						cardfix = cardfix * (100-ele_fix) / 100;
  1092. 						if( cflag&1 && s_ele_ != s_ele ){
  1093. 							int ele_fix_lh = tsd->subele[s_ele_];
  1094. 							for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++){
  1095. 								if(tsd->subele2[i].ele != s_ele_) continue;
  1096. 								if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
  1097. 									 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
  1098. 									 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
  1099. 									continue;
  1100. 								ele_fix_lh += tsd->subele2[i].rate;
  1101. 							}
  1102. 							cardfix = cardfix * (100 - ele_fix_lh) / 100;
  1103. 						}
  1104. 					}
  1105. 					cardfix = cardfix * (100-tsd->subsize[sstatus->size]) / 100;
  1106.  					cardfix = cardfix * (100-tsd->subrace2[s_race2]) / 100;
  1107. 					cardfix = cardfix * (100-tsd->subrace[sstatus->race]) / 100;
  1108. 					cardfix = cardfix * (100-tsd->subrace[is_boss(src)?RC_BOSS:RC_NONBOSS]) / 100;
  1109. 					if( sstatus->race != RC_DEMIHUMAN )
  1110. 						cardfix = cardfix * (100-tsd->subrace[RC_NONDEMIHUMAN]) / 100;
  1111.  
  1112. 					for( i = 0; i < ARRAYLENGTH(tsd->add_def) && tsd->add_def[i].rate;i++ ){
  1113. 						if( tsd->add_def[i].class_ == s_class )
  1114. 						{
  1115. 							cardfix = cardfix * (100 - tsd->add_def[i].rate) / 100;
  1116. 							break;
  1117. 						}
  1118. 					}
  1119.  
  1120. 					if( wflag&BF_SHORT )
  1121. 						cardfix = cardfix * (100 - tsd->bonus.near_attack_def_rate) / 100;
  1122. 					else // BF_LONG (there's no other choice)
  1123. 						cardfix = cardfix * (100 - tsd->bonus.long_attack_def_rate) / 100;
  1124.  
  1125. 					if( tsd->sc.data[SC_PROTECT_DEF] )
  1126. 						cardfix = cardfix * (100 - tsd->sc.data[SC_PROTECT_DEF]->val1) / 100;
  1127.  
  1128. 					if( cardfix != 1000 )
  1129. 						damage = damage * cardfix / 1000;
  1130. 				}
  1131. 			}
  1132. 			break;
  1133. 		case BF_MISC:
  1134. 			if( tsd && !(nk&NK_NO_CARDFIX_DEF) ){
  1135. 			// misc damage reduction from equipment
  1136. 				if (!(nk&NK_NO_ELEFIX))
  1137. 				{
  1138. 					int ele_fix = tsd->subele[s_ele];
  1139. 					for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++)
  1140. 					{
  1141. 						if(tsd->subele2[i].ele != s_ele) continue;
  1142. 						if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
  1143. 							 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
  1144. 							 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
  1145. 							continue;
  1146. 						ele_fix += tsd->subele2[i].rate;
  1147. 					}
  1148. 					cardfix = cardfix * (100 - ele_fix) / 100;
  1149. 				}
  1150. 				cardfix = cardfix*(100-tsd->subsize[sstatus->size]) / 100;
  1151. 				cardfix = cardfix*(100-tsd->subrace2[s_race2]) / 100;
  1152. 				cardfix = cardfix*(100-tsd->subrace[sstatus->race]) / 100;
  1153. 				cardfix = cardfix*(100-tsd->subrace[is_boss(src)?RC_BOSS:RC_NONBOSS]) / 100;
  1154. 				if( sstatus->race != RC_DEMIHUMAN )
  1155. 					cardfix = cardfix * (100 - tsd->subrace[RC_NONDEMIHUMAN]) / 100;
  1156.  
  1157. 				cardfix = cardfix * ( 100 - tsd->bonus.misc_def_rate ) / 100;
  1158. 				if( wflag&BF_SHORT )
  1159. 					cardfix = cardfix * ( 100 - tsd->bonus.near_attack_def_rate ) / 100;
  1160. 				else // BF_LONG (there's no other choice)
  1161. 					cardfix = cardfix * ( 100 - tsd->bonus.long_attack_def_rate ) / 100;
  1162.  
  1163. 				if (cardfix != 1000)
  1164. 					damage = damage * cardfix / 1000;
  1165. 			}
  1166. 			break;
  1167. 	}
  1168.  
  1169. 	return damage;
  1170. }
  1171.  
  1172. /*==========================================
  1173.  * Calculates defense reduction. [malufett]
  1174.  * flag:
  1175.  * &1 - idef/imdef(Ignore defense)
  1176.  * &2 - pdef(Pierce defense)
  1177.  * &4 - tdef(Total defense reduction)
  1178.  *------------------------------------------*/
  1179. int64 battle_calc_defense(int attack_type, struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int64 damage, int flag, int pdef){
  1180. 	struct status_data *sstatus, *tstatus;
  1181. 	struct map_session_data *sd, *tsd;
  1182. 	struct status_change *sc, *tsc;
  1183. 	int i;
  1184.  
  1185. 	if( !damage )
  1186. 		return 0;
  1187.  
  1188. 	nullpo_ret(src);
  1189. 	nullpo_ret(target);
  1190.  
  1191. 	sd = BL_CAST(BL_PC, src);
  1192. 	tsd = BL_CAST(BL_PC, target);
  1193. 	sstatus = status->get_status_data(src);
  1194. 	tstatus = status->get_status_data(target);
  1195. 	sc = status->get_sc(src);
  1196. 	tsc = status->get_sc(target);
  1197.  
  1198. 	switch(attack_type){
  1199. 		case BF_WEAPON:
  1200. 			{
  1201. 			/**	Take note in RE
  1202. 			  *	def1 = equip def
  1203. 			  *	def2 = status def
  1204. 			 **/
  1205. 			defType def1 = status->get_def(target); //Don't use tstatus->def1 due to skill timer reductions.
  1206. 			short def2 = tstatus->def2, vit_def;
  1207. #ifdef RENEWAL			
  1208. 			def1 = status->calc_def2(target, tsc, def1, false); // equip def(RE)
  1209. 			def2 = status->calc_def(target, tsc, def2, false); // status def(RE)
  1210. #else
  1211. 			def1 = status->calc_def(target, tsc, def1, false); // equip def(RE)
  1212. 			def2 = status->calc_def2(target, tsc, def2, false); // status def(RE)
  1213. #endif
  1214.  
  1215. 			if( sd ){
  1216. 				i = sd->ignore_def[is_boss(target)?RC_BOSS:RC_NONBOSS];
  1217. 				i += sd->ignore_def[tstatus->race];
  1218. 				if( i ){
  1219. 					if( i > 100 ) i = 100;
  1220. 					def1 -= def1 * i / 100;
  1221. 					def2 -= def2 * i / 100;
  1222. 				}
  1223. 			}
  1224.  
  1225. 			if( sc && sc->data[SC_EXPIATIO] ){
  1226. 				i = 5 * sc->data[SC_EXPIATIO]->val1; // 5% per level
  1227. 				def1 -= def1 * i / 100;
  1228. 				def2 -= def2 * i / 100;
  1229. 			}
  1230.  
  1231. 			if( battle_config.vit_penalty_type && battle_config.vit_penalty_target&target->type ) {
  1232. 				unsigned char target_count; //256 max targets should be a sane max
  1233. 				target_count = unit->counttargeted(target);
  1234. 				if(target_count >= battle_config.vit_penalty_count) {
  1235. 					if(battle_config.vit_penalty_type == 1) {
  1236. 						if( !tsc || !tsc->data[SC_STEELBODY] )
  1237. 							def1 = (def1 * (100 - (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num))/100;
  1238. 						def2 = (def2 * (100 - (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num))/100;
  1239. 					} else { //Assume type 2
  1240. 						if( !tsc || !tsc->data[SC_STEELBODY] )
  1241. 							def1 -= (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num;
  1242. 						def2 -= (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num;
  1243. 					}
  1244. 				}
  1245. 				if(skill_id == AM_ACIDTERROR) def1 = 0; //Acid Terror ignores only armor defense. [Skotlex]
  1246. 				if(def2 < 1) def2 = 1;
  1247. 			}
  1248. 			//Vitality reduction from rodatazone: http://rodatazone.simgaming.net/mechanics/substats.php#def
  1249. 			if (tsd)	//Sd vit-eq
  1250. 			{
  1251. #ifndef RENEWAL
  1252. 				//[VIT*0.5] + rnd([VIT*0.3], max([VIT*0.3],[VIT^2/150]-1))
  1253. 				vit_def = def2*(def2-15)/150;
  1254. 				vit_def = def2/2 + (vit_def>0?rnd()%vit_def:0);
  1255. #else
  1256. 				vit_def = def2;
  1257. #endif
  1258. 				if((battle->check_undead(sstatus->race,sstatus->def_ele) || sstatus->race==RC_DEMON) && //This bonus already doesnt work vs players
  1259. 					src->type == BL_MOB && (i=pc->checkskill(tsd,AL_DP)) > 0)
  1260. 					vit_def += i*(int)(3 +(tsd->status.base_level+1)*0.04);   // submitted by orn
  1261. 				if( src->type == BL_MOB && (i=pc->checkskill(tsd,RA_RANGERMAIN))>0 &&
  1262. 					(sstatus->race == RC_BRUTE || sstatus->race == RC_FISH || sstatus->race == RC_PLANT) )
  1263. 					vit_def += i*5;
  1264. 			}
  1265. 			else { //Mob-Pet vit-eq
  1266. #ifndef RENEWAL
  1267. 				//VIT + rnd(0,[VIT/20]^2-1)
  1268. 				vit_def = (def2/20)*(def2/20);
  1269. 				vit_def = def2 + (vit_def>0?rnd()%vit_def:0);
  1270. #else
  1271. 				vit_def = def2;
  1272. #endif
  1273. 			}
  1274.  
  1275. 			if (battle_config.weapon_defense_type) {
  1276. 				vit_def += def1*battle_config.weapon_defense_type;
  1277. 				def1 = 0;
  1278. 			}
  1279. 		#ifdef RENEWAL
  1280. 			/**
  1281. 			* RE DEF Reduction
  1282. 			* Pierce defence gains 1 atk per def/2
  1283. 			**/
  1284.  
  1285. 			if( def1 < -399 ) // it stops at -399
  1286. 				def1 = 399; // in aegis it set to 1 but in our case it may lead to exploitation so limit it to 399
  1287. 				//return 1; 
  1288.  
  1289. 			if( flag&2 )
  1290. 				damage += def1 >> 1;
  1291.  
  1292. 			if( !(flag&1) && !(flag&2) ) {
  1293. 				if( flag&4 )
  1294. 					damage -= (def1 + vit_def);
  1295. 				else
  1296. 					damage = (int)((100.0f - def1 / (def1 + 400.0f) * 90.0f) / 100.0f * damage - vit_def);
  1297. 			}
  1298.  
  1299. 		#else
  1300. 				if( def1 > 100 ) def1 = 100;
  1301. 				if( !(flag&1) ){
  1302. 					if( flag&2 )
  1303. 						damage = damage * pdef * (def1+vit_def) / 100;
  1304. 					else
  1305. 						damage = damage * (100-def1) / 100;
  1306. 				}
  1307. 				if( !(flag&1 || flag&2) )
  1308. 					damage -= vit_def;
  1309. 		#endif
  1310. 			}
  1311. 			break;
  1312.  
  1313. 		case BF_MAGIC:
  1314. 			{
  1315. 			defType mdef = tstatus->mdef;
  1316. 			short mdef2= tstatus->mdef2;
  1317. #ifdef RENEWAL
  1318. 			mdef2 = status->calc_mdef(target, tsc, mdef2, false); // status mdef(RE)
  1319. 			mdef = status->calc_mdef2(target, tsc, mdef, false); // equip mde(RE)
  1320. #else
  1321. 			mdef2 = status->calc_mdef2(target, tsc, mdef2, false); // status mdef(RE)
  1322. 			mdef = status->calc_mdef(target, tsc, mdef, false); // equip mde(RE)
  1323. #endif
  1324. 			if( flag&1 )
  1325. 				mdef = 0;
  1326.  
  1327. 			if(sd) {
  1328. 				i = sd->ignore_mdef[is_boss(target)?RC_BOSS:RC_NONBOSS];
  1329. 				i += sd->ignore_mdef[tstatus->race];
  1330. 				if (i)
  1331. 				{
  1332. 					if (i > 100) i = 100;
  1333. 					mdef -= mdef * i/100;
  1334. 					//mdef2-= mdef2* i/100;
  1335. 				}
  1336. 			}
  1337. 		#ifdef RENEWAL
  1338. 			/**
  1339. 			 * RE MDEF Reduction
  1340. 			 **/
  1341. 			if( mdef < -99 ) // it stops at -99
  1342. 				mdef = 99; // in aegis it set to 1 but in our case it may lead to exploitation so limit it to 99
  1343. 				//return 1; 
  1344.  
  1345. 			damage = (int)((100.0f - mdef / (mdef + 100.0f) * 90.0f) / 100.0f * damage - mdef2);
  1346. 		#else
  1347. 			if(battle_config.magic_defense_type)
  1348. 				damage = damage - mdef*battle_config.magic_defense_type - mdef2;
  1349. 			else
  1350. 				damage = damage * (100-mdef)/100 - mdef2;
  1351. 		#endif
  1352. 			}
  1353. 			break;
  1354. 	}
  1355. 	return damage;
  1356. }
  1357.  
  1358. int battle_calc_skillratio(int attack_type, struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int skillratio, int flag){
  1359. 	int i;
  1360. 	struct status_change *sc, *tsc;
  1361. 	struct map_session_data *sd, *tsd;
  1362. 	struct status_data *st, *tst;
  1363.  
  1364. 	nullpo_ret(src);
  1365. 	nullpo_ret(target);
  1366.  
  1367. 	sd = BL_CAST(BL_PC, src);
  1368. 	tsd = BL_CAST(BL_PC, target);
  1369. 	sc = status->get_sc(src);
  1370. 	tsc = status->get_sc(target);
  1371. 	st = status->get_status_data(src);
  1372. 	tst = status->get_status_data(target);
  1373.  
  1374. 	switch(attack_type){
  1375. 		case BF_MAGIC:
  1376. 			switch(skill_id){
  1377. 				case MG_NAPALMBEAT:
  1378. 					skillratio += skill_lv * 10 - 30;
  1379. 					break;
  1380. 				case MG_FIREBALL:
  1381. 			#ifdef RENEWAL
  1382. 					skillratio += 20 * skill_lv;
  1383. 			#else
  1384. 					skillratio += skill_lv * 10 - 30;
  1385. 			#endif
  1386. 					break;
  1387. 				case MG_SOULSTRIKE:
  1388. 					if (battle->check_undead(tst->race,tst->def_ele))
  1389. 						skillratio += 5*skill_lv;
  1390. 					break;
  1391. 				case MG_FIREWALL:
  1392. 					skillratio -= 50;
  1393. 					break;
  1394. 				case MG_THUNDERSTORM:
  1395. 					/**
  1396. 					 * in Renewal Thunder Storm boost is 100% (in pre-re, 80%)
  1397. 					 **/
  1398. 					#ifndef RENEWAL
  1399. 						skillratio -= 20;
  1400. 					#endif
  1401. 					break;
  1402. 				case MG_FROSTDIVER:
  1403. 					skillratio += 10 * skill_lv;
  1404. 					break;
  1405. 				case AL_HOLYLIGHT:
  1406. 					skillratio += 25;
  1407. 					if (sc && sc->data[SC_SOULLINK] && sc->data[SC_SOULLINK]->val2 == SL_PRIEST)
  1408. 						skillratio *= 5; //Does 5x damage include bonuses from other skills?
  1409. 					break;
  1410. 				case AL_RUWACH:
  1411. 					skillratio += 45;
  1412. 					break;
  1413. 				case WZ_FROSTNOVA:
  1414. 					skillratio += (100+skill_lv*10) * 2 / 3 - 100;
  1415. 					break;
  1416. 				case WZ_FIREPILLAR:
  1417. 					if (skill_lv > 10)
  1418. 						skillratio += 100;
  1419. 					else
  1420. 						skillratio -= 80;
  1421. 					break;
  1422. 				case WZ_SIGHTRASHER:
  1423. 					skillratio += 20 * skill_lv;
  1424. 					break;
  1425. 				case WZ_WATERBALL:
  1426. 					skillratio += 30 * skill_lv;
  1427. 					break;
  1428. 				case WZ_STORMGUST:
  1429. 					skillratio += 40 * skill_lv;
  1430. 					break;
  1431. 				case HW_NAPALMVULCAN:
  1432. 					skillratio += 10 * skill_lv - 30;
  1433. 					break;
  1434. 				case SL_STIN:
  1435. 					skillratio += (tst->size!=SZ_SMALL?-99:10*skill_lv); //target size must be small (0) for full damage.
  1436. 					break;
  1437. 				case SL_STUN:
  1438. 					skillratio += (tst->size!=SZ_BIG?5*skill_lv:-99); //Full damage is dealt on small/medium targets
  1439. 					break;
  1440. 				case SL_SMA:
  1441. 					skillratio += -60 + status->get_lv(src); //Base damage is 40% + lv%
  1442. 					break;
  1443. 				case NJ_KOUENKA:
  1444. 					skillratio -= 10;
  1445. 					break;
  1446. 				case NJ_KAENSIN:
  1447. 					skillratio -= 50;
  1448. 					break;
  1449. 				case NJ_BAKUENRYU:
  1450. 					skillratio += 50 * (skill_lv-1);
  1451. 					break;
  1452. 				case NJ_HYOUSYOURAKU:
  1453. 					skillratio += 50 * skill_lv;
  1454. 					break;
  1455. 				case NJ_RAIGEKISAI:
  1456. 					skillratio += 60 + 40 * skill_lv;
  1457. 					break;
  1458. 				case NJ_KAMAITACHI:
  1459. 				case NPC_ENERGYDRAIN:
  1460. 					skillratio += 100 * skill_lv;
  1461. 					break;
  1462. 				case NPC_EARTHQUAKE:
  1463. 					skillratio += 100 + 100 * skill_lv + 100 * (skill_lv/2);
  1464. 					break;
  1465. 			#ifdef RENEWAL
  1466. 				case WZ_HEAVENDRIVE:
  1467. 				case WZ_METEOR:
  1468. 					skillratio += 25;
  1469. 					break;
  1470. 				case WZ_VERMILION:
  1471. 					{
  1472.     					int interval = 0, per = interval, ratio = per;
  1473.     					while( (per++) < skill_lv ){
  1474.      						ratio += interval;
  1475.      						if(per%3==0) interval += 20;
  1476.     					}
  1477. 						if( skill_lv > 9 )
  1478. 							ratio -= 10;
  1479. 						skillratio += ratio;
  1480. 					}
  1481. 					break;
  1482. 				case NJ_HUUJIN:
  1483. 					skillratio += 50;
  1484. 					break;
  1485. 			#else
  1486. 				case WZ_VERMILION:
  1487. 					skillratio += 20*skill_lv-20;
  1488. 					break;
  1489. 			#endif
  1490. 				/**
  1491. 					* Arch Bishop
  1492. 					**/
  1493. 				case AB_JUDEX:
  1494. 					skillratio += 180 + 20 * skill_lv;
  1495. 					if (skill_lv > 4) skillratio += 20;
  1496. 					RE_LVL_DMOD(100);
  1497. 					break;
  1498. 				case AB_ADORAMUS:
  1499. 					skillratio += 400 + 100 * skill_lv;
  1500. 					RE_LVL_DMOD(100);
  1501. 					break;
  1502. 				case AB_DUPLELIGHT_MAGIC:
  1503. 					skillratio += 100 + 20 * skill_lv;
  1504. 					break;
  1505. 				/**
  1506. 					* Warlock
  1507. 					**/
  1508. 				case WL_SOULEXPANSION: // MATK [{( Skill Level + 4 ) x 100 ) + ( Caster?s INT )} x ( Caster?s Base Level / 100 )] %
  1509. 					skillratio += 300 + 100 * skill_lv + status_get_int(src);
  1510. 					RE_LVL_DMOD(100);
  1511. 					break;
  1512. 				case WL_FROSTMISTY: // MATK [{( Skill Level x 100 ) + 200 } x ( Caster?s Base Level / 100 )] %
  1513. 					skillratio += 100 + 100 * skill_lv;
  1514. 					RE_LVL_DMOD(100);
  1515. 					break;
  1516. 				case WL_JACKFROST:
  1517. 					if( tsc && tsc->data[SC_FROSTMISTY] ){
  1518. 						skillratio += 900 + 300 * skill_lv;
  1519. 						RE_LVL_DMOD(100);
  1520. 					}else{
  1521. 						skillratio += 400 + 100 * skill_lv;
  1522. 						RE_LVL_DMOD(150);
  1523. 					}
  1524. 					break;
  1525. 				case WL_DRAINLIFE:
  1526. 					skillratio = 200 * skill_lv + status_get_int(src);
  1527. 					RE_LVL_DMOD(100);
  1528. 					break;
  1529. 				case WL_CRIMSONROCK:
  1530. 					skillratio = 300 * skill_lv;
  1531. 					RE_LVL_DMOD(100);
  1532. 					skillratio += 1300;
  1533. 					break;
  1534. 				case WL_HELLINFERNO:
  1535. 					skillratio = 300 * skill_lv;
  1536. 					RE_LVL_DMOD(100);
  1537. 					// Shadow: MATK [{( Skill Level x 300 ) x ( Caster Base Level / 100 ) x 4/5 }] %
  1538. 					// Fire : MATK [{( Skill Level x 300 ) x ( Caster Base Level / 100 ) /5 }] %
  1539. 					if( flag&ELE_DARK )
  1540. 						skillratio *= 4;
  1541. 					skillratio /= 5;
  1542. 					break;
  1543. 				case WL_COMET:
  1544. 					i = ( sc ? distance_xy(target->x, target->y, sc->comet_x, sc->comet_y) : 8 );
  1545. 					if( i <= 3 ) skillratio += 2400 + 500 * skill_lv; // 7 x 7 cell
  1546. 					else
  1547. 					if( i <= 5 ) skillratio += 1900 + 500 * skill_lv; // 11 x 11 cell
  1548. 					else
  1549. 					if( i <= 7 ) skillratio += 1400 + 500 * skill_lv; // 15 x 15 cell
  1550. 					else
  1551. 						skillratio += 900 + 500 * skill_lv; // 19 x 19 cell
  1552.  
  1553. 					if( sd && sd->status.party_id ){
  1554. 						struct map_session_data* psd;
  1555. 						int p_sd[5] = {0, 0, 0, 0, 0}, c; // just limit it to 5
  1556.  
  1557. 						c = 0;
  1558. 						memset (p_sd, 0, sizeof(p_sd));
  1559. 						party->foreachsamemap(skill->check_condition_char_sub, sd, 3, &sd->bl, &c, &p_sd, skill_id);
  1560. 						c = ( c > 1 ? rand()%c : 0 );
  1561.  
  1562. 						if( (psd = map->id2sd(p_sd[c])) && pc->checkskill(psd,WL_COMET) > 0 ){
  1563. 							skillratio = skill_lv * 400; //MATK [{( Skill Level x 400 ) x ( Caster's Base Level / 120 )} + 2500 ] %
  1564. 							RE_LVL_DMOD(120);
  1565. 							skillratio += 2500;
  1566. 							status_zap(&psd->bl, 0, skill->get_sp(skill_id, skill_lv) / 2);
  1567. 						}
  1568. 					}
  1569. 					break;
  1570. 				case WL_CHAINLIGHTNING_ATK:
  1571. 					skillratio += 400 + 100 * skill_lv;
  1572. 					RE_LVL_DMOD(100);
  1573. 					if(flag > 0)
  1574. 						skillratio += 100 * flag;
  1575. 					break;
  1576. 				case WL_EARTHSTRAIN:
  1577. 					skillratio += 1900 + 100 * skill_lv;
  1578. 					RE_LVL_DMOD(100);
  1579. 					break;
  1580. 				case WL_TETRAVORTEX_FIRE:
  1581. 				case WL_TETRAVORTEX_WATER:
  1582. 				case WL_TETRAVORTEX_WIND:
  1583. 				case WL_TETRAVORTEX_GROUND:
  1584. 					skillratio += 400 + 500 * skill_lv;
  1585. 					break;
  1586. 				case WL_SUMMON_ATK_FIRE:
  1587. 				case WL_SUMMON_ATK_WATER:
  1588. 				case WL_SUMMON_ATK_WIND:
  1589. 				case WL_SUMMON_ATK_GROUND:
  1590. 					skillratio = skill_lv * (status->get_lv(src) + ( sd ? sd->status.job_level : 50 ));// This is close to official, but lacking a little info to finalize. [Rytech]
  1591. 					RE_LVL_DMOD(100);
  1592. 					break;
  1593. 				case LG_RAYOFGENESIS:
  1594. 				{
  1595. 					uint16 lv = skill_lv;
  1596. 					int bandingBonus = 0;
  1597. 					if( sc && sc->data[SC_BANDING] )
  1598. 						bandingBonus = 200 * (sd ? skill->check_pc_partner(sd,skill_id,&lv,skill->get_splash(skill_id,skill_lv),0) : 1);
  1599. 					skillratio = ((300 * skill_lv) + bandingBonus) * (sd ? sd->status.job_level : 1) / 25;
  1600. 				}
  1601. 				break;
  1602. 				case LG_SHIELDSPELL:// [(Casters Base Level x 4) + (Shield MDEF x 100) + (Casters INT x 2)] %
  1603. 					if( sd ) {
  1604. 						skillratio = status->get_lv(src) * 4 + sd->bonus.shieldmdef * 100 + status_get_int(src) * 2;
  1605. 					} else
  1606. 						skillratio += 1900;	//2000%
  1607. 					break;
  1608. 				case WM_METALICSOUND:
  1609. 					skillratio += 120 * skill_lv + 60 * ( sd? pc->checkskill(sd, WM_LESSON) : 10 ) - 100;
  1610. 					break;
  1611. 				/*case WM_SEVERE_RAINSTORM:
  1612. 					skillratio += 50 * skill_lv;
  1613. 					break;
  1614.  
  1615. 					WM_SEVERE_RAINSTORM just set a unit place,
  1616. 					refer to WM_SEVERE_RAINSTORM_MELEE to set the formula.
  1617. 				*/
  1618. 				case WM_REVERBERATION_MAGIC:
  1619. 					// MATK [{(Skill Level x 100) + 100} x Casters Base Level / 100] %
  1620. 					skillratio += 100 * (sd ? pc->checkskill(sd, WM_REVERBERATION) : 1);
  1621. 					RE_LVL_DMOD(100);
  1622. 					break;
  1623. 				case SO_FIREWALK:
  1624. 					skillratio = 300;
  1625. 					RE_LVL_DMOD(100);
  1626. 					if( sc && sc->data[SC_HEATER_OPTION] )
  1627. 						skillratio += sc->data[SC_HEATER_OPTION]->val3;
  1628. 					break;
  1629. 				case SO_ELECTRICWALK:
  1630. 					skillratio = 300;
  1631. 					RE_LVL_DMOD(100);
  1632. 					if( sc && sc->data[SC_BLAST_OPTION] )
  1633. 						skillratio += sd ? sd->status.job_level / 2 : 0;
  1634. 					break;
  1635. 				case SO_EARTHGRAVE:
  1636. 					skillratio = ( 200 * ( sd ? pc->checkskill(sd, SA_SEISMICWEAPON) : 10 ) + status_get_int(src) * skill_lv );
  1637. 					RE_LVL_DMOD(100);
  1638. 					if( sc && sc->data[SC_CURSED_SOIL_OPTION] )
  1639. 						skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val2;
  1640. 					break;
  1641. 				case SO_DIAMONDDUST:
  1642. 					skillratio = ( 200 * ( sd ? pc->checkskill(sd, SA_FROSTWEAPON) : 10 ) + status_get_int(src) * skill_lv );
  1643. 					RE_LVL_DMOD(100);
  1644. 					if( sc && sc->data[SC_COOLER_OPTION] )
  1645. 						skillratio += sc->data[SC_COOLER_OPTION]->val3;
  1646. 					break;
  1647. 				case SO_POISON_BUSTER:
  1648. 					skillratio += 1100 + 300 * skill_lv;
  1649. 					if( sc && sc->data[SC_CURSED_SOIL_OPTION] )
  1650. 						skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val2;
  1651. 					break;
  1652. 				case SO_PSYCHIC_WAVE:
  1653. 					skillratio += -100 + skill_lv * 70 + (status_get_int(src) * 3);
  1654. 					RE_LVL_DMOD(100);
  1655. 					if( sc ){
  1656. 						if( sc->data[SC_HEATER_OPTION] )
  1657. 							skillratio += sc->data[SC_HEATER_OPTION]->val3;
  1658. 						else if(sc->data[SC_COOLER_OPTION] )
  1659. 							skillratio +=  sc->data[SC_COOLER_OPTION]->val3;
  1660. 						else if(sc->data[SC_BLAST_OPTION] )
  1661. 							skillratio += sc->data[SC_BLAST_OPTION]->val2;
  1662. 						else if(sc->data[SC_CURSED_SOIL_OPTION] )
  1663. 							skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val3;
  1664. 					}
  1665. 					break;
  1666. 				case SO_VARETYR_SPEAR: //MATK [{( Endow Tornado skill level x 50 ) + ( Caster INT x Varetyr Spear Skill level )} x Caster Base Level / 100 ] %
  1667. 					skillratio = status_get_int(src) * skill_lv + ( sd ? pc->checkskill(sd, SA_LIGHTNINGLOADER) * 50 : 0 );
  1668. 					RE_LVL_DMOD(100);
  1669. 					if( sc && sc->data[SC_BLAST_OPTION] )
  1670. 						skillratio += sd ? sd->status.job_level * 5 : 0;
  1671. 					break;
  1672. 				case SO_CLOUD_KILL:
  1673. 					skillratio += -100 + skill_lv * 40;
  1674. 					RE_LVL_DMOD(100);
  1675. 					if( sc && sc->data[SC_CURSED_SOIL_OPTION] )
  1676. 						skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val2;
  1677. 					break;
  1678. 				case GN_DEMONIC_FIRE:
  1679. 					if( skill_lv > 20)
  1680. 					{	// Fire expansion Lv.2
  1681. 						skillratio += 110 + 20 * (skill_lv - 20) + status_get_int(src) * 3;	// Need official INT bonus. [LimitLine]
  1682. 					}
  1683. 					else if( skill_lv > 10 )
  1684. 					{	// Fire expansion Lv.1
  1685. 						skillratio += 110 + 20 * (skill_lv - 10) / 2;
  1686. 					}
  1687. 					else
  1688. 						skillratio += 110 + 20 * skill_lv;
  1689. 					break;
  1690. 					// Magical Elemental Spirits Attack Skills
  1691. 				case EL_FIRE_MANTLE:
  1692. 				case EL_WATER_SCREW:
  1693. 					skillratio += 900;
  1694. 					break;
  1695. 				case EL_FIRE_ARROW:
  1696. 				case EL_ROCK_CRUSHER_ATK:
  1697. 					skillratio += 200;
  1698. 					break;
  1699. 				case EL_FIRE_BOMB:
  1700. 				case EL_ICE_NEEDLE:
  1701. 				case EL_HURRICANE_ATK:
  1702. 					skillratio += 400;
  1703. 					break;
  1704. 				case EL_FIRE_WAVE:
  1705. 				case EL_TYPOON_MIS_ATK:
  1706. 					skillratio += 1100;
  1707. 					break;
  1708. 				case MH_ERASER_CUTTER:
  1709. 					if(skill_lv%2) skillratio += 400; //600:800:1000
  1710. 					else skillratio += 700; //1000:1200
  1711. 					skillratio += 100 * skill_lv;
  1712. 					break;
  1713. 				case MH_XENO_SLASHER:
  1714. 					if(skill_lv%2) skillratio += 350 + 50 * skill_lv; //500:600:700
  1715. 					else skillratio += 400 + 100 * skill_lv; //700:900
  1716. 					break;
  1717. 				case MH_HEILIGE_STANGE:
  1718. 					skillratio += 400 + 250 * skill_lv;
  1719. 					break;
  1720. 				case MH_POISON_MIST:
  1721. 					skillratio += 100 * skill_lv;
  1722. 					break;
  1723. 				case KO_KAIHOU:
  1724. 					if( sd ){
  1725. 						ARR_FIND(1, 6, i, sd->charm[i] > 0);
  1726. 						if( i < 5 ){
  1727. 							skillratio += -100 + 200 * sd->charm[i];
  1728. 							RE_LVL_DMOD(100);
  1729. 							pc->del_charm(sd, sd->charm[i], i);
  1730. 						}
  1731. 					}
  1732. 					break;
  1733. 			}
  1734. 			break;
  1735. 		case BF_WEAPON:
  1736. 			switch( skill_id )
  1737. 			{
  1738. 				case SM_BASH:
  1739. 				case MS_BASH:
  1740. 					skillratio += 30 * skill_lv;
  1741. 					break;
  1742. 				case SM_MAGNUM:
  1743. 				case MS_MAGNUM:
  1744. 					skillratio += 20 * skill_lv;
  1745. 					break;
  1746. 				case MC_MAMMONITE:
  1747. 					skillratio += 50 * skill_lv;
  1748. 					break;
  1749. 				case HT_POWER:
  1750. 					skillratio += -50 + 8 * status_get_str(src);
  1751. 					break;
  1752. 				case AC_DOUBLE:
  1753. 				case MA_DOUBLE:
  1754. 					skillratio += 10 * (skill_lv-1);
  1755. 					break;
  1756. 				case AC_SHOWER:
  1757. 				case MA_SHOWER:
  1758. 					#ifdef RENEWAL
  1759. 						skillratio += 50 + 10 * skill_lv;
  1760. 					#else
  1761. 						skillratio += -25 + 5 * skill_lv;
  1762. 					#endif
  1763. 					break;
  1764. 				case AC_CHARGEARROW:
  1765. 				case MA_CHARGEARROW:
  1766. 					skillratio += 50;
  1767. 					break;
  1768. 	#ifndef RENEWAL
  1769. 				case HT_FREEZINGTRAP:
  1770. 				case MA_FREEZINGTRAP:
  1771. 					skillratio += -50 + 10 * skill_lv;
  1772. 					break;
  1773. 	#endif
  1774. 				case KN_PIERCE:
  1775. 				case ML_PIERCE:
  1776. 					skillratio += 10 * skill_lv;
  1777. 					break;
  1778. 				case MER_CRASH:
  1779. 					skillratio += 10 * skill_lv;
  1780. 					break;
  1781. 				case KN_SPEARSTAB:
  1782. 					skillratio += 15 * skill_lv;
  1783. 					break;
  1784. 				case KN_SPEARBOOMERANG:
  1785. 					skillratio += 50*skill_lv;
  1786. 					break;
  1787. 				case KN_BRANDISHSPEAR:
  1788. 				case ML_BRANDISH:
  1789. 				{
  1790. 					int ratio = 100 + 20 * skill_lv;
  1791. 					skillratio += ratio - 100;
  1792. 					if(skill_lv>3 && flag==1) skillratio += ratio / 2;
  1793. 					if(skill_lv>6 && flag==1) skillratio += ratio / 4;
  1794. 					if(skill_lv>9 && flag==1) skillratio += ratio / 8;
  1795. 					if(skill_lv>6 && flag==2) skillratio += ratio / 2;
  1796. 					if(skill_lv>9 && flag==2) skillratio += ratio / 4;
  1797. 					if(skill_lv>9 && flag==3) skillratio += ratio / 2;
  1798. 					break;
  1799. 				}
  1800. 				case KN_BOWLINGBASH:
  1801. 				case MS_BOWLINGBASH:
  1802. 					skillratio+= 40 * skill_lv;
  1803. 					break;
  1804. 				case AS_GRIMTOOTH:
  1805. 					skillratio += 20 * skill_lv;
  1806. 					break;
  1807. 				case AS_POISONREACT:
  1808. 					skillratio += 30 * skill_lv;
  1809. 					break;
  1810. 				case AS_SONICBLOW:
  1811. 					skillratio += 300 + 40 * skill_lv;
  1812. 					break;
  1813. 				case TF_SPRINKLESAND:
  1814. 					skillratio += 30;
  1815. 					break;
  1816. 				case MC_CARTREVOLUTION:
  1817. 					skillratio += 50;
  1818. 					if( sd && sd->cart_weight )
  1819. 						skillratio += 100 * sd->cart_weight / sd->cart_weight_max; // +1% every 1% weight
  1820. 					else if (!sd)
  1821. 						skillratio += 100; //Max damage for non players.
  1822. 					break;
  1823. 				case NPC_RANDOMATTACK:
  1824. 					skillratio += 100 * skill_lv;
  1825. 					break;
  1826. 				case NPC_WATERATTACK:
  1827. 				case NPC_GROUNDATTACK:
  1828. 				case NPC_FIREATTACK:
  1829. 				case NPC_WINDATTACK:
  1830. 				case NPC_POISONATTACK:
  1831. 				case NPC_HOLYATTACK:
  1832. 				case NPC_DARKNESSATTACK:
  1833. 				case NPC_UNDEADATTACK:
  1834. 				case NPC_TELEKINESISATTACK:
  1835. 				case NPC_BLOODDRAIN:
  1836. 				case NPC_ACIDBREATH:
  1837. 				case NPC_DARKNESSBREATH:
  1838. 				case NPC_FIREBREATH:
  1839. 				case NPC_ICEBREATH:
  1840. 				case NPC_THUNDERBREATH:
  1841. 				case NPC_HELLJUDGEMENT:
  1842. 				case NPC_PULSESTRIKE:
  1843. 					skillratio += 100 * (skill_lv-1);
  1844. 					break;
  1845. 				case RG_BACKSTAP:
  1846. 					if( sd && sd->status.weapon == W_BOW && battle_config.backstab_bow_penalty )
  1847. 						skillratio += (200 + 40 * skill_lv) / 2;
  1848. 					else
  1849. 						skillratio += 200 + 40 * skill_lv;
  1850. 					break;
  1851. 				case RG_RAID:
  1852. 					skillratio += 40 * skill_lv;
  1853. 					break;
  1854. 				case RG_INTIMIDATE:
  1855. 					skillratio += 30 * skill_lv;
  1856. 					break;
  1857. 				case CR_SHIELDCHARGE:
  1858. 					skillratio += 20 * skill_lv;
  1859. 					break;
  1860. 				case CR_SHIELDBOOMERANG:
  1861. 					skillratio += 30 * skill_lv;
  1862. 					break;
  1863. 				case NPC_DARKCROSS:
  1864. 				case CR_HOLYCROSS:
  1865. 				{
  1866. 					int ratio = 35 * skill_lv;
  1867. 					#ifdef RENEWAL
  1868. 						if(sd && sd->status.weapon == W_2HSPEAR)
  1869. 							ratio *= 2;
  1870. 					#endif
  1871. 					skillratio += ratio;
  1872. 					break;
  1873. 				}
  1874. 				case AM_DEMONSTRATION:
  1875. 					skillratio += 20 * skill_lv;
  1876. 					break;
  1877. 				case AM_ACIDTERROR:
  1878. 					skillratio += 40 * skill_lv;
  1879. 					break;
  1880. 				case MO_FINGEROFFENSIVE:
  1881. 					skillratio+= 50 * skill_lv;
  1882. 					break;
  1883. 				case MO_INVESTIGATE:
  1884. 					skillratio += 75 * skill_lv;
  1885. 					break;
  1886. 	#ifndef RENEWAL
  1887. 				case MO_EXTREMITYFIST:
  1888. 					{	//Overflow check. [Skotlex]
  1889. 						unsigned int ratio = skillratio + 100*(8 + st->sp/10);
  1890. 						//You'd need something like 6K SP to reach this max, so should be fine for most purposes.
  1891. 						if (ratio > 60000) ratio = 60000; //We leave some room here in case skillratio gets further increased.
  1892. 						skillratio = (unsigned short)ratio;
  1893. 					}
  1894. 					break;
  1895. 	#endif
  1896. 				case MO_TRIPLEATTACK:
  1897. 					skillratio += 20 * skill_lv;
  1898. 					break;
  1899. 				case MO_CHAINCOMBO:
  1900. 					skillratio += 50 + 50 * skill_lv;
  1901. 					break;
  1902. 				case MO_COMBOFINISH:
  1903. 					skillratio += 140 + 60 * skill_lv;
  1904. 					break;
  1905. 				case BA_MUSICALSTRIKE:
  1906. 				case DC_THROWARROW:
  1907. 					skillratio += 25 + 25 * skill_lv;
  1908. 					break;
  1909. 				case CH_TIGERFIST:
  1910. 					skillratio += 100 * skill_lv - 60;
  1911. 					break;
  1912. 				case CH_CHAINCRUSH:
  1913. 					skillratio += 300 + 100 * skill_lv;
  1914. 					break;
  1915. 				case CH_PALMSTRIKE:
  1916. 					skillratio += 100 + 100 * skill_lv;
  1917. 					break;
  1918. 				case LK_HEADCRUSH:
  1919. 					skillratio += 40 * skill_lv;
  1920. 					break;
  1921. 				case LK_JOINTBEAT:
  1922. 					i = 10 * skill_lv - 50;
  1923. 					// Although not clear, it's being assumed that the 2x damage is only for the break neck ailment.
  1924. 					if (flag&BREAK_NECK) i*=2;
  1925. 					skillratio += i;
  1926. 					break;
  1927. 				case ASC_METEORASSAULT:
  1928. 					skillratio += 40 * skill_lv - 60;
  1929. 					break;
  1930. 				case SN_SHARPSHOOTING:
  1931. 				case MA_SHARPSHOOTING:
  1932. 					skillratio += 100 + 50 * skill_lv;
  1933. 					break;
  1934. 				case CG_ARROWVULCAN:
  1935. 					skillratio += 100 + 100 * skill_lv;
  1936. 					break;
  1937. 				case AS_SPLASHER:
  1938. 					skillratio += 400 + 50 * skill_lv;
  1939. 					if(sd)
  1940. 						skillratio += 20 * pc->checkskill(sd,AS_POISONREACT);
  1941. 					break;
  1942. 	#ifndef RENEWAL
  1943. 				case ASC_BREAKER:
  1944. 					skillratio += 100*skill_lv-100;
  1945. 	#else
  1946. 				case LK_SPIRALPIERCE:
  1947. 				case ML_SPIRALPIERCE:
  1948. 					skillratio += 50 * skill_lv;
  1949. 	#endif
  1950. 					break;
  1951. 				case PA_SACRIFICE:
  1952. 					skillratio += 10 * skill_lv - 10;
  1953. 					break;
  1954. 				case PA_SHIELDCHAIN:
  1955. 					skillratio += 30 * skill_lv;
  1956. 					break;
  1957. 				case WS_CARTTERMINATION:
  1958. 					i = 10 * (16 - skill_lv);
  1959. 					if (i < 1) i = 1;
  1960. 					//Preserve damage ratio when max cart weight is changed.
  1961. 					if(sd && sd->cart_weight)
  1962. 						skillratio += sd->cart_weight/i * 80000/battle_config.max_cart_weight - 100;
  1963. 					else if (!sd)
  1964. 						skillratio += 80000 / i - 100;
  1965. 					break;
  1966. 				case TK_DOWNKICK:
  1967. 					skillratio += 60 + 20 * skill_lv;
  1968. 					break;
  1969. 				case TK_STORMKICK:
  1970. 					skillratio += 60 + 20 * skill_lv;
  1971. 					break;
  1972. 				case TK_TURNKICK:
  1973. 					skillratio += 90 + 30 * skill_lv;
  1974. 					break;
  1975. 				case TK_COUNTER:
  1976. 					skillratio += 90 + 30 * skill_lv;
  1977. 					break;
  1978. 				case TK_JUMPKICK:
  1979. 					skillratio += -70 + 10*skill_lv;
  1980. 					if (sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == skill_id)
  1981. 						skillratio += 10 * status->get_lv(src) / 3; //Tumble bonus
  1982. 					if (flag) {
  1983. 						skillratio += 10 * status->get_lv(src) / 3; //Running bonus (TODO: What is the real bonus?)
  1984. 						if( sc && sc->data[SC_STRUP] )  // Spurt bonus
  1985. 							skillratio *= 2;
  1986. 					}
  1987. 					break;
  1988. 				case GS_TRIPLEACTION:
  1989. 					skillratio += 50 * skill_lv;
  1990. 					break;
  1991. 				case GS_BULLSEYE:
  1992. 					//Only works well against brute/demihumans non bosses.
  1993. 					if((tst->race == RC_BRUTE || tst->race == RC_DEMIHUMAN)
  1994. 						&& !(tst->mode&MD_BOSS))
  1995. 						skillratio += 400;
  1996. 					break;
  1997. 				case GS_TRACKING:
  1998. 					skillratio += 100 * (skill_lv+1);
  1999. 					break;
  2000. 				case GS_PIERCINGSHOT:
  2001. #ifdef RENEWAL
  2002. 					if( sd && sd->weapontype1 == W_RIFLE )
  2003. 						skillratio += 50 + 30 * skill_lv;
  2004. 					else
  2005. #endif
  2006. 						skillratio += 20 * skill_lv;
  2007. 					break;
  2008. 				case GS_RAPIDSHOWER:
  2009. 					skillratio += 10 * skill_lv;
  2010. 					break;
  2011. 				case GS_DESPERADO:
  2012. 					skillratio += 50 * (skill_lv-1);
  2013. 					break;
  2014. 				case GS_DUST:
  2015. 					skillratio += 50 * skill_lv;
  2016. 					break;
  2017. 				case GS_FULLBUSTER:
  2018. 					skillratio += 100 * (skill_lv+2);
  2019. 					break;
  2020. 				case GS_SPREADATTACK:
  2021. 				#ifdef RENEWAL
  2022. 					skillratio += 20 * (skill_lv);
  2023. 				#else
  2024. 					skillratio += 20 * (skill_lv-1);
  2025. 				#endif
  2026. 					break;
  2027. 				case NJ_HUUMA:
  2028. 					skillratio += 50 + 150 * skill_lv;
  2029. 					break;
  2030. 				case NJ_TATAMIGAESHI:
  2031. 					skillratio += 10 * skill_lv;
  2032. 					break;
  2033. 				case NJ_KASUMIKIRI:
  2034. 					skillratio += 10 * skill_lv;
  2035. 					break;
  2036. 				case NJ_KIRIKAGE:
  2037. 					skillratio += 100 * (skill_lv-1);
  2038. 					break;
  2039. #ifdef RENEWAL
  2040. 				case NJ_KUNAI:
  2041. 					skillratio += 50 + 150 * skill_lv;
  2042. 					break;
  2043. #endif
  2044. 				case KN_CHARGEATK:
  2045. 					{
  2046. 						int k = (flag-1)/3; //+100% every 3 cells of distance
  2047. 						if( k > 2 ) k = 2; // ...but hard-limited to 300%.
  2048. 						skillratio += 100 * k;
  2049. 					}
  2050. 					break;
  2051. 				case HT_PHANTASMIC:
  2052. 					skillratio += 50;
  2053. 					break;
  2054. 				case MO_BALKYOUNG:
  2055. 					skillratio += 200;
  2056. 					break;
  2057. 				case HFLI_MOON:	//[orn]
  2058. 					skillratio += 10 + 110 * skill_lv;
  2059. 					break;
  2060. 				case HFLI_SBR44:	//[orn]
  2061. 					skillratio += 100 * (skill_lv-1);
  2062. 					break;
  2063. 				case NPC_VAMPIRE_GIFT:
  2064. 					skillratio += ((skill_lv-1)%5+1) * 100;
  2065. 					break;
  2066. 				case RK_SONICWAVE:
  2067. 					skillratio += -100 + 100 * (skill_lv + 5);
  2068. 					skillratio = skillratio * (100 + (status->get_lv(src)-100) / 2) / 100;
  2069. 					break;
  2070. 				case RK_HUNDREDSPEAR:
  2071. 						skillratio += 500 + (80 * skill_lv);
  2072. 						if( sd ){
  2073. 							short index = sd->equip_index[EQI_HAND_R];
  2074. 							if( index >= 0 && sd->inventory_data[index]
  2075. 								&& sd->inventory_data[index]->type == IT_WEAPON )
  2076. 								skillratio += (10000 - min(10000, sd->inventory_data[index]->weight)) / 10;
  2077. 							skillratio = skillratio * (100 + (status->get_lv(src)-100) / 2) / 100 + 50 * pc->checkskill(sd,LK_SPIRALPIERCE);
  2078. 						}
  2079. 					break;
  2080. 				case RK_WINDCUTTER:
  2081. 						skillratio += -100 + 50 * (skill_lv + 2);
  2082. 						RE_LVL_DMOD(100);
  2083. 					break;
  2084. 				case RK_IGNITIONBREAK:
  2085. 						i = distance_bl(src,target);
  2086. 						if( i < 2 )
  2087. 							skillratio += 300 * skill_lv;
  2088. 						else if( i < 4 )
  2089. 							skillratio += 250 * skill_lv;
  2090. 						else
  2091. 							skillratio += 200 * skill_lv;
  2092. 						skillratio = (skillratio - 100) * (100 + (status->get_lv(src)-100)) / 100;
  2093. 						if( st->rhw.ele == ELE_FIRE )
  2094. 							skillratio +=  100 * skill_lv;
  2095. 					break;
  2096. 				case RK_CRUSHSTRIKE:
  2097. 					if( sd )
  2098. 					{//ATK [{Weapon Level * (Weapon Upgrade Level + 6) * 100} + (Weapon ATK) + (Weapon Weight)]%
  2099. 						short index = sd->equip_index[EQI_HAND_R];
  2100. 						if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_WEAPON )
  2101. 							skillratio += -100 + sd->inventory_data[index]->weight/10 + st->rhw.atk +
  2102. 								100 * sd->inventory_data[index]->wlv * (sd->status.inventory[index].refine + 6);
  2103. 					}
  2104. 					break;
  2105. 				case RK_STORMBLAST:
  2106. 					skillratio += -100 + 100 *(sd ? pc->checkskill(sd,RK_RUNEMASTERY) : 1) + status_get_int(src) / 8;
  2107. 					break;
  2108. 				case RK_PHANTOMTHRUST:
  2109. 					skillratio += -100 + 50 * skill_lv + 10 * ( sd ? pc->checkskill(sd,KN_SPEARMASTERY) : 10);
  2110. 					RE_LVL_DMOD(150);
  2111. 					break;
  2112. 				/**
  2113. 					* GC Guilotine Cross
  2114. 					**/
  2115. 				case GC_CROSSIMPACT:
  2116. 					skillratio += 900 + 100 * skill_lv;
  2117. 					RE_LVL_DMOD(120);
  2118. 					break;
  2119. 				case GC_PHANTOMMENACE:
  2120. 					skillratio += 200;
  2121. 					break;
  2122. 				case GC_COUNTERSLASH:
  2123. 					//ATK [{(Skill Level x 100) + 300} x Caster's Base Level / 120]% + ATK [(AGI x 2) + (Caster's Job Level x 4)]%
  2124. 					skillratio += 200 + (100 * skill_lv);
  2125. 					RE_LVL_DMOD(120);
  2126. 					break;
  2127. 				case GC_ROLLINGCUTTER:
  2128. 					skillratio += -50 + 50 * skill_lv;
  2129. 					RE_LVL_DMOD(100);
  2130. 					break;
  2131. 				case GC_CROSSRIPPERSLASHER:
  2132. 					skillratio += 300 + 80 * skill_lv;
  2133. 					RE_LVL_DMOD(100);
  2134. 					if( sc && sc->data[SC_ROLLINGCUTTER] )
  2135. 						skillratio += sc->data[SC_ROLLINGCUTTER]->val1 * status_get_agi(src);
  2136. 					break;
  2137. 				case GC_DARKCROW:
  2138. 					skillratio += 100 * (skill_lv - 1);
  2139. 					break;
  2140. 				/**
  2141. 					* Arch Bishop
  2142. 					**/
  2143. 				case AB_DUPLELIGHT_MELEE:
  2144. 					skillratio += 10 * skill_lv;
  2145. 					break;
  2146. 				/**
  2147. 					* Ranger
  2148. 					**/
  2149. 				case RA_ARROWSTORM:
  2150. 					skillratio += 900 + 80 * skill_lv;
  2151. 					RE_LVL_DMOD(100);
  2152. 					break;
  2153. 				case RA_AIMEDBOLT:
  2154. 					skillratio += 400 + 50 * skill_lv;
  2155. 					RE_LVL_DMOD(100);
  2156. 					break;
  2157. 				case RA_CLUSTERBOMB:
  2158. 					skillratio += 100 + 100 * skill_lv;
  2159. 					break;
  2160. 				case RA_WUGDASH:// ATK 300%
  2161. 					skillratio += 200;
  2162. 					break;
  2163. 				case RA_WUGSTRIKE:
  2164. 					skillratio += -100 + 200 * skill_lv;
  2165. 					break;
  2166. 				case RA_WUGBITE:
  2167. 					skillratio += 300 + 200 * skill_lv;
  2168. 					if ( skill_lv == 5 ) skillratio += 100;
  2169. 					break;
  2170. 				case RA_SENSITIVEKEEN:
  2171. 					skillratio += 50 * skill_lv;
  2172. 					break;
  2173. 				/**
  2174. 					* Mechanic
  2175. 					**/
  2176. 				case NC_BOOSTKNUCKLE:
  2177. 					skillratio += 100 + 100 * skill_lv + status_get_dex(src);
  2178. 					RE_LVL_DMOD(100);
  2179. 					break;
  2180. 				case NC_PILEBUNKER:
  2181. 					skillratio += 200 + 100 * skill_lv + status_get_str(src);
  2182. 					RE_LVL_DMOD(100);
  2183. 					break;
  2184. 				case NC_VULCANARM:
  2185. 					skillratio += -100 + 70 * skill_lv + status_get_dex(src);
  2186. 					RE_LVL_DMOD(100);
  2187. 					break;
  2188. 				case NC_FLAMELAUNCHER:
  2189. 				case NC_COLDSLOWER:
  2190. 					skillratio += 200 + 300 * skill_lv;
  2191. 					RE_LVL_DMOD(100);
  2192. 					break;
  2193. 				case NC_ARMSCANNON:
  2194. 					switch( tst->size ) {
  2195. 						case SZ_SMALL: skillratio += 100 + 500 * skill_lv; break;// Small
  2196. 						case SZ_MEDIUM: skillratio += 100 + 400 * skill_lv; break;// Medium
  2197. 						case SZ_BIG: skillratio += 100 + 300 * skill_lv; break;// Large
  2198. 					}
  2199. 					RE_LVL_DMOD(100);
  2200. 					//NOTE: Their's some other factors that affects damage, but not sure how exactly. Will recheck one day. [Rytech]
  2201. 					break;
  2202. 				case NC_AXEBOOMERANG:
  2203. 					skillratio += 60 + 40 * skill_lv;
  2204. 					if( sd ) {
  2205. 						short index = sd->equip_index[EQI_HAND_R];
  2206. 						if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_WEAPON )
  2207. 						skillratio += sd->inventory_data[index]->weight / 10;// Weight is divided by 10 since 10 weight in coding make 1 whole actural weight. [Rytech]
  2208. 					}
  2209. 					RE_LVL_DMOD(100);
  2210. 					break;
  2211. 				case NC_POWERSWING:
  2212. 					skillratio += 80 + 20 * skill_lv + status_get_str(src) + status_get_dex(src);
  2213. 					RE_LVL_DMOD(100);
  2214. 					break;
  2215. 				case NC_AXETORNADO:
  2216. 					skillratio += 100 + 100 * skill_lv + status_get_vit(src);
  2217. 					RE_LVL_DMOD(100);
  2218. 					break;
  2219. 				case SC_FATALMENACE:
  2220. 					skillratio += 100 * skill_lv;
  2221. 					break;
  2222. 				case SC_TRIANGLESHOT:
  2223. 					skillratio += 270 + 30 * skill_lv;
  2224. 					break;
  2225. 				case SC_FEINTBOMB:
  2226. 					skillratio += 100 + 100 * skill_lv;
  2227. 					break;
  2228. 				case LG_CANNONSPEAR:// Stimated formula. Still need confirm it.
  2229. 					skillratio += -100 + (50  + status_get_str(src)) * skill_lv;
  2230. 					RE_LVL_DMOD(100);
  2231. 					break;
  2232. 				case LG_BANISHINGPOINT:
  2233. 					skillratio += -100 + ((50 * skill_lv) + (30 * ((sd)?pc->checkskill(sd,SM_BASH):1)));
  2234. 					RE_LVL_DMOD(100);
  2235. 					break;
  2236. 				case LG_SHIELDPRESS:
  2237. 					skillratio += 60 + 43 * skill_lv;
  2238. 					RE_LVL_DMOD(100);
  2239. 					break;
  2240. 				case LG_PINPOINTATTACK:
  2241. 					skillratio += -100 + ((100 * skill_lv) + (10 * status_get_agi(src)) );
  2242. 					RE_LVL_DMOD(100);
  2243. 					break;
  2244. 				case LG_RAGEBURST:
  2245. 					if( sd && sd->spiritball_old )
  2246. 						skillratio += -100 + (sd->spiritball_old * 200);
  2247. 					else
  2248. 						skillratio += -100 + 15 * 200;
  2249. 					RE_LVL_DMOD(100);
  2250. 					break;
  2251. 				case LG_SHIELDSPELL:// [(Casters Base Level x 4) + (Shield DEF x 10) + (Casters VIT x 2)] %
  2252. 					if( sd ) {
  2253. 						struct item_data *shield_data = sd->inventory_data[sd->equip_index[EQI_HAND_L]];
  2254. 						skillratio += -100 + status->get_lv(src) * 4 + status_get_vit(src) * 2;
  2255. 						if( shield_data )
  2256. 							skillratio += shield_data->def * 10;
  2257. 					} else
  2258. 						skillratio += 2400;	//2500%
  2259. 					break;
  2260. 				case LG_MOONSLASHER:
  2261. 					skillratio += -100 + (120 * skill_lv + ((sd) ? pc->checkskill(sd,LG_OVERBRAND) : 5) * 80);
  2262. 					RE_LVL_DMOD(100);
  2263. 					break;
  2264. 				case LG_OVERBRAND:
  2265. 					skillratio += -100 + 400 * skill_lv + (pc->checkskill(sd,CR_SPEARQUICKEN) * 30);
  2266. 					RE_LVL_DMOD(100);
  2267. 					break;
  2268. 				case LG_OVERBRAND_BRANDISH:
  2269. 					skillratio += -100 + 300 * skill_lv + (2 * (status_get_str(src) + status_get_dex(src)) / 3);
  2270. 					RE_LVL_DMOD(100);
  2271. 					break;
  2272. 				case LG_OVERBRAND_PLUSATK:
  2273. 					skillratio += -100 + 150 * skill_lv;
  2274. 					RE_LVL_DMOD(100);
  2275. 					break;
  2276. 				case LG_RAYOFGENESIS:
  2277. 					skillratio += 200 + 300 * skill_lv;
  2278. 					RE_LVL_DMOD(100);
  2279. 					break;
  2280. 				case LG_EARTHDRIVE:
  2281. 					skillratio = (skillratio + 100) * skill_lv;
  2282. 					RE_LVL_DMOD(100);
  2283. 					break;
  2284. 				case LG_HESPERUSLIT:
  2285. 					skillratio += 120 * skill_lv - 100;
  2286. 					break;
  2287. 				case SR_DRAGONCOMBO:
  2288. 					skillratio += 40 * skill_lv;
  2289. 					RE_LVL_DMOD(100);
  2290. 					break;
  2291. 				case SR_SKYNETBLOW:
  2292. 					if( sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == SR_DRAGONCOMBO )//ATK [{(Skill Level x 100) + (Caster AGI) + 150} x Caster Base Level / 100] %
  2293. 						skillratio += 100 * skill_lv + status_get_agi(src) + 50;
  2294. 					else //ATK [{(Skill Level x 80) + (Caster AGI)} x Caster Base Level / 100] %
  2295. 						skillratio += -100 + 80 * skill_lv + status_get_agi(src);
  2296. 					RE_LVL_DMOD(100);
  2297. 					break;
  2298. 				case SR_EARTHSHAKER:
  2299. 					if( tsc && (tsc->data[SC_HIDING] || tsc->data[SC_CLOAKING] || // [(Skill Level x 150) x (Caster Base Level / 100) + (Caster INT x 3)] %
  2300. 						tsc->data[SC_CHASEWALK] || tsc->data[SC_CLOAKINGEXCEED] || tsc->data[SC__INVISIBILITY]) ){
  2301. 						skillratio += -100 + 150 * skill_lv;
  2302. 						RE_LVL_DMOD(100);
  2303. 						skillratio += status_get_int(src) * 3;
  2304. 					}else{ //[(Skill Level x 50) x (Caster Base Level / 100) + (Caster INT x 2)] %
  2305. 						skillratio += 50 * (skill_lv-2);
  2306. 						RE_LVL_DMOD(100);
  2307. 						skillratio += status_get_int(src) * 2;
  2308. 					}
  2309. 					break;
  2310. 				case SR_FALLENEMPIRE:// ATK [(Skill Level x 150 + 100) x Caster Base Level / 150] %
  2311. 					skillratio += 150 *skill_lv;
  2312. 					RE_LVL_DMOD(150);
  2313.  					break;
  2314. 				case SR_TIGERCANNON:// ATK [((Caster consumed HP + SP) / 4) x Caster Base Level / 100] %
  2315. 					{
  2316. 						int hp = status_get_max_hp(src) * (10 + 2 * skill_lv) / 100,
  2317. 							sp = status_get_max_sp(src) * (6 + skill_lv) / 100;
  2318. 						if( sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE ) // ATK [((Caster consumed HP + SP) / 2) x Caster Base Level / 100] %
  2319. 							skillratio += -100 + (hp+sp) / 2;
  2320. 						else
  2321. 							skillratio += -100 + (hp+sp) / 4;
  2322. 						RE_LVL_DMOD(100);
  2323. 					}
  2324. 						break;
  2325. 				case SR_RAMPAGEBLASTER:
  2326. 					skillratio += 20 * skill_lv * (sd?sd->spiritball_old:5) - 100;
  2327. 					if( sc && sc->data[SC_EXPLOSIONSPIRITS] ){
  2328. 						skillratio += sc->data[SC_EXPLOSIONSPIRITS]->val1 * 20;
  2329. 						RE_LVL_DMOD(120);
  2330. 					}else
  2331. 						RE_LVL_DMOD(150);
  2332. 					break;
  2333. 				case SR_KNUCKLEARROW:
  2334. 					if( flag&4 ){  // ATK [(Skill Level x 150) + (1000 x Target current weight / Maximum weight) + (Target Base Level x 5) x (Caster Base Level / 150)] %
  2335. 						skillratio += -100 + 150 * skill_lv + status->get_lv(target) * 5 * (status->get_lv(src) / 100) ;
  2336. 						if( tsd && tsd->weight )
  2337. 							skillratio += 100 * (tsd->weight / tsd->max_weight);
  2338. 					}else // ATK [(Skill Level x 100 + 500) x Caster Base Level / 100] %
  2339. 						skillratio += 400 + (100 * skill_lv);
  2340. 					RE_LVL_DMOD(100);
  2341. 					break;
  2342. 				case SR_WINDMILL: // ATK [(Caster Base Level + Caster DEX) x Caster Base Level / 100] %
  2343. 					skillratio += -100 + status->get_lv(src) + status_get_dex(src);
  2344. 					RE_LVL_DMOD(100);
  2345. 					break;
  2346. 				case SR_GATEOFHELL:
  2347. 					if( sc && sc->data[SC_COMBOATTACK]
  2348. 						&& sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE )
  2349. 						skillratio += 800 * skill_lv -100;
  2350. 					else
  2351. 						skillratio += 500 * skill_lv -100;
  2352. 					RE_LVL_DMOD(100);
  2353. 					break;
  2354. 				case SR_GENTLETOUCH_QUIET:
  2355. 					skillratio += 100 * skill_lv - 100 + status_get_dex(src);
  2356. 					RE_LVL_DMOD(100);
  2357. 					break;
  2358. 				case SR_HOWLINGOFLION:
  2359. 					skillratio += 300 * skill_lv - 100;
  2360. 					RE_LVL_DMOD(150);
  2361. 					break;
  2362. 				case SR_RIDEINLIGHTNING: // ATK [{(Skill Level x 200) + Additional Damage} x Caster Base Level / 100] %
  2363. 					if( (st->rhw.ele) == ELE_WIND || (st->lhw.ele) == ELE_WIND )
  2364. 						skillratio += skill_lv * 50;
  2365. 					skillratio += -100 + 200 * skill_lv;
  2366. 					RE_LVL_DMOD(100);
  2367. 					break;
  2368. 				case WM_REVERBERATION_MELEE:
  2369. 					// ATK [{(Skill Level x 100) + 300} x Caster Base Level / 100]
  2370. 					skillratio += 200 + 100 * pc->checkskill(sd, WM_REVERBERATION);
  2371. 					RE_LVL_DMOD(100);
  2372. 					break;
  2373. 				case WM_SEVERE_RAINSTORM_MELEE:
  2374. 					//ATK [{(Caster DEX + AGI) x (Skill Level / 5)} x Caster Base Level / 100] %
  2375. 					skillratio += -100 + (status_get_dex(src) + status_get_agi(src)) * (skill_lv * 2);
  2376. 					RE_LVL_DMOD(100);
  2377. 					skillratio /= 10;
  2378. 					break;
  2379. 				case WM_GREAT_ECHO:
  2380. 					skillratio += 800 + 100 * skill_lv;
  2381. 					if( sd ) {	// Still need official value [pakpil]
  2382. 						uint16 lv = skill_lv;
  2383. 						skillratio += 100 * skill->check_pc_partner(sd,skill_id,&lv,skill->get_splash(skill_id,skill_lv),0);
  2384. 					}
  2385. 					break;
  2386. 				case WM_SOUND_OF_DESTRUCTION:
  2387. 					skillratio += 400;
  2388. 					break;
  2389. 				case GN_CART_TORNADO:
  2390. 					// ATK [( Skill Level x 50 ) + ( Cart Weight / ( 150 - Caster Base STR ))] + ( Cart Remodeling Skill Level x 50 )] %
  2391. 					skillratio += -100 + 50 * skill_lv;
  2392. 					if( sd && sd->cart_weight)
  2393. 							skillratio += sd->cart_weight/10 / max(150-status_get_str(src),1) + pc->checkskill(sd, GN_REMODELING_CART) * 50;
  2394. 					break;
  2395. 				case GN_CARTCANNON:
  2396. 					// ATK [{( Cart Remodeling Skill Level x 50 ) x ( INT / 40 )} + ( Cart Cannon Skill Level x 60 )] %
  2397. 					skillratio += -100 + 60 * skill_lv;
  2398. 					if( sd ) skillratio += pc->checkskill(sd, GN_REMODELING_CART) * 50 * (status_get_int(src) / 40);
  2399. 					break;
  2400. 				case GN_SPORE_EXPLOSION:
  2401. 					skillratio += 200 + 100 * skill_lv;
  2402. 					break;
  2403. 				case GN_CRAZYWEED_ATK:
  2404. 					skillratio += 400 + 100 * skill_lv;
  2405. 					break;
  2406. 				case GN_SLINGITEM_RANGEMELEEATK:
  2407. 					if( sd ) {
  2408. 						switch( sd->itemid ) {
  2409. 							case 13260: // Apple Bomob
  2410. 							case 13261: // Coconut Bomb
  2411. 							case 13262: // Melon Bomb
  2412. 							case 13263: // Pinapple Bomb
  2413. 								skillratio += 400;	// Unconfirded
  2414. 								break;
  2415. 							case 13264: // Banana Bomb 2000%
  2416. 								skillratio += 1900;
  2417. 								break;
  2418. 							case 13265: skillratio -= 75; break; // Black Lump 25%
  2419. 							case 13266: skillratio -= 25; break; // Hard Black Lump 75%
  2420. 							case 13267: skillratio += 100; break; // Extremely Hard Black Lump 200%
  2421. 						}
  2422. 					} else
  2423. 						skillratio += 300;	// Bombs
  2424. 					break;
  2425. 				case SO_VARETYR_SPEAR://ATK [{( Striking Level x 50 ) + ( Varetyr Spear Skill Level x 50 )} x Caster Base Level / 100 ] %
  2426. 					skillratio += -100 + 50 * skill_lv + ( sd ? pc->checkskill(sd, SO_STRIKING) * 50 : 0 );
  2427. 					if( sc && sc->data[SC_BLAST_OPTION] )
  2428. 						skillratio += sd ? sd->status.job_level * 5 : 0;
  2429. 					break;
  2430. 					// Physical Elemantal Spirits Attack Skills
  2431. 				case EL_CIRCLE_OF_FIRE:
  2432. 				case EL_FIRE_BOMB_ATK:
  2433. 				case EL_STONE_RAIN:
  2434. 					skillratio += 200;
  2435. 					break;
  2436. 				case EL_FIRE_WAVE_ATK:
  2437. 					skillratio += 500;
  2438. 					break;
  2439. 				case EL_TIDAL_WEAPON:
  2440. 					skillratio += 1400;
  2441. 					break;
  2442. 				case EL_WIND_SLASH:
  2443. 					skillratio += 100;
  2444. 					break;
  2445. 				case EL_HURRICANE:
  2446. 					skillratio += 600;
  2447. 					break;
  2448. 				case EL_TYPOON_MIS:
  2449. 				case EL_WATER_SCREW_ATK:
  2450. 					skillratio += 900;
  2451. 					break;
  2452. 				case EL_STONE_HAMMER:
  2453. 					skillratio += 400;
  2454. 					break;
  2455. 				case EL_ROCK_CRUSHER:
  2456. 					skillratio += 700;
  2457. 					break;
  2458. 				case KO_JYUMONJIKIRI:
  2459. 					skillratio += -100 + 150 * skill_lv;
  2460. 					RE_LVL_DMOD(120);
  2461. 					if( tsc && tsc->data[SC_KO_JYUMONJIKIRI] )
  2462. 						skillratio += status->get_lv(src) * skill_lv;
  2463. 				case KO_HUUMARANKA:
  2464. 					skillratio += -100 + 150 * skill_lv + status_get_agi(src) + status_get_dex(src) + 100 * (sd ? pc->checkskill(sd, NJ_HUUMA) : 0);
  2465. 					break;
  2466. 				case KO_SETSUDAN:
  2467. 					skillratio += -100 + 100 * skill_lv;
  2468. 					RE_LVL_DMOD(100);
  2469. 					break;
  2470. 				case KO_BAKURETSU:
  2471. 					skillratio += -100 + (50 + status_get_dex(src) / 4) * skill_lv * (sd?pc->checkskill(sd,NJ_TOBIDOUGU):10) * 4 / 100;
  2472. 					RE_LVL_DMOD(120);
  2473. 					skillratio += 10 * (sd ? sd->status.job_level : 0);
  2474. 					break;
  2475. 				case MH_NEEDLE_OF_PARALYZE:
  2476. 					skillratio += 600 + 100 * skill_lv;
  2477. 					break;
  2478. 				case MH_STAHL_HORN:
  2479. 					skillratio += 400 + 100 * skill_lv;
  2480. 					break;
  2481. 				case MH_LAVA_SLIDE:
  2482. 					skillratio += -100 + 70 * skill_lv;
  2483. 					break;
  2484. 				case MH_TINDER_BREAKER:
  2485. 				case MH_MAGMA_FLOW:
  2486. 					skillratio += -100 + 100 * skill_lv;
  2487. 					break;
  2488. 			}
  2489. 			//Skill damage modifiers that stack linearly
  2490. 			if(sc && skill_id != PA_SACRIFICE){
  2491. 				if( sc->data[SC_EDP] ){
  2492. 					if( skill_id == AS_SONICBLOW ||
  2493. 						skill_id == GC_COUNTERSLASH ||
  2494. 						skill_id == GC_CROSSIMPACT )
  2495. 							skillratio >>= 1;
  2496. 				}
  2497. 				if(sc->data[SC_OVERTHRUST])
  2498. 					skillratio += sc->data[SC_OVERTHRUST]->val3;
  2499. 				if(sc->data[SC_OVERTHRUSTMAX])
  2500. 					skillratio += sc->data[SC_OVERTHRUSTMAX]->val2;
  2501. 				if(sc->data[SC_BERSERK] || sc->data[SC_SATURDAY_NIGHT_FEVER])
  2502. #ifndef RENEWAL
  2503. 					skillratio += 100;
  2504. #else
  2505. 					skillratio += 200;
  2506. 				if( sc->data[SC_TRUESIGHT] )
  2507. 					skillratio += 2*sc->data[SC_TRUESIGHT]->val1;
  2508. 				if( sc->data[SC_LKCONCENTRATION] )
  2509. 					skillratio += sc->data[SC_LKCONCENTRATION]->val2;
  2510. 				if( sd && sd->status.weapon == W_KATAR && (i=pc->checkskill(sd,ASC_KATAR)) > 0 )
  2511. 					skillratio += skillratio * (10 + 2 * i) / 100;
  2512. #endif
  2513. 			}
  2514. 	}
  2515. 	if( skillratio < 1 )
  2516. 		return 0;
  2517. 	return skillratio;
  2518. }
  2519. /*==========================================
  2520.  * Check dammage trough status.
  2521.  * ATK may be MISS, BLOCKED FAIL, reduc, increase, end status...
  2522.  * After this we apply bg/gvg reduction
  2523.  *------------------------------------------*/
  2524. int64 battle_calc_damage(struct block_list *src,struct block_list *bl,struct Damage *d,int64 damage,uint16 skill_id,uint16 skill_lv) {
  2525. 	struct map_session_data *sd = NULL;
  2526. 	struct status_change *sc;
  2527. 	struct status_change_entry *sce;
  2528. 	int div_ = d->div_, flag = d->flag;
  2529.  
  2530. 	nullpo_ret(bl);
  2531.  
  2532. 	if( !damage )
  2533. 		return 0;
  2534. 	if( battle_config.ksprotection && mob->ksprotected(src, bl) )
  2535. 		return 0;
  2536. 	if( map->getcell(bl->m, bl->x, bl->y, CELL_CHKMAELSTROM) && skill->get_type(skill_id) != BF_MISC 
  2537. 			&& skill->get_casttype(skill_id) == CAST_GROUND )
  2538. 		return 0;
  2539. 	if (bl->type == BL_PC) {
  2540. 		sd=(struct map_session_data *)bl;
  2541. 		//Special no damage states
  2542. 		if(flag&BF_WEAPON && sd->special_state.no_weapon_damage)
  2543. 			damage -= damage * sd->special_state.no_weapon_damage / 100;
  2544.  
  2545. 		if(flag&BF_MAGIC && sd->special_state.no_magic_damage)
  2546. 			damage -= damage * sd->special_state.no_magic_damage / 100;
  2547.  
  2548. 		if(flag&BF_MISC && sd->special_state.no_misc_damage)
  2549. 			damage -= damage * sd->special_state.no_misc_damage / 100;
  2550.  
  2551. 		if(!damage) return 0;
  2552. 	}
  2553.  
  2554. 	sc = status->get_sc(bl);
  2555.  
  2556. 	if( sc && sc->data[SC_INVINCIBLE] && !sc->data[SC_INVINCIBLEOFF] )
  2557. 		return 1;
  2558.  
  2559. 	if (skill_id == PA_PRESSURE)
  2560. 		return damage; //This skill bypass everything else.
  2561.  
  2562. 	if( sc && sc->count )
  2563. 	{
  2564. 		//First, sc_*'s that reduce damage to 0.
  2565. 		if( sc->data[SC_BASILICA] && !(status_get_mode(src)&MD_BOSS) )
  2566. 		{
  2567. 			d->dmg_lv = ATK_BLOCK;
  2568. 			return 0;
  2569. 		}
  2570. 		if( sc->data[SC_WHITEIMPRISON] && skill_id != HW_GRAVITATION ) { // Gravitation and Pressure do damage without removing the effect
  2571. 			if( skill_id == MG_NAPALMBEAT ||
  2572. 				skill_id == MG_SOULSTRIKE ||
  2573. 				skill_id == WL_SOULEXPANSION ||
  2574. 				(skill_id && skill->get_ele(skill_id, skill_lv) == ELE_GHOST) ||
  2575. 				(!skill_id && (status->get_status_data(src))->rhw.ele == ELE_GHOST)
  2576. 					){
  2577. 				if( skill_id == WL_SOULEXPANSION )
  2578. 					damage <<= 1; // If used against a player in White Imprison, the skill deals double damage.
  2579. 				status_change_end(bl,SC_WHITEIMPRISON,INVALID_TIMER); // Those skills do damage and removes effect
  2580. 			}else{
  2581. 				d->dmg_lv = ATK_BLOCK;
  2582. 				return 0;
  2583. 			}
  2584. 		}
  2585.  
  2586. 		if(sc->data[SC_ZEPHYR] &&
  2587. 			flag&(BF_LONG|BF_SHORT)){
  2588. 				d->dmg_lv = ATK_BLOCK;
  2589. 				return 0;
  2590. 		}
  2591.  
  2592. 		if( sc->data[SC_SAFETYWALL] && (flag&(BF_SHORT|BF_MAGIC))==BF_SHORT )
  2593. 		{
  2594. 			struct skill_unit_group* group = skill->id2group(sc->data[SC_SAFETYWALL]->val3);
  2595. 			uint16 skill_id = sc->data[SC_SAFETYWALL]->val2;
  2596. 			if (group) {
  2597. 				if(skill_id == MH_STEINWAND){
  2598. 				    if (--group->val2<=0)
  2599. 					    skill->del_unitgroup(group,ALC_MARK);
  2600. 				    d->dmg_lv = ATK_BLOCK;
  2601. 				    return 0;
  2602. 				}
  2603. 				/**
  2604. 				 * in RE, SW possesses a lifetime equal to 3 times the caster's health
  2605. 				 **/
  2606. 			#ifdef RENEWAL
  2607. 				d->dmg_lv = ATK_BLOCK;
  2608. 				if ( ( group->val2 - damage) > 0 ) {
  2609. 					group->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
  2610. 				} else
  2611. 					skill->del_unitgroup(group,ALC_MARK);
  2612. 				return 0;
  2613. 			#else
  2614. 				if (--group->val2<=0)
  2615. 					skill->del_unitgroup(group,ALC_MARK);
  2616. 				d->dmg_lv = ATK_BLOCK;
  2617. 				return 0;
  2618. 			#endif
  2619. 			}
  2620. 			status_change_end(bl, SC_SAFETYWALL, INVALID_TIMER);
  2621. 		}
  2622.  
  2623. 		if( ( sc->data[SC_PNEUMA] && (flag&(BF_MAGIC|BF_LONG)) == BF_LONG ) || sc->data[SC__MANHOLE] ) {
  2624. 			d->dmg_lv = ATK_BLOCK;
  2625. 			return 0;
  2626. 		}
  2627. 		if( sc->data[SC_WEAPONBLOCKING] && flag&(BF_SHORT|BF_WEAPON) && rnd()%100 < sc->data[SC_WEAPONBLOCKING]->val2 )
  2628. 		{
  2629. 			clif->skill_nodamage(bl,src,GC_WEAPONBLOCKING,1,1);
  2630. 			d->dmg_lv = ATK_BLOCK;
  2631. 			sc_start2(bl,SC_COMBOATTACK,100,GC_WEAPONBLOCKING,src->id,2000);
  2632. 			return 0;
  2633. 		}
  2634. 		if( (sce=sc->data[SC_AUTOGUARD]) && flag&BF_WEAPON && !(skill->get_nk(skill_id)&NK_NO_CARDFIX_ATK) && rnd()%100 < sce->val2 )
  2635. 		{
  2636. 			int delay;
  2637. 			clif->skill_nodamage(bl,bl,CR_AUTOGUARD,sce->val1,1);
  2638. 			// different delay depending on skill level [celest]
  2639. 			if (sce->val1 <= 5)
  2640. 				delay = 300;
  2641. 			else if (sce->val1 > 5 && sce->val1 <= 9)
  2642. 				delay = 200;
  2643. 			else
  2644. 				delay = 100;
  2645. 			unit->set_walkdelay(bl, timer->gettick(), delay, 1);
  2646.  
  2647. 			if(sc->data[SC_CR_SHRINK] && rnd()%100<5*sce->val1)
  2648. 				skill->blown(bl,src,skill->get_blewcount(CR_SHRINK,1),-1,0);
  2649. 			return 0;
  2650. 		}
  2651.  
  2652. 		if( (sce = sc->data[SC_MILLENNIUMSHIELD]) && sce->val2 > 0 && damage > 0 ) {
  2653. 			clif->skill_nodamage(bl, bl, RK_MILLENNIUMSHIELD, 1, 1);
  2654. 			sce->val3 -= (int)cap_value(damage,INT_MIN,INT_MAX); // absorb damage
  2655. 			d->dmg_lv = ATK_BLOCK;
  2656. 			sc_start(bl,SC_STUN,15,0,skill->get_time2(RK_MILLENNIUMSHIELD,sce->val1)); // There is a chance to be stuned when one shield is broken.
  2657. 			if( sce->val3 <= 0 ) { // Shield Down
  2658. 				sce->val2--;
  2659. 				if( sce->val2 > 0 ) {
  2660. 					if( sd )
  2661. 						clif->millenniumshield(sd,sce->val2);
  2662. 					sce->val3 = 1000; // Next Shield
  2663. 				} else
  2664. 					status_change_end(bl,SC_MILLENNIUMSHIELD,INVALID_TIMER); // All shields down
  2665. 			}
  2666. 			return 0;
  2667. 		}
  2668.  
  2669.  
  2670. 		if( (sce=sc->data[SC_PARRYING]) && flag&BF_WEAPON && skill_id != WS_CARTTERMINATION && rnd()%100 < sce->val2 )
  2671. 		{ // attack blocked by Parrying
  2672. 			clif->skill_nodamage(bl, bl, LK_PARRYING, sce->val1,1);
  2673. 			return 0;
  2674. 		}
  2675.  
  2676. 		if(sc->data[SC_DODGE_READY] && ( !sc->opt1 || sc->opt1 == OPT1_BURNING ) &&
  2677. 			(flag&BF_LONG || sc->data[SC_STRUP])
  2678. 			&& rnd()%100 < 20) {
  2679. 			if (sd && pc_issit(sd)) pc->setstand(sd); //Stand it to dodge.
  2680. 			clif->skill_nodamage(bl,bl,TK_DODGE,1,1);
  2681. 			if (!sc->data[SC_COMBOATTACK])
  2682. 				sc_start4(bl, SC_COMBOATTACK, 100, TK_JUMPKICK, src->id, 1, 0, 2000);
  2683. 			return 0;
  2684. 		}
  2685.  
  2686. 		if(sc->data[SC_HERMODE] && flag&BF_MAGIC)
  2687. 			return 0;
  2688.  
  2689. 		if(sc->data[SC_NJ_TATAMIGAESHI] && (flag&(BF_MAGIC|BF_LONG)) == BF_LONG)
  2690. 			return 0;
  2691.  
  2692. 		if( sc->data[SC_NEUTRALBARRIER] && (flag&(BF_MAGIC|BF_LONG)) == (BF_MAGIC|BF_LONG) ) {
  2693. 			d->dmg_lv = ATK_MISS;
  2694. 			return 0;
  2695. 		}
  2696.  
  2697. 		if((sce=sc->data[SC_KAUPE]) && rnd()%100 < sce->val2)
  2698. 		{	//Kaupe blocks damage (skill or otherwise) from players, mobs, homuns, mercenaries.
  2699. 			clif->specialeffect(bl, 462, AREA);
  2700. 			//Shouldn't end until Breaker's non-weapon part connects.
  2701. 			if (skill_id != ASC_BREAKER || !(flag&BF_WEAPON))
  2702. 				if (--(sce->val3) <= 0) //We make it work like Safety Wall, even though it only blocks 1 time.
  2703. 					status_change_end(bl, SC_KAUPE, INVALID_TIMER);
  2704. 			return 0;
  2705. 		}
  2706.  
  2707. 		if( flag&BF_MAGIC && (sce=sc->data[SC_PRESTIGE]) && rnd()%100 < sce->val2) {
  2708. 			clif->specialeffect(bl, 462, AREA); // Still need confirm it.
  2709. 			return 0;
  2710. 		}
  2711.  
  2712. 		if (((sce=sc->data[SC_NJ_UTSUSEMI]) || sc->data[SC_NJ_BUNSINJYUTSU])
  2713. 		&& flag&BF_WEAPON && !(skill->get_nk(skill_id)&NK_NO_CARDFIX_ATK)) {
  2714.  
  2715. 			skill->additional_effect (src, bl, skill_id, skill_lv, flag, ATK_BLOCK, timer->gettick() );
  2716. 			if( !status->isdead(src) )
  2717. 				skill->counter_additional_effect( src, bl, skill_id, skill_lv, flag, timer->gettick() );
  2718. 			if (sce) {
  2719. 				clif->specialeffect(bl, 462, AREA);
  2720. 				skill->blown(src,bl,sce->val3,-1,0);
  2721. 			}
  2722. 			//Both need to be consumed if they are active.
  2723. 			if (sce && --(sce->val2) <= 0)
  2724. 				status_change_end(bl, SC_NJ_UTSUSEMI, INVALID_TIMER);
  2725. 			if ((sce=sc->data[SC_NJ_BUNSINJYUTSU]) && --(sce->val2) <= 0)
  2726. 				status_change_end(bl, SC_NJ_BUNSINJYUTSU, INVALID_TIMER);
  2727.  
  2728. 			return 0;
  2729. 		}
  2730.  
  2731. 		//Now damage increasing effects
  2732. 		if( sc->data[SC_LEXAETERNA] && skill_id != PF_SOULBURN )
  2733. 		{
  2734. 			if( src->type != BL_MER || skill_id == 0 )
  2735. 				damage <<= 1; // Lex Aeterna only doubles damage of regular attacks from mercenaries
  2736.  
  2737. 			if( skill_id != ASC_BREAKER || !(flag&BF_WEAPON) )
  2738. 				status_change_end(bl, SC_LEXAETERNA, INVALID_TIMER); //Shouldn't end until Breaker's non-weapon part connects.
  2739. 		}
  2740.  
  2741. #ifdef RENEWAL
  2742. 		if( sc->data[SC_RAID] ) {
  2743. 			damage += damage * 20 / 100;
  2744.  
  2745. 			if (--sc->data[SC_RAID]->val1 == 0)
  2746. 				status_change_end(bl, SC_RAID, INVALID_TIMER);
  2747. 		}
  2748. #endif
  2749.  
  2750. 		if( damage ) {
  2751. 			struct map_session_data *tsd = BL_CAST(BL_PC, src);
  2752. 			if( sc->data[SC_DEEP_SLEEP] ) {
  2753. 				damage += damage / 2; // 1.5 times more damage while in Deep Sleep.
  2754. 				status_change_end(bl,SC_DEEP_SLEEP,INVALID_TIMER);
  2755. 			}
  2756. 			if( tsd && sd && sc->data[SC_COLD] && flag&BF_WEAPON ){
  2757. 				switch(tsd->status.weapon){
  2758. 					case W_MACE:
  2759. 					case W_2HMACE:
  2760. 					case W_1HAXE:
  2761. 					case W_2HAXE:
  2762. 						damage = damage * 150/100;
  2763. 						break;
  2764. 					case W_MUSICAL:
  2765. 					case W_WHIP:
  2766. 						if(!sd->state.arrow_atk)
  2767. 							break;
  2768. 					case W_BOW:
  2769. 					case W_REVOLVER:
  2770. 					case W_RIFLE:
  2771. 					case W_GATLING:
  2772. 					case W_SHOTGUN:
  2773. 					case W_GRENADE:
  2774. 					case W_DAGGER:
  2775. 					case W_1HSWORD:
  2776. 					case W_2HSWORD:
  2777. 						damage = damage * 50/100;
  2778. 						break;
  2779. 				}
  2780. 			}
  2781. 			if( sc->data[SC_SIREN] )
  2782. 				status_change_end(bl,SC_SIREN,INVALID_TIMER);
  2783. 		}
  2784.  
  2785. 		//Finally damage reductions....
  2786. 		// Assumptio doubles the def & mdef on RE mode, otherwise gives a reduction on the final damage. [Igniz]
  2787. #ifndef RENEWAL
  2788. 		if( sc->data[SC_ASSUMPTIO] ) {
  2789. 			if( map_flag_vs(bl->m) )
  2790. 				damage = damage*2/3; //Receive 66% damage
  2791. 			else
  2792. 				damage >>= 1; //Receive 50% damage
  2793. 		}
  2794. #endif
  2795.  
  2796. 		if(sc->data[SC_DEFENDER] &&
  2797. 			(flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON))
  2798. 			damage = damage * ( 100 - sc->data[SC_DEFENDER]->val2 ) / 100;
  2799.  
  2800. 		if(sc->data[SC_GS_ADJUSTMENT] &&
  2801. 			(flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON))
  2802. 			damage -= damage * 20 / 100;
  2803.  
  2804. 		if(sc->data[SC_FOGWALL] && skill_id != RK_DRAGONBREATH && skill_id != RK_DRAGONBREATH_WATER) {
  2805. 			if(flag&BF_SKILL) //25% reduction
  2806. 				damage -= damage * 25 / 100;
  2807. 			else if ((flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON))
  2808. 				damage >>= 2; //75% reduction
  2809. 		}
  2810.  
  2811. 		// Compressed code, fixed by map.h [Epoque]
  2812. 		if (src->type == BL_MOB) {
  2813. 			int i;
  2814. 			if (sc->data[SC_MANU_DEF])
  2815. 				for (i=0;ARRAYLENGTH(mob->manuk)>i;i++)
  2816. 					if (mob->manuk[i]==((TBL_MOB*)src)->class_) {
  2817. 						damage -= damage * sc->data[SC_MANU_DEF]->val1 / 100;
  2818. 						break;
  2819. 					}
  2820. 			if (sc->data[SC_SPL_DEF])
  2821. 				for (i=0;ARRAYLENGTH(mob->splendide)>i;i++)
  2822. 					if (mob->splendide[i]==((TBL_MOB*)src)->class_) {
  2823. 						damage -= damage * sc->data[SC_SPL_DEF]->val1 / 100;
  2824. 						break;
  2825. 					}
  2826. 		}
  2827.  
  2828. 		if((sce=sc->data[SC_ARMOR]) && //NPC_DEFENDER
  2829. 			sce->val3&flag && sce->val4&flag)
  2830. 			damage -= damage * sc->data[SC_ARMOR]->val2 / 100;
  2831.  
  2832. #ifdef RENEWAL
  2833. 		if(sc->data[SC_ENERGYCOAT] && (flag&BF_WEAPON || flag&BF_MAGIC) && skill_id != WS_CARTTERMINATION)
  2834. #else
  2835. 		if(sc->data[SC_ENERGYCOAT] && (flag&BF_WEAPON && skill_id != WS_CARTTERMINATION))
  2836. #endif
  2837. 		{
  2838. 			struct status_data *sstatus = status->get_status_data(bl);
  2839. 			int per = 100*sstatus->sp / sstatus->max_sp -1; //100% should be counted as the 80~99% interval
  2840. 			per /=20; //Uses 20% SP intervals.
  2841. 			//SP Cost: 1% + 0.5% per every 20% SP
  2842. 			if (!status->charge(bl, 0, (10+5*per)*sstatus->max_sp/1000))
  2843. 				status_change_end(bl, SC_ENERGYCOAT, INVALID_TIMER);
  2844. 			//Reduction: 6% + 6% every 20%
  2845. 			damage -= damage * (6 * (1+per)) / 100;
  2846. 		}
  2847. 		if(sc->data[SC_GRANITIC_ARMOR]){
  2848. 			damage -= damage * sc->data[SC_GRANITIC_ARMOR]->val2 / 100;
  2849. 		}
  2850. 		if(sc->data[SC_PAIN_KILLER]){
  2851. 			damage -= damage * sc->data[SC_PAIN_KILLER]->val3 / 100;
  2852. 		}
  2853. 		if((sce=sc->data[SC_MAGMA_FLOW]) && (rnd()%100 <= sce->val2) ){
  2854. 			skill->castend_damage_id(bl,src,MH_MAGMA_FLOW,sce->val1,timer->gettick(),0);
  2855. 		}
  2856.  
  2857. 		if( (sce = sc->data[SC_STONEHARDSKIN]) && flag&(BF_SHORT|BF_WEAPON) && damage > 0 ) {
  2858. 			sce->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
  2859. 			if( src->type == BL_PC ) {
  2860. 				TBL_PC *ssd = BL_CAST(BL_PC, src);
  2861. 				if (ssd && ssd->status.weapon != W_BOW)
  2862. 					skill->break_equip(src, EQP_WEAPON, 3000, BCT_SELF);
  2863. 			} else
  2864. 				skill->break_equip(src, EQP_WEAPON, 3000, BCT_SELF);
  2865. 			// 30% chance to reduce monster's ATK by 25% for 10 seconds.
  2866. 			if( src->type == BL_MOB )
  2867. 				sc_start(src, SC_NOEQUIPWEAPON, 30, 0, skill->get_time2(RK_STONEHARDSKIN, sce->val1));
  2868. 			if( sce->val2 <= 0 )
  2869. 				status_change_end(bl, SC_STONEHARDSKIN, INVALID_TIMER);
  2870. 		}
  2871.  
  2872. /**
  2873.  * In renewal steel body reduces all incoming damage by 1/10
  2874.  **/
  2875. #ifdef RENEWAL
  2876. 		if( sc->data[SC_STEELBODY] ) {
  2877. 			damage = damage > 10 ? damage / 10 : 1;
  2878. 		}
  2879. #endif
  2880.  
  2881. 		//Finally added to remove the status of immobile when aimedbolt is used. [Jobbie]
  2882. 		if( skill_id == RA_AIMEDBOLT && (sc->data[SC_WUGBITE] || sc->data[SC_ANKLESNARE] || sc->data[SC_ELECTRICSHOCKER]) )
  2883. 		{
  2884. 			status_change_end(bl, SC_WUGBITE, INVALID_TIMER);
  2885. 			status_change_end(bl, SC_ANKLESNARE, INVALID_TIMER);
  2886. 			status_change_end(bl, SC_ELECTRICSHOCKER, INVALID_TIMER);
  2887. 		}
  2888.  
  2889. 		//Finally Kyrie because it may, or not, reduce damage to 0.
  2890. 		if((sce = sc->data[SC_KYRIE]) && damage > 0){
  2891. 			sce->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
  2892. 			if(flag&BF_WEAPON || skill_id == TF_THROWSTONE){
  2893. 				if(sce->val2>=0)
  2894. 					damage=0;
  2895. 				else
  2896. 					damage=-sce->val2;
  2897. 			}
  2898. 			if((--sce->val3)<=0 || (sce->val2<=0) || skill_id == AL_HOLYLIGHT)
  2899. 				status_change_end(bl, SC_KYRIE, INVALID_TIMER);
  2900. 		}
  2901.  
  2902. 		if( sc->data[SC_MEIKYOUSISUI] && rand()%100 < 40 ) // custom value
  2903. 			damage = 0;
  2904.  
  2905.  
  2906. 		if (!damage) return 0;
  2907.  
  2908. 		if( (sce = sc->data[SC_LIGHTNINGWALK]) && flag&BF_LONG && rnd()%100 < sce->val1 ) {
  2909. 			int dx[8]={0,-1,-1,-1,0,1,1,1};
  2910. 			int dy[8]={1,1,0,-1,-1,-1,0,1};
  2911. 			uint8 dir = map->calc_dir(bl, src->x, src->y);
  2912. 			if( unit->movepos(bl, src->x-dx[dir], src->y-dy[dir], 1, 1) ) {
  2913. 				clif->slide(bl,src->x-dx[dir],src->y-dy[dir]);
  2914. 				unit->setdir(bl, dir);
  2915. 			}
  2916. 			d->dmg_lv = ATK_DEF;
  2917. 			status_change_end(bl, SC_LIGHTNINGWALK, INVALID_TIMER);
  2918. 			return 0;
  2919. 		}
  2920.  
  2921. 		//Probably not the most correct place, but it'll do here
  2922. 		//(since battle_drain is strictly for players currently)
  2923. 		if ((sce=sc->data[SC_HAMI_BLOODLUST]) && flag&BF_WEAPON && damage > 0 &&
  2924. 			rnd()%100 < sce->val3)
  2925. 			status->heal(src, damage*sce->val4/100, 0, 3);
  2926.  
  2927. 		if( sd && (sce = sc->data[SC_FORCEOFVANGUARD]) && flag&BF_WEAPON && rnd()%100 < sce->val2 )
  2928. 			pc->addspiritball(sd,skill->get_time(LG_FORCEOFVANGUARD,sce->val1),sce->val3);
  2929. 		if (sc->data[SC_STYLE_CHANGE] && rnd()%2) {
  2930. 			TBL_HOM *hd = BL_CAST(BL_HOM,bl);
  2931. 			if (hd) homun->addspiritball(hd, 10); //add a sphere
  2932. 		}
  2933.  
  2934. 		if( sc->data[SC__DEADLYINFECT] && damage > 0 && rnd()%100 < 65 + 5 * sc->data[SC__DEADLYINFECT]->val1 )
  2935. 			status->change_spread(bl, src); // Deadly infect attacked side
  2936.  
  2937. 		if( sc && sc->data[SC__SHADOWFORM] ) {
  2938. 			struct block_list *s_bl = map->id2bl(sc->data[SC__SHADOWFORM]->val2);
  2939. 			if( !s_bl || s_bl->m != bl->m ) { // If the shadow form target is not present remove the sc.
  2940. 				status_change_end(bl, SC__SHADOWFORM, INVALID_TIMER);
  2941. 			} else if( status->isdead(s_bl) || !battle->check_target(src,s_bl,BCT_ENEMY)) { // If the shadow form target is dead or not your enemy remove the sc in both.
  2942. 				status_change_end(bl, SC__SHADOWFORM, INVALID_TIMER);
  2943. 				if( s_bl->type == BL_PC )
  2944. 					((TBL_PC*)s_bl)->shadowform_id = 0;
  2945. 			} else {
  2946. 				if( (--sc->data[SC__SHADOWFORM]->val3) < 0 ) { // If you have exceded max hits supported, remove the sc in both.
  2947. 					status_change_end(bl, SC__SHADOWFORM, INVALID_TIMER);
  2948. 					if( s_bl->type == BL_PC )
  2949. 						((TBL_PC*)s_bl)->shadowform_id = 0;
  2950. 				} else {
  2951. 					status->damage(bl, s_bl, damage, 0, clif->damage(s_bl, s_bl, timer->gettick(), 500, 500, damage, -1, 0, 0), 0);
  2952. 					return ATK_NONE;
  2953. 				}
  2954. 			}
  2955. 		}
  2956.  
  2957. 	}
  2958.  
  2959. 	//SC effects from caster side.
  2960. 	sc = status->get_sc(src);
  2961.  
  2962. 	if (sc && sc->count) {
  2963. 		if( sc->data[SC_INVINCIBLE] && !sc->data[SC_INVINCIBLEOFF] )
  2964. 			damage += damage * 75 / 100;
  2965. 		// [Epoque]
  2966. 		if (bl->type == BL_MOB) {
  2967. 			int i;
  2968.  
  2969. 			if ( ((sce=sc->data[SC_MANU_ATK]) && (flag&BF_WEAPON)) ||
  2970. 				 ((sce=sc->data[SC_MANU_MATK]) && (flag&BF_MAGIC))
  2971. 				)
  2972. 				for (i=0;ARRAYLENGTH(mob->manuk)>i;i++)
  2973. 					if (((TBL_MOB*)bl)->class_==mob->manuk[i]) {
  2974. 						damage += damage * sce->val1 / 100;
  2975. 						break;
  2976. 					}
  2977. 			if ( ((sce=sc->data[SC_SPL_ATK]) && (flag&BF_WEAPON)) ||
  2978. 				 ((sce=sc->data[SC_SPL_MATK]) && (flag&BF_MAGIC))
  2979. 				)
  2980. 				for (i=0;ARRAYLENGTH(mob->splendide)>i;i++)
  2981. 					if (((TBL_MOB*)bl)->class_==mob->splendide[i]) {
  2982. 						damage += damage * sce->val1 / 100;
  2983. 						break;
  2984. 					}
  2985. 		}
  2986. 		if( sc->data[SC_POISONINGWEAPON] && skill_id != GC_VENOMPRESSURE && (flag&BF_WEAPON) && damage > 0 && rnd()%100 < sc->data[SC_POISONINGWEAPON]->val3 )
  2987. 			sc_start(bl,sc->data[SC_POISONINGWEAPON]->val2,100,sc->data[SC_POISONINGWEAPON]->val1,skill->get_time2(GC_POISONINGWEAPON, 1));
  2988. 		if( sc->data[SC__DEADLYINFECT] && damage > 0 && rnd()%100 < 65 + 5 * sc->data[SC__DEADLYINFECT]->val1 )
  2989. 			status->change_spread(src, bl);
  2990.                 if (sc->data[SC_STYLE_CHANGE] && rnd()%2) {
  2991.                     TBL_HOM *hd = BL_CAST(BL_HOM,bl);
  2992.                     if (hd) homun->addspiritball(hd, 10);
  2993.                 }
  2994. 	}
  2995. 	/* no data claims these settings affect anything other than players */
  2996. 	if( damage && sd && bl->type == BL_PC ) {
  2997. 		switch( skill_id ) {
  2998. 			//case PA_PRESSURE: /* pressure also belongs to this list but it doesn't reach this area -- so dont worry about it */
  2999. 			case HW_GRAVITATION:
  3000. 			case NJ_ZENYNAGE:
  3001. 			case KO_MUCHANAGE:
  3002. 				break;
  3003. 			default:
  3004. 				if (flag & BF_SKILL) { //Skills get a different reduction than non-skills. [Skotlex]
  3005. 					if (flag&BF_WEAPON)
  3006. 						damage = damage * maplist[bl->m].weapon_damage_rate / 100;
  3007. 					if (flag&BF_MAGIC)
  3008. 						damage = damage * maplist[bl->m].magic_damage_rate / 100;
  3009. 					if (flag&BF_MISC)
  3010. 						damage = damage * maplist[bl->m].misc_damage_rate / 100;
  3011. 				} else { //Normal attacks get reductions based on range.
  3012. 					if (flag & BF_SHORT)
  3013. 						damage = damage * maplist[bl->m].short_damage_rate / 100;
  3014. 					if (flag & BF_LONG)
  3015. 						damage = damage * maplist[bl->m].long_damage_rate / 100;
  3016. 				}
  3017. 				if(!damage) damage  = 1;
  3018. 				break;
  3019. 		}
  3020. 	}
  3021.  
  3022. 	if(battle_config.skill_min_damage && damage > 0 && damage < div_)
  3023. 	{
  3024. 		if ((flag&BF_WEAPON && battle_config.skill_min_damage&1)
  3025. 			|| (flag&BF_MAGIC && battle_config.skill_min_damage&2)
  3026. 			|| (flag&BF_MISC && battle_config.skill_min_damage&4)
  3027. 		)
  3028. 			damage = div_;
  3029. 	}
  3030.  
  3031. 	if( bl->type == BL_MOB && !status->isdead(bl) && src != bl) {
  3032. 	  if (damage > 0 )
  3033. 			mob->skill_event((TBL_MOB*)bl,src,timer->gettick(),flag);
  3034. 	  if (skill_id)
  3035. 			mob->skill_event((TBL_MOB*)bl,src,timer->gettick(),MSC_SKILLUSED|(skill_id<<16));
  3036. 	}
  3037. 	if( sd ) {
  3038. 		if( pc_ismadogear(sd) && rnd()%100 < 50 ) {
  3039. 			short element = skill->get_ele(skill_id, skill_lv);
  3040. 			if( !skill_id || element == -1 ) { //Take weapon's element
  3041. 				struct status_data *sstatus = NULL;
  3042. 				if( src->type == BL_PC && ((TBL_PC*)src)->bonus.arrow_ele )
  3043. 					element = ((TBL_PC*)src)->bonus.arrow_ele;
  3044. 				else if( (sstatus = status->get_status_data(src)) ) {
  3045. 					element = sstatus->rhw.ele;
  3046. 				}
  3047. 			}
  3048. 			else if( element == -2 ) //Use enchantment's element
  3049. 				element = status_get_attack_sc_element(src,status->get_sc(src));
  3050. 			else if( element == -3 ) //Use random element
  3051. 				element = rnd()%ELE_MAX;
  3052. 			if( element == ELE_FIRE || element == ELE_WATER )
  3053. 				pc->overheat(sd,element == ELE_FIRE ? 1 : -1);
  3054. 		}
  3055. 	}
  3056.  
  3057. 	return damage;
  3058. }
  3059.  
  3060. /*==========================================
  3061.  * Calculates BG related damage adjustments.
  3062.  *------------------------------------------*/
  3063. int64 battle_calc_bg_damage(struct block_list *src, struct block_list *bl, int64 damage, int div_, uint16 skill_id, uint16 skill_lv, int flag)
  3064. {
  3065. 	if( !damage )
  3066. 		return 0;
  3067.  
  3068. 	if( bl->type == BL_MOB ) {
  3069. 		struct mob_data* md = BL_CAST(BL_MOB, bl);
  3070.  
  3071. 		if( flag&BF_SKILL && (md->class_ == MOBID_BLUE_CRYST || md->class_ == MOBID_PINK_CRYST) )
  3072. 			return 0; // Crystal cannot receive skill damage on battlegrounds
  3073. 	}
  3074.  
  3075. 	return damage;
  3076. }
  3077.  
  3078. /*==========================================
  3079.  * Calculates GVG related damage adjustments.
  3080.  *------------------------------------------*/
  3081. int64 battle_calc_gvg_damage(struct block_list *src,struct block_list *bl,int64 damage,int div_,uint16 skill_id,uint16 skill_lv,int flag) {
  3082. 	struct mob_data* md = BL_CAST(BL_MOB, bl);
  3083. 	int class_ = status->get_class(bl);
  3084.  
  3085. 	if (!damage) //No reductions to make.
  3086. 		return 0;
  3087.  
  3088. 	if(md && md->guardian_data) {
  3089. 		if(class_ == MOBID_EMPERIUM && flag&BF_SKILL) {
  3090. 		//Skill immunity.
  3091. 			switch (skill_id) {
  3092. #ifndef RENEWAL
  3093. 			case MO_TRIPLEATTACK:
  3094. #endif
  3095. 			case HW_GRAVITATION:
  3096. 				break;
  3097. 			default:
  3098. 				return 0;
  3099. 			}
  3100. 		}
  3101. 		if(src->type != BL_MOB) {
  3102. 			struct guild *g = src->type == BL_PC ? ((TBL_PC *)src)->guild : guild->search(status->get_guild_id(src));
  3103.  
  3104. 			if (class_ == MOBID_EMPERIUM && (!g || guild->checkskill(g,GD_APPROVAL) <= 0 ))
  3105. 				return 0;
  3106.  
  3107. 			if (g && battle_config.guild_max_castles && guild->checkcastles(g)>=battle_config.guild_max_castles)
  3108. 				return 0; // [MouseJstr]
  3109. 		}
  3110. 	}
  3111.  
  3112. 	switch (skill_id) {
  3113. 		case PA_PRESSURE:
  3114. 		case HW_GRAVITATION:
  3115. 		case NJ_ZENYNAGE:
  3116. 		case KO_MUCHANAGE:
  3117. 			break;
  3118. 		default:
  3119. 			/* Uncomment if you want god-mode Emperiums at 100 defense. [Kisuka]
  3120. 			if (md && md->guardian_data) {
  3121. 				damage -= damage * (md->guardian_data->castle->defense/100) * battle_config.castle_defense_rate/100;
  3122. 			}
  3123. 			*/
  3124. 			break;
  3125. 	}
  3126.  
  3127. 	return damage;
  3128. }
  3129.  
  3130. /*==========================================
  3131.  * HP/SP drain calculation
  3132.  *------------------------------------------*/
  3133. int battle_calc_drain(int64 damage, int rate, int per) {
  3134. 	int64 diff = 0;
  3135.  
  3136. 	if (per && rnd()%1000 < rate) {
  3137. 		diff = (damage * per) / 100;
  3138. 		if (diff == 0) {
  3139. 			if (per > 0)
  3140. 				diff = 1;
  3141. 			else
  3142. 				diff = -1;
  3143. 		}
  3144. 	}
  3145. 	return (int)cap_value(diff,INT_MIN,INT_MAX);
  3146. }
  3147.  
  3148. /*==========================================
  3149.  * Consumes ammo for the given skill.
  3150.  *------------------------------------------*/
  3151. void battle_consume_ammo(TBL_PC*sd, int skill_id, int lv) {
  3152. 	int qty=1;
  3153. 	if (!battle_config.arrow_decrement)
  3154. 		return;
  3155.  
  3156. 	if (skill_id) {
  3157. 		qty = skill->get_ammo_qty(skill_id, lv);
  3158. 		if (!qty) qty = 1;
  3159. 	}
  3160.  
  3161. 	if(sd->equip_index[EQI_AMMO]>=0) //Qty check should have been done in skill_check_condition
  3162. 		pc->delitem(sd,sd->equip_index[EQI_AMMO],qty,0,1,LOG_TYPE_CONSUME);
  3163.  
  3164. 	sd->state.arrow_atk = 0;
  3165. }
  3166. //Skill Range Criteria
  3167. int battle_range_type(struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv) {
  3168. 	if (battle_config.skillrange_by_distance &&
  3169. 		(src->type&battle_config.skillrange_by_distance)
  3170. 	) { //based on distance between src/target [Skotlex]
  3171. 		if (check_distance_bl(src, target, 5))
  3172. 			return BF_SHORT;
  3173. 		return BF_LONG;
  3174. 	}
  3175. 	//based on used skill's range
  3176. 	if (skill->get_range2(src, skill_id, skill_lv) < 5)
  3177. 		return BF_SHORT;
  3178. 	return BF_LONG;
  3179. }
  3180. int battle_adjust_skill_damage(int m, unsigned short skill_id) {
  3181. 	if( maplist[m].skill_count ) {
  3182. 		int i;
  3183. 		ARR_FIND(0, maplist[m].skill_count, i, maplist[m].skills[i]->skill_id == skill_id );
  3184.  
  3185. 		if( i < maplist[m].skill_count ) {
  3186. 			return maplist[m].skills[i]->modifier;
  3187. 		}
  3188.  
  3189. 	}
  3190.  
  3191. 	return 0;
  3192. }
  3193. int battle_blewcount_bonus(struct map_session_data *sd, uint16 skill_id) {
  3194. 	int i;
  3195. 	if (!sd->skillblown[0].id)
  3196. 		return 0;
  3197. 	//Apply the bonus blewcount. [Skotlex]
  3198. 	for (i = 0; i < ARRAYLENGTH(sd->skillblown) && sd->skillblown[i].id; i++) {
  3199. 		if (sd->skillblown[i].id == skill_id)
  3200. 			return sd->skillblown[i].val;
  3201. 	}
  3202. 	return 0;
  3203. }
  3204. //For quick div adjustment.
  3205. #define damage_div_fix(dmg, div) { if (div > 1) (dmg)*=div; else if (div < 0) (div)*=-1; }
  3206. /*==========================================
  3207.  * battle_calc_magic_attack [DracoRPG]
  3208.  *------------------------------------------*/
  3209. struct Damage battle_calc_magic_attack(struct block_list *src,struct block_list *target,uint16 skill_id,uint16 skill_lv,int mflag) {
  3210. 	int i, nk;
  3211. 	short s_ele = 0;
  3212. 	unsigned int skillratio = 100;	//Skill dmg modifiers.
  3213.  
  3214.     TBL_PC *sd;
  3215. 	struct status_change *sc;
  3216. 	struct Damage ad;
  3217. 	struct status_data *sstatus = status->get_status_data(src);
  3218. 	struct status_data *tstatus = status->get_status_data(target);
  3219. 	struct {
  3220. 		unsigned imdef : 1;
  3221. 		unsigned infdef : 1;
  3222. 	}	flag;
  3223.  
  3224. 	memset(&ad,0,sizeof(ad));
  3225. 	memset(&flag,0,sizeof(flag));
  3226.  
  3227. 	if(src==NULL || target==NULL)
  3228. 	{
  3229. 		nullpo_info(NLP_MARK);
  3230. 		return ad;
  3231. 	}
  3232. 	//Initial Values
  3233. 	ad.damage = 1;
  3234. 	ad.div_=skill->get_num(skill_id,skill_lv);
  3235. 	ad.amotion=skill->get_inf(skill_id)&INF_GROUND_SKILL?0:sstatus->amotion; //Amotion should be 0 for ground skills.
  3236. 	ad.dmotion=tstatus->dmotion;
  3237. 	ad.blewcount = skill->get_blewcount(skill_id,skill_lv);
  3238. 	ad.flag=BF_MAGIC|BF_SKILL;
  3239. 	ad.dmg_lv=ATK_DEF;
  3240. 	nk = skill->get_nk(skill_id);
  3241. 	flag.imdef = nk&NK_IGNORE_DEF?1:0;
  3242.  
  3243. 	sd = BL_CAST(BL_PC, src);
  3244.  
  3245. 	sc = status->get_sc(src);
  3246.  
  3247. 	//Initialize variables that will be used afterwards
  3248. 	s_ele = skill->get_ele(skill_id, skill_lv);
  3249.  
  3250. 	if (s_ele == -1){ // pl=-1 : the skill takes the weapon's element
  3251. 		s_ele = sstatus->rhw.ele;
  3252. 		if( sd ){ //Summoning 10 charm will endow your weapon
  3253. 			ARR_FIND(1, 6, i, sd->charm[i] >= 10);
  3254. 			if( i < 5 ) s_ele = i;
  3255. 		}
  3256. 	}else if (s_ele == -2) //Use status element
  3257. 		s_ele = status_get_attack_sc_element(src,status->get_sc(src));
  3258. 	else if( s_ele == -3 ) //Use random element
  3259. 		s_ele = rnd()%ELE_MAX;
  3260.  
  3261. 	if( skill_id == SO_PSYCHIC_WAVE ) {
  3262. 		if( sc && sc->count ) {
  3263. 			if( sc->data[SC_HEATER_OPTION] ) s_ele = sc->data[SC_HEATER_OPTION]->val4;
  3264. 			else if( sc->data[SC_COOLER_OPTION] ) s_ele = sc->data[SC_COOLER_OPTION]->val4;
  3265. 			else if( sc->data[SC_BLAST_OPTION] ) s_ele = sc->data[SC_BLAST_OPTION]->val3;
  3266. 			else if( sc->data[SC_CURSED_SOIL_OPTION] ) s_ele = sc->data[SC_CURSED_SOIL_OPTION]->val4;
  3267. 		}
  3268. 	}
  3269.  
  3270. 	//Set miscellaneous data that needs be filled
  3271. 	if(sd) {
  3272. 		sd->state.arrow_atk = 0;
  3273. 		ad.blewcount += battle->blewcount_bonus(sd, skill_id);
  3274. 	}
  3275.  
  3276. 	//Skill Range Criteria
  3277. 	ad.flag |= battle->range_type(src, target, skill_id, skill_lv);
  3278. 	flag.infdef=(tstatus->mode&MD_PLANT?1:0);
  3279. 	if( target->type == BL_SKILL){
  3280. 		TBL_SKILL *su = (TBL_SKILL*)target;
  3281. 		if( su->group && (su->group->skill_id == WM_REVERBERATION || su->group->skill_id == WM_POEMOFNETHERWORLD) )
  3282. 			flag.infdef = 1;
  3283. 	}
  3284.  
  3285. 	switch(skill_id) {
  3286. 		case MG_FIREWALL:
  3287. 		case NJ_KAENSIN:
  3288. 			ad.dmotion = 0; //No flinch animation.
  3289. 			if ( tstatus->def_ele == ELE_FIRE || battle->check_undead(tstatus->race, tstatus->def_ele) )
  3290. 				ad.blewcount = 0; //No knockback
  3291. 			break;
  3292. 		case PR_SANCTUARY:
  3293. 			ad.dmotion = 0; //No flinch animation.
  3294. 			break;
  3295. 		case WL_HELLINFERNO:
  3296. 			if( mflag&ELE_DARK )
  3297. 				s_ele = ELE_DARK;
  3298. 			break;
  3299. 		case KO_KAIHOU:
  3300. 			if( sd ){
  3301. 				ARR_FIND(1, 6, i, sd->charm[i] > 0);
  3302. 				if( i < 5 )
  3303. 					s_ele = i;
  3304. 			}
  3305. 			break;
  3306. #ifdef RENEWAL
  3307. 		case CR_ACIDDEMONSTRATION:
  3308. 		case ASC_BREAKER:
  3309. 		case HW_MAGICCRASHER:
  3310. 			flag.imdef = 1;
  3311. 			break;
  3312. #endif
  3313. 	}
  3314.  
  3315. 	if (!flag.infdef) //No need to do the math for plants
  3316. 	{
  3317. #ifdef RENEWAL
  3318. 		ad.damage = 0; //reinitialize..
  3319. #endif
  3320. //MATK_RATE scales the damage. 100 = no change. 50 is halved, 200 is doubled, etc
  3321. #define MATK_RATE( a ) { ad.damage= ad.damage*(a)/100; }
  3322. //Adds dmg%. 100 = +100% (double) damage. 10 = +10% damage
  3323. #define MATK_ADDRATE( a ) { ad.damage+= ad.damage*(a)/100; }
  3324. //Adds an absolute value to damage. 100 = +100 damage
  3325. #define MATK_ADD( a ) { ad.damage+= a; }
  3326.  
  3327. 		switch (skill_id)
  3328. 		{	//Calc base damage according to skill
  3329. 			case AL_HEAL:
  3330. 			case PR_BENEDICTIO:
  3331. 			case PR_SANCTUARY:
  3332. 			/**
  3333. 			 * Arch Bishop
  3334. 			 **/
  3335. 			case AB_HIGHNESSHEAL:
  3336. 				ad.damage = skill->calc_heal(src, target, skill_id, skill_lv, false);
  3337. 				break;
  3338. 			case PR_ASPERSIO:
  3339. 				ad.damage = 40;
  3340. 				break;
  3341. 			case ALL_RESURRECTION:
  3342. 			case PR_TURNUNDEAD:
  3343. 				//Undead check is on skill_castend_damageid code.
  3344. 				i = 20*skill_lv + sstatus->luk + sstatus->int_ + status->get_lv(src)
  3345. 				  	+ 200 - 200*tstatus->hp/tstatus->max_hp; // there is no changed in success chance in renewal. [malufett]
  3346. 				if(i > 700) i = 700;
  3347. 				if(rnd()%1000 < i && !(tstatus->mode&MD_BOSS))
  3348. 					ad.damage = tstatus->hp;
  3349. 				else {
  3350. 				#ifdef RENEWAL
  3351.                     MATK_ADD(status->get_matk(src, 2));
  3352. 				#else
  3353. 					ad.damage = status->get_lv(src) + sstatus->int_ + skill_lv * 10;
  3354. 				#endif
  3355. 				}
  3356. 				break;
  3357. 			case PF_SOULBURN:
  3358. 				ad.damage = tstatus->sp * 2;
  3359. 				break;
  3360. 			/**
  3361. 			 * Arch Bishop
  3362. 			 **/
  3363. 			case AB_RENOVATIO:
  3364. 				//Damage calculation from iRO wiki. [Jobbie]
  3365. 				ad.damage = (int)((15 * status->get_lv(src)) + (1.5 * sstatus->int_));
  3366. 				break;
  3367.             default: {
  3368. 				MATK_ADD( status->get_matk(src, 2) );
  3369.  
  3370. 				if (nk&NK_SPLASHSPLIT) { // Divide MATK in case of multiple targets skill
  3371. 					if(mflag>0)
  3372. 						ad.damage/= mflag;
  3373. 					else
  3374. 						ShowError("0 enemies targeted by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
  3375. 				}
  3376.  
  3377. 				if (sc){
  3378. 					if( sc->data[SC_TELEKINESIS_INTENSE] && s_ele == ELE_GHOST )
  3379. 						skillratio += sc->data[SC_TELEKINESIS_INTENSE]->val3; 
  3380. 				}
  3381. 				switch(skill_id){
  3382. 					case MG_FIREBOLT:
  3383. 					case MG_COLDBOLT:
  3384. 					case MG_LIGHTNINGBOLT:
  3385. 						if ( sc && sc->data[SC_SPELLFIST] && mflag&BF_SHORT )  {
  3386. 							skillratio += (sc->data[SC_SPELLFIST]->val4 * 100) + (sc->data[SC_SPELLFIST]->val2 * 100) - 100;// val4 = used bolt level, val2 = used spellfist level. [Rytech]
  3387. 							ad.div_ = 1;// ad mods, to make it work similar to regular hits [Xazax]
  3388. 							ad.flag = BF_WEAPON|BF_SHORT;
  3389. 							ad.type = 0;
  3390. 						}
  3391. 					default:
  3392. 						MATK_RATE(battle->calc_skillratio(BF_MAGIC, src, target, skill_id, skill_lv, skillratio, mflag));
  3393. 				}
  3394. 				//Constant/misc additions from skills
  3395. 				if (skill_id == WZ_FIREPILLAR)
  3396. 					MATK_ADD(50);
  3397. 				if( sd && ( sd->status.class_ == JOB_ARCH_BISHOP_T || sd->status.class_ == JOB_ARCH_BISHOP ) &&
  3398. 					(i=pc->checkskill(sd,AB_EUCHARISTICA)) > 0 &&
  3399. 					(tstatus->race == RC_DEMON || tstatus->def_ele == ELE_DARK) )
  3400. 					MATK_ADDRATE(i);
  3401. 			}
  3402. 		}
  3403. #ifndef HMAP_ZONE_DAMAGE_CAP_TYPE
  3404. 		if( target && skill_id ) {
  3405. 			for(i = 0; i < maplist[target->m].zone->capped_skills_count; i++) {
  3406. 				if( skill_id == maplist[target->m].zone->capped_skills[i]->nameid && (maplist[target->m].zone->capped_skills[i]->type & target->type) ) {
  3407. 					if( target->type == BL_MOB && maplist[target->m].zone->capped_skills[i]->subtype != MZS_NONE ) {
  3408. 						if( (((TBL_MOB*)target)->status.mode&MD_BOSS) && !(maplist[target->m].zone->disabled_skills[i]->subtype&MZS_BOSS) )
  3409. 							continue;
  3410. 						if( ((TBL_MOB*)target)->special_state.clone && !(maplist[target->m].zone->disabled_skills[i]->subtype&MZS_CLONE) )
  3411. 							continue;
  3412. 					}
  3413. 					if( ad.damage > maplist[target->m].zone->capped_skills[i]->cap )
  3414. 						ad.damage = maplist[target->m].zone->capped_skills[i]->cap;
  3415. 					if( ad.damage2 > maplist[target->m].zone->capped_skills[i]->cap )
  3416. 						ad.damage2 = maplist[target->m].zone->capped_skills[i]->cap;
  3417. 					break;
  3418. 				}
  3419. 			}
  3420. 		}
  3421. #endif
  3422. #ifdef RENEWAL
  3423. 		ad.damage = battle->calc_cardfix(BF_MAGIC, src, target, nk, s_ele, 0, ad.damage, 0, ad.flag);
  3424. #endif
  3425. 		if(sd) {
  3426. 			//Damage bonuses
  3427. 			if ((i = pc->skillatk_bonus(sd, skill_id)))
  3428. 				ad.damage += ad.damage*i/100;
  3429.  
  3430. 			if( (i = battle->adjust_skill_damage(src->m,skill_id)) )
  3431. 				MATK_RATE(i);
  3432.  
  3433. 			//Ignore Defense?
  3434. 			if (!flag.imdef && (
  3435. 				sd->bonus.ignore_mdef_ele & ( 1 << tstatus->def_ele ) ||
  3436. 				sd->bonus.ignore_mdef_race & ( 1 << tstatus->race ) ||
  3437. 				sd->bonus.ignore_mdef_race & ( is_boss(target) ? 1 << RC_BOSS : 1 << RC_NONBOSS )
  3438. 			))
  3439. 				flag.imdef = 1;
  3440. 		}
  3441.  
  3442. 		ad.damage = battle->calc_defense(BF_MAGIC, src, target, skill_id, skill_lv, ad.damage, (flag.imdef?1:0), 0);
  3443.  
  3444. 		if (skill_id == NPC_EARTHQUAKE)
  3445. 		{	//Adds atk2 to the damage, should be influenced by number of hits and skill-ratio, but not mdef reductions. [Skotlex]
  3446. 			//Also divide the extra bonuses from atk2 based on the number in range [Kevin]
  3447. 			if(mflag>0)
  3448. 				ad.damage+= (sstatus->rhw.atk2*skillratio/100)/mflag;
  3449. 			else
  3450. 				ShowError("Zero range by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
  3451. 		}
  3452.  
  3453. 		if(ad.damage<1)
  3454. 			ad.damage=1;
  3455. 		else if(sc){//only applies when hit
  3456. 			// TODO: there is another factor that contribute with the damage and need to be formulated. [malufett]
  3457. 			switch(skill_id){
  3458. 				case MG_LIGHTNINGBOLT:
  3459. 				case MG_THUNDERSTORM:
  3460. 				case MG_FIREBOLT:
  3461. 				case MG_FIREWALL:
  3462. 				case MG_COLDBOLT:
  3463. 				case MG_FROSTDIVER:
  3464. 				case WZ_EARTHSPIKE:
  3465. 				case WZ_HEAVENDRIVE:
  3466. 					if(sc->data[SC_GUST_OPTION] || sc->data[SC_PETROLOGY_OPTION]
  3467. 						|| sc->data[SC_PYROTECHNIC_OPTION] || sc->data[SC_AQUAPLAY_OPTION])
  3468. 						ad.damage += (6 + sstatus->int_/4) + max(sstatus->dex-10,0)/30;
  3469. 					break;
  3470. 			}
  3471. 		}
  3472.  
  3473. 		if (!(nk&NK_NO_ELEFIX))
  3474. 			ad.damage=battle->attr_fix(src, target, ad.damage, s_ele, tstatus->def_ele, tstatus->ele_lv);
  3475.  
  3476. 		if( skill_id == CR_GRANDCROSS || skill_id == NPC_GRANDDARKNESS )
  3477. 		{ //Apply the physical part of the skill's damage. [Skotlex]
  3478. 			struct Damage wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
  3479. 			ad.damage = battle->attr_fix(src, target, wd.damage + ad.damage, s_ele, tstatus->def_ele, tstatus->ele_lv) * (100 + 40*skill_lv)/100;
  3480. 			if( src == target )
  3481. 			{
  3482. 				if( src->type == BL_PC )
  3483. 					ad.damage = ad.damage/2;
  3484. 				else
  3485. 					ad.damage = 0;
  3486. 			}
  3487. 		}
  3488. #ifndef RENEWAL
  3489.         ad.damage = battle->calc_cardfix(BF_MAGIC, src, target, nk, s_ele, 0, ad.damage, 0, ad.flag);
  3490. #endif
  3491. 	}
  3492.  
  3493. 	damage_div_fix(ad.damage, ad.div_);
  3494.  
  3495. 	if (flag.infdef && ad.damage)
  3496. 		ad.damage = ad.damage>0?1:-1;
  3497.  
  3498. 	ad.damage=battle->calc_damage(src,target,&ad,ad.damage,skill_id,skill_lv);
  3499. 	if( map_flag_gvg2(target->m) )
  3500. 		ad.damage=battle->calc_gvg_damage(src,target,ad.damage,ad.div_,skill_id,skill_lv,ad.flag);
  3501. 	else if( maplist[target->m].flag.battleground )
  3502. 		ad.damage=battle->calc_bg_damage(src,target,ad.damage,ad.div_,skill_id,skill_lv,ad.flag);
  3503.  
  3504. 	switch( skill_id ) { /* post-calc modifiers */
  3505. 		case SO_VARETYR_SPEAR: { // Physical damage.
  3506. 			struct Damage wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
  3507. 			if(!flag.infdef && ad.damage > 1)
  3508. 				ad.damage += wd.damage;
  3509. 			break;
  3510. 		}
  3511. 		//case HM_ERASER_CUTTER:
  3512. 	}
  3513.  
  3514. 	return ad;
  3515. }
  3516.  
  3517. /*==========================================
  3518.  * Calculate Misc dammage for skill_id
  3519.  *------------------------------------------*/
  3520. struct Damage battle_calc_misc_attack(struct block_list *src,struct block_list *target,uint16 skill_id,uint16 skill_lv,int mflag) {
  3521. 	int temp;
  3522. 	short i, nk;
  3523. 	short s_ele;
  3524.  
  3525. 	struct map_session_data *sd, *tsd;
  3526. 	struct Damage md; //DO NOT CONFUSE with md of mob_data!
  3527. 	struct status_data *sstatus = status->get_status_data(src);
  3528. 	struct status_data *tstatus = status->get_status_data(target);
  3529. 	struct status_change *tsc = status->get_sc(target);
  3530. #ifdef RENEWAL
  3531. 	struct status_change *sc = status->get_sc(src);
  3532. #endif
  3533.  
  3534. 	memset(&md,0,sizeof(md));
  3535.  
  3536. 	if( src == NULL || target == NULL ){
  3537. 		nullpo_info(NLP_MARK);
  3538. 		return md;
  3539. 	}
  3540.  
  3541. 	//Some initial values
  3542. 	md.amotion=skill->get_inf(skill_id)&INF_GROUND_SKILL?0:sstatus->amotion;
  3543. 	md.dmotion=tstatus->dmotion;
  3544. 	md.div_=skill->get_num( skill_id,skill_lv );
  3545. 	md.blewcount=skill->get_blewcount(skill_id,skill_lv);
  3546. 	md.dmg_lv=ATK_DEF;
  3547. 	md.flag=BF_MISC|BF_SKILL;
  3548.  
  3549. 	nk = skill->get_nk(skill_id);
  3550.  
  3551. 	sd = BL_CAST(BL_PC, src);
  3552. 	tsd = BL_CAST(BL_PC, target);
  3553.  
  3554. 	if(sd) {
  3555. 		sd->state.arrow_atk = 0;
  3556. 		md.blewcount += battle->blewcount_bonus(sd, skill_id);
  3557. 	}
  3558.  
  3559. 	s_ele = skill->get_ele(skill_id, skill_lv);
  3560. 	if (s_ele < 0 && s_ele != -3) //Attack that takes weapon's element for misc attacks? Make it neutral [Skotlex]
  3561. 		s_ele = ELE_NEUTRAL;
  3562. 	else if (s_ele == -3) //Use random element
  3563. 		s_ele = rnd()%ELE_MAX;
  3564.  
  3565. 	//Skill Range Criteria
  3566. 	md.flag |= battle->range_type(src, target, skill_id, skill_lv);
  3567.  
  3568. 	switch( skill_id )
  3569. 	{
  3570. #ifdef RENEWAL
  3571. 	case HT_LANDMINE:
  3572. 	case MA_LANDMINE:
  3573. 	case HT_BLASTMINE:
  3574. 	case HT_CLAYMORETRAP:
  3575. 		md.damage = skill_lv * sstatus->dex * (3+status->get_lv(src)/100) * (1+sstatus->int_/35);
  3576. 		md.damage += md.damage * (rnd()%20-10) / 100;
  3577. 		md.damage += 40 * (sd?pc->checkskill(sd,RA_RESEARCHTRAP):0);
  3578. 		break;
  3579. #else
  3580. 	case HT_LANDMINE:
  3581. 	case MA_LANDMINE:
  3582. 		md.damage=skill_lv*(sstatus->dex+75)*(100+sstatus->int_)/100;
  3583. 		break;
  3584. 	case HT_BLASTMINE:
  3585. 		md.damage=skill_lv*(sstatus->dex/2+50)*(100+sstatus->int_)/100;
  3586. 		break;
  3587. 	case HT_CLAYMORETRAP:
  3588. 		md.damage=skill_lv*(sstatus->dex/2+75)*(100+sstatus->int_)/100;
  3589. 		break;
  3590. #endif
  3591. 	case HT_BLITZBEAT:
  3592. 	case SN_FALCONASSAULT:
  3593. 		//Blitz-beat Damage.
  3594. 		if(!sd || (temp = pc->checkskill(sd,HT_STEELCROW)) <= 0)
  3595. 			temp=0;
  3596. 		md.damage=(sstatus->dex/10+sstatus->int_/2+temp*3+40)*2;
  3597. 		if(mflag > 1) //Autocasted Blitz.
  3598. 			nk|=NK_SPLASHSPLIT;
  3599.  
  3600. 		if (skill_id == SN_FALCONASSAULT) {
  3601. 			//Div fix of Blitzbeat
  3602. 			temp = skill->get_num(HT_BLITZBEAT, 5);
  3603. 			damage_div_fix(md.damage, temp);
  3604.  
  3605. 			//Falcon Assault Modifier
  3606. 			md.damage=md.damage*(150+70*skill_lv)/100;
  3607. 		}
  3608. 		break;
  3609. 	case TF_THROWSTONE:
  3610. 		md.damage=50;
  3611. 		break;
  3612. 	case BA_DISSONANCE:
  3613. 		md.damage=30+skill_lv*10;
  3614. 		if (sd)
  3615. 			md.damage+= 3*pc->checkskill(sd,BA_MUSICALLESSON);
  3616. 		break;
  3617. 	case NPC_SELFDESTRUCTION:
  3618. 		md.damage = sstatus->hp;
  3619. 		break;
  3620. 	case NPC_SMOKING:
  3621. 		md.damage=3;
  3622. 		break;
  3623. 	case NPC_DARKBREATH:
  3624. 		md.damage = 500 + (skill_lv-1)*1000 + rnd()%1000;
  3625. 		if(md.damage > 9999) md.damage = 9999;
  3626. 		break;
  3627. 	case PA_PRESSURE:
  3628. 		md.damage=500+300*skill_lv;
  3629. 		break;
  3630. 	case PA_GOSPEL:
  3631. 		md.damage = 1+rnd()%9999;
  3632. 		break;
  3633. 	case CR_ACIDDEMONSTRATION:
  3634. #ifdef RENEWAL
  3635. 		{// [malufett]
  3636. 			int64 matk=0, atk;
  3637. 			short tdef = status->get_total_def(target);
  3638. 			short tmdef =  status->get_total_mdef(target);
  3639. 			int targetVit = min(120, status_get_vit(target));
  3640. 			short totaldef = (tmdef + tdef - ((uint64)(tmdef + tdef) >> 32)) >> 1;
  3641.  
  3642. 			matk = battle->calc_magic_attack(src, target, skill_id, skill_lv, mflag).damage;
  3643. 			atk = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, false, s_ele, ELE_NEUTRAL, EQI_HAND_R, (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0), md.flag);
  3644. 			md.damage = matk + atk;
  3645. 			if( src->type == BL_MOB ){
  3646. 				totaldef = (tdef + tmdef) >> 1;
  3647. 				md.damage = 7 * targetVit * skill_lv * (atk + matk) / 100;
  3648. 				/*
  3649. 				// Pending [malufett]
  3650. 				if( unknown condition )
  3651. 					md.damage >>= 1;
  3652. 				if( unknown condition ){
  3653. 					md.damage = 7 * md.damage % 20;
  3654. 					md.damage = 7 * md.damage / 20;
  3655. 				}*/
  3656. 			}else{
  3657. 				float vitfactor = 0.0f, temp;
  3658.  
  3659. 				if( (vitfactor=(status_get_vit(target)-120.0f)) > 0)
  3660. 					vitfactor = (vitfactor * (matk + atk) / 10) / status_get_vit(target);
  3661. 				temp = max(0, vitfactor) + (targetVit * (matk + atk)) / 10;
  3662. 				md.damage = (int64)(temp * 70 * skill_lv / 100);
  3663. 			}
  3664. 			md.damage -= totaldef;
  3665. 		}
  3666. #else
  3667. 		// updated the formula based on a Japanese formula found to be exact [Reddozen]
  3668. 		if(tstatus->vit+sstatus->int_) //crash fix
  3669. 			md.damage = (int)(7*tstatus->vit*sstatus->int_*sstatus->int_ / (10*(tstatus->vit+sstatus->int_)));
  3670. 		else
  3671. 			md.damage = 0;
  3672. 		if (tsd) md.damage>>=1;
  3673. #endif
  3674. 		if (md.damage < 0 || md.damage > INT_MAX>>1)
  3675. 	  	//Overflow prevention, will anyone whine if I cap it to a few billion?
  3676. 		//Not capped to INT_MAX to give some room for further damage increase.
  3677. 			md.damage = INT_MAX>>1;
  3678. 		break;
  3679.  
  3680. 	case KO_MUCHANAGE:
  3681. 		md.damage = skill->get_zeny(skill_id ,skill_lv);
  3682. 		md.damage = md.damage * (50 + rand()%50) / 100;
  3683. 		if ( is_boss(target) || (sd && !pc->checkskill(sd,NJ_TOBIDOUGU)) )
  3684. 			md.damage >>= 1;
  3685. 		break;
  3686. 	case NJ_ZENYNAGE:
  3687. 		md.damage = skill->get_zeny(skill_id ,skill_lv);
  3688. 		if (!md.damage) md.damage = 2;
  3689. 		md.damage = rand()%md.damage + md.damage;
  3690. 		if (is_boss(target))
  3691. 			md.damage=md.damage / 3;
  3692. 		else if (tsd)
  3693. 			md.damage=md.damage / 2;
  3694. 		break;
  3695. 	case GS_FLING:
  3696. 		md.damage = sd?sd->status.job_level:status->get_lv(src);
  3697. 		break;
  3698. 	case HVAN_EXPLOSION:	//[orn]
  3699. 		md.damage = sstatus->max_hp * (50 + 50 * skill_lv) / 100;
  3700. 		break ;
  3701. 	case ASC_BREAKER:
  3702. 		{
  3703. #ifndef RENEWAL
  3704. 		md.damage = 500+rnd()%500 + 5*skill_lv * sstatus->int_;
  3705. 		nk|=NK_IGNORE_FLEE|NK_NO_ELEFIX; //These two are not properties of the weapon based part.
  3706. #else
  3707. 		int ratio = 300 + 50 * skill_lv;
  3708. 		int64 matk = battle->calc_magic_attack(src, target, skill_id, skill_lv, mflag).damage;
  3709. 		short totaldef = status->get_total_def(target) + status->get_total_mdef(target);
  3710. 		int64 atk = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, false, s_ele, ELE_NEUTRAL, EQI_HAND_R, (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0), md.flag);
  3711.  
  3712. 		if( sc && sc->data[SC_EDP] )
  3713. 			ratio >>= 1;
  3714. 		md.damage = (matk + atk) * ratio / 100;
  3715. 		md.damage -= totaldef;
  3716. #endif
  3717. 		}
  3718. 		break;
  3719. 	case HW_GRAVITATION:
  3720. 		md.damage = 200+200*skill_lv;
  3721. 		md.dmotion = 0; //No flinch animation.
  3722. 		break;
  3723. 	case NPC_EVILLAND:
  3724. 		md.damage = skill->calc_heal(src,target,skill_id,skill_lv,false);
  3725. 		break;
  3726. 	case RK_DRAGONBREATH:
  3727. 	case RK_DRAGONBREATH_WATER:
  3728. 		md.damage = ((status_get_hp(src) / 50) + (status_get_max_sp(src) / 4)) * skill_lv;
  3729. 		RE_LVL_MDMOD(150);
  3730. 		if (sd) md.damage = md.damage * (100 + 5 * (pc->checkskill(sd,RK_DRAGONTRAINING) - 1)) / 100;
  3731. 		md.flag |= BF_LONG|BF_WEAPON;
  3732. 		break;
  3733. 	/**
  3734. 	 * Ranger
  3735. 	 **/
  3736. 	case RA_CLUSTERBOMB:
  3737. 	case RA_FIRINGTRAP:
  3738.  	case RA_ICEBOUNDTRAP:
  3739. 		md.damage = skill_lv * sstatus->dex + sstatus->int_ * 5 ;
  3740. 		RE_LVL_TMDMOD();
  3741. 		if(sd)
  3742. 		{
  3743. 			int researchskill_lv = pc->checkskill(sd,RA_RESEARCHTRAP);
  3744. 			if(researchskill_lv)
  3745. 				md.damage = md.damage * 20 * researchskill_lv / (skill_id == RA_CLUSTERBOMB?50:100);
  3746. 			else
  3747. 				md.damage = 0;
  3748. 		}else
  3749. 			md.damage = md.damage * 200 / (skill_id == RA_CLUSTERBOMB?50:100);
  3750.  
  3751. 		break;
  3752. 	/**
  3753. 	 * Mechanic
  3754. 	 **/
  3755. 	case NC_SELFDESTRUCTION:
  3756. 		{
  3757. #ifdef RENEWAL
  3758. 			short totaldef = status->get_total_def(target);
  3759. #else
  3760. 			short totaldef = tstatus->def2 + (short)status->get_def(target);
  3761. #endif
  3762. 			md.damage = ( (sd?pc->checkskill(sd,NC_MAINFRAME):10) + 8 ) * ( skill_lv + 1 ) * ( status_get_sp(src) + sstatus->vit );
  3763. 			RE_LVL_MDMOD(100);
  3764. 			md.damage += status_get_hp(src) - totaldef;
  3765. 		}
  3766. 		break;
  3767. 	case NC_MAGMA_ERUPTION:
  3768. 		md.damage = 1200 + 400 * skill_lv;
  3769. 		break;
  3770. 	case GN_THORNS_TRAP:
  3771. 		md.damage = 100 + 200 * skill_lv + sstatus->int_;
  3772. 		break;
  3773. 	case GN_HELLS_PLANT_ATK:
  3774. 		//[{( Hell Plant Skill Level x Casters Base Level ) x 10 } + {( Casters INT x 7 ) / 2 } x { 18 + ( Casters Job Level / 4 )] x ( 5 / ( 10 - Summon Flora Skill Level ))
  3775. 		md.damage = ( skill_lv * status->get_lv(src) * 10 ) + ( sstatus->int_ * 7 / 2 ) * ( 18 + (sd?sd->status.job_level:0) / 4 ) * ( 5 / (10 - (sd?pc->checkskill(sd,AM_CANNIBALIZE):0)) );
  3776. 		break;
  3777. 	case KO_HAPPOKUNAI:
  3778. 		{
  3779. 			struct Damage wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
  3780. #ifdef RENEWAL
  3781. 			short totaldef = status->get_total_def(target);
  3782. #else
  3783. 			short totaldef = tstatus->def2 + (short)status->get_def(target);
  3784. #endif
  3785. 			md.damage = 3 * wd.damage * (5 + skill_lv) / 5;
  3786. 			md.damage -= totaldef;
  3787. 		}
  3788. 		break;
  3789. 	}
  3790.  
  3791. 	if (nk&NK_SPLASHSPLIT){ // Divide ATK among targets
  3792. 		if(mflag>0)
  3793. 			md.damage/= mflag;
  3794. 		else
  3795. 			ShowError("0 enemies targeted by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
  3796. 	}
  3797.  
  3798. 	damage_div_fix(md.damage, md.div_);
  3799.  
  3800. 	if (!(nk&NK_IGNORE_FLEE))
  3801. 	{
  3802. 		i = 0; //Temp for "hit or no hit"
  3803. 		if(tsc && tsc->opt1 && tsc->opt1 != OPT1_STONEWAIT && tsc->opt1 != OPT1_BURNING)
  3804. 			i = 1;
  3805. 		else {
  3806. 			short
  3807. 				flee = tstatus->flee,
  3808. #ifdef RENEWAL
  3809. 				hitrate = 0; //Default hitrate
  3810. #else
  3811. 				hitrate = 80; //Default hitrate
  3812. #endif
  3813.  
  3814. 			if(battle_config.agi_penalty_type && battle_config.agi_penalty_target&target->type) {
  3815. 				unsigned char attacker_count; //256 max targets should be a sane max
  3816. 				attacker_count = unit->counttargeted(target);
  3817. 				if(attacker_count >= battle_config.agi_penalty_count)
  3818. 				{
  3819. 					if (battle_config.agi_penalty_type == 1)
  3820. 						flee = (flee * (100 - (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num))/100;
  3821. 					else //asume type 2: absolute reduction
  3822. 						flee -= (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num;
  3823. 					if(flee < 1) flee = 1;
  3824. 				}
  3825. 			}
  3826.  
  3827. 			hitrate+= sstatus->hit - flee;
  3828. #ifdef RENEWAL
  3829. 			if( sd ) //in Renewal hit bonus from Vultures Eye is not anymore shown in status window
  3830. 				hitrate += pc->checkskill(sd,AC_VULTURE);
  3831. #endif
  3832. 			if( skill_id == KO_MUCHANAGE )
  3833. 				hitrate = (int)((10 - ((float)1 / (status_get_dex(src) + status_get_luk(src))) * 500) * ((float)skill_lv / 2 + 5));
  3834.  
  3835. 			hitrate = cap_value(hitrate, battle_config.min_hitrate, battle_config.max_hitrate);
  3836.  
  3837. 			if(rnd()%100 < hitrate)
  3838. 				i = 1;
  3839. 		}
  3840. 		if (!i) {
  3841. 			md.damage = 0;
  3842. 			md.dmg_lv=ATK_FLEE;
  3843. 		}
  3844. 	}
  3845. #ifndef HMAP_ZONE_DAMAGE_CAP_TYPE
  3846. 	if( target && skill_id ) {
  3847. 		for(i = 0; i < maplist[target->m].zone->capped_skills_count; i++) {
  3848. 			if( skill_id == maplist[target->m].zone->capped_skills[i]->nameid && (maplist[target->m].zone->capped_skills[i]->type & target->type) ) {
  3849. 				if( target->type == BL_MOB && maplist[target->m].zone->capped_skills[i]->subtype != MZS_NONE ) {
  3850. 					if( (((TBL_MOB*)target)->status.mode&MD_BOSS) && !(maplist[target->m].zone->disabled_skills[i]->subtype&MZS_BOSS) )
  3851. 						continue;
  3852. 					if( ((TBL_MOB*)target)->special_state.clone && !(maplist[target->m].zone->disabled_skills[i]->subtype&MZS_CLONE) )
  3853. 						continue;
  3854. 				}
  3855. 				if( md.damage > maplist[target->m].zone->capped_skills[i]->cap )
  3856. 					md.damage = maplist[target->m].zone->capped_skills[i]->cap;
  3857. 				if( md.damage2 > maplist[target->m].zone->capped_skills[i]->cap )
  3858. 					md.damage2 = maplist[target->m].zone->capped_skills[i]->cap;
  3859. 				break;
  3860. 			}
  3861. 		}
  3862. 	}
  3863. #endif
  3864.     md.damage = battle->calc_cardfix(BF_MISC, src, target, nk, s_ele, 0, md.damage, 0, md.flag);
  3865.  
  3866. 	if (sd && (i = pc->skillatk_bonus(sd, skill_id)))
  3867. 		md.damage += md.damage*i/100;
  3868.  
  3869. 	if( (i = battle->adjust_skill_damage(src->m,skill_id)) )
  3870. 		md.damage = md.damage * i / 100;
  3871.  
  3872. 	if(md.damage < 0)
  3873. 		md.damage = 0;
  3874. 	else if(md.damage && tstatus->mode&MD_PLANT){
  3875. 		switch(skill_id){
  3876. 			case HT_LANDMINE:
  3877. 			case MA_LANDMINE:
  3878. 			case HT_BLASTMINE:
  3879. 			case HT_CLAYMORETRAP:
  3880. 			case RA_CLUSTERBOMB:
  3881. #ifdef RENEWAL
  3882. 				break;
  3883. #endif
  3884. 			default:
  3885. 				md.damage = 1;
  3886. 		}
  3887. 	}else if( target->type == BL_SKILL ){
  3888. 		TBL_SKILL *su = (TBL_SKILL*)target;
  3889. 		if( su->group && (su->group->skill_id == WM_REVERBERATION || su->group->skill_id == WM_POEMOFNETHERWORLD) )
  3890. 			md.damage = 1;
  3891. 	}
  3892.  
  3893. 	if(!(nk&NK_NO_ELEFIX))
  3894. 		md.damage=battle->attr_fix(src, target, md.damage, s_ele, tstatus->def_ele, tstatus->ele_lv);
  3895.  
  3896. 	md.damage=battle->calc_damage(src,target,&md,md.damage,skill_id,skill_lv);
  3897. 	if( map_flag_gvg2(target->m) )
  3898. 		md.damage=battle->calc_gvg_damage(src,target,md.damage,md.div_,skill_id,skill_lv,md.flag);
  3899. 	else if( maplist[target->m].flag.battleground )
  3900. 		md.damage=battle->calc_bg_damage(src,target,md.damage,md.div_,skill_id,skill_lv,md.flag);
  3901.  
  3902. 	switch( skill_id ) {
  3903. 		case RA_FIRINGTRAP:
  3904.  		case RA_ICEBOUNDTRAP:
  3905. 			if( md.damage == 1 ) break;
  3906. 		case RA_CLUSTERBOMB:
  3907. 			{
  3908. 				struct Damage wd;
  3909. 				wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
  3910. 				md.damage += wd.damage;
  3911. 			}
  3912. 			break;
  3913. 		case NJ_ZENYNAGE:
  3914. 			if( sd ) {
  3915. 				if ( md.damage > sd->status.zeny )
  3916. 					md.damage = sd->status.zeny;
  3917. 				pc->payzeny(sd, (int)cap_value(md.damage,INT_MIN,INT_MAX),LOG_TYPE_STEAL,NULL);
  3918. 			}
  3919. 		break;
  3920. 	}
  3921.  
  3922. 	return md;
  3923. }
  3924.  
  3925. /*==========================================
  3926.  * battle_calc_weapon_attack (by Skotlex)
  3927.  *------------------------------------------*/
  3928. struct Damage battle_calc_weapon_attack(struct block_list *src,struct block_list *target,uint16 skill_id,uint16 skill_lv,int wflag)
  3929. {
  3930. 	unsigned int skillratio = 100;	//Skill dmg modifiers.
  3931. 	short temp=0;
  3932. 	short s_ele, s_ele_;
  3933. 	int i, nk;
  3934. 	bool n_ele = false; // non-elemental
  3935.  
  3936. 	struct map_session_data *sd, *tsd;
  3937. 	struct Damage wd;
  3938. 	struct status_change *sc = status->get_sc(src);
  3939. 	struct status_change *tsc = status->get_sc(target);
  3940. 	struct status_data *sstatus = status->get_status_data(src);
  3941. 	struct status_data *tstatus = status->get_status_data(target);
  3942. 	struct {
  3943. 		unsigned hit : 1; //the attack Hit? (not a miss)
  3944. 		unsigned cri : 1;		//Critical hit
  3945. 		unsigned idef : 1;	//Ignore defense
  3946. 		unsigned idef2 : 1;	//Ignore defense (left weapon)
  3947. 		unsigned pdef : 2;	//Pierces defense (Investigate/Ice Pick)
  3948. 		unsigned pdef2 : 2;	//1: Use def+def2/100, 2: Use def+def2/50
  3949. 		unsigned infdef : 1;	//Infinite defense (plants)
  3950. 		unsigned arrow : 1;	//Attack is arrow-based
  3951. 		unsigned rh : 1;		//Attack considers right hand (wd.damage)
  3952. 		unsigned lh : 1;		//Attack considers left hand (wd.damage2)
  3953. 		unsigned weapon : 1; //It's a weapon attack (consider VVS, and all that)
  3954. #ifdef RENEWAL
  3955. 		unsigned tdef : 1; //Total defence reduction
  3956. #endif
  3957. 	}	flag;
  3958.  
  3959. 	memset(&wd,0,sizeof(wd));
  3960. 	memset(&flag,0,sizeof(flag));
  3961.  
  3962. 	if(src==NULL || target==NULL)
  3963. 	{
  3964. 		nullpo_info(NLP_MARK);
  3965. 		return wd;
  3966. 	}
  3967. 	//Initial flag
  3968. 	flag.rh=1;
  3969. 	flag.weapon=1;
  3970. 	flag.infdef=(tstatus->mode&MD_PLANT && skill_id != RA_CLUSTERBOMB
  3971. #ifdef RENEWAL
  3972. 		&& skill_id != HT_FREEZINGTRAP
  3973. #endif
  3974. 		?1:0);
  3975. 	if( target->type == BL_SKILL){
  3976. 		TBL_SKILL *su = (TBL_SKILL*)target;
  3977. 		if( su->group && (su->group->skill_id == WM_REVERBERATION || su->group->skill_id == WM_POEMOFNETHERWORLD) )
  3978. 			flag.infdef = 1;
  3979. 	}
  3980.  
  3981. 	//Initial Values
  3982. 	wd.type=0; //Normal attack
  3983. 	wd.div_=skill_id?skill->get_num(skill_id,skill_lv):1;
  3984. 	wd.amotion=(skill_id && skill->get_inf(skill_id)&INF_GROUND_SKILL)?0:sstatus->amotion; //Amotion should be 0 for ground skills.
  3985. 	if(skill_id == KN_AUTOCOUNTER)
  3986. 		wd.amotion >>= 1;
  3987. 	wd.dmotion=tstatus->dmotion;
  3988. 	wd.blewcount=skill->get_blewcount(skill_id,skill_lv);
  3989. 	wd.flag = BF_WEAPON; //Initial Flag
  3990. 	wd.flag |= (skill_id||wflag)?BF_SKILL:BF_NORMAL; // Baphomet card's splash damage is counted as a skill. [Inkfish]
  3991. 	wd.dmg_lv=ATK_DEF;	//This assumption simplifies the assignation later
  3992. 	nk = skill->get_nk(skill_id);
  3993. 	if( !skill_id && wflag ) //If flag, this is splash damage from Baphomet Card and it always hits.
  3994. 		nk |= NK_NO_CARDFIX_ATK|NK_IGNORE_FLEE;
  3995. 	flag.hit = nk&NK_IGNORE_FLEE?1:0;
  3996. 	flag.idef = flag.idef2 = nk&NK_IGNORE_DEF?1:0;
  3997. #ifdef RENEWAL
  3998. 	flag.tdef = 0;
  3999. #endif
  4000. 	if (sc && !sc->count)
  4001. 		sc = NULL; //Skip checking as there are no status changes active.
  4002. 	if (tsc && !tsc->count)
  4003. 		tsc = NULL; //Skip checking as there are no status changes active.
  4004.  
  4005. 	sd = BL_CAST(BL_PC, src);
  4006. 	tsd = BL_CAST(BL_PC, target);
  4007.  
  4008. 	if(sd)
  4009. 		wd.blewcount += battle->blewcount_bonus(sd, skill_id);
  4010.  
  4011. 	//Set miscellaneous data that needs be filled regardless of hit/miss
  4012. 	if(
  4013. 		(sd && sd->state.arrow_atk) ||
  4014. 		(!sd && ((skill_id && skill->get_ammotype(skill_id)) || sstatus->rhw.range>3))
  4015. 	)
  4016. 		flag.arrow = 1;
  4017.  
  4018. 	if(skill_id) {
  4019. 		wd.flag |= battle->range_type(src, target, skill_id, skill_lv);
  4020. 		switch(skill_id) {
  4021. 			case MO_FINGEROFFENSIVE:
  4022. 				if(sd) {
  4023. 					if (battle_config.finger_offensive_type)
  4024. 						wd.div_ = 1;
  4025. 					else
  4026. 						wd.div_ = sd->spiritball_old;
  4027. 				}
  4028. 				break;
  4029. 			case HT_PHANTASMIC:
  4030. 				//Since these do not consume ammo, they need to be explicitly set as arrow attacks.
  4031. 				flag.arrow = 1;
  4032. 				break;
  4033. #ifndef RENEWAL
  4034. 			case PA_SHIELDCHAIN:
  4035. 			case CR_SHIELDBOOMERANG:
  4036. #endif
  4037. 			case LG_SHIELDPRESS:
  4038. 			case LG_EARTHDRIVE:
  4039. 				flag.weapon = 0;
  4040. 				break;
  4041.  
  4042. 			case KN_PIERCE:
  4043. 			case ML_PIERCE:
  4044. 				wd.div_= (wd.div_>0?tstatus->size+1:-(tstatus->size+1));
  4045. 				break;
  4046.  
  4047. 			case TF_DOUBLE: //For NPC used skill.
  4048. 			case GS_CHAINACTION:
  4049. 				wd.type = 0x08;
  4050. 				break;
  4051.  
  4052. 			case GS_GROUNDDRIFT:
  4053. 			case KN_SPEARSTAB:
  4054. 			case KN_BOWLINGBASH:
  4055. 			case MS_BOWLINGBASH:
  4056. 			case MO_BALKYOUNG:
  4057. 			case TK_TURNKICK:
  4058. 				wd.blewcount=0;
  4059. 				break;
  4060.  
  4061. 			case KN_AUTOCOUNTER:
  4062. 				wd.flag=(wd.flag&~BF_SKILLMASK)|BF_NORMAL;
  4063. 				break;
  4064.  
  4065. 			case NPC_CRITICALSLASH:
  4066. 			case LG_PINPOINTATTACK:
  4067. 				flag.cri = 1; //Always critical skill.
  4068. 				break;
  4069.  
  4070. 			case LK_SPIRALPIERCE:
  4071. 				if (!sd) wd.flag=(wd.flag&~(BF_RANGEMASK|BF_WEAPONMASK))|BF_LONG|BF_MISC;
  4072. 				break;
  4073.  
  4074. 			case MO_INVESTIGATE:
  4075. 				flag.pdef = flag.pdef2 = 2;
  4076. 				break;
  4077.  
  4078. 			case RA_AIMEDBOLT:
  4079. 				if( tsc && (tsc->data[SC_WUGBITE] || tsc->data[SC_ANKLESNARE] || tsc->data[SC_ELECTRICSHOCKER]) )
  4080. 					wd.div_ = tstatus->size + 2 + ( (rnd()%100 < 50-tstatus->size*10) ? 1 : 0 );
  4081. 				break;
  4082. #ifdef RENEWAL
  4083. 			case HW_MAGICCRASHER:
  4084. 				flag.tdef = 1;
  4085. 				break;
  4086. #endif
  4087. 		}
  4088. 	} else //Range for normal attacks.
  4089. 		wd.flag |= flag.arrow?BF_LONG:BF_SHORT;
  4090. 	if ( (!skill_id || skill_id == PA_SACRIFICE) && tstatus->flee2 && rnd()%1000 < tstatus->flee2 )
  4091. 	{	//Check for Lucky Dodge
  4092. 		wd.type=0x0b;
  4093. 		wd.dmg_lv=ATK_LUCKY;
  4094. 		if (wd.div_ < 0) wd.div_*=-1;
  4095. 		return wd;
  4096. 	}
  4097.  
  4098. 	s_ele = s_ele_ = skill->get_ele(skill_id, skill_lv);
  4099. 	if( !skill_id || s_ele == -1 )
  4100. 	{ //Take weapon's element
  4101. 		s_ele = sstatus->rhw.ele;
  4102. 		s_ele_ = sstatus->lhw.ele;
  4103. 		if( sd ){ //Summoning 10 charm will endow your weapon.
  4104. 			ARR_FIND(1, 6, i, sd->charm[i] >= 10);
  4105. 			if( i < 5 ) s_ele = s_ele_ = i;
  4106. 		}
  4107. 		if( flag.arrow && sd && sd->bonus.arrow_ele )
  4108. 			s_ele = sd->bonus.arrow_ele;
  4109. 		if( battle_config.attack_attr_none&src->type )
  4110. 			n_ele = true; //Weapon's element is "not elemental"
  4111. 	}
  4112. 	else if( s_ele == -2 ) //Use enchantment's element
  4113. 		s_ele = s_ele_ = status_get_attack_sc_element(src,sc);
  4114. 	else if( s_ele == -3 ) //Use random element
  4115. 		s_ele = s_ele_ = rnd()%ELE_MAX;
  4116. 	switch( skill_id )
  4117. 	{
  4118. 		case GS_GROUNDDRIFT:
  4119. 			s_ele = s_ele_ = wflag; //element comes in flag.
  4120. 			break;
  4121. 		case LK_SPIRALPIERCE:
  4122. 			if (!sd) n_ele = false; //forced neutral for monsters
  4123. 			break;
  4124. 	}
  4125.  
  4126. 	if (!(nk & NK_NO_ELEFIX) && !n_ele)
  4127. 	    if (src->type == BL_HOM)
  4128. 		n_ele = true; //skill is "not elemental"
  4129. 	if (sc && sc->data[SC_GOLDENE_FERSE] && ((!skill_id && (rnd() % 100 < sc->data[SC_GOLDENE_FERSE]->val4)) || skill_id == MH_STAHL_HORN)) {
  4130. 	    s_ele = s_ele_ = ELE_HOLY;
  4131. 	    n_ele = false;
  4132. 	}
  4133.  
  4134. 	if(!skill_id)
  4135.   	{	//Skills ALWAYS use ONLY your right-hand weapon (tested on Aegis 10.2)
  4136. 		if (sd && sd->weapontype1 == 0 && sd->weapontype2 > 0)
  4137. 		{
  4138. 			flag.rh=0;
  4139. 			flag.lh=1;
  4140. 		}
  4141. 		if (sstatus->lhw.atk)
  4142. 			flag.lh=1;
  4143. 	}
  4144.  
  4145. 	if( sd && !skill_id ) {	//Check for double attack.
  4146. 		if( ( ( skill_lv = pc->checkskill(sd,TF_DOUBLE) ) > 0 && sd->weapontype1 == W_DAGGER )
  4147. 			|| ( sd->bonus.double_rate > 0 && sd->weapontype1 != W_FIST ) //Will fail bare-handed
  4148. 			|| ( sc && sc->data[SC_KAGEMUSYA] && sd->weapontype1 != W_FIST )) // Need confirmation
  4149. 		{	//Success chance is not added, the higher one is used [Skotlex]
  4150. 			if( rnd()%100 < ( 5*skill_lv > sd->bonus.double_rate ? 5*skill_lv : sc && sc->data[SC_KAGEMUSYA]?sc->data[SC_KAGEMUSYA]->val1*3:sd->bonus.double_rate ) )
  4151. 			{
  4152. 				wd.div_ = skill->get_num(TF_DOUBLE,skill_lv?skill_lv:1);
  4153. 				wd.type = 0x08;
  4154. 			}
  4155. 		}
  4156. 		else if( sd->weapontype1 == W_REVOLVER && (skill_lv = pc->checkskill(sd,GS_CHAINACTION)) > 0 && rnd()%100 < 5*skill_lv )
  4157. 		{
  4158. 			wd.div_ = skill->get_num(GS_CHAINACTION,skill_lv);
  4159. 			wd.type = 0x08;
  4160. 		}
  4161. 		else if(sc && sc->data[SC_FEARBREEZE] && sd->weapontype1==W_BOW
  4162. 			&& (i = sd->equip_index[EQI_AMMO]) >= 0 && sd->inventory_data[i] && sd->status.inventory[i].amount > 1){
  4163. 				int chance = rand()%100;
  4164. 				wd.type = 0x08;
  4165. 				switch(sc->data[SC_FEARBREEZE]->val1){
  4166. 					case 5:
  4167. 						if( chance < 3){// 3 % chance to attack 5 times.
  4168. 							wd.div_ = 5;
  4169. 							break;
  4170. 						}
  4171. 					case 4:
  4172. 						if( chance < 7){// 6 % chance to attack 4 times.
  4173. 							wd.div_ = 4;
  4174. 							break;
  4175. 						}
  4176. 					case 3:
  4177. 						if( chance < 10){// 9 % chance to attack 3 times.
  4178. 							wd.div_ = 3;
  4179. 							break;
  4180. 						}
  4181. 					case 2:
  4182. 					case 1:
  4183. 						if( chance < 13){// 12 % chance to attack 2 times.
  4184. 							wd.div_ = 2;
  4185. 							break;
  4186. 						}
  4187. 				}
  4188. 				wd.div_ = min(wd.div_,sd->status.inventory[i].amount);
  4189. 				sc->data[SC_FEARBREEZE]->val4 = wd.div_-1;
  4190. 		}
  4191. 	}
  4192.  
  4193. 	//Check for critical
  4194. 	if( !flag.cri && !(wd.type&0x08) && sstatus->cri &&
  4195. 		(!skill_id ||
  4196. 		skill_id == KN_AUTOCOUNTER ||
  4197. 		skill_id == SN_SHARPSHOOTING || skill_id == MA_SHARPSHOOTING ||
  4198. 		skill_id == NJ_KIRIKAGE))
  4199. 	{
  4200. 		short cri = sstatus->cri;
  4201. 		if (sd)
  4202. 		{
  4203. 			cri+= sd->critaddrace[tstatus->race];
  4204. 			if(flag.arrow)
  4205. 				cri += sd->bonus.arrow_cri;
  4206. 		}
  4207. 		if( sc && sc->data[SC_CAMOUFLAGE] )
  4208. 			cri += 10 * (10-sc->data[SC_CAMOUFLAGE]->val4);
  4209. #ifndef RENEWAL
  4210. 		//The official equation is *2, but that only applies when sd's do critical.
  4211. 		//Therefore, we use the old value 3 on cases when an sd gets attacked by a mob
  4212. 		cri -= tstatus->luk*(!sd&&tsd?3:2);
  4213. #else
  4214. 		cri -= status->get_lv(target) / 15 + 2 * status_get_luk(target);
  4215. #endif
  4216.  
  4217. 		if( tsc && tsc->data[SC_SLEEP] ) {
  4218. 			cri <<= 1;
  4219. 		}
  4220. 		switch (skill_id) {
  4221. 			case KN_AUTOCOUNTER:
  4222. 				if(battle_config.auto_counter_type &&
  4223. 					(battle_config.auto_counter_type&src->type))
  4224. 					flag.cri = 1;
  4225. 				else
  4226. 					cri <<= 1;
  4227. 				break;
  4228. 			case SN_SHARPSHOOTING:
  4229. 			case MA_SHARPSHOOTING:
  4230. 				cri += 200;
  4231. 				break;
  4232. 			case NJ_KIRIKAGE:
  4233. 				cri += 250 + 50*skill_lv;
  4234. 				break;
  4235. 		}
  4236. 		if(tsd && tsd->bonus.critical_def)
  4237. 			cri = cri * ( 100 - tsd->bonus.critical_def ) / 100;
  4238. 		if (rnd()%1000 < cri)
  4239. 			flag.cri = 1;
  4240. 	}
  4241. 	if (flag.cri) {
  4242. 		wd.type = 0x0a;
  4243. #ifndef RENEWAL
  4244. 		flag.idef = flag.idef2 =
  4245. #endif
  4246. 		flag.hit = 1;
  4247. 	} else {	//Check for Perfect Hit
  4248. 		if(sd && sd->bonus.perfect_hit > 0 && rnd()%100 < sd->bonus.perfect_hit)
  4249. 			flag.hit = 1;
  4250. 		if (sc && sc->data[SC_FUSION]) {
  4251. 			flag.hit = 1; //SG_FUSION always hit [Komurka]
  4252. 			flag.idef = flag.idef2 = 1; //def ignore [Komurka]
  4253. 		}
  4254. 		if( !flag.hit )
  4255. 			switch(skill_id)
  4256. 			{
  4257. 				case AS_SPLASHER:
  4258. 					if( !wflag ) // Always hits the one exploding.
  4259. 						flag.hit = 1;
  4260. 					break;
  4261. 				case CR_SHIELDBOOMERANG:
  4262. 					if( sc && sc->data[SC_SOULLINK] && sc->data[SC_SOULLINK]->val2 == SL_CRUSADER )
  4263. 						flag.hit = 1;
  4264. 					break;
  4265. 			}
  4266. 		if (tsc && !flag.hit && tsc->opt1 && tsc->opt1 != OPT1_STONEWAIT && tsc->opt1 != OPT1_BURNING)
  4267. 			flag.hit = 1;
  4268. 	}
  4269.  
  4270. 	if (!flag.hit)
  4271. 	{	//Hit/Flee calculation
  4272. 		short
  4273. 			flee = tstatus->flee,
  4274. #ifdef RENEWAL
  4275. 				hitrate = 0; //Default hitrate
  4276. #else
  4277. 				hitrate = 80; //Default hitrate
  4278. #endif
  4279.  
  4280. 		if(battle_config.agi_penalty_type && battle_config.agi_penalty_target&target->type) {
  4281. 			unsigned char attacker_count; //256 max targets should be a sane max
  4282. 			attacker_count = unit->counttargeted(target);
  4283. 			if(attacker_count >= battle_config.agi_penalty_count) {
  4284. 				if (battle_config.agi_penalty_type == 1)
  4285. 					flee = (flee * (100 - (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num))/100;
  4286. 				else //asume type 2: absolute reduction
  4287. 					flee -= (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num;
  4288. 				if(flee < 1) flee = 1;
  4289. 			}
  4290. 		}
  4291.  
  4292. 		hitrate+= sstatus->hit - flee;
  4293.  
  4294. 		if(wd.flag&BF_LONG && !skill_id && //Fogwall's hit penalty is only for normal ranged attacks.
  4295. 			tsc && tsc->data[SC_FOGWALL])
  4296. 			hitrate -= 50;
  4297.  
  4298. 		if(sd && flag.arrow)
  4299. 			hitrate += sd->bonus.arrow_hit;
  4300. #ifdef RENEWAL
  4301. 		if( sd ) //in Renewal hit bonus from Vultures Eye is not anymore shown in status window
  4302. 			hitrate += pc->checkskill(sd,AC_VULTURE);
  4303. #endif
  4304. 		if(skill_id)
  4305. 			switch(skill_id)
  4306. 		{	//Hit skill modifiers
  4307. 			//It is proven that bonus is applied on final hitrate, not hit.
  4308. 			case SM_BASH:
  4309. 			case MS_BASH:
  4310. 				hitrate += hitrate * 5 * skill_lv / 100;
  4311. 				break;
  4312. 			case MS_MAGNUM:
  4313. 			case SM_MAGNUM:
  4314. 				hitrate += hitrate * 10 * skill_lv / 100;
  4315. 				break;
  4316. 			case KN_AUTOCOUNTER:
  4317. 			case PA_SHIELDCHAIN:
  4318. 			case NPC_WATERATTACK:
  4319. 			case NPC_GROUNDATTACK:
  4320. 			case NPC_FIREATTACK:
  4321. 			case NPC_WINDATTACK:
  4322. 			case NPC_POISONATTACK:
  4323. 			case NPC_HOLYATTACK:
  4324. 			case NPC_DARKNESSATTACK:
  4325. 			case NPC_UNDEADATTACK:
  4326. 			case NPC_TELEKINESISATTACK:
  4327. 			case NPC_BLEEDING:
  4328. 				hitrate += hitrate * 20 / 100;
  4329. 				break;
  4330. 			case KN_PIERCE:
  4331. 			case ML_PIERCE:
  4332. 				hitrate += hitrate * 5 * skill_lv / 100;
  4333. 				break;
  4334. 			case AS_SONICBLOW:
  4335. 				if(sd && pc->checkskill(sd,AS_SONICACCEL)>0)
  4336. 					hitrate += hitrate * 50 / 100;
  4337. 				break;
  4338. 			case MC_CARTREVOLUTION:
  4339. 			case GN_CART_TORNADO:
  4340. 			case GN_CARTCANNON:
  4341. 				if( sd && pc->checkskill(sd, GN_REMODELING_CART) )
  4342. 					hitrate += pc->checkskill(sd, GN_REMODELING_CART) * 4;
  4343. 				break;
  4344. 			case GC_VENOMPRESSURE:
  4345. 				hitrate += 10 + 4 * skill_lv;
  4346. 				break;
  4347. 		}
  4348.  
  4349. 		if( sd ) {
  4350. 			// Weaponry Research hidden bonus
  4351. 			if ((temp = pc->checkskill(sd,BS_WEAPONRESEARCH)) > 0)
  4352. 				hitrate += hitrate * ( 2 * temp ) / 100;
  4353.  
  4354. 			if( (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_DAGGER) &&
  4355. 			   (temp = pc->checkskill(sd, GN_TRAINING_SWORD))>0 )
  4356. 				hitrate += 3 * temp;
  4357. 		}
  4358.  
  4359. 		hitrate = cap_value(hitrate, battle_config.min_hitrate, battle_config.max_hitrate);
  4360. #ifdef RENEWAL
  4361. 		if( !sd )
  4362. 			hitrate = cap_value(hitrate, 5, 95);
  4363. #endif
  4364. 		if(rnd()%100 >= hitrate)
  4365. 			wd.dmg_lv = ATK_FLEE;
  4366. 		else
  4367. 			flag.hit = 1;
  4368. 	}	//End hit/miss calculation
  4369.  
  4370. 	if (flag.hit && !flag.infdef) //No need to do the math for plants
  4371. 	{	//Hitting attack
  4372.  
  4373. //Assuming that 99% of the cases we will not need to check for the flag.rh... we don't.
  4374. //ATK_RATE scales the damage. 100 = no change. 50 is halved, 200 is doubled, etc
  4375. #define ATK_RATE( a ) { wd.damage= wd.damage*(a)/100 ; if(flag.lh) wd.damage2= wd.damage2*(a)/100; }
  4376. #define ATK_RATE2( a , b ) { wd.damage= wd.damage*(a)/100 ; if(flag.lh) wd.damage2= wd.damage2*(b)/100; }
  4377. #define ATK_RATER(a){ wd.damage = wd.damage*(a)/100;}
  4378. #define ATK_RATEL(a){ wd.damage2 = wd.damage2*(a)/100;}
  4379. //Adds dmg%. 100 = +100% (double) damage. 10 = +10% damage
  4380. #define ATK_ADDRATE( a ) { wd.damage+= wd.damage*(a)/100 ; if(flag.lh) wd.damage2+= wd.damage2*(a)/100; }
  4381. #define ATK_ADDRATE2( a , b ) { wd.damage+= wd.damage*(a)/100 ; if(flag.lh) wd.damage2+= wd.damage2*(b)/100; }
  4382. //Adds an absolute value to damage. 100 = +100 damage
  4383. #define ATK_ADD( a ) { wd.damage+= a; if (flag.lh) wd.damage2+= a; }
  4384. #define ATK_ADD2( a , b ) { wd.damage+= a; if (flag.lh) wd.damage2+= b; }
  4385. #ifdef RENEWAL
  4386. #define GET_NORMAL_ATTACK( f ) { wd.damage = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, n_ele, s_ele, s_ele_, EQI_HAND_R, f, wd.flag); }
  4387. #define GET_NORMAL_ATTACK2( f ) { wd.damage2 = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, n_ele, s_ele, s_ele_, EQI_HAND_L, f, wd.flag); }
  4388. #endif
  4389. 		switch (skill_id)
  4390. 		{	//Calc base damage according to skill
  4391. 			case PA_SACRIFICE:
  4392. 				wd.damage = sstatus->max_hp* 9/100;
  4393. 				wd.damage2 = 0;
  4394. #ifdef RENEWAL
  4395. 				wd.damage = battle->calc_elefix(src, target, skill_id, skill_lv, wd.damage, nk, n_ele, s_ele, s_ele_, false, wd.flag); // temporary [malufett]
  4396. #endif
  4397. 				break;
  4398. 			case NJ_ISSEN: // [malufett]
  4399. #ifndef RENEWAL
  4400. 				wd.damage = 40*sstatus->str +skill_lv*(sstatus->hp/10 + 35);
  4401. 				wd.damage2 = 0;
  4402. #else
  4403. 				{
  4404. 					short totaldef = status->get_total_def(target);
  4405. 					i = 0;
  4406. 					GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0) );
  4407. 					if( sc && sc->data[SC_NJ_BUNSINJYUTSU] && (i=sc->data[SC_NJ_BUNSINJYUTSU]->val2) > 0 )
  4408. 						wd.div_ = ~( i++ + 2 ) + 1;
  4409. 					if( wd.damage ){
  4410. 						wd.damage *= sstatus->hp * skill_lv;
  4411. 						wd.damage = wd.damage / sstatus->max_hp + sstatus->hp + i * (wd.damage / sstatus->max_hp + sstatus->hp) / 5;
  4412. 					}
  4413. 					ATK_ADD(-totaldef);
  4414. 					if( is_boss(target) )
  4415. 						ATK_RATE(50);
  4416. 					RE_SKILL_REDUCTION();
  4417. 				}
  4418. 				break;	
  4419. 			case NJ_SYURIKEN: // [malufett]
  4420. 				GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0) );
  4421. 				wd.damage += battle->calc_masteryfix(src, target, skill_id, skill_lv, 4 * skill_lv + (sd ? sd->bonus.arrow_atk : 0), wd.div_, 0, flag.weapon) - status->get_total_def(target);
  4422. 				RE_SKILL_REDUCTION();
  4423. 				break;
  4424. 			case MO_EXTREMITYFIST:	// [malufett]
  4425. 				{
  4426. 					short totaldef = status->get_total_def(target);
  4427. 					GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|8 );
  4428. 					if( wd.damage ){
  4429. 						wd.damage = (250 + 150 * skill_lv) + (10 * (status_get_sp(src)+1) * wd.damage / 100) + (8 * wd.damage);
  4430. 						ATK_ADD(-totaldef);
  4431. 					}
  4432. 					RE_SKILL_REDUCTION();
  4433. 				}
  4434. #endif
  4435. 				break;
  4436. #ifndef RENEWAL
  4437. 			case LK_SPIRALPIERCE:
  4438. 			case ML_SPIRALPIERCE:
  4439. 				if (sd) {
  4440. 					short index = sd->equip_index[EQI_HAND_R];
  4441.  
  4442. 					if (index >= 0 &&
  4443. 						sd->inventory_data[index] &&
  4444. 						sd->inventory_data[index]->type == IT_WEAPON)
  4445. 						wd.damage = sd->inventory_data[index]->weight*8/100; //80% of weight
  4446. 				} else
  4447. 					wd.damage = sstatus->rhw.atk2*8/10; //Else use Atk2
  4448.  
  4449. 				ATK_ADDRATE(50*skill_lv); //Skill modifier applies to weight only.
  4450. 				i = sstatus->str/10;
  4451. 				i*=i;
  4452. 				ATK_ADD(i); //Add str bonus.
  4453. 				switch (tstatus->size) { //Size-fix. Is this modified by weapon perfection?
  4454. 					case SZ_SMALL: //Small: 125%
  4455. 						ATK_RATE(125);
  4456. 						break;
  4457. 					//case SZ_MEDIUM: //Medium: 100%
  4458. 					case SZ_BIG: //Large: 75%
  4459. 						ATK_RATE(75);
  4460. 						break;
  4461. 				}
  4462. 				break;
  4463. #endif
  4464. 			case CR_SHIELDBOOMERANG:
  4465. 			case PA_SHIELDCHAIN:
  4466. 			case LG_SHIELDPRESS:
  4467. 			case LG_EARTHDRIVE:
  4468. 				wd.damage = sstatus->batk;
  4469. 				if (sd) {
  4470. 					short index = sd->equip_index[EQI_HAND_L];
  4471.  
  4472. 					if (index >= 0 &&
  4473. 						sd->inventory_data[index] &&
  4474. 						sd->inventory_data[index]->type == IT_ARMOR)
  4475. 						ATK_ADD(sd->inventory_data[index]->weight/10);
  4476. 				} else
  4477. 					ATK_ADD(sstatus->rhw.atk2); //Else use Atk2
  4478. 				break;
  4479. 			case HFLI_SBR44:	//[orn]
  4480. 				if(src->type == BL_HOM) {
  4481. 					wd.damage = ((TBL_HOM*)src)->homunculus.intimacy ;
  4482. 					break;
  4483. 				}
  4484. 			default:
  4485. 			{
  4486. 				i = (flag.cri
  4487. #ifdef RENEWAL
  4488. 					|| (sc && sc->data[SC_MAXIMIZEPOWER])
  4489. #endif
  4490. 					?1:0)|
  4491. 					(flag.arrow?2:0)|
  4492. #ifndef RENEWAL
  4493. 					(skill_id == HW_MAGICCRASHER?4:0)|
  4494. 					(skill_id == MO_EXTREMITYFIST?8:0)|
  4495. #endif
  4496. 					(!skill_id && sc && sc->data[SC_HLIF_CHANGE]?4:0)|
  4497. 					(sc && sc->data[SC_WEAPONPERFECT]?8:0);
  4498. 				if (flag.arrow && sd)
  4499. 				switch(sd->status.weapon) {
  4500. 					case W_BOW:
  4501. 					case W_REVOLVER:
  4502. 					case W_GATLING:
  4503. 					case W_SHOTGUN:
  4504. 					case W_GRENADE:
  4505. 						break;
  4506. 					default:
  4507. 						i |= 16; // for ex. shuriken must not be influenced by DEX
  4508. 				}
  4509. #ifdef RENEWAL
  4510. 				GET_NORMAL_ATTACK( i );
  4511. 				wd.damage = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage, wd.div_, 0, flag.weapon);
  4512. 				if (flag.lh){
  4513. 					GET_NORMAL_ATTACK2( i );
  4514. 					wd.damage2 = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage2, wd.div_, 1, flag.weapon);
  4515. 				}
  4516. #else
  4517. 				wd.damage = battle->calc_base_damage(sstatus, &sstatus->rhw, sc, tstatus->size, sd, i);
  4518. 				if (flag.lh)
  4519. 					wd.damage2 = battle->calc_base_damage(sstatus, &sstatus->lhw, sc, tstatus->size, sd, i);
  4520. #endif
  4521. 				if (nk&NK_SPLASHSPLIT){ // Divide ATK among targets
  4522. 					if(wflag>0)
  4523. 						wd.damage/= wflag;
  4524. 					else
  4525. 						ShowError("0 enemies targeted by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
  4526. 				}
  4527.  
  4528. 				//Add any bonuses that modify the base baseatk+watk (pre-skills)
  4529. 				if(sd) {
  4530. 					if (sd->bonus.atk_rate)
  4531. 						ATK_ADDRATE(sd->bonus.atk_rate);
  4532. 					if(flag.cri && sd->bonus.crit_atk_rate)
  4533. 						ATK_ADDRATE(sd->bonus.crit_atk_rate);
  4534. #ifndef RENEWAL
  4535.  
  4536. 					if(sd->status.party_id && (temp=pc->checkskill(sd,TK_POWER)) > 0){
  4537. 						if( (i = party->foreachsamemap(party->sub_count, sd, 0)) > 1 ) // exclude the player himself [Inkfish]
  4538. 							ATK_ADDRATE(2*temp*i);
  4539. 					}
  4540. #endif
  4541. 				}
  4542. 				break;
  4543. 			}	//End default case
  4544. 		} //End switch(skill_id)
  4545.  
  4546. 		if( sc && skill_id != PA_SACRIFICE ){
  4547. 			if( sc->data[SC_UNLIMIT] && wd.flag&BF_LONG )
  4548. 				ATK_ADD( 50 * sc->data[SC_UNLIMIT]->val1 );
  4549. 		}
  4550.  
  4551. 		if( tsc && skill_id != PA_SACRIFICE ){
  4552. 			if( tsc->data[SC_DARKCROW] && wd.flag&BF_SHORT )
  4553. 				ATK_ADD( 30 * tsc->data[SC_DARKCROW]->val1 );
  4554. 		}
  4555.  
  4556. 	#ifdef RENEWAL
  4557. 		if( sd && skill_id == NJ_KUNAI ){
  4558. 			flag.tdef = 1;
  4559. 			ATK_ADD( sd->bonus.arrow_atk );
  4560. 		}
  4561. 	#endif
  4562. 		switch(skill_id){
  4563. 	#ifdef RENEWAL
  4564. 			case NJ_TATAMIGAESHI:
  4565. 					ATK_RATE(200);
  4566. 			case LK_SPIRALPIERCE:
  4567. 			case ML_SPIRALPIERCE: // [malufett]
  4568. 				if( skill_id != NJ_TATAMIGAESHI ){
  4569. 					short index = sd?sd->equip_index[EQI_HAND_R]:0;
  4570. 					GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0) );
  4571. 					wd.damage = wd.damage * 70 / 100;
  4572. 					n_ele = true;
  4573.  
  4574. 					if (sd && index >= 0 &&
  4575. 						sd->inventory_data[index] &&
  4576. 						sd->inventory_data[index]->type == IT_WEAPON)
  4577. 						ATK_ADD(sd->inventory_data[index]->weight * 7 / 100);
  4578.  
  4579. 					switch (tstatus->size) {
  4580. 						case SZ_SMALL: //Small: 115%
  4581. 							ATK_RATE(115);
  4582. 							break;
  4583. 						case SZ_BIG: //Large: 85%
  4584. 							ATK_RATE(85);
  4585. 					}
  4586. 					wd.damage = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage, wd.div_, 0, flag.weapon);
  4587. 				}
  4588. 	#endif
  4589. 			default:
  4590. 				ATK_RATE(battle->calc_skillratio(BF_WEAPON, src, target, skill_id, skill_lv, skillratio, wflag));
  4591. 		}
  4592.  
  4593. 			//Constant/misc additions from skills
  4594. 		switch (skill_id) {
  4595. #ifdef RENEWAL
  4596. 			case HW_MAGICCRASHER:
  4597. 				ATK_ADD(battle->calc_magic_attack(src, target, skill_id, skill_lv, wflag).damage / 5);
  4598. 				break;
  4599. #else
  4600. 			case MO_EXTREMITYFIST:
  4601. 				ATK_ADD(250 + 150*skill_lv);
  4602. 				break;
  4603. #endif
  4604. 			case TK_DOWNKICK:
  4605. 			case TK_STORMKICK:
  4606. 			case TK_TURNKICK:
  4607. 			case TK_COUNTER:
  4608. 			case TK_JUMPKICK:
  4609. 				//TK_RUN kick damage bonus.
  4610. 				if(sd && sd->weapontype1 == W_FIST && sd->weapontype2 == W_FIST)
  4611. 					ATK_ADD(10*pc->checkskill(sd, TK_RUN));
  4612. 				break;
  4613. 			case GS_MAGICALBULLET:
  4614. #ifndef RENEWAL
  4615. 				ATK_ADD( status->get_matk(src, 2) );
  4616. #else
  4617. 				ATK_ADD( battle->calc_magic_attack(src, target, skill_id, skill_lv, wflag).damage );
  4618. 				flag.tdef = 1;
  4619. #endif
  4620. #ifndef RENEWAL
  4621. 			case NJ_SYURIKEN:
  4622. 				ATK_ADD(4*skill_lv);
  4623. #endif
  4624. 				break;
  4625. 			case GC_COUNTERSLASH:
  4626. 				ATK_ADD( status_get_agi(src) * 2 + (sd?sd->status.job_level:0) * 4 );
  4627. 				break;
  4628. 			case SR_TIGERCANNON: // (Tiger Cannon skill level x 240) + (Target Base Level x 40)
  4629. 				ATK_ADD( skill_lv * 240 + status->get_lv(target) * 40 );
  4630. 				if( sc && sc->data[SC_COMBOATTACK]
  4631. 					&& sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE ) // (Tiger Cannon skill level x 500) + (Target Base Level x 40)
  4632. 						ATK_ADD( skill_lv * 500 + status->get_lv(target) * 40 );
  4633. 				break;
  4634. 			case SR_FALLENEMPIRE:// [(Target Size value + Skill Level - 1) x Caster STR] + [(Target current weight x Caster DEX / 120)]
  4635. 				ATK_ADD( ((tstatus->size+1)*2 + skill_lv - 1) * sstatus->str);
  4636. 				if( tsd && tsd->weight ){
  4637. 					ATK_ADD( (tsd->weight/10) * sstatus->dex / 120 );
  4638. 				}else{
  4639. 					ATK_ADD( status->get_lv(target) * 50 ); //mobs
  4640. 				}
  4641. 				break;
  4642. 			case KO_SETSUDAN:
  4643. 				if( tsc && tsc->data[SC_SOULLINK] ){
  4644. 					ATK_ADDRATE(200*tsc->data[SC_SOULLINK]->val1);
  4645. 					status_change_end(target,SC_SOULLINK,INVALID_TIMER);
  4646. 				}
  4647. 				break;
  4648. 			case KO_MAKIBISHI:
  4649. 				wd.damage = 20 * skill_lv;
  4650. 				break;
  4651. 		}
  4652. #ifndef RENEWAL
  4653. 		//Div fix.
  4654. 		damage_div_fix(wd.damage, wd.div_);
  4655. #endif
  4656. 		//The following are applied on top of current damage and are stackable.
  4657. 		if ( sc ) {
  4658. #ifndef RENEWAL
  4659. 			if( sc->data[SC_TRUESIGHT] )
  4660. 				ATK_ADDRATE(2*sc->data[SC_TRUESIGHT]->val1);
  4661. #endif
  4662. 			if( sc->data[SC_GLOOMYDAY_SK] &&
  4663. 				( skill_id == LK_SPIRALPIERCE || skill_id == KN_BRANDISHSPEAR ||
  4664. 				  skill_id == CR_SHIELDBOOMERANG || skill_id == PA_SHIELDCHAIN ||
  4665. 				  skill_id == LG_SHIELDPRESS || skill_id == RK_HUNDREDSPEAR ||
  4666. 				  skill_id == CR_SHIELDCHARGE ) )
  4667. 				ATK_ADDRATE(sc->data[SC_GLOOMYDAY_SK]->val2);
  4668.  
  4669. #ifndef RENEWAL_EDP
  4670. 			if( sc->data[SC_EDP] ){
  4671. 				switch(skill_id){
  4672. 					case AS_SPLASHER:
  4673. 					case AS_GRIMTOOTH:
  4674. 					case ASC_BREAKER:
  4675. 					case AS_VENOMKNIFE:
  4676. 					case ASC_METEORASSAULT: break;
  4677. 					default:
  4678. 						ATK_ADDRATE(sc->data[SC_EDP]->val3);
  4679. 				}
  4680. 			}
  4681. #endif
  4682. 			if(sc->data[SC_STYLE_CHANGE]){
  4683. 				TBL_HOM *hd = BL_CAST(BL_HOM,src);
  4684. 				if (hd) ATK_ADD(hd->homunculus.spiritball * 3);
  4685. 			}
  4686. 		}
  4687.  
  4688. 		switch (skill_id) {
  4689. 			case AS_SONICBLOW:
  4690. 				if (sc && sc->data[SC_SOULLINK] &&
  4691. 					sc->data[SC_SOULLINK]->val2 == SL_ASSASIN)
  4692. 					ATK_ADDRATE(map_flag_gvg(src->m)?25:100); //+25% dmg on woe/+100% dmg on nonwoe
  4693.  
  4694. 				if(sd && pc->checkskill(sd,AS_SONICACCEL)>0)
  4695. 					ATK_ADDRATE(10);
  4696. 			break;
  4697. 			case CR_SHIELDBOOMERANG:
  4698. 				if(sc && sc->data[SC_SOULLINK] &&
  4699. 					sc->data[SC_SOULLINK]->val2 == SL_CRUSADER)
  4700. 					ATK_ADDRATE(100);
  4701. 				break;
  4702. 			case NC_AXETORNADO:
  4703. 				if( (sstatus->rhw.ele) == ELE_WIND || (sstatus->lhw.ele) == ELE_WIND )
  4704. 					ATK_ADDRATE(50);
  4705. 				break;
  4706. 		}
  4707.  
  4708. 		if( (i = battle->adjust_skill_damage(src->m,skill_id)) )
  4709. 			ATK_RATE(i);
  4710. 	#ifdef RENEWAL
  4711. 		if( skill_id && (wd.damage+wd.damage2) ){
  4712. 			RE_SKILL_REDUCTION();
  4713. 		}
  4714. 	#endif	
  4715. 		if( sd ) {
  4716. 			if (skill_id && (i = pc->skillatk_bonus(sd, skill_id)))
  4717. 				ATK_ADDRATE(i);
  4718. 	#ifdef RENEWAL
  4719. 			if( wd.flag&BF_LONG )
  4720. 				ATK_ADDRATE(sd->bonus.long_attack_atk_rate);
  4721. 	#endif	
  4722. 			if( (i=pc->checkskill(sd,AB_EUCHARISTICA)) > 0 &&
  4723. 				(tstatus->race == RC_DEMON || tstatus->def_ele == ELE_DARK) )
  4724. 				ATK_ADDRATE(-i);
  4725. 			if( skill_id != PA_SACRIFICE && skill_id != MO_INVESTIGATE && skill_id != CR_GRANDCROSS && skill_id != NPC_GRANDDARKNESS && skill_id != PA_SHIELDCHAIN && !flag.cri )
  4726. 			{ //Elemental/Racial adjustments
  4727. 				if( sd->right_weapon.def_ratio_atk_ele & (1<<tstatus->def_ele) ||
  4728. 					sd->right_weapon.def_ratio_atk_race & (1<<tstatus->race) ||
  4729. 					sd->right_weapon.def_ratio_atk_race & (1<<(is_boss(target)?RC_BOSS:RC_NONBOSS))
  4730. 				)
  4731. 					flag.pdef = 1;
  4732.  
  4733. 				if( sd->left_weapon.def_ratio_atk_ele & (1<<tstatus->def_ele) ||
  4734. 					sd->left_weapon.def_ratio_atk_race & (1<<tstatus->race) ||
  4735. 					sd->left_weapon.def_ratio_atk_race & (1<<(is_boss(target)?RC_BOSS:RC_NONBOSS))
  4736. 				)
  4737. 				{ //Pass effect onto right hand if configured so. [Skotlex]
  4738. 					if (battle_config.left_cardfix_to_right && flag.rh)
  4739. 						flag.pdef = 1;
  4740. 					else
  4741. 						flag.pdef2 = 1;
  4742. 				}
  4743. 			}
  4744.  
  4745. 			if (skill_id != CR_GRANDCROSS && skill_id != NPC_GRANDDARKNESS)
  4746. 		  	{	//Ignore Defense?
  4747. 				if (!flag.idef && (
  4748. 					sd->right_weapon.ignore_def_ele & (1<<tstatus->def_ele) ||
  4749. 					sd->right_weapon.ignore_def_race & (1<<tstatus->race) ||
  4750. 					sd->right_weapon.ignore_def_race & (is_boss(target)?1<<RC_BOSS:1<<RC_NONBOSS)
  4751. 				))
  4752. 					flag.idef = 1;
  4753.  
  4754. 				if (!flag.idef2 && (
  4755. 					sd->left_weapon.ignore_def_ele & (1<<tstatus->def_ele) ||
  4756. 					sd->left_weapon.ignore_def_race & (1<<tstatus->race) ||
  4757. 					sd->left_weapon.ignore_def_race & (is_boss(target)?1<<RC_BOSS:1<<RC_NONBOSS)
  4758. 				)) {
  4759. 						if(battle_config.left_cardfix_to_right && flag.rh) //Move effect to right hand. [Skotlex]
  4760. 							flag.idef = 1;
  4761. 						else
  4762. 							flag.idef2 = 1;
  4763. 				}
  4764. 			}
  4765. 		}
  4766.  
  4767. 		if(!flag.idef || !flag.idef2) { //Defense reduction
  4768. 			wd.damage = battle->calc_defense(BF_WEAPON, src, target, skill_id, skill_lv, wd.damage,
  4769. 											 (flag.idef?1:0)|(flag.pdef?2:0)
  4770. #ifdef RENEWAL
  4771. 											 |(flag.tdef?4:0)
  4772. #endif
  4773. 											 , flag.pdef);
  4774. 			if( wd.damage2 )
  4775. 				wd.damage2 = battle->calc_defense(BF_WEAPON, src, target, skill_id, skill_lv, wd.damage2,
  4776. 												  (flag.idef2?1:0)|(flag.pdef2?2:0)
  4777. #ifdef RENEWAL
  4778. 												  |(flag.tdef?4:0)
  4779. #endif
  4780. 												  , flag.pdef2);
  4781. 		}
  4782.  
  4783. #ifdef RENEWAL
  4784. 		//Div fix.
  4785. 		damage_div_fix(wd.damage, wd.div_);
  4786. #endif
  4787. 		//Post skill/vit reduction damage increases
  4788. 		if( sc )
  4789. 		{	//SC skill damages
  4790. 			if(sc->data[SC_AURABLADE]
  4791. #ifndef RENEWAL
  4792. 					&& skill_id != LK_SPIRALPIERCE && skill_id != ML_SPIRALPIERCE
  4793. #endif
  4794. 			){
  4795. 				int lv = sc->data[SC_AURABLADE]->val1;
  4796. #ifdef RENEWAL
  4797. 				lv *= ((skill_id == LK_SPIRALPIERCE || skill_id == ML_SPIRALPIERCE)?wd.div_:1); // +100 per hit in lv 5
  4798. #endif
  4799. 				ATK_ADD(20*lv);
  4800. 			}
  4801.  
  4802. 			if( !skill_id ) {
  4803. 				if( sc->data[SC_ENCHANTBLADE] ) {
  4804. 					//[( ( Skill Lv x 20 ) + 100 ) x ( casterBaseLevel / 150 )] + casterInt
  4805. 					i = ( sc->data[SC_ENCHANTBLADE]->val1 * 20 + 100 ) * status->get_lv(src) / 150 + status_get_int(src);
  4806. 					i = i - status->get_total_mdef(target) + status->get_matk(src, 2);
  4807. 					if( i )
  4808. 						ATK_ADD(i);
  4809. 				}
  4810. 				if( sc->data[SC_GIANTGROWTH] && rnd()%100 < 15 )
  4811. 					ATK_ADDRATE(200); // Triple Damage
  4812. 			}
  4813.  
  4814. 		}
  4815. #ifndef RENEWAL
  4816. 		//Refine bonus
  4817. 		if( sd && flag.weapon && skill_id != MO_INVESTIGATE && skill_id != MO_EXTREMITYFIST )
  4818. 		{ // Counts refine bonus multiple times
  4819. 			if( skill_id == MO_FINGEROFFENSIVE )
  4820. 			{
  4821. 				ATK_ADD2(wd.div_*sstatus->rhw.atk2, wd.div_*sstatus->lhw.atk2);
  4822. 			} else {
  4823. 				ATK_ADD2(sstatus->rhw.atk2, sstatus->lhw.atk2);
  4824. 			}
  4825. 		}
  4826. 		//Set to min of 1
  4827. 		if (flag.rh && wd.damage < 1) wd.damage = 1;
  4828. 		if (flag.lh && wd.damage2 < 1) wd.damage2 = 1;
  4829. #else
  4830. 		if (flag.rh && wd.damage < 1) wd.damage = 0;
  4831. 		if (flag.lh && wd.damage2 < 1) wd.damage2 = 0;
  4832. #endif
  4833.  
  4834. #ifndef RENEWAL
  4835. 		wd.damage = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage, wd.div_, 0, flag.weapon);
  4836.