bool CVehiclePuresyncPacket::Write ( NetBitStreamInterface& BitStream ) const
{
    // Got a player to send?
    if ( m_pSourceElement )
    {
        CPlayer * pSourcePlayer = static_cast < CPlayer * > ( m_pSourceElement );

        // Player is in a vehicle and is the driver?
        CVehicle* pVehicle = pSourcePlayer->GetOccupiedVehicle ();
        if ( pVehicle )
        {
            // Player ID
            ElementID PlayerID = pSourcePlayer->GetID ();
            BitStream.Write ( PlayerID );

            // Write the time context of that player
            BitStream.Write ( pSourcePlayer->GetSyncTimeContext () );

            // Write his ping divided with 2 plus a small number so the client can find out when this packet was sent
            unsigned short usLatency = pSourcePlayer->GetPing ();
            BitStream.WriteCompressed ( usLatency );

            // Write the keysync data
            CControllerState ControllerState = pSourcePlayer->GetPad ()->GetCurrentControllerState ();
            WriteFullKeysync ( ControllerState, BitStream );

            // Write the vehicle matrix only if he's the driver
            CVector vecTemp;
            unsigned int uiSeat = pSourcePlayer->GetOccupiedVehicleSeat ();
            if ( uiSeat == 0 )
            {
                // Vehicle position
                SPositionSync position ( false );
                position.data.vecPosition = pVehicle->GetPosition ();
                BitStream.Write ( &position );

                // Vehicle rotation
                SRotationDegreesSync rotation;
                pVehicle->GetRotationDegrees ( rotation.data.vecRotation );
                BitStream.Write ( &rotation );

                // Move speed vector
                SVelocitySync velocity;
                velocity.data.vecVelocity = pVehicle->GetVelocity ();
                BitStream.Write ( &velocity );

                // Turn speed vector
                SVelocitySync turnSpeed;
                turnSpeed.data.vecVelocity = pVehicle->GetTurnSpeed ();
                BitStream.Write ( &turnSpeed );

                // Health
                SVehicleHealthSync health;
                health.data.fValue = pVehicle->GetHealth ();
                BitStream.Write ( &health );
            }

            // Player health and armor
            SPlayerHealthSync health;
            health.data.fValue = pSourcePlayer->GetHealth ();
            BitStream.Write ( &health );

            SPlayerArmorSync armor;
            armor.data.fValue = pSourcePlayer->GetArmor ();
            BitStream.Write ( &armor );

            // Weapon
            unsigned char ucWeaponType = pSourcePlayer->GetWeaponType ();

            // Flags
            SVehiclePuresyncFlags flags;
            flags.data.bIsWearingGoggles     = pSourcePlayer->IsWearingGoggles ();
            flags.data.bIsDoingGangDriveby   = pSourcePlayer->IsDoingGangDriveby ();
            flags.data.bIsSirenOrAlarmActive = pVehicle->IsSirenActive ();
            flags.data.bIsSmokeTrailEnabled  = pVehicle->IsSmokeTrailEnabled ();
            flags.data.bIsLandingGearDown    = pVehicle->IsLandingGearDown ();
            flags.data.bIsOnGround           = pVehicle->IsOnGround ();
            flags.data.bIsInWater            = pVehicle->IsInWater ();
            flags.data.bIsDerailed           = pVehicle->IsDerailed ();
            flags.data.bIsAircraft           = ( pVehicle->GetVehicleType () == VEHICLE_PLANE ||
                                                 pVehicle->GetVehicleType () == VEHICLE_HELI );
            flags.data.bHasAWeapon           = ( ucWeaponType != 0 );
            flags.data.bIsHeliSearchLightVisible = pVehicle->IsHeliSearchLightVisible ();
            BitStream.Write ( &flags );

            // Write the weapon stuff
            if ( flags.data.bHasAWeapon )
            {
                // Write the weapon slot
                SWeaponSlotSync slot;
                slot.data.uiSlot = pSourcePlayer->GetWeaponSlot ();
                BitStream.Write ( &slot );

                if ( flags.data.bIsDoingGangDriveby && CWeaponNames::DoesSlotHaveAmmo ( slot.data.uiSlot ) )
                {
                    // Write the ammo states
                    SWeaponAmmoSync ammo ( ucWeaponType, false, true );
                    ammo.data.usAmmoInClip = pSourcePlayer->GetWeaponAmmoInClip ();
                    BitStream.Write ( &ammo );

                    // Sync aim data
                    SWeaponAimSync aim ( 0.0f, true );
                    aim.data.vecOrigin = pSourcePlayer->GetSniperSourceVector ();
                    pSourcePlayer->GetTargettingVector ( aim.data.vecTarget );
                    aim.data.fArm = pSourcePlayer->GetAimDirection ();
                    BitStream.Write ( &aim );

                    // Sync driveby direction
                    SDrivebyDirectionSync driveby;
                    driveby.data.ucDirection = pSourcePlayer->GetDriveByDirection ();
                    BitStream.Write ( &driveby );
                }
            }

            // Vehicle specific data only if he's the driver
            if ( uiSeat == 0 )
            {
                WriteVehicleSpecific ( pVehicle, BitStream );
            }

            // Write vehicle_look_left and vehicle_look_right control states when
            // it's an aircraft.
            if ( flags.data.bIsAircraft )
            {
                BitStream.WriteBit ( ControllerState.LeftShoulder2 != 0 );
                BitStream.WriteBit ( ControllerState.RightShoulder2 != 0 );
            }

            // Success
            return true;
        }
    }

    return false;
}
Beispiel #2
0
bool CKeysyncPacket::Write ( NetBitStreamInterface& BitStream ) const
{
    // Got a player to write?
    if ( m_pSourceElement )
    {
        CPlayer * pSourcePlayer = static_cast < CPlayer * > ( m_pSourceElement );
        CVehicle* pVehicle = pSourcePlayer->GetOccupiedVehicle ();

        // Write the source player id
        ElementID PlayerID = pSourcePlayer->GetID ();
        BitStream.WriteCompressed ( PlayerID );

        // Write the keysync data
        const CControllerState& ControllerState = pSourcePlayer->GetPad ()->GetCurrentControllerState ();
        const CControllerState& LastControllerState = pSourcePlayer->GetPad ()->GetLastControllerState ();
        WriteSmallKeysync ( ControllerState, LastControllerState, BitStream );

        // Flags
        SKeysyncFlags flags;
        flags.data.bIsDucked = ( pSourcePlayer->IsDucked () == true );
        flags.data.bIsChoking = ( pSourcePlayer->IsChoking () == true );
        flags.data.bAkimboTargetUp = ( pSourcePlayer->IsAkimboArmUp () == true );
        flags.data.bSyncingVehicle = ( pVehicle != NULL && pSourcePlayer->GetOccupiedVehicleSeat () == 0 );

        // Write the flags
        BitStream.Write ( &flags );

        // If he's shooting
        if ( ControllerState.ButtonCircle )
        {
            // Write his current weapon slot
            unsigned int uiSlot = pSourcePlayer->GetWeaponSlot ();
            SWeaponSlotSync slot;
            slot.data.uiSlot = uiSlot;
            BitStream.Write ( &slot );

            if ( CWeaponNames::DoesSlotHaveAmmo ( uiSlot ) )
            {
                // Write his ammo in clip
                SWeaponAmmoSync ammo ( pSourcePlayer->GetWeaponType (), false, true );
                ammo.data.usAmmoInClip = pSourcePlayer->GetWeaponAmmoInClip ();
                BitStream.Write ( &ammo );

                // Write the weapon aim data
                SWeaponAimSync aim ( 0.0f );
                aim.data.vecOrigin = pSourcePlayer->GetSniperSourceVector ();
                pSourcePlayer->GetTargettingVector ( aim.data.vecTarget );
                aim.data.fArm = pSourcePlayer->GetAimDirection ();
                BitStream.Write ( &aim );

                // Write the driveby aim directoin
                BitStream.Write ( pSourcePlayer->GetDriveByDirection () );
            }
            else
            {
                pSourcePlayer->SetWeaponAmmoInClip ( 1 );
                pSourcePlayer->SetWeaponTotalAmmo ( 1 );
            }
        }

        // If he's in a vehicle, read out the small vehicle specific data
        if ( flags.data.bSyncingVehicle )
        {
            WriteVehicleSpecific ( pVehicle, BitStream );

            if ( pVehicle->GetUpgrades ()->HasUpgrade ( 1087 ) ) // Hydraulics?
            {
                BitStream.Write ( ControllerState.RightStickX );
                BitStream.Write ( ControllerState.RightStickY );
            }

            if ( pVehicle->GetVehicleType () == VEHICLE_PLANE ||
                 pVehicle->GetVehicleType () == VEHICLE_HELI )
            {
                BitStream.WriteBit ( ControllerState.LeftShoulder2 != 0);
                BitStream.WriteBit ( ControllerState.RightShoulder2 != 0);
            }
        }

        return true;
    }

    return false;
}