{$CLEO .cs}
{$USE cleo+}
nop
script_name 'DRELFIX'
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 i1
int iAnimPlaying, iReloadTime
int iPlPtr, iWeapInfo, iCWeapon
int iTotalAmmo, iClip

while true
    wait 500
    if and
        is_player_playing $PLAYER_ACTOR
        not is_on_scripted_cutscene
        not is_char_sitting_in_any_car $PLAYER_ACTOR
    then
        while is_char_using_gun $PLAYER_ACTOR
            wait 0
            get_char_weapon_state $PLAYER_ACTOR store_to i1
            if
                i1 == 2
            then
                gosub @FindAnim
                if
                    iAnimPlaying == true
                then
                    TIMERA = 0
                    
                    repeat
                        wait 0
                        gosub @FindAnim                                              
                    until iAnimPlaying == false
                    
                    wait 0
                    
                    if and
                        TIMERA > iReloadTime
                        is_char_using_gun $PLAYER_ACTOR
                        is_player_playing $PLAYER_ACTOR
                        not is_on_scripted_cutscene
                    then
                        get_char_weapon_state $PLAYER_ACTOR store_to i1                       
                        if
                            i1 == 2                            
                        then
                            get_current_char_weaponinfo $PLAYER_ACTOR store_to iWeapInfo
                            read_struct_offset i1 iWeapInfo offset 0x14 size 4    //Slot
                            i1 *= 0x1C //CWeapon Size
                            get_ped_pointer iPlPtr $PLAYER_ACTOR
                            i1 += iPlPtr
                            int_add iCWeapon = i1 + 0x5A0 //CWeapon
                                               
                            read_struct_offset iClip iWeapInfo offset 0x20 size 2   //Ammo clip                                             
                            read_struct_offset i1 iWeapInfo offset 0x18 size 4   //Flags
                            if and
                                TIMERA < 1200
                                i1 == 14387    //Dual wielded
                            then
                                iClip /= 2
                            end
                            
                            read_struct_offset iTotalAmmo iCWeapon offset 0x0C size 4    //Total ammo
                            clamp_int iClip min 1 max iTotalAmmo store_to iClip
                            write_struct_offset iCWeapon offset 0x08 size 4 value iClip  //Current ammo in clip    
                            write_struct_offset iCWeapon offset 0x04 size 4 value 0x00  //Weapon state
                        end
                    end                           
                end
            end
        end
    end
end


:FindAnim
iAnimPlaying = True
if or
    is_char_playing_anim $PLAYER_ACTOR performing "UZI_reload"
    is_char_playing_anim $PLAYER_ACTOR performing "UZI_crouchreload"
    is_char_playing_anim $PLAYER_ACTOR performing "RIFLE_load"
    is_char_playing_anim $PLAYER_ACTOR performing "RIFLE_crouchload"
    is_char_playing_anim $PLAYER_ACTOR performing "python_reload"
    is_char_playing_anim $PLAYER_ACTOR performing "python_crouchreload"
    is_char_playing_anim $PLAYER_ACTOR performing "buddy_reload"
    is_char_playing_anim $PLAYER_ACTOR performing "buddy_crouchreload"
then
    iReloadTime = 640
    return
end
if or
    is_char_playing_anim $PLAYER_ACTOR performing "TEC_reload"
    is_char_playing_anim $PLAYER_ACTOR performing "TEC_crouchreload"
    is_char_playing_anim $PLAYER_ACTOR performing "colt45_reload"
    is_char_playing_anim $PLAYER_ACTOR performing "colt45_crouchreload"
    is_char_playing_anim $PLAYER_ACTOR performing "CrouchReload"
    is_char_playing_anim $PLAYER_ACTOR performing "Silence_reload"
    is_char_playing_anim $PLAYER_ACTOR performing "sawnoff_reload"
then
    iReloadTime = 420
    return
end
iAnimPlaying = False
return


:ILoveMyGirlfriendJessica
TIMERA = 0

repeat
    0AD1: print_formatted_now "Double Reload Fix: CLEO+ 1.2.0 or newer required. Please download and install it in cleo folder." time 100
    wait 0
    if or
        89E7: not is_player_control_on $PLAYER_CHAR
        016B: fading
    then
        TIMERA = 0
    end
until TIMERA > 8000         

terminate_this_custom_script