Resolve #8336 - Remove 'incorrect area' code for super-old maps

This commit is contained in:
Yair Morgenstern 2023-01-08 19:09:17 +02:00
parent 37cfc12a64
commit f9da3204b6
5 changed files with 8 additions and 51 deletions

View File

@ -524,8 +524,6 @@ Area: [amount] tiles, [amount2]% water, [amount3] continents/islands =
Do you want to leave without saving the recent changes? =
Leave =
Do you want to load another map without saving the recent changes? =
Invalid map: Area ([area]) does not match saved dimensions ([dimensions]). =
The dimensions have now been fixed for you. =
River generation failed! =
Please don't use step 'Landmass' with map type 'Empty', create a new empty map instead. =
This map has errors: =

View File

@ -14,23 +14,13 @@ object MapSaver {
private fun getMap(mapName:String) = Gdx.files.local("$mapsFolder/$mapName")
fun mapFromSavedString(mapString: String, checkSizeErrors: Boolean = true): TileMap {
fun mapFromSavedString(mapString: String): TileMap {
val unzippedJson = try {
Gzip.unzip(mapString.trim())
} catch (ex: Exception) {
mapString
}
return mapFromJson(unzippedJson).apply {
// old maps (rarely) can come with mapSize fields not matching tile list
if (checkSizeErrors && mapParameters.getArea() != values.size)
throw UncivShowableException("Invalid map: Area ([${values.size}]) does not match saved dimensions ([${mapParameters.displayMapDimensions()}]).")
// compatibility with rare maps saved with old mod names
if (!checkSizeErrors)
mapParameters.mods.filter { '-' in it }.forEach {
mapParameters.mods.remove(it)
mapParameters.mods.add(it.replace('-',' '))
}
}
return mapFromJson(unzippedJson)
}
fun mapToSavedString(tileMap: TileMap): String {
tileMap.assignContinents(TileMap.AssignContinentsMode.Reassign)
@ -42,8 +32,8 @@ object MapSaver {
getMap(mapName).writeString(mapToSavedString(tileMap), false)
}
fun loadMap(mapFile: FileHandle, checkSizeErrors: Boolean = true): TileMap {
return mapFromSavedString(mapFile.readString(), checkSizeErrors)
fun loadMap(mapFile: FileHandle): TileMap {
return mapFromSavedString(mapFile.readString())
}
fun getMaps(): Array<FileHandle> = Gdx.files.local(mapsFolder).list()

View File

@ -9,9 +9,9 @@ import com.unciv.logic.MapSaver
import com.unciv.logic.UncivShowableException
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.translations.tr
import com.unciv.ui.popup.ConfirmPopup
import com.unciv.ui.popup.Popup
import com.unciv.ui.popup.ToastPopup
import com.unciv.ui.popup.ConfirmPopup
import com.unciv.ui.utils.AutoScrollPane
import com.unciv.ui.utils.BaseScreen
import com.unciv.ui.utils.KeyCharAndCode
@ -105,7 +105,7 @@ class MapEditorLoadTab(
}
}
try {
val map = MapSaver.loadMap(chosenMap!!, checkSizeErrors = false)
val map = MapSaver.loadMap(chosenMap!!)
val missingMods = map.mapParameters.mods.filter { it !in RulesetCache }.toMutableList()
// [TEMPORARY] conversion of old maps with a base ruleset contained in the mods

View File

@ -75,7 +75,7 @@ class MapEditorOptionsTab(
private fun pasteHandler() {
try {
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val loadedMap = MapSaver.mapFromSavedString(clipboardContentsString, checkSizeErrors = false)
val loadedMap = MapSaver.mapFromSavedString(clipboardContentsString)
editorScreen.loadMap(loadedMap)
} catch (ex: Exception) {
ToastPopup("Could not load map!", editorScreen)

View File

@ -3,9 +3,7 @@ package com.unciv.ui.mapeditor
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.unciv.UncivGame
import com.unciv.logic.HexMath
import com.unciv.logic.map.MapParameters
import com.unciv.logic.map.MapShape
import com.unciv.logic.map.MapSize
import com.unciv.logic.map.MapSizeNew
import com.unciv.logic.map.TileInfo
@ -14,10 +12,8 @@ import com.unciv.models.metadata.BaseRuleset
import com.unciv.models.metadata.GameSetupInfo
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.translations.tr
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.popup.ConfirmPopup
import com.unciv.ui.popup.ToastPopup
import com.unciv.ui.tilegroups.TileGroup
import com.unciv.ui.utils.BaseScreen
import com.unciv.ui.utils.KeyCharAndCode
@ -159,7 +155,6 @@ class MapEditorScreen(map: TileMap? = null): BaseScreen(), RecreateOnResize {
fun loadMap(map: TileMap, newRuleset: Ruleset? = null, selectPage: Int = 0) {
mapHolder.remove()
tileMap = map
checkAndFixMapSize()
ruleset = newRuleset ?: RulesetCache.getComplexRuleset(map.mapParameters)
mapHolder = newMapHolder()
isDirty = false
@ -217,31 +212,5 @@ class MapEditorScreen(map: TileMap? = null): BaseScreen(), RecreateOnResize {
highlightTile(tile, color)
}
private fun checkAndFixMapSize() {
val areaFromTiles = tileMap.values.size
val params = tileMap.mapParameters
val areaFromSize = params.getArea()
if (areaFromSize == areaFromTiles) return
Gdx.app.postRunnable {
val message = ("Invalid map: Area ([$areaFromTiles]) does not match saved dimensions ([" +
params.displayMapDimensions() + "]).").tr() +
"\n" + "The dimensions have now been fixed for you.".tr()
ToastPopup(message, this@MapEditorScreen, 4000L )
}
if (params.shape == MapShape.hexagonal || params.shape == MapShape.flatEarth) {
params.mapSize = MapSizeNew(HexMath.getHexagonalRadiusForArea(areaFromTiles).toInt())
return
}
// These mimic tileMap.max* without the abs()
val minLatitude = (tileMap.values.map { it.latitude }.minOrNull() ?: 0f).toInt()
val minLongitude = (tileMap.values.map { it.longitude }.minOrNull() ?: 0f).toInt()
val maxLatitude = (tileMap.values.map { it.latitude }.maxOrNull() ?: 0f).toInt()
val maxLongitude = (tileMap.values.map { it.longitude }.maxOrNull() ?: 0f).toInt()
params.mapSize = MapSizeNew((maxLongitude - minLongitude + 1), (maxLatitude - minLatitude + 1) / 2)
}
override fun recreate(): BaseScreen = MapEditorScreen(tileMap)
}