From 6c9297abc210d06de2fe41d2edf93fcf24dbd2cc Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sat, 24 Nov 2018 21:58:39 +0200 Subject: [PATCH] Performance improvement - added baseTerrain transient in tileInfo --- .../com/unciv/logic/map/RandomMapGenerator.kt | 2 +- core/src/com/unciv/logic/map/TileInfo.kt | 119 ++++++++++-------- core/src/com/unciv/logic/map/TileMap.kt | 10 +- .../unciv/logic/map/UnitMovementAlgorithms.kt | 2 +- 4 files changed, 68 insertions(+), 65 deletions(-) diff --git a/core/src/com/unciv/logic/map/RandomMapGenerator.kt b/core/src/com/unciv/logic/map/RandomMapGenerator.kt index 91426ec0a0..89fd6bd651 100644 --- a/core/src/com/unciv/logic/map/RandomMapGenerator.kt +++ b/core/src/com/unciv/logic/map/RandomMapGenerator.kt @@ -243,7 +243,7 @@ open class RandomMapGenerator { var tileResources = GameBasics.TileResources.values.toList() // Resources are placed according to TerrainFeature, if exists, otherwise according to BaseLayer. - tileResources = tileResources.filter { it.terrainsCanBeFoundOn.contains(tileInfo.lastTerrain.name) } + tileResources = tileResources.filter { it.terrainsCanBeFoundOn.contains(tileInfo.getLastTerrain().name) } var resource: TileResource? = null when { diff --git a/core/src/com/unciv/logic/map/TileInfo.kt b/core/src/com/unciv/logic/map/TileInfo.kt index 92e687733e..7708aff06e 100644 --- a/core/src/com/unciv/logic/map/TileInfo.kt +++ b/core/src/com/unciv/logic/map/TileInfo.kt @@ -13,6 +13,7 @@ import kotlin.math.abs open class TileInfo { @Transient lateinit var tileMap: TileMap @Transient var owningCity:CityInfo?=null + @Transient lateinit var baseTerrainObject:Terrain var militaryUnit:MapUnit?=null var civilianUnit:MapUnit?=null @@ -43,7 +44,7 @@ open class TileInfo { return toReturn } - + //region pure functions fun getUnits(): List { val list = ArrayList(2) if(militaryUnit!=null) list.add(militaryUnit!!) @@ -52,12 +53,9 @@ open class TileInfo { // this used to be "return listOf(militaryUnit,civilianUnit).filterNotNull()" but profiling revealed that that took considerably longer } - fun getCity(): CityInfo? { - return owningCity - } + fun getCity(): CityInfo? = owningCity - val lastTerrain: Terrain - get() = if (terrainFeature == null) getBaseTerrain() else getTerrainFeature()!! + fun getLastTerrain(): Terrain = if (terrainFeature == null) getBaseTerrain() else getTerrainFeature()!! fun getTileResource(): TileResource = if (resource == null) throw Exception("No resource exists for this tile!") @@ -78,18 +76,15 @@ open class TileInfo { return internalNeighbors!! } - val height: Int - get() { - var height = 0 - if (listOf("Forest", "Jungle").contains(terrainFeature)) height += 1 - if ("Hill" == baseTerrain) height += 2 - return height - } - - fun getBaseTerrain(): Terrain { - return GameBasics.Terrains[baseTerrain]!! + fun getHeight(): Int { + var height = 0 + if (listOf("Forest", "Jungle").contains(terrainFeature)) height += 1 + if ("Hill" == baseTerrain) height += 2 + return height } + fun getBaseTerrain(): Terrain = baseTerrainObject + fun getOwner(): CivilizationInfo? { val containingCity = getCity() if(containingCity==null) return null @@ -100,6 +95,13 @@ open class TileInfo { return if (terrainFeature == null) null else GameBasics.Terrains[terrainFeature!!] } + fun isWorked(): Boolean { + val city = getCity() + return city!=null && city.workedTiles.contains(position) + } + + fun isLand() = getBaseTerrain().type==TerrainType.Land + fun isWater() = getBaseTerrain().type==TerrainType.Water fun getTileStats(observingCiv: CivilizationInfo): Stats { return getTileStats(getCity(), observingCiv) @@ -188,15 +190,37 @@ open class TileInfo { } - fun startWorkingOnImprovement(improvement: TileImprovement, civInfo: CivilizationInfo) { - improvementInProgress = improvement.name - turnsToImprovement = improvement.getTurnsToBuild(civInfo) + fun hasViewableResource(civInfo: CivilizationInfo): Boolean { + return resource != null && (getTileResource().revealedBy == null || civInfo.tech.isResearched(getTileResource().revealedBy!!)) } - fun stopWorkingOnImprovement() { - improvementInProgress = null + fun hasIdleUnit(): Boolean { + return getUnits().any{it.isIdle()} } + fun getViewableTiles(distance:Int): MutableList { + return tileMap.getViewableTiles(this.position,distance) + } + + fun getTilesInDistance(distance:Int): List { + return tileMap.getTilesInDistance(position,distance) + } + + fun getTilesAtDistance(distance:Int): List { + return tileMap.getTilesAtDistance(position,distance) + } + + fun getDefensiveBonus(): Float { + var bonus = getBaseTerrain().defenceBonus + if(terrainFeature!=null) bonus += getTerrainFeature()!!.defenceBonus + return bonus + } + + fun arialDistanceTo(otherTile:TileInfo): Int { + val xDelta = position.x-otherTile.position.x + val yDelta = position.y-otherTile.position.y + return listOf(abs(xDelta),abs(yDelta), abs(xDelta-yDelta)).max()!!.toInt() + } override fun toString(): String { val SB = StringBuilder() @@ -233,43 +257,28 @@ open class TileInfo { return SB.toString().trim() } - fun hasViewableResource(civInfo: CivilizationInfo): Boolean { - return resource != null && (getTileResource().revealedBy == null || civInfo.tech.isResearched(getTileResource().revealedBy!!)) + //endregion + + //region state-changing functions + fun setTransients(){ + baseTerrainObject = GameBasics.Terrains[baseTerrain]!! + + if(militaryUnit!=null) militaryUnit!!.currentTile = this + if(civilianUnit!=null) civilianUnit!!.currentTile = this + + for (unit in getUnits()) { + unit.assignOwner(tileMap.gameInfo.civilizations.first { it.civName == unit.owner }) + unit.setTransients() + } } - fun hasIdleUnit(): Boolean { - return getUnits().any{it.isIdle()} + fun startWorkingOnImprovement(improvement: TileImprovement, civInfo: CivilizationInfo) { + improvementInProgress = improvement.name + turnsToImprovement = improvement.getTurnsToBuild(civInfo) } - fun getViewableTiles(distance:Int): MutableList { - return tileMap.getViewableTiles(this.position,distance) - } - - fun getTilesInDistance(distance:Int): List { - return tileMap.getTilesInDistance(position,distance) - } - - fun getTilesAtDistance(distance:Int): List { - return tileMap.getTilesAtDistance(position,distance) - } - - fun getDefensiveBonus(): Float { - var bonus = getBaseTerrain().defenceBonus - if(terrainFeature!=null) bonus += getTerrainFeature()!!.defenceBonus - return bonus - } - - fun isWorked(): Boolean { - val city = getCity() - return city!=null && city.workedTiles.contains(position) - } - - fun isLand() = getBaseTerrain().type==TerrainType.Land - fun isWater() = getBaseTerrain().type==TerrainType.Water - - fun arialDistanceTo(otherTile:TileInfo): Int { - val xDelta = position.x-otherTile.position.x - val yDelta = position.y-otherTile.position.y - return listOf(abs(xDelta),abs(yDelta), abs(xDelta-yDelta)).max()!!.toInt() + fun stopWorkingOnImprovement() { + improvementInProgress = null } + //endregion } \ No newline at end of file diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index cb2d9959c8..610ed4db39 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -91,7 +91,7 @@ class TileMap { val viewableTiles = getTilesInDistance(position, 1).toMutableList() for (i in 1..sightDistance) { // in each layer, getTilesAtDistance(position, i).filterTo(viewableTiles) // take only tiles which have a visible neighbor, which is lower than the tile - { tile -> tile.neighbors.any{viewableTiles.contains(it) && (it.height==0 || it.height < tile.height)} } + { tile -> tile.neighbors.any{viewableTiles.contains(it) && (it.getHeight() ==0 || it.getHeight() < tile.getHeight())} } } return viewableTiles @@ -115,13 +115,7 @@ class TileMap { for (tileInfo in values){ tileMatrix[tileInfo.position.x.toInt()-leftX][tileInfo.position.y.toInt()-bottomY] = tileInfo tileInfo.tileMap = this - if(tileInfo.militaryUnit!=null) tileInfo.militaryUnit!!.currentTile = tileInfo - if(tileInfo.civilianUnit!=null) tileInfo.civilianUnit!!.currentTile = tileInfo - - for (unit in tileInfo.getUnits()) { - unit.assignOwner(gameInfo.civilizations.first { it.civName == unit.owner }) - unit.setTransients() - } + tileInfo.setTransients() } } diff --git a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt index 3c7356aba8..bee0867858 100644 --- a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt +++ b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt @@ -28,7 +28,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) { if(unit.hasUnique("Double movement in coast") && to.baseTerrain=="Coast") return 1/2f - return to.lastTerrain.movementCost.toFloat() // no road + return to.getLastTerrain().movementCost.toFloat() // no road }