Compare commits

...

2 Commits

Author SHA1 Message Date
436456dcc5
Fix cable path heuristics 2025-01-14 18:27:20 +07:00
07121d8b41
Fix crash in addLivelyNode 2025-01-14 18:19:44 +07:00
2 changed files with 36 additions and 6 deletions

View File

@ -20,10 +20,14 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
private val livelyNodesList = ArrayList<EnergyCableBlockEntity.Node>()
fun addLivelyNode(node: EnergyCableBlockEntity.Node) {
require(node in nodesSet) { "$node does not belong to $this" }
if (livelyNodes.add(node)) {
livelyNodesList.add(node)
when (contains(node)) {
ContainsStatus.ABOUT_TO_BE_REMOVED, ContainsStatus.ABOUT_TO_BE_ADDED -> { } // do nothing
ContainsStatus.DOES_NOT_BELONG -> throw IllegalArgumentException("$node does not belong to $this")
ContainsStatus.CONTAINS -> {
if (livelyNodes.add(node)) {
livelyNodesList.add(node)
}
}
}
}
@ -95,8 +99,14 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
var transferredLastTick = Decimal.ZERO
private set
val availableThroughput: Decimal
get() = throughput - transferredLastTick
val availableThroughput: Decimal get() {
if (!throughputKnown) {
throughput = nodes.maxOf { it.energyThroughput }
throughputKnown = true
}
return throughput - transferredLastTick
}
private var throughputKnown = false
private var lastTick = 0

View File

@ -22,6 +22,26 @@ open class GraphNodeList<N : GraphNode<N, G>, G : GraphNodeList<N, G>> : ICondit
val nodesSet: Set<N> = Collections.unmodifiableSet(nodesSetInternal)
val size get() = nodesInternal.size
enum class ContainsStatus(val belongs: Boolean, val potentiallyBelongs: Boolean) {
ABOUT_TO_BE_REMOVED(true, false),
DOES_NOT_BELONG(false, false),
ABOUT_TO_BE_ADDED(false, true),
CONTAINS(true, true)
}
fun contains(node: N): ContainsStatus {
if (node in nodesSet) {
if (node in queuedRemove)
return ContainsStatus.ABOUT_TO_BE_REMOVED
return ContainsStatus.CONTAINS
} else if (node in queuedAdd) {
return ContainsStatus.ABOUT_TO_BE_ADDED
} else {
return ContainsStatus.DOES_NOT_BELONG
}
}
var isValid = true
private set