Skip to content

Commit

Permalink
chore: renamed all "distance to tiles" to "movement"
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Jan 5, 2025
1 parent d6e09b1 commit e4694ab
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object CivilianUnitAutomation {
val tilesCanMoveTo = unit.movement.getDistanceToTiles()
.filter { unit.movement.canMoveTo(it.key) }
if (tilesCanMoveTo.isNotEmpty())
unit.movement.moveToTile(tilesCanMoveTo.minByOrNull { it.value.totalDistance }!!.key)
unit.movement.moveToTile(tilesCanMoveTo.minByOrNull { it.value.totalMovement }!!.key)
}

if (unit.isAutomatingRoadConnection())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ object HeadTowardsEnemyCityAutomation {
unitRange && it.key !in tilesInBombardRange
&& unit.getDamageFromTerrain(it.key) <= 0 // Don't set up on a mountain
}
.minByOrNull { it.value.totalDistance }?.key ?: return false // return false if no tile to move to
.minByOrNull { it.value.totalMovement }?.key ?: return false // return false if no tile to move to

// move into position far away enough that the bombard doesn't hurt
unit.movement.headTowards(tileToMoveTo)
Expand Down
6 changes: 3 additions & 3 deletions core/src/com/unciv/logic/automation/unit/UnitAutomation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ object UnitAutomation {
}

