From 835ed003be2d4dd72fed1405fb2bc7ffdeb84de2 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 14 Dec 2020 11:07:11 +0200 Subject: [PATCH] Resolved #3380 - added tryHeadTowardsSiegedCity to unit automation, by nob0dy73 --- .../unciv/logic/automation/UnitAutomation.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index d698e8eef7..fc11e37b94 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -9,7 +9,6 @@ import com.unciv.logic.civilization.diplomacy.DiplomaticStatus import com.unciv.logic.map.MapUnit import com.unciv.logic.map.TileInfo import com.unciv.models.ruleset.unit.UnitType -import com.unciv.models.translations.equalsPlaceholderText import com.unciv.ui.worldscreen.unit.UnitActions object UnitAutomation { @@ -126,6 +125,8 @@ object UnitAutomation { // Accompany settlers if (tryAccompanySettlerOrGreatPerson(unit)) return + if(tryHeadTowardsSiegedCity(unit)) return + if (unit.health < 50 && tryHealUnit(unit)) return // do nothing but heal // if a embarked melee unit can land and attack next turn, do not attack from water. @@ -268,6 +269,25 @@ object UnitAutomation { return true } + private fun tryHeadTowardsSiegedCity(unit: MapUnit): Boolean { + val siegedCities = unit.civInfo.cities + .asSequence() + .filter { unit.civInfo == it.civInfo && + it.health < it.getMaxHealth() * 0.75 } //Weird health issues and making sure that not all forces move to good defenses + + val reachableTileNearSiegedCity = siegedCities + .flatMap { it.getCenterTile().getTilesAtDistance(2) } + .sortedBy { it.aerialDistanceTo(unit.currentTile) } + .firstOrNull { unit.movement.canReach(it) } + + if (reachableTileNearSiegedCity != null) { + unit.movement.headTowards(reachableTileNearSiegedCity) + return true + } + return false + } + + private fun tryHeadTowardsEnemyCity(unit: MapUnit): Boolean { if (unit.civInfo.cities.isEmpty()) return false