From a91dca09303711b7dd0576582b889938b622d7cc Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Sun, 21 May 2023 11:08:03 +0200 Subject: [PATCH] Fix off-by-one error in autoAssignPopulation (#9411) * Fix off-by-one error in autoAssignPopulation * Fix more off-by-one errors from for-repeat conversions * Linting: Use Actions.forever shortcut where appropriate --- .../civilization/NextTurnAutomation.kt | 2 +- .../city/managers/CityPopulationManager.kt | 33 ++++++++----------- .../unciv/logic/map/mapgenerator/Perlin.kt | 2 +- .../savescreens/VerticalFileListScrollPane.kt | 2 +- .../bottombar/BattleTableHelpers.kt | 6 ++-- .../status/MultiplayerStatusButton.kt | 2 +- 6 files changed, 21 insertions(+), 26 deletions(-) diff --git a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt index 39c53f35af..6c885de59e 100644 --- a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt @@ -688,7 +688,7 @@ object NextTurnAutomation { // not have used a great prophet to found/enhance our religion. for (belief in BeliefType.values()) { if (belief == BeliefType.None) continue - repeat((beliefsToChoose[belief] ?: 0) - 1) { + repeat(beliefsToChoose[belief] ?: 0) { chosenBeliefs.add( chooseBeliefOfType(civInfo, belief, chosenBeliefs) ?: return@repeat ) diff --git a/core/src/com/unciv/logic/city/managers/CityPopulationManager.kt b/core/src/com/unciv/logic/city/managers/CityPopulationManager.kt index c9a636e856..f8e15fd412 100644 --- a/core/src/com/unciv/logic/city/managers/CityPopulationManager.kt +++ b/core/src/com/unciv/logic/city/managers/CityPopulationManager.kt @@ -154,42 +154,37 @@ class CityPopulationManager : IsPartOfGameInfoSerialization { specialistFoodBonus *= unique.params[0].toPercent() specialistFoodBonus = 2f - specialistFoodBonus - val currentCiv = city.civ - - val tilesToEvaluate = city.getCenterTile().getTilesInDistance(3) - .filter { it.getOwner() == currentCiv && !it.isBlockaded() }.toList().asSequence() + val tilesToEvaluate = city.getWorkableTiles() + .filter { !it.isBlockaded() }.toList().asSequence() val localUniqueCache = LocalUniqueCache() - repeat(getFreePopulation() - 1) { + repeat(getFreePopulation()) { //evaluate tiles - val (bestTile, valueBestTile) = tilesToEvaluate + val bestTileAndRank = tilesToEvaluate .filterNot { it.providesYield() } .associateWith { Automation.rankTileForCityWork(it, city, cityStats, localUniqueCache) } .maxByOrNull { it.value } - ?: object : Map.Entry { - override val key: Tile? = null - override val value = 0f - } + val bestTile = bestTileAndRank?.key + val valueBestTile = bestTileAndRank?.value ?: 0f - val bestJob: String? = if (city.manualSpecialists) null else getMaxSpecialists() + val bestJobAndRank = if (city.manualSpecialists) null + else getMaxSpecialists().asSequence() .filter { specialistAllocations[it.key]!! < it.value } .map { it.key } - .maxByOrNull { Automation.rankSpecialist(it, city, cityStats, localUniqueCache) } - - var valueBestSpecialist = 0f - if (bestJob != null) { - valueBestSpecialist = Automation.rankSpecialist(bestJob, city, cityStats, localUniqueCache) - } + .associateWith { Automation.rankSpecialist(it, city, cityStats, localUniqueCache) } + .maxByOrNull { it.value } + val bestJob = bestJobAndRank?.key + val valueBestSpecialist = bestJobAndRank?.value ?: 0f //assign population if (valueBestTile > valueBestSpecialist) { if (bestTile != null) { city.workedTiles = city.workedTiles.withItem(bestTile.position) - cityStats[Stat.Food] += bestTile.stats.getTileStats(city, city.civ, localUniqueCache)[Stat.Food] + cityStats.food += bestTile.stats.getTileStats(city, city.civ, localUniqueCache).food } } else if (bestJob != null) { specialistAllocations.add(bestJob, 1) - cityStats[Stat.Food] += specialistFoodBonus + cityStats.food += specialistFoodBonus } } city.cityStats.update() diff --git a/core/src/com/unciv/logic/map/mapgenerator/Perlin.kt b/core/src/com/unciv/logic/map/mapgenerator/Perlin.kt index a65b24024a..9cd92242c2 100644 --- a/core/src/com/unciv/logic/map/mapgenerator/Perlin.kt +++ b/core/src/com/unciv/logic/map/mapgenerator/Perlin.kt @@ -46,7 +46,7 @@ object Perlin { var amp = 1.0 var max = 0.0 var total = 0.0 - repeat(nOctaves - 1) { + repeat(nOctaves) { total += amp * noise(x * freq / scale, y * freq / scale, z * freq / scale) max += amp freq *= lacunarity diff --git a/core/src/com/unciv/ui/screens/savescreens/VerticalFileListScrollPane.kt b/core/src/com/unciv/ui/screens/savescreens/VerticalFileListScrollPane.kt index b5b5f9bb23..1a6136d6a8 100644 --- a/core/src/com/unciv/ui/screens/savescreens/VerticalFileListScrollPane.kt +++ b/core/src/com/unciv/ui/screens/savescreens/VerticalFileListScrollPane.kt @@ -60,7 +60,7 @@ class VerticalFileListScrollPane( val loadImage = ImageGetter.getImage("OtherIcons/Load") loadImage.setSize(50f, 50f) // So the origin sets correctly loadImage.setOrigin(Align.center) - val loadAnimation = Actions.repeat(Int.MAX_VALUE, Actions.rotateBy(360f, 2f)) + val loadAnimation = Actions.forever(Actions.rotateBy(360f, 2f)) loadImage.addAction(loadAnimation) existingSavesTable.add(loadImage).size(50f).center() diff --git a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt index b6b699a2b3..e9ff87be67 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt @@ -237,11 +237,11 @@ object BattleTableHelpers { val damagedHealth = ImageGetter.getDot(Color.FIREBRICK) if (UncivGame.Current.settings.continuousRendering) { - damagedHealth.addAction(Actions.repeat( - RepeatAction.FOREVER, Actions.sequence( + damagedHealth.addAction(Actions.forever(Actions.sequence( Actions.color(Color.BLACK, 0.7f), Actions.color(Color.FIREBRICK, 0.7f) - ))) } + ))) + } val maybeDamagedHealth = ImageGetter.getDot(Color.ORANGE) diff --git a/core/src/com/unciv/ui/screens/worldscreen/status/MultiplayerStatusButton.kt b/core/src/com/unciv/ui/screens/worldscreen/status/MultiplayerStatusButton.kt index eac712d2fe..bb41cc6dc2 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/status/MultiplayerStatusButton.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/status/MultiplayerStatusButton.kt @@ -82,7 +82,7 @@ class MultiplayerStatusButton( if (UncivGame.Current.settings.continuousRendering) { loadingImage.clearActions() - loadingImage.addAction(Actions.repeat(RepeatAction.FOREVER,Actions.rotateBy(-90f, 1f))) + loadingImage.addAction(Actions.forever(Actions.rotateBy(-90f, 1f))) } loadingImage.isVisible = true