From 734fba8f30cd7c34b4043304bc83d7fbc0723da4 Mon Sep 17 00:00:00 2001 From: Xander Lenstra <71121390+xlenstra@users.noreply.github.com> Date: Wed, 13 Oct 2021 11:03:34 +0200 Subject: [PATCH] Fixed bugs with diplomatic victory (#5474) --- .../logic/civilization/CivilizationInfo.kt | 52 ++++++++++--------- .../logic/civilization/VictoryManager.kt | 5 +- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt index 208f74b34a..2465332349 100644 --- a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt +++ b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt @@ -835,11 +835,10 @@ class CivilizationInfo { private fun startTurnFlags() { for (flag in flagsCountdown.keys.toList()) { - // There are cases where we remove flags while iterating, like ShowDiplomaticVotingResults + // In case we remove flags while iterating if (!flagsCountdown.containsKey(flag)) continue - // the "ignoreCase = true" is to catch 'cityStateGreatPersonGift' instead of 'CityStateGreatPersonGift' being in old save files - if (flag == CivFlags.CityStateGreatPersonGift.name || flag.equals(CivFlags.CityStateGreatPersonGift.name, ignoreCase = true)) { + if (flag == CivFlags.CityStateGreatPersonGift.name) { val cityStateAllies = getKnownCivs().filter { it.isCityState() && it.getAllyCiv() == civName } if (cityStateAllies.any()) flagsCountdown[flag] = flagsCountdown[flag]!! - 1 @@ -857,31 +856,35 @@ class CivilizationInfo { if (flagsCountdown[flag]!! > 0) flagsCountdown[flag] = flagsCountdown[flag]!! - 1 - - if (flagsCountdown[flag]!! != 0) continue - - when (flag) { - CivFlags.TurnsTillNextDiplomaticVote.name -> addFlag(CivFlags.ShowDiplomaticVotingResults.name, 1) - CivFlags.ShouldResetDiplomaticVotes.name -> { - gameInfo.diplomaticVictoryVotesCast.clear() - removeFlag(CivFlags.ShouldResetDiplomaticVotes.name) - removeFlag(CivFlags.ShowDiplomaticVotingResults.name) - } - CivFlags.ShowDiplomaticVotingResults.name -> { - - if (gameInfo.civilizations.any { it.victoryManager.hasWon() } ) - // We have either already done this calculation, or it doesn't matter anymore, - // so don't waste resources doing it - continue - - addFlag(CivFlags.ShouldResetDiplomaticVotes.name, 1) - } + + } + handleDiplomaticVictoryFlags() + } + + private fun handleDiplomaticVictoryFlags() { + if (flagsCountdown[CivFlags.ShouldResetDiplomaticVotes.name] == 0) { + gameInfo.diplomaticVictoryVotesCast.clear() + removeFlag(CivFlags.ShouldResetDiplomaticVotes.name) + removeFlag(CivFlags.ShowDiplomaticVotingResults.name) + } + + if (flagsCountdown[CivFlags.ShowDiplomaticVotingResults.name] == 0) { + if (gameInfo.civilizations.any { it.victoryManager.hasWon() } ) { + removeFlag(CivFlags.TurnsTillNextDiplomaticVote.name) + } else { + addFlag(CivFlags.ShouldResetDiplomaticVotes.name, 1) + addFlag(CivFlags.TurnsTillNextDiplomaticVote.name, getTurnsBetweenDiplomaticVotings()) } } + + if (flagsCountdown[CivFlags.TurnsTillNextDiplomaticVote.name] == 0) { + addFlag(CivFlags.ShowDiplomaticVotingResults.name, 1) + } } - fun addFlag(flag: String, count: Int) { flagsCountdown[flag] = count } - fun removeFlag(flag: String) { flagsCountdown.remove(flag) } + fun addFlag(flag: String, count: Int) = flagsCountdown.set(flag, count) + + fun removeFlag(flag: String) = flagsCountdown.remove(flag) fun getTurnsBetweenDiplomaticVotings() = (15 * gameInfo.gameParameters.gameSpeed.modifier).toInt() // Dunno the exact calculation, hidden in Lua files @@ -895,7 +898,6 @@ class CivilizationInfo { fun diplomaticVoteForCiv(chosenCivName: String?) { if (chosenCivName != null) gameInfo.diplomaticVictoryVotesCast[civName] = chosenCivName - addFlag(CivFlags.TurnsTillNextDiplomaticVote.name, getTurnsBetweenDiplomaticVotings()) } fun shouldShowDiplomaticVotingResults() = diff --git a/core/src/com/unciv/logic/civilization/VictoryManager.kt b/core/src/com/unciv/logic/civilization/VictoryManager.kt index 4b8dcc5463..22b59b119c 100644 --- a/core/src/com/unciv/logic/civilization/VictoryManager.kt +++ b/core/src/com/unciv/logic/civilization/VictoryManager.kt @@ -52,12 +52,13 @@ class VictoryManager { fun hasEnoughVotesForDiplomaticVictory(): Boolean { val results = calculateDiplomaticVotingResults(civInfo.gameInfo.diplomaticVictoryVotesCast) - val bestCiv = results.maxByOrNull { it.value } - if (bestCiv == null) return false + val bestCiv = results.maxByOrNull { it.value } ?: return false // If we don't have the highest score, we have not won anyway if (bestCiv.key != civInfo.civName) return false + if (bestCiv.value < votesNeededForDiplomaticVictory()) return false + // If there's a tie, we haven't won either return (results.none { it != bestCiv && it.value == bestCiv.value }) }