Skip to content

Commit

Permalink
perf(cpu): cheaper, and slightly clearer, 'movement affected by zone …
Browse files Browse the repository at this point in the history
…of control'

God bless the profiler which can show cpu time of individual lines within if
  • Loading branch information
yairm210 committed Nov 14, 2024
1 parent 1599db1 commit 5315ddc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions core/src/com/unciv/logic/map/mapunit/movement/MovementCost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ object MovementCost {
// these two tiles can perhaps be optimized. Using a hex-math-based "commonAdjacentTiles"
// function is surprisingly less efficient than the current neighbor-intersection approach.
// See #4085 for more details.
val tilesExertingZoneOfControl = getTilesExertingZoneOfControl(unit, from)
if (tilesExertingZoneOfControl.none { to.aerialDistanceTo(it) == 1 })
if (!anyTilesExertingZoneOfControl(unit, from, to))
return false

// Even though this is a very fast check, we perform it last. This is because very few units
Expand All @@ -185,16 +184,19 @@ object MovementCost {
return true
}

private fun getTilesExertingZoneOfControl(unit: MapUnit, tile: Tile) = sequence {
for (neighbor in tile.neighbors) {
if (neighbor.isCityCenter() && unit.civ.isAtWarWith(neighbor.getOwner()!!)) {
yield(neighbor)
}
else if (neighbor.militaryUnit != null && unit.civ.isAtWarWith(neighbor.militaryUnit!!.civ)) {
if (neighbor.militaryUnit!!.type.isWaterUnit() || (unit.type.isLandUnit() && !neighbor.militaryUnit!!.isEmbarked()))
yield(neighbor)
private fun anyTilesExertingZoneOfControl(unit: MapUnit, from: Tile, to:Tile): Boolean {
for (neighbor in from.neighbors) {
if (neighbor.isCityCenter()) {
if (neighbor.aerialDistanceTo(to) == 1
&& unit.civ.isAtWarWith(neighbor.getOwner()!!))
return true
} else if (neighbor.militaryUnit != null) {
if (neighbor.aerialDistanceTo(to) == 1
&& (neighbor.militaryUnit!!.type.isWaterUnit() || (unit.type.isLandUnit() && !neighbor.militaryUnit!!.isEmbarked()))
&& unit.civ.isAtWarWith(neighbor.militaryUnit!!.civ))
return true
}
}
return false
}

}

0 comments on commit 5315ddc

Please sign in to comment.