From e01f13a845e6e6e542ff0656170f758ce2a17587 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Tue, 30 Jan 2024 02:04:58 +0530 Subject: [PATCH] dynamic queue production and consumption --- src/Marble.hx | 23 +++++++++++------------ src/MarbleWorld.hx | 6 +++--- src/net/MoveManager.hx | 10 ++++++++-- src/net/NetPacket.hx | 6 +++--- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Marble.hx b/src/Marble.hx index c6135b87..21b107e7 100644 --- a/src/Marble.hx +++ b/src/Marble.hx @@ -243,7 +243,6 @@ class Marble extends GameObject { public var contacts:Array = []; public var bestContact:CollisionInfo; public var contactEntities:Array = []; - public var collidingMarbles:Array = []; var queuedContacts:Array = []; var appliedImpulses:Array<{impulse:Vector, contactImpulse:Bool}> = []; @@ -296,7 +295,6 @@ class Marble extends GameObject { var connection:net.Net.ClientConnection; var moveMotionDir:Vector; var isNetUpdate:Bool = false; - var collisionToken:Int = 0; public function new() { super(); @@ -529,11 +527,6 @@ class Marble extends GameObject { this.contacts = queuedContacts; var c = collisiomWorld.sphereIntersection(this.collider, timeState); this.contactEntities = c.foundEntities; - this.collidingMarbles = []; - for (e in this.contacts) { - if (e.collider is SphereCollisionEntity) - this.collidingMarbles.push(cast(e.collider, SphereCollisionEntity).marble); - } contacts = contacts.concat(c.contacts); } @@ -1689,6 +1682,7 @@ class Marble extends GameObject { marbleUpdate.velocity = this.velocity; marbleUpdate.omega = this.omega; marbleUpdate.move = move; + marbleUpdate.moveQueueSize = this.connection != null ? this.connection.moveManager.getQueueSize() : 255; marbleUpdate.serialize(b); return b.getBytes(); } @@ -1707,6 +1701,16 @@ class Marble extends GameObject { this.collider.transform.setPosition(p.position); this.velocity = p.velocity; this.omega = p.omega; + if (this.controllable && Net.isClient) { + // We are client, need to do something about the queue + var mm = Net.clientConnection.moveManager; + // trace('Queue size: ${mm.getQueueSize()}, server: ${p.moveQueueSize}'); + if (mm.getQueueSize() / p.moveQueueSize < 2) { + mm.stall = true; + } else { + mm.stall = false; + } + } return true; } @@ -1745,11 +1749,6 @@ class Marble extends GameObject { } playedSounds = []; advancePhysics(timeState, move.move, collisionWorld, pathedInteriors); - for (marble in this.collidingMarbles) { - marble.collisionToken = timeState.ticks; - } - if (this.collidingMarbles.length != 0) - this.collisionToken = timeState.ticks; physicsAccumulator = 0; return move; // if (Net.isHost) { diff --git a/src/MarbleWorld.hx b/src/MarbleWorld.hx index bee91fd1..ac6f0e3f 100644 --- a/src/MarbleWorld.hx +++ b/src/MarbleWorld.hx @@ -1071,7 +1071,7 @@ class MarbleWorld extends Scheduler { ackLag = ourQueuedMoves.length; if (mvStored != null) { - trace('ACK Lag: ${timeState.ticks - mvStored.timeState.ticks} ${ourQueuedMoves.length}'); + // trace('ACK Lag: ${timeState.ticks - mvStored.timeState.ticks} ${ourQueuedMoves.length}'); } // Tick the remaining moves (ours) @@ -1086,10 +1086,10 @@ class MarbleWorld extends Scheduler { for (client => arr in lastMoves.otherMarbleUpdates) { if (arr.length > 0) { var m = arr[0]; - if (m.serverTicks == ourLastMoveTime) { + if (m.serverTicks <= ourLastMoveTime) { var marbleToUpdate = clientMarbles[Net.clientIdMap[client]]; Debug.drawSphere(@:privateAccess marbleToUpdate.newPos, marbleToUpdate._radius); - m.calculationTicks = Std.int(ackLag / 2); + m.calculationTicks = Std.int(ackLag); marblesToTick.set(client, m); arr.shift(); } diff --git a/src/net/MoveManager.hx b/src/net/MoveManager.hx index 63ff9e43..228ebe43 100644 --- a/src/net/MoveManager.hx +++ b/src/net/MoveManager.hx @@ -39,6 +39,8 @@ class MoveManager { var maxMoves = 45; + public var stall = false; + public function new(connection:ClientConnection) { queuedMoves = []; nextMoveId = 0; @@ -46,7 +48,7 @@ class MoveManager { } public function recordMove(motionDir:Vector, timeState:TimeState) { - if (queuedMoves.length >= maxMoves) + if (queuedMoves.length >= maxMoves || stall) return queuedMoves[queuedMoves.length - 1]; var move = new Move(); move.d = new Vector(); @@ -154,6 +156,10 @@ class MoveManager { } } + public function getQueueSize() { + return queuedMoves.length; + } + public function acknowledgeMove(m:Int, timeState:TimeState) { if (m == 65535 || m == -1) return null; @@ -170,7 +176,7 @@ class MoveManager { delta = queuedMoves[0].id - lastAckMoveId; mv = queuedMoves.shift(); ackRTT = timeState.ticks - mv.timeState.ticks; - maxMoves = Std.int(ackRTT * 1.5); + maxMoves = ackRTT + 2; } lastAckMoveId = m; return mv; diff --git a/src/net/NetPacket.hx b/src/net/NetPacket.hx index 66b2ad46..585a527c 100644 --- a/src/net/NetPacket.hx +++ b/src/net/NetPacket.hx @@ -38,7 +38,7 @@ class MarbleUpdatePacket implements NetPacket { var position:Vector; var velocity:Vector; var omega:Vector; - var collisionToken:Int; + var moveQueueSize:Int; public function new() {} @@ -46,7 +46,7 @@ class MarbleUpdatePacket implements NetPacket { b.writeUInt16(clientId); MoveManager.packMove(move, b); b.writeUInt16(serverTicks); - b.writeUInt16(collisionToken); + b.writeByte(moveQueueSize); b.writeFloat(position.x); b.writeFloat(position.y); b.writeFloat(position.z); @@ -62,7 +62,7 @@ class MarbleUpdatePacket implements NetPacket { clientId = b.readUInt16(); move = MoveManager.unpackMove(b); serverTicks = b.readUInt16(); - collisionToken = b.readUInt16(); + moveQueueSize = b.readByte(); position = new Vector(b.readFloat(), b.readFloat(), b.readFloat()); velocity = new Vector(b.readFloat(), b.readFloat(), b.readFloat()); omega = new Vector(b.readFloat(), b.readFloat(), b.readFloat());