mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-22 10:54:19 -04:00
perf(cpu): Optimized trade mission AI logic
This commit is contained in:
parent
d8c779fd9b
commit
67f73b2892
@ -251,7 +251,10 @@ object SpecificUnitAutomation {
|
||||
* `false` can be interpreted as: the unit doesn't know where to go or there are no city
|
||||
* states. */
|
||||
fun conductTradeMission(unit: MapUnit): Boolean {
|
||||
val closestCityStateTile =
|
||||
val conductedTradeMission = UnitActions.invokeUnitAction(unit, UnitActionType.ConductTradeMission)
|
||||
if (conductedTradeMission) return true
|
||||
|
||||
val relevantTiles =
|
||||
unit.civ.gameInfo.civilizations
|
||||
.filter {
|
||||
it != unit.civ
|
||||
@ -261,16 +264,15 @@ object SpecificUnitAutomation {
|
||||
}
|
||||
.flatMap { it.cities[0].getTiles() }
|
||||
.filter { unit.civ.hasExplored(it) }
|
||||
.mapNotNull { tile ->
|
||||
.sortedBy { unit.currentTile.aerialDistanceTo(it) }
|
||||
|
||||
val closestCityStateTile = relevantTiles.firstOrNull { tile ->
|
||||
if (!unit.movement.canPassThrough(tile)) return@firstOrNull false
|
||||
val path = unit.movement.getShortestPath(tile)
|
||||
// 0 is unreachable, 10 is too far away
|
||||
if (path.size in 1..10) tile to path.size else null
|
||||
path.size in 1..10
|
||||
}
|
||||
.minByOrNull { it.second }?.first
|
||||
?: return false
|
||||
|
||||
val conductedTradeMission = UnitActions.invokeUnitAction(unit, UnitActionType.ConductTradeMission)
|
||||
if (conductedTradeMission) return true
|
||||
if (closestCityStateTile == null) return false
|
||||
|
||||
val unitTileBeforeMovement = unit.currentTile
|
||||
unit.movement.headTowards(closestCityStateTile)
|
||||
|
Loading…
x
Reference in New Issue
Block a user