Extracted name, type, radius and noRuins of map from GameParameters to MapParameters, so that Maps can be created and defined outside of games

This commit is contained in:
Yair Morgenstern 2019-12-09 20:46:10 +02:00
parent a90059988b
commit 9493ce1a2e
10 changed files with 56 additions and 39 deletions

View file

@ -8,6 +8,7 @@ import com.badlogic.gdx.audio.Music
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
import com.unciv.logic.GameStarter
import com.unciv.logic.map.MapParameters
import com.unciv.models.gamebasics.Ruleset
import com.unciv.models.metadata.GameParameters
import com.unciv.models.metadata.GameSettings
@ -91,7 +92,7 @@ class UncivGame(val version: String) : Game() {
}
fun startNewGame() {
val newGame = GameStarter().startNewGame(GameParameters().apply { difficulty="Chieftain" })
val newGame = GameStarter().startNewGame(GameParameters().apply { difficulty="Chieftain" }, MapParameters())
loadGame(newGame)
}

View file

@ -13,15 +13,16 @@ import kotlin.math.max
class GameStarter{
fun startNewGame(newGameParameters: GameParameters): GameInfo {
fun startNewGame(newGameParameters: GameParameters, mapParameters: MapParameters): GameInfo {
val gameInfo = GameInfo()
gameInfo.gameParameters = newGameParameters
val gameBasics = UncivGame.Current.ruleset
if(newGameParameters.mapType==MapType.file)
gameInfo.tileMap = MapSaver().loadMap(newGameParameters.mapFileName!!)
else gameInfo.tileMap = MapGenerator().generateMap(newGameParameters, gameBasics)
if(mapParameters.name!="")
gameInfo.tileMap = MapSaver().loadMap(mapParameters.name)
else gameInfo.tileMap = MapGenerator().generateMap(mapParameters, gameBasics)
gameInfo.tileMap.mapParameters = mapParameters
gameInfo.tileMap.setTransients(gameBasics)
@ -29,7 +30,7 @@ class GameStarter{
gameInfo.difficulty = newGameParameters.difficulty
addCivilizations(newGameParameters, gameInfo,gameBasics) // this is before the setTransients so gameInfo doesn't yet have the gameBasics
addCivilizations(newGameParameters, gameInfo, gameBasics) // this is before the setTransients so gameInfo doesn't yet have the gameBasics
gameInfo.setTransients() // needs to be before placeBarbarianUnit because it depends on the tilemap having its gameinfo set

View file

@ -169,7 +169,9 @@ class WorkerAutomation(val unit: MapUnit) {
if (tileInfo.isWorked()) priority += 3
}
// give a minor priority to tiles that we could expand onto
else if (tileInfo.getOwner()==null && tileInfo.neighbors.any { it.getOwner() ==civInfo }) priority += 1
else if (tileInfo.getOwner()==null && tileInfo.neighbors.any { it.getOwner() ==civInfo })
priority += 1
if (priority!=0 && tileInfo.hasViewableResource(civInfo)) priority += 1
return priority
}

View file

@ -55,7 +55,7 @@ class TechManager {
// 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
val worldSizeModifier = when(civInfo.gameInfo.gameParameters.mapRadius) {
val worldSizeModifier = when(civInfo.gameInfo.tileMap.mapParameters.radius) {
20 -> floatArrayOf(1.1f, 0.05f) // Medium Size
30 -> floatArrayOf(1.2f, 0.03f) // Large Size
40 -> floatArrayOf(1.3f, 0.02f) // Huge Size

View file

@ -7,7 +7,6 @@ import com.unciv.models.Counter
import com.unciv.models.gamebasics.Ruleset
import com.unciv.models.gamebasics.tile.ResourceType
import com.unciv.models.gamebasics.tile.TerrainType
import com.unciv.models.metadata.GameParameters
import java.util.*
import kotlin.collections.HashMap
import kotlin.math.*
@ -22,17 +21,18 @@ class MapType {
val perlin="Perlin"
val continents = "Continents"
val pangaea = "Pangaea"
val file = "File"
val custom="Custom"
}
}
class MapGenerator {
fun generateMap(gameParameters: GameParameters, ruleset: Ruleset): TileMap {
val mapRadius = gameParameters.mapRadius
val mapType = gameParameters.mapType
fun generateMap(mapParameters: MapParameters, ruleset: Ruleset): TileMap {
val mapRadius = mapParameters.radius
val mapType = mapParameters.type
val map = TileMap(mapRadius, ruleset)
map.mapParameters = mapParameters
// Step one - separate land and water, in form of Grasslands and Oceans
if (mapType == MapType.perlin)
@ -45,7 +45,7 @@ class MapGenerator {
setWaterTiles(map)
for (tile in map.values) randomizeTile(tile, gameParameters, ruleset)
for (tile in map.values) randomizeTile(tile, mapParameters, ruleset)
randomizeResources(map, mapRadius, ruleset)
@ -94,13 +94,14 @@ class MapGenerator {
}
}
fun randomizeTile(tileInfo: TileInfo, gameParameters: GameParameters, ruleset: Ruleset) {
fun randomizeTile(tileInfo: TileInfo, mapParameters: MapParameters, ruleset: Ruleset) {
if (tileInfo.getBaseTerrain().type == TerrainType.Land && Math.random() < 0.05f) {
tileInfo.baseTerrain = Constants.mountain
tileInfo.setTransients()
}
addRandomTerrainFeature(tileInfo, ruleset)
maybeAddAncientRuins(tileInfo, gameParameters)
if(!mapParameters.noRuins)
maybeAddAncientRuins(tileInfo)
}
fun getLatitude(vector: Vector2): Float {
@ -194,9 +195,9 @@ class MapGenerator {
}
fun maybeAddAncientRuins(tile: TileInfo, gameParameters: GameParameters) {
fun maybeAddAncientRuins(tile: TileInfo) {
val baseTerrain = tile.getBaseTerrain()
if (!gameParameters.noRuins && baseTerrain.type != TerrainType.Water && !baseTerrain.impassable && Random().nextDouble() < 1f / 100)
if (baseTerrain.type != TerrainType.Water && !baseTerrain.impassable && Random().nextDouble() < 1f / 100)
tile.improvement = Constants.ancientRuins
}

View file

@ -0,0 +1,8 @@
package com.unciv.logic.map
class MapParameters {
var name = ""
var type = MapType.pangaea
var radius = 20
var noRuins = false
}

View file

@ -17,6 +17,7 @@ class TileMap {
@Deprecated("as of 2.7.10")
private var tiles = HashMap<String, TileInfo>()
var mapParameters= MapParameters()
private var tileList = ArrayList<TileInfo>()
constructor() // for json parsing, we need to have a default constructor
@ -169,4 +170,5 @@ class TileMap {
}
}
}
}

View file

@ -1,24 +1,21 @@
package com.unciv.models.metadata
import com.unciv.logic.civilization.PlayerType
import com.unciv.logic.map.MapType
import com.unciv.models.gamebasics.VictoryType
import com.unciv.models.gamebasics.tech.TechEra
class GameParameters { // Default values are the default new game
var difficulty = "Prince"
var gameSpeed = GameSpeed.Standard
var mapRadius = 20
var players = ArrayList<Player>().apply {
add(Player().apply { playerType = PlayerType.Human })
for (i in 1..3) add(Player())
}
var numberOfCityStates = 0
var mapType = MapType.pangaea
var noBarbarians = false
var oneCityChallenge = false
var noRuins = false;
var mapFileName: String? = null
var victoryTypes: ArrayList<VictoryType> = VictoryType.values().toCollection(ArrayList()) // By default, all victory types
var startingEra = TechEra.Ancient

View file

@ -24,13 +24,14 @@ import kotlin.concurrent.thread
class NewGameScreen: PickerScreen(){
val newGameParameters= UncivGame.Current.gameInfo.gameParameters
val mapParameters = UncivGame.Current.gameInfo.tileMap.mapParameters
val ruleSet = Ruleset(true)
init {
setDefaultCloseAction()
val playerPickerTable = PlayerPickerTable(this, newGameParameters)
topTable.add(NewGameScreenOptionsTable(newGameParameters,ruleSet) { playerPickerTable.update() })
topTable.add(NewGameScreenOptionsTable(newGameParameters,mapParameters, ruleSet) { playerPickerTable.update() })
topTable.add(playerPickerTable).pad(10f)
topTable.pack()
topTable.setFillParent(true)
@ -67,7 +68,7 @@ class NewGameScreen: PickerScreen(){
thread {
// Creating a new game can take a while and we don't want ANRs
try {
newGame = GameStarter().startNewGame(newGameParameters)
newGame = GameStarter().startNewGame(newGameParameters,mapParameters)
if (newGameParameters.isOnlineMultiplayer) {
newGame!!.isUpToDate=true // So we don't try to download it from dropbox the second after we upload it - the file is not yet ready for loading!
try {

View file

@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
import com.badlogic.gdx.utils.Array
import com.unciv.logic.MapSaver
import com.unciv.logic.map.MapParameters
import com.unciv.logic.map.MapType
import com.unciv.models.gamebasics.Ruleset
import com.unciv.models.gamebasics.VictoryType
@ -18,7 +19,9 @@ import com.unciv.models.metadata.GameSpeed
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.toLabel
class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val ruleset: Ruleset, val onMultiplayerToggled:()->Unit)
class NewGameScreenOptionsTable(val newGameParameters: GameParameters,
val mapParameters: MapParameters,
val ruleset: Ruleset, val onMultiplayerToggled:()->Unit)
: Table(CameraStageBaseScreen.skin){
init{
addMapTypeSizeAndFile()
@ -61,10 +64,10 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val rules
private fun addNoRuinsCheckbox() {
val noRuinsCheckbox = CheckBox("No ancient ruins".tr(), CameraStageBaseScreen.skin)
noRuinsCheckbox.isChecked = newGameParameters.noRuins
noRuinsCheckbox.isChecked = mapParameters.noRuins
noRuinsCheckbox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.noRuins = noRuinsCheckbox.isChecked
mapParameters.noRuins = noRuinsCheckbox.isChecked
}
})
add(noRuinsCheckbox).colspan(2).row()
@ -86,32 +89,32 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val rules
private fun addMapTypeSizeAndFile() {
add("{Map type}:".tr())
val mapTypes = arrayListOf(MapType.default,MapType.continents,MapType.perlin,MapType.pangaea)
if(MapSaver().getMaps().isNotEmpty()) mapTypes.add(MapType.file)
if(MapSaver().getMaps().isNotEmpty()) mapTypes.add(MapType.custom)
val mapFileLabel = "{Map file}:".toLabel()
val mapFileSelectBox = getMapFileSelectBox()
mapFileLabel.isVisible = false
mapFileSelectBox.isVisible = false
val mapTypeSelectBox = TranslatedSelectBox(mapTypes, newGameParameters.mapType, CameraStageBaseScreen.skin)
val mapTypeSelectBox = TranslatedSelectBox(mapTypes, mapParameters.type, CameraStageBaseScreen.skin)
val worldSizeSelectBox = getWorldSizeSelectBox()
val worldSizeLabel = "{World size}:".toLabel()
fun updateOnMapTypeChange(){
newGameParameters.mapType = mapTypeSelectBox.selected.value
if (newGameParameters.mapType == MapType.file) {
mapParameters.type = mapTypeSelectBox.selected.value
if (mapParameters.type == MapType.custom) {
worldSizeSelectBox.isVisible = false
worldSizeLabel.isVisible = false
mapFileSelectBox.isVisible = true
mapFileLabel.isVisible = true
newGameParameters.mapFileName = mapFileSelectBox.selected
mapParameters.name = mapFileSelectBox.selected
} else {
worldSizeSelectBox.isVisible = true
worldSizeLabel.isVisible = true
mapFileSelectBox.isVisible = false
mapFileLabel.isVisible = false
newGameParameters.mapFileName = null
mapParameters.name = ""
}
}
@ -217,10 +220,11 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val rules
val mapNames = Array<String>()
for (mapName in MapSaver().getMaps()) mapNames.add(mapName)
mapFileSelectBox.items = mapNames
if (mapParameters.name in mapNames) mapFileSelectBox.selected = mapParameters.name
mapFileSelectBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.mapFileName = mapFileSelectBox.selected!!
mapParameters.name = mapFileSelectBox.selected!!
}
})
return mapFileSelectBox
@ -234,12 +238,13 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val rules
worldSizeToRadius["Large"] = 30
worldSizeToRadius["Huge"] = 40
val currentWorldSizeName = worldSizeToRadius.entries.first { it.value == newGameParameters.mapRadius }.key
val currentWorldSizeName = worldSizeToRadius.entries
.first { it.value == mapParameters.radius }.key
val worldSizeSelectBox = TranslatedSelectBox(worldSizeToRadius.keys, currentWorldSizeName, CameraStageBaseScreen.skin)
worldSizeSelectBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.mapRadius = worldSizeToRadius[worldSizeSelectBox.selected.value]!!
mapParameters.radius = worldSizeToRadius[worldSizeSelectBox.selected.value]!!
}
})
return worldSizeSelectBox
@ -251,7 +256,6 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val rules
add("{Victory conditions}:".tr()).colspan(2).row()
// Create a checkbox for each VictoryType existing
var i = 0
val modCheckboxTable = Table().apply { defaults().pad(10f) }
val mods = Gdx.files.local("mods")