From 20f64e2ce52e10e2a7392f5e4ff696f2ef6d4f86 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 11 Jan 2023 12:06:47 +0200 Subject: [PATCH] Damage gets randomness as input --- .../com/unciv/logic/battle/BattleDamage.kt | 21 +++++++++---------- .../ui/worldscreen/bottombar/BattleTable.kt | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/logic/battle/BattleDamage.kt b/core/src/com/unciv/logic/battle/BattleDamage.kt index 932b1a47a0..c5b4ea8348 100644 --- a/core/src/com/unciv/logic/battle/BattleDamage.kt +++ b/core/src/com/unciv/logic/battle/BattleDamage.kt @@ -242,31 +242,34 @@ object BattleDamage { fun calculateDamageToAttacker( attacker: ICombatant, defender: ICombatant, - ignoreRandomness: Boolean = false, + /** Between 0 and 1. */ + randomnessFactor: Float = Random(attacker.getCivInfo().gameInfo.turns * attacker.getTile().position.hashCode().toLong()).nextFloat() ): Int { if (attacker.isRanged() && !attacker.isAirUnit()) return 0 if (defender.isCivilian()) return 0 val ratio = getAttackingStrength(attacker, defender) / getDefendingStrength( attacker, defender) - return (damageModifier(ratio, true, attacker, ignoreRandomness) * getHealthDependantDamageRatio(defender)).roundToInt() + return (damageModifier(ratio, true, randomnessFactor) * getHealthDependantDamageRatio(defender)).roundToInt() } fun calculateDamageToDefender( attacker: ICombatant, defender: ICombatant, - ignoreRandomness: Boolean = false, + /** Between 0 and 1. Defaults to turn and location-based random to avoid save scumming */ + randomnessFactor: Float = Random(attacker.getCivInfo().gameInfo.turns * attacker.getTile().position.hashCode().toLong()).nextFloat() + , ): Int { if (defender.isCivilian()) return 40 val ratio = getAttackingStrength(attacker, defender) / getDefendingStrength( attacker, defender) - return (damageModifier(ratio, false, attacker, ignoreRandomness) * getHealthDependantDamageRatio(attacker)).roundToInt() + return (damageModifier(ratio, false, randomnessFactor) * getHealthDependantDamageRatio(attacker)).roundToInt() } private fun damageModifier( attackerToDefenderRatio: Float, damageToAttacker: Boolean, - attacker: ICombatant, // for the randomness - ignoreRandomness: Boolean = false, + /** Between 0 and 1. */ + randomnessFactor: Float, ): Float { // https://forums.civfanatics.com/threads/getting-the-combat-damage-math.646582/#post-15468029 val strongerToWeakerRatio = @@ -274,14 +277,10 @@ object BattleDamage { var ratioModifier = (((strongerToWeakerRatio + 3) / 4).pow(4) + 1) / 2 if (damageToAttacker && attackerToDefenderRatio > 1 || !damageToAttacker && attackerToDefenderRatio < 1) // damage ratio from the weaker party is inverted ratioModifier = ratioModifier.pow(-1) - val randomSeed = attacker.getCivInfo().gameInfo.turns * attacker.getTile().position.hashCode() // so people don't save-scum to get optimal results - val randomCenteredAround30 = 24 + - if (ignoreRandomness) 6f - else 12 * Random(randomSeed.toLong()).nextFloat() + val randomCenteredAround30 = 24 + 12 * randomnessFactor return randomCenteredAround30 * ratioModifier } } - enum class CombatAction { Attack, Defend, diff --git a/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt b/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt index 96ad77f444..39a8beda19 100644 --- a/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt +++ b/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt @@ -186,8 +186,8 @@ class BattleTable(val worldScreen: WorldScreen): Table() { row() } - var damageToDefender = BattleDamage.calculateDamageToDefender(attacker, defender, true) - var damageToAttacker = BattleDamage.calculateDamageToAttacker(attacker, defender, true) + var damageToDefender = BattleDamage.calculateDamageToDefender(attacker, defender, 0.5f) + var damageToAttacker = BattleDamage.calculateDamageToAttacker(attacker, defender, 0.5f) if (damageToAttacker > attacker.getHealth() && damageToDefender > defender.getHealth()) { // when damage exceeds health, we don't want to show negative health numbers