From 5370b0e207d0123071176e83f7b3505511157c48 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Fri, 31 May 2024 19:06:34 +0530 Subject: [PATCH] fix id allocation and marble nan --- src/Marble.hx | 17 +++++++++++++++++ src/net/Net.hx | 27 ++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Marble.hx b/src/Marble.hx index 5280532b..18625d6b 100644 --- a/src/Marble.hx +++ b/src/Marble.hx @@ -760,6 +760,8 @@ class Marble extends GameObject { } function computeMoveForces(m:Move, aControl:Vector, desiredOmega:Vector) { + if (this.currentUp == null) + this.currentUp = new Vector(0, 0, 1); var currentGravityDir = this.currentUp.multiply(-1); var R = currentGravityDir.multiply(-this._radius); var rollVelocity = this.omega.cross(R); @@ -1608,6 +1610,11 @@ class Marble extends GameObject { lastMove = m; } + if (m == null) { + m = new Move(); + m.d = new Vector(); + } + if (this.blastTicks < (30000 >> 5)) this.blastTicks += 1; @@ -1670,6 +1677,16 @@ class Marble extends GameObject { stoppedPaths = this.velocityCancel(timeState.currentAttemptTime, timeStep, isCentered, false, stoppedPaths, pathedInteriors); var A = this.getExternalForces(tempState, m); var a = this.applyContactForces(timeStep, m, isCentered, aControl, desiredOmega, A); + + // NaN check so OpenAL doesn't freak out + if (Math.isNaN(A.lengthSq())) { + A.set(0, 0, 0); + } + + if (Math.isNaN(a.lengthSq())) { + a.set(0, 0, 0); + } + this.velocity.set(this.velocity.x + A.x * timeStep, this.velocity.y + A.y * timeStep, this.velocity.z + A.z * timeStep); this.omega.set(this.omega.x + a.x * timeStep, this.omega.y + a.y * timeStep, this.omega.z + a.z * timeStep); if (this.mode == Start) { diff --git a/src/net/Net.hx b/src/net/Net.hx index fc9a11d0..27286cdc 100644 --- a/src/net/Net.hx +++ b/src/net/Net.hx @@ -74,6 +74,7 @@ class Net { public static var lobbyClientReady:Bool; public static var hostReady:Bool; + static var clientIdAllocs:Int = 1; public static var clientId:Int; public static var networkRNG:Float; public static var clients:Map = []; @@ -348,6 +349,7 @@ class Net { Net.client = null; Net.clientDatachannel = null; Net.clientId = 0; + Net.clientIdAllocs = 1; Net.clients.clear(); Net.clientIdMap.clear(); Net.clientConnection = null; @@ -366,6 +368,7 @@ class Net { Net.isClient = false; Net.isHost = false; Net.clientId = 0; + Net.clientIdAllocs = 1; Net.clients.clear(); Net.clientIdMap.clear(); MasterServerClient.disconnectFromMasterServer(); @@ -440,7 +443,11 @@ class Net { } static function onClientConnect(c:RTCPeerConnection, dc:RTCDataChannel, dcu:RTCDataChannel, joiningPrivate:Bool) { - clientId += 1; + var clientId = allocateClientId(); + if (clientId == -1) { + c.close(); + return; // Failed to allocate ID + } var cc = new ClientConnection(clientId, c, dc, dcu); clients.set(c, cc); cc.isPrivate = joiningPrivate; @@ -528,6 +535,10 @@ class Net { return; NetCommands.clientDisconnected(cc.id); + if (cc.id != 0) { + freeClientId(cc.id); + } + serverInfo.players = 1; for (k => v in clientIdMap) { // Recount serverInfo.players++; @@ -734,6 +745,20 @@ class Net { } } + static function allocateClientId() { + for (id in 0...32) { + if (Net.clientIdAllocs & (1 << id) == 0) { + Net.clientIdAllocs |= (1 << id); + return id; + } + } + return -1; + } + + static function freeClientId(id:Int) { + Net.clientIdAllocs &= ~(1 << id); + } + public static function sendPacketToAll(packetData:OutputBitStream) { var bytes = packetData.getBytes(); for (c => v in clients) {