From 27b4c2c2abc1bbfab37da6f4e8cf35348f5924d6 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 13 Aug 2018 09:25:29 +0300 Subject: [PATCH] AI units now fortify when healing and prefer tiles with defensive bonuses while healing --- .../unciv/logic/automation/UnitAutomation.kt | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index 3b81481ca6..b654535e9a 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -80,21 +80,18 @@ class UnitAutomation{ } fun healUnit(unit:MapUnit) { - val tilesInDistance = unit.getDistanceToTiles().keys + val tilesInDistance = unit.getDistanceToTiles().keys.filter { unit.canMoveTo(it) } val unitTile = unit.getTile() - // Go to friendly tile if within distance - better healing! - val friendlyTile = tilesInDistance.firstOrNull { it.getOwner()?.civName == unit.owner && unit.canMoveTo(it) } - if (unitTile.getOwner()?.civName != unit.owner && friendlyTile != null) { - unit.moveToTile(friendlyTile) - return - } - - // Or at least get out of enemy territory yaknow - val neutralTile = tilesInDistance.firstOrNull { it.getOwner() == null && unit.canMoveTo(it)} - if (unitTile.getOwner()?.civName != unit.owner && unitTile.getOwner() != null && neutralTile != null) { - unit.moveToTile(neutralTile) - return + val tilesByHealingRate = tilesInDistance.groupBy { rankTileForHealing(it,unit) } + if(tilesByHealingRate.isEmpty()) return + val bestTilesForHealing = tilesByHealingRate.maxBy { it.key }!!.value + // within the tiles with best healing rate, we'll prefer one which has defensive bonuses + val bestTileForHealing = bestTilesForHealing.maxBy { it.getDefensiveBonus() }!! + if(unitTile!=bestTileForHealing && rankTileForHealing(bestTileForHealing,unit)>rankTileForHealing(unitTile,unit)) + unit.moveToTile(bestTileForHealing) + if(unit.currentMovement>0 && !unit.hasUnique("No defensive terrain bonus") && !unit.isFortified() ){ + unit.action="Fortify 0" } }