Skip to content

Commit 03698e4

Browse files
authored
Add IHasUniques.era() (yairm210#10593)
* Update IHasUniques.kt * Update IHasUniques.kt * Update IConstruction.kt * Update IHasUniques.kt * Update CivInfoTransientCache.kt * Update UnitUpgradeManager.kt * Update WonderOverviewTab.kt * Update IHasUniques.kt
1 parent 7162011 commit 03698e4

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

core/src/com/unciv/logic/civilization/transients/CivInfoTransientCache.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class CivInfoTransientCache(val civInfo: Civilization) {
4747
val applicableBuildings = ruleset.buildings.values.filter { it.requiresResource(resource, StateForConditionals.IgnoreConditionals) && civInfo.getEquivalentBuilding(it) == it }
4848
val applicableUnits = ruleset.units.values.filter { it.requiresResource(resource, StateForConditionals.IgnoreConditionals) && civInfo.getEquivalentUnit(it) == it }
4949

50-
val lastEraForBuilding = applicableBuildings.maxOfOrNull { ruleset.eras[ruleset.technologies[it.requiredTech]?.era()]?.eraNumber ?: 0 }
51-
val lastEraForUnit = applicableUnits.maxOfOrNull { ruleset.eras[ruleset.technologies[it.requiredTech]?.era()]?.eraNumber ?: 0 }
50+
val lastEraForBuilding = applicableBuildings.maxOfOrNull { it.era(ruleset)?.eraNumber ?: 0 }
51+
val lastEraForUnit = applicableUnits.maxOfOrNull { it.era(ruleset)?.eraNumber ?: 0 }
5252

5353
if (lastEraForBuilding != null)
5454
lastEraResourceUsedForBuilding[resource] = lastEraForBuilding

core/src/com/unciv/logic/map/mapunit/UnitUpgradeManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class UnitUpgradeManager(val unit:MapUnit) {
102102
// do clamping and rounding here so upgrading stepwise costs the same as upgrading far down the chain
103103
var stepCost = constants.base
104104
stepCost += (constants.perProduction * (baseUnit.cost - currentUnit.cost)).coerceAtLeast(0f)
105-
val era = ruleset.eras[ruleset.technologies[baseUnit.requiredTech]?.era()]
105+
val era = baseUnit.era(ruleset)
106106
if (era != null)
107107
stepCost *= (1f + era.eraNumber * constants.eraMultiplier)
108108
stepCost = (stepCost * civModifier).pow(constants.exponent)

core/src/com/unciv/models/ruleset/IConstruction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface INonPerpetualConstruction : IConstruction, INamed, IHasUniques {
3434
@Deprecated("The functionality provided by the requiredTech field is provided by the OnlyAvailableWhen unique.")
3535
var requiredTech: String?
3636

37-
fun requiredTechs(): Sequence<String> = if (requiredTech == null) sequenceOf() else sequenceOf(requiredTech!!)
37+
override fun legacyRequiredTechs(): Sequence<String> = if (requiredTech == null) sequenceOf() else sequenceOf(requiredTech!!)
3838

3939
fun getProductionCost(civInfo: Civilization): Int
4040
fun getStatBuyCost(city: City, stat: Stat): Int?

core/src/com/unciv/models/ruleset/unique/IHasUniques.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.unciv.models.ruleset.unique
22

3+
import com.unciv.models.ruleset.Ruleset
4+
import com.unciv.models.ruleset.tech.Era
5+
import com.unciv.models.ruleset.tech.Technology
36
import com.unciv.models.stats.INamed
47

58
/**
@@ -42,4 +45,22 @@ interface IHasUniques : INamed {
4245

4346
fun hasUnique(uniqueType: UniqueType, stateForConditionals: StateForConditionals? = null) =
4447
getMatchingUniques(uniqueType.placeholderText, stateForConditionals).any()
48+
49+
fun techsRequiredByUniques(): Sequence<String> {
50+
val uniquesForWhenThisIsAvailable: Sequence<Unique> = getMatchingUniques(UniqueType.OnlyAvailableWhen, StateForConditionals.IgnoreConditionals)
51+
val conditionalsForWhenThisIsAvailable: Sequence<Unique> = uniquesForWhenThisIsAvailable.flatMap{ it.conditionals }
52+
val techRequiringConditionalsForWhenThisIsAvailable: Sequence<Unique> = conditionalsForWhenThisIsAvailable.filter{ it.isOfType(UniqueType.ConditionalTech) }
53+
return techRequiringConditionalsForWhenThisIsAvailable.map{ it.params[0] }
54+
}
55+
56+
fun legacyRequiredTechs(): Sequence<String> = sequenceOf()
57+
58+
fun requiredTechs(): Sequence<String> = legacyRequiredTechs() + techsRequiredByUniques()
59+
60+
fun requiredTechnologies(ruleset: Ruleset): Sequence<Technology> =
61+
requiredTechs().map{ ruleset.technologies[it]!! }
62+
63+
fun era(ruleset: Ruleset): Era? =
64+
requiredTechnologies(ruleset).map{ it.era() }.map{ ruleset.eras[it]!! }.maxByOrNull{ it.eraNumber }
65+
// This will return null only if requiredTechnologies() is empty.
4566
}

core/src/com/unciv/ui/screens/overviewscreen/WonderOverviewTab.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class WonderInfo {
178178
val wonderEraMap: Map<String, Era?> =
179179
ruleSet.buildings.values.asSequence()
180180
.filter { it.isWonder }
181-
.associate { it.name to ruleSet.eras[ruleSet.technologies[it.requiredTech]?.era()] }
181+
.associate { it.name to it.era(ruleSet) }
182182

183183
// Maps all World Wonders by their position in sort order to their name
184184
val allWonderMap: Map<Int, String> =

0 commit comments

Comments
 (0)