From b9327b76192a73ccd11a1f9c1aa34c592cb23a4e Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 15 Sep 2024 17:29:13 +0300 Subject: [PATCH] Allowed starting Scenarios through the new game screen! :D --- core/src/com/unciv/logic/map/MapParameters.kt | 1 + .../screens/newgamescreen/MapOptionsTable.kt | 55 +++++++++++++++++++ .../ui/screens/newgamescreen/NewGameScreen.kt | 13 ++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/core/src/com/unciv/logic/map/MapParameters.kt b/core/src/com/unciv/logic/map/MapParameters.kt index cb2bce6de0..aa0360ffce 100644 --- a/core/src/com/unciv/logic/map/MapParameters.kt +++ b/core/src/com/unciv/logic/map/MapParameters.kt @@ -18,6 +18,7 @@ object MapGeneratedMainType { const val randomGenerated = "Random Generated" // Non-generated maps const val custom = "Custom" + const val scenario = "Scenario" } diff --git a/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt b/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt index 14845e7cba..a0a07313ec 100644 --- a/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt +++ b/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt @@ -1,12 +1,55 @@ package com.unciv.ui.screens.newgamescreen +import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.unciv.Constants +import com.unciv.logic.GameInfoPreview import com.unciv.logic.map.MapGeneratedMainType +import com.unciv.logic.map.MapParameters import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.components.input.onChange import com.unciv.ui.components.widgets.TranslatedSelectBox import com.unciv.ui.screens.basescreen.BaseScreen +class ScenarioSelectTable(newGameScreen: NewGameScreen, mapParameters: MapParameters) : Table() { + + data class ScenarioData(val name:String, val file: FileHandle){ + var preview: GameInfoPreview? = null + } + + val scenarios = HashMap() + lateinit var selectedScenario: ScenarioData + + init { + // TODO make this async so it's not slow + val scenarioFiles = newGameScreen.game.files.getScenarioFiles() + for ((file, ruleset) in scenarioFiles) + scenarios[file.name()] = ScenarioData(file.name(), file) + + val scenarioSelectBox = TranslatedSelectBox(scenarios.keys, scenarios.keys.first()) + + fun selectScenario(){ + val scenario = scenarios[scenarioSelectBox.selected.value]!! + val preload = if (scenario.preview != null) scenario.preview!! else { + val preview = newGameScreen.game.files.loadGamePreviewFromFile(scenario.file) + scenario.preview = preview + preview + } + newGameScreen.gameSetupInfo.gameParameters.players = preload.gameParameters.players + .apply { removeAll { it.chosenCiv == Constants.spectator } } + newGameScreen.gameSetupInfo.gameParameters.baseRuleset = preload.gameParameters.baseRuleset + newGameScreen.gameSetupInfo.gameParameters.mods = preload.gameParameters.mods + newGameScreen.tryUpdateRuleset(true) + newGameScreen.playerPickerTable.update() + selectedScenario = scenario + } + + scenarioSelectBox.onChange { selectScenario() } + add(scenarioSelectBox) + selectScenario() + } +} + class MapOptionsTable(private val newGameScreen: NewGameScreen) : Table() { private val mapParameters = newGameScreen.gameSetupInfo.mapParameters @@ -14,6 +57,7 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen) : Table() { internal val generatedMapOptionsTable = MapParametersTable(newGameScreen, mapParameters, MapGeneratedMainType.generated) private val randomMapOptionsTable = MapParametersTable(newGameScreen, mapParameters, MapGeneratedMainType.randomGenerated) private val savedMapOptionsTable = MapFileSelectTable(newGameScreen, mapParameters) + private val scenarioOptionsTable = ScenarioSelectTable(newGameScreen, mapParameters) internal val mapTypeSelectBox: TranslatedSelectBox init { @@ -23,6 +67,7 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen) : Table() { val mapTypes = arrayListOf(MapGeneratedMainType.generated, MapGeneratedMainType.randomGenerated) if (savedMapOptionsTable.isNotEmpty()) mapTypes.add(MapGeneratedMainType.custom) + if (newGameScreen.game.files.getScenarioFiles().any()) mapTypes.add(MapGeneratedMainType.scenario) mapTypeSelectBox = TranslatedSelectBox(mapTypes, MapGeneratedMainType.generated) @@ -46,6 +91,11 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen) : Table() { mapTypeSpecificTable.add(randomMapOptionsTable) newGameScreen.unlockTables() } + MapGeneratedMainType.scenario -> { + mapParameters.name = "" + mapTypeSpecificTable.add(scenarioOptionsTable) + newGameScreen.lockTables() + } } newGameScreen.gameSetupInfo.gameParameters.godMode = false newGameScreen.updateTables() @@ -62,6 +112,11 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen) : Table() { add(mapTypeSelectWrapper).pad(10f).fillX().row() add(mapTypeSpecificTable).row() } + + fun getSelectedScenario(): ScenarioSelectTable.ScenarioData? { + if (mapTypeSelectBox.selected.value != MapGeneratedMainType.scenario) return null + return scenarioOptionsTable.selectedScenario + } internal fun cancelBackgroundJobs() = savedMapOptionsTable.cancelBackgroundJobs() } diff --git a/core/src/com/unciv/ui/screens/newgamescreen/NewGameScreen.kt b/core/src/com/unciv/ui/screens/newgamescreen/NewGameScreen.kt index 2cf6c53d34..2aa3e8fdd7 100644 --- a/core/src/com/unciv/ui/screens/newgamescreen/NewGameScreen.kt +++ b/core/src/com/unciv/ui/screens/newgamescreen/NewGameScreen.kt @@ -299,7 +299,18 @@ class NewGameScreen( val newGame:GameInfo try { - newGame = GameStarter.startNewGame(gameSetupInfo) + val selectedScenario = mapOptionsTable.getSelectedScenario() + newGame = if (selectedScenario == null) + GameStarter.startNewGame(gameSetupInfo) + else { + val gameInfo = game.files.loadGameFromFile(selectedScenario.file) + // Instead of removing spectator we AI-ify it, so we don't get problems in e.g. diplomacy + gameInfo.civilizations.firstOrNull { it.civName == Constants.spectator }?.playerType = PlayerType.AI + for (playerInfo in gameSetupInfo.gameParameters.players){ + gameInfo.civilizations.firstOrNull { it.civName == playerInfo.chosenCiv }?.playerType = playerInfo.playerType + } + gameInfo + } } catch (exception: Exception) { exception.printStackTrace() launchOnGLThread {