diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index f7302f4e2f..6ad3f8fba6 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -17,13 +17,29 @@ object UnitAutomation { const val CLOSE_ENEMY_TILES_AWAY_LIMIT = 5 const val CLOSE_ENEMY_TURNS_AWAY_LIMIT = 3f + private fun isGoodTileToExplore(unit:MapUnit, tile:TileInfo): Boolean { + return unit.movement.canMoveTo(tile) + && (tile.getOwner() == null || !tile.getOwner()!!.isCityState()) + && tile.neighbors.any { it.position !in unit.civInfo.exploredTiles } + && unit.movement.canReach(tile) + } + internal fun tryExplore(unit: MapUnit): Boolean { if (tryGoToRuin(unit) && unit.currentMovement == 0f) return true - for (tile in unit.currentTile.getTilesInDistance(5)) // number increases exponentially with distance - at 10 this took a looong time - if (unit.movement.canMoveTo(tile) && tile.neighbors.any { it.position !in unit.civInfo.exploredTiles } - && unit.movement.canReach(tile) - && (tile.getOwner() == null || !tile.getOwner()!!.isCityState())) { + val explorableTilesThisTurn = + 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' + .maxBy { it.aerialDistanceTo(unit.currentTile) }!! // primary sort is by 'how far can you go' + unit.movement.headTowards(bestTile) + return true + } + + // Nothing immediate, let's look further. Number increases exponentially with distance - at 10 this took a looong time + for (tile in unit.currentTile.getTilesInDistance(5)) + if (isGoodTileToExplore(unit, tile)) { unit.movement.headTowards(tile) return true } diff --git a/core/src/com/unciv/logic/map/TileInfo.kt b/core/src/com/unciv/logic/map/TileInfo.kt index dbb6030a96..f20e3b1f24 100644 --- a/core/src/com/unciv/logic/map/TileInfo.kt +++ b/core/src/com/unciv/logic/map/TileInfo.kt @@ -94,10 +94,6 @@ open class TileInfo { if (resource == null) throw Exception("No resource exists for this tile!") else ruleset.tileResources[resource!!]!! - fun getTileResourceOrNull(): TileResource? = - if (resource == null) null - else ruleset.tileResources.getOrElse(resource!!) {null } - fun getNaturalWonder() : Terrain = if (naturalWonder == null) throw Exception("No natural wonder exists for this tile!") else ruleset.terrains[naturalWonder!!]!! @@ -323,9 +319,13 @@ open class TileInfo { fun isRoughTerrain() = getBaseTerrain().rough || getTerrainFeature()?.rough == true - fun toString(viewingCiv: CivilizationInfo): String { + override fun toString():String { // for debugging, it helps to see what you're doing + return toString(null) + } + + fun toString(viewingCiv: CivilizationInfo?): String { val lineList = ArrayList() // more readable than StringBuilder, with same performance for our use-case - val isViewableToPlayer = UncivGame.Current.viewEntireMapForDebug + val isViewableToPlayer = viewingCiv==null || UncivGame.Current.viewEntireMapForDebug || viewingCiv.viewableTiles.contains(this) if (isCityCenter()) {