Esempio n. 1
0
        private void ParseGamestate(Q3HuffmanStream stream)
        {
            this.gameState.Clear();
            this.incomingCommandSequence = stream.ReadInt32();
            EntityState nullstate = new EntityState();

            ServerCommandType cmd;

            entityBaselinesEvent.WaitOne();

            while (ServerCommandType.EOF != (cmd = ( ServerCommandType )stream.ReadByte()))
            {
                switch (cmd)
                {
                case ServerCommandType.ConfigString:
                    int i = stream.ReadInt16();
                    gameState [i] = stream.ReadString();
                    break;

                case ServerCommandType.BaseLine:
                    int newnum = stream.ReadBits(10);

                    ReadDeltaEntity(stream, nullstate, ref entityBaselines [newnum], newnum);
                    break;

                default:
                    // Unknown command
                    break;
                }
            }

            entityBaselinesEvent.Set();

            this.clientNum    = stream.ReadInt32();
            this.checksumFeed = stream.ReadInt32();

            SystemInfoChanged();
            this.connState = ConnectionState.Primed;

            //SendPureChecksums ();
        }
Esempio n. 2
0
        private void ReadDeltaPlayerstate(Q3HuffmanStream stream, PlayerState from, ref PlayerState to)
        {
            int i;

            if (from == null)
            {
                from = new PlayerState();
            }

            from.CopyTo(to);

            int      lc = stream.ReadByte();
            NetField field;
            int      trunc;

            for (i = 0, field = PlayerState.fields [i]; i < lc; i++)
            {
                field = PlayerState.fields [i];

                if (stream.ReadBits(1) == 0)
                {
                    // no change
                    KeyValueCoder.TrySetFieldValue(to, field.name,
                                                   KeyValueCoder.TryGetFieldValue(from, field.name));
                }
                else
                {
                    if (field.bits == 0)
                    {
                        // float
                        if (stream.ReadBits(1) == 0)
                        {
                            // integral float
                            trunc = stream.ReadBits(NetField.FLOAT_INT_BITS);
                            // bias to allow equal parts positive and negative
                            trunc -= NetField.FLOAT_INT_BIAS;
                            KeyValueCoder.TrySetFieldValue(to, field.name, trunc);
                        }
                        else
                        {
                            // full floating point value
                            // FIXIT: wrong conversion from 32 bits to floating point value
                            KeyValueCoder.TrySetFieldValue(to, field.name, stream.ReadInt32());
                        }
                    }
                    else
                    {
                        // integer
                        KeyValueCoder.TrySetFieldValue(to, field.name, stream.ReadBits(( int )field.bits));
                    }
                }
            }

            for (i = lc, field = PlayerState.fields [lc]; i < PlayerState.fields.Length; i++)
            {
                field = PlayerState.fields [i];

                // no change
                KeyValueCoder.TrySetFieldValue(to, field.name,
                                               KeyValueCoder.TryGetFieldValue(from, field.name));
            }

            short bits;

            // read the arrays
            if (0 != stream.ReadBits(1))
            {
                // parse stats
                if (0 != stream.ReadBits(1))
                {
                    bits = stream.ReadInt16();

                    for (i = 0; i < 16; i++)
                    {
                        if (0 != (bits & (1 << i)))
                        {
                            to.stats [i] = stream.ReadInt16();
                        }
                    }
                }

                // parse persistant stats
                if (0 != stream.ReadBits(1))
                {
                    bits = stream.ReadInt16();

                    for (i = 0; i < 16; i++)
                    {
                        if (0 != (bits & (1 << i)))
                        {
                            to.persistant [i] = stream.ReadInt16();
                        }
                    }
                }

                // parse ammo
                if (0 != stream.ReadBits(1))
                {
                    bits = stream.ReadInt16();

                    for (i = 0; i < 16; i++)
                    {
                        if (0 != (bits & (1 << i)))
                        {
                            to.ammo [i] = stream.ReadInt16();
                        }
                    }
                }

                // parse powerups
                if (0 != stream.ReadBits(1))
                {
                    bits = stream.ReadInt16();

                    for (i = 0; i < 16; i++)
                    {
                        if (0 != (bits & (1 << i)))
                        {
                            to.powerups [i] = stream.ReadInt32();
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private void ReadDeltaEntity(Q3HuffmanStream stream, EntityState from, ref EntityState to, int number)
        {
            // check for a remove
            if (stream.ReadBits(1) == 1)
            {
                new EntityState().CopyTo(to);
                to.number = MAX_GENTITIES - 1;

                return;
            }

            // check for no delta
            if (stream.ReadBits(1) == 0)
            {
                from.CopyTo(to);
                to.number = number;

                return;
            }

            int lc = stream.ReadByte();

            to.number = number;
            int      i;
            NetField field;
            int      trunc;

            for (i = 0, field = EntityState.fields [i]; i < lc && i < EntityState.fields.Length; i++)
            {
                field = EntityState.fields [i];

                if (stream.ReadBits(1) == 0)
                {
                    // no change
                    KeyValueCoder.TrySetFieldValue(to, field.name,
                                                   KeyValueCoder.TryGetFieldValue(from, field.name));
                }
                else
                {
                    if (field.bits == 0)
                    {
                        // float
                        if (stream.ReadBits(1) == 0)
                        {
                            KeyValueCoder.TrySetFieldValue(to, field.name, 0);
                        }
                        else
                        {
                            if (stream.ReadBits(1) == 0)
                            {
                                // integral float
                                trunc = stream.ReadBits(NetField.FLOAT_INT_BITS);
                                // bias to allow equal parts positive and negative
                                trunc -= NetField.FLOAT_INT_BIAS;
                                KeyValueCoder.TrySetFieldValue(to, field.name, trunc);
                            }
                            else
                            {
                                // full floating point value
                                // FIXIT: wrong conversion from 32 bits to floating point value
                                KeyValueCoder.TrySetFieldValue(to, field.name, stream.ReadInt32());
                            }
                        }
                    }
                    else
                    {
                        if (stream.ReadBits(1) == 0)
                        {
                            KeyValueCoder.TrySetFieldValue(to, field.name, 0);
                        }
                        else
                        {
                            // integer
                            KeyValueCoder.TrySetFieldValue(to, field.name, stream.ReadBits(( int )field.bits));
                        }
                    }
                }
            }

            for (i = lc; i < EntityState.fields.Length; i++)
            {
                field = EntityState.fields [i];

                // no change
                KeyValueCoder.TrySetFieldValue(to, field.name,
                                               KeyValueCoder.TryGetFieldValue(from, field.name));
            }
        }
Esempio n. 4
0
        private void ParsePacketEntities(Q3HuffmanStream stream, Snapshot oldFrame, Snapshot newFrame)
        {
            EntityState oldState = null;
            int         oldIndex = 0;
            int         oldNum;

            parseEntitiesEvent.WaitOne();
            entityBaselinesEvent.WaitOne();
            newFrame.parseEntitiesNum = this.parseEntitiesNum;
            newFrame.numEntities      = 0;

            if (null == oldFrame)
            {
                oldNum = 99999;
            }
            else
            {
                if (oldIndex >= oldFrame.numEntities)
                {
                    oldNum = 99999;
                }
                else
                {
                    oldState = this.parseEntities [(oldFrame.parseEntitiesNum + oldIndex) & (MAX_PARSE_ENTITIES - 1)];
                    oldNum   = oldState.number;
                }
            }

            int newNum;

            while ((MAX_GENTITIES - 1) != (newNum = stream.ReadBits(GENTITYNUM_BITS)))
            {
                while (oldNum < newNum)
                {
                    // one or more entities from the old packet are unchanged
                    DeltaEntity(stream, newFrame, oldNum, oldState, true);

                    oldIndex++;

                    if (oldIndex >= oldFrame.numEntities)
                    {
                        oldNum = 99999;
                    }
                    else
                    {
                        oldState = this.parseEntities[(oldFrame.parseEntitiesNum + oldIndex) & (MAX_PARSE_ENTITIES - 1)];
                        oldNum   = oldState.number;
                    }
                }

                if (oldNum == newNum)
                {
                    // delta from previous
                    DeltaEntity(stream, newFrame, newNum, oldState, false);

                    oldIndex++;

                    if (oldIndex >= oldFrame.numEntities)
                    {
                        oldNum = 99999;
                    }
                    else
                    {
                        oldState = this.parseEntities[(oldFrame.parseEntitiesNum + oldIndex) & (MAX_PARSE_ENTITIES - 1)];
                        oldNum   = oldState.number;
                    }

                    continue;
                }

                if (oldNum > newNum)
                {
                    // delta from baseline
                    DeltaEntity(stream, newFrame, newNum, this.entityBaselines [newNum], false);

                    continue;
                }
            }

            // any remaining entities in the old frame are copied over
            while (oldNum != 99999)
            {
                // one or more entities from the old packet are unchanged
                DeltaEntity(stream, newFrame, oldNum, oldState, true);

                oldIndex++;

                if (oldIndex >= oldFrame.numEntities)
                {
                    oldNum = 99999;
                }
                else
                {
                    oldState = this.parseEntities[(oldFrame.parseEntitiesNum + oldIndex) & (MAX_PARSE_ENTITIES - 1)];
                    oldNum   = oldState.number;
                }
            }

            entityBaselinesEvent.Set();
            parseEntitiesEvent.Set();
        }