Converted era parameter to a transient var (#6063)

The cost of casting getEra() constantly is definitely non-trivial, and why do we even generate the era every time? It only changes when we add a new tech! So we can save it as a transient in the civInfo.tech and be done with it!
This commit is contained in:
Yair Morgenstern 2022-01-27 15:44:33 +02:00 committed by GitHub
parent 7c478f4cd3
commit 957d5b4008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 24 deletions

View File

@ -572,30 +572,7 @@ class CivilizationInfo {
else -> getCivUnits().none()
}
fun getEra(): Era {
if (gameInfo.ruleSet.technologies.isEmpty() || tech.researchedTechnologies.isEmpty())
return Era()
val maxEraOfResearchedTechs = tech.researchedTechnologies
.asSequence()
.map { it.column!! }
.maxByOrNull { it.columnNumber }!!
.era
val maxEra = gameInfo.ruleSet.eras[maxEraOfResearchedTechs]!!
val researchedTechsHashset = tech.researchedTechnologies.toHashSet()
val minEraOfNonResearchedTechs = gameInfo.ruleSet.technologies.values
.asSequence()
.filter { it !in researchedTechsHashset }
.map { it.column!! }
.minByOrNull { it.columnNumber }
?.era
?: return maxEra
val minEra = gameInfo.ruleSet.eras[minEraOfNonResearchedTechs]!!
return if (minEra.eraNumber > maxEra.eraNumber) minEra
else maxEra
}
fun getEra(): Era = tech.era
fun getEraNumber(): Int = getEra().eraNumber

View File

@ -4,6 +4,7 @@ import com.unciv.logic.city.CityInfo
import com.unciv.logic.map.MapSize
import com.unciv.logic.map.RoadStatus
import com.unciv.logic.map.TileInfo
import com.unciv.models.ruleset.Era
import com.unciv.models.ruleset.unique.UniqueMap
import com.unciv.models.ruleset.unique.UniqueTriggerActivation
import com.unciv.models.ruleset.tech.Technology
@ -20,6 +21,9 @@ import kotlin.math.max
import kotlin.math.min
class TechManager {
@Transient
var era: Era = Era()
@Transient
lateinit var civInfo: CivilizationInfo
/** This is the Transient list of Technologies */
@ -326,6 +330,37 @@ class TechManager {
if (unique.params[1] != techName) continue
civInfo.addNotification("You have unlocked [The Long Count]!", MayaLongCountAction(), MayaCalendar.notificationIcon)
}
updateEra()
}
fun updateEra() {
val ruleset = civInfo.gameInfo.ruleSet
if (ruleset.technologies.isEmpty() || researchedTechnologies.isEmpty())
return
val maxEraOfResearchedTechs = researchedTechnologies
.asSequence()
.map { it.column!! }
.maxByOrNull { it.columnNumber }!!
.era
val maxEra = ruleset.eras[maxEraOfResearchedTechs]!!
val minEraOfNonResearchedTechs = ruleset.technologies.values
.asSequence()
.filter { it !in researchedTechnologies }
.map { it.column!! }
.minByOrNull { it.columnNumber }
?.era
if (minEraOfNonResearchedTechs == null) {
era = maxEra
return
}
val minEra = ruleset.eras[minEraOfNonResearchedTechs]!!
era = if (minEra.eraNumber <= maxEra.eraNumber) maxEra
else minEra
}
fun addTechToTransients(tech: Technology) {
@ -337,6 +372,7 @@ class TechManager {
researchedTechnologies.addAll(techsResearched.map { getRuleset().technologies[it]!! })
researchedTechnologies.forEach { addTechToTransients(it) }
updateTransientBooleans()
updateEra()
}
private fun updateTransientBooleans() {