diff --git a/core/src/com/unciv/GameStarter.kt b/core/src/com/unciv/GameStarter.kt new file mode 100644 index 0000000000..6a33065b76 --- /dev/null +++ b/core/src/com/unciv/GameStarter.kt @@ -0,0 +1,37 @@ +package com.unciv + +import com.badlogic.gdx.math.Vector2 +import com.unciv.logic.GameInfo +import com.unciv.logic.civilization.CivilizationInfo +import com.unciv.logic.map.TileMap +import com.unciv.models.gamebasics.GameBasics +import com.unciv.ui.utils.getRandom + +class GameStarter(){ + fun startNewGame(mapRadius: Int, numberOfCivs: Int, civilization: String): GameInfo { + val gameInfo = GameInfo() + + gameInfo.tileMap = TileMap(mapRadius) + gameInfo.civilizations.add(CivilizationInfo(civilization, Vector2.Zero, gameInfo)) // first one is player civ + + val freeTiles = gameInfo.tileMap.values.toMutableList() + freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(Vector2.Zero,6)) + + val barbarianCivilization = CivilizationInfo() + gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ + + for (civname in GameBasics.Civilizations.keys.filterNot { it=="Barbarians" || it==civilization }.take(numberOfCivs)) { + val startingLocation = freeTiles.toList().getRandom().position + gameInfo.civilizations.add(CivilizationInfo(civname, startingLocation, gameInfo)) // all the rest whatever + freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(startingLocation, 6)) + } + + barbarianCivilization.civName = "Barbarians" + + gameInfo.setTransients() // needs to be before placeBarbarianUnit because it depends on the tilemap having its gameinfo set + + (1..5).forEach { gameInfo.placeBarbarianUnit(freeTiles.toList().getRandom()) } + + return gameInfo + } +} \ No newline at end of file diff --git a/core/src/com/unciv/UnCivGame.kt b/core/src/com/unciv/UnCivGame.kt index 76a269403e..f4e53f89b5 100644 --- a/core/src/com/unciv/UnCivGame.kt +++ b/core/src/com/unciv/UnCivGame.kt @@ -2,18 +2,11 @@ package com.unciv import com.badlogic.gdx.Game import com.badlogic.gdx.Gdx -import com.badlogic.gdx.math.Vector2 -import com.badlogic.gdx.utils.Json import com.unciv.logic.GameInfo import com.unciv.logic.GameSaver -import com.unciv.logic.civilization.CivilizationInfo -import com.unciv.logic.map.TileMap -import com.unciv.models.gamebasics.* -import com.unciv.models.gamebasics.Unit -import com.unciv.models.stats.INamed +import com.unciv.models.gamebasics.GameBasics import com.unciv.ui.GameSettings import com.unciv.ui.NewGameScreen -import com.unciv.ui.utils.getRandom import com.unciv.ui.worldscreen.WorldScreen class UnCivGame : Game() { @@ -23,8 +16,7 @@ class UnCivGame : Game() { var worldScreen: WorldScreen? = null override fun create() { - setupGameBasics() - + GameBasics.run { } // just to initialize Current = this if (GameSaver().getSave("Autosave").exists()) { try { @@ -43,34 +35,11 @@ class UnCivGame : Game() { } fun startNewGame(saveTutorialState:Boolean = false) { + val newGame = GameStarter().startNewGame(20, 3, "Babylon") if(saveTutorialState) { - val tutorials = gameInfo.tutorial - gameInfo = GameInfo() - gameInfo.tutorial = tutorials + newGame.tutorial = gameInfo.tutorial } - else gameInfo = GameInfo() - - gameInfo.tileMap = TileMap(20) - gameInfo.civilizations.add(CivilizationInfo("Babylon", Vector2.Zero, gameInfo)) // first one is player civ - - val freeTiles = gameInfo.tileMap.values.toMutableList() - freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(Vector2.Zero,6)) - - val barbarianCivilization = CivilizationInfo() - gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ - - for (civname in listOf("Greece","China","Egypt")) { - val startingLocation = freeTiles.toList().getRandom().position - gameInfo.civilizations.add(CivilizationInfo(civname, startingLocation, gameInfo)) // all the rest whatever - freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(startingLocation, 6)) - } - - barbarianCivilization.civName = "Barbarians" - - gameInfo.setTransients() // needs to be before placeBarbarianUnit because it depends on the tilemap having its gameinfo set - - (1..5).forEach { gameInfo.placeBarbarianUnit(freeTiles.toList().getRandom()) } - + gameInfo = newGame worldScreen = WorldScreen() setWorldScreen() @@ -83,59 +52,6 @@ class UnCivGame : Game() { Gdx.input.inputProcessor = worldScreen!!.stage } - private fun getFromJson(tClass: Class, name: String): T { - val jsonText = Gdx.files.internal("jsons/$name.json").readString() - return Json().fromJson(tClass, jsonText) - } - - private fun createHashmap(items: Array): LinkedHashMap { - val hashMap = LinkedHashMap() - for (item in items) - hashMap[item.name] = item - return hashMap - } - - private fun setupGameBasics() { - GameBasics.Buildings += createHashmap(getFromJson(Array::class.java, "Buildings")) - GameBasics.Terrains += createHashmap(getFromJson(Array::class.java, "Terrains")) - GameBasics.TileResources += createHashmap(getFromJson(Array::class.java, "TileResources")) - GameBasics.TileImprovements += createHashmap(getFromJson(Array::class.java, "TileImprovements")) - GameBasics.Helps += createHashmap(getFromJson(Array::class.java, "BasicHelp")) - GameBasics.Units += createHashmap(getFromJson(Array::class.java, "Units")) - GameBasics.PolicyBranches += createHashmap(getFromJson(Array::class.java, "Policies")) - GameBasics.Civilizations += createHashmap(getFromJson(Array::class.java, "Civilizations")) - - // ...Yes. Total Voodoo. I wish I didn't have to do this. - val x = LinkedHashMap>>() - val tutorials = getFromJson(x.javaClass, "Tutorials") - for (tut in tutorials) - GameBasics.Tutorials[tut.key] = tut.value.map{it.joinToString("\r\n")} - - val techColumns = getFromJson(Array::class.java, "Techs") - for (techColumn in techColumns) { - for (tech in techColumn.techs) { - tech.cost = techColumn.techCost - tech.column = techColumn - GameBasics.Technologies[tech.name] = tech - } - } - for (building in GameBasics.Buildings.values) { - if (building.requiredTech == null) continue - val column = building.getRequiredTech().column - if (building.cost == 0) - building.cost = if (building.isWonder) column!!.wonderCost else column!!.buildingCost - } - - for (branch in GameBasics.PolicyBranches.values) { - branch.requires = ArrayList() - branch.branch = branch.name - for (policy in branch.policies) { - policy.branch = branch.name - if (policy.requires == null) policy.requires = arrayListOf(branch.name) - } - branch.policies.last().name = branch.name + " Complete" - } - } companion object { lateinit var Current: UnCivGame diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index 7052708adf..9f4c17da2e 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -34,7 +34,6 @@ class GameInfo { civInfo.endTurn() } - // 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 diff --git a/core/src/com/unciv/logic/city/PopulationManager.kt b/core/src/com/unciv/logic/city/PopulationManager.kt index bce8171245..0188ebfd5d 100644 --- a/core/src/com/unciv/logic/city/PopulationManager.kt +++ b/core/src/com/unciv/logic/city/PopulationManager.kt @@ -45,10 +45,12 @@ class PopulationManager { // starvation! { population-- - val lowestRankedWorkedTile = cityInfo.workedTiles - .map{cityInfo.tileMap[it]} - .minBy { Automation().rankTile(it,cityInfo.civInfo) }!! - cityInfo.workedTiles.remove(lowestRankedWorkedTile.position) + if(cityInfo.workedTiles.size>population) { + val lowestRankedWorkedTile = cityInfo.workedTiles + .map { cityInfo.tileMap[it] } + .minBy { Automation().rankTile(it, cityInfo.civInfo) }!! + cityInfo.workedTiles.remove(lowestRankedWorkedTile.position) + } foodStored = 0 cityInfo.civInfo.addNotification(cityInfo.name + " is starving!", cityInfo.location) } diff --git a/core/src/com/unciv/models/gamebasics/GameBasics.kt b/core/src/com/unciv/models/gamebasics/GameBasics.kt index 582ff2cce8..98056869e1 100644 --- a/core/src/com/unciv/models/gamebasics/GameBasics.kt +++ b/core/src/com/unciv/models/gamebasics/GameBasics.kt @@ -1,5 +1,9 @@ package com.unciv.models.gamebasics +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.utils.Json +import com.unciv.models.stats.INamed + object GameBasics { val Buildings = LinkedHashMap() val Terrains = LinkedHashMap() @@ -11,4 +15,58 @@ object GameBasics { val Civilizations = LinkedHashMap() val PolicyBranches = LinkedHashMap() val Tutorials = LinkedHashMap>() + + private fun getFromJson(tClass: Class, name: String): T { + val jsonText = Gdx.files.internal("jsons/$name.json").readString() + return Json().fromJson(tClass, jsonText) + } + + private fun createHashmap(items: Array): LinkedHashMap { + val hashMap = LinkedHashMap() + for (item in items) + hashMap[item.name] = item + return hashMap + } + + init { + Buildings += createHashmap(getFromJson(Array::class.java, "Buildings")) + Terrains += createHashmap(getFromJson(Array::class.java, "Terrains")) + TileResources += createHashmap(getFromJson(Array::class.java, "TileResources")) + TileImprovements += createHashmap(getFromJson(Array::class.java, "TileImprovements")) + Helps += createHashmap(getFromJson(Array::class.java, "BasicHelp")) + Units += createHashmap(getFromJson(Array::class.java, "Units")) + PolicyBranches += createHashmap(getFromJson(Array::class.java, "Policies")) + Civilizations += createHashmap(getFromJson(Array::class.java, "Civilizations")) + + // ...Yes. Total Voodoo. I wish I didn't have to do this. + val x = LinkedHashMap>>() + val tutorials = getFromJson(x.javaClass, "Tutorials") + for (tut in tutorials) + Tutorials[tut.key] = tut.value.map{it.joinToString("\r\n")} + + val techColumns = getFromJson(Array::class.java, "Techs") + for (techColumn in techColumns) { + for (tech in techColumn.techs) { + tech.cost = techColumn.techCost + tech.column = techColumn + Technologies[tech.name] = tech + } + } + for (building in Buildings.values) { + if (building.requiredTech == null) continue + val column = building.getRequiredTech().column + if (building.cost == 0) + building.cost = if (building.isWonder) column!!.wonderCost else column!!.buildingCost + } + + for (branch in PolicyBranches.values) { + branch.requires = ArrayList() + branch.branch = branch.name + for (policy in branch.policies) { + policy.branch = branch.name + if (policy.requires == null) policy.requires = arrayListOf(branch.name) + } + branch.policies.last().name = branch.name + " Complete" + } + } } \ No newline at end of file diff --git a/core/src/com/unciv/ui/LoadScreen.kt b/core/src/com/unciv/ui/LoadScreen.kt index f0787d243d..a2942f746b 100644 --- a/core/src/com/unciv/ui/LoadScreen.kt +++ b/core/src/com/unciv/ui/LoadScreen.kt @@ -1,13 +1,10 @@ package com.unciv.ui import com.badlogic.gdx.graphics.Color -import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton -import com.badlogic.gdx.utils.Array import com.unciv.UnCivGame import com.unciv.logic.GameSaver -import com.unciv.models.gamebasics.GameBasics import com.unciv.ui.cityscreen.addClickListener import com.unciv.ui.pickerscreens.PickerScreen import com.unciv.ui.utils.CameraStageBaseScreen @@ -56,36 +53,3 @@ class LoadScreen : PickerScreen() { } -class NewGameScreen:CameraStageBaseScreen(){ - init { - val table = Table() - table.skin=skin - - table.add("Civilization: ") - val civSelectBox = SelectBox(skin) - val civArray = Array() - GameBasics.Civilizations.keys.filterNot { it=="Barbarians" }.forEach{civArray.add(it)} - civSelectBox.setItems(civArray) - civSelectBox.selected = civSelectBox.items.first() - table.add(civSelectBox).pad(10f).row() - - table.add("World size: ") - val worldSizeToRadius=LinkedHashMap() - worldSizeToRadius.put("Small",10) - worldSizeToRadius.put("Medium",20) - worldSizeToRadius.put("Large",30) - val worldSizeSelectBox = SelectBox(skin) - val worldSizeArray = Array() - worldSizeToRadius.keys.forEach{worldSizeArray.add(it)} - worldSizeSelectBox.setItems(worldSizeArray) - worldSizeSelectBox.selected = "Medium" - table.add(worldSizeSelectBox) - - val createButton = TextButton("Start game!",skin) - createButton.addClickListener { } - - table.setFillParent(true) - table.pack() - stage.addActor(table) - } -} \ No newline at end of file diff --git a/core/src/com/unciv/ui/NewGameScreen.kt b/core/src/com/unciv/ui/NewGameScreen.kt new file mode 100644 index 0000000000..f2510dd141 --- /dev/null +++ b/core/src/com/unciv/ui/NewGameScreen.kt @@ -0,0 +1,2 @@ +package com.unciv.ui + diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreenOptionsTable.kt b/core/src/com/unciv/ui/worldscreen/WorldScreenOptionsTable.kt index 8224098305..c4cad0cc63 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreenOptionsTable.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreenOptionsTable.kt @@ -4,10 +4,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton import com.unciv.logic.civilization.CivilizationInfo -import com.unciv.ui.CivilopediaScreen -import com.unciv.ui.LoadScreen -import com.unciv.ui.SaveScreen -import com.unciv.ui.VictoryScreen +import com.unciv.ui.* import com.unciv.ui.cityscreen.addClickListener import com.unciv.ui.pickerscreens.PolicyPickerScreen import com.unciv.ui.utils.CameraStageBaseScreen @@ -48,7 +45,7 @@ class WorldScreenOptionsTable internal constructor(worldScreen: WorldScreen, pri add(saveGameButton ).row() val startNewGameButton = TextButton("Start new game", CameraStageBaseScreen.skin).apply { color=ImageGetter.getBlue() } - startNewGameButton.addClickListener { worldScreen.game.startNewGame(true) } + startNewGameButton.addClickListener { worldScreen.game.screen = NewGameScreen() } add(startNewGameButton).row() val openVictoryScreen = TextButton("Victory status", CameraStageBaseScreen.skin).apply { color=ImageGetter.getBlue() }