mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-07 00:41:39 +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.BackwardCompatibility.removeMissingModReferences
|
||||||
import com.unciv.logic.GameInfo.Companion.CURRENT_COMPATIBILITY_NUMBER
|
import com.unciv.logic.GameInfo.Companion.CURRENT_COMPATIBILITY_NUMBER
|
||||||
import com.unciv.logic.GameInfo.Companion.FIRST_WITHOUT
|
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.city.CityInfo
|
||||||
import com.unciv.logic.civilization.CivilizationInfo
|
import com.unciv.logic.civilization.CivilizationInfo
|
||||||
import com.unciv.logic.civilization.CivilizationInfoPreview
|
import com.unciv.logic.civilization.CivilizationInfoPreview
|
||||||
@ -240,63 +239,79 @@ class GameInfo : IsPartOfGameInfoSerialization, HasGameInfoSerializationVersion
|
|||||||
//region State changing functions
|
//region State changing functions
|
||||||
|
|
||||||
fun nextTurn() {
|
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() {
|
// We rotate Players in cycle: 1,2...N,1,2...
|
||||||
thisPlayer.endTurn()
|
fun setNextPlayer() {
|
||||||
currentPlayerIndex = (currentPlayerIndex + 1) % civilizations.size
|
playerIndex = (playerIndex + 1) % civilizations.size
|
||||||
if (currentPlayerIndex == 0) {
|
if (playerIndex == 0) {
|
||||||
turns++
|
turns++
|
||||||
if (UncivGame.Current.simulateUntilTurnForDebug != 0)
|
if (UncivGame.Current.simulateUntilTurnForDebug != 0)
|
||||||
debug("Starting simulation of turn %s", turns)
|
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
|
// Ending current player's turn
|
||||||
//this happens when resigning a multiplayer game
|
// (Check is important or else switchTurn
|
||||||
if (thisPlayer.isHuman()) {
|
// would skip a turn if an AI civ calls nextTurn
|
||||||
endTurn()
|
// this happens when resigning a multiplayer game)
|
||||||
|
if (player.isHuman()) {
|
||||||
|
player.endTurn()
|
||||||
|
setNextPlayer()
|
||||||
}
|
}
|
||||||
|
|
||||||
while (thisPlayer.playerType == PlayerType.AI
|
// Do we automatically simulate until N turn?
|
||||||
|| turns < UncivGame.Current.simulateUntilTurnForDebug
|
val isSimulation = turns < UncivGame.Current.simulateUntilTurnForDebug
|
||||||
|| turns < simulateMaxTurns && simulateUntilWin
|
|| turns < simulateMaxTurns && simulateUntilWin
|
||||||
// For multiplayer, if there are 3+ players and one is defeated or spectator,
|
val isOnline = gameParameters.isOnlineMultiplayer
|
||||||
// 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)
|
|
||||||
|
|
||||||
// Placing barbarians after their turn
|
// We process player automatically if:
|
||||||
if (thisPlayer.isBarbarian() && !gameParameters.noBarbarians)
|
while (isSimulation || // simulation is active
|
||||||
barbarians.updateEncampments()
|
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
|
// Starting preparations
|
||||||
if (thisPlayer.victoryManager.hasWon() && simulateUntilWin) {
|
player.startTurn()
|
||||||
// stop simulation
|
|
||||||
simulateUntilWin = false
|
// Automation done here
|
||||||
break
|
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)
|
if (turns == UncivGame.Current.simulateUntilTurnForDebug)
|
||||||
UncivGame.Current.simulateUntilTurnForDebug = 0
|
UncivGame.Current.simulateUntilTurnForDebug = 0
|
||||||
|
|
||||||
|
// We found human player, so we are making him current
|
||||||
currentTurnStartTime = System.currentTimeMillis()
|
currentTurnStartTime = System.currentTimeMillis()
|
||||||
currentPlayer = thisPlayer.civName
|
currentPlayer = player.civName
|
||||||
currentPlayerCiv = getCivilization(currentPlayer)
|
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(
|
UncivGame.Current.musicController.chooseTrack(
|
||||||
currentPlayerCiv.civName,
|
currentPlayerCiv.civName,
|
||||||
MusicMood.peaceOrWar(currentPlayerCiv.isAtWar()), MusicTrackChooserFlags.setNextTurn
|
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
|
// Start our turn immediately before the player can make decisions - affects
|
||||||
// whether our units can commit automated actions and then be attacked immediately etc.
|
// whether our units can commit automated actions and then be attacked immediately etc.
|
||||||
notifyOfCloseEnemyUnits(thisPlayer)
|
notifyOfCloseEnemyUnits(player)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notifyOfCloseEnemyUnits(thisPlayer: CivilizationInfo) {
|
private fun notifyOfCloseEnemyUnits(thisPlayer: CivilizationInfo) {
|
||||||
|
@ -348,6 +348,7 @@ class CivilizationInfo : IsPartOfGameInfoSerialization {
|
|||||||
)
|
)
|
||||||
fun getCapital() = cities.firstOrNull { it.isCapital() }
|
fun getCapital() = cities.firstOrNull { it.isCapital() }
|
||||||
fun isHuman() = playerType == PlayerType.Human
|
fun isHuman() = playerType == PlayerType.Human
|
||||||
|
fun isAI() = playerType == PlayerType.AI
|
||||||
fun isOneCityChallenger() = (
|
fun isOneCityChallenger() = (
|
||||||
playerType == PlayerType.Human &&
|
playerType == PlayerType.Human &&
|
||||||
gameInfo.gameParameters.oneCityChallenge)
|
gameInfo.gameParameters.oneCityChallenge)
|
||||||
@ -910,6 +911,20 @@ class CivilizationInfo : IsPartOfGameInfoSerialization {
|
|||||||
fun updateViewableTiles() = transients().updateViewableTiles()
|
fun updateViewableTiles() = transients().updateViewableTiles()
|
||||||
fun updateDetailedCivResources() = transients().updateCivResources()
|
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() {
|
fun startTurn() {
|
||||||
civConstructions.startTurn()
|
civConstructions.startTurn()
|
||||||
attacksSinceTurnStart.clear()
|
attacksSinceTurnStart.clear()
|
||||||
|
Reference in New Issue
Block a user