More cityinfo cleanup

This commit is contained in:
Yair Morgenstern
2023-01-18 21:53:00 +02:00
parent 6dd0bf51c7
commit 9caef68866
4 changed files with 23 additions and 20 deletions

View File

@ -22,7 +22,6 @@ import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.models.ruleset.unit.BaseUnit
import com.unciv.models.stats.Stat
import java.util.*
import kotlin.math.ceil
import kotlin.math.roundToInt
enum class CityFlags {
@ -252,21 +251,6 @@ class CityInfo : IsPartOfGameInfoSerialization {
fun foodForNextTurn() = cityStats.currentCityStats.food.roundToInt()
/** Take null to mean infinity. */
fun getNumTurnsToNewPopulation(): Int? {
if (!isGrowing()) return null
val roundedFoodPerTurn = foodForNextTurn().toFloat()
val remainingFood = population.getFoodToNextPopulation() - population.foodStored
var turnsToGrowth = ceil(remainingFood / roundedFoodPerTurn).toInt()
if (turnsToGrowth < 1) turnsToGrowth = 1
return turnsToGrowth
}
/** Take null to mean infinity. */
fun getNumTurnsToStarvation(): Int? {
if (!isStarving()) return null
return population.foodStored / -foodForNextTurn() + 1
}
fun containsBuildingUnique(uniqueType: UniqueType) =
cityConstructions.getBuiltBuildings().flatMap { it.uniqueObjects }.any { it.isOfType(uniqueType) }

View File

@ -12,6 +12,7 @@ import com.unciv.models.stats.Stat
import com.unciv.ui.utils.extensions.toPercent
import com.unciv.ui.utils.extensions.withItem
import com.unciv.ui.utils.extensions.withoutItem
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.pow
@ -58,6 +59,24 @@ class CityPopulationManager : IsPartOfGameInfoSerialization {
return foodRequired.toInt()
}
/** Take null to mean infinity. */
fun getNumTurnsToStarvation(): Int? {
if (!cityInfo.isStarving()) return null
return foodStored / -cityInfo.foodForNextTurn() + 1
}
/** Take null to mean infinity. */
fun getNumTurnsToNewPopulation(): Int? {
if (!cityInfo.isGrowing()) return null
val roundedFoodPerTurn = cityInfo.foodForNextTurn().toFloat()
val remainingFood = getFoodToNextPopulation() - foodStored
var turnsToGrowth = ceil(remainingFood / roundedFoodPerTurn).toInt()
if (turnsToGrowth < 1) turnsToGrowth = 1
return turnsToGrowth
}
//endregion
/** Implements [UniqueParameterType.PopulationFilter][com.unciv.models.ruleset.unique.UniqueParameterType.PopulationFilter] */

View File

@ -158,11 +158,11 @@ class CityStatsTable(val cityScreen: CityScreen): Table() {
var turnsToPopString =
when {
cityInfo.isStarving() -> "[${cityInfo.getNumTurnsToStarvation()}] turns to lose population"
cityInfo.isStarving() -> "[${cityInfo.population.getNumTurnsToStarvation()}] turns to lose population"
cityInfo.getRuleset().units[cityInfo.cityConstructions.currentConstructionFromQueue]
.let { it != null && it.hasUnique(UniqueType.ConvertFoodToProductionWhenConstructed) }
-> "Food converts to production"
cityInfo.isGrowing() -> "[${cityInfo.getNumTurnsToNewPopulation()}] turns to new population"
cityInfo.isGrowing() -> "[${cityInfo.population.getNumTurnsToNewPopulation()}] turns to new population"
else -> "Stopped population growth"
}.tr()
turnsToPopString += " (${cityInfo.population.foodStored}${Fonts.food}/${cityInfo.population.getFoodToNextPopulation()}${Fonts.food})"

View File

@ -403,11 +403,11 @@ class CityButton(val city: CityInfo, private val tileGroup: WorldTileGroup): Tab
val turnLabelText = when {
city.isGrowing() -> {
val turnsToGrowth = city.getNumTurnsToNewPopulation()
val turnsToGrowth = city.population.getNumTurnsToNewPopulation()
if (turnsToGrowth != null && turnsToGrowth < 100) turnsToGrowth.toString() else ""
}
city.isStarving() -> {
val turnsToStarvation = city.getNumTurnsToStarvation()
val turnsToStarvation = city.population.getNumTurnsToStarvation()
if (turnsToStarvation != null && turnsToStarvation < 100) turnsToStarvation.toString() else ""
}
else -> ""