Compare commits

...

2 Commits

View File

@ -3,15 +3,12 @@ package ru.dbotthepony.mc.otm.block.entity.cable
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
import it.unimi.dsi.fastutil.objects.ReferenceArraySet
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet
import net.minecraft.core.BlockPos
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.mc.otm.SERVER_IS_LIVE
import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
import ru.dbotthepony.mc.otm.capability.receiveEnergy
import ru.dbotthepony.mc.otm.config.CablesConfig
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.RelativeSide
@ -480,7 +477,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
private var searchedOnTick = 0
private fun getPath(a: EnergyCableBlockEntity.Node, b: EnergyCableBlockEntity.Node, energyToTransfer: Decimal, sort: Boolean): List<SegmentPath> {
if (!a.canTraverse || !b.canTraverse)
if (!a.canTraverse || !b.canTraverse || energyToTransfer <= Decimal.ZERO)
return listOf()
if (searchedOnTick != UNIVERSE_TICKS) {
@ -493,17 +490,18 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
val maxSearches = CablesConfig.SEARCHES_PER_TICK
if (!list.saturated && lastTickSearches <= maxSearches) {
var maxThroughput = list.paths.iterator().map { it.availableThroughput }.reduce(Decimal.ZERO, Decimal::plus)
var maxThroughput = list.paths.maxOfOrNull { it.availableThroughput } ?: Decimal.ZERO
var combinedThroughput = list.paths.iterator().map { it.availableThroughput }.reduce(Decimal.ZERO, Decimal::plus)
while (maxThroughput < energyToTransfer && !list.saturated && ++lastTickSearches <= maxSearches) {
// TODO: Heuristics here is wrong
while (combinedThroughput < energyToTransfer && !list.saturated && ++lastTickSearches <= maxSearches) {
val find = findPath(a, b, list.paths, maxThroughput)
if (find == null || find in list.paths) {
list.saturated = true
} else {
list.paths.add(find)
maxThroughput += find.availableThroughput
combinedThroughput += find.availableThroughput
maxThroughput = maxOf(maxThroughput, find.availableThroughput)
}
}
}
@ -525,7 +523,6 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
node.segment.paths.forEach {
pathCache.remove(it.a to it.b)
pathCache.remove(it.b to it.a)
}
node.segment.paths = ReferenceLinkedOpenHashSet()
@ -561,7 +558,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
val it = side.neighbour.get() ?: continue
if (it is EnergyCableBlockEntity.CableSide) continue
val paths = getPath(fromNode, node, residue, !simulate)
val paths = getPath(fromNode, node, it.receiveEnergy(residue, true), !simulate)
hit = true
if (paths.size == 1) {