Tile height set by lazy.

This is good enough to give a 10% performance boost for regular players when there are a lot of units.
If mods add elevation somehow WITHIN a turn, this will be lost, but will recalc next turn correctly.
This commit is contained in:
yairm210 2021-10-08 15:54:24 +03:00
parent c70d315fae
commit 0b3f8d11d3
4 changed files with 10 additions and 7 deletions

View File

@ -31,7 +31,7 @@ object UnitAutomation {
unit.movement.getDistanceToTiles().keys.filter { isGoodTileToExplore(unit, it) }
if (explorableTilesThisTurn.any()) {
val bestTile = explorableTilesThisTurn
.sortedByDescending { it.getHeight() } // secondary sort is by 'how far can you see'
.sortedByDescending { it.height } // secondary sort is by 'how far can you see'
.maxByOrNull { it.aerialDistanceTo(unit.currentTile) }!! // primary sort is by 'how far can you go'
unit.movement.headTowards(bestTile)
return true

View File

@ -157,12 +157,14 @@ open class TileInfo {
// We have to .toList() so that the values are stored together once for caching,
// and the toSequence so that aggregations (like neighbors.flatMap{it.units} don't take up their own space
fun getHeight(): Int {
return getAllTerrains().flatMap { it.uniqueObjects }
.filter { it.placeholderText == "Has an elevation of [] for visibility calculations" }
@delegate:Transient
val height : Int by lazy {
getAllTerrains().flatMap { it.uniqueObjects }
.filter { it.isOfType(UniqueType.VisibilityElevation) }
.map { it.params[0].toInt() }.sum()
}
fun getBaseTerrain(): Terrain = baseTerrainObject
fun getOwner(): CivilizationInfo? {

View File

@ -280,7 +280,7 @@ class TileMap {
/** @return List of tiles visible from location [position] for a unit with sight range [sightDistance] */
fun getViewableTiles(position: Vector2, sightDistance: Int): List<TileInfo> {
val viewableTiles = getTilesInDistance(position, 1).toMutableList()
val currentTileHeight = get(position).getHeight()
val currentTileHeight = get(position).height
for (i in 1..sightDistance) { // in each layer,
// This is so we don't use tiles in the same distance to "see over",
@ -288,7 +288,7 @@ class TileMap {
val tilesToAddInDistanceI = ArrayList<TileInfo>()
for (cTile in getTilesAtDistance(position, i)) { // for each tile in that layer,
val cTileHeight = cTile.getHeight()
val cTileHeight = cTile.height
/*
Okay so, if we're looking at a tile from a to c with b in the middle,
@ -307,7 +307,7 @@ class TileMap {
val containsViewableNeighborThatCanSeeOver = cTile.neighbors.any {
bNeighbor: TileInfo ->
val bNeighborHeight = bNeighbor.getHeight()
val bNeighborHeight = bNeighbor.height
viewableTiles.contains(bNeighbor) && (
currentTileHeight > bNeighborHeight // a>b
|| cTileHeight > bNeighborHeight // c>b

View File

@ -223,6 +223,7 @@ enum class UniqueType(val text:String, vararg targets: UniqueTarget) {
TileProvidesYieldWithoutPopulation("Tile provides yield without assigned population", UniqueTarget.Terrain, UniqueTarget.Improvement),
NullifyYields("Nullifies all other stats this tile provides", UniqueTarget.Terrain),
VisibilityElevation("Has an elevation of [amount] for visibility calculations", UniqueTarget.Terrain),
NoNaturalGeneration("Doesn't generate naturally", UniqueTarget.Terrain),