mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-28 06:16:37 -04:00
Update Simulation.kt (#13843)
This commit is contained in:
parent
98de517d61
commit
57eb2b33f3
@ -6,6 +6,7 @@ import com.unciv.logic.GameInfo
|
|||||||
import com.unciv.logic.GameStarter
|
import com.unciv.logic.GameStarter
|
||||||
import com.unciv.models.metadata.GameSetupInfo
|
import com.unciv.models.metadata.GameSetupInfo
|
||||||
import kotlinx.coroutines.CoroutineName
|
import kotlinx.coroutines.CoroutineName
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
@ -63,52 +64,60 @@ class Simulation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun start() = runBlocking {
|
fun start() = runBlocking {
|
||||||
|
|
||||||
startTime = System.currentTimeMillis()
|
startTime = System.currentTimeMillis()
|
||||||
val jobs: ArrayList<Job> = ArrayList()
|
|
||||||
println("Starting new game with major civs: "+newGameInfo.civilizations.filter { it.isMajorCiv() }.joinToString { it.civName }
|
println(
|
||||||
+ " and minor civs: "+newGameInfo.civilizations.filter { it.isCityState }.joinToString { it.civName })
|
"Starting new game with major civs: " +
|
||||||
|
newGameInfo.civilizations.filter { it.isMajorCiv() }.joinToString { it.civName } +
|
||||||
|
" and minor civs: " +
|
||||||
|
newGameInfo.civilizations.filter { it.isCityState }.joinToString { it.civName }
|
||||||
|
)
|
||||||
|
|
||||||
newGameInfo.gameParameters.shufflePlayerOrder = true
|
newGameInfo.gameParameters.shufflePlayerOrder = true
|
||||||
for (threadId in 1..threadsNumber) {
|
|
||||||
jobs.add(launch(CoroutineName("simulation-${threadId}")) {
|
val jobs = (1..threadsNumber).map { threadId ->
|
||||||
|
launch(Dispatchers.Default + CoroutineName("simulation-$threadId")) {
|
||||||
repeat(simulationsPerThread) {
|
repeat(simulationsPerThread) {
|
||||||
val step = SimulationStep(newGameInfo, statTurns)
|
val step = SimulationStep(newGameInfo, statTurns)
|
||||||
val gameSetupInfo = GameSetupInfo(newGameInfo)
|
val gameSetupInfo = GameSetupInfo(newGameInfo)
|
||||||
gameSetupInfo.mapParameters.seed = 0 // Creates a new random map seed
|
gameSetupInfo.mapParameters.seed = 0
|
||||||
val gameInfo = GameStarter.startNewGame(gameSetupInfo)
|
val gameInfo = GameStarter.startNewGame(gameSetupInfo)
|
||||||
|
|
||||||
gameInfo.simulateUntilWin = true
|
gameInfo.simulateUntilWin = true
|
||||||
|
|
||||||
for (turn in statTurns) {
|
for (turn in statTurns) {
|
||||||
gameInfo.simulateMaxTurns = turn
|
gameInfo.simulateMaxTurns = turn
|
||||||
gameInfo.nextTurn()
|
gameInfo.nextTurn()
|
||||||
step.update(gameInfo)
|
step.update(gameInfo)
|
||||||
if (step.victoryType != null)
|
if (step.victoryType != null) break
|
||||||
break
|
|
||||||
step.saveTurnStats(gameInfo)
|
step.saveTurnStats(gameInfo)
|
||||||
}
|
}
|
||||||
// check if Victory
|
|
||||||
step.update(gameInfo)
|
step.update(gameInfo)
|
||||||
|
|
||||||
if (step.victoryType == null) {
|
if (step.victoryType == null) {
|
||||||
gameInfo.simulateMaxTurns = maxTurns
|
gameInfo.simulateMaxTurns = maxTurns
|
||||||
gameInfo.nextTurn()
|
gameInfo.nextTurn()
|
||||||
|
step.update(gameInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
step.update(gameInfo) // final game state
|
|
||||||
|
|
||||||
if (step.victoryType != null) {
|
if (step.victoryType != null) {
|
||||||
step.saveTurnStats(gameInfo)
|
step.saveTurnStats(gameInfo)
|
||||||
step.winner = step.currentPlayer
|
step.winner = step.currentPlayer
|
||||||
println("${step.winner} won ${step.victoryType} victory on turn ${step.turns}")
|
println("${step.winner} won ${step.victoryType} victory on turn ${step.turns}")
|
||||||
|
} else {
|
||||||
|
println("Max simulation ${step.turns} turns reached: Draw")
|
||||||
}
|
}
|
||||||
else println("Max simulation ${step.turns} turns reached: Draw")
|
|
||||||
|
|
||||||
|
// ⚠️ these need to be thread-safe
|
||||||
updateCounter(threadId)
|
updateCounter(threadId)
|
||||||
add(step)
|
add(step)
|
||||||
print()
|
print()
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
// wait for all to finish
|
|
||||||
for (job in jobs) job.join()
|
jobs.forEach { it.join() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNUSED_PARAMETER") // used when activating debug output
|
@Suppress("UNUSED_PARAMETER") // used when activating debug output
|
||||||
|
Loading…
x
Reference in New Issue
Block a user