From 6d278bc2fe01912917e5347f29b0821a34d361c0 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Thu, 13 Feb 2025 23:25:40 +0200 Subject: [PATCH] AI: choose policy branch at random between those with the least remaining policies --- .../civilization/NextTurnAutomation.kt | 16 ++++++---------- .../com/unciv/app/desktop/UniqueDocsWriter.kt | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt index be88b10306..f63501947c 100644 --- a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt @@ -328,26 +328,22 @@ object NextTurnAutomation { // If incomplete branches have higher priorities than any newly adoptable branch, if (maxAdoptablePriority <= maxIncompletePriority) { // Prioritize finishing one of the unfinished branches - incompleteBranches.filter { - priorityMap[it] == maxIncompletePriority - }.toSet() + incompleteBranches.filter { priorityMap[it] == maxIncompletePriority }.toSet() } // If newly adoptable branches have higher priorities than any incomplete branch, else { // Prioritize adopting one of the new branches - adoptableBranches.filter { - priorityMap[it] == maxAdoptablePriority - }.toSet() + adoptableBranches.filter { priorityMap[it] == maxAdoptablePriority }.toSet() } // branchCompletionMap but keys are only candidates val candidateCompletionMap: Map = - civInfo.policies.branchCompletionMap.filterKeys { key -> - key in candidates - } + civInfo.policies.branchCompletionMap.filterKeys { key -> key in candidates } // Choose the branch with the LEAST REMAINING policies, not the MOST ADOPTED ones - val targetBranch = candidateCompletionMap.minBy { it.key.policies.size - it.value }.key + val targetBranch = candidateCompletionMap.asIterable() + .groupBy { it.key.policies.size - it.value } + .minByOrNull { it.key }!!.value.random().key val policyToAdopt: Policy = if (civInfo.policies.isAdoptable(targetBranch)) targetBranch diff --git a/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt b/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt index 5cf16ffc09..0225147923 100644 --- a/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt +++ b/desktop/src/com/unciv/app/desktop/UniqueDocsWriter.kt @@ -42,7 +42,7 @@ class UniqueDocsWriter { // by their UniqueTarget.ordinal as well - source code order. val targetTypesToUniques: Map> = if (showUniqueOnOneTarget) - UniqueType.entries.asSequence() + UniqueType.entries .groupBy { it.targetTypes.minOrNull()!! } .toSortedMap() else