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) } unit.movement.getDistanceToTiles().keys.filter { isGoodTileToExplore(unit, it) }
if (explorableTilesThisTurn.any()) { if (explorableTilesThisTurn.any()) {
val bestTile = explorableTilesThisTurn 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' .maxByOrNull { it.aerialDistanceTo(unit.currentTile) }!! // primary sort is by 'how far can you go'
unit.movement.headTowards(bestTile) unit.movement.headTowards(bestTile)
return true 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, // 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 // and the toSequence so that aggregations (like neighbors.flatMap{it.units} don't take up their own space
fun getHeight(): Int { @delegate:Transient
return getAllTerrains().flatMap { it.uniqueObjects } val height : Int by lazy {
.filter { it.placeholderText == "Has an elevation of [] for visibility calculations" } getAllTerrains().flatMap { it.uniqueObjects }
.filter { it.isOfType(UniqueType.VisibilityElevation) }
.map { it.params[0].toInt() }.sum() .map { it.params[0].toInt() }.sum()
} }
fun getBaseTerrain(): Terrain = baseTerrainObject fun getBaseTerrain(): Terrain = baseTerrainObject
fun getOwner(): CivilizationInfo? { 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] */ /** @return List of tiles visible from location [position] for a unit with sight range [sightDistance] */
fun getViewableTiles(position: Vector2, sightDistance: Int): List<TileInfo> { fun getViewableTiles(position: Vector2, sightDistance: Int): List<TileInfo> {
val viewableTiles = getTilesInDistance(position, 1).toMutableList() val viewableTiles = getTilesInDistance(position, 1).toMutableList()
val currentTileHeight = get(position).getHeight() val currentTileHeight = get(position).height
for (i in 1..sightDistance) { // in each layer, for (i in 1..sightDistance) { // in each layer,
// This is so we don't use tiles in the same distance to "see over", // 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>() val tilesToAddInDistanceI = ArrayList<TileInfo>()
for (cTile in getTilesAtDistance(position, i)) { // for each tile in that layer, 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, 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 { val containsViewableNeighborThatCanSeeOver = cTile.neighbors.any {
bNeighbor: TileInfo -> bNeighbor: TileInfo ->
val bNeighborHeight = bNeighbor.getHeight() val bNeighborHeight = bNeighbor.height
viewableTiles.contains(bNeighbor) && ( viewableTiles.contains(bNeighbor) && (
currentTileHeight > bNeighborHeight // a>b currentTileHeight > bNeighborHeight // a>b
|| cTileHeight > bNeighborHeight // c>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), TileProvidesYieldWithoutPopulation("Tile provides yield without assigned population", UniqueTarget.Terrain, UniqueTarget.Improvement),
NullifyYields("Nullifies all other stats this tile provides", UniqueTarget.Terrain), 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), NoNaturalGeneration("Doesn't generate naturally", UniqueTarget.Terrain),