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
This commit is contained in:
Yair Morgenstern 2020-06-01 01:54:26 +03:00
parent 4b649825cd
commit d77415de8d
4 changed files with 35 additions and 35 deletions

View file

@ -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"

View file

@ -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

View file

@ -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)
}

View file

@ -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()
@ -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)