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