From a27aecda0968c59d484f77a91c7f16a452ff22aa Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 13 Aug 2020 17:06:37 +0300 Subject: [PATCH] The new age of scenarios is upon us! Can save a game and have it get turned instantly into a scenario! --- core/src/com/unciv/logic/GameInfo.kt | 2 +- .../unciv/logic/civilization/TechManager.kt | 5 +-- core/src/com/unciv/logic/map/MapParameters.kt | 2 + .../unciv/ui/newgamescreen/MapOptionsTable.kt | 38 ++++++++++++++++++- .../unciv/ui/newgamescreen/NewGameScreen.kt | 18 ++++++++- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index 93eb1c82bc..7ad364d8ec 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -74,7 +74,7 @@ class GameInfo { fun nextTurn() { 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 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) diff --git a/core/src/com/unciv/logic/civilization/TechManager.kt b/core/src/com/unciv/logic/civilization/TechManager.kt index 3bcd31a8e8..8a7639bbab 100644 --- a/core/src/com/unciv/logic/civilization/TechManager.kt +++ b/core/src/com/unciv/logic/civilization/TechManager.kt @@ -3,7 +3,6 @@ package com.unciv.logic.civilization import com.badlogic.gdx.graphics.Color import com.unciv.Constants -import com.unciv.UncivGame import com.unciv.logic.map.MapSize import com.unciv.logic.map.RoadStatus import com.unciv.models.ruleset.tech.Technology @@ -62,7 +61,7 @@ class TechManager { techCost *= civInfo.getDifficulty().researchCostModifier techCost *= civInfo.gameInfo.gameParameters.gameSpeed.modifier val techsResearchedKnownCivs = civInfo.getKnownCivs().count { it.isMajorCiv() && it.tech.isResearched(techName) } - val undefeatedCivs = UncivGame.Current.gameInfo.civilizations.count { it.isMajorCiv() && !it.isDefeated() } + val undefeatedCivs = civInfo.gameInfo.civilizations.count { it.isMajorCiv() && !it.isDefeated() } // https://forums.civfanatics.com/threads/the-mechanics-of-overflow-inflation.517970/ techCost /= 1 + techsResearchedKnownCivs / undefeatedCivs.toFloat() * 0.3f // http://www.civclub.net/bbs/forum.php?mod=viewthread&tid=123976 @@ -186,7 +185,7 @@ class TechManager { if (overflowScience != 0) { // https://forums.civfanatics.com/threads/the-mechanics-of-overflow-inflation.517970/ val techsResearchedKnownCivs = civInfo.getKnownCivs() .count { it.isMajorCiv() && it.tech.isResearched(currentTechnologyName()!!) } - val undefeatedCivs = UncivGame.Current.gameInfo.civilizations.count { it.isMajorCiv() && !it.isDefeated() } + val undefeatedCivs = civInfo.gameInfo.civilizations.count { it.isMajorCiv() && !it.isDefeated() } techsInProgress[currentTechnology] = techsInProgress[currentTechnology]!! + ((1 + techsResearchedKnownCivs / undefeatedCivs.toFloat() * 0.3f) * overflowScience).toInt() overflowScience = 0 } diff --git a/core/src/com/unciv/logic/map/MapParameters.kt b/core/src/com/unciv/logic/map/MapParameters.kt index 43b3aea11d..8e1dcad214 100644 --- a/core/src/com/unciv/logic/map/MapParameters.kt +++ b/core/src/com/unciv/logic/map/MapParameters.kt @@ -28,6 +28,8 @@ object MapType { // Loaded scenario const val scenario = "Scenario" + const val scenarioFromSavedGame = "ScenarioFromSavedGame" + // All ocean tiles const val empty = "Empty" } diff --git a/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt b/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt index 3c9ad7f949..73c27f4e46 100644 --- a/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt @@ -4,6 +4,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Array import com.unciv.UncivGame +import com.unciv.logic.GameInfo +import com.unciv.logic.GameSaver import com.unciv.logic.MapSaver import com.unciv.logic.map.MapType import com.unciv.ui.utils.CameraStageBaseScreen @@ -17,6 +19,9 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { private val generatedMapOptionsTable = MapParametersTable(mapParameters) private val savedMapOptionsTable = Table() private val savedScenarioOptionsTable = Table() + private val scenarioFromSavedGameOptionsTable = Table() + var selectedScenarioSaveGame: GameInfo? = null + lateinit var mapTypeSelectBox: TranslatedSelectBox init { defaults().pad(5f) @@ -24,6 +29,15 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { addMapTypeSelection() } + fun selectSavedGameAsScenario(gameName: String){ + val savedGame = GameSaver.loadGameByName(gameName) + mapParameters.type = MapType.scenarioFromSavedGame + mapParameters.name = gameName + newGameScreen.updateTables() + newGameScreen.gameSetupInfo.gameParameters = savedGame.gameParameters + newGameScreen.gameSetupInfo.mapParameters = savedGame.tileMap.mapParameters + selectedScenarioSaveGame = savedGame + } private fun addMapTypeSelection() { add("{Map Type}:".toLabel()) @@ -31,7 +45,9 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { if (MapSaver.getMaps().isNotEmpty()) mapTypes.add(MapType.custom) if (MapSaver.getScenarios().isNotEmpty() && UncivGame.Current.settings.extendedMapEditor) mapTypes.add(MapType.scenario) - val mapTypeSelectBox = TranslatedSelectBox(mapTypes, "Generated", CameraStageBaseScreen.skin) + if (UncivGame.Current.settings.extendedMapEditor && GameSaver.getSaves().any { it.endsWith("Scenario") }) + mapTypes.add(MapType.scenarioFromSavedGame) + mapTypeSelectBox = TranslatedSelectBox(mapTypes, "Generated", CameraStageBaseScreen.skin) val mapFileSelectBox = getMapFileSelectBox() savedMapOptionsTable.defaults().pad(5f) @@ -40,6 +56,7 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { savedMapOptionsTable.add(mapFileSelectBox).maxWidth(newGameScreen.stage.width / 2) .right().row() + val scenarioFileSelectBox = getScenarioFileSelectBox() savedScenarioOptionsTable.defaults().pad(5f) savedScenarioOptionsTable.add("{Scenario file}:".toLabel()).left() @@ -47,6 +64,17 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { savedScenarioOptionsTable.add(scenarioFileSelectBox).maxWidth(newGameScreen.stage.width / 2) .right().row() + + val scenarioFromSavedGameSelectBox = SelectBox(CameraStageBaseScreen.skin) + for (savedGame in GameSaver.getSaves()) { + if (savedGame.endsWith("Scenario")) + scenarioFromSavedGameSelectBox.items.add(savedGame) + } + scenarioFromSavedGameSelectBox.onChange { selectSavedGameAsScenario(scenarioFromSavedGameSelectBox.selected) } + scenarioFromSavedGameSelectBox.selected = scenarioFileSelectBox.items.first() + scenarioFromSavedGameOptionsTable.add("{Scenario file}:".toLabel()).left() + scenarioFromSavedGameOptionsTable.add(scenarioFromSavedGameSelectBox) + fun updateOnMapTypeChange() { mapTypeSpecificTable.clear() if (mapTypeSelectBox.selected.value == MapType.custom) { @@ -66,7 +94,13 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { // update PlayerTable and GameOptionsTable newGameScreen.lockTables() newGameScreen.updateTables() - } else { + } else if(mapTypeSelectBox.selected.value == MapType.scenarioFromSavedGame){ + selectSavedGameAsScenario(scenarioFromSavedGameSelectBox.selected) + mapTypeSpecificTable.add(scenarioFromSavedGameOptionsTable) + newGameScreen.updateRuleset() + newGameScreen.lockTables() + newGameScreen.updateTables() + } else { // generated map mapParameters.name = "" mapParameters.type = generatedMapOptionsTable.mapTypeSelectBox.selected.value mapTypeSpecificTable.add(generatedMapOptionsTable) diff --git a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt index 354fdcb857..4b5f6dc685 100644 --- a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt +++ b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt @@ -9,6 +9,7 @@ import com.unciv.UncivGame import com.unciv.logic.* import com.unciv.logic.civilization.PlayerType import com.unciv.logic.map.MapParameters +import com.unciv.logic.map.MapType import com.unciv.models.metadata.GameParameters import com.unciv.models.ruleset.Ruleset import com.unciv.models.ruleset.RulesetCache @@ -98,7 +99,19 @@ class NewGameScreen(previousScreen:CameraStageBaseScreen, _gameSetupInfo: GameSe private fun newGameThread() { try { - newGame = GameStarter.startNewGame(gameSetupInfo) + if (mapOptionsTable.mapTypeSelectBox.selected.value == MapType.scenarioFromSavedGame) { + newGame = mapOptionsTable.selectedScenarioSaveGame + // to take the definition of which players are human and which are AI + for (player in gameSetupInfo.gameParameters.players) { + newGame!!.getCivilization(player.chosenCiv).playerType = player.playerType + } + if (newGame!!.getCurrentPlayerCivilization().playerType == PlayerType.AI) { + newGame!!.setTransients() + newGame!!.nextTurn() // can't start the game on an AI turn + } + newGame!!.gameParameters.godMode = false + } + else newGame = GameStarter.startNewGame(gameSetupInfo) } catch (exception: Exception) { Gdx.app.postRunnable { val cantMakeThatMapPopup = Popup(this) @@ -163,7 +176,8 @@ class NewGameScreen(previousScreen:CameraStageBaseScreen, _gameSetupInfo: GameSe var newGame: GameInfo? = null override fun render(delta: Float) { - if (newGame != null) game.loadGame(newGame!!) + if (newGame != null) + game.loadGame(newGame!!) super.render(delta) } }