Merge branch '1.21' of https://git.dbotthepony.ru/DBot/overdrive_that_matters into world-structures
This commit is contained in:
commit
863d52230e
@ -358,6 +358,9 @@ abstract class MatteryBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun get(): T? {
|
override fun get(): T? {
|
||||||
|
if (isRemoved)
|
||||||
|
return null
|
||||||
|
|
||||||
return cache?.capability
|
return cache?.capability
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,13 +374,13 @@ abstract class MatteryBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state
|
|||||||
if (!SERVER_IS_LIVE) return
|
if (!SERVER_IS_LIVE) return
|
||||||
val level = level as? ServerLevel
|
val level = level as? ServerLevel
|
||||||
|
|
||||||
|
val creationVersion = ++currentVersion
|
||||||
|
|
||||||
if (level == null) {
|
if (level == null) {
|
||||||
cache = null
|
cache = null
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val creationVersion = ++currentVersion
|
|
||||||
|
|
||||||
val direction = blockRotation.side2Dir(side)
|
val direction = blockRotation.side2Dir(side)
|
||||||
|
|
||||||
cache = BlockCapabilityCache.create(
|
cache = BlockCapabilityCache.create(
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.block.entity.cable
|
package ru.dbotthepony.mc.otm.block.entity.cable
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectArraySet
|
||||||
import net.minecraft.core.BlockPos
|
import net.minecraft.core.BlockPos
|
||||||
import net.minecraft.core.Direction
|
import net.minecraft.core.Direction
|
||||||
import net.minecraft.world.level.Level
|
import net.minecraft.world.level.Level
|
||||||
@ -114,6 +115,8 @@ abstract class EnergyCableBlockEntity(type: BlockEntityType<*>, blockPos: BlockP
|
|||||||
|
|
||||||
val sides get() = energySides
|
val sides get() = energySides
|
||||||
|
|
||||||
|
val currentlyTransferringTo = ObjectArraySet<Pair<Node, RelativeSide>>()
|
||||||
|
|
||||||
override fun onNeighbour(link: Link) {
|
override fun onNeighbour(link: Link) {
|
||||||
if (link is DirectionLink) {
|
if (link is DirectionLink) {
|
||||||
updateBlockState(link.direction, true)
|
updateBlockState(link.direction, true)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.block.entity.cable
|
package ru.dbotthepony.mc.otm.block.entity.cable
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntRBTreeSet
|
||||||
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
|
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
|
||||||
import it.unimi.dsi.fastutil.objects.ReferenceArraySet
|
import it.unimi.dsi.fastutil.objects.ReferenceArraySet
|
||||||
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet
|
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet
|
||||||
@ -19,6 +20,7 @@ import ru.dbotthepony.mc.otm.core.shuffle
|
|||||||
import ru.dbotthepony.mc.otm.graph.GraphNodeList
|
import ru.dbotthepony.mc.otm.graph.GraphNodeList
|
||||||
import ru.dbotthepony.mc.otm.onceServer
|
import ru.dbotthepony.mc.otm.onceServer
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.concurrent.atomic.AtomicLong
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
import kotlin.collections.HashSet
|
import kotlin.collections.HashSet
|
||||||
import kotlin.collections.LinkedHashSet
|
import kotlin.collections.LinkedHashSet
|
||||||
@ -626,108 +628,138 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
notifyThroughputsChanged()
|
notifyThroughputsChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun receiveEnergy(howMuch: Decimal, simulate: Boolean, fromNode: EnergyCableBlockEntity.Node, fromSide: RelativeSide): Decimal {
|
private val indicesToRemove = IntRBTreeSet()
|
||||||
livelyNodesList.shuffle(fromNode.blockEntity.level!!.otmRandom)
|
private var recursionLevel = 0
|
||||||
|
|
||||||
|
fun receiveEnergy(howMuch: Decimal, simulate: Boolean, fromNode: EnergyCableBlockEntity.Node, fromSide: RelativeSide): Decimal {
|
||||||
|
val thisLevel = ++recursionLevel
|
||||||
|
val isMaster = thisLevel == 1
|
||||||
|
|
||||||
|
if (isMaster)
|
||||||
|
livelyNodesList.shuffle(fromNode.blockEntity.level!!.otmRandom)
|
||||||
|
|
||||||
|
var i = -1
|
||||||
val itr = livelyNodesList.iterator()
|
val itr = livelyNodesList.iterator()
|
||||||
var received = Decimal.ZERO
|
var received = Decimal.ZERO
|
||||||
var residue = howMuch.coerceAtMost(fromNode.energyThroughput)
|
var residue = howMuch.coerceAtMost(fromNode.energyThroughput)
|
||||||
val snapshot = Reference2ObjectOpenHashMap<Segment, Decimal>()
|
val snapshot = Reference2ObjectOpenHashMap<Segment, Decimal>()
|
||||||
|
|
||||||
for (pair in itr) {
|
for (pair in itr) {
|
||||||
val (node, relSide) = pair
|
i++
|
||||||
val side = node.sides[relSide]!!
|
|
||||||
|
|
||||||
if (!side.isEnabled) {
|
// recursion prevention
|
||||||
itr.remove()
|
if (!fromNode.currentlyTransferringTo.add(pair))
|
||||||
check(livelyNodes.remove(pair)) { "Lively nodes Set does not contain $pair" }
|
|
||||||
continue
|
continue
|
||||||
} else if (fromNode === node && side.side === fromSide) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val it = side.neighbour.get()
|
try {
|
||||||
|
val (node, relSide) = pair
|
||||||
|
val side = node.sides[relSide]!!
|
||||||
|
|
||||||
if (it == null || it is EnergyCableBlockEntity.CableSide) {
|
if (!side.isEnabled) {
|
||||||
itr.remove()
|
indicesToRemove.add(i)
|
||||||
check(livelyNodes.remove(pair)) { "Lively nodes Set does not contain $pair" }
|
livelyNodes.remove(pair)
|
||||||
continue
|
continue
|
||||||
}
|
} else if (fromNode === node && side.side === fromSide) {
|
||||||
|
|
||||||
val paths = getPath(fromNode, node, it.receiveEnergy(residue, true), !simulate)
|
|
||||||
|
|
||||||
if (paths.size == 1) {
|
|
||||||
// Single path, fast scenario
|
|
||||||
val path = paths[0]
|
|
||||||
val pathTransferred = path.transfer(residue, simulate, snapshot)
|
|
||||||
if (pathTransferred <= Decimal.ZERO) continue
|
|
||||||
val thisReceived = it.receiveEnergy(pathTransferred, simulate)
|
|
||||||
|
|
||||||
// If cable transferred more than machine accepted, then "refund" energy
|
|
||||||
// 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
|
|
||||||
residue -= thisReceived
|
|
||||||
if (!residue.isPositive) return received
|
|
||||||
} else if (paths.size >= 2) {
|
|
||||||
// Multiple paths, a bit more complicated
|
|
||||||
// Determine how much machine is likely to accept
|
|
||||||
val potentiallyAccepted = it.receiveEnergy(residue, true)
|
|
||||||
|
|
||||||
// Won't accept anything
|
|
||||||
if (potentiallyAccepted <= Decimal.ZERO) continue
|
|
||||||
|
|
||||||
// Now determine combined available throughput
|
|
||||||
// Make a copy of snapshot, so we can freely write into it
|
|
||||||
val copy = snapshot.clone()
|
|
||||||
var calcResidue = potentiallyAccepted
|
|
||||||
|
|
||||||
// TODO: Currently, all transfers cause Braess's paradox, because of greedy selection of "fastest" cable
|
|
||||||
// Need to implement heuristics to better distribute load across different paths/segments
|
|
||||||
for (path in paths) {
|
|
||||||
val passed = path.transfer(calcResidue, true, copy)
|
|
||||||
calcResidue -= passed
|
|
||||||
if (calcResidue <= Decimal.ZERO) break
|
|
||||||
}
|
|
||||||
|
|
||||||
if (calcResidue == potentiallyAccepted) {
|
|
||||||
// мда
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var thisReceived = it.receiveEnergy(potentiallyAccepted - calcResidue, simulate)
|
val it = side.neighbour.get()
|
||||||
received += thisReceived
|
|
||||||
residue -= thisReceived
|
|
||||||
|
|
||||||
for (path in paths) {
|
if (it == null || it is EnergyCableBlockEntity.CableSide) {
|
||||||
val passed = path.transfer(thisReceived, simulate, snapshot)
|
indicesToRemove.add(i)
|
||||||
|
livelyNodes.remove(pair)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if (!simulate)
|
val paths = getPath(fromNode, node, it.receiveEnergy(residue, true), !simulate)
|
||||||
|
|
||||||
|
if (paths.size == 1) {
|
||||||
|
// Single path, fast scenario
|
||||||
|
val path = paths[0]
|
||||||
|
val pathTransferred = path.transfer(residue, simulate, snapshot)
|
||||||
|
if (pathTransferred <= Decimal.ZERO) continue
|
||||||
|
val thisReceived = it.receiveEnergy(pathTransferred, simulate)
|
||||||
|
|
||||||
|
// If cable transferred more than machine accepted, then "refund" energy
|
||||||
|
// 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()
|
path.triggerBlockstateUpdates()
|
||||||
|
}
|
||||||
|
|
||||||
thisReceived -= passed
|
received += thisReceived
|
||||||
if (thisReceived <= Decimal.ZERO) break
|
residue -= thisReceived
|
||||||
}
|
if (!residue.isPositive) return received
|
||||||
|
} else if (paths.size >= 2) {
|
||||||
if (!residue.isPositive) return received
|
// Multiple paths, a bit more complicated
|
||||||
//check(thisReceived <= Decimal.ZERO) { "Путом, алло, Путом, какого чёрта Путом? Путом почему ты заблокировал логику, а Путом?" }
|
// Determine how much machine is likely to accept
|
||||||
|
val potentiallyAccepted = it.receiveEnergy(residue, true)
|
||||||
if (thisReceived > Decimal.ZERO) {
|
|
||||||
LOGGER.warn("Cable path from $fromNode to $node doesn't follow common sense, disabling.")
|
// Won't accept anything
|
||||||
paths.forEach { it.shortCircuit = true }
|
if (potentiallyAccepted <= Decimal.ZERO) continue
|
||||||
|
|
||||||
|
// Now determine combined available throughput
|
||||||
|
// Make a copy of snapshot, so we can freely write into it
|
||||||
|
val copy = snapshot.clone()
|
||||||
|
var calcResidue = potentiallyAccepted
|
||||||
|
|
||||||
|
// TODO: Currently, all transfers cause Braess's paradox, because of greedy selection of "fastest" cable
|
||||||
|
// Need to implement heuristics to better distribute load across different paths/segments
|
||||||
|
for (path in paths) {
|
||||||
|
val passed = path.transfer(calcResidue, true, copy)
|
||||||
|
calcResidue -= passed
|
||||||
|
if (calcResidue <= Decimal.ZERO) break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (calcResidue == potentiallyAccepted) {
|
||||||
|
// мда
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var thisReceived = it.receiveEnergy(potentiallyAccepted - calcResidue, simulate)
|
||||||
|
received += thisReceived
|
||||||
|
residue -= thisReceived
|
||||||
|
|
||||||
|
for (path in paths) {
|
||||||
|
val passed = path.transfer(thisReceived, simulate, snapshot)
|
||||||
|
|
||||||
|
if (!simulate)
|
||||||
|
path.triggerBlockstateUpdates()
|
||||||
|
|
||||||
|
thisReceived -= passed
|
||||||
|
if (thisReceived <= Decimal.ZERO) break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!residue.isPositive) return received
|
||||||
|
//check(thisReceived <= Decimal.ZERO) { "Путом, алло, Путом, какого чёрта Путом? Путом почему ты заблокировал логику, а Путом?" }
|
||||||
|
|
||||||
|
if (thisReceived > Decimal.ZERO) {
|
||||||
|
LOGGER.warn("Cable path from $fromNode to $node doesn't follow common sense, disabling.")
|
||||||
|
paths.forEach { it.shortCircuit = true }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
fromNode.currentlyTransferringTo.remove(pair)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recursionLevel--
|
||||||
|
|
||||||
|
if (isMaster && indicesToRemove.isNotEmpty()) {
|
||||||
|
val indices = indicesToRemove.iterator(indicesToRemove.lastInt())
|
||||||
|
|
||||||
|
while (indices.hasPrevious()) {
|
||||||
|
livelyNodesList.removeAt(indices.previousInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
indicesToRemove.clear()
|
||||||
|
}
|
||||||
|
|
||||||
return received
|
return received
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ open class MatteryMenuSlot(container: Container, index: Int, x: Int = 0, y: Int
|
|||||||
}
|
}
|
||||||
|
|
||||||
open fun canTakeItemForPickAll(): Boolean {
|
open fun canTakeItemForPickAll(): Boolean {
|
||||||
return (container.containerSlotOrNull(slotIndex) as? IFilteredContainerSlot)?.filter == null
|
return (container.containerSlotOrNull(slotIndex) as? IFilteredContainerSlot)?.filter?.allowAll != false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMaxStackSize(): Int {
|
override fun getMaxStackSize(): Int {
|
||||||
|
Loading…
Reference in New Issue
Block a user