Performance improvement - added baseTerrain transient in tileInfo

This commit is contained in:
Yair Morgenstern 2018-11-24 21:58:39 +02:00
parent 347d329233
commit 6c9297abc2
4 changed files with 68 additions and 65 deletions

View file

@ -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 {

View file

@ -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<MapUnit> {
val list = ArrayList<MapUnit>(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<TileInfo> {
return tileMap.getViewableTiles(this.position,distance)
}
fun getTilesInDistance(distance:Int): List<TileInfo> {
return tileMap.getTilesInDistance(position,distance)
}
fun getTilesAtDistance(distance:Int): List<TileInfo> {
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<TileInfo> {
return tileMap.getViewableTiles(this.position,distance)
}
fun getTilesInDistance(distance:Int): List<TileInfo> {
return tileMap.getTilesInDistance(position,distance)
}
fun getTilesAtDistance(distance:Int): List<TileInfo> {
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
}

View file

@ -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()
}
}

View file

@ -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
}