Increase cable visuals update frequency the more it oscillate during last 17 ticks

This commit is contained in:
DBotThePony 2025-01-15 21:52:30 +07:00
parent b4e37fbd1f
commit bd4fe17e33
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.mc.otm.block.entity.cable
import it.unimi.dsi.fastutil.ints.IntLists
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
@ -110,6 +111,10 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
return min
}
fun triggerBlockstateUpdates() {
segments.forEach { it.triggerBlockstateUpdates() }
}
fun refund(amount: Decimal, simulate: Boolean, instantSnapshot: MutableMap<Segment, Decimal>) {
segments.forEach { it.refund(amount, simulate, instantSnapshot) }
}
@ -152,6 +157,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
private var throughputKnown = false
private var lastTick = 0
private var lastPoweredStatus = 0
private var lastPoweredSwitchFrequency = 0
private fun updateBlockstates(): Boolean {
if (nodes.isEmpty()) return false
@ -175,15 +181,32 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
private var hasBlockstateTimer = false
private fun blockstateUpdateStep() {
val new = updateBlockstates()
val changed = hasBlockstateTimer != new
hasBlockstateTimer = updateBlockstates()
lastPoweredSwitchFrequency = (lastPoweredSwitchFrequency shl 1) or (if (changed) 1 else 0)
if (hasBlockstateTimer) {
onceServer(20) {
var i = 20
for (index in 0 .. 16) {
if (lastPoweredSwitchFrequency and (index shl index) != 0) {
i--
}
}
onceServer(i) {
blockstateUpdateStep()
}
}
}
fun triggerBlockstateUpdates() {
if (!hasBlockstateTimer && transferredLastTick > Decimal.ZERO) {
blockstateUpdateStep()
}
}
fun throughput(instantSnapshot: Reference2ObjectOpenHashMap<Segment, Decimal>): Decimal {
if (lastTick != UNIVERSE_TICKS) {
transferredLastTick = Decimal.ZERO
@ -229,17 +252,13 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
val diff = new - transferredLastTick
transferredLastTick = new
if (!hasBlockstateTimer) {
blockstateUpdateStep()
}
return diff
}
fun refund(amount: Decimal, simulate: Boolean, instantSnapshot: MutableMap<Segment, Decimal>) {
if (simulate) {
if (this in instantSnapshot) {
instantSnapshot[this] = maxOf(instantSnapshot[this]!! - amount, Decimal.ZERO)
instantSnapshot[this] = maxOf((instantSnapshot[this] ?: Decimal.ZERO) - amount, Decimal.ZERO)
}
} else {
transferredLastTick = maxOf(transferredLastTick - amount, Decimal.ZERO)
@ -472,6 +491,12 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
// so cables record actual value transferred through them
if (thisReceived != pathTransferred) {
path.refund(pathTransferred - thisReceived, simulate, snapshot)
if (!simulate && thisReceived != Decimal.ZERO) {
path.triggerBlockstateUpdates()
}
} else if (!simulate) {
path.triggerBlockstateUpdates()
}
received += thisReceived
@ -510,6 +535,10 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
for (path in paths) {
val passed = path.transfer(thisReceived, simulate, snapshot)
if (!simulate)
path.triggerBlockstateUpdates()
thisReceived -= passed
if (thisReceived <= Decimal.ZERO) break
}