package formula

// Combat formulas mirror config/balance.json and PHP CombatFormulas.

const ArmorCapPercent = 85.0

func DamageReduction(armor int) float64 {
	reduction := (float64(armor) / (float64(armor) + 200)) * 100
	if reduction > ArmorCapPercent {
		reduction = ArmorCapPercent
	}
	return reduction / 100
}

func PhysicalDamage(strength, weaponBonus int) int {
	d := int(float64(strength)*0.5) + weaponBonus
	if d < 1 {
		return 1
	}
	return d
}

func ApplyArmor(damage, armor int) int {
	reduction := DamageReduction(armor)
	d := int(float64(damage) * (1 - reduction))
	if d < 1 {
		return 1
	}
	return d
}

func HitChance(attackerDex, defenderDex int) float64 {
	base := 0.75
	delta := float64(attackerDex-defenderDex) * 0.01
	if base+delta < 0.1 {
		return 0.1
	}
	if base+delta > 0.95 {
		return 0.95
	}
	return base + delta
}

func CritChance(agility int) float64 {
	c := 0.05 + float64(agility)*0.005
	if c > 0.5 {
		return 0.5
	}
	return c
}

func MaxHP(strength, spirit int) int {
	return 50 + strength*5 + spirit*2
}

func MaxMana(intelligence, spirit int) int {
	return 20 + intelligence*3 + spirit*4
}

func MaxFatigue(agility, spirit int) int {
	return 50 + agility*2 + spirit
}
