From d61ed8b7cc0c058d6cba6dc01b435a616900e650 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 11 Feb 2021 22:32:42 -0500 Subject: [PATCH] Terminal: Fix extraction having different latency than insertion --- .../block/terminal/TerminalBlockEntity.kt | 60 +++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt index 25db5e8..8c43d35 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt @@ -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() val internalBuffer = TerminalBufferInventory(18) + private val locateRequestQueue = LinkedList() private val pendingRequests = LinkedList() private val pendingInsertions = Int2ObjectArrayMap() @@ -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 + } }