Happiness is now a saved transient - saves about 10% of all runtime! Can't believe I hadn't thought of that before! 0_0

This commit is contained in:
Yair Morgenstern
2020-04-27 00:01:40 +03:00
parent ba2d6fe405
commit 6fed455d76
2 changed files with 8 additions and 10 deletions

View File

@ -89,16 +89,12 @@ class CivInfoStats(val civInfo: CivilizationInfo){
}
}
for (entry in getHappinessBreakdown()) {
statMap.add(entry.key, Stats().apply { happiness=entry.value })
}
statMap["Transportation upkeep"] = Stats().apply { gold=- getTransportationUpkeep().toFloat()}
statMap["Unit upkeep"] = Stats().apply { gold=- getUnitUpkeep().toFloat()}
if (civInfo.policies.hasEffect("50% of excess happiness added to culture towards policies")) {
val happiness = statMap.values.map { it.happiness }.sum()
if(happiness>0) statMap.add("Policies", Stats().apply { culture=happiness/2 })
val happiness = civInfo.getHappiness()
if(happiness>0) statMap.add("Policies", Stats().apply { culture=happiness/2f })
}
// negative gold hurts science
@ -127,7 +123,7 @@ class CivInfoStats(val civInfo: CivilizationInfo){
statMap["Luxury resources"]= civInfo.getCivResources().map { it.resource }
.count { it.resourceType === ResourceType.Luxury } * happinessPerUniqueLuxury
for(city in civInfo.cities.toList()){
for(city in civInfo.cities){
for(keyvalue in city.cityStats.happinessList){
if(statMap.containsKey(keyvalue.key))
statMap[keyvalue.key] = statMap[keyvalue.key]!!+keyvalue.value

View File

@ -50,6 +50,7 @@ class CivilizationInfo {
/** This is for performance since every movement calculation depends on this, see MapUnit comment */
@Transient var hasActiveGreatWall = false
@Transient var statsForNextTurn = Stats()
@Transient var happinessForNextTurn = 0
@Transient var detailedCivResources = ResourceSupplyList()
var playerType = PlayerType.AI
@ -146,11 +147,12 @@ class CivilizationInfo {
fun stats() = CivInfoStats(this)
fun transients() = CivInfoTransientUpdater(this)
fun updateStatsForNextTurn(){
statsForNextTurn = stats().getStatMapForNextTurn().values.toList().reduce{a,b->a+b}
fun updateStatsForNextTurn() {
happinessForNextTurn = stats().getHappinessBreakdown().values.sum().roundToInt()
statsForNextTurn = stats().getStatMapForNextTurn().values.reduce { a, b -> a + b }
}
fun getHappiness() = stats().getHappinessBreakdown().values.sum().roundToInt()
fun getHappiness() = happinessForNextTurn
fun getCivResources(): ResourceSupplyList {