Skip to content

Commit

Permalink
Pathfinding: Avoid unfriendly city state tiles when this doesn't affe…
Browse files Browse the repository at this point in the history
…ct movement speed - #12718
  • Loading branch information
yairm210 committed Dec 29, 2024
1 parent 905ef25 commit d23f32d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions core/src/com/unciv/logic/map/mapunit/movement/UnitMovement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.unciv.logic.map.mapunit.movement

import com.badlogic.gdx.math.Vector2
import com.unciv.Constants
import com.unciv.logic.civilization.diplomacy.RelationshipLevel
import com.unciv.logic.map.BFS
import com.unciv.logic.map.mapunit.MapUnit
import com.unciv.logic.map.tile.Tile
Expand Down Expand Up @@ -138,11 +139,16 @@ class UnitMovement(val unit: MapUnit) {

while (true) {
newTilesToCheck.clear()

var tilesByPreference = tilesToCheck.sortedBy { it.aerialDistanceTo(destination) }
// Avoid embarkation when possible
if (unit.type.isLandUnit()) tilesByPreference = tilesByPreference.sortedByDescending { it.isLand }

fun isUnfriendlyCityState(tile:Tile): Boolean = tile.getOwner().let { it != null && it.isCityState
&& it.getDiplomacyManager(unit.civ)?.isRelationshipLevelLT(RelationshipLevel.Friend) == true }

// When comparing booleans, we get false first, so we need to negate the isLand / isCityState checks
// By order of preference: 1. Land tiles 2. Aerial distance 3. Not city states
val comparison: Comparator<Tile> = if (unit.type.isLandUnit())
compareBy({!it.isLand}, {it.aerialDistanceTo(destination)}, ::isUnfriendlyCityState)
else compareBy({it.aerialDistanceTo(destination)}, ::isUnfriendlyCityState)

val tilesByPreference = tilesToCheck.sortedWith(comparison)

for (tileToCheck in tilesByPreference) {
val distanceToTilesThisTurn = if (distance == 1) {
Expand Down

0 comments on commit d23f32d

Please sign in to comment.