{$CLEO .cs}
{$USE CLEO+}
{$USE ini}
nop
script_name 'EHIDATE'

int iIconFlagsPtr, iIconFlags, iDateReport, iPassedCat1, iPassedCat2, iMissionsPassed, i, iFlags, iState, iGameSaved, iScriptSpace, iFinalOffset
int iBlip[5]

const
    Montgomery = 0
    LasPayasadas = 1
    AngelPine = 2
    RegularTom = 3
    Bayside = 4
end

if
    iIconFlagsPtr == 0
then
    terminate_this_custom_script
end

i = 0
read_int_from_ini_file i "cleo\EHI.ini" section "RESTAURANTS" key "Diner1"
if
    i > 0
then
    set_local_var_bit_const iFlags bit 0
end
i = 0
read_int_from_ini_file i "cleo\EHI.ini" section "RESTAURANTS" key "Diner2"
if
    i > 0
then
    set_local_var_bit_const iFlags bit 1
end
i = 0
read_int_from_ini_file i "cleo\EHI.ini" section "RESTAURANTS" key "SecretValley"
if
    i > 0
then
    set_local_var_bit_const iFlags bit 2
end
i = 0
read_int_from_ini_file i "cleo\EHI.ini" section "RESTAURANTS" key "WorldOfCoq"
if
    i > 0
then
    set_local_var_bit_const iFlags bit 3
end
i = 0
read_int_from_ini_file i "cleo\EHI.ini" section "RESTAURANTS" key "WelcomePump"
if
    i > 0
then
    set_local_var_bit_const iFlags bit 4
else
    iMissionsPassed = -1
end
i = 0

07FB: switch_entry_exit 'DINER1' access 0
07FB: switch_entry_exit 'DINER2' access 0
07FB: switch_entry_exit 'REST2' access 0
07FB: switch_entry_exit 'FDREST1' access 0
07FB: switch_entry_exit 'TSDINER' access 0

gosub @EnableDatingInteriors

for i = 0 to 4 step 1
    wait 0
    iBlip[i] = -1
end

read_memory iIconFlags iIconFlagsPtr size 4 virtual_protect 1
clear_local_var_bit_const iIconFlags bit 5
clear_local_var_bit_const iIconFlags bit 6

if
    iFlags > 0
then
    set_script_event_save_confirmation 1 label @CleanUpBeforeSave var_slot iGameSaved
else
    if
        iIconFlags < 1
    then
        terminate_this_custom_script
    end
end

while true
    wait 500    
    if
        iGameSaved > -1
    then
        wait 0
        wait 0
        gosub @EnableDatingInteriors
    end
    if
        iMissionsPassed > -1
    then
        get_int_stat i = integer_stat 147  // Missions Passed
        if
            i <> iMissionsPassed
        then
            iMissionsPassed = i
            iState = 1
            gosub @CheckWelcomePump
        end
    end   
    get_script_struct_named i 'GFDATE' 
    if and
        i <> 0
        i <> -1
    then
        if
            iFlags > 0
        then
            set_script_event_save_confirmation 0 label @CleanUpBeforeSave var_slot iGameSaved
        end
        wait 500
        
        read_memory iScriptSpace 0x00468D5E size 4 virtual_protect 1
        int_add iFinalOffset = iScriptSpace + 1560 // Offset of global var 390
        read_memory iDateReport iFinalOffset size 4 virtual_protect 0
        
        if and
            iIconFlags > 0
            is_local_var_bit_set_const iDateReport bit 11   // Eat Out
        then   
            if
                is_local_var_bit_set_const iIconFlags bit Montgomery
            then
                add_cleo_blip 17 position 1294.688 235.702 is_short 1 RGBA 255 255 255 255 store_to iBlip[0]
            end
            if
                is_local_var_bit_set_const iIconFlags bit LasPayasadas
            then
                add_cleo_blip 17 position -254.806 2603.212 is_short 1 RGBA 255 255 255 255 store_to iBlip[1]
            end
            if
                is_local_var_bit_set_const iIconFlags bit AngelPine
            then
                add_cleo_blip 17 position -2103.958 -2342.111 is_short 1 RGBA 255 255 255 255 store_to iBlip[2]
            end
            if
                is_local_var_bit_set_const iIconFlags bit RegularTom
            then
                add_cleo_blip 50 position -314.358 1774.541 is_short 1 RGBA 255 255 255 255 store_to iBlip[3]
            end
            if
                is_local_var_bit_set_const iIconFlags bit BaySide
            then
                add_cleo_blip 50 position -2342.350 2532.693 is_short 1 RGBA 255 255 255 255 store_to iBlip[4]
            end
        end
                    
        repeat
            wait 0
            read_memory i iFinalOffset size 4 virtual_protect 0
        until not is_local_var_bit_set_const i bit 1    // Date in progress
        
        for i = 0 to 4 step 1            
            if
                iBlip[i] > -1
            then
                remove_cleo_blip iBlip[i]
                iBlip[i] = -1
            end
        end
        
        if
            iFlags > 0
        then
            gosub @SwitchDatingInteriors
            set_script_event_save_confirmation 1 label @CleanUpBeforeSave var_slot iGameSaved
        end                
        wait 500       
    end
end


:EnableDatingInteriors
iGameSaved = -1
iState = 1

:SwitchDatingInteriors
if
    is_local_var_bit_set_const iFlags bit 0
then
    switch_entry_exit 'DINER1' access iState
end
if
    is_local_var_bit_set_const iFlags bit 1
then
    switch_entry_exit 'DINER2' access iState
end
if
    is_local_var_bit_set_const iFlags bit 2
then
    switch_entry_exit 'REST2' access iState
end
if
    is_local_var_bit_set_const iFlags bit 3
then
    switch_entry_exit 'FDREST1' access iState
end
if
    is_local_var_bit_set_const iFlags bit 4
then
    if
        iState == 1
    then
        :CheckWelcomePump
        get_int_stat iPassedCat1 = integer_stat 318
        get_int_stat iPassedCat2 = integer_stat 315
        if and
            iPassedCat1 < 1
            iPassedCat2 < 1
        then
            get_script_struct_named i 'TRUCKS' 
            if and
                i <> 0
                i <> -1
            return_if_false
        end
        iMissionsPassed = -1
    end    
    switch_entry_exit 'TSDINER' access iState
end
return


:CleanUpBeforeSave
iState = 0
gosub @SwitchDatingInteriors
return_script_event


terminate_this_custom_script