From de6e3f9ec823f949ce680eba80ec2d481a622e91 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 3 Jul 2018 16:37:20 +0300 Subject: [PATCH] All AI units that don't have a speciific target to attack will head towards the closest hostile city --- .../unciv/logic/automation/UnitAutomation.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index e764eb402c..c60a46c9be 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -1,6 +1,7 @@ package com.unciv.logic.automation import com.unciv.UnCivGame +import com.unciv.logic.HexMath import com.unciv.logic.battle.Battle import com.unciv.logic.battle.BattleDamage import com.unciv.logic.battle.MapUnitCombatant @@ -146,6 +147,22 @@ class UnitAutomation{ return } + // Focus all units without a specific target on the enemy city closest to one of our cities + val enemyCities = unit.civInfo.exploredTiles.map { unit.civInfo.gameInfo.tileMap[it] } + .filter { it.isCityCenter() && it.getOwner()!=unit.civInfo } + if(enemyCities.isNotEmpty() && unit.civInfo.cities.isNotEmpty()) { + val closestReachableEnemyCity = enemyCities + .filter { unit.movementAlgs().canReach(it) } + .minBy { city -> + unit.civInfo.cities.map { HexMath().getDistance(city.position, it.getCenterTile().position) }.min()!! + } + if (closestReachableEnemyCity != null) { + unit.movementAlgs().headTowards(closestReachableEnemyCity) + return + } + } + + // else, go to a random space randomWalk(unit) // if both failed, then... there aren't any reachable tiles. Which is possible.