示例#1
0
        public void WriteByte(byte value)
        {
            _newBaseStream.WriteByte(value);

            if (_baseStream != null && value == _baseStream.ReadByte())
            {
                _bitStream.WriteBool(false);
            }
            else
            {
                _bitStream.WriteBool(true);
                _bitStream.WriteByte(value);
            }
        }
示例#2
0
        private static void SendCommandPacket()
        {
            var         message = new BitStream();
            UserCommand?command, lastCommand = null;

            // last received server message
            message.WriteUInt32(_lastServerMessage);

            // last received reliable message
            message.WriteUInt32(_lastReliableMessage);

            // current server state ID
            message.WriteInt32(_svStateID);

            // reliable messages
            for (int i = _reliableAcknowledged + 1; i <= _reliableSequence; i++)
            {
                // header byte
                message.WriteByte(2);

                // command number
                message.WriteInt32(i);

                // command
                message.WriteString(_reliableCommands[i & (_reliableCommands.Length - 1)]);
            }

            // header byte
            message.WriteByte(UserCommand.CommandType);

            // command count
            message.WriteByte((byte)ClientInput.NumCommands);

            while ((command = ClientInput.GetCommand()) != null)
            {
                // write a command
                command.Value.Serialize(message, lastCommand);

                // set the new command as base
                lastCommand = command;
            }

            // end of command
            message.WriteByte(0xFF);

            // now send the command to the server
            _serverChannel.SendMessage(message);

            _commandSequence++;
        }
示例#3
0
        // serialization functions
        public void Serialize(BitStream message, UserCommand?old)
        {
            if (old == null)
            {
                message.WriteUInt32(ServerTime);
                message.WriteInt16((short)Buttons);
            }
            else
            {
                var oldCommand = old.Value;

                if ((ServerTime - oldCommand.ServerTime) < 255)
                {
                    message.WriteBool(true);
                    message.WriteByte((byte)(ServerTime - oldCommand.ServerTime));
                }
                else
                {
                    message.WriteBool(false);
                    message.WriteUInt32(ServerTime);
                }

                message.WriteDeltaInt16((short)Buttons, (short)oldCommand.Buttons);
            }
        }
示例#4
0
        private static void SendInitializationState(ServerClient client)
        {
            // send enough state for the client to initialize the game
            var message = new BitStream();

            message.WriteByte(Server.InitStateNumber);

            // current state id
            message.WriteInt32(_stateID);

            // map name
            message.WriteString(_mapName);

            // and send the message
            message.WriteByte(0xFF);
            client.Channel.SendMessage(message);

            client.LastInitStateMessage = client.Channel.SequenceOut;
        }
示例#5
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            if (args.Length >= 1)
            {
                if (args[0] == "exportMap")
                {
                    ExportMap.ExportMain(args);
                    return;
                }

                if (args[0] == "exportStyle")
                {
                    ExportStyle.ExportMain(args);
                    return;
                }

                if (args[0] == "sty2mat")
                {
                    Sty2Mat.ToolMain(args);
                    return;
                }
            }

            var outStream = new BitStream();

            outStream.WriteBool(true);
            outStream.WriteByte(125);
            outStream.WriteInt16(32766);
            outStream.WriteInt32(300000);
            outStream.WriteInt32(300000, 20);
            outStream.WriteSingle(0.1f);

            var inStream = new BitStream(outStream.Bytes);

            Console.WriteLine(inStream.ReadBool());
            Console.WriteLine(inStream.ReadByte());
            Console.WriteLine(inStream.ReadInt16());
            Console.WriteLine(inStream.ReadInt32());
            Console.WriteLine(inStream.ReadInt32(20));
            Console.WriteLine(inStream.ReadSingle());

            Game.Initialize();

            while (true)
            {
                Game.Process();
            }
        }
示例#6
0
        private static void SendSnapshot(ServerClient client)
        {
            // send entity states
            var message = new BitStream();

            // reliable messages
            for (uint i = client.ReliableAcknowledged + 1; i <= client.ReliableSequence; i++)
            {
                // header byte
                message.WriteByte(Server.ReliableCommandNumber);

                // command number
                message.WriteUInt32(i);

                // command
                message.WriteString(client.ReliableCommands[i & (client.ReliableCommands.Length - 1)]);
            }

            message.WriteByte(Server.SnapshotNumber);
            message.WriteUInt32(_serverTime);
            message.WriteUInt32(client.LastReceivedReliableCommand);

            var useDelta = true;

            if ((client.Channel.SequenceOut - client.LastAcknowledgedMessage) >= 28)
            {
                useDelta = false;
                Log.Write(LogLevel.Warning, "client {0} lagged behind more than [max stored old packets]", client.Name);
            }

            for (int i = 0; i < Entities.Length; i++)
            {
                var entity = Entities[i];

                if (entity == null)
                {
                    continue;
                }

                // write the entity number
                message.WriteInt32(i, 12);

                var entityBase = (useDelta) ? ((client.EntityBases[i] != null) ? client.EntityBases[i].Get() : null) : null;

                var deltaMessage = new DeltaBitStream(entityBase, message);

                // write the spawn key
                deltaMessage.WriteInt32(entity.SpawnKey, 20);

                // write the type code
                deltaMessage.WriteInt32(entity.TypeCode, 4);

                // write the actual data
                entity.Serialize(deltaMessage);

                // set the new base
                if (client.EntityBases[i] == null)
                {
                    client.EntityBases[i] = new ServerClientEntityBase(client);
                }

                client.EntityBases[i].Set(deltaMessage.NewBase);
            }

            message.WriteInt32(4095, 12);
            message.WriteByte(0xFF);

            // send the message
            client.Channel.SendMessage(message);
        }