mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-05 15:59:50 +07:00
Refactored and commented GameInfo.nextTurn() (#8296)
* Refactored and commented GameInfo.nextTurn() * Little nitpicks Co-authored-by: tunerzinc@gmail.com <vfylfhby>
This commit is contained in:
@ -10,7 +10,6 @@ import com.unciv.logic.BackwardCompatibility.migrateBarbarianCamps
|
||||
import com.unciv.logic.BackwardCompatibility.removeMissingModReferences
|
||||
import com.unciv.logic.GameInfo.Companion.CURRENT_COMPATIBILITY_NUMBER
|
||||
import com.unciv.logic.GameInfo.Companion.FIRST_WITHOUT
|
||||
import com.unciv.logic.automation.civilization.NextTurnAutomation
|
||||
import com.unciv.logic.city.CityInfo
|
||||
import com.unciv.logic.civilization.CivilizationInfo
|
||||
import com.unciv.logic.civilization.CivilizationInfoPreview
|
||||
@ -240,63 +239,79 @@ class GameInfo : IsPartOfGameInfoSerialization, HasGameInfoSerializationVersion
|
||||
//region State changing functions
|
||||
|
||||
fun nextTurn() {
|
||||
val previousHumanPlayer = getCurrentPlayerCivilization()
|
||||
var thisPlayer = previousHumanPlayer // not calling it currentPlayer because that's already taken and I can't think of a better name
|
||||
var currentPlayerIndex = civilizations.indexOf(thisPlayer)
|
||||
|
||||
var player = currentPlayerCiv
|
||||
var playerIndex = civilizations.indexOf(player)
|
||||
|
||||
fun endTurn() {
|
||||
thisPlayer.endTurn()
|
||||
currentPlayerIndex = (currentPlayerIndex + 1) % civilizations.size
|
||||
if (currentPlayerIndex == 0) {
|
||||
// We rotate Players in cycle: 1,2...N,1,2...
|
||||
fun setNextPlayer() {
|
||||
playerIndex = (playerIndex + 1) % civilizations.size
|
||||
if (playerIndex == 0) {
|
||||
turns++
|
||||
if (UncivGame.Current.simulateUntilTurnForDebug != 0)
|
||||
debug("Starting simulation of turn %s", turns)
|
||||
}
|
||||
thisPlayer = civilizations[currentPlayerIndex]
|
||||
player = civilizations[playerIndex]
|
||||
}
|
||||
|
||||
//check is important or else switchTurn
|
||||
//would skip a turn if an AI civ calls nextTurn
|
||||
//this happens when resigning a multiplayer game
|
||||
if (thisPlayer.isHuman()) {
|
||||
endTurn()
|
||||
|
||||
// Ending current player's turn
|
||||
// (Check is important or else switchTurn
|
||||
// would skip a turn if an AI civ calls nextTurn
|
||||
// this happens when resigning a multiplayer game)
|
||||
if (player.isHuman()) {
|
||||
player.endTurn()
|
||||
setNextPlayer()
|
||||
}
|
||||
|
||||
while (thisPlayer.playerType == PlayerType.AI
|
||||
|| turns < UncivGame.Current.simulateUntilTurnForDebug
|
||||
// Do we automatically simulate until N turn?
|
||||
val isSimulation = turns < UncivGame.Current.simulateUntilTurnForDebug
|
||||
|| turns < simulateMaxTurns && simulateUntilWin
|
||||
// For multiplayer, if there are 3+ players and one is defeated or spectator,
|
||||
// we'll want to skip over their turn
|
||||
|| gameParameters.isOnlineMultiplayer && (thisPlayer.isDefeated() || thisPlayer.isSpectator() && thisPlayer.playerId != UncivGame.Current.settings.multiplayer.userId)
|
||||
) {
|
||||
thisPlayer.startTurn()
|
||||
if (!thisPlayer.isDefeated() || thisPlayer.isBarbarian()) {
|
||||
NextTurnAutomation.automateCivMoves(thisPlayer)
|
||||
val isOnline = gameParameters.isOnlineMultiplayer
|
||||
|
||||
// Placing barbarians after their turn
|
||||
if (thisPlayer.isBarbarian() && !gameParameters.noBarbarians)
|
||||
barbarians.updateEncampments()
|
||||
// We process player automatically if:
|
||||
while (isSimulation || // simulation is active
|
||||
player.isAI() || // or player is AI
|
||||
isOnline && (player.isDefeated() || // or player is online defeated
|
||||
player.isSpectator())) // or player is online spectator
|
||||
{
|
||||
|
||||
// exit simulation mode when player wins
|
||||
if (thisPlayer.victoryManager.hasWon() && simulateUntilWin) {
|
||||
// stop simulation
|
||||
simulateUntilWin = false
|
||||
break
|
||||
}
|
||||
// Starting preparations
|
||||
player.startTurn()
|
||||
|
||||
// Automation done here
|
||||
player.doTurn()
|
||||
|
||||
// Do we need to break if player won?
|
||||
if (simulateUntilWin && player.victoryManager.hasWon()) {
|
||||
simulateUntilWin = false
|
||||
break
|
||||
}
|
||||
endTurn()
|
||||
|
||||
// Clean up
|
||||
player.endTurn()
|
||||
|
||||
// To the next player
|
||||
setNextPlayer()
|
||||
}
|
||||
|
||||
if (turns == UncivGame.Current.simulateUntilTurnForDebug)
|
||||
UncivGame.Current.simulateUntilTurnForDebug = 0
|
||||
|
||||
// We found human player, so we are making him current
|
||||
currentTurnStartTime = System.currentTimeMillis()
|
||||
currentPlayer = thisPlayer.civName
|
||||
currentPlayer = player.civName
|
||||
currentPlayerCiv = getCivilization(currentPlayer)
|
||||
thisPlayer.startTurn()
|
||||
if (currentPlayerCiv.isSpectator()) currentPlayerCiv.popupAlerts.clear() // no popups for spectators
|
||||
|
||||
if (turns % 10 == 0) //todo measuring actual play time might be nicer
|
||||
// Starting his turn
|
||||
player.startTurn()
|
||||
|
||||
// No popups for spectators
|
||||
if (currentPlayerCiv.isSpectator())
|
||||
currentPlayerCiv.popupAlerts.clear()
|
||||
|
||||
// Play some nice music TODO: measuring actual play time might be nicer
|
||||
if (turns % 10 == 0)
|
||||
UncivGame.Current.musicController.chooseTrack(
|
||||
currentPlayerCiv.civName,
|
||||
MusicMood.peaceOrWar(currentPlayerCiv.isAtWar()), MusicTrackChooserFlags.setNextTurn
|
||||
@ -304,7 +319,7 @@ class GameInfo : IsPartOfGameInfoSerialization, HasGameInfoSerializationVersion
|
||||
|
||||
// Start our turn immediately before the player can make decisions - affects
|
||||
// whether our units can commit automated actions and then be attacked immediately etc.
|
||||
notifyOfCloseEnemyUnits(thisPlayer)
|
||||
notifyOfCloseEnemyUnits(player)
|
||||
}
|
||||
|
||||
private fun notifyOfCloseEnemyUnits(thisPlayer: CivilizationInfo) {
|
||||
|
@ -348,6 +348,7 @@ class CivilizationInfo : IsPartOfGameInfoSerialization {
|
||||
)
|
||||
fun getCapital() = cities.firstOrNull { it.isCapital() }
|
||||
fun isHuman() = playerType == PlayerType.Human
|
||||
fun isAI() = playerType == PlayerType.AI
|
||||
fun isOneCityChallenger() = (
|
||||
playerType == PlayerType.Human &&
|
||||
gameInfo.gameParameters.oneCityChallenge)
|
||||
@ -910,6 +911,20 @@ class CivilizationInfo : IsPartOfGameInfoSerialization {
|
||||
fun updateViewableTiles() = transients().updateViewableTiles()
|
||||
fun updateDetailedCivResources() = transients().updateCivResources()
|
||||
|
||||
fun doTurn() {
|
||||
|
||||
// Defeated civs do nothing
|
||||
if (isDefeated())
|
||||
return
|
||||
|
||||
// Do stuff
|
||||
NextTurnAutomation.automateCivMoves(this)
|
||||
|
||||
// Update barbarian camps
|
||||
if (isBarbarian() && !gameInfo.gameParameters.noBarbarians)
|
||||
gameInfo.barbarians.updateEncampments()
|
||||
}
|
||||
|
||||
fun startTurn() {
|
||||
civConstructions.startTurn()
|
||||
attacksSinceTurnStart.clear()
|
||||
|
Reference in New Issue
Block a user