From 93d2ba2af537c9822c60a996b95c0c2fc87ab407 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Sat, 8 Apr 2023 20:36:19 +0200 Subject: [PATCH] Carrier right-click attack fix (#9130) --- .../com/unciv/logic/automation/unit/BattleHelper.kt | 13 +++++++++---- .../src/com/unciv/logic/map/mapunit/UnitMovement.kt | 6 +++--- .../unciv/ui/screens/worldscreen/WorldMapHolder.kt | 10 +++++++--- .../ui/screens/worldscreen/bottombar/BattleTable.kt | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/core/src/com/unciv/logic/automation/unit/BattleHelper.kt b/core/src/com/unciv/logic/automation/unit/BattleHelper.kt index cb180c523f..d68569fa37 100644 --- a/core/src/com/unciv/logic/automation/unit/BattleHelper.kt +++ b/core/src/com/unciv/logic/automation/unit/BattleHelper.kt @@ -127,15 +127,20 @@ object BattleHelper { ) return false + if (combatant is MapUnitCombatant && combatant.hasUnique(UniqueType.CannotAttack)) + return false + if (combatant is MapUnitCombatant && - combatant.unit.hasUnique(UniqueType.CanOnlyAttackUnits) && - combatant.unit.getMatchingUniques(UniqueType.CanOnlyAttackUnits).none { tileCombatant.matchesCategory(it.params[0]) } + combatant.unit.getMatchingUniques(UniqueType.CanOnlyAttackUnits).run { + any() && none { tileCombatant.matchesCategory(it.params[0]) } + } ) return false if (combatant is MapUnitCombatant && - combatant.unit.getMatchingUniques(UniqueType.CanOnlyAttackTiles) - .let { unique -> unique.any() && unique.none { tile.matchesFilter(it.params[0]) } } + combatant.unit.getMatchingUniques(UniqueType.CanOnlyAttackTiles).run { + any() && none { tile.matchesFilter(it.params[0]) } + } ) return false diff --git a/core/src/com/unciv/logic/map/mapunit/UnitMovement.kt b/core/src/com/unciv/logic/map/mapunit/UnitMovement.kt index e033a96621..d86f91252d 100644 --- a/core/src/com/unciv/logic/map/mapunit/UnitMovement.kt +++ b/core/src/com/unciv/logic/map/mapunit/UnitMovement.kt @@ -304,6 +304,8 @@ class UnitMovement(val unit: MapUnit) { } } + class UnreachableDestinationException(msg: String) : Exception(msg) + fun getTileToMoveToThisTurn(finalDestination: Tile): Tile { val currentTile = unit.getTile() @@ -314,8 +316,6 @@ class UnitMovement(val unit: MapUnit) { val distanceToTiles = getDistanceToTiles() - class UnreachableDestinationException(msg: String) : Exception(msg) - // If the tile is far away, we need to build a path how to get there, and then take the first step if (!distanceToTiles.containsKey(finalDestination)) return getShortestPath(finalDestination).firstOrNull() @@ -329,7 +329,7 @@ class UnitMovement(val unit: MapUnit) { val destinationNeighbors = finalDestination.neighbors return when (currentTile) { in destinationNeighbors -> currentTile // We're right nearby anyway, no need to move - else -> destinationNeighbors.asSequence() + else -> destinationNeighbors .filter { distanceToTiles.containsKey(it) && canMoveTo(it) } .minByOrNull { distanceToTiles.getValue(it).totalDistance } // we can get a little closer ?: currentTile // We can't get closer... diff --git a/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt b/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt index aa8b56eda9..fd8d4525e4 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt @@ -26,6 +26,7 @@ import com.unciv.logic.city.City import com.unciv.logic.civilization.Civilization import com.unciv.logic.map.TileMap import com.unciv.logic.map.mapunit.MapUnit +import com.unciv.logic.map.mapunit.UnitMovement import com.unciv.logic.map.tile.Tile import com.unciv.models.UncivSound import com.unciv.models.helpers.MapArrowType @@ -266,9 +267,12 @@ class WorldMapHolder( try { tileToMoveTo = selectedUnit.movement.getTileToMoveToThisTurn(targetTile) } catch (ex: Exception) { - Log.error("Exception in getTileToMoveToThisTurn", ex) - return@run - } // can't move here + // This is normal e.g. when selecting an air unit then right-clicking on an empty tile + // Or telling a ship to run onto a coastal land tile. + if (ex !is UnitMovement.UnreachableDestinationException) + Log.error("Exception in getTileToMoveToThisTurn", ex) + return@run // can't move here + } worldScreen.preActionGameInfo = worldScreen.gameInfo.clone() diff --git a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt index faaacfbb50..fbccc3708d 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt @@ -90,7 +90,7 @@ class BattleTable(val worldScreen: WorldScreen): Table() { val unitTable = worldScreen.bottomUnitTable return if (unitTable.selectedUnit != null && !unitTable.selectedUnit!!.isCivilian() - && !unitTable.selectedUnit!!.hasUnique(UniqueType.CannotAttack)) + && !unitTable.selectedUnit!!.hasUnique(UniqueType.CannotAttack)) // purely cosmetic - hide battle table MapUnitCombatant(unitTable.selectedUnit!!) else if (unitTable.selectedCity != null) CityCombatant(unitTable.selectedCity!!)