From ab6a04693d99125c4caeddbb2532d97324cf0adf Mon Sep 17 00:00:00 2001 From: genxium Date: Thu, 5 Jan 2023 12:14:55 +0800 Subject: [PATCH] Enhanced backend input prediction alignment with the frontend counterpart. --- battle_srv/models/player.go | 7 ++++--- battle_srv/models/room.go | 23 +++++++++++++---------- frontend/assets/scenes/login.fire | 2 +- frontend/assets/scripts/Map.js | 8 -------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/battle_srv/models/player.go b/battle_srv/models/player.go index 11fe20e..6ec281b 100644 --- a/battle_srv/models/player.go +++ b/battle_srv/models/player.go @@ -46,9 +46,10 @@ type Player struct { TutorialStage int `db:"tutorial_stage"` // other in-battle info fields - LastSentInputFrameId int32 - AckingFrameId int32 - AckingInputFrameId int32 + LastReceivedInputFrameId int32 + LastSentInputFrameId int32 + AckingFrameId int32 + AckingInputFrameId int32 } func ExistPlayerByName(name string) (bool, error) { diff --git a/battle_srv/models/room.go b/battle_srv/models/room.go index 0cc7ac3..4b3039e 100644 --- a/battle_srv/models/room.go +++ b/battle_srv/models/room.go @@ -172,6 +172,7 @@ func (pR *Room) AddPlayerIfPossible(pPlayerFromDbInit *Player, session *websocke pPlayerFromDbInit.AckingFrameId = -1 pPlayerFromDbInit.AckingInputFrameId = -1 pPlayerFromDbInit.LastSentInputFrameId = MAGIC_LAST_SENT_INPUT_FRAME_ID_NORMAL_ADDED + pPlayerFromDbInit.LastReceivedInputFrameId = MAGIC_LAST_SENT_INPUT_FRAME_ID_NORMAL_ADDED pPlayerFromDbInit.BattleState = PlayerBattleStateIns.ADDED_PENDING_BATTLE_COLLIDER_ACK pPlayerFromDbInit.ColliderRadius = DEFAULT_PLAYER_RADIUS // Hardcoded @@ -1033,16 +1034,16 @@ func (pR *Room) getOrPrefabInputFrameDownsync(inputFrameId int32) *battle.InputF ConfirmedList: uint64(0), } - j2 := j - 1 - tmp2 := pR.InputsBuffer.GetByFrameId(j2) - if nil != tmp2 { - prevInputFrameDownsync := tmp2.(*battle.InputFrameDownsync) - for i, _ := range currInputFrameDownsync.InputList { + for i, _ := range currInputFrameDownsync.InputList { + j2 := pR.PlayersArr[i].LastReceivedInputFrameId + if j2 < pR.InputsBuffer.StFrameId { + j2 = pR.InputsBuffer.StFrameId + } + tmp2 := pR.InputsBuffer.GetByFrameId(j2) + if nil != tmp2 { + prevInputFrameDownsync := tmp2.(*battle.InputFrameDownsync) currInputFrameDownsync.InputList[i] = prevInputFrameDownsync.InputList[i] } - } - - for i, _ := range currInputFrameDownsync.InputList { // Don't predict "btnA & btnB"! currInputFrameDownsync.InputList[i] = (currInputFrameDownsync.InputList[i] & uint64(15)) } @@ -1066,8 +1067,8 @@ func (pR *Room) markConfirmationIfApplicable(inputFrameUpsyncBatch []*pb.InputFr Logger.Debug(fmt.Sprintf("Omitting obsolete inputFrameUpsync#1: roomId=%v, playerId=%v, clientInputFrameId=%v, InputsBuffer=%v", pR.Id, playerId, clientInputFrameId, pR.InputsBufferString(false))) continue } - if clientInputFrameId < pR.LastAllConfirmedInputFrameId { - Logger.Debug(fmt.Sprintf("Omitting obsolete inputFrameUpsync#2: roomId=%v, playerId=%v, clientInputFrameId=%v, InputsBuffer=%v", pR.Id, playerId, clientInputFrameId, pR.InputsBufferString(false))) + if clientInputFrameId < player.LastReceivedInputFrameId { + Logger.Debug(fmt.Sprintf("Omitting obsolete inputFrameUpsync#2: roomId=%v, playerId=%v, clientInputFrameId=%v, playerLastReceivedInputFrameId=%v, InputsBuffer=%v", pR.Id, playerId, clientInputFrameId, player.LastReceivedInputFrameId, pR.InputsBufferString(false))) continue } if clientInputFrameId > pR.InputsBuffer.EdFrameId { @@ -1079,6 +1080,8 @@ func (pR *Room) markConfirmationIfApplicable(inputFrameUpsyncBatch []*pb.InputFr targetInputFrameDownsync.InputList[player.JoinIndex-1] = inputFrameUpsync.Encoded targetInputFrameDownsync.ConfirmedList |= uint64(1 << uint32(player.JoinIndex-1)) + player.LastReceivedInputFrameId = clientInputFrameId + if clientInputFrameId > pR.LatestPlayerUpsyncedInputFrameId { pR.LatestPlayerUpsyncedInputFrameId = clientInputFrameId } diff --git a/frontend/assets/scenes/login.fire b/frontend/assets/scenes/login.fire index 76a6bfa..e3c7ca3 100644 --- a/frontend/assets/scenes/login.fire +++ b/frontend/assets/scenes/login.fire @@ -440,7 +440,7 @@ "array": [ 0, 0, - 216.50635094610968, + 210.60543794365393, 0, 0, 0, diff --git a/frontend/assets/scripts/Map.js b/frontend/assets/scripts/Map.js index e26ed8c..d81652b 100644 --- a/frontend/assets/scripts/Map.js +++ b/frontend/assets/scripts/Map.js @@ -1082,14 +1082,6 @@ othersForcedDownsyncRenderFrame=${JSON.stringify(othersForcedDownsyncRenderFrame } const nextRdf = gopkgs.ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs(self.recentInputCache, currRdf, collisionSys, collisionSysMap, self.spaceOffsetX, self.spaceOffsetY, self.chConfigsOrderedByJoinIndex); - if (3 == nextRdf.PlayersArr[0].ActiveSkillId && 3 != currRdf.PlayersArr[0].ActiveSkillId) { - console.log(`Started skill 3 at rdf.Id=${nextRdf.Id}`); - self.lastSkill3Started = nextRdf.Id; - } - if (3 != nextRdf.PlayersArr[0].ActiveSkillId && 3 == currRdf.PlayersArr[0].ActiveSkillId) { - console.log(`Stopped skill 3 at rdf.Id=${nextRdf.Id}, duration = ${nextRdf.Id-self.lastSkill3Started}`); - } - if (true == isChasing) { // [WARNING] Move the cursor "self.chaserRenderFrameId" when "true == isChasing", keep in mind that "self.chaserRenderFrameId" is not monotonic! self.chaserRenderFrameId = nextRdf.Id;