diff --git a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt index 9df5e4b67b..c9586dcbe9 100644 --- a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt +++ b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt @@ -113,7 +113,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) { } /** - * Does not consider if the destination tile can actually be entered, use canMoveTo for that. + * Does not consider if the [destination] tile can actually be entered, use [canMoveTo] for that. * Returns an empty list if there's no way to get to the destination. */ fun getShortestPath(destination: TileInfo): List { @@ -152,7 +152,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) { val path = mutableListOf(destination) // Traverse the tree upwards to get the list of tiles leading to the destination, // Get the tile from which the distance to the final tile in least - // this is so that when we finally get there, we'll have as many movement points as possible - var intermediateTile = distanceToDestination.minBy { it.value }!!.key + var intermediateTile = distanceToDestination.minByOrNull { it.value }!!.key while (intermediateTile != currentTile) { path.add(intermediateTile) intermediateTile = movementTreeParents[intermediateTile]!! diff --git a/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt b/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt index 82904dfd23..0dcf759102 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt @@ -44,6 +44,8 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap private val unitActionOverlays: ArrayList = ArrayList() + private val unitMovementPaths: HashMap> = HashMap() + init { if (Gdx.app.type == Application.ApplicationType.Desktop) this.setFlingTime(0f) continuousScrollingX = tileMap.mapParameters.worldWrap @@ -135,6 +137,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap private fun onTileClicked(tileInfo: TileInfo) { removeUnitActionOverlay() selectedTile = tileInfo + unitMovementPaths.clear() val unitTable = worldScreen.bottomUnitTable val previousSelectedUnits = unitTable.selectedUnits.toList() // create copy @@ -160,6 +163,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap else { // this can take a long time, because of the unit-to-tile calculation needed, so we put it in a different thread addTileOverlaysWithUnitMovement(previousSelectedUnits, tileInfo) + } } else addTileOverlays(tileInfo) // no unit movement but display the units in the tile etc. @@ -251,14 +255,19 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap val unitToTurnsToTile = HashMap() for (unit in selectedUnits) { + val shortestPath = ArrayList() val turnsToGetThere = if (unit.type.isAirUnit()) { if (unit.movement.canReach(tileInfo)) 1 else 0 } else if (unit.action == Constants.unitActionParadrop) { if (unit.movement.canReach(tileInfo)) 1 else 0 - } else - unit.movement.getShortestPath(tileInfo).size // this is what takes the most time, tbh + } else { + // this is the most time-consuming call + shortestPath.addAll(unit.movement.getShortestPath(tileInfo)) + shortestPath.size + } + unitMovementPaths[unit] = shortestPath unitToTurnsToTile[unit] = turnsToGetThere } @@ -518,6 +527,14 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap } } + // Movement paths + if (unitMovementPaths.containsKey(unit)) { + for (tile in unitMovementPaths[unit]!!) { + for (tileToColor in tileGroups[tile]!!) + tileToColor.showCircle(Color.SKY, 0.8f) + } + } + if(unit.isMoving()) { val destinationTileGroups = tileGroups[unit.getMovementDestination()]!! for (tileGroup in destinationTileGroups)