From d77415de8d5886b429bfd55e6647158110ef5b49 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 1 Jun 2020 01:54:26 +0300 Subject: [PATCH] Barbarian automation's run away uses the same function as the worker's run away - solves a crash caused by bad logic in the barbarian automation 3.9.0-patch1 --- buildSrc/src/main/kotlin/BuildConfig.kt | 4 +-- .../logic/automation/BarbarianAutomation.kt | 10 ++----- .../unciv/logic/automation/UnitAutomation.kt | 27 +++++++++++++++++ .../logic/automation/WorkerAutomation.kt | 29 ++----------------- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/buildSrc/src/main/kotlin/BuildConfig.kt b/buildSrc/src/main/kotlin/BuildConfig.kt index 05a9554a..60a754a0 100644 --- a/buildSrc/src/main/kotlin/BuildConfig.kt +++ b/buildSrc/src/main/kotlin/BuildConfig.kt @@ -3,8 +3,8 @@ package com.unciv.build object BuildConfig { const val kotlinVersion = "1.3.71" const val appName = "Unciv" - const val appCodeNumber = 433 - const val appVersion = "3.9.0" + const val appCodeNumber = 434 + const val appVersion = "3.9.0-patch1" const val gdxVersion = "1.9.10" const val roboVMVersion = "2.3.1" diff --git a/core/src/com/unciv/logic/automation/BarbarianAutomation.kt b/core/src/com/unciv/logic/automation/BarbarianAutomation.kt index 7badcac4..be4a2922 100644 --- a/core/src/com/unciv/logic/automation/BarbarianAutomation.kt +++ b/core/src/com/unciv/logic/automation/BarbarianAutomation.kt @@ -48,14 +48,13 @@ class BarbarianAutomation(val civInfo: CivilizationInfo) { val possibleDamage = nearEnemyTiles .map { BattleDamage.calculateDamageToAttacker(MapUnitCombatant(unit), + it.tileToAttackFrom, Battle.getMapCombatantOfTile(it.tileToAttack)!!) } .sum() val possibleHeal = unit.rankTileForHealing(unit.currentTile) if (possibleDamage > possibleHeal) { - // run - val furthestTile = findFurthestTileCanMoveTo(unit, unitDistanceToTiles, nearEnemyTiles) - if(furthestTile!=null) unit.movement.moveToTile(furthestTile) + UnitAutomation.runAway(unit) } unit.fortifyIfCan() return @@ -85,10 +84,7 @@ class BarbarianAutomation(val civInfo: CivilizationInfo) { // 1 - heal or run if death is near if (unit.health < 50) { - if (nearEnemyTiles.isNotEmpty()) { - val furthestTile = findFurthestTileCanMoveTo(unit, unitDistanceToTiles, nearEnemyTiles) - if(furthestTile!=null) unit.movement.moveToTile(furthestTile) - } + if (nearEnemyTiles.isNotEmpty()) UnitAutomation.runAway(unit) unit.fortifyIfCan() return diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index d3242919..976c1049 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -234,6 +234,7 @@ object UnitAutomation { ).filter { // Ignore units that would 1-shot you if you attacked BattleDamage.calculateDamageToAttacker(MapUnitCombatant(unit), + it.tileToAttackFrom, Battle.getMapCombatantOfTile(it.tileToAttack)!!) < unit.health } @@ -404,4 +405,30 @@ object UnitAutomation { unit.civInfo.addNotification("[${unit.name}] finished exploring.", unit.currentTile.position, Color.GRAY) unit.action = null } + + + fun runAway(unit: MapUnit) { + val reachableTiles = unit.movement.getDistanceToTiles() + val enterableCity = reachableTiles.keys.firstOrNull { it.isCityCenter() && unit.movement.canMoveTo(it) } + if(enterableCity!=null) { + unit.movement.moveToTile(enterableCity) + return + } + val tileFurthestFromEnemy = reachableTiles.keys.filter { unit.movement.canMoveTo(it) } + .maxBy{ countDistanceToClosestEnemy(unit, it)} + if(tileFurthestFromEnemy==null) return // can't move anywhere! + unit.movement.moveToTile(tileFurthestFromEnemy) + } + + + fun countDistanceToClosestEnemy(unit: MapUnit, tile: TileInfo): Int { + for(i in 1..3) + if(tile.getTilesAtDistance(i).any{containsEnemyMilitaryUnit(unit,it)}) + return i + return 4 + } + + fun containsEnemyMilitaryUnit(unit: MapUnit, tileInfo: TileInfo) = tileInfo.militaryUnit != null + && tileInfo.militaryUnit!!.civInfo.isAtWarWith(unit.civInfo) + } \ No newline at end of file diff --git a/core/src/com/unciv/logic/automation/WorkerAutomation.kt b/core/src/com/unciv/logic/automation/WorkerAutomation.kt index 9aee7e88..10512161 100644 --- a/core/src/com/unciv/logic/automation/WorkerAutomation.kt +++ b/core/src/com/unciv/logic/automation/WorkerAutomation.kt @@ -11,14 +11,12 @@ import com.unciv.models.ruleset.tile.TileImprovement class WorkerAutomation(val unit: MapUnit) { - fun containsEnemyMilitaryUnit(tileInfo: TileInfo) = tileInfo.militaryUnit != null - && tileInfo.militaryUnit!!.civInfo.isAtWarWith(unit.civInfo) fun automateWorkerAction() { val enemyUnitsInWalkingDistance = unit.movement.getDistanceToTiles().keys - .filter(this::containsEnemyMilitaryUnit) + .filter { UnitAutomation.containsEnemyMilitaryUnit(unit, it) } - if (enemyUnitsInWalkingDistance.isNotEmpty()) return runAway() + if (enemyUnitsInWalkingDistance.isNotEmpty()) return UnitAutomation.runAway(unit) val currentTile = unit.getTile() val tileToWork = findTileToWork() @@ -34,7 +32,7 @@ class WorkerAutomation(val unit: MapUnit) { } if (currentTile.improvementInProgress == null && currentTile.isLand - && tileCanBeImproved(currentTile,unit.civInfo)) { + && tileCanBeImproved(currentTile, unit.civInfo)) { return currentTile.startWorkingOnImprovement(chooseImprovement(currentTile, unit.civInfo)!!, unit.civInfo) } @@ -62,27 +60,6 @@ class WorkerAutomation(val unit: MapUnit) { } - fun countDistanceToClosestEnemy(tile: TileInfo): Int { - for(i in 1..3) - if(tile.getTilesAtDistance(i).any(this::containsEnemyMilitaryUnit)) - return i - return 4 - } - - private fun runAway() { - val reachableTiles = unit.movement.getDistanceToTiles() - val enterableCity = reachableTiles.keys.firstOrNull { it.isCityCenter() && unit.movement.canMoveTo(it) } - if(enterableCity!=null) { - unit.movement.moveToTile(enterableCity) - return - } - val tileFurthestFromEnemy = reachableTiles.keys.filter { unit.movement.canMoveTo(it) } - .maxBy(this::countDistanceToClosestEnemy) - if(tileFurthestFromEnemy==null) return // can't move anywhere! - unit.movement.moveToTile(tileFurthestFromEnemy) - } - - private fun tryConnectingCities(unit: MapUnit):Boolean { // returns whether we actually did anything //Player can choose not to auto-build roads & railroads. if (unit.civInfo.isPlayerCivilization() && !UncivGame.Current.settings.autoBuildingRoads)