Optimize StatusController.updateStats

This commit is contained in:
DBotThePony 2024-06-28 22:55:10 +07:00
parent f95bc9762f
commit 34dcc68e15
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -745,6 +745,8 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf
private class LiveStatImpl : LiveStat() { private class LiveStatImpl : LiveStat() {
override var baseValue: Double = 0.0 override var baseValue: Double = 0.0
var tickVisited = -1
// Value with just the base percent modifiers applied and the value // Value with just the base percent modifiers applied and the value
// modifiers // modifiers
override var baseModifiedValue: Double = 0.0 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 return (liveStatsInternal[name]?.effectiveModifiedValue ?: 0.0) > 0.0
} }
private var statsTick = 0
private fun updateStats(delta: Double) { private fun updateStats(delta: Double) {
// We use two intermediate values for calculating the effective stat value. // We use two intermediate values for calculating the effective stat value.
// The baseModifiedValue represents the application of the base percentage // 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 // modifiers successively on the baseModifiedValue, causing them to stack with
// each other in addition to base multipliers and value modifiers // each other in addition to base multipliers and value modifiers
val neverVisited = ObjectOpenHashSet(liveStats.keys) statsTick++
for ((statName, stat) in config.stats) { for ((statName, stat) in config.stats) {
val live = liveStatsInternal[statName]!! val live = liveStatsInternal[statName]!!
live.baseValue = stat.baseValue live.baseValue = stat.baseValue
live.baseModifiedValue = stat.baseValue live.baseModifiedValue = stat.baseValue
neverVisited.remove(statName) live.tickVisited = statsTick
} }
for (group in statModifiers.values) { for (group in statModifiers.values) {
@ -950,7 +954,7 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf
liveStatsInternal[modifier.stat] = live liveStatsInternal[modifier.stat] = live
} }
neverVisited.remove(modifier.stat) live.tickVisited = statsTick
if (modifier.type == StatModifierType.BASE_MULTIPLICATION) { if (modifier.type == StatModifierType.BASE_MULTIPLICATION) {
live.baseModifiedValue += (modifier.value - 1.0) * live.baseValue live.baseModifiedValue += (modifier.value - 1.0) * live.baseValue
@ -978,12 +982,12 @@ class StatusController(val entity: ActorEntity, val config: StatusControllerConf
} }
} }
for (value in neverVisited) { for (live in liveStatsInternal.values) {
val live = liveStatsInternal[value]!! if (live.tickVisited != statsTick) {
live.baseValue = 0.0
live.baseValue = 0.0 live.effectiveModifiedValue = 0.0
live.effectiveModifiedValue = 0.0 live.baseModifiedValue = 0.0
live.baseModifiedValue = 0.0 }
} }
// Then update all the resources due to charging and percentage tracking, // Then update all the resources due to charging and percentage tracking,