A few quick wins for performance

This commit is contained in:
Yair Morgenstern 2022-01-26 23:49:21 +02:00
parent 6855a52466
commit 6a969b98f5
4 changed files with 8 additions and 31 deletions

View File

@ -27,16 +27,16 @@ interface INonPerpetualConstruction : IConstruction, INamed, IHasUniques {
fun postBuildEvent(cityConstructions: CityConstructions, boughtWith: Stat? = null): Boolean // Yes I'm hilarious.
fun canBePurchasedWithStat(cityInfo: CityInfo?, stat: Stat): Boolean {
if (stat in listOf(Stat.Production, Stat.Happiness)) return false
if (stat == Stat.Production || stat == Stat.Happiness) return false
if (hasUnique(UniqueType.CannotBePurchased)) return false
if (stat == Stat.Gold) return !hasUnique(UniqueType.Unbuildable)
// Can be purchased with [Stat] [cityFilter]
if (getMatchingUniques(UniqueType.CanBePurchasedWithStat)
.any { it.params[0] == stat.name && (cityInfo != null && cityInfo.matchesFilter(it.params[1])) }
.any { cityInfo != null && it.params[0] == stat.name && cityInfo.matchesFilter(it.params[1]) }
) return true
// Can be purchased for [amount] [Stat] [cityFilter]
if (getMatchingUniques(UniqueType.CanBePurchasedForAmountStat)
.any { it.params[1] == stat.name && (cityInfo != null && cityInfo.matchesFilter(it.params[2])) }
.any { cityInfo != null && it.params[1] == stat.name && cityInfo.matchesFilter(it.params[2]) }
) return true
return false
}

View File

@ -157,7 +157,7 @@ class UniqueMap: HashMap<String, ArrayList<Unique>>() {
}
fun getUniques(placeholderText: String): Sequence<Unique> {
return this[placeholderText]?.asSequence() ?: sequenceOf()
return this[placeholderText]?.asSequence() ?: emptySequence()
}
fun getUniques(uniqueType: UniqueType) = getUniques(uniqueType.placeholderText)

View File

@ -190,8 +190,6 @@ enum class UniqueType(val text: String, vararg targets: UniqueTarget, val flags:
BuyUnitsByProductionCost("May buy [baseUnitFilter] units with [stat] for [amount] times their normal Production cost", UniqueTarget.FollowerBelief, UniqueTarget.Global),
BuyBuildingsByProductionCost("May buy [buildingFilter] buildings with [stat] for [amount] times their normal Production cost", UniqueTarget.FollowerBelief, UniqueTarget.Global),
@Deprecated("As of 3.17.9", ReplaceWith ("May buy [baseUnitFilter] units for [amount] [stat] [cityFilter] at an increasing price ([amount]) <starting from the [era]>"))
BuyUnitsIncreasingCostEra("May buy [baseUnitFilter] units for [amount] [stat] [cityFilter] starting from the [era] at an increasing price ([amount])", UniqueTarget.Global),
// ToDo: Unify
EnablesGoldProduction("Enables conversion of city production to gold", UniqueTarget.Global),
@ -624,6 +622,9 @@ enum class UniqueType(val text: String, vararg targets: UniqueTarget, val flags:
// region DEPRECATED AND REMOVED
@Deprecated("As of 3.17.9, removed as of 3.19.3", ReplaceWith ("May buy [baseUnitFilter] units for [amount] [stat] [cityFilter] at an increasing price ([amount]) <starting from the [era]>"), DeprecationLevel.ERROR)
BuyUnitsIncreasingCostEra("May buy [baseUnitFilter] units for [amount] [stat] [cityFilter] starting from the [era] at an increasing price ([amount])", UniqueTarget.Global),
@Deprecated("As of 3.17.10 - removed 3.18.19", ReplaceWith("[stats] from [tileFilter] tiles <after discovering [tech]>"), DeprecationLevel.ERROR)
StatsOnTileWithTech("[stats] on [tileFilter] tiles once [tech] is discovered", UniqueTarget.Improvement),
@Deprecated("As of 3.17.10 - removed 3.18.19", ReplaceWith("[stats] <after discovering [tech]>"), DeprecationLevel.ERROR)

View File

@ -252,15 +252,7 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
if (cityInfo == null) return super.canBePurchasedWithStat(cityInfo, stat)
val conditionalState = StateForConditionals(civInfo = cityInfo.civInfo, cityInfo = cityInfo)
return (
cityInfo.getMatchingUniques(UniqueType.BuyUnitsIncreasingCostEra, conditionalState)
.any {
it.params[2] == stat.name
&& cityInfo.civInfo.getEraNumber() >= ruleset.eras[it.params[4]]!!.eraNumber
&& matchesFilter(it.params[0])
&& cityInfo.matchesFilter(it.params[3])
}
|| cityInfo.getMatchingUniques(UniqueType.BuyUnitsIncreasingCost, conditionalState)
return (cityInfo.getMatchingUniques(UniqueType.BuyUnitsIncreasingCost, conditionalState)
.any {
it.params[2] == stat.name
&& matchesFilter(it.params[0])
@ -292,22 +284,6 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
val baseCost = super.getBaseBuyCost(cityInfo, stat)
if (baseCost != null)
yield(baseCost)
// Deprecated since 3.17.9
yieldAll(cityInfo.getMatchingUniques(UniqueType.BuyUnitsIncreasingCostEra, conditionalState)
.filter {
it.params[2] == stat.name
&& matchesFilter(it.params[0])
&& cityInfo.matchesFilter(it.params[3])
&& cityInfo.civInfo.getEraNumber() >= ruleset.eras[it.params[4]]!!.eraNumber
}.map {
getCostForConstructionsIncreasingInPrice(
it.params[1].toInt(),
it.params[5].toInt(),
cityInfo.civInfo.civConstructions.boughtItemsWithIncreasingPrice[name] ?: 0
)
}
)
//
yieldAll(cityInfo.getMatchingUniques(UniqueType.BuyUnitsIncreasingCost, conditionalState)
.filter {
it.params[2] == stat.name