cityInfo.getMAtchingUniques accepts uniqueType

This commit is contained in:
yairm210 2021-09-19 21:15:26 +03:00
parent ef7676183d
commit bdefb7894c
4 changed files with 31 additions and 15 deletions

View File

@ -711,11 +711,32 @@ class CityInfo {
}
}
// Finds matching uniques provided from both local and non-local sources.
fun getMatchingUniques(
uniqueType: UniqueType,
// We might have this cached to avoid concurrency problems. If we don't, just get it directly
localUniques: Sequence<Unique> = getLocalMatchingUniques(uniqueType),
): Sequence<Unique> {
// The localUniques might not be filtered when passed as a parameter, so we filter it anyway
// The time loss shouldn't be that large I don't think
return civInfo.getMatchingUniques(uniqueType, this) +
localUniques.filter {
it.isOfType(uniqueType)
&& it.params.none { param -> param == "in other cities" }
}
}
// Matching uniques provided by sources in the city itself
fun getLocalMatchingUniques(placeholderText: String): Sequence<Unique> {
return cityConstructions.builtBuildingUniqueMap.getUniques(placeholderText)
.filter { it.params.none { param -> param == "in other cities" } } +
religion.getMatchingUniques(placeholderText)
religion.getUniques().filter { it.placeholderText == placeholderText }
}
fun getLocalMatchingUniques(uniqueType: UniqueType): Sequence<Unique> {
return cityConstructions.builtBuildingUniqueMap.getUniques(uniqueType)
.filter { it.params.none { param -> param == "in other cities" } } +
religion.getUniques().filter { it.isOfType(uniqueType) }
}
// Get all uniques that originate from this city
@ -731,7 +752,7 @@ class CityInfo {
}
fun getMatchingUniquesWithNonLocalEffectsByEnum(uniqueType: UniqueType): Sequence<Unique> {
fun getMatchingUniquesWithNonLocalEffects(uniqueType: UniqueType): Sequence<Unique> {
return cityConstructions.builtBuildingUniqueMap.getUniques(uniqueType)
.filter { it.params.none { param -> param == "in this city" } }
// Note that we don't query religion here, as those only have local effects (for now at least)

View File

@ -63,13 +63,8 @@ class CityInfoReligionManager {
return majorityReligion.getFollowerUniques()
}
fun getMatchingUniques(unique: String): Sequence<Unique> {
return getUniques().filter { it.placeholderText == unique }
}
fun getPressures(): Counter<String> {
return pressures.clone()
}
fun getPressures(): Counter<String> = pressures.clone()
private fun clearAllPressures() {
pressures.clear()

View File

@ -21,7 +21,7 @@ class CivInfoStats(val civInfo: CivilizationInfo) {
private fun getUnitMaintenance(): Int {
val baseUnitCost = 0.5f
var freeUnits = 3
for (unique in civInfo.getMatchingUniquesByEnum(UniqueType.FreeUnits)) {
for (unique in civInfo.getMatchingUniques(UniqueType.FreeUnits)) {
if (!unique.conditionalsApply(civInfo)) continue
freeUnits += unique.params[0].toInt()
}
@ -36,7 +36,7 @@ class CivInfoStats(val civInfo: CivilizationInfo) {
var numberOfUnitsToPayFor = max(0f, unitsToPayFor.count().toFloat() - freeUnits)
for (unique in civInfo.getMatchingUniquesByEnum(UniqueType.UnitMaintenanceDiscount)) {
for (unique in civInfo.getMatchingUniques(UniqueType.UnitMaintenanceDiscount)) {
if (!unique.conditionalsApply(civInfo)) continue
val numberOfUnitsWithDiscount = min(
numberOfUnitsToPayFor,
@ -45,14 +45,14 @@ class CivInfoStats(val civInfo: CivilizationInfo) {
numberOfUnitsToPayFor -= numberOfUnitsWithDiscount * unique.params[0].toFloat() / 100f
}
for (unique in civInfo.getMatchingUniquesByEnum(UniqueType.DecreasedUnitMaintenanceCostsByFilter)) {
for (unique in civInfo.getMatchingUniques(UniqueType.DecreasedUnitMaintenanceCostsByFilter)) {
val numberOfUnitsWithDiscount = min(
numberOfUnitsToPayFor,
unitsToPayFor.count { it.matchesFilter(unique.params[1]) }.toFloat()
)
numberOfUnitsToPayFor -= numberOfUnitsWithDiscount * unique.params[0].toFloat() / 100f
}
for (unique in civInfo.getMatchingUniquesByEnum(UniqueType.DecreasedUnitMaintenanceCostsGlobally)) {
for (unique in civInfo.getMatchingUniques(UniqueType.DecreasedUnitMaintenanceCostsGlobally)) {
numberOfUnitsToPayFor *= 1f - unique.params[0].toFloat() / 100f
}

View File

@ -317,11 +317,11 @@ class CivilizationInfo {
fun hasUnique(unique: String) = getMatchingUniques(unique).any()
/** Destined to replace getMatchingUniques, gradually, as we fill the enum */
fun getMatchingUniquesByEnum(uniqueType: UniqueType, cityToIgnore: CityInfo?=null): Sequence<Unique> {
fun getMatchingUniques(uniqueType: UniqueType, cityToIgnore: CityInfo?=null): Sequence<Unique> {
val ruleset = gameInfo.ruleSet
return nation.uniqueObjects.asSequence().filter { it.matches(uniqueType, ruleset) } +
cities.asSequence().filter { it != cityToIgnore }.flatMap { city ->
city.getMatchingUniquesWithNonLocalEffectsByEnum(uniqueType)
city.getMatchingUniquesWithNonLocalEffects(uniqueType)
} +
policies.policyUniques.getUniques(uniqueType) +
tech.techUniques.getUniques(uniqueType) +