Ejemplo n.º 1
0
        internal void Apply(FrameSyncInput input, InputFrame i1, InputFrame i2)
        {
            //copy i1 to i2
            SWBytes.CopyFull(i1.bytes, i2.bytes);

            //let input reset
            //important to reset triggers
            input.InputJustCopied(i2.bytes);

            //apply delta for each player
            byte inputSize = input.Size;

            SWConsole.Crit($"ApplyDelta delta frameNumber={frameNumber} {bytes.FullString()}");

            while (bytes.DataLength > 0)
            {
                byte            playerID = bytes.PopByte();
                FrameSyncPlayer player   = input.GetPlayer(playerID);
                if (player == null)
                {
                    SWConsole.Error($"InputFrameDelta Apply: player not found {playerID}");
                }
                byte offset = player.InputOffset;
                SWBytes.Copy(bytes, i2.bytes, bytes.ReadIndex, offset, inputSize);
                bytes.SkipRead(inputSize);
            }

            //reset read index
            bytes.SetReadIndex(0);

            //prepare bitarray
            input.InputDeltaJustApplied(i2.bytes);
        }
Ejemplo n.º 2
0
        //should include startFrame, include endframe
        public void HandleInputFramesInBackground(SWBytes initialInputFramesData, int startFrameNumber, int endFrameNumber)
        {
            lock (FRAME_SYNC_LOCK)
            {
                if (_game.gameState == FrameSyncGameState.Stopped)
                {
                    return;
                }
                SWConsole.Info($"HandleInputFramesInBackground startFrameNumber={startFrameNumber} endFrameNumber={endFrameNumber}");
                _startFrameNumber = startFrameNumber;
                _endFrameNumber   = endFrameNumber;
                _initialInputFrameDeltas.Clear();
                _initialInputFramesData = initialInputFramesData;
                for (int i = startFrameNumber; i < endFrameNumber; i++)
                {
                    InputFrameDelta delta  = new InputFrameDelta();
                    byte            length = initialInputFramesData.PopByte();
                    initialInputFramesData.PopByteBuffer(delta.bytes, 0, length);
                    _initialInputFrameDeltas.Add(delta);
                }

                int expected = endFrameNumber - startFrameNumber;
                int got      = _initialInputFrameDeltas.Count;
                //reset read index, we will save the data to disk later
                _initialInputFramesData.SetReadIndex(0);
                if (expected != got)
                {
                    SWConsole.Error($"HandleInputFramesInBackground got={got} expected={expected}");
                }
            }
        }
Ejemplo n.º 3
0
        public void SendInputFrameDeltas(SWBytes inputFrameDeltas, int count, byte inputSize)
        {
            for (int i = 0; i < count; i++)
            {
                int playerFrameNumber    = inputFrameDeltas.PopInt();
                int predictedFrameNumber = inputFrameDeltas.PopInt();

                byte length = inputFrameDeltas.PopByte();

                if (playerFrameNumber == _lastReceivedPlayerFrameNumber + 1)
                {
                    int correctPredictedFrameNumber = 0;

                    if (predictedFrameNumber != 0)
                    {
                        correctPredictedFrameNumber = _lastPredictedFrameNumber + 1;
                        _predictedFrameNumber       = predictedFrameNumber;
                        _correctFrameNumber         = correctPredictedFrameNumber;
                        SWConsole.Info($"MOCK: SendInputFrameDeltas playerFrameNumber={playerFrameNumber} correctPredictedFrameNumber={correctPredictedFrameNumber} _predictedFrameNumber={_predictedFrameNumber}");
                    }

                    InputFrameDelta delta = new InputFrameDelta();
                    delta.frameNumber = playerFrameNumber;
                    inputFrameDeltas.PopByteBuffer(delta.bytes, 0, length);
                    _receivedInputFrameDeltas.Enqueue(delta);
                    _lastReceivedPlayerFrameNumber = playerFrameNumber;
                    _lastPredictedFrameNumber      = correctPredictedFrameNumber;
                }
                else
                {
                    SWConsole.Info($"MOCK: SendInputFrameDeltas SKIP playerFrameNumber={playerFrameNumber}");
                    inputFrameDeltas.SkipRead(length);
                }
            }
        }
