diff --git a/core/src/com/unciv/logic/simulation/MutableInt.kt b/core/src/com/unciv/logic/simulation/MutableInt.kt index b3a993ddc3c5e..5eac80e02f62b 100644 --- a/core/src/com/unciv/logic/simulation/MutableInt.kt +++ b/core/src/com/unciv/logic/simulation/MutableInt.kt @@ -6,6 +6,7 @@ class MutableInt(var value: Int = 0) { fun inc() { ++value } fun get(): Int { return value } fun set(newValue: Int) { value = newValue } + fun add(addend: Int) { value += addend } override fun toString(): String { return value.tr() diff --git a/core/src/com/unciv/logic/simulation/Simulation.kt b/core/src/com/unciv/logic/simulation/Simulation.kt index 4e28c77a04ad3..614714facf3d4 100644 --- a/core/src/com/unciv/logic/simulation/Simulation.kt +++ b/core/src/com/unciv/logic/simulation/Simulation.kt @@ -20,7 +20,8 @@ class Simulation( private val newGameInfo: GameInfo, val simulationsPerThread: Int = 1, private val threadsNumber: Int = 1, - private val maxTurns: Int = 500 + private val maxTurns: Int = 500, + private val statTurns: List = listOf() ) { private val maxSimulations = threadsNumber * simulationsPerThread val civilizations = newGameInfo.civilizations.filter { it.civName != Constants.spectator }.map { it.civName } @@ -28,6 +29,7 @@ class Simulation( private var startTime: Long = 0 var steps = ArrayList() var numWins = mutableMapOf() + var sumStat = mutableMapOf>() private var winRateByVictory = HashMap>() private var winTurnByVictory = HashMap>() private var avgSpeed = 0f @@ -40,6 +42,8 @@ class Simulation( init{ for (civ in civilizations) { this.numWins[civ] = MutableInt(0) + for (turn in statTurns) + this.sumStat.getOrPut(civ) { mutableMapOf() }[turn] = MutableInt(0) winRateByVictory[civ] = mutableMapOf() for (victory in UncivGame.Current.gameInfo!!.ruleset.victories.keys) winRateByVictory[civ]!![victory] = MutableInt(0) @@ -60,12 +64,23 @@ class Simulation( jobs.add(launch(CoroutineName("simulation-${threadId}")) { repeat(simulationsPerThread) { val gameInfo = GameStarter.startNewGame(GameSetupInfo(newGameInfo)) - gameInfo.simulateMaxTurns = maxTurns gameInfo.simulateUntilWin = true - gameInfo.nextTurn() + for (turn in statTurns) { + gameInfo.simulateMaxTurns = turn + gameInfo.nextTurn() + val step = SimulationStep(gameInfo) + if (step.victoryType != null) + break + saveStat(gameInfo) + } + // check if Victory + var step = SimulationStep(gameInfo) + if (step.victoryType == null) { + gameInfo.simulateMaxTurns = maxTurns + gameInfo.nextTurn() + } - val step = SimulationStep(gameInfo) - println("First: ${gameInfo.civilizations.first().civName}") + step = SimulationStep(gameInfo) // final game state if (step.victoryType != null) { step.winner = step.currentPlayer @@ -94,6 +109,16 @@ class Simulation( println("Simulation step ($stepCounter/$maxSimulations)") } + @Synchronized + fun saveStat(gameInfo: GameInfo) { + val turn = gameInfo.turns + for (civ in gameInfo.civilizations.filter { it.civName != Constants.spectator }) { + val popsum = civ.cities.sumOf { it.population.population } + //println("$civ $popsum") + sumStat[civ.civName]!![turn]!!.add(popsum) + } + } + @Synchronized fun print(){ getStats() @@ -109,7 +134,7 @@ class Simulation( if (it.winner != null) { numWins[it.winner!!]!!.inc() winRateByVictory[it.winner!!]!![it.victoryType]!!.inc() - winTurnByVictory[it.winner!!]!![it.victoryType]!!.set(winTurnByVictory[it.winner!!]!![it.victoryType]!!.get() + it.turns) + winTurnByVictory[it.winner!!]!![it.victoryType]!!.add(it.turns) } } totalTurns = steps.sumOf { it.turns } @@ -151,6 +176,8 @@ class Simulation( outString += "$victory: $winsTurns " } outString += "avg turns\n" + for (turn in statTurns) + outString += "avgStat (@$turn): ${sumStat[civ]!![turn]!!.value.toFloat()/numSteps}\n" } outString += "\nAverage speed: %.1f turns/s \n".format(avgSpeed) outString += "Average game duration: $avgDuration\n"