Use ranged strength when defending against ranged attacks (#6783)

* Ranged units should use ranged strength when defending against ranged attacks.

* Renamed variable plus some minor style cleanup

Co-authored-by: OptimizedForDensity <>
This commit is contained in:
OptimizedForDensity
2022-05-12 17:11:04 -04:00
committed by GitHub
parent 940f361a5f
commit 4ab7d56c14
5 changed files with 19 additions and 12 deletions

View File

@ -255,7 +255,7 @@ object BattleDamage {
*/ */
private fun getDefendingStrength(attacker: ICombatant, defender: ICombatant): Float { private fun getDefendingStrength(attacker: ICombatant, defender: ICombatant): Float {
val defenceModifier = modifiersToMultiplicationBonus(getDefenceModifiers(attacker, defender)) val defenceModifier = modifiersToMultiplicationBonus(getDefenceModifiers(attacker, defender))
return max(1f, defender.getDefendingStrength() * defenceModifier) return max(1f, defender.getDefendingStrength(attacker.isRanged()) * defenceModifier)
} }
fun calculateDamageToAttacker( fun calculateDamageToAttacker(

View File

@ -33,7 +33,7 @@ class CityCombatant(val city: CityInfo) : ICombatant {
override fun getUnitType(): UnitType = UnitType.City override fun getUnitType(): UnitType = UnitType.City
override fun getAttackingStrength(): Int = (getCityStrength(CombatAction.Attack) * 0.75).roundToInt() override fun getAttackingStrength(): Int = (getCityStrength(CombatAction.Attack) * 0.75).roundToInt()
override fun getDefendingStrength(): Int { override fun getDefendingStrength(attackedByRanged: Boolean): Int {
if (isDefeated()) return 1 if (isDefeated()) return 1
return getCityStrength() return getCityStrength()
} }

View File

@ -7,19 +7,19 @@ import com.unciv.models.ruleset.unit.UnitType
interface ICombatant { interface ICombatant {
fun getName(): String fun getName(): String
fun getHealth():Int fun getHealth(): Int
fun getMaxHealth():Int fun getMaxHealth(): Int
fun getUnitType(): UnitType fun getUnitType(): UnitType
fun getAttackingStrength(): Int fun getAttackingStrength(): Int
fun getDefendingStrength(): Int fun getDefendingStrength(attackedByRanged: Boolean = false): Int
fun takeDamage(damage:Int) fun takeDamage(damage: Int)
fun isDefeated():Boolean fun isDefeated(): Boolean
fun getCivInfo(): CivilizationInfo fun getCivInfo(): CivilizationInfo
fun getTile(): TileInfo fun getTile(): TileInfo
fun isInvisible(to: CivilizationInfo): Boolean fun isInvisible(to: CivilizationInfo): Boolean
fun canAttack(): Boolean fun canAttack(): Boolean
/** Implements [UniqueParameterType.CombatantFilter][com.unciv.models.ruleset.unique.UniqueParameterType.CombatantFilter] */ /** Implements [UniqueParameterType.CombatantFilter][com.unciv.models.ruleset.unique.UniqueParameterType.CombatantFilter] */
fun matchesCategory(category:String): Boolean fun matchesCategory(category: String): Boolean
fun getAttackSound(): UncivSound fun getAttackSound(): UncivSound
fun isMelee(): Boolean = !isRanged() fun isMelee(): Boolean = !isRanged()

View File

@ -18,7 +18,7 @@ class MapUnitCombatant(val unit: MapUnit) : ICombatant {
override fun isDefeated(): Boolean = unit.health <= 0 override fun isDefeated(): Boolean = unit.health <= 0
override fun isInvisible(to: CivilizationInfo): Boolean = unit.isInvisible(to) override fun isInvisible(to: CivilizationInfo): Boolean = unit.isInvisible(to)
override fun canAttack(): Boolean = unit.canAttack() override fun canAttack(): Boolean = unit.canAttack()
override fun matchesCategory(category:String) = unit.matchesFilter(category) override fun matchesCategory(category: String) = unit.matchesFilter(category)
override fun getAttackSound() = unit.baseUnit.attackSound.let { override fun getAttackSound() = unit.baseUnit.attackSound.let {
if (it==null) UncivSound.Click else UncivSound.custom(it) if (it==null) UncivSound.Click else UncivSound.custom(it)
} }
@ -34,9 +34,11 @@ class MapUnitCombatant(val unit: MapUnit) : ICombatant {
else unit.baseUnit().strength else unit.baseUnit().strength
} }
override fun getDefendingStrength(): Int { override fun getDefendingStrength(attackedByRanged: Boolean): Int {
return if (unit.isEmbarked() && !isCivilian()) return if (unit.isEmbarked() && !isCivilian())
unit.civInfo.getEra().embarkDefense unit.civInfo.getEra().embarkDefense
else if (isRanged() && attackedByRanged)
unit.baseUnit().rangedStrength
else unit.baseUnit().strength else unit.baseUnit().strength
} }

View File

@ -121,8 +121,13 @@ class BattleTable(val worldScreen: WorldScreen): Table() {
addSeparator().pad(0f) addSeparator().pad(0f)
add(attacker.getAttackingStrength().toString() + Fonts.strength) val attackIcon = if (attacker.isRanged()) Fonts.rangedStrength else Fonts.strength
add(defender.getDefendingStrength().toString() + Fonts.strength).row() var defenceIcon =
if (attacker.isRanged() && defender.isRanged() && !defender.isCity() && !(defender is MapUnitCombatant && defender.unit.isEmbarked()))
Fonts.rangedStrength
else Fonts.strength // use strength icon if attacker is melee, defender is melee, defender is a city, or defender is embarked
add(attacker.getAttackingStrength().toString() + attackIcon)
add(defender.getDefendingStrength(attacker.isRanged()).toString() + defenceIcon).row()
val quarterScreen = worldScreen.stage.width/4 val quarterScreen = worldScreen.stage.width/4