dynamic queue production and consumption

This commit is contained in:
RandomityGuy 2024-01-30 02:04:58 +05:30
parent 9eb801cc87
commit e01f13a845
4 changed files with 25 additions and 20 deletions

View file

@ -243,7 +243,6 @@ class Marble extends GameObject {
public var contacts:Array<CollisionInfo> = []; public var contacts:Array<CollisionInfo> = [];
public var bestContact:CollisionInfo; public var bestContact:CollisionInfo;
public var contactEntities:Array<CollisionEntity> = []; public var contactEntities:Array<CollisionEntity> = [];
public var collidingMarbles:Array<Marble> = [];
var queuedContacts:Array<CollisionInfo> = []; var queuedContacts:Array<CollisionInfo> = [];
var appliedImpulses:Array<{impulse:Vector, contactImpulse:Bool}> = []; var appliedImpulses:Array<{impulse:Vector, contactImpulse:Bool}> = [];
@ -296,7 +295,6 @@ class Marble extends GameObject {
var connection:net.Net.ClientConnection; var connection:net.Net.ClientConnection;
var moveMotionDir:Vector; var moveMotionDir:Vector;
var isNetUpdate:Bool = false; var isNetUpdate:Bool = false;
var collisionToken:Int = 0;
public function new() { public function new() {
super(); super();
@ -529,11 +527,6 @@ class Marble extends GameObject {
this.contacts = queuedContacts; this.contacts = queuedContacts;
var c = collisiomWorld.sphereIntersection(this.collider, timeState); var c = collisiomWorld.sphereIntersection(this.collider, timeState);
this.contactEntities = c.foundEntities; 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); contacts = contacts.concat(c.contacts);
} }
@ -1689,6 +1682,7 @@ class Marble extends GameObject {
marbleUpdate.velocity = this.velocity; marbleUpdate.velocity = this.velocity;
marbleUpdate.omega = this.omega; marbleUpdate.omega = this.omega;
marbleUpdate.move = move; marbleUpdate.move = move;
marbleUpdate.moveQueueSize = this.connection != null ? this.connection.moveManager.getQueueSize() : 255;
marbleUpdate.serialize(b); marbleUpdate.serialize(b);
return b.getBytes(); return b.getBytes();
} }
@ -1707,6 +1701,16 @@ class Marble extends GameObject {
this.collider.transform.setPosition(p.position); this.collider.transform.setPosition(p.position);
this.velocity = p.velocity; this.velocity = p.velocity;
this.omega = p.omega; 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; return true;
} }
@ -1745,11 +1749,6 @@ class Marble extends GameObject {
} }
playedSounds = []; playedSounds = [];
advancePhysics(timeState, move.move, collisionWorld, pathedInteriors); 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; physicsAccumulator = 0;
return move; return move;
// if (Net.isHost) { // if (Net.isHost) {

View file

@ -1071,7 +1071,7 @@ class MarbleWorld extends Scheduler {
ackLag = ourQueuedMoves.length; ackLag = ourQueuedMoves.length;
if (mvStored != null) { 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) // Tick the remaining moves (ours)
@ -1086,10 +1086,10 @@ class MarbleWorld extends Scheduler {
for (client => arr in lastMoves.otherMarbleUpdates) { for (client => arr in lastMoves.otherMarbleUpdates) {
if (arr.length > 0) { if (arr.length > 0) {
var m = arr[0]; var m = arr[0];
if (m.serverTicks == ourLastMoveTime) { if (m.serverTicks <= ourLastMoveTime) {
var marbleToUpdate = clientMarbles[Net.clientIdMap[client]]; var marbleToUpdate = clientMarbles[Net.clientIdMap[client]];
Debug.drawSphere(@:privateAccess marbleToUpdate.newPos, marbleToUpdate._radius); Debug.drawSphere(@:privateAccess marbleToUpdate.newPos, marbleToUpdate._radius);
m.calculationTicks = Std.int(ackLag / 2); m.calculationTicks = Std.int(ackLag);
marblesToTick.set(client, m); marblesToTick.set(client, m);
arr.shift(); arr.shift();
} }

View file

@ -39,6 +39,8 @@ class MoveManager {
var maxMoves = 45; var maxMoves = 45;
public var stall = false;
public function new(connection:ClientConnection) { public function new(connection:ClientConnection) {
queuedMoves = []; queuedMoves = [];
nextMoveId = 0; nextMoveId = 0;
@ -46,7 +48,7 @@ class MoveManager {
} }
public function recordMove(motionDir:Vector, timeState:TimeState) { public function recordMove(motionDir:Vector, timeState:TimeState) {
if (queuedMoves.length >= maxMoves) if (queuedMoves.length >= maxMoves || stall)
return queuedMoves[queuedMoves.length - 1]; return queuedMoves[queuedMoves.length - 1];
var move = new Move(); var move = new Move();
move.d = new Vector(); move.d = new Vector();
@ -154,6 +156,10 @@ class MoveManager {
} }
} }
public function getQueueSize() {
return queuedMoves.length;
}
public function acknowledgeMove(m:Int, timeState:TimeState) { public function acknowledgeMove(m:Int, timeState:TimeState) {
if (m == 65535 || m == -1) if (m == 65535 || m == -1)
return null; return null;
@ -170,7 +176,7 @@ class MoveManager {
delta = queuedMoves[0].id - lastAckMoveId; delta = queuedMoves[0].id - lastAckMoveId;
mv = queuedMoves.shift(); mv = queuedMoves.shift();
ackRTT = timeState.ticks - mv.timeState.ticks; ackRTT = timeState.ticks - mv.timeState.ticks;
maxMoves = Std.int(ackRTT * 1.5); maxMoves = ackRTT + 2;
} }
lastAckMoveId = m; lastAckMoveId = m;
return mv; return mv;

View file

@ -38,7 +38,7 @@ class MarbleUpdatePacket implements NetPacket {
var position:Vector; var position:Vector;
var velocity:Vector; var velocity:Vector;
var omega:Vector; var omega:Vector;
var collisionToken:Int; var moveQueueSize:Int;
public function new() {} public function new() {}
@ -46,7 +46,7 @@ class MarbleUpdatePacket implements NetPacket {
b.writeUInt16(clientId); b.writeUInt16(clientId);
MoveManager.packMove(move, b); MoveManager.packMove(move, b);
b.writeUInt16(serverTicks); b.writeUInt16(serverTicks);
b.writeUInt16(collisionToken); b.writeByte(moveQueueSize);
b.writeFloat(position.x); b.writeFloat(position.x);
b.writeFloat(position.y); b.writeFloat(position.y);
b.writeFloat(position.z); b.writeFloat(position.z);
@ -62,7 +62,7 @@ class MarbleUpdatePacket implements NetPacket {
clientId = b.readUInt16(); clientId = b.readUInt16();
move = MoveManager.unpackMove(b); move = MoveManager.unpackMove(b);
serverTicks = b.readUInt16(); serverTicks = b.readUInt16();
collisionToken = b.readUInt16(); moveQueueSize = b.readByte();
position = new Vector(b.readFloat(), b.readFloat(), b.readFloat()); position = new Vector(b.readFloat(), b.readFloat(), b.readFloat());
velocity = 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()); omega = new Vector(b.readFloat(), b.readFloat(), b.readFloat());