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() {
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,