Exemple #1
0
        private void WriteDeltaUsercmdKey(int key, UserCommand from, ref UserCommand to)
        {
            if (to.serverTime - from.serverTime < 256)
            {
                q3HuffCStream.WriteBits(1, 1);
                q3HuffCStream.WriteByte(( byte )(to.serverTime - from.serverTime));
            }
            else
            {
                q3HuffCStream.WriteBits(0, 1);
                q3HuffCStream.WriteInt32(to.serverTime);
            }
            if (from.angles [0] == to.angles [0] &&
                from.angles [1] == to.angles [1] &&
                from.angles [2] == to.angles [2] &&
                from.forwardmove == to.forwardmove &&
                from.rightmove == to.rightmove &&
                from.upmove == to.upmove &&
                from.buttons == to.buttons &&
                from.weapon == to.weapon)
            {
                q3HuffCStream.WriteBits(0, 1);                                                          // no change

                return;
            }

            key ^= to.serverTime;
            q3HuffCStream.WriteBits(1, 1);
            WriteDeltaKey(key, from.angles [0], to.angles [0], 16);
            WriteDeltaKey(key, from.angles [1], to.angles [1], 16);
            WriteDeltaKey(key, from.angles [2], to.angles [2], 16);
            WriteDeltaKey(key, from.forwardmove, to.forwardmove, 8);
            WriteDeltaKey(key, from.rightmove, to.rightmove, 8);
            WriteDeltaKey(key, from.upmove, to.upmove, 8);
            WriteDeltaKey(key, from.buttons, to.buttons, 16);
            WriteDeltaKey(key, ( byte )from.weapon, ( byte )to.weapon, 8);
        }
Exemple #2
0
        private void CreateNewCommands()
        {
            if (this.connState < ConnectionState.Primed)
            {
                return;
            }

            this.cmdNumber++;
            int         cmdNum = this.cmdNumber & CMD_MASK;
            UserCommand cmd    = new UserCommand();

            cmd.weapon      = this.SelectedWeapon;
            cmd.forwardmove = this.MovementForward;
            cmd.rightmove   = this.MovementRight;
            cmd.upmove      = this.MovementUp;
            cmd.serverTime  = this.snap.serverTime;
            cmd.buttons     = this.buttons;
            cmd.angles [0]  = ViewAngleY;
            cmd.angles [1]  = ViewAngleX;

            //ViewAngleX = ( ViewAngleX - 200 ) & 0xffff;
            //ViewAngleY = ( ( int ) ( Math.Sin ( a ) * 0x7fff ) / 2 ) & 0xffff;
            //a += 0.01;

            /*if ( Math.Abs ( ViewAngleX ) >= 0x7fff )
             *      sgnX = -sgnX;
             *
             * ViewAngleX += 200 * sgnX;
             *
             * if ( Math.Abs ( ViewAngleY ) >= 0x3fff )
             *      sgnY = -sgnY;
             *
             * ViewAngleY += 200 * sgnY;*/


            this.cmds [cmdNum] = cmd;
        }
Exemple #3
0
        /*
         *      During normal gameplay, a client packet will contain something like:
         *
         *      4	sequence number
         *      2	qport
         *      4	serverid
         *      4	acknowledged sequence number
         *      4	serverCommandSequence
         *      <optional reliable commands>
         *      1	ClientCommands.Move or ClientCommands.MoveNoDelta
         *      1	command count
         *      <count * usercmds>
         */
        private void WritePacket()
        {
            // 'sequence number' and 'qport' fields will be written by underlying
            // Q3NetworkStream to provide these values without compressing and cyphering
            q3HuffCStream.WriteInt32(this.serverId);                            // serverid
            q3HuffCStream.WriteInt32(this.incomingSequence);                    // acknowledged sequence number
            q3HuffCStream.WriteInt32(this.incomingCommandSequence);             // serverCommandSequence

            reliableCommandsEvent.WaitOne();
            //<optional reliable commands>
            for (int i = reliableAcknowledge + 1; i <= reliableSequence; i++)
            {
                q3HuffCStream.WriteByte(( byte )ClientCommandType.ClientCommand);
                q3HuffCStream.WriteInt32(i);
                q3HuffCStream.WriteString(outgoingReliableCommands [i & (MAX_RELIABLE_COMMANDS - 1)]);
            }
            reliableCommandsEvent.Set();

            // we want to send all the usercmds that were generated in the last
            // few packet, so even if a couple packets are dropped in a row,
            // all the cmds will make it to the server
            if (this.packetDup < 0)
            {
                this.packetDup = 0;
            }
            else if (this.packetDup > 5)
            {
                this.packetDup = 5;
            }

            int oldPacketNum = (this.outgoingSequence - 1 - packetDup) & PACKET_MASK;
            int count        = this.cmdNumber - this.outPackets [oldPacketNum].cmdNumber;

            if (count > MAX_PACKET_USERCMDS)
            {
                count = MAX_PACKET_USERCMDS;
            }

            UserCommand oldcmd = new UserCommand();

            if (count >= 1 && this.newSnapshots)
            {
                // begin a client move command
                if (/*cl_nodelta->integer ||*/ !snap.valid ||               /*|| clc.demowaiting*/
                    this.incomingSequence != this.snap.messageNum)
                {
                    q3HuffCStream.WriteByte(( byte )ClientCommandType.MoveNoDelta);
                }
                else
                {
                    q3HuffCStream.WriteByte(( byte )ClientCommandType.Move);
                }

                // write the command count
                q3HuffCStream.WriteByte(( byte )count);

                // use the checksum feed in the key
                int key = this.checksumFeed;
                // also use the message acknowledge
                key ^= this.incomingSequence;
                // also use the last acknowledged server command in the key
                key ^= this.HashKey(this.incomingReliableCommands [this.incomingCommandSequence & (MAX_RELIABLE_COMMANDS - 1)], 32);

                // write all the commands, including the predicted command
                for (int i = 0; i < count; i++)
                {
                    int j = (this.cmdNumber - count + i + 1) & CMD_MASK;
                    WriteDeltaUsercmdKey(key, oldcmd, ref this.cmds [j]);
                    oldcmd = this.cmds [j];
                }

                this.newSnapshots = false;
            }

            int packetNum = this.outgoingSequence & PACKET_MASK;

            this.outPackets[packetNum].realtime   = DateTime.Now.Millisecond;
            this.outPackets[packetNum].serverTime = oldcmd.serverTime;
            this.outPackets[packetNum].cmdNumber  = this.cmdNumber;
            //clc.lastPacketSentTime = cls.realtime;

            q3HuffCStream.WriteByte(( byte )ClientCommandType.EOF);
            q3HuffCStream.Flush();
        }