Compare commits
No commits in common. "d0d9b6ae7919a2d3cfc802a6d1c2359f016973a4" and "a2c9234501e9e3eae3b1d3050d16e26bcff96922" have entirely different histories.
d0d9b6ae79
...
a2c9234501
@ -111,6 +111,11 @@ abstract class EnergyCableBlockEntity(type: BlockEntityType<*>, blockPos: BlockP
|
|||||||
_segment = value
|
_segment = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun onInvalidate() {
|
||||||
|
_segment = EnergyCableGraph.Segment(this)
|
||||||
|
updatePoweredState(false)
|
||||||
|
}
|
||||||
|
|
||||||
val sides get() = energySides
|
val sides get() = energySides
|
||||||
|
|
||||||
override fun onNeighbour(link: Link) {
|
override fun onNeighbour(link: Link) {
|
||||||
|
@ -8,11 +8,6 @@ import org.apache.logging.log4j.LogManager
|
|||||||
import ru.dbotthepony.mc.otm.SERVER_IS_LIVE
|
import ru.dbotthepony.mc.otm.SERVER_IS_LIVE
|
||||||
import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
|
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.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
|
||||||
@ -71,9 +66,8 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SegmentPath(val a: EnergyCableBlockEntity.Node, val b: EnergyCableBlockEntity.Node) {
|
class SegmentPath(private val a: BlockPos, private val b: BlockPos) {
|
||||||
val segments = LinkedHashSet<Segment>()
|
val segments = LinkedHashSet<Segment>()
|
||||||
val nodes = HashSet<EnergyCableBlockEntity.Node>()
|
|
||||||
var shortCircuit = false
|
var shortCircuit = false
|
||||||
private var lastTickTransfers = 0
|
private var lastTickTransfers = 0
|
||||||
private var lastTick = 0
|
private var lastTick = 0
|
||||||
@ -86,7 +80,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
}
|
}
|
||||||
|
|
||||||
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
|
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
|
||||||
return node in nodes
|
return segments.any { node in it }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
@ -139,7 +133,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val nodes = LinkedHashSet<EnergyCableBlockEntity.Node>()
|
private val nodes = LinkedHashSet<EnergyCableBlockEntity.Node>()
|
||||||
var paths = ReferenceLinkedOpenHashSet<SegmentPath>()
|
private var paths = ReferenceLinkedOpenHashSet<SegmentPath>()
|
||||||
|
|
||||||
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
|
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
|
||||||
return node in nodes
|
return node in nodes
|
||||||
@ -171,11 +165,6 @@ 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
|
||||||
}
|
}
|
||||||
@ -294,13 +283,11 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
fun remove(node: EnergyCableBlockEntity.Node) {
|
fun remove(node: EnergyCableBlockEntity.Node) {
|
||||||
check(nodes.remove(node)) { "Tried to remove node $node from segment $this, but that node does not belong to this segment" }
|
check(nodes.remove(node)) { "Tried to remove node $node from segment $this, but that node does not belong to this segment" }
|
||||||
|
|
||||||
paths.forEach { it.nodes.remove(node) }
|
|
||||||
throughputKnown = false
|
throughputKnown = false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun add(node: EnergyCableBlockEntity.Node) {
|
fun add(node: EnergyCableBlockEntity.Node) {
|
||||||
check(nodes.add(node)) { "Tried to add node $node to segment $this, but we already have that node added" }
|
check(nodes.add(node)) { "Tried to add node $node to segment $this, but we already have that node added" }
|
||||||
paths.forEach { it.nodes.add(node) }
|
|
||||||
|
|
||||||
if (throughputKnown)
|
if (throughputKnown)
|
||||||
throughput = maxOf(throughput, node.energyThroughput)
|
throughput = maxOf(throughput, node.energyThroughput)
|
||||||
@ -309,14 +296,12 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
fun add(path: SegmentPath) {
|
fun add(path: SegmentPath) {
|
||||||
if (paths.add(path)) {
|
if (paths.add(path)) {
|
||||||
check(path.segments.add(this)) { "Path set and Segment disagree whenever $this is absent from $path" }
|
check(path.segments.add(this)) { "Path set and Segment disagree whenever $this is absent from $path" }
|
||||||
path.nodes.addAll(nodes)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun remove(path: SegmentPath) {
|
fun remove(path: SegmentPath) {
|
||||||
check(paths.remove(path)) { "Path $path should already contain $this" }
|
check(paths.remove(path)) { "Path $path should already contain $this" }
|
||||||
check(path.segments.remove(this)) { "Path set and Segment disagree whenever $this is present in $path" }
|
check(path.segments.remove(this)) { "Path set and Segment disagree whenever $this is present in $path" }
|
||||||
path.nodes.removeAll(nodes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// breaks "instant snapshots" of segments atm
|
// breaks "instant snapshots" of segments atm
|
||||||
@ -346,26 +331,6 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun split(): List<Segment> {
|
|
||||||
val result = ArrayList<Segment>()
|
|
||||||
result.add(this)
|
|
||||||
|
|
||||||
for (v in nodes) {
|
|
||||||
val segment = Segment()
|
|
||||||
v._segment = segment
|
|
||||||
segment.nodes.add(v)
|
|
||||||
segment.throughput = v.energyThroughput
|
|
||||||
segment.throughputKnown = true
|
|
||||||
segment.transferredLastTick = transferredLastTick
|
|
||||||
result.add(segment)
|
|
||||||
v.updatePoweredState(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// mark this segment as dead
|
|
||||||
this.nodes.clear()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun combineInto(segment: Segment) {
|
private fun combineInto(segment: Segment) {
|
||||||
nodes.forEach { it._segment = segment }
|
nodes.forEach { it._segment = segment }
|
||||||
segment.nodes.addAll(nodes)
|
segment.nodes.addAll(nodes)
|
||||||
@ -413,6 +378,11 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun invalidatePathCache() {
|
||||||
|
pathCache.clear()
|
||||||
|
nodes.forEach { it.onInvalidate() }
|
||||||
|
}
|
||||||
|
|
||||||
// isn't exactly A*, but greedy algorithm, which searched for locally optimal solutions because they lead to globally optimal ones
|
// isn't exactly A*, but greedy algorithm, which searched for locally optimal solutions because they lead to globally optimal ones
|
||||||
private fun findPath(a: EnergyCableBlockEntity.Node, b: EnergyCableBlockEntity.Node, existing: Collection<SegmentPath>, threshold: Decimal): SegmentPath? {
|
private fun findPath(a: EnergyCableBlockEntity.Node, b: EnergyCableBlockEntity.Node, existing: Collection<SegmentPath>, threshold: Decimal): SegmentPath? {
|
||||||
val seenTop = existing.any { a in it }
|
val seenTop = existing.any { a in it }
|
||||||
@ -458,7 +428,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val path = SegmentPath(a, b)
|
val path = SegmentPath(a.blockEntity.blockPos, b.blockEntity.blockPos)
|
||||||
solution.forEach { it.segment.add(path) }
|
solution.forEach { it.segment.add(path) }
|
||||||
touchedSegments.forEach { it.tryCombine(touchedSegments) }
|
touchedSegments.forEach { it.tryCombine(touchedSegments) }
|
||||||
|
|
||||||
@ -476,34 +446,23 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private var lastTickSearches = 0
|
|
||||||
private var searchedOnTick = 0
|
|
||||||
|
|
||||||
private fun getPath(a: EnergyCableBlockEntity.Node, b: EnergyCableBlockEntity.Node, energyToTransfer: Decimal, sort: Boolean): List<SegmentPath> {
|
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)
|
||||||
return listOf()
|
return listOf()
|
||||||
|
|
||||||
if (searchedOnTick != UNIVERSE_TICKS) {
|
|
||||||
searchedOnTick = UNIVERSE_TICKS
|
|
||||||
lastTickSearches = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
val list = pathCache.computeIfAbsent(a to b) { CacheEntry() }
|
val list = pathCache.computeIfAbsent(a to b) { CacheEntry() }
|
||||||
|
|
||||||
val maxSearches = CablesConfig.SEARCHES_PER_TICK
|
if (!list.saturated) {
|
||||||
|
var maxThroughput = list.paths.maxOfOrNull { it.availableThroughput } ?: Decimal.ZERO
|
||||||
|
|
||||||
if (!list.saturated && lastTickSearches <= maxSearches) {
|
while (maxThroughput < energyToTransfer && !list.saturated) {
|
||||||
var maxThroughput = list.paths.iterator().map { it.availableThroughput }.reduce(Decimal.ZERO, Decimal::plus)
|
|
||||||
|
|
||||||
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 += find.availableThroughput
|
maxThroughput = maxOf(maxThroughput, find.availableThroughput)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -514,29 +473,18 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
return list.paths
|
return list.paths
|
||||||
}
|
}
|
||||||
|
|
||||||
fun notifyThroughputsChanged() {
|
|
||||||
pathCache.values.forEach { it.saturated = false }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onNodeRemoved(node: EnergyCableBlockEntity.Node) {
|
override fun onNodeRemoved(node: EnergyCableBlockEntity.Node) {
|
||||||
if (livelyNodes.remove(node)) {
|
if (livelyNodes.remove(node)) {
|
||||||
check(livelyNodesList.remove(node))
|
check(livelyNodesList.remove(node))
|
||||||
}
|
}
|
||||||
|
|
||||||
node.segment.paths.forEach {
|
invalidatePathCache()
|
||||||
pathCache.remove(it.a to it.b)
|
|
||||||
pathCache.remove(it.b to it.a)
|
|
||||||
}
|
|
||||||
|
|
||||||
node.segment.paths = ReferenceLinkedOpenHashSet()
|
|
||||||
node.segment.split()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNodeAdded(node: EnergyCableBlockEntity.Node) {
|
override fun onNodeAdded(node: EnergyCableBlockEntity.Node) {
|
||||||
check(livelyNodes.add(node))
|
check(livelyNodes.add(node))
|
||||||
livelyNodesList.add(node)
|
livelyNodesList.add(node)
|
||||||
|
invalidatePathCache()
|
||||||
notifyThroughputsChanged()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun receiveEnergy(howMuch: Decimal, simulate: Boolean, fromNode: EnergyCableBlockEntity.Node, fromSide: RelativeSide): Decimal {
|
fun receiveEnergy(howMuch: Decimal, simulate: Boolean, fromNode: EnergyCableBlockEntity.Node, fromSide: RelativeSide): Decimal {
|
||||||
|
@ -27,10 +27,4 @@ object CablesConfig : AbstractConfig("cables") {
|
|||||||
init {
|
init {
|
||||||
E.SUPERCONDUCTOR
|
E.SUPERCONDUCTOR
|
||||||
}
|
}
|
||||||
|
|
||||||
val SEARCHES_PER_TICK: Int by builder
|
|
||||||
.comment("How many paths are allowed to be searched for during single tick *per* cable network")
|
|
||||||
.comment("Too low value will make huge cable networks woozy when their cabling changes (e.g. cable added or removed, or valve switched)")
|
|
||||||
.comment("Too high value will create lag spikes depending on complexity of cable network at hand when cabling changes")
|
|
||||||
.defineInRange("SEARCHER_PER_TICK", 40, 1)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user