From e6bb9d4d9ad7690c3b7ee8017615accd2b987a02 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 19 Nov 2023 15:56:21 +0200 Subject: [PATCH] More simplification of motivationToAttack --- .../MotivationToAttackAutomation.kt | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/core/src/com/unciv/logic/automation/civilization/MotivationToAttackAutomation.kt b/core/src/com/unciv/logic/automation/civilization/MotivationToAttackAutomation.kt index 6c3ab3a69f..47f10b17fb 100644 --- a/core/src/com/unciv/logic/automation/civilization/MotivationToAttackAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/MotivationToAttackAutomation.kt @@ -19,20 +19,12 @@ object MotivationToAttackAutomation { /** Will return the motivation to attack, but might short circuit if the value is guaranteed to * be lower than `atLeast`. So any values below `atLeast` should not be used for comparison. */ - public fun hasAtLeastMotivationToAttack(civInfo: Civilization, otherCiv: Civilization, atLeast: Int): Int { + fun hasAtLeastMotivationToAttack(civInfo: Civilization, otherCiv: Civilization, atLeast: Int): Int { val closestCities = NextTurnAutomation.getClosestCities(civInfo, otherCiv) ?: return 0 val baseForce = 30f - var ourCombatStrength = civInfo.getStatForRanking(RankingType.Force).toFloat() + baseForce - if (civInfo.getCapital() != null) ourCombatStrength += CityCombatant(civInfo.getCapital()!!).getCityStrength() - var theirCombatStrength = otherCiv.getStatForRanking(RankingType.Force).toFloat() + baseForce - if(otherCiv.getCapital() != null) theirCombatStrength += CityCombatant(otherCiv.getCapital()!!).getCityStrength() - - //for city-states, also consider their protectors - if (otherCiv.isCityState() and otherCiv.cityStateFunctions.getProtectorCivs().isNotEmpty()) { - theirCombatStrength += otherCiv.cityStateFunctions.getProtectorCivs().filterNot { it == civInfo } - .sumOf { it.getStatForRanking(RankingType.Force) } - } + val ourCombatStrength = calculateSelfCombatStrength(civInfo, baseForce) + val theirCombatStrength = calculateCombatStrengthWithProtectors(otherCiv, baseForce, civInfo) if (theirCombatStrength > ourCombatStrength) return 0 @@ -98,12 +90,8 @@ object MotivationToAttackAutomation { var motivationSoFar = modifierMap.values.sum() - // We don't need to execute the expensive BFSs below if we're below the threshold here - // anyways, since it won't get better from those, only worse. - if (motivationSoFar < atLeast) { - return motivationSoFar - } - + // Short-circuit to avoid expensive BFS + if (motivationSoFar < atLeast) return motivationSoFar val landPathBFS = BFS(ourCity.getCenterTile()) { it.isLand && isTileCanMoveThrough(it) @@ -113,11 +101,8 @@ object MotivationToAttackAutomation { if (!landPathBFS.hasReachedTile(theirCity.getCenterTile())) motivationSoFar -= -10 - // We don't need to execute the expensive BFSs below if we're below the threshold here - // anyways, since it won't get better from those, only worse. - if (motivationSoFar < atLeast) { - return motivationSoFar - } + // Short-circuit to avoid expensive BFS + if (motivationSoFar < atLeast) return motivationSoFar val reachableEnemyCitiesBfs = BFS(civInfo.getCapital(true)!!.getCenterTile()) { isTileCanMoveThrough(it) } reachableEnemyCitiesBfs.stepToEnd() @@ -127,6 +112,23 @@ object MotivationToAttackAutomation { return motivationSoFar } + private fun calculateCombatStrengthWithProtectors(otherCiv: Civilization, baseForce: Float, civInfo: Civilization): Float { + var theirCombatStrength = calculateSelfCombatStrength(otherCiv, baseForce) + + //for city-states, also consider their protectors + if (otherCiv.isCityState() and otherCiv.cityStateFunctions.getProtectorCivs().isNotEmpty()) { + theirCombatStrength += otherCiv.cityStateFunctions.getProtectorCivs().filterNot { it == civInfo } + .sumOf { it.getStatForRanking(RankingType.Force) } + } + return theirCombatStrength + } + + private fun calculateSelfCombatStrength(civInfo: Civilization, baseForce: Float): Float { + var ourCombatStrength = civInfo.getStatForRanking(RankingType.Force).toFloat() + baseForce + if (civInfo.getCapital() != null) ourCombatStrength += CityCombatant(civInfo.getCapital()!!).getCityStrength() + return ourCombatStrength + } + private fun addWonderBasedMotivations(otherCiv: Civilization, modifierMap: HashMap) { var wonderCount = 0 for (city in otherCiv.cities) {