Performance improvements for .nextTurn()

This commit is contained in:
Yair Morgenstern 2020-04-26 23:09:06 +03:00
parent f0adc40e81
commit ba2d6fe405
4 changed files with 18 additions and 8 deletions

View file

@ -53,17 +53,17 @@ class CivInfoTransientUpdater(val civInfo: CivilizationInfo) {
// There are a LOT of tiles usually.
// And making large lists of them just as intermediaries before we shove them into the hashset is very space-inefficient.
// Ans so, sequences to the rescue!
// And so, sequences to the rescue!
val ownedTiles = civInfo.cities.asSequence().flatMap { it.getTiles() }
newViewableTiles.addAll(ownedTiles)
val neighboringUnownedTiles = ownedTiles.flatMap { it.neighbors.asSequence().filter { it.getOwner() != civInfo } }
val neighboringUnownedTiles = ownedTiles.flatMap { it.neighbors.filter { it.getOwner() != civInfo } }
newViewableTiles.addAll(neighboringUnownedTiles)
newViewableTiles.addAll(civInfo.getCivUnits().flatMap { it.viewableTiles.asSequence() })
newViewableTiles.addAll(civInfo.getCivUnits().flatMap { it.viewableTiles.asSequence().filter { it.getOwner()!=civInfo } })
if (!civInfo.isCityState()) {
for (otherCiv in civInfo.getKnownCivs()) {
if (otherCiv.getAllyCiv() == civInfo.civName) {
newViewableTiles.addAll(otherCiv.cities.asSequence().flatMap { it.getTiles().asSequence() })
newViewableTiles.addAll(otherCiv.cities.asSequence().flatMap { it.getTiles() })
}
}
}

View file

@ -222,7 +222,7 @@ class MapUnit {
fun isEmbarked(): Boolean {
if(!type.isLandUnit()) return false
return currentTile.getBaseTerrain().type==TerrainType.Water
return currentTile.isWater
}
fun isInvisible(): Boolean {

View file

@ -86,6 +86,14 @@ open class TileInfo {
if (airUnits.isNotEmpty()) yieldAll(airUnits)
}
/** This is for performance reasons of canPassThrough() - faster than getUnits().firstOrNull() */
fun getFirstUnit(): MapUnit? {
if (militaryUnit != null) return militaryUnit!!
if (civilianUnit != null) return civilianUnit!!
if (airUnits.isNotEmpty()) return airUnits.first()
return null
}
fun getCity(): CityInfo? = owningCity
fun getLastTerrain(): Terrain = if (terrainFeature != null) getTerrainFeature()!! else if(naturalWonder != null) getNaturalWonder() else getBaseTerrain()

View file

@ -54,7 +54,9 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
val distanceToTiles = PathsToTilesWithinTurn()
if (unitMovement == 0f) return distanceToTiles
val unitTile = unit.getTile().tileMap[origin]
val currentUnitTile = unit.currentTile
// This is for performance, because this is called all the time
val unitTile = if(origin==currentUnitTile.position) currentUnitTile else currentUnitTile.tileMap[origin]
distanceToTiles[unitTile] = ParentTileAndTotalDistance(unitTile, 0f)
var tilesToCheck = listOf(unitTile)
@ -343,14 +345,14 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
if (tile.naturalWonder != null) return false
val tileOwner = tile.getOwner()
if (tileOwner != null && tileOwner.civName != unit.owner) {
if (tileOwner != null && tileOwner != unit.civInfo) { // comparing the CivInfo objects is cheaper than comparing strings?
if (tile.isCityCenter() && !tile.getCity()!!.hasJustBeenConquered) return false
if (!unit.civInfo.canEnterTiles(tileOwner)
&& !(unit.civInfo.isPlayerCivilization() && tileOwner.isCityState())) return false
// AIs won't enter city-state's border.
}
val firstUnit = tile.getUnits().firstOrNull()
val firstUnit = tile.getFirstUnit()
if (firstUnit != null && firstUnit.civInfo != unit.civInfo && unit.civInfo.isAtWarWith(firstUnit.civInfo))
return false