Ejemplo n.º 4
0
        internal void ImportData(SWBytes buffer)
        {
            _byteBuffer.Reset();

            if (largeDataContainer)
            {
                UInt16 size = _byteBuffer.PopUInt16();
                buffer.PopByteBuffer(_byteBuffer, 0, size);
            }
            else
            {
                byte size = buffer.PopByte();
                buffer.PopByteBuffer(_byteBuffer, 0, size);
            }

            foreach (IFrameSyncData frameSyncData in _frameSyncDatas)
            {
                frameSyncData.Import(_byteBuffer);
            }
        }
Ejemplo n.º 5
0
        public void HandleInputFrameInBackground(SWBytes inputFrame, int playerFrameCountOnServer, int roomStep, int playerFrameNumber)
        {
            lock (FRAME_SYNC_LOCK)
            {
                SWConsole.Crit($"<<<======Engine: HandleInputFrameInBackground roomStep={roomStep} playerFrameCountOnServer={playerFrameCountOnServer} playerFrameNumber={playerFrameNumber}");

                if (_game.gameState == FrameSyncGameState.Stopped)
                {
                    SWConsole.Crit($"Engine: HandleInputFrameInBackground game stopped");
                    return;
                }

                _playerFrameCountOnServer = playerFrameCountOnServer;

                if (_lastReceivedInputFrameDeltaNumber == 0)
                {
                    int startIndex = roomStep - 10;
                    if (startIndex < 0)
                    {
                        startIndex = 0;
                    }

                    InitializeFrames(startIndex);
                    _lastReceivedInputFrameDeltaNumber = roomStep;

                    InputFrameDelta firstDelta = new InputFrameDelta(roomStep);
                    firstDelta.playerFrameNumber = playerFrameNumber;
                    byte length = inputFrame.PopByte();
                    SWBytes.Copy(inputFrame, firstDelta.bytes, length);
                    inputFrameDeltas[roomStep]         = firstDelta;
                    _currentInputFrameNumber           = 0; //will be updated in the waiting for room frame state
                    _currentLocalInputFrameDeltaNumber = 0;
                    SWConsole.Crit($"Engine: HandleInputFrameInBackground startIndex={startIndex}");
                    return;
                }

                InputFrameDelta delta = inputFrameDeltas[roomStep];

                if (delta == null)
                {
                    delta = new InputFrameDelta();
                    inputFrameDeltas[roomStep] = delta;
                }

                if (delta.frameNumber == roomStep)
                {
                    SWConsole.Crit($"HandleInputFrameInBackground already has {roomStep}");
                }
                else
                {
                    delta.frameNumber       = roomStep;
                    delta.playerFrameNumber = playerFrameNumber;
                    SWConsole.Crit($"HandleInputFrameInBackground copy roomStep={roomStep}");// bytes={inputFrame.FullString()}");
                    byte length = inputFrame.PopByte();

                    SWBytes.Copy(inputFrame, delta.bytes, length);
                }

                //SWConsole.Crit($"Engine: HandleInputFrameInBackground roomStep={roomStep} _lastReceivedInputFrameDeltaNumber={_lastReceivedInputFrameDeltaNumber}");

                if (roomStep == _lastReceivedInputFrameDeltaNumber + 1)
                {
                    if (_firstFrameReceived == 0)
                    {   //set firstFrameReceived when we have subsequence room steps
                        _firstFrameReceived = _lastReceivedInputFrameDeltaNumber;
                    }

                    _lastReceivedInputFrameDeltaNumber = roomStep;

                    //check if there is any more received frames
                    bool shouldContinue  = true;
                    int  nextFrameNumber = roomStep + 1;
                    while (shouldContinue)
                    {
                        InputFrameDelta nextDelta = inputFrameDeltas[nextFrameNumber];

                        if (nextDelta == null)
                        {
                            break;
                        }

                        if (nextDelta.frameNumber != nextFrameNumber)
                        {
                            break;
                        }

                        _lastReceivedInputFrameDeltaNumber = nextFrameNumber;

                        nextFrameNumber++;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void Import(SWBytes buffer)
        {
            byte dataLength = buffer.PopByte();

            buffer.PopByteBuffer(bytes, 0, (int)dataLength);
        }