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

int iPropertyPrice[3]   // Received from main script, keep it as first!
int pBuyPropertyPickup[3], pPropertyBlip[3]
float fBuyPropertyPickupX[3], fBuyPropertyPickupY[3], fBuyPropertyPickupZ[3]
float fEnexX[3], fEnexY[3]
float f
int pSavePickup = -1, iSavingFlags, iGameSaved, iCities, iCarGen, i

// Restore Saving Flags from global var (Z-coord of Madd Dogg mission marker in LV)  
set_lvar_float_to_var_float f = $Z_ROYAL_CASINO
f -= 9.8    // Original value
f *= 1000.0
f += 0.2    // Prevent float precision problem
cset_lvar_int_to_lvar_float iSavingFlags = f
if or
    iSavingFlags < 0
    iSavingFlags > 7
then
    terminate_this_custom_script    // If global var has an unexpected value
end

for i = 0 to 2 step 1
    pBuyPropertyPickup[i] = -1
    pPropertyBlip[i] = -1
end       

// Mulholland
fBuyPropertyPickupX[0] = 1497.125
fBuyPropertyPickupY[0] = -694.366
fBuyPropertyPickupZ[0] = 94.250

fEnexX[0] = 1496.995
fEnexY[0] = -687.892

// Richman
fBuyPropertyPickupX[1] = 255.719
fBuyPropertyPickupY[1] = -1226.193
fBuyPropertyPickupZ[1] = 74.360

fEnexX[1] = 251.442
fEnexY[1] = -1220.204

// Bayside
fBuyPropertyPickupX[2] = -2349.814
fBuyPropertyPickupY[2] = 2421.126
fBuyPropertyPickupZ[2] = 6.777

fEnexX[2] = -2348.577
fEnexY[2] = 2423.293       

if
    iPropertyPrice[2] > 0
then
    get_int_stat iCities = integer_stat 181
end
clamp_int iCities min 1 max 2 store_to iCities

set_script_event_save_confirmation 1 label @CleanUpBeforeSave var_slot iGameSaved

if
    locate_char_distance_to_coordinates $PLAYER_ACTOR pos 2311.139 -1141.286 1054.0 radius 4.0
then
    set_char_heading $PLAYER_ACTOR 270.0
    restore_camera_jumpcut
    set_camera_behind_player
end


while true
    wait 100
    if
        iGameSaved > -1
    then
        wait 0
        wait 0
        
        // Create buy pickup or activate property
        for i = 0 to 2 step 1
            if
                iPropertyPrice[i] > 0
            then
                if
                    not is_local_var_bit_set_lvar iSavingFlags bit i
                then
                    if and
                        i == 2
                        iCities == 1
                    then
                        create_locked_property_pickup pBuyPropertyPickup[i] = create_unavailable_asset_pickup 'PROP_4' at fBuyPropertyPickupX[i] fBuyPropertyPickupY[i] fBuyPropertyPickupZ[i]
                        add_cleo_blip 32 position fBuyPropertyPickupX[i] fBuyPropertyPickupY[i] is_short 1 RGBA 255 255 255 255 store_to pPropertyBlip[i]
                    else
                        :CreateForsalePickup    
                        create_forsale_property_pickup pBuyPropertyPickup[i] = create_available_asset_pickup 'PROP_3' at fBuyPropertyPickupX[i] fBuyPropertyPickupY[i] fBuyPropertyPickupZ[i] price iPropertyPrice[i]
                        add_cleo_blip 31 position fBuyPropertyPickupX[i] fBuyPropertyPickupY[i] is_short 1 RGBA 255 255 255 255 store_to pPropertyBlip[i]
                    end
                    set_closest_entry_exit_flag fEnexX[i] fEnexY[i] radius 10.0 bitmask 16384 flag 0
                else
                    add_cleo_blip 35 position fEnexX[i] fEnexY[i] is_short 1 RGBA 255 255 255 255 store_to pPropertyBlip[i]
                    set_closest_entry_exit_flag fEnexX[i] fEnexY[i] radius 10.0 bitmask 16384 flag 1
                    gosub @CreateCarGen
                end
            end
        end
        
        iGameSaved = -1
        continue
    end
    
    // Save pickup event
    if or
        iPropertyPrice[0] > 0
        iPropertyPrice[1] > 0
    then    
        if
            not is_on_mission
        then
            if
                not does_pickup_exist pSavePickup
            then
                create_pickup pSavePickup 1277 3 2311.139 -1141.286 1054.0
                continue
            end
            if
                has_pickup_been_collected pSavePickup
            then
                remove_pickup pSavePickup
                pSavePickup = -1
                set_player_control $PLAYER_CHAR 0   		    
    		    activate_save_menu
                            
                while not has_save_game_finished
                    wait 0
                end
                
                set_fading_colour 0 0 0
                do_fade 0 time 1000             
                if
                    iGameSaved == -1
                then               
                    clear_area 1 at 2312.374 -1138.771 1053.3 radius 1.0
                    set_char_coordinates $PLAYER_ACTOR 2312.374 -1138.771 1053.3
                    set_char_heading $PLAYER_ACTOR 270.0
                end                               
    		    wait 0    		    
    		    do_fade 1 time 1000               
    		    restore_camera_jumpcut
                set_camera_behind_player
                wait 500
                set_player_control $PLAYER_CHAR 1
            end
        else
            if
                does_pickup_exist pSavePickup
            then
                remove_pickup pSavePickup
                pSavePickup = -1
            end
        end
    end
    
    if
        iSavingFlags < 7
    jf continue
    
    if and
        iCities == 1
        iPropertyPrice[2] > 0
    then
        get_int_stat iCities = integer_stat 181
        clamp_int iCities min 1 max 2 store_to iCities
        if
            iCities == 2
        then
            if
                does_pickup_exist pBuyPropertyPickup[2]
            then
                remove_pickup pBuyPropertyPickup[2]
                pBuyPropertyPickup[2] = -1
            end
            if
                pPropertyBlip[2] > -1
            then
                remove_cleo_blip pPropertyBlip[2]
                pPropertyBlip[2] = -1
            end
            i = 2
            jump @CreateForsalePickup
        end
    end
        
    // Buy pickup event
    for i = 0 to iCities step 1
        if
            does_pickup_exist pBuyPropertyPickup[i]
        then
            if
                has_pickup_been_collected pBuyPropertyPickup[i]
            then
                remove_pickup pBuyPropertyPickup[i]
                pBuyPropertyPickup[i] = -1                
                gosub @CreateCarGen                                           
                set_everyone_ignore_player $PLAYER_CHAR 1                
                report_mission_audio_event_at_position fBuyPropertyPickupX[i] fBuyPropertyPickupY[i] fBuyPropertyPickupZ[i] event 1149
                wait 100
                set_fading_colour 0 0 1
                set_player_control $PLAYER_CHAR 0
		        switch_widescreen 1
		        
                if
                    i == 0
                then
                    set_fixed_camera_position 1477.095 -714.565 105.034 0.0 0.0 0.0
                    point_camera_at_point 1477.681 -713.776 104.851 2
                end
                if
                    i == 1
                then
                    set_fixed_camera_position 257.895 -1251.221 85.560 0.0 0.0 0.0
                    point_camera_at_point 257.645 -1250.264 85.414 2
                end
                if
                    i == 2
                then
                    set_fixed_camera_position -2382.475 2411.135 14.175 0.0 0.0 0.0
                    point_camera_at_point -2381.483 2411.222 14.080 2
                end
                                
                if
                    pPropertyBlip[i] > -1
                then
                    remove_cleo_blip pPropertyBlip[i]
                    pPropertyBlip[i] = -1
                end                
                add_cleo_blip 35 position fEnexX[i] fEnexY[i] is_short 1 RGBA 255 255 255 255 store_to pPropertyBlip[i]
                set_closest_entry_exit_flag fEnexX[i] fEnexY[i] radius 10.0 bitmask 16384 flag 1
                increment_int_stat iPropertyPrice[i] to_integer_stat 15
                set_local_var_bit_lvar iSavingFlags bit i
                
                print_big 'BUYPRO' time 5000 style 2
                play_mission_passed_tune 2
                wait 5000
                clear_prints
                set_camera_behind_player
                restore_camera_jumpcut
                set_player_control $PLAYER_CHAR 1
                set_everyone_ignore_player $PLAYER_CHAR 0
                switch_widescreen 0
            end
        end
    end
