CivilizationInfo.getUnits is now a sequence, for better performance throughout

This commit is contained in:
Yair Morgenstern 2020-01-26 21:22:58 +02:00
parent 66b5263509
commit 7339c43c0d
7 changed files with 22 additions and 20 deletions

View file

@ -146,7 +146,7 @@ class GameInfo {
}
val totalBarbariansAllowedOnMap = encampments.size * 3
var extraBarbarians = totalBarbariansAllowedOnMap - getBarbarianCivilization().getCivUnits().size
var extraBarbarians = totalBarbariansAllowedOnMap - getBarbarianCivilization().getCivUnits().count()
for (tile in tileMap.values.filter { it.improvement == Constants.barbarianEncampment }) {
if (extraBarbarians <= 0) break

View file

@ -24,8 +24,8 @@ class ConstructionAutomation(val cityConstructions: CityConstructions){
.filter { it.isWonder || it.isNationalWonder }
val civUnits = civInfo.getCivUnits()
val militaryUnits = civUnits.filter { !it.type.isCivilian()}.size
val workers = civUnits.filter { it.name == Constants.worker }.size.toFloat()
val militaryUnits = civUnits.filter { !it.type.isCivilian()}.count()
val workers = civUnits.filter { it.name == Constants.worker }.count().toFloat()
val cities = civInfo.cities.size
val canBuildWorkboat = cityInfo.cityConstructions.getConstructableUnits().map { it.name }.contains("Work Boats")
&& !cityInfo.getTiles().any { it.civilianUnit?.name == "Work Boats" }

View file

@ -391,7 +391,7 @@ class NextTurnAutomation{
enemy.tradeRequests.add(TradeRequest(civInfo.civName, tradeLogic.currentTrade.reverse()))
else {
if (enemy.victoryType()!=VictoryType.Cultural
&& enemy.getCivUnits().filter { !it.type.isCivilian() }.size > enemy.cities.size
&& enemy.getCivUnits().filter { !it.type.isCivilian() }.count() > enemy.cities.size
&& enemy.getHappiness() > 0) {
continue //enemy AI has too large army and happiness. It continues to fight for profit.
}
@ -425,7 +425,7 @@ class NextTurnAutomation{
if (civInfo.cities.isEmpty() || civInfo.diplomacy.isEmpty()) return
if (civInfo.isAtWar() || civInfo.getHappiness() <= 0) return
val ourMilitaryUnits = civInfo.getCivUnits().filter { !it.type.isCivilian() }.size
val ourMilitaryUnits = civInfo.getCivUnits().filter { !it.type.isCivilian() }.count()
if (ourMilitaryUnits < civInfo.cities.size) return
//evaluate war

View file

@ -193,7 +193,7 @@ class CivilizationInfo {
//region Units
fun getCivUnits(): List<MapUnit> = units
fun getCivUnits(): Sequence<MapUnit> = units.asSequence()
fun addUnit(mapUnit: MapUnit, updateCivInfo:Boolean=true){
val newList = ArrayList(units)
@ -220,12 +220,12 @@ class CivilizationInfo {
fun getDueUnits() = getCivUnits().filter { it.due && it.isIdle() }
fun shouldGoToDueUnit() = UncivGame.Current.settings.checkForDueUnits && getDueUnits().isNotEmpty()
fun shouldGoToDueUnit() = UncivGame.Current.settings.checkForDueUnits && getDueUnits().any()
fun getNextDueUnit(): MapUnit? {
val dueUnits = getDueUnits()
if(dueUnits.isNotEmpty()) {
val unit = dueUnits[0]
if(dueUnits.any()) {
val unit = dueUnits.first()
unit.due = false
return unit
}
@ -407,7 +407,7 @@ class CivilizationInfo {
if (!isBarbarian() && gold < -100 && nextTurnStats.gold.toInt() < 0) {
for (i in 1 until (gold / -100)) {
var civMilitaryUnits = getCivUnits().filter { !it.type.isCivilian() }
if (civMilitaryUnits.isNotEmpty()) {
if (civMilitaryUnits.any()) {
val unitToDisband = civMilitaryUnits.first()
unitToDisband.destroy()
civMilitaryUnits -= unitToDisband

View file

@ -553,9 +553,11 @@ class MapUnit {
}
fun canIntercept(attackedTile: TileInfo): Boolean {
return interceptChance()!=0
&& (attacksThisTurn==0 || hasUnique("1 extra Interception may be made per turn") && attacksThisTurn<2)
&& currentTile.arialDistanceTo(attackedTile) <= baseUnit.interceptRange
if(attacksThisTurn>1) return false
if(interceptChance()==0) return false
if(attacksThisTurn>0 && !hasUnique("1 extra Interception may be made per turn")) return false
if(currentTile.arialDistanceTo(attackedTile) > baseUnit.interceptRange) return false
return true
}
fun interceptChance():Int{

View file

@ -110,7 +110,7 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
val tileToCenterOn: Vector2 =
when {
viewingCiv.cities.isNotEmpty() -> viewingCiv.getCapital().location
viewingCiv.getCivUnits().isNotEmpty() -> viewingCiv.getCivUnits().first().getTile().position
viewingCiv.getCivUnits().any() -> viewingCiv.getCivUnits().first().getTile().position
else -> Vector2.Zero
}
mapHolder.setCenterPosition(tileToCenterOn,true)

View file

@ -17,7 +17,7 @@ class IdleUnitButton (
val image = ImageGetter.getImage("OtherIcons/BackArrow")
fun hasIdleUnits() = unitTable.worldScreen.viewingCiv.getIdleUnits().isNotEmpty()
fun hasIdleUnits() = unitTable.worldScreen.viewingCiv.getIdleUnits().any()
init {
val imageSize = 25f
@ -31,17 +31,17 @@ class IdleUnitButton (
onClick {
val idleUnits = unitTable.worldScreen.viewingCiv.getIdleUnits()
if(idleUnits.isEmpty()) return@onClick
if(idleUnits.none()) return@onClick
val unitToSelect: MapUnit
if (unitTable.selectedUnit==null || !idleUnits.contains(unitTable.selectedUnit!!))
unitToSelect = idleUnits[0]
unitToSelect = idleUnits.first()
else {
var index = idleUnits.indexOf(unitTable.selectedUnit!!)
if(previous) index-- else index++
index += idleUnits.size
index %= idleUnits.size // for looping
unitToSelect = idleUnits[index]
index += idleUnits.count()
index %= idleUnits.count() // for looping
unitToSelect = idleUnits.elementAt(index)
}
unitToSelect.due = false