diff --git a/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt b/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt index 5f81aea1ef..af890c4a74 100644 --- a/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/SpecificUnitAutomation.kt @@ -119,9 +119,7 @@ object SpecificUnitAutomation { luxuryResourcesInCivArea: Sequence): Float { val bestTilesFromOuterLayer = tileInfo.getTilesAtDistance(2) .sortedByDescending { nearbyTileRankings[it] }.take(2) - .toList() - val top5Tiles = tileInfo.neighbors.union(bestTilesFromOuterLayer) - .asSequence() + val top5Tiles = (tileInfo.neighbors + bestTilesFromOuterLayer) .sortedByDescending { nearbyTileRankings[it] } .take(5) var rank = top5Tiles.map { nearbyTileRankings.getValue(it) }.sum() diff --git a/core/src/com/unciv/logic/battle/Battle.kt b/core/src/com/unciv/logic/battle/Battle.kt index 9f94ac4f7f..5562fb10f5 100644 --- a/core/src/com/unciv/logic/battle/Battle.kt +++ b/core/src/com/unciv/logic/battle/Battle.kt @@ -448,8 +448,8 @@ object Battle { //assert(fromTile in attTile.neighbors) // function should never be called with attacker not adjacent to defender // the following yields almost always exactly three tiles in a half-moon shape (exception: edge of map) val candidateTiles = fromTile.neighbors.filterNot { it == attTile || it in attTile.neighbors } - if (candidateTiles.isEmpty()) return false // impossible on our map shapes? No - corner of a rectangular map - val toTile = candidateTiles.random() + if (candidateTiles.none()) return false // impossible on our map shapes? No - corner of a rectangular map + val toTile = candidateTiles.toList().random() // Now make sure the move is allowed - if not, sorry, bad luck if (!defender.unit.movement.canMoveTo(toTile)) { // forbid impassable or blocked val blocker = toTile.militaryUnit diff --git a/core/src/com/unciv/logic/city/CityExpansionManager.kt b/core/src/com/unciv/logic/city/CityExpansionManager.kt index fdea563778..24b52b99eb 100644 --- a/core/src/com/unciv/logic/city/CityExpansionManager.kt +++ b/core/src/com/unciv/logic/city/CityExpansionManager.kt @@ -22,9 +22,9 @@ class CityExpansionManager { } fun tilesClaimed(): Int { - val tilesAroundCity = cityInfo.getCenterTile().getTilesInDistance(1) + val tilesAroundCity = cityInfo.getCenterTile().neighbors .map { it.position } - return cityInfo.tiles.filterNot { it in tilesAroundCity }.size + return cityInfo.tiles.count { it != cityInfo.location && it !in tilesAroundCity} } fun isAreaMaxed(): Boolean = cityInfo.tiles.size >= 90 diff --git a/core/src/com/unciv/logic/map/MapGenerator.kt b/core/src/com/unciv/logic/map/MapGenerator.kt index 879794d572..96e7a89b54 100644 --- a/core/src/com/unciv/logic/map/MapGenerator.kt +++ b/core/src/com/unciv/logic/map/MapGenerator.kt @@ -253,7 +253,7 @@ class MapGenerator(val ruleset: Ruleset) { .filter { it.resource == null && it.improvement == null && wonder.occursOn!!.contains(it.getLastTerrain().name) && it.neighbors.all{ it.isWater } } - .random() + .toList().random() location2.naturalWonder = wonder.name location2.baseTerrain = wonder.turnsInto!! diff --git a/core/src/com/unciv/logic/map/TileInfo.kt b/core/src/com/unciv/logic/map/TileInfo.kt index fa1e7732b5..5944e596e8 100644 --- a/core/src/com/unciv/logic/map/TileInfo.kt +++ b/core/src/com/unciv/logic/map/TileInfo.kt @@ -80,14 +80,14 @@ open class TileInfo { //region pure functions /** Returns military, civilian and air units in tile */ - fun getUnits(): List { + fun getUnits(): Sequence { if(militaryUnit==null && civilianUnit==null && airUnits.isEmpty()) - return emptyList() // for performance reasons - costs much less to initialize than an empty ArrayList or list() + return emptySequence() // for performance reasons - costs much less to initialize than an empty ArrayList or list() val list = ArrayList(2) if(militaryUnit!=null) list.add(militaryUnit!!) if(civilianUnit!=null) list.add(civilianUnit!!) list.addAll(airUnits) - return list + return list.asSequence() } fun getCity(): CityInfo? = owningCity @@ -115,7 +115,7 @@ open class TileInfo { // This is for performance - since we access the neighbors of a tile ALL THE TIME, // and the neighbors of a tile never change, it's much more efficient to save the list once and for all! @delegate:Transient - val neighbors: List by lazy { getTilesAtDistance(1).toList() } + val neighbors: Sequence by lazy { getTilesAtDistance(1) } fun getHeight(): Int { if (baseTerrain == Constants.mountain) return 4 @@ -400,7 +400,7 @@ open class TileInfo { fun hasEnemySubmarine(viewingCiv:CivilizationInfo): Boolean { val unitsInTile = getUnits() - if (unitsInTile.isEmpty()) return false + if (unitsInTile.none()) return false if (unitsInTile.first().civInfo!=viewingCiv && unitsInTile.firstOrNull { it.isInvisible() } != null) { return true diff --git a/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt b/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt index 2bcca6c364..a0bda82947 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldMapHolder.kt @@ -78,12 +78,12 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap else addTileOverlays(tileInfo) // no unit movement but display the units in the tile etc. - if(newSelectedUnit==null || newSelectedUnit.type==UnitType.Civilian){ + if(newSelectedUnit==null || newSelectedUnit.type==UnitType.Civilian) { val unitsInTile = selectedTile!!.getUnits() - if(previousSelectedCity != null && !previousSelectedCity.attackedThisTurn + if (previousSelectedCity != null && !previousSelectedCity.attackedThisTurn && selectedTile!!.getTilesInDistance(2).contains(previousSelectedCity.getCenterTile()) - && unitsInTile.isNotEmpty() - && unitsInTile.first().civInfo.isAtWarWith(worldScreen.viewingCiv)){ + && unitsInTile.any() + && unitsInTile.first().civInfo.isAtWarWith(worldScreen.viewingCiv)) { // try to select the closest city to bombard this guy unitTable.citySelected(previousSelectedCity) } @@ -216,7 +216,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap tileGroup.showCircle(Color.RED) val unitsInTile = tileGroup.tileInfo.getUnits() - val canSeeEnemy = unitsInTile.isNotEmpty() && unitsInTile.first().civInfo.isAtWarWith(viewingCiv) + val canSeeEnemy = unitsInTile.any() && unitsInTile.first().civInfo.isAtWarWith(viewingCiv) && tileGroup.showMilitaryUnit(viewingCiv) if(tileGroup.isViewable(viewingCiv) && canSeeEnemy) tileGroup.showCircle(Color.RED) // Display ALL viewable enemies with a red circle so that users don't need to go "hunting" for enemy units