int Simulate(int frameNumber)
        {
            SWConsole.Crit($"Engine: Simulate frameNumber={frameNumber}");

            InputFrame lastInputFrame = inputFrames[frameNumber - 1];

            InputFrameDelta lastInputFrameDelta = inputFrameDeltas[frameNumber - 1];

            int playerFrameNumber = lastInputFrameDelta.playerFrameNumber;

            InputFrame inputFrame = inputFrames[frameNumber];

            if (inputFrame == null)
            {
                inputFrame = new InputFrame(frameNumber);
                inputFrames[frameNumber] = inputFrame;
            }

            inputFrame.FrameNumber = frameNumber;
            inputFrame.ResetBytes();

            if (lastInputFrame == null || _input == null || inputFrame == null || lastInputFrameDelta == null)
            {
                SWConsole.Error($"Engine: Simulate input data is nil {lastInputFrame} {_input} {inputFrame} {lastInputFrameDelta}");
            }

            lastInputFrameDelta.Apply(_input, lastInputFrame, inputFrame);

            FrameSyncUpdateType updateType = FrameSyncUpdateType.Normal;

            DoSimulate(updateType, inputFrame, frameNumber);

            return(playerFrameNumber);
        }
        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);
        }
        void FlushInputOnlinePrediction()
        {
            InputFrameDelta previousInputDelta = localInputFrameDeltas[_currentLocalInputFrameDeltaNumber];

            _currentLocalInputFrameDeltaNumber++;

            if (_nextPlayerFrameNumberToConfirm == 0)
            {
                _nextPlayerFrameNumberToConfirm = _currentLocalInputFrameDeltaNumber;
            }

            InputFrameDelta inputFrameDelta = localInputFrameDeltas[_currentLocalInputFrameDeltaNumber];

            if (inputFrameDelta == null)
            {
                inputFrameDelta = new InputFrameDelta(_currentLocalInputFrameDeltaNumber);
                localInputFrameDeltas[_currentLocalInputFrameDeltaNumber] = inputFrameDelta;
            }

            inputFrameDelta.frameNumber = _currentLocalInputFrameDeltaNumber;
            inputFrameDelta.resend      = FrameSyncConstant.LOCAL_INPUT_FRAME_RESEND_COUNT;
            inputFrameDelta.ResetBytes();
            _input.ExportInput(inputFrameDelta.bytes);

            bool inputChanged = false;

            if (previousInputDelta == null)
            {
                inputChanged = true;
            }
            else
            {
                bool sameInput = previousInputDelta.IsSameInput(inputFrameDelta);
                inputChanged = !sameInput;
            }

            if (!inputChanged)
            {
                SWConsole.Crit($"Engine: FlushInputOnlinePrediction Input did NOT Change: localFN={_currentLocalInputFrameDeltaNumber}");
                //_currentLocalInputFrameDeltaNumber--;
                //send an empty frame to keep the fixed delta time adjustment running
                inputFrameDelta.ResetBytes();
            }
            else
            {
                SWConsole.Crit($"Engine: FlushInputOnlinePrediction Input changed: localFN={_currentLocalInputFrameDeltaNumber}");
            }

            SendLocalInputs();
        }
        void WaitingForRoomFrame()
        {
            if (_firstFrameReceived > 0)
            {
                SWConsole.Crit($"WaitingForRoomFrame _firstFrameReceived={_firstFrameReceived}");
                InputFrameDelta delta = inputFrameDeltas[_firstFrameReceived];

                if (delta != null)
                {
                    SWConsole.Crit($"WaitingForRoomFrame delta not null Delta.frameNumber = {delta.frameNumber}");
                    if (delta.frameNumber == _firstFrameReceived)
                    {
                        if (_firstFrameReceived > 1)
                        {
                            _game.gameState = FrameSyncGameState.WaitingForInitialSystemData;
                            SWConsole.Crit($"WaitingForRoomFrame RequestInputFrames end={_firstFrameReceived}");

                            _io.RequestInputFrames(1, _firstFrameReceived);
                            SWConsole.Crit($"WaitingForRoomFrame game WaitingForInitialSystemData now");
                        }
                        else
                        {
                            //start from 1st frame
                            _currentInputFrameNumber = 1;

                            //create an empty input frame to start with
                            inputFrames[_currentInputFrameNumber] = new InputFrame(_currentInputFrameNumber);
                            _game.gameState = FrameSyncGameState.Running;
                            SetSaveHandler(0);
                            SWConsole.Crit($"WaitingForRoomFrame game running now");
                        }

                        ResetTimeStamp();
                        return;
                    }
                }
            }
            if (CheckInterval(FrameSyncConstant.SERVER_FRAME_INITIALIZATION_INTERVAL))
            {
                SWBytes buffer = new SWBytes(32);

                buffer.Push(0); //frame number
                buffer.Push(0); //predict
                byte length = 0;
                buffer.Push(length);
                _io.SendInputFrameDeltas(buffer, 1, _input.Size);
            }
        }
        void RestoreToConfirmedFrame()
        {
            //skip the first frame because there is no systemData to restore to
            if (_currentInputFrameNumber > 1)
            {
                SWConsole.Crit($"Engine: RestoreToConfirmedFrame {_currentInputFrameNumber}");
                SWSystemDataFrame systemDataFrame = systemDataFrames[_currentInputFrameNumber];

                IRestorable restorable = systemDataFrame.GetUserRestorable();
                if (restorable != null)
                {
                    restorable.Restore();
                }

                ReloadSystemDataSnapshot(systemDataFrame.bytes);
                systemDataFrame.bytes.SetReadIndex(0);
            }
        }
        void WaitingForInitialSystemData()
        {
            if (HasNewInitialInputFrameDeltas())
            {
                //play all initial input frame
                SWConsole.Crit($"WaitingForInitialSystemData has initial input deltas startFrameNumber={_startFrameNumber}");
                InputFrame inputFrame1 = new InputFrame();
                InputFrame inputFrame2 = new InputFrame();

                int frameNumber = _startFrameNumber + 1; //if start number is 1 delta, we need to simulate 2 because 2 = 1 input + 1 delta
                foreach (InputFrameDelta delta in _initialInputFrameDeltas)
                {
                    inputFrame2.ResetBytes();
                    delta.Apply(_input, inputFrame1, inputFrame2);

                    FrameSyncUpdateType updateType = FrameSyncUpdateType.Restore;
                    SWConsole.Crit($"WaitingForInitialSystemData simulate {frameNumber}");

                    DoSimulate(updateType, inputFrame2, frameNumber);

                    InputFrame temp = inputFrame1;
                    inputFrame1 = inputFrame2;
                    inputFrame2 = temp;
                    frameNumber++;
                }

                //start from the last restored frame;
                frameNumber--;
                _currentInputFrameNumber = frameNumber;
                ExportSimulationResult();
                //create an empty input frame to start with
                inputFrames[frameNumber] = inputFrame1;
                //export system data
                ExportSimulationResult();
                SWConsole.Warn($"WaitingForInitialSystemData _initialInputFramesData={_initialInputFramesData.DataLength}");
                _saveHandler(_initialInputFramesData, _startFrameNumber, _endFrameNumber);
                _game.gameState = FrameSyncGameState.Running;

                SetSaveHandler(_endFrameNumber - 1); //end frame was excluded from initial frames, so we want to save it
                SWConsole.Crit($"WaitingForInitialSystemData game is running now _currentInputFrameNumber={_currentInputFrameNumber}");
                ResetTimeStamp();
                return;
            }
        }
        bool Predict(int localFrameDeltaNumber, int frameNumber)
        {
            SWConsole.Crit($"Engine: Predict localFrameDeltaNumber={localFrameDeltaNumber} frameNumber={frameNumber}");

            InputFrameDelta inputFrameDelta = localInputFrameDeltas[localFrameDeltaNumber];

            _inputFrameForPrediction.FrameNumber = frameNumber;
            _inputFrameForPrediction.ResetBytes();

            inputFrameDelta.Apply(_input, _lastInputFrameForPrediction, _inputFrameForPrediction);

            _input.ApplyPredictionModifier(_inputFrameForPrediction.bytes);

            FrameSyncUpdateType updateType = FrameSyncUpdateType.Prediction;

            DoSimulate(updateType, _inputFrameForPrediction, frameNumber);

            return(true);
        }
        public void FlushInputOnline()
        {
            InputFrameDelta previousInputDelta = localInputFrameDeltas[_currentLocalInputFrameDeltaNumber];

            _currentLocalInputFrameDeltaNumber++;

            InputFrameDelta inputFrameDelta = localInputFrameDeltas[_currentLocalInputFrameDeltaNumber];

            if (inputFrameDelta == null)
            {
                inputFrameDelta = new InputFrameDelta(_currentLocalInputFrameDeltaNumber);
                localInputFrameDeltas[_currentLocalInputFrameDeltaNumber] = inputFrameDelta;
            }

            inputFrameDelta.frameNumber = _currentLocalInputFrameDeltaNumber;
            inputFrameDelta.resend      = FrameSyncConstant.LOCAL_INPUT_FRAME_RESEND_COUNT;
            inputFrameDelta.ResetBytes();
            _input.ExportInput(inputFrameDelta.bytes);

            bool inputChanged = false;

            if (previousInputDelta == null)
            {
                inputChanged = true;
            }
            else
            {
                bool sameInput = previousInputDelta.IsSameInput(inputFrameDelta);
                inputChanged = !sameInput;
            }

            if (!inputChanged)
            {
                SWConsole.Crit("Engine: Input did NOT Change");
                _currentLocalInputFrameDeltaNumber--;
            }
            else
            {
                SWConsole.Crit("Engine: Input Changed");
            }

            SendLocalInputs();
        }
        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++;
                    }
                }
            }
        }
        void RunningOnlineWithPrediction()
        {
            SWConsole.Crit("Engine: ================RunningOnlineWithPrediction=================");
            //FlushInputOnlinePrediction();

            // check if we got new server frame to simulate
            int nextServerFrame = _currentInputFrameNumber + 1;

            if (true)  //CanSimulateInputFrame(nextServerFrame))
            {
                // restore the last simulated server frame before simulate any new server frame
                RestoreToConfirmedFrame();
            }

            int lastSimulatedPlayerFrameNumber = 0;

            // simulate all server frames first
            for (; nextServerFrame <= _lastReceivedInputFrameDeltaNumber + 1; nextServerFrame++)
            {
                if (CanSimulateInputFrame(nextServerFrame))
                {
                    lastSimulatedPlayerFrameNumber = SimulateInputFrame(nextServerFrame);
                    SWConsole.Crit($"lastSimulatedPlayerFrameNumber={lastSimulatedPlayerFrameNumber}");
                    if (lastSimulatedPlayerFrameNumber == 0)
                    {
                        // local player's input frame missing for this frame
                        if (_nextPlayerFrameNumberToConfirm > 1)
                        {
                            SWConsole.Warn("wtf");
                        }
                    }
                    else
                    {
                        _nextPlayerFrameNumberToConfirm = lastSimulatedPlayerFrameNumber + 1;
                        SWConsole.Crit($"nextPlayerFrameNumberToConfirm={_nextPlayerFrameNumberToConfirm}");
                    }

                    ExportSimulationResult();
                }
                else
                {
                    break;
                }
            }

            // if last simulated server frame has local player's input
            // we should simulate all player's input frames after it
            if (true)  // lastSimulatedPlayerFrameNumber > 0)
            {
                InputFrame lastInputFrame = inputFrames[nextServerFrame - 1];
                lastInputFrame.Copy(_lastInputFrameForPrediction);
                int startPlayerFrameNumber = _nextPlayerFrameNumberToConfirm;

                int endPlayerFrameNumber = _currentLocalInputFrameDeltaNumber - FrameSyncConstant.PREDICTION_GLOBAL_DEBAY_FRAMES;

                int predictFrameNumber = nextServerFrame;

                SWConsole.Crit($"startPlayerFrameNumber={startPlayerFrameNumber}");
                SWConsole.Crit($"endPlayerFrameNumber={endPlayerFrameNumber}");
                // endPlayerFrameNumber + 1 to include the endPlayerFrameNumber
                for (int i = startPlayerFrameNumber; i < endPlayerFrameNumber + 1; i++)
                {
                    Predict(i, predictFrameNumber);
                    predictFrameNumber++;

                    //swap prediction InputFrames for the next prediction
                    InputFrame temp = _lastInputFrameForPrediction;
                    _lastInputFrameForPrediction = _inputFrameForPrediction;
                    _inputFrameForPrediction     = temp;
                }
            }

            //reset game.frameNumber to the last confirmed frame
            //this is for debug server
            _game.frameNumber = _currentInputFrameNumber;

            SWConsole.Crit("Engine: ================end=================");
        }
        void DoTick()
        {
            _frameNumber++;
            _sealedFrameNumber = _frameNumber - 10;
            if (_sealedFrameNumber < 1)
            {
                _sealedFrameNumber = 1;
            }

            if (_receivedInputFrameDeltas.Count > 0)
            {
                SWConsole.Crit($"MockIO: DoTick playerFrameCount={_receivedInputFrameDeltas.Count}");
                InputFrameDelta delta = _receivedInputFrameDeltas.Peek();

                if (true)
                {
                    delta = _receivedInputFrameDeltas.Dequeue();
                    //SWConsole.Crit($"MockIO: DoTick playerFrameCount 1 ={_receivedInputFrameDeltas.Count}");
                    _inputFrameDeltas[_frameNumber] = delta;

                    _data.Reset();

                    byte length = (byte)delta.bytes.DataLength;
                    _data.Push(length);
                    _data.PushAll(delta.bytes);

                    SWConsole.Crit($"MockIO: DoTick send PLAYER={delta.frameNumber} roomStep={_frameNumber}");

                    MockHandleInputFrameOperaion operation = new MockHandleInputFrameOperaion(_handler, _pingMilliseconds);
                    operation.inputFrameData = SWBytes.Clone(_data);
                    operation.playerLastInputFrameOnServer = _receivedInputFrameDeltas.Count;
                    operation.predictionFrameNumber        = _predictedFrameNumber;
                    operation.correctFrameNumber           = _correctFrameNumber;
                    operation.roomStep          = _frameNumber;
                    operation.sealedFrameNumber = _sealedFrameNumber;
                    _operationQueue.AddOperation(operation);
                    return;
                }
                //else if(delta.predictedServerFrameNumber < _frameNumber)
                //{
                //    delta = _receivedInputFrameDeltas.Dequeue();
                //    delta.version = 1;
                //    _inputFrameDeltas[delta.predictedServerFrameNumber] = delta;

                //    _data.Reset();

                //    byte length = (byte)delta.bytes.DataLength;
                //    _data.Push(length);
                //    _data.Push(delta.bytes, 0);

                //    SWConsole.Crit($"MockIO: DoTick send PLAYER={delta.frameNumber} roomStep={_frameNumber} prediction={delta.predictedServerFrameNumber}");
                //    _frameNumber--;
                //    _sealedFrameNumber = _frameNumber - 10;
                //    if (_sealedFrameNumber < 1)
                //    {
                //        _sealedFrameNumber = 1;
                //    }
                //    MockHandleInputFrameOperaion operation = new MockHandleInputFrameOperaion(_handler, _pingMilliseconds);
                //    operation.inputFrameData = SWBytes.Clone(_data);
                //    operation.playerLastInputFrameOnServer = _receivedInputFrameDeltas.Count;
                //    operation.predictionError = _predictionError;
                //    operation.roomStep = delta.predictedServerFrameNumber;
                //    operation.version = delta.version;
                //    operation.sealedFrameNumber = _sealedFrameNumber;
                //    _operationQueue.AddOperation(operation);
                //    return;
                //}
            }

            {
                _lastPredictedFrameNumber = _frameNumber;
                InputFrameDelta delta = new InputFrameDelta();
                delta.frameNumber = 0; //playerFrameNumber
                _inputFrameDeltas[_frameNumber] = delta;

                _data.Reset();
                _data.Push((byte)0); //length
                SWConsole.Crit($"MockIO: DoTick send EMPTY={delta.frameNumber} roomStep={_frameNumber}");

                MockHandleInputFrameOperaion operation = new MockHandleInputFrameOperaion(_handler, _pingMilliseconds);
                operation.inputFrameData = SWBytes.Clone(_data);
                operation.playerLastInputFrameOnServer = _receivedInputFrameDeltas.Count;
                operation.predictionFrameNumber        = _predictedFrameNumber;
                operation.correctFrameNumber           = _correctFrameNumber;
                operation.roomStep          = _frameNumber;
                operation.sealedFrameNumber = _sealedFrameNumber;
                _operationQueue.AddOperation(operation);
            }
        }