mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-23 22:30:18 +07:00
Errors from icorrect pathing were happening in another function, so got rid of all passing around of unitTilesToDistance, for good measure
This commit is contained in:
@ -73,14 +73,14 @@ class BarbarianAutomation(val civInfo: CivilizationInfo) {
|
||||
|
||||
// 3 - trying to attack enemy
|
||||
// if a embarked melee unit can land and attack next turn, do not attack from water.
|
||||
if (battleHelper.tryDisembarkUnitToAttackPosition(unit, unitDistanceToTiles)) return
|
||||
if (battleHelper.tryDisembarkUnitToAttackPosition(unit)) return
|
||||
if (battleHelper.tryAttackNearbyEnemy(unit)) return
|
||||
|
||||
// 4 - trying to pillage tile or route
|
||||
if (UnitAutomation().tryPillageImprovement(unit, unitDistanceToTiles)) return
|
||||
if (UnitAutomation().tryPillageImprovement(unit)) return
|
||||
|
||||
// 5 - heal the unit if needed
|
||||
if (unit.health < 100 && UnitAutomation().tryHealUnit(unit, unitDistanceToTiles)) return
|
||||
if (unit.health < 100 && UnitAutomation().tryHealUnit(unit)) return
|
||||
|
||||
// 6 - wander
|
||||
UnitAutomation.wander(unit)
|
||||
@ -105,10 +105,10 @@ class BarbarianAutomation(val civInfo: CivilizationInfo) {
|
||||
// TODO
|
||||
|
||||
// 3 - trying to pillage tile or trade route
|
||||
if (UnitAutomation().tryPillageImprovement(unit, unitDistanceToTiles)) return
|
||||
if (UnitAutomation().tryPillageImprovement(unit)) return
|
||||
|
||||
// 4 - heal the unit if needed
|
||||
if (unit.health < 100 && UnitAutomation().tryHealUnit(unit, unitDistanceToTiles)) return
|
||||
if (unit.health < 100 && UnitAutomation().tryHealUnit(unit)) return
|
||||
|
||||
// 5 - wander
|
||||
UnitAutomation.wander(unit)
|
||||
|
@ -104,7 +104,8 @@ class BattleHelper {
|
||||
return true
|
||||
}
|
||||
|
||||
fun tryDisembarkUnitToAttackPosition(unit: MapUnit, unitDistanceToTiles: PathsToTilesWithinTurn): Boolean {
|
||||
fun tryDisembarkUnitToAttackPosition(unit: MapUnit): Boolean {
|
||||
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
if (!unit.type.isMelee() || !unit.type.isLandUnit() || !unit.isEmbarked()) return false
|
||||
|
||||
val attackableEnemiesNextTurn = getAttackableEnemies(unit, unitDistanceToTiles)
|
||||
|
@ -27,7 +27,7 @@ class SpecificUnitAutomation {
|
||||
.firstOrNull { unit.movement.canReach(it) }
|
||||
|
||||
when (closestReachableResource) {
|
||||
null -> UnitAutomation.tryExplore(unit, unit.movement.getDistanceToTiles())
|
||||
null -> UnitAutomation.tryExplore(unit)
|
||||
else -> {
|
||||
unit.movement.headTowards(closestReachableResource)
|
||||
|
||||
@ -134,7 +134,7 @@ class SpecificUnitAutomation {
|
||||
|
||||
if (bestCityLocation == null) { // We got a badass over here, all tiles within 5 are taken? Screw it, random walk.
|
||||
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
if (UnitAutomation.tryExplore(unit, unitDistanceToTiles)) return // try to find new areas
|
||||
if (UnitAutomation.tryExplore(unit)) return // try to find new areas
|
||||
UnitAutomation.wander(unit) // go around aimlessly
|
||||
return
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import com.unciv.logic.city.CityInfo
|
||||
import com.unciv.logic.civilization.GreatPersonManager
|
||||
import com.unciv.logic.civilization.diplomacy.DiplomaticStatus
|
||||
import com.unciv.logic.map.MapUnit
|
||||
import com.unciv.logic.map.PathsToTilesWithinTurn
|
||||
import com.unciv.logic.map.TileInfo
|
||||
import com.unciv.models.ruleset.unit.UnitType
|
||||
import com.unciv.ui.worldscreen.unit.UnitActions
|
||||
@ -18,8 +17,8 @@ class UnitAutomation {
|
||||
const val CLOSE_ENEMY_TILES_AWAY_LIMIT = 5
|
||||
const val CLOSE_ENEMY_TURNS_AWAY_LIMIT = 3f
|
||||
|
||||
internal fun tryExplore(unit: MapUnit, unitDistanceToTiles: PathsToTilesWithinTurn): Boolean {
|
||||
if (tryGoToRuin(unit, unitDistanceToTiles) && unit.currentMovement == 0f) return true
|
||||
internal fun tryExplore(unit: MapUnit): Boolean {
|
||||
if (tryGoToRuin(unit) && unit.currentMovement == 0f) return true
|
||||
|
||||
for (tile in unit.currentTile.getTilesInDistance(10))
|
||||
if (unit.movement.canMoveTo(tile) && tile.position !in unit.civInfo.exploredTiles
|
||||
@ -31,8 +30,9 @@ class UnitAutomation {
|
||||
return false
|
||||
}
|
||||
|
||||
private fun tryGoToRuin(unit: MapUnit, unitDistanceToTiles: PathsToTilesWithinTurn): Boolean {
|
||||
private fun tryGoToRuin(unit: MapUnit): Boolean {
|
||||
if (!unit.civInfo.isMajorCiv()) return false // barbs don't have anything to do in ruins
|
||||
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
val tileWithRuin = unitDistanceToTiles.keys
|
||||
.firstOrNull {
|
||||
it.improvement == Constants.ancientRuins && unit.movement.canMoveTo(it)
|
||||
@ -73,21 +73,17 @@ class UnitAutomation {
|
||||
private val battleHelper = BattleHelper()
|
||||
|
||||
fun automateUnitMoves(unit: MapUnit) {
|
||||
if (unit.civInfo.isBarbarian()) {
|
||||
if (unit.civInfo.isBarbarian())
|
||||
throw IllegalStateException("Barbarians is not allowed here.")
|
||||
}
|
||||
|
||||
if (unit.name == Constants.settler) {
|
||||
if (unit.name == Constants.settler)
|
||||
return SpecificUnitAutomation().automateSettlerActions(unit)
|
||||
}
|
||||
|
||||
if (unit.name == Constants.worker) {
|
||||
if (unit.name == Constants.worker)
|
||||
return WorkerAutomation(unit).automateWorkerAction()
|
||||
}
|
||||
|
||||
if (unit.name == "Work Boats") {
|
||||
if (unit.name == "Work Boats")
|
||||
return SpecificUnitAutomation().automateWorkBoats(unit)
|
||||
}
|
||||
|
||||
if (unit.name == Constants.greatGeneral || unit.baseUnit.replaces == Constants.greatGeneral)
|
||||
return SpecificUnitAutomation().automateGreatGeneral(unit)
|
||||
@ -101,16 +97,13 @@ class UnitAutomation {
|
||||
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
|
||||
&& unit.name in GreatPersonManager().statToGreatPersonMapping.values)// So "Great War Infantry" isn't caught here
|
||||
return SpecificUnitAutomation().automateGreatPerson(unit)
|
||||
}
|
||||
|
||||
var unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
|
||||
if (tryGoToRuin(unit, unitDistanceToTiles)) {
|
||||
if (tryGoToRuin(unit)) {
|
||||
if (unit.currentMovement == 0f) return
|
||||
unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
}
|
||||
|
||||
if (tryUpgradeUnit(unit)) return
|
||||
@ -118,22 +111,22 @@ class UnitAutomation {
|
||||
// Accompany settlers
|
||||
if (tryAccompanySettlerOrGreatPerson(unit)) return
|
||||
|
||||
if (unit.health < 50 && tryHealUnit(unit, unitDistanceToTiles)) return // do nothing but heal
|
||||
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.
|
||||
if (battleHelper.tryDisembarkUnitToAttackPosition(unit, unitDistanceToTiles)) return
|
||||
if (battleHelper.tryDisembarkUnitToAttackPosition(unit)) return
|
||||
|
||||
// if there is an attackable unit in the vicinity, attack!
|
||||
if (battleHelper.tryAttackNearbyEnemy(unit)) return
|
||||
|
||||
if (tryGarrisoningUnit(unit)) return
|
||||
|
||||
if (unit.health < 80 && tryHealUnit(unit, unitDistanceToTiles)) return
|
||||
if (unit.health < 80 && tryHealUnit(unit)) return
|
||||
|
||||
// move towards the closest reasonably attackable enemy unit within 3 turns of movement (and 5 tiles range)
|
||||
if (tryAdvanceTowardsCloseEnemy(unit)) return
|
||||
|
||||
if (unit.health < 100 && tryHealUnit(unit, unitDistanceToTiles)) return
|
||||
if (unit.health < 100 && tryHealUnit(unit)) return
|
||||
|
||||
// Focus all units without a specific target on the enemy city closest to one of our cities
|
||||
if (tryHeadTowardsEnemyCity(unit)) return
|
||||
@ -141,7 +134,7 @@ class UnitAutomation {
|
||||
if (tryHeadTowardsEncampment(unit)) return
|
||||
|
||||
// else, try to go o unreached tiles
|
||||
if (tryExplore(unit, unitDistanceToTiles)) return
|
||||
if (tryExplore(unit)) return
|
||||
}
|
||||
|
||||
private fun tryHeadTowardsEncampment(unit: MapUnit): Boolean {
|
||||
@ -159,12 +152,13 @@ class UnitAutomation {
|
||||
return true
|
||||
}
|
||||
|
||||
fun tryHealUnit(unit: MapUnit, unitDistanceToTiles: PathsToTilesWithinTurn): Boolean {
|
||||
fun tryHealUnit(unit: MapUnit): Boolean {
|
||||
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
val tilesInDistance = unitDistanceToTiles.keys.filter { unit.movement.canMoveTo(it) }
|
||||
if (unitDistanceToTiles.isEmpty()) return true // can't move, so...
|
||||
val currentUnitTile = unit.getTile()
|
||||
|
||||
if (tryPillageImprovement(unit, unitDistanceToTiles)) return true
|
||||
if (tryPillageImprovement(unit)) return true
|
||||
|
||||
val tilesByHealingRate = tilesInDistance.groupBy { unit.rankTileForHealing(it) }
|
||||
|
||||
@ -190,8 +184,9 @@ class UnitAutomation {
|
||||
return true
|
||||
}
|
||||
|
||||
fun tryPillageImprovement(unit: MapUnit, unitDistanceToTiles: PathsToTilesWithinTurn): Boolean {
|
||||
fun tryPillageImprovement(unit: MapUnit): Boolean {
|
||||
if (unit.type.isCivilian()) return false
|
||||
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
val tilesThatCanWalkToAndThenPillage = unitDistanceToTiles
|
||||
.filter { it.value.totalDistance < unit.currentMovement }.keys
|
||||
.filter { unit.movement.canMoveTo(it) && UnitActions.canPillage(unit, it) }
|
||||
@ -390,10 +385,9 @@ class UnitAutomation {
|
||||
/** This is what a unit with the 'explore' action does.
|
||||
It also explores, but also has other functions, like healing if necessary. */
|
||||
fun automatedExplore(unit: MapUnit) {
|
||||
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
|
||||
if (tryGoToRuin(unit, unitDistanceToTiles) && unit.currentMovement == 0f) return
|
||||
if (unit.health < 80 && tryHealUnit(unit, unitDistanceToTiles)) return
|
||||
if (tryExplore(unit, unit.movement.getDistanceToTiles())) return
|
||||
if (tryGoToRuin(unit) && unit.currentMovement == 0f) return
|
||||
if (unit.health < 80 && tryHealUnit(unit)) return
|
||||
if (tryExplore(unit)) return
|
||||
unit.civInfo.addNotification("[${unit.name}] finished exploring.", unit.currentTile.position, Color.GRAY)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user