From 401aa942ebf68ca6fc31d08bfa4b163cfa13d6fa Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 11 Jan 2019 09:58:32 +0200 Subject: [PATCH] Can now play with multiple human players! Need to find a way for all human players to choose their civs in a simple manner --- core/src/com/unciv/GameStarter.kt | 13 +++-- core/src/com/unciv/logic/GameInfo.kt | 54 ++++++++++--------- .../com/unciv/ui/worldscreen/WorldScreen.kt | 7 +++ 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/core/src/com/unciv/GameStarter.kt b/core/src/com/unciv/GameStarter.kt index fbaa3f3ab6..327c54f489 100644 --- a/core/src/com/unciv/GameStarter.kt +++ b/core/src/com/unciv/GameStarter.kt @@ -31,20 +31,23 @@ class GameStarter{ val startingLocations = getStartingLocations( newGameParameters.numberOfEnemies+newGameParameters.humanPlayers, gameInfo.tileMap) + val availableCivNames = Stack() + availableCivNames.addAll(GameBasics.Nations.keys.shuffled()) + availableCivNames.remove(newGameParameters.nation) for(i in 1..newGameParameters.humanPlayers) { - val playerCiv = CivilizationInfo(newGameParameters.nation) + val playerCiv = + if(i==1) CivilizationInfo(newGameParameters.nation) + else CivilizationInfo(availableCivNames.pop()) gameInfo.difficulty = newGameParameters.difficulty playerCiv.playerType = PlayerType.Human gameInfo.civilizations.add(playerCiv) } - val barbarianCivilization = CivilizationInfo() - barbarianCivilization.civName = "Barbarians" + val barbarianCivilization = CivilizationInfo("Barbarians") gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ - for (nationName in GameBasics.Nations.keys.filterNot { it=="Barbarians" || it==newGameParameters.nation }.shuffled() - .take(newGameParameters.numberOfEnemies)) { + for (nationName in availableCivNames.take(newGameParameters.numberOfEnemies)) { val civ = CivilizationInfo(nationName) gameInfo.civilizations.add(civ) } diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index f0f160046b..0a55b340d3 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -37,46 +37,52 @@ class GameInfo { //endregion fun nextTurn() { - val currentPlayer = getCurrentPlayerCivilization() + val previousHumanPlayer = getCurrentPlayerCivilization() + var thisPlayer = previousHumanPlayer // not calling is currentPlayer because that's alreay taken and I can't think of a better name + var currentPlayerIndex = civilizations.indexOf(thisPlayer) - for (civInfo in civilizations) { - if (civInfo.tech.techsToResearch.isEmpty()) { // should belong in automation? yes/no? - val researchableTechs = GameBasics.Technologies.values - .filter { !civInfo.tech.isResearched(it.name) && civInfo.tech.canBeResearched(it.name) } - civInfo.tech.techsToResearch.add(researchableTechs.minBy { it.cost }!!.name) + fun switchTurn(){ + thisPlayer.endTurn() + currentPlayerIndex = (currentPlayerIndex+1) % civilizations.size + if(currentPlayerIndex==0){ + turns++ + if (turns % 10 == 0) { // every 10 turns add a barbarian in a random place + placeBarbarianUnit(null) + } } - civInfo.endTurn() + thisPlayer = civilizations[currentPlayerIndex] + thisPlayer.startTurn() } - // We need to update the stats after ALL the cities are done updating because - // maybe one of them has a wonder that affects the stats of all the rest of the cities + switchTurn() - for (civInfo in civilizations.filterNot { it == currentPlayer || (it.isDefeated() && !it.isBarbarianCivilization()) }) { - civInfo.startTurn() - NextTurnAutomation().automateCivMoves(civInfo) + while(thisPlayer.playerType==PlayerType.AI){ + NextTurnAutomation().automateCivMoves(thisPlayer) + if (thisPlayer.tech.techsToResearch.isEmpty()) { // should belong in automation? yes/no? + val researchableTechs = GameBasics.Technologies.values + .filter { !thisPlayer.tech.isResearched(it.name) && thisPlayer.tech.canBeResearched(it.name) } + thisPlayer.tech.techsToResearch.add(researchableTechs.minBy { it.cost }!!.name) + } + switchTurn() } - if (turns % 10 == 0) { // every 10 turns add a barbarian in a random place - placeBarbarianUnit(null) - } + currentPlayer=thisPlayer.civName // Start our turn immediately before the player can made decisions - affects whether our units can commit automated actions and then be attacked immediately etc. - currentPlayer.startTurn() - - val enemyUnitsCloseToTerritory = currentPlayer.viewableTiles + val enemyUnitsCloseToTerritory = thisPlayer.viewableTiles .filter { - it.militaryUnit != null && it.militaryUnit!!.civInfo != currentPlayer - && currentPlayer.isAtWarWith(it.militaryUnit!!.civInfo) - && (it.getOwner() == currentPlayer || it.neighbors.any { neighbor -> neighbor.getOwner() == currentPlayer }) + it.militaryUnit != null && it.militaryUnit!!.civInfo != thisPlayer + && thisPlayer.isAtWarWith(it.militaryUnit!!.civInfo) + && (it.getOwner() == thisPlayer || it.neighbors.any { neighbor -> neighbor.getOwner() == thisPlayer }) } + for (enemyUnitTile in enemyUnitsCloseToTerritory) { - val inOrNear = if (enemyUnitTile.getOwner() == currentPlayer) "in" else "near" + val inOrNear = if (enemyUnitTile.getOwner() == thisPlayer) "in" else "near" val unitName = enemyUnitTile.militaryUnit!!.name - currentPlayer.addNotification("An enemy [$unitName] was spotted $inOrNear our territory", enemyUnitTile.position, Color.RED) + thisPlayer.addNotification("An enemy [$unitName] was spotted $inOrNear our territory", enemyUnitTile.position, Color.RED) } - turns++ } fun placeBarbarianUnit(tileToPlace: TileInfo?) { diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 65bb4373dd..98879ce654 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -264,6 +264,13 @@ class WorldScreen : CameraStageBaseScreen() { var shouldUpdate=false override fun render(delta: Float) { if(shouldUpdate){ // This is so that updates happen in the MAIN THREAD, where there is a GL Context, + if(currentPlayerCiv!=gameInfo.getCurrentPlayerCivilization()){ + UnCivGame.Current.worldScreen= WorldScreen().apply { + shouldUpdate=true + } + UnCivGame.Current.setWorldScreen() + } + // otherwise images will not load properly! update()