perf(cpu): cheaper, and slightly clearer, 'movement affected by zone of control'

God bless the profiler which can show cpu time of individual lines within if
This commit is contained in:
yairm210 2024-11-14 15:18:44 +02:00
parent 1599db1740
commit 5315ddc62a

View File

@ -172,8 +172,7 @@ object MovementCost {
// these two tiles can perhaps be optimized. Using a hex-math-based "commonAdjacentTiles" // these two tiles can perhaps be optimized. Using a hex-math-based "commonAdjacentTiles"
// function is surprisingly less efficient than the current neighbor-intersection approach. // function is surprisingly less efficient than the current neighbor-intersection approach.
// See #4085 for more details. // See #4085 for more details.
val tilesExertingZoneOfControl = getTilesExertingZoneOfControl(unit, from) if (!anyTilesExertingZoneOfControl(unit, from, to))
if (tilesExertingZoneOfControl.none { to.aerialDistanceTo(it) == 1 })
return false return false
// Even though this is a very fast check, we perform it last. This is because very few units // Even though this is a very fast check, we perform it last. This is because very few units
@ -185,16 +184,19 @@ object MovementCost {
return true return true
} }
private fun getTilesExertingZoneOfControl(unit: MapUnit, tile: Tile) = sequence { private fun anyTilesExertingZoneOfControl(unit: MapUnit, from: Tile, to:Tile): Boolean {
for (neighbor in tile.neighbors) { for (neighbor in from.neighbors) {
if (neighbor.isCityCenter() && unit.civ.isAtWarWith(neighbor.getOwner()!!)) { if (neighbor.isCityCenter()) {
yield(neighbor) if (neighbor.aerialDistanceTo(to) == 1
} && unit.civ.isAtWarWith(neighbor.getOwner()!!))
else if (neighbor.militaryUnit != null && unit.civ.isAtWarWith(neighbor.militaryUnit!!.civ)) { return true
if (neighbor.militaryUnit!!.type.isWaterUnit() || (unit.type.isLandUnit() && !neighbor.militaryUnit!!.isEmbarked())) } else if (neighbor.militaryUnit != null) {
yield(neighbor) 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
} }
} }