From 34dcc68e157fa989c7dc88130567479e34fe8b91 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 28 Jun 2024 22:55:10 +0700 Subject: [PATCH] Optimize StatusController.updateStats --- .../world/entities/StatusController.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/StatusController.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/StatusController.kt index c4b15507..38a6f94a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/StatusController.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/StatusController.kt @@ -745,6 +745,8 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf private class LiveStatImpl : LiveStat() { override var baseValue: Double = 0.0 + var tickVisited = -1 + // Value with just the base percent modifiers applied and the value // modifiers override var baseModifiedValue: Double = 0.0 @@ -924,6 +926,8 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf return (liveStatsInternal[name]?.effectiveModifiedValue ?: 0.0) > 0.0 } + private var statsTick = 0 + private fun updateStats(delta: Double) { // We use two intermediate values for calculating the effective stat value. // The baseModifiedValue represents the application of the base percentage @@ -932,13 +936,13 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf // modifiers successively on the baseModifiedValue, causing them to stack with // each other in addition to base multipliers and value modifiers - val neverVisited = ObjectOpenHashSet(liveStats.keys) + statsTick++ for ((statName, stat) in config.stats) { val live = liveStatsInternal[statName]!! live.baseValue = stat.baseValue live.baseModifiedValue = stat.baseValue - neverVisited.remove(statName) + live.tickVisited = statsTick } for (group in statModifiers.values) { @@ -950,7 +954,7 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf liveStatsInternal[modifier.stat] = live } - neverVisited.remove(modifier.stat) + live.tickVisited = statsTick if (modifier.type == StatModifierType.BASE_MULTIPLICATION) { live.baseModifiedValue += (modifier.value - 1.0) * live.baseValue @@ -978,12 +982,12 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf } } - for (value in neverVisited) { - val live = liveStatsInternal[value]!! - - live.baseValue = 0.0 - live.effectiveModifiedValue = 0.0 - live.baseModifiedValue = 0.0 + for (live in liveStatsInternal.values) { + if (live.tickVisited != statsTick) { + live.baseValue = 0.0 + live.effectiveModifiedValue = 0.0 + live.baseModifiedValue = 0.0 + } } // Then update all the resources due to charging and percentage tracking,