Carrier right-click attack fix (#9130)

This commit is contained in:
SomeTroglodyte 2023-04-08 20:36:19 +02:00 committed by GitHub
parent cb20d91822
commit 93d2ba2af5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 11 deletions

View File

@ -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

View File

@ -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...

View File

@ -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) {
// 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
return@run // can't move here
}
worldScreen.preActionGameInfo = worldScreen.gameInfo.clone()

View File

@ -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!!)