예제 #1
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}");
                }
            }
        }
예제 #2
0
        public override int GetIntValue(BitArray bitArray)
        {
            int result = 0;

            for (int i = 0; i < _size; i++)
            {
                int  index = _bitOffset + i;
                bool bit   = bitArray[index];
                if (bit)
                {
                    result |= 1 << i;
                }
            }

            //-2, -1, 0, 1, 2
            // 0,  1, 2, 3, 4
            //if result is greater than the max value
            //the actual value = result - number of values
            //for example, for result 3, actual value = 3 - 5 = -2

            //is result valid?
            if (result > _nvalues)
            {
                SWConsole.Error($"invalid result({result}): result={result} min={_min} max={_max} default={_defaultValue}");
                result = _defaultValue;
            }
            else if (result > _max)
            {
                result = result - _nvalues;
            }

            return(result);
        }
예제 #3
0
        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);
        }
예제 #4
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);
        }
예제 #5
0
 internal CompressedIntInputDataController(byte bitOffset, int min, int max, byte size, int defaultValue = 0) : base(bitOffset)
 {
     _min          = min;
     _max          = max;
     _nvalues      = _max - _min + 1;
     _size         = size;
     _defaultValue = defaultValue;
     if (_defaultValue < _min || _defaultValue > _max)
     {
         SWConsole.Error($"Default value({_defaultValue}) should be between {_min} and {_max}. Using {_min} as the default value.");
         _defaultValue = _min;
     }
 }
예제 #6
0
 internal CompressedFloatInputDataController(byte bitOffset, Fix64 min, Fix64 max, Fix64 precision, byte size, Fix64 defaultValue, Func <Fix64, Fix64> predictionModifer) : base(bitOffset)
 {
     _min                = min;
     _max                = max;
     _precision          = precision;
     _nvalues            = (int)((_max - _min) / precision) + 1;
     _size               = size;
     _defaultValue       = defaultValue;
     _predictionModifier = predictionModifer;
     if (_defaultValue < _min || _defaultValue > _max)
     {
         SWConsole.Error($"Default value({_defaultValue}) should be between {_min} and {_max}. Using {_min} as the default value.");
         _defaultValue = _min;
     }
 }
예제 #7
0
        public FrameSyncPlayer CreateOfflineGamePlayer()
        {
            if (_gameState != FrameSyncGameState.Default)
            {
                SWConsole.Error("SWFrameSyncGame: not allow to add players after starting the game");
                return(null);
            }

            FrameSyncPlayer player = _input.CreatePlayer(_offlineGamePlayerID);

            _offlineGamePlayerID++;

            OnNewPlayerCreated(player);

            return(player);
        }
        public static FrameSyncInputSetting CompressedIntInput(string name, int min, int max, int defaultValue)
        {
            if (min >= max)
            {
                SWConsole.Error("");
                return(NullSetting);
            }

            FrameSyncInputSetting setting = new FrameSyncInputSetting();

            setting._inputType       = FrameSyncInputType.CompressedInt;
            setting._name            = name;
            setting._bitSize         = CompressedIntSize(min, max);
            setting._minInt          = min;
            setting._maxInt          = max;
            setting._defaultValueInt = defaultValue;

            return(setting);
        }
        public static FrameSyncInputSetting CompressedFloatInput(string name, Fix64 min, Fix64 max, Fix64 precision, Fix64 defaultValue, Func <Fix64, Fix64> predictionModifier)
        {
            if (min >= max)
            {
                SWConsole.Error("");
                return(NullSetting);
            }

            FrameSyncInputSetting setting = new FrameSyncInputSetting();

            setting._inputType          = FrameSyncInputType.CompressedFloat;
            setting._name               = name;
            setting._bitSize            = CompressedFloatSize(min, max, precision);
            setting._minFloat           = min;
            setting._maxFloat           = max;
            setting._precisionFloat     = precision;
            setting._defaultValueFloat  = defaultValue;
            setting._predictionModifier = predictionModifier;

            return(setting);
        }
예제 #10
0
        public override Fix64 GetFloatValue(BitArray bitArray)
        {
            int result = 0;

            for (int i = 0; i < _size; i++)
            {
                int  index = _bitOffset + i;
                bool bit   = bitArray[index];
                if (bit)
                {
                    result |= 1 << i;
                }
            }

            //min = -1.0
            //max = 1.0
            //precision = 0.5
            //default = 0.0

            //-1.0, -0.5, 0, 0.5, 1.0
            // 0,  1, 2, 3, 4
            //if result is greater than the max value
            //the actual value = (result - number of values) * precision
            //for example, for result 3, actual value = (3 - 5) * 0.5 = -2 * 0.5 = -1.0
            Fix64 floatResult = (Fix64)result * _precision;

            //is result valid?
            if (result > _nvalues)
            {
                SWConsole.Error($"invalid result({result}): result={result} min={_min} max={_max} default={_defaultValue}");
                floatResult = _defaultValue;
            }
            else if (floatResult > _max)
            {
                floatResult = (Fix64)(result - _nvalues) * _precision;
            }

            return(floatResult);
        }