Added TileSetConfigs (#3766)

* First iteration of TileSet json

* Adding json support for mods

* Renamed TilesetConfig to TileSetConfig

+ changed the config for FHex to fix the mountain natural wonder problem
This commit is contained in:
GGGuenni 2021-04-13 06:49:49 +02:00 committed by GitHub
parent 881a885c14
commit b7d6bacc5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 5 deletions

View File

@ -0,0 +1,12 @@
{
"useColorAsBaseTerrain": "false",
"ruleVariants": {
"Mountain+Barringer Crater": ["Barringer Crater"],
"Mountain+Cerro de Potosi": ["Cerro de Potosi"],
"Mountain+Grand Mesa": ["Grand Mesa"],
"Mountain+Krakatoa": ["Krakatoa"],
"Mountain+Mount Fuji": ["Mount Fuji"],
"Mountain+Old Faithful": ["Old Faithful"],
"Mountain+Rock of Gibraltar": ["Rock of Gibraltar"]
}
}

View File

@ -13,6 +13,7 @@ import com.unciv.logic.GameSaver
import com.unciv.logic.civilization.PlayerType
import com.unciv.models.metadata.GameSettings
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.tilesets.TileSetCache
import com.unciv.models.translations.Translations
import com.unciv.ui.LanguagePickerScreen
import com.unciv.ui.utils.*
@ -97,6 +98,7 @@ class UncivGame(parameters: UncivGameParameters) : Game() {
RulesetCache.loadRulesets(printOutput = true)
translations.tryReadTranslationForCurrentLanguage()
translations.loadPercentageCompleteOfLanguages()
TileSetCache.loadTileSetConfigs(printOutput = true)
if (settings.userId.isEmpty()) { // assign permanent user id
settings.userId = UUID.randomUUID().toString()

View File

@ -0,0 +1,62 @@
package com.unciv.models.tilesets
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.unciv.JsonParser
object TileSetCache : HashMap<String, TileSetConfig>(){
fun loadTileSetConfigs(consoleMode: Boolean = false, printOutput: Boolean = false){
clear()
var tileSetName = ""
//load default TileSets
for (configFile in Gdx.files.local("jsons/TileSets").list()){
tileSetName = configFile.nameWithoutExtension().removeSuffix("Config")
try {
if (this[tileSetName] == null)
this[tileSetName] = JsonParser().getFromJson(TileSetConfig::class.java, configFile)
else
this[tileSetName]!!.updateConfig(JsonParser().getFromJson(TileSetConfig::class.java, configFile))
if (printOutput) {
println("TileSetConfig loaded successfully: ${configFile.name()}")
println()
}
} catch (ex: Exception){
if (printOutput){
println("Exception loading TileSetConfig '${configFile.path()}':")
println(" ${ex.localizedMessage}")
println(" (Source file ${ex.stackTrace[0].fileName} line ${ex.stackTrace[0].lineNumber})")
}
}
}
//load mod TileSets
val modsHandles = if (consoleMode) FileHandle("mods").list()
else Gdx.files.local("mods").list()
for (modFolder in modsHandles) {
if (modFolder.name().startsWith('.')) continue
if (!modFolder.isDirectory) continue
try {
for (configFile in modFolder.child("jsons/TileSets").list()){
tileSetName = configFile.nameWithoutExtension().removeSuffix("Config")
if (this[tileSetName] == null)
this[tileSetName] = JsonParser().getFromJson(TileSetConfig::class.java, configFile)
else
this[tileSetName]!!.updateConfig(JsonParser().getFromJson(TileSetConfig::class.java, configFile))
if (printOutput) {
println("TileSetConfig loaded successfully: ${configFile.path()}")
println()
}
}
} catch (ex: Exception){
if (printOutput) {
println("Exception loading TileSetConfig '${modFolder.name()}/jsons/TileSets/${tileSetName}':")
println(" ${ex.localizedMessage}")
println(" (Source file ${ex.stackTrace[0].fileName} line ${ex.stackTrace[0].lineNumber})")
}
}
}
}
}

View File

@ -0,0 +1,13 @@
package com.unciv.models.tilesets
class TileSetConfig {
var useColorAsBaseTerrain = true
var ruleVariants: HashMap<String, Array<String>> = HashMap()
fun updateConfig(other: TileSetConfig){
useColorAsBaseTerrain = other.useColorAsBaseTerrain
for ((tileSetString, renderOrder) in other.ruleVariants){
ruleVariants[tileSetString] = renderOrder
}
}
}

View File

@ -182,10 +182,14 @@ open class TileGroup(var tileInfo: TileInfo, var tileSetStrings:TileSetStrings,
resourceAndImprovementSequence = resourceAndImprovementSequence.filterNotNull()
val terrainImages = (sequenceOf(tileInfo.baseTerrain) + tileInfo.terrainFeatures.asSequence()).filterNotNull()
val allTogether = (terrainImages + resourceAndImprovementSequence).joinToString("+").let { tileSetStrings.getTile(it) }
val allTogether = (terrainImages + resourceAndImprovementSequence).joinToString("+")
val allTogetherLocation = tileSetStrings.getTile(allTogether)
if (ImageGetter.imageExists(allTogether)) return listOf(allTogether)
else return getTerrainImageLocations(terrainImages) + getImprovementAndResourceImages(resourceAndImprovementSequence)
return when {
tileSetStrings.tileSetConfig.ruleVariants[allTogether] != null -> tileSetStrings.tileSetConfig.ruleVariants[allTogether]!!.map { tileSetStrings.getTile(it) }
ImageGetter.imageExists(allTogetherLocation) -> listOf(allTogetherLocation)
else -> getTerrainImageLocations(terrainImages) + getImprovementAndResourceImages(resourceAndImprovementSequence)
}
}
fun getTerrainImageLocations(terrainSequence: Sequence<String>): List<String> {
@ -195,8 +199,6 @@ open class TileGroup(var tileInfo: TileInfo, var tileSetStrings:TileSetStrings,
}
fun getImprovementAndResourceImages(resourceAndImprovementSequence: Sequence<String>): List<String> {
if(tileInfo.resource=="Silk")
println()
val altogether = resourceAndImprovementSequence.joinToString("+").let { tileSetStrings.getTile(it) }
if (ImageGetter.imageExists(altogether)) return listOf(altogether)
else return resourceAndImprovementSequence.map { tileSetStrings.getTile(it) }.toList()

View File

@ -1,11 +1,14 @@
package com.unciv.ui.tilegroups
import com.unciv.UncivGame
import com.unciv.models.tilesets.TileSetCache
import com.unciv.models.tilesets.TileSetConfig
class TileSetStrings {
// this is so that when we have 100s of TileGroups, they won't all individually come up with all these strings themselves,
// it gets pretty memory-intensive (10s of MBs which is a lot for lower-end phones)
val tileSetLocation = "TileSets/" + UncivGame.Current.settings.tileSet + "/"
val tileSetConfig = TileSetCache[UncivGame.Current.settings.tileSet] ?: TileSetConfig()
val hexagon = tileSetLocation + "Hexagon"
val crosshatchHexagon = tileSetLocation + "CrosshatchHexagon"

View File

@ -12,6 +12,7 @@ import com.unciv.models.metadata.GameSpeed
import com.unciv.models.metadata.Player
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.simulation.Simulation
import com.unciv.models.tilesets.TileSetCache
import com.unciv.ui.newgamescreen.GameSetupInfo
internal object ConsoleLauncher {
@ -35,6 +36,7 @@ internal object ConsoleLauncher {
}
RulesetCache.loadRulesets(true)
TileSetCache.loadTileSetConfigs(true)
val gameParameters = getGameParameters("China", "Greece")
val mapParameters = getMapParameters()