Terminal: Fix extraction having different latency than insertion
This commit is contained in:
parent
1c3e358f2e
commit
d61ed8b7cc
|
@ -26,8 +26,6 @@ import net.shadowfacts.phycon.init.PhyBlockEntities
|
|||
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
||||
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
||||
import net.shadowfacts.phycon.network.packet.*
|
||||
import net.shadowfacts.phycon.util.fromTag
|
||||
import net.shadowfacts.phycon.util.toTag
|
||||
import java.util.*
|
||||
import kotlin.math.min
|
||||
|
||||
|
@ -44,6 +42,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
|
||||
val internalBuffer = TerminalBufferInventory(18)
|
||||
|
||||
private val locateRequestQueue = LinkedList<StackLocateRequest>()
|
||||
private val pendingRequests = LinkedList<StackLocateRequest>()
|
||||
private val pendingInsertions = Int2ObjectArrayMap<PendingStackInsertion>()
|
||||
|
||||
|
@ -85,7 +84,6 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
if (request != null) {
|
||||
request.results.add(packet.amount to packet.sourceInterface)
|
||||
if (request.totalResultAmount >= request.amount || counter - request.timestamp >= LOCATE_REQUEST_TIMEOUT || request.results.size >= inventoryCache.size) {
|
||||
pendingRequests.remove(request)
|
||||
stackLocateRequestCompleted(request)
|
||||
}
|
||||
}
|
||||
|
@ -111,9 +109,8 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
}
|
||||
if (insertion != null) {
|
||||
insertion.results.add(packet.capacity to packet.receivingInterface)
|
||||
if (insertion.totalCapacity >= insertion.stack.count || counter - insertion.timestamp >= INSERTION_TIMEOUT || insertion.results.size >= inventoryCache.size) {
|
||||
pendingInsertions.remove(insertion.bufferSlot)
|
||||
completeInsertion(insertion)
|
||||
if (insertion.isFinishable(counter)) {
|
||||
finishInsertion(insertion)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -146,6 +143,35 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
}
|
||||
}
|
||||
|
||||
private fun finishPendingInsertions() {
|
||||
if (world!!.isClient) return
|
||||
|
||||
for (insertion in pendingInsertions.values) {
|
||||
if (!insertion.isFinishable(counter)) continue
|
||||
finishInsertion(insertion)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendEnqueuedLocateRequests() {
|
||||
if (world!!.isClient) return
|
||||
|
||||
for (request in locateRequestQueue) {
|
||||
pendingRequests.add(request)
|
||||
sendToSingle(LocateStackPacket(request.stack, macAddress))
|
||||
}
|
||||
locateRequestQueue.clear()
|
||||
}
|
||||
|
||||
private fun finishPendingRequests() {
|
||||
if (world!!.isClient) return
|
||||
|
||||
for (request in pendingRequests) {
|
||||
if (request.isFinishable(counter)) {
|
||||
stackLocateRequestCompleted(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun addObserver() {
|
||||
observers++
|
||||
}
|
||||
|
@ -155,12 +181,15 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
}
|
||||
|
||||
override fun tick() {
|
||||
if (observers > 0 && (++counter % 20) == 0) {
|
||||
if (observers > 0 && (++counter % 10) == 0) {
|
||||
if (world!!.isClient) {
|
||||
println(cachedNetItems)
|
||||
} else {
|
||||
updateNetItems()
|
||||
sendEnqueuedLocateRequests()
|
||||
finishPendingRequests()
|
||||
beginInsertions()
|
||||
finishPendingInsertions()
|
||||
sync()
|
||||
}
|
||||
}
|
||||
|
@ -190,11 +219,12 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
}
|
||||
|
||||
fun requestItem(stack: ItemStack, amount: Int = stack.count) {
|
||||
pendingRequests.add(StackLocateRequest(stack, amount, counter))
|
||||
sendToSingle(LocateStackPacket(stack, macAddress))
|
||||
locateRequestQueue.add(StackLocateRequest(stack, amount, counter))
|
||||
}
|
||||
|
||||
private fun stackLocateRequestCompleted(request: StackLocateRequest) {
|
||||
pendingRequests.remove(request)
|
||||
|
||||
// todo: also sort results by interface priority
|
||||
val sortedResults = request.results.sortedByDescending { it.first }.toMutableList()
|
||||
var amountRequested = 0
|
||||
|
@ -206,7 +236,9 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
|||
}
|
||||
}
|
||||
|
||||
private fun completeInsertion(insertion: PendingStackInsertion) {
|
||||
private fun finishInsertion(insertion: PendingStackInsertion) {
|
||||
pendingInsertions.remove(insertion.bufferSlot)
|
||||
|
||||
// todo: also sort results by interface priority
|
||||
val sortedResults = insertion.results.sortedBy { it.first }.toMutableList()
|
||||
val remaining = insertion.stack
|
||||
|
@ -279,6 +311,10 @@ data class StackLocateRequest(
|
|||
) {
|
||||
val totalResultAmount: Int
|
||||
get() = results.fold(0) { acc, (amount, _) -> acc + amount }
|
||||
|
||||
fun isFinishable(currentTimestamp: Int): Boolean {
|
||||
return totalResultAmount > amount || currentTimestamp - timestamp >= TerminalBlockEntity.LOCATE_REQUEST_TIMEOUT
|
||||
}
|
||||
}
|
||||
|
||||
data class PendingStackInsertion(
|
||||
|
@ -289,4 +325,8 @@ data class PendingStackInsertion(
|
|||
) {
|
||||
val totalCapacity: Int
|
||||
get() = results.fold(0) { acc, (amount, _) -> acc + amount }
|
||||
|
||||
fun isFinishable(currentTimestamp: Int): Boolean {
|
||||
return totalCapacity > stack.count || currentTimestamp - timestamp >= TerminalBlockEntity.INSERTION_TIMEOUT
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue