public static VehicleData ReadUnoccupiedVehicleSync(byte[] array)
        {
            var r    = new BitReader(array);
            var data = new VehicleData();

            // Read vehicle's nethandle.
            data.VehicleHandle = r.ReadInt32();

            // Read position, rotation and velocity.
            Vector3 position = new Vector3();
            Vector3 rotation = new Vector3();
            Vector3 velocity = new Vector3();

            position.X = r.ReadSingle();
            position.Y = r.ReadSingle();
            position.Z = r.ReadSingle();

            rotation.X = r.ReadSingle();
            rotation.Y = r.ReadSingle();
            rotation.Z = r.ReadSingle();

            velocity.X = r.ReadSingle();
            velocity.Y = r.ReadSingle();
            velocity.Z = r.ReadSingle();

            data.Position   = position;
            data.Quaternion = rotation;
            data.Velocity   = velocity;

            // Read vehicle health
            data.VehicleHealth = r.ReadInt16();

            // Read is dead
            if (r.ReadBoolean())
            {
                data.Flag = (short)VehicleDataFlags.VehicleDead;
            }
            else
            {
                data.Flag = 0;
            }

            // Read tyre states
            data.PlayerHealth = r.ReadByte();

            // Read vehicle damage model
            data.DamageModel               = new VehicleDamageModel();
            data.DamageModel.BrokenDoors   = r.ReadByte();
            data.DamageModel.BrokenWindows = r.ReadByte();

            return(data);
        }
        public static VehicleData ReadLightVehicleSync(byte[] array)
        {
            var data = new VehicleData();
            var r    = new BitReader(array);

            // Read player nethandle
            data.NetHandle = r.ReadInt32();

            // read model
            data.PedModelHash = r.ReadInt32();

            // read vehicle handle
            data.VehicleHandle = r.ReadInt32();

            // read vehicle seat
            data.VehicleSeat = (sbyte)r.ReadByte();

            // read gun model.
            data.WeaponHash = r.ReadInt32();

            // Read vehicle damage model
            if (r.ReadBoolean())
            {
                data.DamageModel               = new VehicleDamageModel();
                data.DamageModel.BrokenDoors   = r.ReadByte();
                data.DamageModel.BrokenWindows = r.ReadByte();
                data.DamageModel.BrokenLights  = r.ReadInt32();
            }

            // Does he have a traielr?
            if (r.ReadBoolean())
            {
                Vector3 trailerPos = new Vector3();

                trailerPos.X = r.ReadSingle();
                trailerPos.Y = r.ReadSingle();
                trailerPos.Z = r.ReadSingle();

                data.Trailer = trailerPos;
            }

            // Try to read latency
            if (r.CanRead(2))
            {
                var latency = r.ReadInt16();
                data.Latency = latency / 1000f;
            }

            return(data);
        }
        public static byte[] WriteUnOccupiedVehicleSync(VehicleData data)
        {
            List <byte> byteArray = new List <byte>();

            // Write vehicle's nethandle.
            byteArray.AddRange(GetBytes(data.VehicleHandle.Value));

            // Write vehicle position, rotation and velocity
            byteArray.AddRange(GetBytes(data.Position.X));
            byteArray.AddRange(GetBytes(data.Position.Y));
            byteArray.AddRange(GetBytes(data.Position.Z));

            byteArray.AddRange(GetBytes(data.Quaternion.X));
            byteArray.AddRange(GetBytes(data.Quaternion.Y));
            byteArray.AddRange(GetBytes(data.Quaternion.Z));

            byteArray.AddRange(GetBytes(data.Velocity.X));
            byteArray.AddRange(GetBytes(data.Velocity.Y));
            byteArray.AddRange(GetBytes(data.Velocity.Z));

            // Write vehicle health
            byteArray.AddRange(GetBytes((short)((int)data.VehicleHealth.Value)));

            // Set if whether its dead
            if (CheckBit(data.Flag.Value, VehicleDataFlags.VehicleDead))
            {
                byteArray.Add(0x01);
            }
            else
            {
                byteArray.Add(0x00);
            }

            // Write the tyre state, using the playerhealth in VehicleData
            byteArray.Add(data.PlayerHealth.Value);

            // Write vehicle damage model
            byteArray.Add(data.DamageModel.BrokenDoors);   // Write doors
            byteArray.Add(data.DamageModel.BrokenWindows); // Write windows

            return(byteArray.ToArray());
        }
        public static VehicleData ReadPureVehicleSync(byte[] array)
        {
            var data = new VehicleData();
            var r    = new BitReader(array);

            // Read player nethandle
            data.NetHandle = r.ReadInt32();

            // read health values
            data.PlayerHealth = r.ReadByte();
            data.PedArmor     = r.ReadByte();

            // read flag
            data.Flag = r.ReadInt16();

            // If we're shooting/aiming, read gun stuff
            if (CheckBit(data.Flag.Value, VehicleDataFlags.Shooting) ||
                CheckBit(data.Flag.Value, VehicleDataFlags.HasAimData) ||
                CheckBit(data.Flag.Value, VehicleDataFlags.Aiming))
            {
                // read gun model
                data.WeaponHash = r.ReadInt32();

                // read aim coordinates
                Vector3 aimCoords = new Vector3();

                aimCoords.X = r.ReadSingle();
                aimCoords.Y = r.ReadSingle();
                aimCoords.Z = r.ReadSingle();

                data.AimCoords = aimCoords;
            }

            // Are we the driver?
            if (CheckBit(data.Flag.Value, VehicleDataFlags.Driver))
            {
                // Read position, rotation and velocity.
                Vector3 position = new Vector3();
                Vector3 rotation = new Vector3();
                Vector3 velocity = new Vector3();

                position.X = r.ReadSingle();
                position.Y = r.ReadSingle();
                position.Z = r.ReadSingle();

                rotation.X = r.ReadSingle();
                rotation.Y = r.ReadSingle();
                rotation.Z = r.ReadSingle();

                velocity.X = r.ReadSingle();
                velocity.Y = r.ReadSingle();
                velocity.Z = r.ReadSingle();

                data.Position   = position;
                data.Quaternion = rotation;
                data.Velocity   = velocity;

                // Read car health
                data.VehicleHealth = r.ReadInt16();

                // read RPM & steering angle
                byte rpmCompressed = r.ReadByte();
                data.RPM = rpmCompressed / (float)byte.MaxValue;

                byte angleCompressed = r.ReadByte();
                var  angleDenorm     = 120f * (angleCompressed / (float)byte.MaxValue);
                data.Steering = angleDenorm - 60f;
            }

            return(data);
        }
        public static byte[] WriteLightSync(VehicleData data)
        {
            List <byte> byteArray = new List <byte>();

            // Write player's nethandle.
            if (data.NetHandle.HasValue)
            {
                byteArray.AddRange(GetBytes(data.NetHandle.Value));
            }
            else
            {
                byteArray.Add(0x00);
                byteArray.Add(0x00);
                byteArray.Add(0x00);
                byteArray.Add(0x00);
            }

            // Write player model
            byteArray.AddRange(GetBytes(data.PedModelHash.Value));

            // Write his vehicle handle
            byteArray.AddRange(GetBytes(data.VehicleHandle.Value));

            // Write his seat
            byteArray.Add((byte)data.VehicleSeat.Value);

            // Write the gun model
            byteArray.AddRange(GetBytes(data.WeaponHash.Value));

            // Write vehicle damage model
            if (data.DamageModel != null)
            {
                byteArray.Add(0x01);
                byteArray.Add(data.DamageModel.BrokenDoors);                 // Write doors
                byteArray.Add(data.DamageModel.BrokenWindows);               // Write windows
                byteArray.AddRange(GetBytes(data.DamageModel.BrokenLights)); // Lights
            }
            else
            {
                byteArray.Add(0x00);
            }

            // If he has a trailer attached, write it's position. (Maybe we can use his pos & rot to calculate it serverside?)
            if (data.Trailer != null)
            {
                byteArray.Add(0x01);
                byteArray.AddRange(GetBytes(data.Trailer.X));
                byteArray.AddRange(GetBytes(data.Trailer.Y));
                byteArray.AddRange(GetBytes(data.Trailer.Z));
            }
            else
            {
                byteArray.Add(0x00);
            }

            // Write player's latency
            if (data.Latency.HasValue)
            {
                var latency = data.Latency.Value * 1000;
                byteArray.AddRange(GetBytes((short)latency));
            }

            return(byteArray.ToArray());
        }
        public static byte[] WritePureSync(VehicleData data)
        {
            List <byte> byteArray = new List <byte>();

            // Write player's nethandle.
            if (data.NetHandle.HasValue)
            {
                byteArray.AddRange(GetBytes(data.NetHandle.Value));
            }
            else
            {
                byteArray.Add(0x00);
                byteArray.Add(0x00);
                byteArray.Add(0x00);
                byteArray.Add(0x00);
            }

            // Write player health and armor
            byteArray.Add(data.PlayerHealth.Value);
            byteArray.Add(data.PedArmor.Value);

            // Write the flag
            byteArray.AddRange(GetBytes(data.Flag.Value));

            if (CheckBit(data.Flag.Value, VehicleDataFlags.Aiming) ||
                CheckBit(data.Flag.Value, VehicleDataFlags.HasAimData) ||
                CheckBit(data.Flag.Value, VehicleDataFlags.Shooting))
            {
                // Write the gun model
                byteArray.AddRange(GetBytes(data.WeaponHash.Value));

                // Write the aiming point
                byteArray.AddRange(GetBytes(data.AimCoords.X));
                byteArray.AddRange(GetBytes(data.AimCoords.Y));
                byteArray.AddRange(GetBytes(data.AimCoords.Z));
            }

            // Are we the driver?
            if (CheckBit(data.Flag.Value, VehicleDataFlags.Driver))
            {
                // Write vehicle position, rotation and velocity
                byteArray.AddRange(GetBytes(data.Position.X));
                byteArray.AddRange(GetBytes(data.Position.Y));
                byteArray.AddRange(GetBytes(data.Position.Z));

                byteArray.AddRange(GetBytes(data.Quaternion.X));
                byteArray.AddRange(GetBytes(data.Quaternion.Y));
                byteArray.AddRange(GetBytes(data.Quaternion.Z));


                byteArray.AddRange(GetBytes(data.Velocity.X));
                byteArray.AddRange(GetBytes(data.Velocity.Y));
                byteArray.AddRange(GetBytes(data.Velocity.Z));

                // Write vehicle health
                byteArray.AddRange(GetBytes((short)((int)data.VehicleHealth.Value)));

                // Write engine stuff
                byte rpm = (byte)(data.RPM.Value * byte.MaxValue);

                float angle = Extensions.Clamp(data.Steering.Value, -60f, 60f);
                angle += 60f;
                byte angleCrammed = (byte)((angle / 120f) * byte.MaxValue);

                byteArray.Add(rpm);
                byteArray.Add(angleCrammed);
            }

            return(byteArray.ToArray());
        }