val reachableTilesMaxWalkingDistance = reachableTiles
.filter { it.value.totalDistance == unit.currentMovement
.filter { it.value.totalMovement == unit.currentMovement
&& unit.getDamageFromTerrain(it.key) <= 0 // Don't end turn on damaging terrain for no good reason
&& (!stayInTerritory || it.key.getOwner() == unit.civ) }
if (reachableTilesMaxWalkingDistance.any()) unit.movement.moveToTile(reachableTilesMaxWalkingDistance.toList().random().first)
Expand Down Expand Up @@ -447,7 +447,7 @@ object UnitAutomation {
if (unit.isCivilian()) return false
val unitDistanceToTiles = unit.movement.getDistanceToTiles()
val tilesThatCanWalkToAndThenPillage = unitDistanceToTiles
.filter { it.value.totalDistance < unit.currentMovement }.keys
.filter { it.value.totalMovement < unit.currentMovement }.keys
.filter { unit.movement.canMoveTo(it) && UnitActionsPillage.canPillage(unit, it)
&& (it.canPillageTileImprovement()
|| (!onlyPillageToHeal && it.canPillageRoad() && it.getRoadOwner() != null && unit.civ.isAtWarWith(it.getRoadOwner()!!))) }
Expand All @@ -471,7 +471,7 @@ object UnitAutomation {
* Tiles attack from which would result in instant death of the [unit] are ignored. */
private fun tryAdvanceTowardsCloseEnemy(unit: MapUnit): Boolean {
// this can be sped up if we check each layer separately
val unitDistanceToTiles = unit.movement.getDistanceToTilesAtPosition(
val unitDistanceToTiles = unit.movement.getMovementToTilesAtPosition(
unit.getTile().position,
unit.getMaxMovement() * CLOSE_ENEMY_TURNS_AWAY_LIMIT
)
Expand Down
18 changes: 10 additions & 8 deletions core/src/com/unciv/logic/battle/Battle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -417,33 +417,35 @@ object Battle {
) {
if (attacker.getCivInfo() != defender.getCivInfo()) {
// If what happened was that a civilian unit was captured, that's dealt with in the captureCivilianUnit function
val (whatHappenedIcon, whatHappenedString) = when {
val (battleActionIcon, battleActionString) = when {
attacker !is CityCombatant && attacker.isDefeated() ->
NotificationIcon.War to " was destroyed while attacking"
NotificationIcon.War to "was destroyed while attacking"
!defender.isDefeated() ->
NotificationIcon.War to " has attacked"
NotificationIcon.War to "has attacked"
defender.isCity() && attacker.isMelee() && attacker.getCivInfo().isBarbarian ->
NotificationIcon.War to " has raided"
NotificationIcon.War to "has raided"
defender.isCity() && attacker.isMelee() ->
NotificationIcon.War to " has captured"
NotificationIcon.War to "has captured"
else ->
NotificationIcon.Death to " has destroyed"
NotificationIcon.Death to "has destroyed"
}
val attackerString =
if (attacker.isCity()) "Enemy city [" + attacker.getName() + "]"
else "An enemy [" + attacker.getName() + "]"

val defenderString =
if (defender.isCity())
if (defender.isDefeated() && attacker.isRanged()) " the defence of [" + defender.getName() + "]"
else " [" + defender.getName() + "]"
else " our [" + defender.getName() + "]"

val attackerHurtString = if (damageDealt != null && damageDealt.defenderDealt != 0) " ([-${damageDealt.defenderDealt}] HP)" else ""
val defenderHurtString = if (damageDealt != null) " ([-${damageDealt.attackerDealt}] HP)" else ""
val notificationString = attackerString + attackerHurtString + whatHappenedString + defenderString + defenderHurtString
val notificationString = "[{$attackerString}{$attackerHurtString}] [$battleActionString] [{$defenderString}{$defenderHurtString}]"
val attackerIcon = if (attacker is CityCombatant) NotificationIcon.City else attacker.getName()
val defenderIcon = if (defender is CityCombatant) NotificationIcon.City else defender.getName()
val locations = LocationAction(attackedTile.position, attackerTile?.position)
defender.getCivInfo().addNotification(notificationString, locations, NotificationCategory.War, attackerIcon, whatHappenedIcon, defenderIcon)
defender.getCivInfo().addNotification(notificationString, locations, NotificationCategory.War, attackerIcon, battleActionIcon, defenderIcon)
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/com/unciv/logic/battle/TargetHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object TargetHelper {
if (unit.baseUnit.isMelee() && unit.isEscorting()) {
val escortingUnit = unit.getOtherEscortUnit()!!
if (!escortingUnit.movement.canReachInCurrentTurn(reachableTile)
|| escortingUnit.currentMovement - escortingUnit.movement.getDistanceToTiles()[reachableTile]!!.totalDistance <= 0f)
|| escortingUnit.currentMovement - escortingUnit.movement.getDistanceToTiles()[reachableTile]!!.totalMovement <= 0f)
continue
}

Expand Down Expand Up @@ -76,7 +76,7 @@ object TargetHelper {
val movementPointsToExpendBeforeAttack =
if (tile == unit.currentTile) movementPointsToExpendHere else movementPointsToExpendAfterMovement
val movementLeft =
unit.currentMovement - distance.totalDistance - movementPointsToExpendBeforeAttack
unit.currentMovement - distance.totalMovement - movementPointsToExpendBeforeAttack
Pair(tile, movementLeft)
}
// still got leftover movement points after all that, to attack
Expand Down
22 changes: 11 additions & 11 deletions core/src/com/unciv/logic/map/mapunit/movement/UnitMovement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UnitMovement(val unit: MapUnit) {

private val pathfindingCache = PathfindingCache(unit)

class ParentTileAndTotalDistance(val tile: Tile, val parentTile: Tile, val totalDistance: Float)
class ParentTileAndTotalMovement(val tile: Tile, val parentTile: Tile, val totalMovement: Float)

fun isUnknownTileWeShouldAssumeToBePassable(tile: Tile) = !unit.civ.hasExplored(tile)

Expand All @@ -26,7 +26,7 @@ class UnitMovement(val unit: MapUnit) {
* Does not consider if tiles can actually be entered, use canMoveTo for that.
* If a tile can be reached within the turn, but it cannot be passed through, the total distance to it is set to unitMovement
*/
fun getDistanceToTilesAtPosition(
fun getMovementToTilesAtPosition(
position: Vector2,
unitMovement: Float,
considerZoneOfControl: Boolean = true,
Expand All @@ -40,7 +40,7 @@ class UnitMovement(val unit: MapUnit) {
val currentUnitTile = unit.currentTile
// This is for performance, because this is called all the time
val unitTile = if (position == currentUnitTile.position) currentUnitTile else currentUnitTile.tileMap[position]
distanceToTiles[unitTile] = ParentTileAndTotalDistance(unitTile, unitTile, 0f)
distanceToTiles[unitTile] = ParentTileAndTotalMovement(unitTile, unitTile, 0f)

// If I can't move my only option is to stay...
if (unitMovement == 0f || unit.cache.cannotMove) return distanceToTiles
Expand All @@ -57,7 +57,7 @@ class UnitMovement(val unit: MapUnit) {
if (tilesToIgnore?.contains(neighbor) == true) continue // ignore this tile
var totalDistanceToTile: Float = when {
!neighbor.isExplored(unit.civ) ->
distanceToTiles[tileToCheck]!!.totalDistance + 1f // If we don't know then we just guess it to be 1.
distanceToTiles[tileToCheck]!!.totalMovement + 1f // If we don't know then we just guess it to be 1.
!passThroughCache.getOrPut(neighbor) { canPassThrough(neighbor) } -> unitMovement // Can't go here.
// The reason that we don't just "return" is so that when calculating how to reach an enemy,
// You need to assume his tile is reachable, otherwise all movement algorithms on reaching enemy
Expand All @@ -67,12 +67,12 @@ class UnitMovement(val unit: MapUnit) {
val movementCost = movementCostCache.getOrPut(key) {
MovementCost.getMovementCostBetweenAdjacentTilesEscort(unit, tileToCheck, neighbor, considerZoneOfControl, includeOtherEscortUnit)
}
distanceToTiles[tileToCheck]!!.totalDistance + movementCost
distanceToTiles[tileToCheck]!!.totalMovement + movementCost
}
}

val currentBestPath = distanceToTiles[neighbor]
if (currentBestPath == null || currentBestPath.totalDistance > totalDistanceToTile) { // this is the new best path
if (currentBestPath == null || currentBestPath.totalMovement > totalDistanceToTile) { // this is the new best path
val usableMovement = if (includeOtherEscortUnit && unit.isEscorting())
minOf(unitMovement, unit.getOtherEscortUnit()!!.currentMovement)
else unitMovement
Expand All @@ -84,7 +84,7 @@ class UnitMovement(val unit: MapUnit) {
// In Civ V, you can always travel between adjacent tiles, even if you don't technically
// have enough movement points - it simply depletes what you have

distanceToTiles[neighbor] = ParentTileAndTotalDistance(neighbor, tileToCheck, totalDistanceToTile)
distanceToTiles[neighbor] = ParentTileAndTotalMovement(neighbor, tileToCheck, totalDistanceToTile)
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ class UnitMovement(val unit: MapUnit) {
getDistanceToTiles(true, passThroughCache, movementCostCache) // check cache
}
else {
getDistanceToTilesAtPosition(
getMovementToTilesAtPosition(
tileToCheck.position,
unitMaxMovement,
false,
Expand Down Expand Up @@ -243,7 +243,7 @@ class UnitMovement(val unit: MapUnit) {
in destinationNeighbors -> currentTile // We're right nearby anyway, no need to move
else -> destinationNeighbors
.filter { distanceToTiles.containsKey(it) && canMoveTo(it) }
.minByOrNull { distanceToTiles.getValue(it).totalDistance } // we can get a little closer
.minByOrNull { distanceToTiles.getValue(it).totalMovement } // we can get a little closer
?: currentTile // We can't get closer...
}
}
Expand Down Expand Up @@ -715,7 +715,7 @@ class UnitMovement(val unit: MapUnit) {
// if (cacheResults != null) {
// return cacheResults
// }
val distanceToTiles = getDistanceToTilesAtPosition(
val distanceToTiles = getMovementToTilesAtPosition(
unit.currentTile.position,
unit.currentMovement,
considerZoneOfControl,
Expand Down Expand Up @@ -850,7 +850,7 @@ class PathfindingCache(private val unit: MapUnit) {
}
}

class PathsToTilesWithinTurn : LinkedHashMap<Tile, UnitMovement.ParentTileAndTotalDistance>() {
class PathsToTilesWithinTurn : LinkedHashMap<Tile, UnitMovement.ParentTileAndTotalMovement>() {
fun getPathToTile(tile: Tile): List<Tile> {
if (!containsKey(tile))
throw Exception("Can't reach this tile!")
Expand Down

0 comments on commit e4694ab

Please sign in to comment.