Enhanced inplace rdf update.

This commit is contained in:
genxium 2023-02-17 18:54:51 +08:00
parent 60866674b5
commit b19549b0a8
9 changed files with 7681 additions and 225 deletions

View File

@ -518,7 +518,7 @@
"array": [
0,
0,
209.66956379694378,
210.4441731196186,
0,
0,
0,

View File

@ -188,6 +188,8 @@ cc.Class({
prefabbedInputList[k] = (prefabbedInputList[k] & 15);
}
}
// [WARNING] Do not blindly use "selfJoinIndexMask" here, as the "actuallyUsedInput for self" couldn't be confirmed while prefabbing, otherwise we'd have confirmed a wrong self input by "_markConfirmationIfApplicable()"!
let initConfirmedList = 0;
if (null != existingInputFrame) {
// When "null != existingInputFrame", it implies that "true == canConfirmSelf" here
@ -197,10 +199,21 @@ cc.Class({
prefabbedInputList[(joinIndex - 1)] = currSelfInput;
while (self.recentInputCache.GetEdFrameId() <= inputFrameId) {
// Fill the gap
// [WARNING] Do not blindly use "selfJoinIndexMask" here, as the "actuallyUsedInput for self" couldn't be confirmed while prefabbing, otherwise we'd have confirmed a wrong self input by "_markConfirmationIfApplicable()"!
const prefabbedInputFrameDownsync = gopkgs.NewInputFrameDownsync(self.recentInputCache.GetEdFrameId(), prefabbedInputList, initConfirmedList);
// console.log(`Prefabbed inputFrameId=${prefabbedInputFrameDownsync.GetInputFrameId()}`);
self.recentInputCache.Put(prefabbedInputFrameDownsync);
const gapInputFrameId = self.recentInputCache.GetEdFrameId();
self.recentInputCache.DryPut();
let ifdHolder = gopkgs.GetInputFrameDownsync(self.recentInputCache, gapInputFrameId);
if (null == ifdHolder) {
// Lazy heap alloc, calling "gopkgs.NewInputFrameDownsync" would trigger not only heap alloc but also "gopherjs $externalize", neither is efficient T_T
const prefabbedInputFrameDownsync = gopkgs.NewInputFrameDownsync(gapInputFrameId, prefabbedInputList, initConfirmedList);
// console.log(`Prefabbed inputFrameId=${prefabbedInputFrameDownsync.GetInputFrameId()}`);
self.recentInputCache.SetByFrameId(prefabbedInputFrameDownsync, gapInputFrameId);
} else {
gopkgs.SetInputFrameId(ifdHolder, gapInputFrameId);
for (let k = 0; k < window.boundRoomCapacity; ++k) {
gopkgs.SetInput(ifdHolder, k, prefabbedInputList[k]);
}
gopkgs.SetConfirmedList(ifdHolder, initConfirmedList);
}
}
return [previousSelfInput, currSelfInput];
@ -680,7 +693,7 @@ cc.Class({
}
// This function is also applicable to "re-joining".
const rdf = gopkgs.NewRoomDownsyncFrameJs(pbRdf.id, jsPlayersArr, pbRdf.bulletLocalIdCounter, jsMeleeBulletsArr, jsFireballBulletsArr);
const rdf = gopkgs.NewRoomDownsyncFrameJs(pbRdf.id, jsPlayersArr, pbRdf.bulletLocalIdCounter, jsMeleeBulletsArr, jsFireballBulletsArr); // TODO: Check whether a "proper" preallocated rdf is available and reuse it to avoid redundant heap alloc. By "proper" I mean "pbRdf.id" should yield a "non window.RING_BUFF_FAILED_TO_SET" result, yet currently it's a bit difficult to sort out the following codes for efficient reuse, thus I'm keeping it as-is.
const self = window.mapIns;
self.onInputFrameDownsyncBatch(accompaniedInputFrameDownsyncBatch); // Important to do this step before setting IN_BATTLE
if (!self.recentRenderCache) {

View File

@ -35,7 +35,7 @@ cc.Class({
self.inputFrameUpsyncDelayTolerance = 2;
self.collisionMinStep = 8;
self.renderCacheSize = 1024;
self.renderCacheSize = 128;
self.serverFps = 60;
self.rollbackEstimatedDt = 0.016667;
self.rollbackEstimatedDtMillis = 16.667;

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"ver": "1.0.5",
"uuid": "eeaa56f4-bd6c-4208-bec4-6ab1aa39ca93",
"uuid": "02c5cdc1-9797-49ab-bc11-963215909926",
"isPlugin": true,
"loadPluginInWeb": true,
"loadPluginInNative": true,

View File

@ -571,9 +571,19 @@ func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(inputsBuffer *resolv.Rin
var ret *RoomDownsyncFrame = nil
candidate := renderFrameBuffer.GetByFrameId(nextRenderFrameId)
if nil == candidate {
// Lazy alloc heap-mem for holder, will be called on each "nextRenderFrameId == renderFrameBuffer.EdFrameId"
ret = NewPreallocatedRoomDownsyncFrame(roomCapacity, 64, 64)
renderFrameBuffer.SetByFrameId(ret, nextRenderFrameId)
if nextRenderFrameId == renderFrameBuffer.EdFrameId {
renderFrameBuffer.DryPut()
candidate = renderFrameBuffer.GetByFrameId(nextRenderFrameId)
if nil == candidate {
// Lazy alloc heap-mem for holder
ret = NewPreallocatedRoomDownsyncFrame(roomCapacity, 64, 64)
renderFrameBuffer.SetByFrameId(ret, nextRenderFrameId)
} else {
ret = candidate.(*RoomDownsyncFrame)
}
} else {
panic("Invalid nextRenderFrameId=" + string(nextRenderFrameId) + "!")
}
} else {
ret = candidate.(*RoomDownsyncFrame)
}
@ -893,6 +903,8 @@ func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(inputsBuffer *resolv.Rin
fireballBulletCnt++
}
}
// Explicitly specify termination of fireball bullets
nextRenderFrameFireballBullets[fireballBulletCnt].BattleAttr.BulletLocalId = TERMINATING_BULLET_LOCAL_ID
for _, prevMelee := range currRenderFrame.MeleeBullets {
if TERMINATING_BULLET_LOCAL_ID == prevMelee.BattleAttr.BulletLocalId {
@ -931,6 +943,8 @@ func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(inputsBuffer *resolv.Rin
meleeBulletCnt++
}
}
// Explicitly specify termination of melee bullets
nextRenderFrameMeleeBullets[meleeBulletCnt].BattleAttr.BulletLocalId = TERMINATING_BULLET_LOCAL_ID
// 4. Calc pushbacks for each player (after its movement) w/o bullets
for i, currPlayerDownsync := range currRenderFrame.PlayersArr {

View File

@ -1,198 +0,0 @@
package battle
import (
"resolv"
)
/*
[WARNING] NOT USED ANYWHERE YET!!!
*/
type InplaceRingBuffer struct {
Ed int32 // write index, open index
St int32 // read index, closed index
EdFrameId int32
StFrameId int32
N int32
Cnt int32 // the count of valid elements in the buffer, used mainly to distinguish what "st == ed" means for "Pop" and "Get" methods
Eles []interface{}
}
func NewInplaceRingBuffer(n int32) *InplaceRingBuffer {
return &InplaceRingBuffer{
Ed: 0,
St: 0,
EdFrameId: 0,
StFrameId: 0,
N: n,
Cnt: 0,
Eles: make([]interface{}, n),
}
}
func (rb *InplaceRingBuffer) Put(pItem interface{}) {
switch pItem.(type) {
case *RoomDownsyncFrame:
default:
// Other types are not supported!
return
}
for 0 < rb.Cnt && rb.Cnt >= rb.N {
// Make room for the new element
rb.Pop(nil)
}
switch v := pItem.(type) {
case *RoomDownsyncFrame:
CloneRoomDownsyncFrame(v.Id, v.PlayersArr, v.BulletLocalIdCounter, v.MeleeBullets, v.FireballBullets, rb.Eles[rb.Ed].(*RoomDownsyncFrame))
}
rb.EdFrameId++
rb.Cnt++
rb.Ed++
if rb.Ed >= rb.N {
rb.Ed -= rb.N // Deliberately not using "%" operator for performance concern
}
}
func (rb *InplaceRingBuffer) Pop(holder interface{}) bool {
switch holder.(type) {
case *RoomDownsyncFrame, nil:
default:
// Other types are not supported!
return false
}
if 0 == rb.Cnt {
return false
}
switch u := holder.(type) {
case *RoomDownsyncFrame:
v := rb.Eles[rb.St].(*RoomDownsyncFrame)
CloneRoomDownsyncFrame(v.Id, v.PlayersArr, v.BulletLocalIdCounter, v.MeleeBullets, v.FireballBullets, u)
// If nil, there's no holder for output, I'm OK for that...
}
rb.StFrameId++
rb.Cnt--
rb.St++
if rb.St >= rb.N {
rb.St -= rb.N
}
return true
}
func (rb *InplaceRingBuffer) GetArrIdxByOffset(offsetFromSt int32) int32 {
if 0 == rb.Cnt || 0 > offsetFromSt {
return -1
}
arrIdx := rb.St + offsetFromSt
if rb.St < rb.Ed {
// case#1: 0...st...ed...N-1
if rb.St <= arrIdx && arrIdx < rb.Ed {
return arrIdx
}
} else {
// if rb.St >= rb.Ed
// case#2: 0...ed...st...N-1
if arrIdx >= rb.N {
arrIdx -= rb.N
}
if arrIdx >= rb.St || arrIdx < rb.Ed {
return arrIdx
}
}
return -1
}
func (rb *InplaceRingBuffer) GetByOffset(offsetFromSt int32, holder interface{}) bool {
switch holder.(type) {
case *RoomDownsyncFrame:
default:
// Other types are not supported!
return false
}
arrIdx := rb.GetArrIdxByOffset(offsetFromSt)
if -1 == arrIdx {
return false
}
switch u := holder.(type) {
case *RoomDownsyncFrame:
v := rb.Eles[arrIdx].(*RoomDownsyncFrame)
CloneRoomDownsyncFrame(v.Id, v.PlayersArr, v.BulletLocalIdCounter, v.MeleeBullets, v.FireballBullets, u)
}
return true
}
func (rb *InplaceRingBuffer) GetByFrameId(frameId int32, holder interface{}) bool {
switch holder.(type) {
case *RoomDownsyncFrame:
default:
// Other types are not supported!
return false
}
if frameId >= rb.EdFrameId || frameId < rb.StFrameId {
return false
}
return rb.GetByOffset(frameId-rb.StFrameId, holder)
}
// [WARNING] During a battle, frontend could receive non-consecutive frames (either renderFrame or inputFrame) due to resync, the buffer should handle these frames properly.
func (rb *InplaceRingBuffer) SetByFrameId(pItem interface{}, frameId int32) (int32, int32, int32) {
oldStFrameId, oldEdFrameId := rb.StFrameId, rb.EdFrameId
switch pItem.(type) {
case *RoomDownsyncFrame:
default:
// Other types are not supported!
return resolv.RING_BUFF_FAILED_TO_SET, oldStFrameId, oldEdFrameId
}
if frameId < oldStFrameId {
return resolv.RING_BUFF_FAILED_TO_SET, oldStFrameId, oldEdFrameId
}
// By now "rb.StFrameId <= frameId"
if oldEdFrameId > frameId {
arrIdx := rb.GetArrIdxByOffset(frameId - rb.StFrameId)
if -1 != arrIdx {
switch v := pItem.(type) {
case *RoomDownsyncFrame:
CloneRoomDownsyncFrame(v.Id, v.PlayersArr, v.BulletLocalIdCounter, v.MeleeBullets, v.FireballBullets, rb.Eles[arrIdx].(*RoomDownsyncFrame))
return resolv.RING_BUFF_CONSECUTIVE_SET, oldStFrameId, oldEdFrameId
default:
// Other types are not supported!
return resolv.RING_BUFF_FAILED_TO_SET, oldStFrameId, oldEdFrameId
}
}
}
// By now "rb.EdFrameId <= frameId"
ret := resolv.RING_BUFF_CONSECUTIVE_SET
if oldEdFrameId < frameId {
rb.St, rb.Ed = 0, 0
rb.StFrameId, rb.EdFrameId = frameId, frameId
rb.Cnt = 0
ret = resolv.RING_BUFF_NON_CONSECUTIVE_SET
}
// By now "rb.EdFrameId == frameId"
rb.Put(pItem)
return ret, oldStFrameId, oldEdFrameId
}
func (rb *InplaceRingBuffer) Clear() {
for 0 < rb.Cnt {
rb.Pop(nil)
}
rb.St = 0
rb.Ed = 0
rb.StFrameId = 0
rb.EdFrameId = 0
}
func (rb *InplaceRingBuffer) GetStFrameId() int32 {
return rb.StFrameId
}
func (rb *InplaceRingBuffer) GetEdFrameId() int32 {
return rb.EdFrameId
}
func (rb *InplaceRingBuffer) GetCnt() int32 {
return rb.Cnt
}

View File

@ -79,14 +79,9 @@ func NewNpcPatrolCue(flAct, frAct uint64, x, y float64) *js.Object {
}
func NewRoomDownsyncFrameJs(id int32, playersArr []*PlayerDownsync, bulletLocalIdCounter int32, meleeBullets []*MeleeBullet, fireballBullets []*FireballBullet) *js.Object {
// [WARNING] Avoid using "pb.RoomDownsyncFrame" here, in practive "MakeFullWrapper" doesn't expose the public fields for a "protobuf struct" as expected and requires helper functions like "GetCollisionSpaceObjsJs".
return js.MakeWrapper(&RoomDownsyncFrame{
Id: id,
BulletLocalIdCounter: bulletLocalIdCounter,
PlayersArr: playersArr,
MeleeBullets: meleeBullets,
FireballBullets: fireballBullets,
})
preallocatedRdf := NewPreallocatedRoomDownsyncFrame(len(playersArr), 64, 64)
CloneRoomDownsyncFrame(id, playersArr, bulletLocalIdCounter, meleeBullets, fireballBullets, preallocatedRdf)
return js.MakeWrapper(preallocatedRdf)
}
func GetCollisionSpaceObjsJs(space *resolv.Space) []*js.Object {
@ -138,6 +133,28 @@ func GetInput(ifd *InputFrameDownsync, i int) uint64 {
return ifd.InputList[i]
}
func SetInputFrameId(ifd *InputFrameDownsync, newVal int32) bool {
// [WARNING] This function should be only used by frontend which is single-threaded; on the backend more rigorous thread-safety concerns are taken care of by proper locking.
ifd.InputFrameId = newVal
return true
}
func SetInput(ifd *InputFrameDownsync, i int, newVal uint64) bool {
// [WARNING] This function should be only used by frontend which is single-threaded; on the backend more rigorous thread-safety concerns are taken care of by proper locking.
if i >= len(ifd.InputList) {
return false
}
ifd.InputList[i] = newVal
return true
}
func SetConfirmedList(ifd *InputFrameDownsync, newVal uint64) bool {
// [WARNING] This function should be only used by frontend which is single-threaded; on the backend more rigorous thread-safety concerns are taken care of by proper locking.
ifd.ConfirmedList = newVal
return true
}
func GetPlayer(rdf *RoomDownsyncFrame, i int) *js.Object {
// [WARNING] Calling "rdf.GetPlayersArr()" directly from transpiled frontend code would automatically invoke the expensive "$externalize" and "$mapArray"! See profiling result for more details.
return js.MakeWrapper(rdf.PlayersArr[i])
@ -192,5 +209,8 @@ func main() {
"GetFireballBullet": GetFireballBullet,
"GetInput": GetInput,
"NewDynamicRectangleColliders": NewDynamicRectangleColliders,
"SetInputFrameId": SetInputFrameId,
"SetInput": SetInput,
"SetConfirmedList": SetConfirmedList,
})
}

View File

@ -28,6 +28,19 @@ func NewRingBuffer(n int32) *RingBuffer {
}
}
func (rb *RingBuffer) DryPut() {
for 0 < rb.Cnt && rb.Cnt >= rb.N {
// Make room for the new element
rb.Pop()
}
rb.EdFrameId++
rb.Cnt++
rb.Ed++
if rb.Ed >= rb.N {
rb.Ed -= rb.N // Deliberately not using "%" operator for performance concern
}
}
func (rb *RingBuffer) Put(pItem interface{}) {
for 0 < rb.Cnt && rb.Cnt >= rb.N {
// Make room for the new element