More sequencing, it can only help

This commit is contained in:
Yair Morgenstern 2020-04-20 09:24:38 +03:00
parent 81097fdde9
commit bc7ee18141
6 changed files with 16 additions and 18 deletions

View File

@ -119,9 +119,7 @@ object SpecificUnitAutomation {
luxuryResourcesInCivArea: Sequence<TileResource>): Float { luxuryResourcesInCivArea: Sequence<TileResource>): Float {
val bestTilesFromOuterLayer = tileInfo.getTilesAtDistance(2) val bestTilesFromOuterLayer = tileInfo.getTilesAtDistance(2)
.sortedByDescending { nearbyTileRankings[it] }.take(2) .sortedByDescending { nearbyTileRankings[it] }.take(2)
.toList() val top5Tiles = (tileInfo.neighbors + bestTilesFromOuterLayer)
val top5Tiles = tileInfo.neighbors.union(bestTilesFromOuterLayer)
.asSequence()
.sortedByDescending { nearbyTileRankings[it] } .sortedByDescending { nearbyTileRankings[it] }
.take(5) .take(5)
var rank = top5Tiles.map { nearbyTileRankings.getValue(it) }.sum() var rank = top5Tiles.map { nearbyTileRankings.getValue(it) }.sum()

View File

@ -448,8 +448,8 @@ object Battle {
//assert(fromTile in attTile.neighbors) // function should never be called with attacker not adjacent to defender //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) // 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 } 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 if (candidateTiles.none()) return false // impossible on our map shapes? No - corner of a rectangular map
val toTile = candidateTiles.random() val toTile = candidateTiles.toList().random()
// Now make sure the move is allowed - if not, sorry, bad luck // Now make sure the move is allowed - if not, sorry, bad luck
if (!defender.unit.movement.canMoveTo(toTile)) { // forbid impassable or blocked if (!defender.unit.movement.canMoveTo(toTile)) { // forbid impassable or blocked
val blocker = toTile.militaryUnit val blocker = toTile.militaryUnit

View File

@ -22,9 +22,9 @@ class CityExpansionManager {
} }
fun tilesClaimed(): Int { fun tilesClaimed(): Int {
val tilesAroundCity = cityInfo.getCenterTile().getTilesInDistance(1) val tilesAroundCity = cityInfo.getCenterTile().neighbors
.map { it.position } .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 fun isAreaMaxed(): Boolean = cityInfo.tiles.size >= 90

View File

@ -253,7 +253,7 @@ class MapGenerator(val ruleset: Ruleset) {
.filter { it.resource == null && it.improvement == null .filter { it.resource == null && it.improvement == null
&& wonder.occursOn!!.contains(it.getLastTerrain().name) && wonder.occursOn!!.contains(it.getLastTerrain().name)
&& it.neighbors.all{ it.isWater } } && it.neighbors.all{ it.isWater } }
.random() .toList().random()
location2.naturalWonder = wonder.name location2.naturalWonder = wonder.name
location2.baseTerrain = wonder.turnsInto!! location2.baseTerrain = wonder.turnsInto!!

View File

@ -80,14 +80,14 @@ open class TileInfo {
//region pure functions //region pure functions
/** Returns military, civilian and air units in tile */ /** Returns military, civilian and air units in tile */
fun getUnits(): List<MapUnit> { fun getUnits(): Sequence<MapUnit> {
if(militaryUnit==null && civilianUnit==null && airUnits.isEmpty()) 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<MapUnit>(2) val list = ArrayList<MapUnit>(2)
if(militaryUnit!=null) list.add(militaryUnit!!) if(militaryUnit!=null) list.add(militaryUnit!!)
if(civilianUnit!=null) list.add(civilianUnit!!) if(civilianUnit!=null) list.add(civilianUnit!!)
list.addAll(airUnits) list.addAll(airUnits)
return list return list.asSequence()
} }
fun getCity(): CityInfo? = owningCity 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, // 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! // and the neighbors of a tile never change, it's much more efficient to save the list once and for all!
@delegate:Transient @delegate:Transient
val neighbors: List<TileInfo> by lazy { getTilesAtDistance(1).toList() } val neighbors: Sequence<TileInfo> by lazy { getTilesAtDistance(1) }
fun getHeight(): Int { fun getHeight(): Int {
if (baseTerrain == Constants.mountain) return 4 if (baseTerrain == Constants.mountain) return 4
@ -400,7 +400,7 @@ open class TileInfo {
fun hasEnemySubmarine(viewingCiv:CivilizationInfo): Boolean { fun hasEnemySubmarine(viewingCiv:CivilizationInfo): Boolean {
val unitsInTile = getUnits() val unitsInTile = getUnits()
if (unitsInTile.isEmpty()) return false if (unitsInTile.none()) return false
if (unitsInTile.first().civInfo!=viewingCiv && if (unitsInTile.first().civInfo!=viewingCiv &&
unitsInTile.firstOrNull { it.isInvisible() } != null) { unitsInTile.firstOrNull { it.isInvisible() } != null) {
return true return true

View File

@ -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. 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() val unitsInTile = selectedTile!!.getUnits()
if(previousSelectedCity != null && !previousSelectedCity.attackedThisTurn if (previousSelectedCity != null && !previousSelectedCity.attackedThisTurn
&& selectedTile!!.getTilesInDistance(2).contains(previousSelectedCity.getCenterTile()) && selectedTile!!.getTilesInDistance(2).contains(previousSelectedCity.getCenterTile())
&& unitsInTile.isNotEmpty() && unitsInTile.any()
&& unitsInTile.first().civInfo.isAtWarWith(worldScreen.viewingCiv)){ && unitsInTile.first().civInfo.isAtWarWith(worldScreen.viewingCiv)) {
// try to select the closest city to bombard this guy // try to select the closest city to bombard this guy
unitTable.citySelected(previousSelectedCity) unitTable.citySelected(previousSelectedCity)
} }
@ -216,7 +216,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap
tileGroup.showCircle(Color.RED) tileGroup.showCircle(Color.RED)
val unitsInTile = tileGroup.tileInfo.getUnits() 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) && tileGroup.showMilitaryUnit(viewingCiv)
if(tileGroup.isViewable(viewingCiv) && canSeeEnemy) 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 tileGroup.showCircle(Color.RED) // Display ALL viewable enemies with a red circle so that users don't need to go "hunting" for enemy units