end


:CreateCarGen
if
    i == 0
then
    create_car_generator_with_plate iCarGen = parked_car_generator_w_numberplate 587 colors 40 0 force_spawn 1 alarm 0 door_lock 0 min_delay 0 max_delay 10000 plate "DEMUT_X3" at 1503.398 -695.479 94.525 angle 163.232
end
if
    i == 1
then
    create_car_generator_with_plate iCarGen = parked_car_generator_w_numberplate 603 colors 43 77 force_spawn 1 alarm 0 door_lock 0 min_delay 0 max_delay 10000 plate "EHIR_LUV" at 260.149 -1222.770 74.443 angle 189.860
end
if
    i == 2
then
    create_car_generator_with_plate iCarGen = parked_car_generator_w_numberplate 521 colors 44 61 force_spawn 1 alarm 0 door_lock 0 min_delay 0 max_delay 10000 plate "CATCHY69" at -2340.364 2421.037 6.878 angle 98.596
end
set_has_been_owned_for_car_generator iCarGen to_player_owned 1
switch_car_generator iCarGen cars_to_generate_to 101
set_car_generator_no_save iCargen
return


:CleanUpBeforeSave
// Store Saving Flags in this global var (Z-coord of Madd Dogg mission marker in LV)
cset_lvar_float_to_lvar_int f = iSavingFlags
f /= 1000.0
f += 9.8
set_var_float_to_lvar_float $Z_ROYAL_CASINO = f    // Changes the value by 0.07 max if all properties are bought

for i = 0 to 2 step 1
    if
        does_pickup_exist pBuyPropertyPickup[i]
    then
        remove_pickup pBuyPropertyPickup[i]
        pBuyPropertyPickup[i] = -1
    end
    if
        pPropertyBlip[i] > -1
    then
        remove_cleo_blip pPropertyBlip[i]
        pPropertyBlip[i] = -1
    end
end
if
    does_pickup_exist pSavePickup
then
    remove_pickup pSavePickup
    pSavePickup = -1
end
if
    locate_char_distance_to_coordinates $PLAYER_ACTOR pos 2311.139 -1141.286 1054.0 radius 4.0
then
    clear_area 1 at 2312.374 -1138.771 1053.3 radius 1.0
    set_char_coordinates $PLAYER_ACTOR 2312.374 -1138.771 1053.3
    set_char_heading $PLAYER_ACTOR 270.0
end
return_script_event


terminate_this_custom_script