token ring stuff wip

This commit is contained in:
minjaesong
2025-03-01 23:03:56 +09:00
parent f154b2348b
commit f74b7218b0
4 changed files with 192 additions and 8 deletions

View File

@@ -98,6 +98,7 @@ open class Electric : FixtureBase {
/** Triggered when 'digital_bit' is held low. This function WILL NOT be triggered simultaneously with the falling edge. Level detection only considers the real component (labeled as 'x') of the vector */
//open fun onSignalLow(readFrom: BlockBoxIndex) {}
// called right after `onRisingEdge` and `onFallingEdge` in the `updateImpl`
open fun updateSignal() {}
fun getWireStateAt(offsetX: Int, offsetY: Int, sinkType: WireEmissionType): Vector2 {

View File

@@ -11,7 +11,7 @@ import java.util.zip.CRC32
* ## The Header
*
* - (Byte1) Frame Type
* - 00 : invalid
* - 00 : invalid (mark the packet as "to be destroyed" by game)
* - FF : token (an "empty" packet for a Token Ring)
* - AA : data
* - EE : abort
@@ -107,6 +107,10 @@ data class IngameNetPacket(val byteArray: ByteArray) {
return byteArray.toBigInt32(6)
}
fun discardPacket() {
byteArray[0] = 0
}
companion object {
private fun ByteArray.makeHeader(frameType: Int, mac: Int): ByteArray {
this[0] = frameType.toByte()
@@ -114,26 +118,26 @@ data class IngameNetPacket(val byteArray: ByteArray) {
return this
}
fun makeToken(mac: Int) = ByteArray(5).makeHeader(0xff, mac)
fun makeToken(mac: Int) = IngameNetPacket(ByteArray(5).makeHeader(0xff, mac))
fun makeAbort(mac: Int) = ByteArray(5).makeHeader(0xee, mac)
fun makeAbort(mac: Int) = IngameNetPacket(ByteArray(5).makeHeader(0xee, mac))
fun makeBallot(mac: Int) = ByteArray(9).makeHeader(0x99, mac)
fun makeBallot(mac: Int) = IngameNetPacket(ByteArray(9).makeHeader(0x99, mac))
fun makeData(sender: Int, recipient: Int, data: ByteArray) = ByteArray(18 + data.size).also {
fun makeData(sender: Int, recipient: Int, data: ByteArray) = IngameNetPacket(ByteArray(18 + data.size).also {
it.makeHeader(0xaa, sender)
it.writeBigInt32(recipient, 6)
it.writeBigInt32(data.size, 10)
System.arraycopy(data, 0, it, 14, data.size)
val crc = CRC32().also { it.update(data) }.value.toInt()
it.writeBigInt32(crc, 14 + data.size)
}
})
fun makeAck(sender: Int, recipient: Int, status: Int = 0) = ByteArray(12).also {
fun makeAck(sender: Int, recipient: Int, status: Int = 0) = IngameNetPacket(ByteArray(12).also {
it.makeHeader(0x55, sender)
it.writeBigInt32(recipient, 6)
it.writeBigInt16(status, 10)
}
})
}
}

View File

@@ -1,5 +1,6 @@
package net.torvald.terrarum.modulebasegame.gameworld
import net.torvald.random.HQRNG
import net.torvald.terrarum.gameworld.TerrarumSavegameExtrafieldSerialisable
import java.util.TreeMap
@@ -10,6 +11,8 @@ import java.util.TreeMap
*/
class PacketRunner : TerrarumSavegameExtrafieldSerialisable {
@Transient private val rng = HQRNG()
private val ledger = TreeMap<Int, IngameNetPacket>()
operator fun set(id: Int, packet: IngameNetPacket) {
@@ -18,5 +21,15 @@ class PacketRunner : TerrarumSavegameExtrafieldSerialisable {
operator fun get(id: Int) = ledger[id]!!
fun addPacket(packet: IngameNetPacket): Int {
var i = rng.nextInt()
while (ledger.containsKey(i)) {
i = rng.nextInt()
}
ledger[i] = packet
return i
}
}