diff --git a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt index 214c368929..da099c1bc5 100644 --- a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt @@ -20,6 +20,7 @@ import com.unciv.logic.civilization.diplomacy.DiplomaticStatus import com.unciv.logic.civilization.diplomacy.RelationshipLevel import com.unciv.logic.civilization.managers.ReligionState import com.unciv.logic.map.BFS +import com.unciv.logic.map.mapunit.MapUnit import com.unciv.logic.map.tile.Tile import com.unciv.logic.trade.Trade import com.unciv.logic.trade.TradeEvaluation @@ -1019,17 +1020,22 @@ object NextTurnAutomation { private fun automateUnits(civInfo: Civilization) { - val sortedUnits = civInfo.units.getCivUnits().sortedBy { unit -> - when { - unit.baseUnit.isAirUnit() -> 2 - unit.baseUnit.isRanged() -> 3 - unit.baseUnit.isMelee() -> 4 - unit.isGreatPersonOfType("War") -> 5 // Generals move after military units - else -> 1 // Civilian - } - } + val isAtWar = civInfo.isAtWar() + val sortedUnits = civInfo.units.getCivUnits().sortedBy { unit -> getUnitPriority(unit, isAtWar) } for (unit in sortedUnits) UnitAutomation.automateUnitMoves(unit) } + + private fun getUnitPriority(unit: MapUnit, isAtWar: Boolean): Int { + if (unit.isCivilian() && !unit.isGreatPersonOfType("War")) return 1 // Civilian + if (unit.baseUnit.isAirUnit()) return 2 + val distance = if (!isAtWar) 0 else unit.getDistanceToEnemyUnit(6) + return (distance ?: 5) + when { + unit.baseUnit.isRanged() -> 2 + unit.baseUnit.isMelee() -> 3 + unit.isGreatPersonOfType("War") -> 100 // Generals move after military units + else -> 1 + } + } private fun automateCityBombardment(civInfo: Civilization) { for (city in civInfo.cities) UnitAutomation.tryBombardEnemy(city) diff --git a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt index d0faa6157f..f06aae4f5c 100644 --- a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt +++ b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt @@ -442,6 +442,14 @@ class MapUnit : IsPartOfGameInfoSerialization { fun isGreatPerson() = baseUnit.isGreatPerson() fun isGreatPersonOfType(type: String) = baseUnit.isGreatPersonOfType(type) + fun getDistanceToEnemyUnit(maxDist: Int): Int? { + for (i in 1..maxDist) { + if (currentTile.getTilesAtDistance(i).any {it.militaryUnit != null + && it.militaryUnit!!.civ.isAtWarWith(civ) }) + return i + } + return null + } //endregion //region state-changing functions