There is no need for "greedy" strategy when searching for extra cable paths

This commit is contained in:
DBotThePony 2025-02-11 01:17:34 +07:00
parent e68f3a7996
commit e708d3e6b2
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -10,6 +10,9 @@ import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
import ru.dbotthepony.mc.otm.capability.receiveEnergy import ru.dbotthepony.mc.otm.capability.receiveEnergy
import ru.dbotthepony.mc.otm.config.CablesConfig import ru.dbotthepony.mc.otm.config.CablesConfig
import ru.dbotthepony.mc.otm.core.addAll import ru.dbotthepony.mc.otm.core.addAll
import ru.dbotthepony.mc.otm.core.collect.map
import ru.dbotthepony.mc.otm.core.collect.max
import ru.dbotthepony.mc.otm.core.collect.reduce
import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.math.RelativeSide import ru.dbotthepony.mc.otm.core.math.RelativeSide
import ru.dbotthepony.mc.otm.core.shuffle import ru.dbotthepony.mc.otm.core.shuffle
@ -168,6 +171,11 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
private set private set
val availableThroughput: Decimal get() { val availableThroughput: Decimal get() {
if (lastTick != UNIVERSE_TICKS) {
transferredLastTick = Decimal.ZERO
lastTick = UNIVERSE_TICKS
}
checkThroughput() checkThroughput()
return throughput - transferredLastTick return throughput - transferredLastTick
} }
@ -485,16 +493,17 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
val maxSearches = CablesConfig.SEARCHES_PER_TICK val maxSearches = CablesConfig.SEARCHES_PER_TICK
if (!list.saturated && lastTickSearches <= maxSearches) { if (!list.saturated && lastTickSearches <= maxSearches) {
var maxThroughput = list.paths.maxOfOrNull { it.availableThroughput } ?: Decimal.ZERO var maxThroughput = list.paths.iterator().map { it.availableThroughput }.reduce(Decimal.ZERO, Decimal::plus)
while (maxThroughput < energyToTransfer && !list.saturated && ++lastTickSearches <= maxSearches) { while (maxThroughput < energyToTransfer && !list.saturated && ++lastTickSearches <= maxSearches) {
// TODO: Heuristics here is wrong
val find = findPath(a, b, list.paths, maxThroughput) val find = findPath(a, b, list.paths, maxThroughput)
if (find == null || find in list.paths) { if (find == null || find in list.paths) {
list.saturated = true list.saturated = true
} else { } else {
list.paths.add(find) list.paths.add(find)
maxThroughput = maxOf(maxThroughput, find.availableThroughput) maxThroughput += find.availableThroughput
} }
} }
} }