mirror of
https://github.com/yairm210/Unciv.git
synced 2024-12-22 17:24:24 +07:00
Simulation: Save Stat support (#12663)
* Changes * Default to not using avgStat * Make print a float * Make statTurns a list rename sumStat better summation for popsum * Fix potential nullpointer * Add .add() * And another place to use .add()
This commit is contained in:
parent
7918123a79
commit
2963d47295
@ -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()
|
||||
|
@ -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<Int> = 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<SimulationStep>()
|
||||
var numWins = mutableMapOf<String, MutableInt>()
|
||||
var sumStat = mutableMapOf<String, MutableMap<Int, MutableInt>>()
|
||||
private var winRateByVictory = HashMap<String, MutableMap<String, MutableInt>>()
|
||||
private var winTurnByVictory = HashMap<String, MutableMap<String, MutableInt>>()
|
||||
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"
|
||||
|
Loading…
Reference in New Issue
Block a user