From 52165e692f33ee5a39566535fe497db057d2dee3 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 2 Jan 2020 19:36:19 +0200 Subject: [PATCH] Specific automation for Missile units means they won't try to move to tiles that they can't move to --- .../automation/SpecificUnitAutomation.kt | 24 +++++++++++++++++++ .../unciv/logic/automation/UnitAutomation.kt | 3 +++ .../unciv/logic/map/UnitMovementAlgorithms.kt | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt b/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt index 764f7f8019..3da84501f5 100644 --- a/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt @@ -252,4 +252,28 @@ class SpecificUnitAutomation{ airUnit.movement.moveToTile(firstStepInPath) } + + + // This really needs to be changed, to have better targetting for missiles + fun automateMissile(unit: MapUnit) { + if (UnitAutomation().tryAttackNearbyEnemy(unit)) return + + val tilesInRange = unit.currentTile.getTilesInDistance(unit.getRange()) + + val immediatelyReachableCities = tilesInRange + .filter { it.isCityCenter() && it.getOwner() == unit.civInfo && unit.movement.canMoveTo(it) } + + for (city in immediatelyReachableCities) { + if (city.getTilesInDistance(unit.getRange()) + .any { UnitAutomation().containsAttackableEnemy(it, MapUnitCombatant(unit)) }) { + unit.movement.moveToTile(city) + return + } + } + + val pathsToCities = unit.movement.getArialPathsToCities() + if (pathsToCities.isEmpty()) return // can't actually move anywhere else + tryMoveToCitiesToArialAttackFrom(pathsToCities, unit) + } + } \ No newline at end of file diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index b05f572e53..ff5c6095ef 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -45,6 +45,9 @@ class UnitAutomation{ if(unit.type==UnitType.Bomber) return SpecificUnitAutomation().automateBomber(unit) + if(unit.type==UnitType.Missile) + return SpecificUnitAutomation().automateMissile(unit) + if(unit.name.startsWith("Great") && unit.name in GreatPersonManager().statToGreatPersonMapping.values){ // So "Great War Infantry" isn't caught here return SpecificUnitAutomation().automateGreatPerson(unit) diff --git a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt index 2db18b1a76..b5ae7bfe4a 100644 --- a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt +++ b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt @@ -214,7 +214,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) { class CantEnterThisTileException(msg: String) : Exception(msg) if(!canMoveTo(destination)) - throw CantEnterThisTileException("$this can't enter $destination") + throw CantEnterThisTileException("$unit can't enter $destination") if(unit.type.isAirUnit()){ // they move differently from all other units unit.action=null