{$CLEO .cs}
{$USE CLEO+}
nop
script_name 'BETRFLY'
wait 0
wait 0

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
jf @ILoveMyGirlfriendJessica
if
    0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
then
    0AA7: call_function_return 30@ num_params 0 pop 0 29@
end
0AA3: free_dynamic_library 31@
if
    29@ >= 0x1020000
jf @ILoveMyGirlfriendJessica


int iCar, iCarPtr, iHdlgData, iIntensity, iTweak = -128
float fFormLift, fAttackLift


:Main
wait 400

:ResetEverything
TIMERA = 0

:ResetHandlingData
if
    iHdlgData <> 0
then
    write_struct_offset iHdlgData offset 0x28 size 4 value fFormLift      // FormLift
    write_struct_offset iHdlgData offset 0x2C size 4 value fAttackLift    // AttackLift
    iHdlgData = 0
    iTweak = -128
end

while true
    wait 0
    if
        is_player_playing $PLAYER_CHAR
    jf @Main
    if
        is_char_sitting_in_any_car $PLAYER_ACTOR
    jf @Main
    if and
        is_player_control_on $PLAYER_CHAR
        is_char_in_flying_vehicle $PLAYER_ACTOR
    jf @Main
    if and
        not is_char_in_model $PLAYER_ACTOR 520  // Hydra 
        not is_player_in_remote_mode $PLAYER_CHAR
        not is_char_attached_to_any_car $PLAYER_ACTOR
    jf @Main
             
    if 
        is_button_pressed 0 6       // Space / R1
    jf @ResetEverything
    if
        not is_button_pressed 0 1   // ArrowUp / LeftStickY
    jf @ResetHandlingData
    
    if
        is_char_in_any_plane $PLAYER_ACTOR
    then
        iTweak = 32
        if
            not is_button_pressed 0 16  // Accelerate / Cross
        then
            iTweak = -64
            if
                not is_button_pressed 0 14  // Brake / Square
            then
                write_memory 0xB73478 size 2 value 255 virtual_protect 0
            end
        end            
    else   
        if
            iHdlgData == 0
        then
            store_car_char_is_in_no_save iCar $PLAYER_ACTOR
            get_vehicle_pointer iCarPtr iCar
            read_struct_offset iHdlgData iCarPtr offset 0x388 size 4 
            read_struct_offset fFormLift iHdlgData offset 0x28 size 4
            read_struct_offset fAttackLift iHdlgData offset 0x2C size 4
        end
        
        iIntensity = -128
        if and
            not is_button_pressed 0 16  // Accelerate / Cross
            not is_button_pressed 0 14  // Brake / Square
        then
            if
                TIMERA > 500
            then
                write_struct_offset iHdlgData offset 0x28 size 4 value 0.52      // FormLift
                write_struct_offset iHdlgData offset 0x2C size 4 value 0.0020    // AttackLift
            end
        else
            write_struct_offset iHdlgData offset 0x28 size 4 value 0.56      // FormLift
            write_struct_offset iHdlgData offset 0x2C size 4 value 0.0022    // AttackLift
            if
                is_button_pressed 0 14  // Brake / Square
            then
                write_memory 0xB73474 size 2 value 0 virtual_protect 0
                iIntensity = 128
            end
        end
        
        if and
            fFormLift > 0.65
            fAttackLift > 0.0025
        then
            iIntensity /= 2
        end        
         
        if
            iTweak <> iIntensity
        then
            if
                iTweak > iIntensity
            then
                iTweak -= 8
            else
                iTweak += 8
            end
        end
    end 
   
    write_memory 0xB7345A size 2 value iTweak virtual_protect 0   // Emulate ArrowUp / LeftStickY
end




:ILoveMyGirlfriendJessica
TIMERA = 0

repeat
    wait 0
    0AD1: print_formatted_now "Better Flight Control: CLEO+ 1.2.0 or newer required. It's a plugin for CLEO. Please download and install it." time 200
    if and
        09E7: is_player_control_on $PLAYER_CHAR
        816B: not get_fading_status
    jf @ILoveMyGirlfriendJessica
until TIMERA > 10000                    

terminate_this_custom_script