Compare commits

..

No commits in common. "main" and "v1.0.12-cc" have entirely different histories.

27 changed files with 586 additions and 431 deletions

View File

@ -1,5 +1,3 @@
Please refer to [DelayNoMoreUnity](https://github.com/genxium/DelayNoMoreUnity) for a Unity rebuild with .net backend.
# Preface # Preface
This project is a demo for a websocket-based rollback netcode inspired by [GGPO](https://github.com/pond3r/ggpo/blob/master/doc/README.md). This project is a demo for a websocket-based rollback netcode inspired by [GGPO](https://github.com/pond3r/ggpo/blob/master/doc/README.md).
@ -8,13 +6,14 @@ This project is a demo for a websocket-based rollback netcode inspired by [GGPO]
![Merged_cut_annotated_spedup](./charts/Merged_cut_annotated_spedup.gif) ![Merged_cut_annotated_spedup](./charts/Merged_cut_annotated_spedup.gif)
(battle between 2 celluar 4G users using Android phones, [original video here](https://pan.baidu.com/s/1m50d-VZxEGT3IgeZtww49g?pwd=eqx1)) (battle between 2 celluar 4G users using Android phones, [original video here](https://pan.baidu.com/s/1RL-9M-cK8cFS_Q8afMTrJA?pwd=ryzv))
![Phone4g_battle_spedup](./charts/Phone4g_battle_spedup.gif) ![Phone4g_battle_spedup](./charts/Phone4g_battle_spedup.gif)
**Since v1.0.13, smoothness in worst cases (e.g. turn-around on ground, in air and after dashing) is drastically improved due to update of prediction approach. The gifs and corresponding screenrecordings above are not updated because there's no big difference when network is good -- however, `input delay` is now set to `4 frames` -- while `input delay = 6 frames` was used in the screenrecordings -- and smoothness is even better now (well there's [a new screenrecording for PcWifi vs Android4g here](https://pan.baidu.com/s/1iNrQ2l_wqbWkURMIfyG88w?pwd=fe2f)).** Key changes are listed below. **Since v1.0.12, smoothness in worst cases (e.g. turn-around on ground, in air and after dashing) is drastically improved due to update of prediction approach. The gifs and corresponding screenrecordings above are not updated because there's no big difference when network is good -- however, `input delay` is now set to `4 frames` -- while `input delay = 6 frames` was used in the screenrecordings -- and smoothness is even better now.** Key changes are listed below.
- [change#1](https://github.com/genxium/DelayNoMore/blob/c582071f4f2e3dd7e83d65562c7c99981252c358/jsexport/battle/battle.go#L647) - [change#1](https://github.com/genxium/DelayNoMore/blob/de9f3c90902bc6da98359be996887a55964e011e/jsexport/battle/battle.go#L647)
- [change#2](https://github.com/genxium/DelayNoMore/blob/c582071f4f2e3dd7e83d65562c7c99981252c358/frontend/assets/scripts/Map.js#L1446) - [change#2](https://github.com/genxium/DelayNoMore/blob/de9f3c90902bc6da98359be996887a55964e011e/frontend/assets/scripts/Map.js#L1451)
- [change#3](https://github.com/genxium/DelayNoMore/blob/de9f3c90902bc6da98359be996887a55964e011e/battle_srv/models/room.go#L1312)
As lots of feedbacks ask for a discussion on using UDP instead, I tried to summarize my personal opinion about it in [ConcerningEdgeCases](./ConcerningEdgeCases.md) -- **since v0.9.25, the project is actually equipped with UDP capabilities as follows**. As lots of feedbacks ask for a discussion on using UDP instead, I tried to summarize my personal opinion about it in [ConcerningEdgeCases](./ConcerningEdgeCases.md) -- **since v0.9.25, the project is actually equipped with UDP capabilities as follows**.
- When using the so called `native apps` on `Android` and `Windows` (I'm working casually hard to support `iOS` next), the frontends will try to use UDP hole-punching w/ the help of backend as a registry. If UDP hole-punching is working, the rollback is often less than `turn-around frames to recover` and thus not noticeable, being much better than using websocket alone. This video shows how the UDP holepunched p2p performs for [Phone-Wifi v.s. PC-Wifi (viewed by PC side)](https://pan.baidu.com/s/1K6704bJKlrSBTVqGcXhajA?pwd=l7ok). - When using the so called `native apps` on `Android` and `Windows` (I'm working casually hard to support `iOS` next), the frontends will try to use UDP hole-punching w/ the help of backend as a registry. If UDP hole-punching is working, the rollback is often less than `turn-around frames to recover` and thus not noticeable, being much better than using websocket alone. This video shows how the UDP holepunched p2p performs for [Phone-Wifi v.s. PC-Wifi (viewed by PC side)](https://pan.baidu.com/s/1K6704bJKlrSBTVqGcXhajA?pwd=l7ok).
@ -23,9 +22,9 @@ As lots of feedbacks ask for a discussion on using UDP instead, I tried to summa
# Notable Features # Notable Features
- Backend dynamics toggle via [Room.BackendDynamicsEnabled](https://github.com/genxium/DelayNoMore/blob/c582071f4f2e3dd7e83d65562c7c99981252c358/battle_srv/models/room.go#L147) - Backend dynamics toggle via [Room.BackendDynamicsEnabled](https://github.com/genxium/DelayNoMore/blob/v0.9.14/battle_srv/models/room.go#L786)
- Recovery upon reconnection (only if backend dynamics is ON) - Recovery upon reconnection (only if backend dynamics is ON)
- Automatic correction for "slow ticker", especially "active slow ticker" which is well-known to be a headache for input synchronization - Automatically correction for "slow ticker", especially "active slow ticker" which is well-known to be a headache for input synchronization
- Frame data logging toggle for both frontend & backend, useful for debugging out of sync entities when developing new features - Frame data logging toggle for both frontend & backend, useful for debugging out of sync entities when developing new features
_(how input delay roughly works)_ _(how input delay roughly works)_
@ -98,14 +97,7 @@ The easy way is to try out 2 players with test accounts on a same machine.
- Open one browser instance, visit _http://localhost:7456?expectedRoomId=1_, input `add`on the username box and click to request a captcha, this is a test account so a captcha would be returned by the backend and filled automatically (as shown in the figure below), then click and click to proceed to a matching scene. - Open one browser instance, visit _http://localhost:7456?expectedRoomId=1_, input `add`on the username box and click to request a captcha, this is a test account so a captcha would be returned by the backend and filled automatically (as shown in the figure below), then click and click to proceed to a matching scene.
- Open another browser instance, visit _http://localhost:7456?expectedRoomId=1_, input `bdd`on the username box and click to request a captcha, this is another test account so a captcha would be returned by the backend and filled automatically, then click and click to proceed, when matched a `battle`(but no competition rule yet) would start. - Open another browser instance, visit _http://localhost:7456?expectedRoomId=1_, input `bdd`on the username box and click to request a captcha, this is another test account so a captcha would be returned by the backend and filled automatically, then click and click to proceed, when matched a `battle`(but no competition rule yet) would start.
- Try out the onscreen virtual joysticks to move the cars and see if their movements are in-sync. - Try out the onscreen virtual joysticks to move the cars and see if their movements are in-sync.
![screenshot-2](./charts/screenshot-2.png)
![How-to-play-1](./charts/How-to-play-1.png)
![How-to-play-2](./charts/How-to-play-2.png)
![How-to-play-3](./charts/How-to-play-3.png)
![How-to-play-4](./charts/How-to-play-4.png)
## 2 Troubleshooting ## 2 Troubleshooting
@ -120,9 +112,9 @@ Just restart your `redis-server` process.
The most important reason for not showing "PING value" is simple: in most games the "PING value" is collected by a dedicated kernel thread which doesn't interfere the UI thread or the primary networking thread. As this demo primarily runs on browser by far, I don't have this capability easily. The most important reason for not showing "PING value" is simple: in most games the "PING value" is collected by a dedicated kernel thread which doesn't interfere the UI thread or the primary networking thread. As this demo primarily runs on browser by far, I don't have this capability easily.
Moreover, in practice I found that to spot sync anomalies, the following tools are much more useful than the "PING VALUE". Moreover, in practice I found that to spot sync anomalies, the following tools are much more useful than the "PING VALUE".
- Detection of [prediction mismatch on the frontend](https://github.com/genxium/DelayNoMore/blob/c582071f4f2e3dd7e83d65562c7c99981252c358/frontend/assets/scripts/Map.js#L968). - Detection of [prediction mismatch on the frontend](https://github.com/genxium/DelayNoMore/blob/v0.9.19/frontend/assets/scripts/Map.js#L842).
- Detection of [type#1 forceConfirmation on the backend](https://github.com/genxium/DelayNoMore/blob/c582071f4f2e3dd7e83d65562c7c99981252c358/battle_srv/models/room.go#L1315). - Detection of [type#1 forceConfirmation on the backend](https://github.com/genxium/DelayNoMore/blob/v0.9.19/battle_srv/models/room.go#L1246).
- Detection of [type#2 forceConfirmation on the backend](https://github.com/genxium/DelayNoMore/blob/c582071f4f2e3dd7e83d65562c7c99981252c358/battle_srv/models/room.go#L1328). - Detection of [type#2 forceConfirmation on the backend](https://github.com/genxium/DelayNoMore/blob/v0.9.19/battle_srv/models/room.go#L1259).
There's also some useful information displayed on the frontend when `true == Map.showNetworkDoctorInfo`. There's also some useful information displayed on the frontend when `true == Map.showNetworkDoctorInfo`.
![networkstats](./charts/networkstats.png) ![networkstats](./charts/networkstats.png)

View File

@ -157,6 +157,7 @@ type Room struct {
TmxPolygonsMap StrToPolygon2DListMap TmxPolygonsMap StrToPolygon2DListMap
rdfIdToActuallyUsedInput map[int32]*pb.InputFrameDownsync rdfIdToActuallyUsedInput map[int32]*pb.InputFrameDownsync
allowUpdateInputFrameInPlaceUponDynamics bool
LastIndividuallyConfirmedInputFrameId []int32 LastIndividuallyConfirmedInputFrameId []int32
LastIndividuallyConfirmedInputList []uint64 LastIndividuallyConfirmedInputList []uint64
@ -807,6 +808,7 @@ func (pR *Room) OnDismissed() {
pR.RenderFrameBuffer = resolv.NewRingBuffer(pR.RenderCacheSize) pR.RenderFrameBuffer = resolv.NewRingBuffer(pR.RenderCacheSize)
pR.InputsBuffer = resolv.NewRingBuffer((pR.RenderCacheSize >> 1) + 1) pR.InputsBuffer = resolv.NewRingBuffer((pR.RenderCacheSize >> 1) + 1)
pR.rdfIdToActuallyUsedInput = make(map[int32]*pb.InputFrameDownsync) pR.rdfIdToActuallyUsedInput = make(map[int32]*pb.InputFrameDownsync)
pR.allowUpdateInputFrameInPlaceUponDynamics = true
pR.LastIndividuallyConfirmedInputFrameId = make([]int32, pR.Capacity) pR.LastIndividuallyConfirmedInputFrameId = make([]int32, pR.Capacity)
for i := 0; i < pR.Capacity; i++ { for i := 0; i < pR.Capacity; i++ {
pR.LastIndividuallyConfirmedInputFrameId[i] = MAGIC_LAST_SENT_INPUT_FRAME_ID_NORMAL_ADDED pR.LastIndividuallyConfirmedInputFrameId[i] = MAGIC_LAST_SENT_INPUT_FRAME_ID_NORMAL_ADDED
@ -1213,7 +1215,7 @@ func (pR *Room) markConfirmationIfApplicable(inputFrameUpsyncBatch []*pb.InputFr
targetInputFrameDownsync.ConfirmedList |= uint64(1 << uint32(player.JoinIndex-1)) targetInputFrameDownsync.ConfirmedList |= uint64(1 << uint32(player.JoinIndex-1))
if false == fromUDP { if false == fromUDP {
/** /*
[WARNING] We have to distinguish whether or not the incoming batch is from UDP here, otherwise "pR.LatestPlayerUpsyncedInputFrameId - pR.LastAllConfirmedInputFrameId" might become unexpectedly large in case of "UDP packet loss + slow ws session"! [WARNING] We have to distinguish whether or not the incoming batch is from UDP here, otherwise "pR.LatestPlayerUpsyncedInputFrameId - pR.LastAllConfirmedInputFrameId" might become unexpectedly large in case of "UDP packet loss + slow ws session"!
Moreover, only ws session upsyncs should advance "player.LastConsecutiveRecvInputFrameId" & "pR.LatestPlayerUpsyncedInputFrameId". Moreover, only ws session upsyncs should advance "player.LastConsecutiveRecvInputFrameId" & "pR.LatestPlayerUpsyncedInputFrameId".
@ -1282,7 +1284,7 @@ func (pR *Room) markConfirmationIfApplicable(inputFrameUpsyncBatch []*pb.InputFr
snapshotStFrameId := (pR.LastAllConfirmedInputFrameId - newAllConfirmedCount) snapshotStFrameId := (pR.LastAllConfirmedInputFrameId - newAllConfirmedCount)
refRenderFrameIdIfNeeded := pR.CurDynamicsRenderFrameId - 1 refRenderFrameIdIfNeeded := pR.CurDynamicsRenderFrameId - 1
refSnapshotStFrameId := battle.ConvertToDelayedInputFrameId(refRenderFrameIdIfNeeded) refSnapshotStFrameId := battle.ConvertToDelayedInputFrameId(refRenderFrameIdIfNeeded)
if pR.BackendDynamicsEnabled && refSnapshotStFrameId < snapshotStFrameId { if refSnapshotStFrameId < snapshotStFrameId {
snapshotStFrameId = refSnapshotStFrameId snapshotStFrameId = refSnapshotStFrameId
} }
Logger.Debug(fmt.Sprintf("markConfirmationIfApplicable for roomId=%v returning newAllConfirmedCount=%d: InputsBuffer=%v", pR.Id, newAllConfirmedCount, pR.InputsBufferString(false))) Logger.Debug(fmt.Sprintf("markConfirmationIfApplicable for roomId=%v returning newAllConfirmedCount=%d: InputsBuffer=%v", pR.Id, newAllConfirmedCount, pR.InputsBufferString(false)))
@ -1307,6 +1309,9 @@ func (pR *Room) forceConfirmationIfApplicable(prevRenderFrameId int32) uint64 {
panic(fmt.Sprintf("inputFrameId=%v doesn't exist for roomId=%v! InputsBuffer=%v", j, pR.Id, pR.InputsBufferString(false))) panic(fmt.Sprintf("inputFrameId=%v doesn't exist for roomId=%v! InputsBuffer=%v", j, pR.Id, pR.InputsBufferString(false)))
} }
inputFrameDownsync := tmp.(*battle.InputFrameDownsync) inputFrameDownsync := tmp.(*battle.InputFrameDownsync)
if pR.allowUpdateInputFrameInPlaceUponDynamics {
battle.UpdateInputFrameInPlaceUponDynamics(j, pR.Capacity, inputFrameDownsync.ConfirmedList, inputFrameDownsync.InputList, pR.LastIndividuallyConfirmedInputFrameId, pR.LastIndividuallyConfirmedInputList, int32(MAGIC_JOIN_INDEX_INVALID))
}
unconfirmedMask |= (allConfirmedMask ^ inputFrameDownsync.ConfirmedList) unconfirmedMask |= (allConfirmedMask ^ inputFrameDownsync.ConfirmedList)
inputFrameDownsync.ConfirmedList = allConfirmedMask inputFrameDownsync.ConfirmedList = allConfirmedMask
pR.onInputFrameDownsyncAllConfirmed(inputFrameDownsync, -1) pR.onInputFrameDownsyncAllConfirmed(inputFrameDownsync, -1)
@ -1336,7 +1341,7 @@ func (pR *Room) forceConfirmationIfApplicable(prevRenderFrameId int32) uint64 {
func (pR *Room) produceInputsBufferSnapshotWithCurDynamicsRenderFrameAsRef(unconfirmedMask uint64, snapshotStFrameId, snapshotEdFrameId int32) *pb.InputsBufferSnapshot { func (pR *Room) produceInputsBufferSnapshotWithCurDynamicsRenderFrameAsRef(unconfirmedMask uint64, snapshotStFrameId, snapshotEdFrameId int32) *pb.InputsBufferSnapshot {
// [WARNING] This function MUST BE called while "pR.InputsBufferLock" is locked! // [WARNING] This function MUST BE called while "pR.InputsBufferLock" is locked!
refRenderFrameIdIfNeeded := pR.CurDynamicsRenderFrameId - 1 refRenderFrameIdIfNeeded := pR.CurDynamicsRenderFrameId - 1
if pR.BackendDynamicsEnabled && 0 > refRenderFrameIdIfNeeded { if 0 > refRenderFrameIdIfNeeded {
return nil return nil
} }
// Duplicate downsynced inputFrameIds will be filtered out by frontend. // Duplicate downsynced inputFrameIds will be filtered out by frontend.
@ -1387,7 +1392,7 @@ func (pR *Room) applyInputFrameDownsyncDynamics(fromRenderFrameId int32, toRende
} }
} }
battle.ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(pR.InputsBuffer, currRenderFrame.Id, pR.Space, pR.CollisionSysMap, pR.SpaceOffsetX, pR.SpaceOffsetY, pR.CharacterConfigsArr, pR.RenderFrameBuffer, pR.collisionHolder, pR.effPushbacks, pR.hardPushbackNormsArr, pR.jumpedOrNotList, pR.dynamicRectangleColliders, pR.LastIndividuallyConfirmedInputFrameId, pR.LastIndividuallyConfirmedInputList, false, MAGIC_JOIN_INDEX_INVALID) // DON'T mutate inputs upon dynamics on backend to avoid complicating the edge cases battle.ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(pR.InputsBuffer, currRenderFrame.Id, pR.Space, pR.CollisionSysMap, pR.SpaceOffsetX, pR.SpaceOffsetY, pR.CharacterConfigsArr, pR.RenderFrameBuffer, pR.collisionHolder, pR.effPushbacks, pR.hardPushbackNormsArr, pR.jumpedOrNotList, pR.dynamicRectangleColliders, pR.LastIndividuallyConfirmedInputFrameId, pR.LastIndividuallyConfirmedInputList, false, MAGIC_JOIN_INDEX_INVALID) // "allowUpdateInputFrameInPlaceUponDynamics" is instead used in "forceConfirmationIfApplicable"
pR.CurDynamicsRenderFrameId++ pR.CurDynamicsRenderFrameId++
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 MiB

After

Width:  |  Height:  |  Size: 4.4 MiB

BIN
charts/screenshot-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 684 KiB

View File

@ -461,7 +461,7 @@
"array": [ "array": [
0, 0,
0, 0,
209.57814771583418, 210.43877906529718,
0, 0,
0, 0,
0, 0,

View File

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

View File

@ -512,8 +512,7 @@ cc.Class({
window.clearBoundRoomIdInBothVolatileAndPersistentStorage(); window.clearBoundRoomIdInBothVolatileAndPersistentStorage();
window.initPersistentSessionClient(self.initAfterWSConnected, null /* Deliberately NOT passing in any `expectedRoomId`. -- YFLu */ ); window.initPersistentSessionClient(self.initAfterWSConnected, null /* Deliberately NOT passing in any `expectedRoomId`. -- YFLu */ );
}; };
resultPanelScriptIns.onCloseDelegate = () => { resultPanelScriptIns.onCloseDelegate = () => {};
};
self.gameRuleNode = cc.instantiate(self.gameRulePrefab); self.gameRuleNode = cc.instantiate(self.gameRulePrefab);
self.gameRuleNode.width = self.canvasNode.width; self.gameRuleNode.width = self.canvasNode.width;
@ -714,9 +713,9 @@ cc.Class({
if (notSelfUnconfirmed) { if (notSelfUnconfirmed) {
shouldForceDumping2 = false; shouldForceDumping2 = false;
shouldForceResync = false; shouldForceResync = false;
self.othersForcedDownsyncRenderFrameDict.set(rdfId, [pbRdf, rdf]); self.othersForcedDownsyncRenderFrameDict.set(rdfId, rdf);
if (CC_DEBUG) { if (CC_DEBUG) {
console.warn(`Someone else is forced to resync! renderFrameId=${rdfId} console.warn(`Someone else is forced to resync! renderFrameId=${rdf.GetId()}
backendUnconfirmedMask=${pbRdf.backendUnconfirmedMask} backendUnconfirmedMask=${pbRdf.backendUnconfirmedMask}
accompaniedInputFrameDownsyncBatchRange=[${null == accompaniedInputFrameDownsyncBatch ? null : accompaniedInputFrameDownsyncBatch[0].inputFrameId}, ${null == accompaniedInputFrameDownsyncBatch ? null : accompaniedInputFrameDownsyncBatch[accompaniedInputFrameDownsyncBatch.length - 1].inputFrameId}]`); accompaniedInputFrameDownsyncBatchRange=[${null == accompaniedInputFrameDownsyncBatch ? null : accompaniedInputFrameDownsyncBatch[0].inputFrameId}, ${null == accompaniedInputFrameDownsyncBatch ? null : accompaniedInputFrameDownsyncBatch[accompaniedInputFrameDownsyncBatch.length - 1].inputFrameId}]`);
} }
@ -948,7 +947,7 @@ accompaniedInputFrameDownsyncBatchRange=[${accompaniedInputFrameDownsyncBatch[0]
_handleIncorrectlyRenderedPrediction(firstPredictedYetIncorrectInputFrameId, batch, fromUDP) { _handleIncorrectlyRenderedPrediction(firstPredictedYetIncorrectInputFrameId, batch, fromUDP) {
if (null == firstPredictedYetIncorrectInputFrameId) return; if (null == firstPredictedYetIncorrectInputFrameId) return;
const self = this; const self = this;
const renderFrameId1 = gopkgs.ConvertToFirstUsedRenderFrameId(firstPredictedYetIncorrectInputFrameId); const renderFrameId1 = gopkgs.ConvertToFirstUsedRenderFrameId(firstPredictedYetIncorrectInputFrameId) - 1;
if (renderFrameId1 >= self.chaserRenderFrameId) return; if (renderFrameId1 >= self.chaserRenderFrameId) return;
/* /*
@ -1003,7 +1002,7 @@ fromUDP=${fromUDP}`);
const inputFrameId = inputFrame.inputFrameId; const inputFrameId = inputFrame.inputFrameId;
const peerEncodedInput = (true == fromUDP ? inputFrame.encoded : inputFrame.inputList[peerJoinIndex - 1]); const peerEncodedInput = (true == fromUDP ? inputFrame.encoded : inputFrame.inputList[peerJoinIndex - 1]);
if (false == self.allowRollbackOnPeerUpsync && inputFrameId <= renderedInputFrameIdUpper) { if (false == self.allowRollbackOnPeerUpsync && inputFrameId <= renderedInputFrameIdUpper) {
// [WARNING] Avoid obfuscating already rendered history, even at "inputFrameId == renderedInputFrameIdUpper", due to the use of "INPUT_SCALE_FRAMES" some previous render frames might've already been rendered with "inputFrameId"! // [WARNING] Avoid obfuscating already rendered history, even at "inputFrameId == renderedInputFrameIdUpper", due to the use of "INPUT_SCALE_FRAMES" some previous render frames might already be rendered with "inputFrameId"!
continue; continue;
} }
if (inputFrameId <= self.lastAllConfirmedInputFrameId) { if (inputFrameId <= self.lastAllConfirmedInputFrameId) {
@ -1022,11 +1021,12 @@ fromUDP=${fromUDP}`);
self.lastIndividuallyConfirmedInputList[peerJoinIndex - 1] = peerEncodedInput; self.lastIndividuallyConfirmedInputList[peerJoinIndex - 1] = peerEncodedInput;
} }
effCnt += 1; effCnt += 1;
// the returned "gopkgs.NewInputFrameDownsync.InputList" is immutable, thus we can only modify the value in "newInputList"! // the returned "gopkgs.NewInputFrameDownsync.InputList" is immutable, thus we can only modify the values in "newInputList" and "newConfirmedList"!
const existingInputList = existingInputFrame.GetInputList(); const existingInputList = existingInputFrame.GetInputList();
let newInputList = existingInputFrame.GetInputList().slice(); let newInputList = existingInputFrame.GetInputList().slice();
newInputList[peerJoinIndex - 1] = peerEncodedInput; newInputList[peerJoinIndex - 1] = peerEncodedInput;
const newInputFrameDownsyncLocal = gopkgs.NewInputFrameDownsync(inputFrameId, newInputList, existingConfirmedList); let newConfirmedList = (existingConfirmedList | peerJoinIndexMask);
const newInputFrameDownsyncLocal = gopkgs.NewInputFrameDownsync(inputFrameId, newInputList, newConfirmedList);
//console.log(`Updated encoded input of peerJoinIndex=${peerJoinIndex} to ${peerEncodedInput} for inputFrameId=${inputFrameId}/renderedInputFrameIdUpper=${renderedInputFrameIdUpper} from ${JSON.stringify(inputFrame)}; newInputFrameDownsyncLocal=${self.gopkgsInputFrameDownsyncStr(newInputFrameDownsyncLocal)}; existingInputFrame=${self.gopkgsInputFrameDownsyncStr(existingInputFrame)}`); //console.log(`Updated encoded input of peerJoinIndex=${peerJoinIndex} to ${peerEncodedInput} for inputFrameId=${inputFrameId}/renderedInputFrameIdUpper=${renderedInputFrameIdUpper} from ${JSON.stringify(inputFrame)}; newInputFrameDownsyncLocal=${self.gopkgsInputFrameDownsyncStr(newInputFrameDownsyncLocal)}; existingInputFrame=${self.gopkgsInputFrameDownsyncStr(existingInputFrame)}`);
self.recentInputCache.SetByFrameId(newInputFrameDownsyncLocal, inputFrameId); self.recentInputCache.SetByFrameId(newInputFrameDownsyncLocal, inputFrameId);
@ -1047,15 +1047,6 @@ fromUDP=${fromUDP}`);
self.networkDoctor.logPeerInputFrameUpsync(batch[0].inputFrameId, batch[batch.length - 1].inputFrameId); self.networkDoctor.logPeerInputFrameUpsync(batch[0].inputFrameId, batch[batch.length - 1].inputFrameId);
} }
if (true == self.allowRollbackOnPeerUpsync) { if (true == self.allowRollbackOnPeerUpsync) {
/*
[WARNING]
Deliberately NOT setting "existingInputFrame.ConfirmedList = (existingConfirmedList | peerJoinIndexMask)", thus NOT helping the move of "lastAllConfirmedInputFrameId" in "_markConfirmationIfApplicable()".
The edge case of concern here is "type#1 forceConfirmation". Assume that there is a battle among [P_u, P_v, P_x, P_y] where [P_x] is being an "ActiveSlowerTicker", then for [P_u, P_v, P_y] there might've been some "inputFrameUpsync"s received from [P_x] by UDP peer-to-peer transmission EARLIER THAN BUT CONFLICTING WITH the "accompaniedInputFrameDownsyncBatch of type#1 forceConfirmation" -- in such case the latter should be respected -- by "conflicting", the backend actually ignores those "inputFrameUpsync"s from [P_x] by "forceConfirmation".
However, we should still call "_handleIncorrectlyRenderedPrediction(...)" here to break rollbacks into smaller chunks, because even if not used for "inputFrameDownsync.ConfirmedList", a "UDP inputFrameUpsync" is still more accurate than the locally predicted inputs.
*/
self._handleIncorrectlyRenderedPrediction(firstPredictedYetIncorrectInputFrameId, batch, fromUDP); self._handleIncorrectlyRenderedPrediction(firstPredictedYetIncorrectInputFrameId, batch, fromUDP);
} }
}, },
@ -1173,25 +1164,25 @@ fromUDP=${fromUDP}`);
rollbackFrames = 0; rollbackFrames = 0;
} }
self.networkDoctor.logRollbackFrames(rollbackFrames); self.networkDoctor.logRollbackFrames(rollbackFrames);
let prevRdf = latestRdfResults[0], // Having "prevRdf.Id == self.renderFrameId" let prevRdf = latestRdfResults[0],
rdf = latestRdfResults[1]; // Having "rdf.Id == self.renderFrameId+1" rdf = latestRdfResults[1];
/* /*
const nonTrivialChaseEnded = (prevChaserRenderFrameId < nextChaserRenderFrameId && nextChaserRenderFrameId == self.renderFrameId); const nonTrivialChaseEnded = (prevChaserRenderFrameId < nextChaserRenderFrameId && nextChaserRenderFrameId == self.renderFrameId);
if (nonTrivialChaseEnded) { if (nonTrivialChaseEnded) {
console.debug("Non-trivial chase ended, prevChaserRenderFrameId=" + prevChaserRenderFrameId + ", nextChaserRenderFrameId=" + nextChaserRenderFrameId); console.debug("Non-trivial chase ended, prevChaserRenderFrameId=" + prevChaserRenderFrameId + ", nextChaserRenderFrameId=" + nextChaserRenderFrameId);
} }
*/ */
// [WARNING] Don't try to get "prevRdf(i.e. renderFrameId == latest-1)" by "self.recentRenderCache.GetByFrameId(...)" here, as the cache might have been updated by asynchronous "onRoomDownsyncFrame(...)" calls!
if (self.othersForcedDownsyncRenderFrameDict.has(rdf.GetId())) { if (self.othersForcedDownsyncRenderFrameDict.has(rdf.GetId())) {
const [pbOthersForcedDownsyncRenderFrame, othersForcedDownsyncRenderFrame] = self.othersForcedDownsyncRenderFrameDict.get(rdf.GetId()); const delayedInputFrameId = gopkgs.ConvertToDelayedInputFrameId(rdf.GetId());
const othersForcedDownsyncRenderFrame = self.othersForcedDownsyncRenderFrameDict.get(rdf.GetId());
if (self.lastAllConfirmedInputFrameId >= delayedInputFrameId && !self.equalRoomDownsyncFrames(othersForcedDownsyncRenderFrame, rdf)) { if (self.lastAllConfirmedInputFrameId >= delayedInputFrameId && !self.equalRoomDownsyncFrames(othersForcedDownsyncRenderFrame, rdf)) {
if (CC_DEBUG) { if (CC_DEBUG) {
console.warn(`Mismatched render frame@rdf.id=${rdf.GetId()} w/ inputFrameId=${delayedInputFrameId}: console.warn(`Mismatched render frame@rdf.id=${rdf.GetId()} w/ inputFrameId=${delayedInputFrameId}:
rdf=${self._stringifyGopkgRdfForFrameDataLogging(rdf)} rdf=${self._stringifyGopkgRdfForFrameDataLogging(rdf)}
othersForcedDownsyncRenderFrame=${self._stringifyGopkgRdfForFrameDataLogging(othersForcedDownsyncRenderFrame)}`); othersForcedDownsyncRenderFrame=${self._stringifyGopkgRdfForFrameDataLogging(othersForcedDownsyncRenderFrame)}`);
} }
// [WARNING] When this happens, something is intrinsically wrong -- to avoid having an inconsistent history in the "recentRenderCache", thus a wrong prediction all the way from here on, clear the history! rdf = othersForcedDownsyncRenderFrame;
pbOthersForcedDownsyncRenderFrame.backendUnconfirmedMask = ((1 << window.boundRoomCapacity) - 1);
self.onRoomDownsyncFrame(pbOthersForcedDownsyncRenderFrame, null);
self.othersForcedDownsyncRenderFrameDict.delete(rdf.GetId()); self.othersForcedDownsyncRenderFrameDict.delete(rdf.GetId());
} }
} }
@ -1447,7 +1438,7 @@ othersForcedDownsyncRenderFrame=${self._stringifyGopkgRdfForFrameDataLogging(oth
const j = gopkgs.ConvertToDelayedInputFrameId(i); const j = gopkgs.ConvertToDelayedInputFrameId(i);
const delayedInputFrame = gopkgs.GetInputFrameDownsync(self.recentInputCache, j); const delayedInputFrame = gopkgs.GetInputFrameDownsync(self.recentInputCache, j);
const allowUpdateInputFrameInPlaceUponDynamics = (!isChasing); // [WARNING] Input mutation could trigger chasing on frontend, thus don't trigger mutation when chasing to avoid confusing recursion. const allowUpdateInputFrameInPlaceUponDynamics = (!isChasing);
const hasInputFrameUpdatedOnDynamics = gopkgs.ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs(self.recentInputCache, i, collisionSys, collisionSysMap, self.spaceOffsetX, self.spaceOffsetY, self.chConfigsOrderedByJoinIndex, self.recentRenderCache, self.collisionHolder, self.effPushbacks, self.hardPushbackNormsArr, self.jumpedOrNotList, self.dynamicRectangleColliders, self.lastIndividuallyConfirmedInputFrameId, self.lastIndividuallyConfirmedInputList, allowUpdateInputFrameInPlaceUponDynamics, self.selfPlayerInfo.joinIndex); const hasInputFrameUpdatedOnDynamics = gopkgs.ApplyInputFrameDownsyncDynamicsOnSingleRenderFrameJs(self.recentInputCache, i, collisionSys, collisionSysMap, self.spaceOffsetX, self.spaceOffsetY, self.chConfigsOrderedByJoinIndex, self.recentRenderCache, self.collisionHolder, self.effPushbacks, self.hardPushbackNormsArr, self.jumpedOrNotList, self.dynamicRectangleColliders, self.lastIndividuallyConfirmedInputFrameId, self.lastIndividuallyConfirmedInputList, allowUpdateInputFrameInPlaceUponDynamics, self.selfPlayerInfo.joinIndex);
if (hasInputFrameUpdatedOnDynamics) { if (hasInputFrameUpdatedOnDynamics) {
const ii = gopkgs.ConvertToFirstUsedRenderFrameId(j); const ii = gopkgs.ConvertToFirstUsedRenderFrameId(j);
@ -1455,6 +1446,10 @@ othersForcedDownsyncRenderFrame=${self._stringifyGopkgRdfForFrameDataLogging(oth
/* /*
[WARNING] [WARNING]
If we don't rollback at this spot, when the mutated "delayedInputFrame.inputList" a.k.a. "inputFrame#j" matches the later downsynced version, rollback WOULDN'T be triggered for the incorrectly rendered "renderFrame#(ii+1)", and it would STAY IN HISTORY FOREVER -- as the history becomes incorrect, EVERY LATEST renderFrame since "inputFrame#j" was mutated would be ALWAYS incorrectly rendering too! If we don't rollback at this spot, when the mutated "delayedInputFrame.inputList" a.k.a. "inputFrame#j" matches the later downsynced version, rollback WOULDN'T be triggered for the incorrectly rendered "renderFrame#(ii+1)", and it would STAY IN HISTORY FOREVER -- as the history becomes incorrect, EVERY LATEST renderFrame since "inputFrame#j" was mutated would be ALWAYS incorrectly rendering too!
The backend counterpart doesn't need this rollback because
1. Backend only applies all-confirmed inputFrames to calc dynamics.
2. Backend applies an all-confirmed inputFrame to all applicable render frames at once.
*/ */
self._handleIncorrectlyRenderedPrediction(j, null, false); self._handleIncorrectlyRenderedPrediction(j, null, false);
} }

File diff suppressed because one or more lines are too long

View File

@ -100,7 +100,7 @@ bool RecvRingBuff::pop(RecvWork* out) {
2. If "0 >= oldCnt", we need guard against another "pop" to avoid over-popping. 2. If "0 >= oldCnt", we need guard against another "pop" to avoid over-popping.
*/ */
if (0 >= oldCnt) { if (0 >= oldCnt) {
// "pop" could be accessed by either "GameThread/pollUdpRecvRingBuff" or "UvRecvThread/put", thus we should be proactively guarding against concurrent popping while "1 == cnt" // "pop" could be accessed by either "GameThread/pollUdpRecvRingBuff" or "UvRecvThread/put", thus we should be proactively guard against concurrent popping while "1 == cnt"
++cnt; ++cnt;
return false; return false;
} }

View File

@ -372,7 +372,7 @@ bool DelayNoMore::UdpSession::pollUdpRecvRingBuff() {
// This function is called by GameThread 60 fps. // This function is called by GameThread 60 fps.
//uv_mutex_lock(&recvRingBuffLock); //uv_mutex_lock(&recvRingBuffLock);
while (true && NULL != recvLoop) { while (true) {
RecvWork f; RecvWork f;
bool res = recvRingBuff->pop(&f); bool res = recvRingBuff->pop(&f);
if (!res) { if (!res) {

View File

@ -76,7 +76,7 @@
"shelter_z_reducer", "shelter_z_reducer",
"shelter" "shelter"
], ],
"last-module-event-record-time": 1680159688511, "last-module-event-record-time": 1677337364473,
"simulator-orientation": false, "simulator-orientation": false,
"simulator-resolution": { "simulator-resolution": {
"height": 640, "height": 640,

View File

@ -7,7 +7,7 @@ import (
) )
const ( const (
MAX_FLOAT64 = resolv.MaxFloat64 MAX_FLOAT64 = 1.7e+308
MAX_INT32 = int32(999999999) MAX_INT32 = int32(999999999)
COLLISION_PLAYER_INDEX_PREFIX = (1 << 17) COLLISION_PLAYER_INDEX_PREFIX = (1 << 17)
COLLISION_BARRIER_INDEX_PREFIX = (1 << 16) COLLISION_BARRIER_INDEX_PREFIX = (1 << 16)
@ -655,7 +655,7 @@ func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(inputsBuffer *resolv.Rin
jumpedOrNotList[i] = jumpedOrNot jumpedOrNotList[i] = jumpedOrNot
joinIndex := currPlayerDownsync.JoinIndex joinIndex := currPlayerDownsync.JoinIndex
skillId := chConfig.SkillMapper(patternId, currPlayerDownsync, chConfig.SpeciesId) skillId := chConfig.SkillMapper(patternId, currPlayerDownsync)
if skillConfig, existent := skills[skillId]; existent { if skillConfig, existent := skills[skillId]; existent {
thatPlayerInNextFrame.ActiveSkillId = int32(skillId) thatPlayerInNextFrame.ActiveSkillId = int32(skillId)
thatPlayerInNextFrame.ActiveSkillHit = 0 thatPlayerInNextFrame.ActiveSkillHit = 0
@ -786,7 +786,7 @@ func ApplyInputFrameDownsyncDynamicsOnSingleRenderFrame(inputsBuffer *resolv.Rin
// Revive from Dying // Revive from Dying
newVx, newVy = currPlayerDownsync.RevivalVirtualGridX, currPlayerDownsync.RevivalVirtualGridY newVx, newVy = currPlayerDownsync.RevivalVirtualGridX, currPlayerDownsync.RevivalVirtualGridY
thatPlayerInNextFrame.CharacterState = ATK_CHARACTER_STATE_GET_UP1 thatPlayerInNextFrame.CharacterState = ATK_CHARACTER_STATE_GET_UP1
thatPlayerInNextFrame.FramesInChState = 0 thatPlayerInNextFrame.FramesInChState = ATK_CHARACTER_STATE_GET_UP1
thatPlayerInNextFrame.FramesToRecover = chConfig.GetUpFramesToRecover thatPlayerInNextFrame.FramesToRecover = chConfig.GetUpFramesToRecover
thatPlayerInNextFrame.FramesInvinsible = chConfig.GetUpInvinsibleFrames thatPlayerInNextFrame.FramesInvinsible = chConfig.GetUpInvinsibleFrames
thatPlayerInNextFrame.Hp = currPlayerDownsync.MaxHp thatPlayerInNextFrame.Hp = currPlayerDownsync.MaxHp

View File

@ -1,6 +1,6 @@
package battle package battle
type SkillMapperType = func(patternId int, currPlayerDownsync *PlayerDownsync, speciesId int) int type SkillMapperType func(patternId int, currPlayerDownsync *PlayerDownsync) int
type CharacterConfig struct { type CharacterConfig struct {
SpeciesId int SpeciesId int
@ -31,9 +31,34 @@ type CharacterConfig struct {
SkillMapper SkillMapperType SkillMapper SkillMapperType
} }
func defaultSkillMapper(patternId int, currPlayerDownsync *PlayerDownsync, speciesId int) int { var Characters = map[int]*CharacterConfig{
switch speciesId { 0: &CharacterConfig{
case 0: SpeciesId: 0,
SpeciesName: "MonkGirl",
InAirIdleFrameIdxTurningPoint: 11,
InAirIdleFrameIdxTurnedCycle: 1,
LayDownFrames: int32(16),
LayDownFramesToRecover: int32(16),
GetUpInvinsibleFrames: int32(10),
GetUpFramesToRecover: int32(27),
Speed: int32(float64(2.1) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingInitVelY: int32(float64(8) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingFramesToRecover: int32(2),
InertiaFramesToRecover: int32(9),
DashingEnabled: true,
OnWallEnabled: true,
WallJumpingFramesToRecover: int32(8), // 8 would be the minimum for an avg human
WallJumpingInitVelX: int32(float64(2.8) * WORLD_TO_VIRTUAL_GRID_RATIO), // Default is "appeared facing right", but actually holding ctrl against left
WallJumpingInitVelY: int32(float64(7) * WORLD_TO_VIRTUAL_GRID_RATIO),
WallSlidingVelY: int32(float64(-1) * WORLD_TO_VIRTUAL_GRID_RATIO),
SkillMapper: func(patternId int, currPlayerDownsync *PlayerDownsync) int {
if 1 == patternId { if 1 == patternId {
if 0 == currPlayerDownsync.FramesToRecover { if 0 == currPlayerDownsync.FramesToRecover {
if currPlayerDownsync.InAir { if currPlayerDownsync.InAir {
@ -67,7 +92,35 @@ func defaultSkillMapper(patternId int, currPlayerDownsync *PlayerDownsync, speci
// By default no skill can be fired // By default no skill can be fired
return NO_SKILL return NO_SKILL
case 1: },
},
1: &CharacterConfig{
SpeciesId: 1,
SpeciesName: "KnifeGirl",
InAirIdleFrameIdxTurningPoint: 9,
InAirIdleFrameIdxTurnedCycle: 1,
LayDownFrames: int32(16),
LayDownFramesToRecover: int32(16),
GetUpInvinsibleFrames: int32(10),
GetUpFramesToRecover: int32(27),
Speed: int32(float64(2.2) * WORLD_TO_VIRTUAL_GRID_RATIO), // I don't know why "2.2" is so special that it throws a compile error
JumpingInitVelY: int32(float64(7.5) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingFramesToRecover: int32(2),
InertiaFramesToRecover: int32(9),
DashingEnabled: true,
OnWallEnabled: true,
WallJumpingFramesToRecover: int32(8), // 8 would be the minimum for an avg human
WallJumpingInitVelX: int32(float64(2.8) * WORLD_TO_VIRTUAL_GRID_RATIO), // Default is "appeared facing right", but actually holding ctrl against left
WallJumpingInitVelY: int32(float64(7) * WORLD_TO_VIRTUAL_GRID_RATIO),
WallSlidingVelY: int32(float64(-1) * WORLD_TO_VIRTUAL_GRID_RATIO),
SkillMapper: func(patternId int, currPlayerDownsync *PlayerDownsync) int {
if 1 == patternId { if 1 == patternId {
if 0 == currPlayerDownsync.FramesToRecover { if 0 == currPlayerDownsync.FramesToRecover {
if currPlayerDownsync.InAir { if currPlayerDownsync.InAir {
@ -100,7 +153,31 @@ func defaultSkillMapper(patternId int, currPlayerDownsync *PlayerDownsync, speci
// By default no skill can be fired // By default no skill can be fired
return NO_SKILL return NO_SKILL
case 4096: },
},
4096: &CharacterConfig{
SpeciesId: 4096,
SpeciesName: "Monk",
InAirIdleFrameIdxTurningPoint: 42,
InAirIdleFrameIdxTurnedCycle: 2,
LayDownFrames: int32(14),
LayDownFramesToRecover: int32(14),
GetUpInvinsibleFrames: int32(8),
GetUpFramesToRecover: int32(30),
Speed: int32(float64(1.8) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingInitVelY: int32(float64(7.8) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingFramesToRecover: int32(2),
InertiaFramesToRecover: int32(9),
DashingEnabled: true,
OnWallEnabled: false,
SkillMapper: func(patternId int, currPlayerDownsync *PlayerDownsync) int {
if 1 == patternId { if 1 == patternId {
if 0 == currPlayerDownsync.FramesToRecover { if 0 == currPlayerDownsync.FramesToRecover {
if currPlayerDownsync.InAir { if currPlayerDownsync.InAir {
@ -138,91 +215,7 @@ func defaultSkillMapper(patternId int, currPlayerDownsync *PlayerDownsync, speci
// By default no skill can be fired // By default no skill can be fired
return NO_SKILL return NO_SKILL
}
return NO_SKILL
}
var Characters = map[int]*CharacterConfig{
0: &CharacterConfig{
SpeciesId: 0,
SpeciesName: "MonkGirl",
InAirIdleFrameIdxTurningPoint: 11,
InAirIdleFrameIdxTurnedCycle: 1,
LayDownFrames: int32(16),
LayDownFramesToRecover: int32(16),
GetUpInvinsibleFrames: int32(10),
GetUpFramesToRecover: int32(27),
Speed: int32(float64(2.1) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingInitVelY: int32(float64(8) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingFramesToRecover: int32(2),
InertiaFramesToRecover: int32(9),
DashingEnabled: true,
OnWallEnabled: true,
WallJumpingFramesToRecover: int32(8), // 8 would be the minimum for an avg human
WallJumpingInitVelX: int32(float64(2.8) * WORLD_TO_VIRTUAL_GRID_RATIO), // Default is "appeared facing right", but actually holding ctrl against left
WallJumpingInitVelY: int32(float64(7) * WORLD_TO_VIRTUAL_GRID_RATIO),
WallSlidingVelY: int32(float64(-1) * WORLD_TO_VIRTUAL_GRID_RATIO),
SkillMapper: defaultSkillMapper,
}, },
1: &CharacterConfig{
SpeciesId: 1,
SpeciesName: "KnifeGirl",
InAirIdleFrameIdxTurningPoint: 9,
InAirIdleFrameIdxTurnedCycle: 1,
LayDownFrames: int32(16),
LayDownFramesToRecover: int32(16),
GetUpInvinsibleFrames: int32(10),
GetUpFramesToRecover: int32(27),
Speed: int32(float64(2.2) * WORLD_TO_VIRTUAL_GRID_RATIO), // I don't know why "2.2" is so special that it throws a compile error
JumpingInitVelY: int32(float64(7.5) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingFramesToRecover: int32(2),
InertiaFramesToRecover: int32(9),
DashingEnabled: true,
OnWallEnabled: true,
WallJumpingFramesToRecover: int32(8), // 8 would be the minimum for an avg human
WallJumpingInitVelX: int32(float64(2.8) * WORLD_TO_VIRTUAL_GRID_RATIO), // Default is "appeared facing right", but actually holding ctrl against left
WallJumpingInitVelY: int32(float64(7) * WORLD_TO_VIRTUAL_GRID_RATIO),
WallSlidingVelY: int32(float64(-1) * WORLD_TO_VIRTUAL_GRID_RATIO),
SkillMapper: defaultSkillMapper,
},
4096: &CharacterConfig{
SpeciesId: 4096,
SpeciesName: "Monk",
InAirIdleFrameIdxTurningPoint: 42,
InAirIdleFrameIdxTurnedCycle: 2,
LayDownFrames: int32(14),
LayDownFramesToRecover: int32(14),
GetUpInvinsibleFrames: int32(8),
GetUpFramesToRecover: int32(30),
Speed: int32(float64(1.8) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingInitVelY: int32(float64(7.8) * WORLD_TO_VIRTUAL_GRID_RATIO),
JumpingFramesToRecover: int32(2),
InertiaFramesToRecover: int32(9),
DashingEnabled: true,
OnWallEnabled: false,
SkillMapper: defaultSkillMapper,
}, },
} }
@ -233,7 +226,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(30), RecoveryFramesOnHit: int32(30),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK1, BoundChState: ATK_CHARACTER_STATE_ATK1,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(7), StartupFrames: int32(7),
@ -268,7 +261,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(36), RecoveryFramesOnHit: int32(36),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK2, BoundChState: ATK_CHARACTER_STATE_ATK2,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(18), StartupFrames: int32(18),
@ -302,7 +295,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(50), RecoveryFramesOnHit: int32(50),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK3, BoundChState: ATK_CHARACTER_STATE_ATK3,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(8), StartupFrames: int32(8),
@ -331,7 +324,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(30), RecoveryFramesOnHit: int32(30),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK1, BoundChState: ATK_CHARACTER_STATE_ATK1,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(7), StartupFrames: int32(7),
@ -366,7 +359,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(36), RecoveryFramesOnHit: int32(36),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK2, BoundChState: ATK_CHARACTER_STATE_ATK2,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(18), StartupFrames: int32(18),
@ -400,7 +393,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(45), RecoveryFramesOnHit: int32(45),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK3, BoundChState: ATK_CHARACTER_STATE_ATK3,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(8), StartupFrames: int32(8),
@ -429,7 +422,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(30), RecoveryFramesOnHit: int32(30),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK1, BoundChState: ATK_CHARACTER_STATE_ATK1,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(7), StartupFrames: int32(7),
@ -464,7 +457,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(36), RecoveryFramesOnHit: int32(36),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK2, BoundChState: ATK_CHARACTER_STATE_ATK2,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(18), StartupFrames: int32(18),
@ -498,7 +491,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(40), RecoveryFramesOnHit: int32(40),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK3, BoundChState: ATK_CHARACTER_STATE_ATK3,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(7), StartupFrames: int32(7),
@ -527,7 +520,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(38), RecoveryFramesOnHit: int32(38),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK4, BoundChState: ATK_CHARACTER_STATE_ATK4,
Hits: []AnyBullet{ Hits: []interface{}{
&FireballBullet{ &FireballBullet{
Speed: int32(float64(6) * WORLD_TO_VIRTUAL_GRID_RATIO), Speed: int32(float64(6) * WORLD_TO_VIRTUAL_GRID_RATIO),
Bullet: &BulletConfig{ Bullet: &BulletConfig{
@ -557,7 +550,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(60), RecoveryFramesOnHit: int32(60),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK5, BoundChState: ATK_CHARACTER_STATE_ATK5,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(3), StartupFrames: int32(3),
@ -586,7 +579,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(10), RecoveryFramesOnHit: int32(10),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_DASHING, BoundChState: ATK_CHARACTER_STATE_DASHING,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(3), StartupFrames: int32(3),
@ -613,7 +606,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(12), RecoveryFramesOnHit: int32(12),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_DASHING, BoundChState: ATK_CHARACTER_STATE_DASHING,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(3), StartupFrames: int32(3),
@ -640,7 +633,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(8), RecoveryFramesOnHit: int32(8),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_DASHING, BoundChState: ATK_CHARACTER_STATE_DASHING,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(4), StartupFrames: int32(4),
@ -667,7 +660,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(48), RecoveryFramesOnHit: int32(48),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK4, BoundChState: ATK_CHARACTER_STATE_ATK4,
Hits: []AnyBullet{ Hits: []interface{}{
&FireballBullet{ &FireballBullet{
Speed: int32(float64(4) * WORLD_TO_VIRTUAL_GRID_RATIO), Speed: int32(float64(4) * WORLD_TO_VIRTUAL_GRID_RATIO),
Bullet: &BulletConfig{ Bullet: &BulletConfig{
@ -697,7 +690,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(60), RecoveryFramesOnHit: int32(60),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_ATK4, BoundChState: ATK_CHARACTER_STATE_ATK4,
Hits: []AnyBullet{ Hits: []interface{}{
&FireballBullet{ &FireballBullet{
Speed: int32(float64(4) * WORLD_TO_VIRTUAL_GRID_RATIO), Speed: int32(float64(4) * WORLD_TO_VIRTUAL_GRID_RATIO),
Bullet: &BulletConfig{ Bullet: &BulletConfig{
@ -727,7 +720,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(30), RecoveryFramesOnHit: int32(30),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_INAIR_ATK1, BoundChState: ATK_CHARACTER_STATE_INAIR_ATK1,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(3), StartupFrames: int32(3),
@ -756,7 +749,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(20), RecoveryFramesOnHit: int32(20),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_INAIR_ATK1, BoundChState: ATK_CHARACTER_STATE_INAIR_ATK1,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(3), StartupFrames: int32(3),
@ -785,7 +778,7 @@ var skills = map[int]*Skill{
RecoveryFramesOnHit: int32(30), RecoveryFramesOnHit: int32(30),
ReleaseTriggerType: int32(1), ReleaseTriggerType: int32(1),
BoundChState: ATK_CHARACTER_STATE_INAIR_ATK1, BoundChState: ATK_CHARACTER_STATE_INAIR_ATK1,
Hits: []AnyBullet{ Hits: []interface{}{
&MeleeBullet{ &MeleeBullet{
Bullet: &BulletConfig{ Bullet: &BulletConfig{
StartupFrames: int32(4), StartupFrames: int32(4),

View File

@ -2,8 +2,6 @@ package battle
// TODO: Replace all "int32", "int64", "uint32" and "uint64" with just "int" for better performance in JavaScript! Reference https://github.com/gopherjs/gopherjs#performance-tips // TODO: Replace all "int32", "int64", "uint32" and "uint64" with just "int" for better performance in JavaScript! Reference https://github.com/gopherjs/gopherjs#performance-tips
type AnyBullet interface{}
type Vec2D struct { type Vec2D struct {
X float64 X float64
Y float64 Y float64
@ -134,7 +132,7 @@ type Skill struct {
RecoveryFramesOnHit int32 RecoveryFramesOnHit int32
ReleaseTriggerType int32 // 1: rising-edge, 2: falling-edge ReleaseTriggerType int32 // 1: rising-edge, 2: falling-edge
BoundChState int32 BoundChState int32
Hits []AnyBullet // Hits within a "Skill" are automatically triggered Hits []interface{} // Hits within a "Skill" are automatically triggered
// [WARN] Multihit of a fireball is more difficult to handle than that of melee, because we have to count from the fireball's first hit; the situation becomes even more complicated when a multihit fireball is in a crowd -- remains to be designed // [WARN] Multihit of a fireball is more difficult to handle than that of melee, because we have to count from the fireball's first hit; the situation becomes even more complicated when a multihit fireball is in a crowd -- remains to be designed
} }

View File

@ -8,11 +8,11 @@ type Cell struct {
// newCell creates a new cell at the specified X and Y position. Should not be used directly. // newCell creates a new cell at the specified X and Y position. Should not be used directly.
func newCell(x, y int) *Cell { func newCell(x, y int) *Cell {
c := &Cell{} return &Cell{
c.X = x X: x,
c.Y = y Y: y,
c.Objects = NewRingBuffer(16) // A single cell is so small thus wouldn't have many touching objects simultaneously Objects: NewRingBuffer(16), // A single cell is so small thus wouldn't have many touching objects simultaneously
return c }
} }
// register registers an object with a Cell. Should not be used directly. // register registers an object with a Cell. Should not be used directly.

View File

@ -10,10 +10,10 @@ type Collision struct {
} }
func NewCollision() *Collision { func NewCollision() *Collision {
c := &Collision{} return &Collision{
c.Objects = NewRingBuffer(16) // I don't expect it to exceed 10 actually Objects: NewRingBuffer(16), // I don't expect it to exceed 10 actually
c.Cells = NewRingBuffer(16) Cells: NewRingBuffer(16),
return c }
} }
func (cc *Collision) Clear() { func (cc *Collision) Clear() {
@ -52,7 +52,7 @@ func (cc *Collision) HasTags(tags ...string) bool {
// This slice does not contain the Object that called Check(). // This slice does not contain the Object that called Check().
func (cc *Collision) ObjectsByTags(tags ...string) []*Object { func (cc *Collision) ObjectsByTags(tags ...string) []*Object {
objs := []*Object{} objects := []*Object{}
rb := cc.Objects rb := cc.Objects
for i := rb.StFrameId; i < rb.EdFrameId; i++ { for i := rb.StFrameId; i < rb.EdFrameId; i++ {
@ -61,30 +61,30 @@ func (cc *Collision) ObjectsByTags(tags ...string) []*Object {
continue continue
} }
if o.HasTags(tags...) { if o.HasTags(tags...) {
objs = append(objs, o) objects = append(objects, o)
} }
} }
return objs return objects
} }
// ContactWithObject returns the delta to move to come into contact with the specified Object. // ContactWithObject returns the delta to move to come into contact with the specified Object.
func (cc *Collision) ContactWithObject(obj *Object) Vector { func (cc *Collision) ContactWithObject(object *Object) Vector {
delta := Vector{0, 0} delta := Vector{0, 0}
if cc.dx < 0 { if cc.dx < 0 {
delta[0] = obj.X + obj.W - cc.checkingObject.X delta[0] = object.X + object.W - cc.checkingObject.X
} else if cc.dx > 0 { } else if cc.dx > 0 {
delta[0] = obj.X - cc.checkingObject.W - cc.checkingObject.X delta[0] = object.X - cc.checkingObject.W - cc.checkingObject.X
} }
if cc.dy < 0 { if cc.dy < 0 {
delta[1] = obj.Y + obj.H - cc.checkingObject.Y delta[1] = object.Y + object.H - cc.checkingObject.Y
} else if cc.dy > 0 { } else if cc.dy > 0 {
delta[1] = obj.Y - cc.checkingObject.H - cc.checkingObject.Y delta[1] = object.Y - cc.checkingObject.H - cc.checkingObject.Y
} }
return delta return delta
@ -135,10 +135,10 @@ func (cc *Collision) SlideAgainstCell(cell *Cell, avoidTags ...string) Vector {
diffX := oX - ccX diffX := oX - ccX
diffY := oY - ccY diffY := oY - ccY
left := sp.GetCell(collidingCell.X-1, collidingCell.Y) left := sp.Cell(collidingCell.X-1, collidingCell.Y)
right := sp.GetCell(collidingCell.X+1, collidingCell.Y) right := sp.Cell(collidingCell.X+1, collidingCell.Y)
up := sp.GetCell(collidingCell.X, collidingCell.Y-1) up := sp.Cell(collidingCell.X, collidingCell.Y-1)
down := sp.GetCell(collidingCell.X, collidingCell.Y+1) down := sp.Cell(collidingCell.X, collidingCell.Y+1)
slide := Vector{0, 0} slide := Vector{0, 0}

24
resolv_tailored/gonum.go Normal file
View File

@ -0,0 +1,24 @@
//go:build !amd64 || noasm
// +build !amd64 noasm
package resolv
// This function is from the gonum repository:
// https://github.com/gonum/gonum/blob/c3867503e73e5c3fee7ab93e3c2c562eb2be8178/internal/asm/f64/axpy.go#L23
func axpyUnitaryTo(dst []float64, alpha float64, x, y []float64) {
dim := len(y)
for i, v := range x {
if i == dim {
return
}
dst[i] = alpha*v + y[i]
}
}
// This function is from the gonum repository:
// https://github.com/gonum/gonum/blob/c3867503e73e5c3fee7ab93e3c2c562eb2be8178/internal/asm/f64/scal.go#L23
func scalUnitaryTo(dst []float64, alpha float64, x []float64) {
for i := range x {
dst[i] *= alpha
}
}

View File

@ -1,5 +1,9 @@
package resolv package resolv
import (
"math"
)
// Object represents an object that can be spread across one or more Cells in a Space. An Object is essentially an AABB (Axis-Aligned Bounding Box) Rectangle. // Object represents an object that can be spread across one or more Cells in a Space. An Object is essentially an AABB (Axis-Aligned Bounding Box) Rectangle.
type Object struct { type Object struct {
Shape Shape // A shape for more specific collision-checking. Shape Shape // A shape for more specific collision-checking.
@ -13,27 +17,29 @@ type Object struct {
// NewObject returns a new Object of the specified position and size. // NewObject returns a new Object of the specified position and size.
func NewObjectSingleTag(x, y, w, h float64, tag string) *Object { func NewObjectSingleTag(x, y, w, h float64, tag string) *Object {
o := &Object{} o := &Object{
o.X = x X: x,
o.Y = y Y: y,
o.W = w W: w,
o.H = h H: h,
o.TouchingCells = NewRingBuffer(512) // [WARNING] Should make N large enough to cover all "TouchingCells", otherwise some cells would fail to unregister an object, resulting in memory corruption and incorrect detection result! TouchingCells: NewRingBuffer(512), // [WARNING] Should make N large enough to cover all "TouchingCells", otherwise some cells would fail to unregister an object, resulting in memory corruption and incorrect detection result!
o.tags = []string{tag} tags: []string{tag},
o.ignoreList = make(map[*Object]bool) ignoreList: map[*Object]bool{},
}
return o return o
} }
func NewObject(x, y, w, h float64, tags ...string) *Object { func NewObject(x, y, w, h float64, tags ...string) *Object {
o := &Object{} o := &Object{
o.X = x X: x,
o.Y = y Y: y,
o.W = w W: w,
o.H = h H: h,
o.TouchingCells = NewRingBuffer(512) TouchingCells: NewRingBuffer(512),
o.tags = []string{} tags: []string{},
o.ignoreList = make(map[*Object]bool) ignoreList: map[*Object]bool{},
}
if len(tags) > 0 { if len(tags) > 0 {
o.AddTags(tags...) o.AddTags(tags...)
@ -61,7 +67,7 @@ func (obj *Object) Clone() *Object {
if obj.Shape != nil { if obj.Shape != nil {
newObj.SetShape(obj.Shape.Clone()) newObj.SetShape(obj.Shape.Clone())
} }
for k, _ := range obj.ignoreList { for k := range obj.ignoreList {
newObj.AddToIgnoreList(k) newObj.AddToIgnoreList(k)
} }
return newObj return newObj
@ -89,7 +95,7 @@ func (obj *Object) Update() {
for x := cx; x <= ex; x++ { for x := cx; x <= ex; x++ {
c := obj.Space.GetCell(x, y) c := obj.Space.Cell(x, y)
if c != nil { if c != nil {
c.register(obj) c.register(obj)
@ -252,15 +258,15 @@ func (obj *Object) CheckAllWithHolder(dx, dy float64, cc *Collision) bool {
cc.checkingObject = obj cc.checkingObject = obj
if dx < 0 { if dx < 0 {
dx = Min(dx, -1) dx = math.Min(dx, -1)
} else if dx > 0 { } else if dx > 0 {
dx = Max(dx, 1) dx = math.Max(dx, 1)
} }
if dy < 0 { if dy < 0 {
dy = Min(dy, -1) dy = math.Min(dy, -1)
} else if dy > 0 { } else if dy > 0 {
dy = Max(dy, 1) dy = math.Max(dy, 1)
} }
cc.dx = dx cc.dx = dx
@ -275,7 +281,7 @@ func (obj *Object) CheckAllWithHolder(dx, dy float64, cc *Collision) bool {
for x := cx; x <= ex; x++ { for x := cx; x <= ex; x++ {
if c := obj.Space.GetCell(x, y); c != nil { if c := obj.Space.Cell(x, y); c != nil {
rb := c.Objects rb := c.Objects
for i := rb.StFrameId; i < rb.EdFrameId; i++ { for i := rb.StFrameId; i < rb.EdFrameId; i++ {

View File

@ -1,14 +1,11 @@
package resolv package resolv
const ( const (
// Declare type "int32" explicitly to prevent go2cs from transpiling them to "var" RING_BUFF_CONSECUTIVE_SET = int32(0)
RING_BUFF_CONSECUTIVE_SET int32 = 0 RING_BUFF_NON_CONSECUTIVE_SET = int32(1)
RING_BUFF_NON_CONSECUTIVE_SET int32 = 1 RING_BUFF_FAILED_TO_SET = int32(2)
RING_BUFF_FAILED_TO_SET int32 = 2
) )
type AnyObj interface{}
type RingBuffer struct { type RingBuffer struct {
Ed int32 // write index, open index Ed int32 // write index, open index
St int32 // read index, closed index St int32 // read index, closed index
@ -16,19 +13,19 @@ type RingBuffer struct {
StFrameId int32 StFrameId int32
N 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 Cnt int32 // the count of valid elements in the buffer, used mainly to distinguish what "st == ed" means for "Pop" and "Get" methods
Eles []AnyObj Eles []interface{}
} }
func NewRingBuffer(n int32) *RingBuffer { func NewRingBuffer(n int32) *RingBuffer {
ret := &RingBuffer{} return &RingBuffer{
ret.Ed = 0 Ed: 0,
ret.St = 0 St: 0,
ret.EdFrameId = 0 EdFrameId: 0,
ret.StFrameId = 0 StFrameId: 0,
ret.N = n N: n,
ret.Cnt = 0 Cnt: 0,
ret.Eles = make([]AnyObj, n) Eles: make([]interface{}, n),
return ret }
} }
func (rb *RingBuffer) DryPut() { func (rb *RingBuffer) DryPut() {
@ -44,7 +41,7 @@ func (rb *RingBuffer) DryPut() {
} }
} }
func (rb *RingBuffer) Put(pItem AnyObj) { func (rb *RingBuffer) Put(pItem interface{}) {
for 0 < rb.Cnt && rb.Cnt >= rb.N { for 0 < rb.Cnt && rb.Cnt >= rb.N {
// Make room for the new element // Make room for the new element
rb.Pop() rb.Pop()
@ -58,7 +55,7 @@ func (rb *RingBuffer) Put(pItem AnyObj) {
} }
} }
func (rb *RingBuffer) Pop() AnyObj { func (rb *RingBuffer) Pop() interface{} {
if 0 == rb.Cnt { if 0 == rb.Cnt {
return nil return nil
} }
@ -96,7 +93,7 @@ func (rb *RingBuffer) GetArrIdxByOffset(offsetFromSt int32) int32 {
return -1 return -1
} }
func (rb *RingBuffer) GetByOffset(offsetFromSt int32) AnyObj { func (rb *RingBuffer) GetByOffset(offsetFromSt int32) interface{} {
arrIdx := rb.GetArrIdxByOffset(offsetFromSt) arrIdx := rb.GetArrIdxByOffset(offsetFromSt)
if -1 == arrIdx { if -1 == arrIdx {
return nil return nil
@ -104,7 +101,7 @@ func (rb *RingBuffer) GetByOffset(offsetFromSt int32) AnyObj {
return rb.Eles[arrIdx] return rb.Eles[arrIdx]
} }
func (rb *RingBuffer) GetByFrameId(frameId int32) AnyObj { func (rb *RingBuffer) GetByFrameId(frameId int32) interface{} {
if frameId >= rb.EdFrameId || frameId < rb.StFrameId { if frameId >= rb.EdFrameId || frameId < rb.StFrameId {
return nil return nil
} }
@ -112,7 +109,7 @@ func (rb *RingBuffer) GetByFrameId(frameId int32) AnyObj {
} }
// [WARNING] During a battle, frontend could receive non-consecutive frames (either renderFrame or inputFrame) due to resync, the buffer should handle these frames properly. // [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 *RingBuffer) SetByFrameId(pItem AnyObj, frameId int32) (int32, int32, int32) { func (rb *RingBuffer) SetByFrameId(pItem interface{}, frameId int32) (int32, int32, int32) {
oldStFrameId, oldEdFrameId := rb.StFrameId, rb.EdFrameId oldStFrameId, oldEdFrameId := rb.StFrameId, rb.EdFrameId
if frameId < oldStFrameId { if frameId < oldStFrameId {
return RING_BUFF_FAILED_TO_SET, oldStFrameId, oldEdFrameId return RING_BUFF_FAILED_TO_SET, oldStFrameId, oldEdFrameId

View File

@ -1,5 +1,9 @@
package resolv package resolv
import (
"math"
)
type Shape interface { type Shape interface {
// Intersection tests to see if a Shape intersects with the other given Shape. dx and dy are delta movement variables indicating // Intersection tests to see if a Shape intersects with the other given Shape. dx and dy are delta movement variables indicating
// movement to be applied before the intersection check (thereby allowing you to see if a Shape would collide with another if it // movement to be applied before the intersection check (thereby allowing you to see if a Shape would collide with another if it
@ -23,10 +27,10 @@ type Line struct {
} }
func NewLine(x, y, x2, y2 float64) *Line { func NewLine(x, y, x2, y2 float64) *Line {
l := &Line{} return &Line{
l.Start = Vector{x, y} Start: Vector{x, y},
l.End = Vector{x2, y2} End: Vector{x2, y2},
return l }
} }
func (line *Line) Normal() Vector { func (line *Line) Normal() Vector {
@ -78,9 +82,10 @@ type ConvexPolygon struct {
// polygon square, with the vertices at {0,0}, {10,0}, {10, 10}, and {0, 10}. // polygon square, with the vertices at {0,0}, {10,0}, {10, 10}, and {0, 10}.
func NewConvexPolygon(points ...float64) *ConvexPolygon { func NewConvexPolygon(points ...float64) *ConvexPolygon {
cp := &ConvexPolygon{} cp := &ConvexPolygon{
cp.Points = NewRingBuffer(6) // I don't expected more points to be coped with in this particular game Points: NewRingBuffer(6), // I don't expected more points to be coped with in this particular game
cp.Closed = true Closed: true,
}
cp.AddPoints(points...) cp.AddPoints(points...)
@ -183,7 +188,7 @@ func (cp *ConvexPolygon) Bounds() (Vector, Vector) {
transformed := cp.Transformed() transformed := cp.Transformed()
topLeft := Vector{transformed[0][0], transformed[0][1]} topLeft := Vector{transformed[0][0], transformed[0][1]}
bottomRight := Vector{transformed[0][0], transformed[0][1]} bottomRight := topLeft.Clone()
for i := 0; i < len(transformed); i++ { for i := 0; i < len(transformed); i++ {
@ -220,8 +225,8 @@ func (cp *ConvexPolygon) SetPosition(x, y float64) {
// SetPositionVec allows you to set the position of the ConvexPolygon using a Vector. The offset of the vertices compared to the X and Y // SetPositionVec allows you to set the position of the ConvexPolygon using a Vector. The offset of the vertices compared to the X and Y
// position is relative to however you initially defined the polygon and added the vertices. // position is relative to however you initially defined the polygon and added the vertices.
func (cp *ConvexPolygon) SetPositionVec(vec Vector) { func (cp *ConvexPolygon) SetPositionVec(vec Vector) {
cp.X = vec.GetX() cp.X = vec.X()
cp.Y = vec.GetY() cp.Y = vec.Y()
} }
// Move translates the ConvexPolygon by the designated X and Y values. // Move translates the ConvexPolygon by the designated X and Y values.
@ -232,8 +237,25 @@ func (cp *ConvexPolygon) Move(x, y float64) {
// MoveVec translates the ConvexPolygon by the designated Vector. // MoveVec translates the ConvexPolygon by the designated Vector.
func (cp *ConvexPolygon) MoveVec(vec Vector) { func (cp *ConvexPolygon) MoveVec(vec Vector) {
cp.X += vec.GetX() cp.X += vec.X()
cp.Y += vec.GetY() cp.Y += vec.Y()
}
// Project projects (i.e. flattens) the ConvexPolygon onto the provided axis.
func (cp *ConvexPolygon) Project(axis Vector) Projection {
axis = axis.Unit()
vertices := cp.Transformed()
min := axis.Dot(Vector{vertices[0][0], vertices[0][1]})
max := min
for i := 1; i < len(vertices); i++ {
p := axis.Dot(Vector{vertices[i][0], vertices[i][1]})
if p < min {
min = p
} else if p > max {
max = p
}
}
return Projection{min, max}
} }
// SATAxes returns the axes of the ConvexPolygon for SAT intersection testing. // SATAxes returns the axes of the ConvexPolygon for SAT intersection testing.
@ -272,11 +294,11 @@ type ContactSet struct {
} }
func NewContactSet() *ContactSet { func NewContactSet() *ContactSet {
cs := &ContactSet{} return &ContactSet{
cs.Points = []Vector{} Points: []Vector{},
cs.MTV = Vector{0, 0} MTV: Vector{0, 0},
cs.Center = Vector{} Center: Vector{0, 0},
return cs }
} }
// LeftmostPoint returns the left-most point out of the ContactSet's Points slice. If the Points slice is empty somehow, this returns nil. // LeftmostPoint returns the left-most point out of the ContactSet's Points slice. If the Points slice is empty somehow, this returns nil.
@ -399,3 +421,22 @@ func NewRectangle(x, y, w, h float64) *ConvexPolygon {
x, y+h, x, y+h,
) )
} }
type Projection struct {
Min, Max float64
}
// Overlapping returns whether a Projection is overlapping with the other, provided Projection. Credit to https://www.sevenson.com.au/programming/sat/
func (projection Projection) Overlapping(other Projection) bool {
return projection.Overlap(other) > 0
}
// Overlap returns the amount that a Projection is overlapping with the other, provided Projection. Credit to https://dyn4j.org/2010/01/sat/#sat-nointer
func (projection Projection) Overlap(other Projection) float64 {
return math.Min(projection.Max, other.Max) - math.Max(projection.Min, other.Min)
}
// IsInside returns whether the Projection is wholly inside of the other, provided Projection.
func (projection Projection) IsInside(other Projection) bool {
return projection.Min >= other.Min && projection.Max <= other.Max
}

View File

@ -1,112 +0,0 @@
package resolv
import "unsafe"
const (
uvnan = 0x7FF8000000000001
uvinf = 0x7FF0000000000000
uvneginf = 0xFFF0000000000000
uvone = 0x3FF0000000000000
mask = 0x7FF
shift = 64 - 11 - 1
bias = 1023
signMask = 1 << 63
fracMask = 1<<shift - 1
MaxFloat64 = 1.79e+308
magic32 = 0x5f3759df
magic64 = 0x5fe6eb50c7b537a9
)
func Max(a, b float64) float64 {
if a > b {
return a
} else {
return b
}
}
func Min(a, b float64) float64 {
if a < b {
return a
} else {
return b
}
}
func Floor(x float64) float64 {
if x == 0 || IsInf(x, 0) || IsNaN(x) {
return x
}
if x < 0 {
d, fract := Modf(-x)
if fract != 0.0 {
d = d + 1
}
return -d
}
d, _ := Modf(x)
return d
}
func Modf(f float64) (outval float64, frac float64) {
if f < 1 {
if f < 0 {
outval1, frac1 := Modf(-f)
return -outval1, -frac1
} else if f == 0 {
return f, f // Return -0, -0 when f == -0
}
return 0, f
}
x := Float64bits(f)
e := ((uint)(x>>shift))&mask - bias
// Keep the top 12+e bits, the integer part; clear the rest.
if e < 64-12 {
x &^= 1<<(64-12-e) - 1
}
outval = Float64frombits(x)
frac = f - outval
return
}
func Float32bits(f float32) uint32 { return *(*uint32)(unsafe.Pointer(&f)) }
func Float32frombits(b uint32) float32 { return *(*float32)(unsafe.Pointer(&b)) }
func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }
func Float64frombits(b uint64) float64 { return *(*float64)(unsafe.Pointer(&b)) }
func NaN() float64 { return Float64frombits(uvnan) }
func IsNaN(f float64) (is bool) {
return f != f
}
func IsInf(f float64, sign int) bool {
return sign >= 0 && f > MaxFloat64 || sign <= 0 && f < -MaxFloat64
}
// FastInvSqrt reference https://medium.com/@adrien.za/fast-inverse-square-root-in-go-and-javascript-for-fun-6b891e74e5a8
func FastInvSqrt32(n float32) float32 {
if n < 0 {
return float32(NaN())
}
n2, th := n*0.5, float32(1.5)
b := Float32bits(n)
b = magic32 - (b >> 1)
f := Float32frombits(b)
f *= th - (n2 * f * f)
return f
}
func FastInvSqrt64(n float64) float64 {
if n < 0 {
return NaN()
}
n2, th := n*0.5, float64(1.5)
b := Float64bits(n)
b = magic64 - (b >> 1)
f := Float64frombits(b)
f *= th - (n2 * f * f)
return f
}

View File

@ -1,5 +1,9 @@
package resolv package resolv
import (
"math"
)
// Space represents a collision space. Internally, each Space contains a 2D array of Cells, with each Cell being the same size. Cells contain information on which // Space represents a collision space. Internally, each Space contains a 2D array of Cells, with each Cell being the same size. Cells contain information on which
// Objects occupy those spaces. // Objects occupy those spaces.
type Space struct { type Space struct {
@ -12,12 +16,16 @@ type Space struct {
// speed of one cell size per collision check to avoid missing any possible collisions. // speed of one cell size per collision check to avoid missing any possible collisions.
func NewSpace(spaceWidth, spaceHeight, cellWidth, cellHeight int) *Space { func NewSpace(spaceWidth, spaceHeight, cellWidth, cellHeight int) *Space {
sp := &Space{} sp := &Space{
sp.CellWidth = cellWidth CellWidth: cellWidth,
sp.CellHeight = cellHeight CellHeight: cellHeight,
}
sp.Resize(spaceWidth/cellWidth, spaceHeight/cellHeight) sp.Resize(spaceWidth/cellWidth, spaceHeight/cellHeight)
// sp.Resize(int(math.Ceil(float64(spaceWidth)/float64(cellWidth))),
// int(math.Ceil(float64(spaceHeight)/float64(cellHeight))))
return sp return sp
} }
@ -93,10 +101,10 @@ func (sp *Space) Objects() []*Object {
objectsAdded := map[*Object]bool{} objectsAdded := map[*Object]bool{}
objects := []*Object{} objects := []*Object{}
cyUpper := len(sp.Cells)
for cy := 0; cy < cyUpper; cy++ { for cy := range sp.Cells {
cxUpper := len(sp.Cells[cy])
for cx := 0; cx < cxUpper; cx++ { for cx := range sp.Cells[cy] {
rb := sp.Cells[cy][cx].Objects rb := sp.Cells[cy][cx].Objects
for i := rb.StFrameId; i < rb.EdFrameId; i++ { for i := rb.StFrameId; i < rb.EdFrameId; i++ {
o := rb.GetByFrameId(i).(*Object) o := rb.GetByFrameId(i).(*Object)
@ -124,7 +132,9 @@ func (sp *Space) Resize(width, height int) {
} }
} }
func (sp *Space) GetCell(x, y int) *Cell { // Cell returns the Cell at the given cellular / spatial (not world) X and Y position in the Space. If the X and Y position are
// out of bounds, Cell() will return nil.
func (sp *Space) Cell(x, y int) *Cell {
if y >= 0 && y < len(sp.Cells) && x >= 0 && x < len(sp.Cells[y]) { if y >= 0 && y < len(sp.Cells) && x >= 0 && x < len(sp.Cells[y]) {
return sp.Cells[y][x] return sp.Cells[y][x]
@ -141,7 +151,7 @@ func (sp *Space) CheckCells(x, y, w, h int, tags ...string) *Object {
for iy := y; iy < y+h; iy++ { for iy := y; iy < y+h; iy++ {
cell := sp.GetCell(ix, iy) cell := sp.Cell(ix, iy)
if cell != nil { if cell != nil {
rb := cell.Objects rb := cell.Objects
@ -198,9 +208,8 @@ func (sp *Space) UnregisterAllObjects() {
// WorldToSpace converts from a world position (x, y) to a position in the Space (a grid-based position). // WorldToSpace converts from a world position (x, y) to a position in the Space (a grid-based position).
func (sp *Space) WorldToSpace(x, y float64) (int, int) { func (sp *Space) WorldToSpace(x, y float64) (int, int) {
// [WARNING] DON'T use "int(...)" syntax to convert float to int, it's not supported by go2cs! fx := int(math.Floor(x / float64(sp.CellWidth)))
var fx int = (int)(Floor(x / float64(sp.CellWidth))) fy := int(math.Floor(y / float64(sp.CellHeight)))
var fy int = (int)(Floor(y / float64(sp.CellHeight)))
return fx, fy return fx, fy
} }
@ -227,8 +236,8 @@ func (sp *Space) Width() int {
func (sp *Space) CellsInLine(startX, startY, endX, endY int) []*Cell { func (sp *Space) CellsInLine(startX, startY, endX, endY int) []*Cell {
cells := []*Cell{} cells := []*Cell{}
cell := sp.GetCell(startX, startY) cell := sp.Cell(startX, startY)
endCell := sp.GetCell(endX, endY) endCell := sp.Cell(endX, endY)
if cell != nil && endCell != nil { if cell != nil && endCell != nil {
@ -257,7 +266,7 @@ func (sp *Space) CellsInLine(startX, startY, endX, endY int) []*Cell {
} }
cx, cy := sp.WorldToSpace(p[0], p[1]) cx, cy := sp.WorldToSpace(p[0], p[1])
c := sp.GetCell(cx, cy) c := sp.Cell(cx, cy)
if c != cell { if c != cell {
cell = c cell = c
} }

View File

@ -1,6 +1,8 @@
package resolv package resolv
import "math" import (
"math"
)
// Vector is the definition of a row vector that contains scalars as // Vector is the definition of a row vector that contains scalars as
// 64 bit floats // 64 bit floats
@ -12,13 +14,108 @@ type Axis int
const ( const (
// the consts below are used to represent vector axis, they are useful // the consts below are used to represent vector axis, they are useful
// to lookup values within the vector. // to lookup values within the vector.
X Axis = 0 X Axis = iota
Y Axis = 1 Y
Z Axis = 2 Z
) )
// Clone a vector
func Clone(v Vector) Vector {
return v.Clone()
}
// Clone a vector
func (v Vector) Clone() Vector {
clone := make(Vector, len(v))
copy(clone, v)
return clone
}
/*
// Add a vector with a vector or a set of vectors
func Add(v1 Vector, vs ...Vector) Vector {
return v1.Clone().Add(vs...)
}
// Add a vector with a vector or a set of vectors
func (v Vector) Add(vs ...Vector) Vector {
dim := len(v)
for i := range vs {
if len(vs[i]) > dim {
axpyUnitaryTo(v, 1, v, vs[i][:dim])
} else {
axpyUnitaryTo(v, 1, v, vs[i])
}
}
return v
}
// Sub subtracts a vector with another vector or a set of vectors
func Sub(v1 Vector, vs ...Vector) Vector {
return v1.Clone().Sub(vs...)
}
// Sub subtracts a vector with another vector or a set of vectors
func (v Vector) Sub(vs ...Vector) Vector {
dim := len(v)
for i := range vs {
if len(vs[i]) > dim {
axpyUnitaryTo(v, -1, vs[i][:dim], v)
} else {
axpyUnitaryTo(v, -1, vs[i], v)
}
}
return v
}
// Scale vector with a given size
func Scale(v Vector, size float64) Vector {
return v.Clone().Scale(size)
}
// Scale vector with a given size
func (v Vector) Scale(size float64) Vector {
scalUnitaryTo(v, size, v)
return v
}
*/
// Equal compares that two vectors are equal to each other
func Equal(v1, v2 Vector) bool {
return v1.Equal(v2)
}
// Equal compares that two vectors are equal to each other
func (v Vector) Equal(v2 Vector) bool {
if len(v) != len(v2) {
return false
}
for i := range v {
if math.Abs(v[i]-v2[i]) > 1e-8 {
return false
}
}
return true
}
// Magnitude of a vector
func Magnitude(v Vector) float64 {
return v.Magnitude()
}
// Magnitude of a vector
func (v Vector) Magnitude() float64 {
return math.Sqrt(v.Magnitude2())
}
func (v Vector) Magnitude2() float64 { func (v Vector) Magnitude2() float64 {
var result float64 = 0. var result float64
for _, scalar := range v { for _, scalar := range v {
result += scalar * scalar result += scalar * scalar
@ -27,19 +124,129 @@ func (v Vector) Magnitude2() float64 {
return result return result
} }
// Unit returns a direction vector with the length of one.
func Unit(v Vector) Vector {
return v.Clone().Unit()
}
// Unit returns a direction vector with the length of one. // Unit returns a direction vector with the length of one.
func (v Vector) Unit() Vector { func (v Vector) Unit() Vector {
l2 := v.Magnitude2() l := v.Magnitude()
if l2 < 1e-16 {
if l < 1e-8 {
return v return v
} }
l := math.Sqrt(l2) for i := range v {
//inv := FastInvSqrt64(l2) // "Fast Inverse Square Root" is arch dependent, it's by far non-trivial to use it in Golang as well as make it feasible in the transpiled JavaScript.
for i := 0; i < len(v); i++ {
v[i] = v[i] / l v[i] = v[i] / l
//v[i] = v[i] * inv }
return v
}
// Dot product of two vectors
func Dot(v1, v2 Vector) float64 {
result, dim1, dim2 := 0., len(v1), len(v2)
if dim1 > dim2 {
v2 = append(v2, make(Vector, dim1-dim2)...)
}
if dim1 < dim2 {
v1 = append(v1, make(Vector, dim2-dim1)...)
}
for i := range v1 {
result += v1[i] * v2[i]
}
return result
}
// Dot product of two vectors
func (v Vector) Dot(v2 Vector) float64 {
return Dot(v, v2)
}
// Cross product of two vectors
func Cross(v1, v2 Vector) Vector {
return v1.Cross(v2)
}
// Cross product of two vectors
func (v Vector) Cross(v2 Vector) Vector {
if len(v) != 3 || len(v2) != 3 {
return nil
}
return Vector{
v[Y]*v2[Z] - v[Z]*v2[Y],
v[Z]*v2[X] - v[X]*v2[Z],
v[X]*v2[Z] - v[Z]*v2[X],
}
}
// Rotate is rotating a vector around a specified axis.
// If no axis are specified, it will default to the Z axis.
//
// If a vector with more than 3-dimensions is rotated, it will cut the extra
// dimensions and return a 3-dimensional vector.
//
// NOTE: the ...Axis is just syntactic sugar that allows the axis to not be
// specified and default to Z, if multiple axis is passed the first will be
// set as the rotation axis
func Rotate(v Vector, angle float64, as ...Axis) Vector {
return v.Clone().Rotate(angle, as...)
}
// Rotate is rotating a vector around a specified axis.
// If no axis are specified, it will default to the Z axis.
//
// If a vector with more than 3-dimensions is rotated, it will cut the extra
// dimensions and return a 3-dimensional vector.
//
// NOTE: the ...Axis is just syntactic sugar that allows the axis to not be
// specified and default to Z, if multiple axis is passed the first will be
// set as the rotation axis
func (v Vector) Rotate(angle float64, as ...Axis) Vector {
axis, dim := Z, len(v)
if dim == 0 {
return v
}
if len(as) > 0 {
axis = as[0]
}
if dim == 1 && axis != Z {
v = append(v, 0, 0)
}
if (dim < 2 && axis == Z) || (dim == 2 && axis != Z) {
v = append(v, 0)
}
x, y := v[X], v[Y]
cos, sin := math.Cos(angle), math.Sin(angle)
switch axis {
case X:
z := v[Z]
v[Y] = y*cos - z*sin
v[Z] = y*sin + z*cos
case Y:
z := v[Z]
v[X] = x*cos + z*sin
v[Z] = -x*sin + z*cos
case Z:
v[X] = x*cos - y*sin
v[Y] = x*sin + y*cos
}
if dim > 3 {
return v[:3]
} }
return v return v
@ -47,7 +254,7 @@ func (v Vector) Unit() Vector {
// X is corresponding to doing a v[0] lookup, if index 0 does not exist yet, a // X is corresponding to doing a v[0] lookup, if index 0 does not exist yet, a
// 0 will be returned instead // 0 will be returned instead
func (v Vector) GetX() float64 { func (v Vector) X() float64 {
if len(v) < 1 { if len(v) < 1 {
return 0. return 0.
} }
@ -57,7 +264,7 @@ func (v Vector) GetX() float64 {
// Y is corresponding to doing a v[1] lookup, if index 1 does not exist yet, a // Y is corresponding to doing a v[1] lookup, if index 1 does not exist yet, a
// 0 will be returned instead // 0 will be returned instead
func (v Vector) GetY() float64 { func (v Vector) Y() float64 {
if len(v) < 2 { if len(v) < 2 {
return 0. return 0.
} }
@ -67,7 +274,7 @@ func (v Vector) GetY() float64 {
// Z is corresponding to doing a v[2] lookup, if index 2 does not exist yet, a // Z is corresponding to doing a v[2] lookup, if index 2 does not exist yet, a
// 0 will be returned instead // 0 will be returned instead
func (v Vector) GetZ() float64 { func (v Vector) Z() float64 {
if len(v) < 3 { if len(v) < 3 {
return 0. return 0.
} }