From c41410812a1f86df8cecef1b834b6ca064974c06 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 16 Feb 2022 12:29:28 +0200 Subject: [PATCH] Resolved #6175 - attack randomness is turn and tile based to avoid save-scumming --- core/src/com/unciv/logic/battle/BattleDamage.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core/src/com/unciv/logic/battle/BattleDamage.kt b/core/src/com/unciv/logic/battle/BattleDamage.kt index 96000cc2fa..9da043f17b 100644 --- a/core/src/com/unciv/logic/battle/BattleDamage.kt +++ b/core/src/com/unciv/logic/battle/BattleDamage.kt @@ -295,7 +295,7 @@ object BattleDamage { attacker, defender ) - return (damageModifier(ratio, true) * getHealthDependantDamageRatio(defender)).roundToInt() + return (damageModifier(ratio, true, attacker) * getHealthDependantDamageRatio(defender)).roundToInt() } fun calculateDamageToDefender( @@ -309,17 +309,22 @@ object BattleDamage { defender ) if (defender.isCivilian()) return 40 - return (damageModifier(ratio, false) * getHealthDependantDamageRatio(attacker)).roundToInt() + return (damageModifier(ratio, false, attacker) * getHealthDependantDamageRatio(attacker)).roundToInt() } - private fun damageModifier(attackerToDefenderRatio: Float, damageToAttacker: Boolean): Float { + private fun damageModifier( + attackerToDefenderRatio: Float, + damageToAttacker: Boolean, + attacker: ICombatant // for the randomness + ): Float { // https://forums.civfanatics.com/threads/getting-the-combat-damage-math.646582/#post-15468029 val strongerToWeakerRatio = attackerToDefenderRatio.pow(if (attackerToDefenderRatio < 1) -1 else 1) 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 randomCenteredAround30 = 24 + 12 * Random().nextFloat() + val randomSeed = attacker.getCivInfo().gameInfo.turns * attacker.getTile().position.hashCode() // so people don't save-scum to get optimal results + val randomCenteredAround30 = 24 + 12 * Random(randomSeed.toLong()).nextFloat() return randomCenteredAround30 * ratioModifier } }