Fixed bug where units could still be purchased if they used a depleted resource (#4798)

This commit is contained in:
Xander Lenstra 2021-08-07 22:44:53 +02:00 committed by GitHub
parent 0c1a21f6ae
commit 6d5fe2ff7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 14 deletions

View File

@ -365,12 +365,11 @@ class CityConstructions {
val inProgressSnapshot = inProgressConstructions.keys.filter { it != currentConstructionFromQueue }
for (constructionName in inProgressSnapshot) {
val construction = getConstruction(constructionName)
val rejectionReason: String =
when (construction) {
is Building -> construction.getRejectionReason(this)
is BaseUnit -> construction.getRejectionReason(this)
else -> ""
}
// Perpetual constructions should always still be valid (I hope)
if (construction is PerpetualConstruction) continue
val rejectionReason =
(construction as INonPerpetualConstruction).getRejectionReason(this)
if (rejectionReason.endsWith("lready built")
|| rejectionReason.startsWith("Cannot be built with")

View File

@ -22,6 +22,7 @@ interface INonPerpetualConstruction : IConstruction, INamed {
fun getProductionCost(civInfo: CivilizationInfo): Int
fun getStatBuyCost(cityInfo: CityInfo, stat: Stat): Int?
fun getRejectionReason(cityConstructions: CityConstructions): String
private fun getMatchingUniques(uniqueTemplate: String): Sequence<Unique> {
return uniqueObjects.asSequence().filter { it.placeholderText == uniqueTemplate }
@ -41,6 +42,13 @@ interface INonPerpetualConstruction : IConstruction, INamed {
) return true
return false
}
/** Checks if the construction should be purchasable, not whether it can be bought with a stat at all */
fun isPurchasable(cityConstructions: CityConstructions): Boolean {
val rejectionReason = getRejectionReason(cityConstructions)
return rejectionReason == ""
|| rejectionReason == "Can only be purchased"
}
fun canBePurchasedWithAnyStat(cityInfo: CityInfo): Boolean {
return Stat.values().any { canBePurchasedWithStat(cityInfo, it) }
@ -107,7 +115,7 @@ open class PerpetualConstruction(override var name: String, val description: Str
override fun isBuildable(cityConstructions: CityConstructions): Boolean =
throw Exception("Impossible!")
override fun postBuildEvent(cityConstructions: CityConstructions, wasBought: Boolean) =
throw Exception("Impossible!")

View File

@ -392,7 +392,7 @@ class Building : NamedStats(), INonPerpetualConstruction, ICivilopediaText {
|| rejectionReason == "Can only be purchased"
}
fun getRejectionReason(construction: CityConstructions): String {
override fun getRejectionReason(construction: CityConstructions): String {
if (construction.isBuilt(name)) return "Already built"
// for buildings that are created as side effects of other things, and not directly built
// unless they can be bought with faith

View File

@ -240,7 +240,7 @@ class BaseUnit : INamed, INonPerpetualConstruction, CivilopediaText() {
|| rejectionReason == "Can only be purchased"
}
fun getRejectionReason(cityConstructions: CityConstructions): String {
override fun getRejectionReason(cityConstructions: CityConstructions): String {
if (isWaterUnit() && !cityConstructions.cityInfo.isCoastal())
return "Can only build water units in coastal cities"
val civInfo = cityConstructions.cityInfo.civInfo

View File

@ -429,11 +429,12 @@ class CityConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBase
}
if (!cityScreen.canChangeState
|| city.isPuppet
|| city.isInResistance()
|| !city.canPurchase(construction)
|| (constructionBuyCost > city.getStatReserve(stat) && !city.civInfo.gameInfo.gameParameters.godMode))
button.disable()
|| !construction.isPurchasable(city.cityConstructions)
|| city.isPuppet
|| city.isInResistance()
|| !city.canPurchase(construction)
|| (constructionBuyCost > city.getStatReserve(stat) && !city.civInfo.gameInfo.gameParameters.godMode)
) button.disable()
}
button.labelCell.pad(5f)