From 3cd21c4b8e437f0b35e050b5055e2843ae9c2736 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 11 May 2018 10:50:08 +0300 Subject: [PATCH] Settler search optimization --- .../unciv/logic/automation/UnitAutomation.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index 53b7cac4db..90eacfe581 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -133,16 +133,19 @@ class UnitAutomation{ // This is to improve performance - instead of ranking each tile in the area up to 19 times, do it once. val nearbyTileRankings = unit.getTile().getTilesInDistance(7) .associateBy ( {it},{ Automation().rankTile(it,unit.civInfo) }) - var bestCityLocation = unit.getTile().getTilesInDistance(5) - .minus(tilesNearCities) - .maxBy { rankTileAsCityCenter(it, nearbyTileRankings) } - if(bestCityLocation==null) { // We got a badass over here, all tiles within 5 are taken? SEARCH EVERYWHERE - bestCityLocation = unit.civInfo.getViewableTiles() + var possibleTiles = unit.getTile().getTilesInDistance(5) + .minus(tilesNearCities) + + if(possibleTiles.isEmpty()) // We got a badass over here, all tiles within 5 are taken? SEARCH EVERYWHERE + possibleTiles = unit.civInfo.getViewableTiles() .minus(tilesNearCities) - .maxBy { rankTileAsCityCenter(it, nearbyTileRankings) } - } - bestCityLocation!! + + if(possibleTiles.isEmpty())// STILL? Practically impossibru but this may prevent a crash + return // todo: add random walk? + + val bestCityLocation = possibleTiles + .maxBy { rankTileAsCityCenter(it, nearbyTileRankings) }!! if (unit.getTile() == bestCityLocation) UnitActions().getUnitActions(unit, UnCivGame.Current.worldScreen!!).first { it.name == "Found city" }.action()