mirror of
https://github.com/genxium/DelayNoMore
synced 2025-10-09 00:26:39 +00:00
Enhanced inplace rdf update.
This commit is contained in:
@@ -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 {
|
||||
|
@@ -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
|
||||
}
|
@@ -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,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user