From a81d5f0837dc0842ea4efe6d1649402862c6e1b5 Mon Sep 17 00:00:00 2001 From: Oskar Niesen Date: Sat, 7 Oct 2023 14:00:49 -0500 Subject: [PATCH] AI now uses free tech points (#10245) * AI now uses free tech points * Refactored chooseTechToResearch * Reverted combining lists to use a + --- .../civilization/NextTurnAutomation.kt | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt index c800cc33f7..ce4fc18cfd 100644 --- a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt @@ -515,22 +515,32 @@ object NextTurnAutomation { } private fun chooseTechToResearch(civInfo: Civilization) { - if (civInfo.tech.techsToResearch.isEmpty()) { + fun getGroupedResearchableTechs(): List> { val researchableTechs = civInfo.gameInfo.ruleset.technologies.values - .filter { civInfo.tech.canBeResearched(it.name) } - val techsGroups = researchableTechs.groupBy { it.cost } - val costs = techsGroups.keys.sorted() + .asSequence() + .filter { civInfo.tech.canBeResearched(it.name) } + .groupBy { it.cost } + return researchableTechs.toSortedMap().values.toList() + } + while(civInfo.tech.freeTechs > 0) { + val costs = getGroupedResearchableTechs() + if (costs.isEmpty()) return - if (researchableTechs.isEmpty()) return - - val cheapestTechs = techsGroups[costs[0]]!! + val mostExpensiveTechs = costs[costs.size - 1] + civInfo.tech.getFreeTechnology(mostExpensiveTechs.random().name) + } + if (civInfo.tech.techsToResearch.isEmpty()) { + val costs = getGroupedResearchableTechs() + if (costs.isEmpty()) return + + val cheapestTechs = costs[0] //Do not consider advanced techs if only one tech left in cheapest group val techToResearch: Technology = if (cheapestTechs.size == 1 || costs.size == 1) { cheapestTechs.random() } else { //Choose randomly between cheapest and second cheapest group - val techsAdvanced = techsGroups[costs[1]]!! + val techsAdvanced = costs[1] (cheapestTechs + techsAdvanced).random() }