{$CLEO .cs}
{$USE CLEO+}
{$USE ini}
0000: nop
03A4: script_name 'EHIFIXD'

const
    MD_Mansion = 0
    LuxuryHouses = 1
    NewSaveHouses = 2
    CrackDen = 3
    VagosHouse = 4
    PleasureDomes = 5
    Abattoir = 6
    Planning = 7
    SweetsHouse = 8
    RydersHouse = 9
    OGLocsHouse = 10
    BDupsApartment = 11
    BDupsCrackPalace = 12
    BlastinFoolsRecords = 13
    WooziesBettingShop = 14
    FourDragManagement = 15
    FourDragJanitor = 16
    CaligulasManagement = 17
    ShamalInterior = 18
    LibertyCity = 19
    SaintMarksBistro = 20
    Airports = 21
    PetrolCheck = 22
    PetrolDoors = 23
    Denise = 0
    Katie = 1
    Helena = 2
    Michelle = 3
    Millie = 4
    iBuffer = 0@
    A51_Gate_1 = 1@
    A51_Gate_2 = 2@
    A51_Gate_3 = 3@
    A51_Gate_4 = 4@
    AccessFlags = 5@   
    GF_House = 6@
    Required_Doherty_Missions = 7@
    DG_Backdoor = 8@
    DohertyEnex = 9@
    DohertyWeapons = 10@
    AP_Trailer = 11@
    Toreno_House = 12@    
    AP_Icon = 13@
    Toreno_Icon = 14@
    Doherty_Icon = 15@
    Garage_Icon = 16@
    iIconFlags = 17@
    iAmmunEnex = 18@
    iGolfClub = 19@
    Game_Saved = 20@    
    Reset_Flag = 21@   
    Gate = 22@
    iPickups = 23@
    MarkerDistX2 = 24@   
    FDC_Door = 25@
    iSavecar = 26@
    // 28@ - 31@ reserved for temp vars!
end

0@ = 2

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
29@ = 0
30@ = 0

0@ = 1

if
    8AAB: not does_file_exist "cleo\EHI.cs"
jf @ILoveMyGirlfriendJessica

0@ = 0

if
    0AAB: does_file_exist "cleo\EHI.ini"
jf @ILoveMyGirlfriendJessica

AP_Icon = -1
Toreno_Icon = -1
Doherty_Icon = -1
Garage_Icon = -1

0AC7: get_var_pointer 29@ = var iIconFlags

if
    0AAB: does_file_exist "cleo\DatingInteriors.s"
then
    0A92: stream_custom_script "DatingInteriors.s" 29@
end        
    
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "GlazedOffice"
if and
    31@ > 0
    0AAB: does_file_exist "cleo\GlazedOfficeLift.s"
then
    0A92: stream_custom_script "GlazedOfficeLift.s"
    07FB: switch_entry_exit 'E_OFFIC' access 1
else
    07FB: switch_entry_exit 'E_OFFIC' access 0  // LS Glazed Office
end

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "GENERAL" key "MarkersDrawDistX2"    // thanks vladvo
if  
    31@ < 1
gosub_if_false @MarkerDistX2

0AC6: get_label_pointer iBuffer = label @Buffer

0247: request_model 985
0247: request_model 986
0247: request_model 2255
038B: load_all_models_now

// Tiki Theater Bar Sign
0E01: create_object_no_save 2255 at 754.843 1441.558 1105.138 offset false ground false to 31@
0566: set_object_area_visible 31@ to_interior 6
0E28: write_struct_offset iBuffer offset 0x00 size 4 value 31@
0E01: create_object_no_save 2255 at 755.631 1441.557 1105.138 offset false ground false to 31@
0566: set_object_area_visible 31@ to_interior 6
0E28: write_struct_offset iBuffer offset 0x04 size 4 value 31@
0E01: create_object_no_save 2255 at 756.421 1441.556 1105.138 offset false ground false to 31@
0566: set_object_area_visible 31@ to_interior 6
0E28: write_struct_offset iBuffer offset 0x08 size 4 value 31@
0E01: create_object_no_save 2255 at 757.211 1441.555 1105.138 offset false ground false to 31@
0566: set_object_area_visible 31@ to_interior 6
0E28: write_struct_offset iBuffer offset 0x0C size 4 value 31@
0E01: create_object_no_save 2255 at 758.002 1441.554 1105.138 offset false ground false to 31@
0566: set_object_area_visible 31@ to_interior 6
0E28: write_struct_offset iBuffer offset 0x10 size 4 value 31@
08BD: set_local_var_bit_const Gate bit 9

// Area69 Fence Gates
0E01: create_object_no_save 985 at 139.075 1941.32 20.101 offset false ground false to A51_Gate_1
0177: set_object_heading A51_Gate_1 0.0
0E01: create_object_no_save 986 at 131.115 1941.32 20.101 offset false ground false to A51_Gate_2
0177: set_object_heading A51_Gate_2 0.0
0E01: create_object_no_save 985 at 285.641 1817.41 18.428 offset false ground false to A51_Gate_3
0177: set_object_heading A51_Gate_3 270.0
0E01: create_object_no_save 986 at 285.641 1825.37 18.428 offset false ground false to A51_Gate_4
0177: set_object_heading A51_Gate_4 270.0

0249: mark_model_as_no_longer_needed 985
0249: mark_model_as_no_longer_needed 986
0249: mark_model_as_no_longer_needed 2255

// Dinghy boat at Toreno Ranch
014B: create_car_generator 31@ = init_car_generator 473 color -1 -1 force_spawn 1 alarm 0 door_lock 0 min_delay 0 max_delay 10000 at -661.779 876.503 0.0 angle 225.857
0A17: set_has_been_owned_for_car_generator 31@ to_player_owned 1
014C: switch_car_generator 31@ cars_to_generate_to 101
0E02: set_car_generator_no_save 31@

//------------ EHI.ipl enex

07FB: switch_entry_exit 'E_GF5' access 1    // Police Station
07FB: switch_entry_exit 'E_SAYON' access 1 
07FB: switch_entry_exit 'E_BOGST' access 1 
07FB: switch_entry_exit 'E_LVBAN' access 1 
07FB: switch_entry_exit 'E_NBAN' access 1 
07FB: switch_entry_exit 'E_EBAN' access 1

//------------ Enex from vanilla IPLs

// 24/7 interiors
07FB: switch_entry_exit 'X7_11S' access 1       
07FB: switch_entry_exit 'X7_11B' access 1       
07FB: switch_entry_exit 'X7_11C' access 1       
07FB: switch_entry_exit 'X7_11D' access 1       
07FB: switch_entry_exit 'X711S2' access 1       
07FB: switch_entry_exit 'X711S3' access 1

// Misc interiors
07FB: switch_entry_exit 'POLICE2' access 1  // SF Police Department
07FB: switch_entry_exit 'BROTHL1' access 1  // Wooden Brothel
07FB: switch_entry_exit 'BIKESCH' access 1  // Bike School
07FB: switch_entry_exit 'SVLABIG' access 1  // Unused savehouse


//------------ Misc Stuff

// Toreno Save house (SVLABIG)
09B4: set_closest_entry_exit_flag -684.079 939.561 radius 5.0 bitmask 16384 flag 0

// New Save house Mulholland (SVLABIG)
09B4: set_closest_entry_exit_flag 1496.995 -687.892 radius 5.0 bitmask 16384 flag 0

// New Save house Richman (SVLABIG)
09B4: set_closest_entry_exit_flag 251.442 -1220.204 radius 5.0 bitmask 16384 flag 0

// New Save house Bayside (SVVGMD)
09B4: set_closest_entry_exit_flag -2348.577 2423.293 radius 5.0 bitmask 16384 flag 0

// Four Dragon's Apartment (SVVGHO1)
09B4: set_closest_entry_exit_flag 2019.891 988.393 radius 5.0 bitmask 16384 flag 0
07FB: switch_entry_exit 'E_FDCA' access 0

// Bayside Didier Sachs
09B4: set_closest_entry_exit_flag -2492.166 2363.161 radius 5.0 bitmask 16384 flag 0

// Bayside Burger Shot
09B4: set_closest_entry_exit_flag -2477.121 2286.480 radius 5.0 bitmask 16384 flag 0 

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISC" key "LS_Lighthouse"
0EF7: clamp_int 31@ min 0 max 1 store_to 31@
07FB: switch_entry_exit 'E_LSLH' access 31@
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISC" key "LS_OperaHouse"
0EF7: clamp_int 31@ min 0 max 1 store_to 31@
07FB: switch_entry_exit 'E_OPERA' access 31@


//------------ [MISSIONINTERIORS]

gosub @DisableVanillaMissionInteriors

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISSIONINTERIORS" key "CrackDen"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit CrackDen
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISSIONINTERIORS" key "VagosGangHouse"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit VagosHouse
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISSIONINTERIORS" key "PleasureDomes"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit PleasureDomes
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISSIONINTERIORS" key "SindaccoAbattoir"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit Abattoir
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISSIONINTERIORS" key "PlanningDepartment"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit Planning
end


//------------ [CUTSCENEINTERIORS]

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "SweetsHouse"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit SweetsHouse
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "RydersHouse"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit RydersHouse
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "OGLocsHouse"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit OGLocsHouse
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "BDupsApartment"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit BDupsApartment
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "BDupsCrackPalace"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit BDupsCrackPalace
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "BlastinFoolsRecords"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit BlastinFoolsRecords
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "WooziesBettingShop"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit WooziesBettingShop
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "FourDragManagement"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit FourDragManagement
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "FourDragJanitor"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit FourDragJanitor
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "CaligulasManagement"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit CaligulasManagement
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "ShamalInterior"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit ShamalInterior
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "LibertyCity"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit LibertyCity
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "SaintMarksBistro"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit SaintMarksBistro
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "CUTSCENEINTERIORS" key "Airports"
if 31@ > 0
then 08BD: set_local_var_bit_const AccessFlags bit Airports
end

DG_Backdoor = 11416 // Added DG door model ID.
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "DohertyBackdoor"
if
    31@ > 0
then
    DG_Backdoor = 11359 // Vanilla DG backdoor model ID. It gets disabled later and the game will use the movable door instead.   
end

0AF0: DohertyEnex = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "DohertyEnex"
0AF0: AP_Trailer = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "AngelPineTrailer"
0AF0: Toreno_House = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "TorenoSafeHouse"


//------------ Requirements & compatibility check for OnlyRawFeaturesMode

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "GENERAL" key "RequirementsCheck"
if
    31@ > 0
then
    31@ = 0
    28@ = 2
    if
        0AA2: 31@ = load_dynamic_library "RepairGTA.SA.asi"
    jf @OnlyRawFeatures
    0AA3: free_dynamic_library 31@
    28@ = 1
    // CEntryExitPool* at 0x96A7D8
    0A8D: 30@ = read_memory 0x96A7D8 size 4 virtual_protect 0    
    // pool->m_nEntryExits (0x8)
    0D4E: 31@ = read_struct_offset 30@ offset 0x8 size 4
    if
        31@ >= 700
    jf @OnlyRawFeatures
end

28@ = 0
30@ = 0

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "GENERAL" key "OnlyRawFeatures"
if
    31@ > -1
then
    if and
        31@ == 0
        &3 == 43808 // Global var space in vanilla main.scm
    jf @OnlyRawFeatures
    
    // Check if global vars have expected values
    if and
        $INTRO_TOTAL_PASSED_MISSIONS >= 0
        $SWEET_TOTAL_PASSED_MISSIONS >= 0
        $RYDER_TOTAL_PASSED_MISSIONS >= 0
        $OG_LOC_TOTAL_PASSED_MISSIONS >= 0
        $CRASH_LS_TOTAL_PASSED_MISSIONS >= 0
        $LS_FINAL_TOTAL_PASSED_MISSIONS >= 0
        $TRUTH_TOTAL_PASSED_MISSIONS >= 0
        $GARAGE_TOTAL_PASSED_MISSIONS >= 0
    jf @OnlyRawFeatures
    if and
        $INTRO_TOTAL_PASSED_MISSIONS <= 30
        $SWEET_TOTAL_PASSED_MISSIONS <= 30
        $RYDER_TOTAL_PASSED_MISSIONS <= 30
        $OG_LOC_TOTAL_PASSED_MISSIONS <= 30
        $CRASH_LS_TOTAL_PASSED_MISSIONS <= 30
        $LS_FINAL_TOTAL_PASSED_MISSIONS <= 30
        $TRUTH_TOTAL_PASSED_MISSIONS <= 30
        $GARAGE_TOTAL_PASSED_MISSIONS <= 30
    jf @OnlyRawFeatures
    if and
        $WUZIMU_TOTAL_PASSED_MISSIONS >= 0
        $SYNDICATE_TOTAL_PASSED_MISSIONS >= 0
        $TORENO_TOTAL_PASSED_MISSIONS >= 0
        $CASINO_TOTAL_PASSED_MISSIONS >= 0
        $HEIST_TOTAL_PASSED_MISSIONS >= 0
        $MANSION_TOTAL_PASSED_MISSIONS >= 0
        $GROVE_TOTAL_PASSED_MISSIONS >= 0
        $RIOT_TOTAL_PASSED_MISSIONS >= 0
    jf @OnlyRawFeatures
    if and
        $WUZIMU_TOTAL_PASSED_MISSIONS <= 30
        $SYNDICATE_TOTAL_PASSED_MISSIONS <= 30
        $TORENO_TOTAL_PASSED_MISSIONS <= 30
        $CASINO_TOTAL_PASSED_MISSIONS <= 30
        $HEIST_TOTAL_PASSED_MISSIONS <= 30
        $MANSION_TOTAL_PASSED_MISSIONS <= 30
        $GROVE_TOTAL_PASSED_MISSIONS <= 30
        $RIOT_TOTAL_PASSED_MISSIONS <= 30
    jf @OnlyRawFeatures
    if and
        $GIRL_DESIRED_PROGRESS_TO_INVITE[0] >= -100000
        $GIRL_DESIRED_PROGRESS_TO_INVITE[1] >= -100000 
        $GIRL_DESIRED_PROGRESS_TO_INVITE[2] >= -100000 
        $GIRL_DESIRED_PROGRESS_TO_INVITE[4] >= -100000
        $GIRL_DESIRED_PROGRESS_TO_INVITE[0] <= 100000
        $GIRL_DESIRED_PROGRESS_TO_INVITE[1] <= 100000 
        $GIRL_DESIRED_PROGRESS_TO_INVITE[2] <= 100000 
        $GIRL_DESIRED_PROGRESS_TO_INVITE[4] <= 100000
    jf @OnlyRawFeatures
    if and
        $807 >= -2
        $808 >= -2
        $809 >= -2
        $810 >= -2
        $807 <= 222222222   // My game crashed if doing something with >= 230000000
        $808 <= 222222222
        $809 <= 222222222
        $810 <= 222222222
    jf @OnlyRawFeatures
    if and
        $811 >= -2
        $812 >= -2
        $813 >= -2
        $814 >= -2
        $811 <= 222222222
        $812 <= 222222222
        $813 <= 222222222
        $814 <= 222222222
    jf @OnlyRawFeatures
    if and
        $2873 >= -2
        $2874 >= -2
        $2875 >= -2
        $2876 >= -2
        $2873 <= 222222222
        $2874 <= 222222222
        $2875 <= 222222222
        $2876 <= 222222222
    jf @OnlyRawFeatures
    if and
        $2662 >= -2
        $2663 >= -2
        $2664 >= -2
        $5290 >= -2
        $2662 <= 222222222
        $2663 <= 222222222
        $2664 <= 222222222
        $5290 <= 222222222
    jf @OnlyRawFeatures
    if and
        $2670 >= -2  
        $OBJECT_M_A51_BLASTDOORR >= -2
        $OBJECT_M_A51_BLASTDOORL >= -2
        $OBJECT_M_A51_VENTCOVERB >= -2
        $2670 <= 222222222
        $OBJECT_M_A51_BLASTDOORR <= 222222222
        $OBJECT_M_A51_BLASTDOORL <= 222222222
        $OBJECT_M_A51_VENTCOVERB <= 222222222
    jf @OnlyRawFeatures  
    if and
        $2703 >= -2
        $2703 <= 222222222
        $X_SF_GARAGE >= -100000.0
        $Y_SF_GARAGE >= -100000.0 
        $Z_SF_GARAGE >= -100000.0
        $X_SF_GARAGE <= 100000.0
        $Y_SF_GARAGE <= 100000.0 
        $Z_SF_GARAGE <= 100000.0
    jf @OnlyRawFeatures
    if and
        $X_THE_FOUR_DRAGONS_CASINO >= -100000.0
        $Y_THE_FOUR_DRAGONS_CASINO >= -100000.0 
        $Z_THE_FOUR_DRAGONS_CASINO >= -100000.0
        $X_THE_FOUR_DRAGONS_CASINO <= 100000.0
        $Y_THE_FOUR_DRAGONS_CASINO <= 100000.0 
        $Z_THE_FOUR_DRAGONS_CASINO <= 100000.0
    jf @OnlyRawFeatures
    if and
        $X_ROYAL_CASINO >= -100000.0
        $Y_ROYAL_CASINO >= -100000.0 
        $Z_ROYAL_CASINO >= -100000.0
        $X_ROYAL_CASINO <= 100000.0
        $Y_ROYAL_CASINO <= 100000.0 
        $Z_ROYAL_CASINO <= 100000.0
    jf @OnlyRawFeatures
    if and
        $X_ANGEL_PINE >= -100000.0
        $Y_ANGEL_PINE >= -100000.0 
        $Z_ANGEL_PINE >= -100000.0
        $X_ANGEL_PINE <= 100000.0
        $Y_ANGEL_PINE <= 100000.0 
        $Z_ANGEL_PINE <= 100000.0
    jf @OnlyRawFeatures
    if and
        $436 >= -2
        $484 >= -2
        $582 >= -2
        $584 >= -2
        $436 <= 222222222
        $484 <= 222222222
        $582 <= 222222222
        $584 <= 222222222
    jf @OnlyRawFeatures
    if and
        $MARKER_SWEET_HOUSE >= -2
        $MARKER_TRIADS_SF_GARAGE >= -2
        $MARKER_SWEET_HOUSE <= 222222222
        $MARKER_TRIADS_SF_GARAGE <= 222222222
    jf @OnlyRawFeatures
    if and
        $1730 >= -2
        $1732 >= -2
        $1733 >= -2
        $1730 <= 222222222
        $1732 <= 222222222
        $1733 <= 222222222
        $TOTAL_AVAILABLE_SAVE_PICKUPS >= -2
        $TOTAL_AVAILABLE_SAVE_PICKUPS <= 1000
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[13] >= -100000.0  
        $SAVE_PICKUPS_Y[13] >= -100000.0 
        $SAVE_PICKUPS_Z[13] >= -100000.0
        $SAVE_POINTS_X[13] >= -100000.0
        $SAVE_POINTS_Y[13] >= -100000.0  
        $SAVE_POINTS_Z[13] >= -100000.0
        $SAVE_POINTS_ANGLE[13] >= -360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[13] <= 100000.0  
        $SAVE_PICKUPS_Y[13] <= 100000.0 
        $SAVE_PICKUPS_Z[13] <= 100000.0
        $SAVE_POINTS_X[13] <= 100000.0
        $SAVE_POINTS_Y[13] <= 100000.0  
        $SAVE_POINTS_Z[13] <= 100000.0
        $SAVE_POINTS_ANGLE[13] <= 360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[15] >= -100000.0  
        $SAVE_PICKUPS_Y[15] >= -100000.0 
        $SAVE_PICKUPS_Z[15] >= -100000.0
        $SAVE_POINTS_X[15] >= -100000.0
        $SAVE_POINTS_Y[15] >= -100000.0  
        $SAVE_POINTS_Z[15] >= -100000.0
        $SAVE_POINTS_ANGLE[15] >= -360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[15] <= 100000.0  
        $SAVE_PICKUPS_Y[15] <= 100000.0 
        $SAVE_PICKUPS_Z[15] <= 100000.0
        $SAVE_POINTS_X[15] <= 100000.0
        $SAVE_POINTS_Y[15] <= 100000.0  
        $SAVE_POINTS_Z[15] <= 100000.0
        $SAVE_POINTS_ANGLE[15] <= 360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[16] >= -100000.0  
        $SAVE_PICKUPS_Y[16] >= -100000.0 
        $SAVE_PICKUPS_Z[16] >= -100000.0
        $SAVE_POINTS_X[16] >= -100000.0
        $SAVE_POINTS_Y[16] >= -100000.0  
        $SAVE_POINTS_Z[16] >= -100000.0
        $SAVE_POINTS_ANGLE[16] >= -360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[16] <= 100000.0  
        $SAVE_PICKUPS_Y[16] <= 100000.0 
        $SAVE_PICKUPS_Z[16] <= 100000.0
        $SAVE_POINTS_X[16] <= 100000.0
        $SAVE_POINTS_Y[16] <= 100000.0  
        $SAVE_POINTS_Z[16] <= 100000.0
        $SAVE_POINTS_ANGLE[16] <= 360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[17] >= -100000.0  
        $SAVE_PICKUPS_Y[17] >= -100000.0 
        $SAVE_PICKUPS_Z[17] >= -100000.0
        $SAVE_POINTS_X[17] >= -100000.0
        $SAVE_POINTS_Y[17] >= -100000.0  
        $SAVE_POINTS_Z[17] >= -100000.0
        $SAVE_POINTS_ANGLE[17] >= -360.0
    jf @OnlyRawFeatures
    if and
        $SAVE_PICKUPS_X[17] <= 100000.0  
        $SAVE_PICKUPS_Y[17] <= 100000.0 
        $SAVE_PICKUPS_Z[17] <= 100000.0
        $SAVE_POINTS_X[17] <= 100000.0
        $SAVE_POINTS_Y[17] <= 100000.0  
        $SAVE_POINTS_Z[17] <= 100000.0
        $SAVE_POINTS_ANGLE[17] <= 360.0
    jf @OnlyRawFeatures
end

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "2FloorAmmunation"
0AC7: get_var_pointer 30@ = var iAmmunEnex
if
    0AAB: does_file_exist "cleo\EHI_Enex_Tweaks.s"
then
    0A92: stream_custom_script "EHI_Enex_Tweaks.s" 29@ 30@ 31@
end

29@ = 0
0AF0: 29@ = read_int_from_ini_file "cleo\EHI.ini" section "PROPERTIES" key "Mulholland"
30@ = 0
0AF0: 30@ = read_int_from_ini_file "cleo\EHI.ini" section "PROPERTIES" key "Richman"
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "PROPERTIES" key "Bayside"
if or
    29@ > 0
    30@ > 0
    31@ > 0
then
    if
        0AAB: does_file_exist "cleo\NewSaveHouses.s"
    then
        0A92: stream_custom_script "NewSaveHouses.s" 29@ 30@ 31@
        if or
            29@ > 0
            30@ > 0
        then
            08BD: set_local_var_bit_const AccessFlags bit NewSaveHouses
        end
    end    
end

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "MaddDoggMansion"
if
    31@ > 0
then
    08BD: set_local_var_bit_const AccessFlags bit MD_Mansion
end
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "MISC" key "LS_LuxuryHouses"
if
    31@ > 0
then
    08BD: set_local_var_bit_const AccessFlags bit LuxuryHouses
end    

0AF0: DohertyWeapons = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "DohertyWeapons"

Required_Doherty_Missions = 100
31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "DohertyTuning"
if
    31@ > 0
then
    if
        31@ == 1
    then
        Required_Doherty_Missions = -1
    else
        if
            31@ == 2
        then
            Required_Doherty_Missions = 0
        else
            Required_Doherty_Missions = 3
        end
    end    
end

if and
    DohertyEnex < 1
    DohertyWeapons < 1
    Required_Doherty_Missions == 100
gosub_if_false @GarageCamFix

iGolfClub = -1
FDC_Door = -1
iSavecar = -1
0E28: write_struct_offset iBuffer offset 0x1C size 4 value -1
0E28: write_struct_offset iBuffer offset 0x20 size 4 value -1
0E28: write_struct_offset iBuffer offset 0x24 size 4 value -1

0ED1: set_script_event_save_confirmation 1 label @RestoreBeforeSave var_slot Game_Saved    // Restores global vars and pickups when saving

31@ = 0
0AF0: 31@ = read_int_from_ini_file "cleo\EHI.ini" section "SPECIALINTERIORS" key "FourDragonsFlat"
if
    31@ > 0
then
    // Start of a dynamic system that disables and enables door and enex if the player uses SVVGHO1 interior somewhere else than "inside Four Dragons"
    0247: request_model 1491
    038B: load_all_models_now
    0E01: create_object_no_save 1491 at 2213.131 -1078.848 1049.484 offset false ground false to FDC_Door
    0566: set_object_area_visible FDC_Door to_interior 1
    0177: set_object_heading FDC_Door 0.0
    0905: lock_door FDC_Door lock 1
    0249: mark_model_as_no_longer_needed 1491
    094C: get_position_of_entry_exit_char_used $PLAYER_ACTOR currently_used_EnEx_3D_coord_to 28@ 29@ 30@ number_to 31@
    if and
        28@ > 2019.8
        28@ < 2019.9
    then
        28@ = 2019.891
    end
    if and
        28@ > 1996.0
        28@ < 1998.2
    then
        28@ = 1998.100
    end
    if and
        $SAVE_PICKUPS_Z[17] > 0.0
        $SAVE_PICKUPS_Z[17] < 0.2
    then
        $SAVE_PICKUPS_Z[17] = 0.1
    end        
    if or
        28@ == 2019.891
        28@ == 1998.100
        $SAVE_PICKUPS_Z[17] == 0.1
    then
        07FB: switch_entry_exit 'E_FDCA' access 1
        08BD: set_local_var_bit_const Gate bit 7
    else        
        0750: set_object_visible FDC_Door 0
    end
    
    :ResetFourDragons
    $X_THE_FOUR_DRAGONS_CASINO = 1959.104
    $Y_THE_FOUR_DRAGONS_CASINO = 972.942    
    $SAVE_PICKUPS_Z[17] = 0.1
    gosub @FourDragonsSave
end

:ResetAfterSave
Game_Saved = -1

// "Madd Dogg" mission marker position
// I found that the marker position is too close to enex for casino floor interior. So, I moved it a bit.
$X_ROYAL_CASINO = 2086.233
$Y_ROYAL_CASINO = 1447.532
gosub @MaddDoggMissionMarker 

if
    DohertyEnex > 0
then
    if
        DohertyEnex < 3
    then
        // Doherty Garage mission marker
        $Y_SF_GARAGE_3 = 144.2279
        $Y_SF_GARAGE = 144.2279
        gosub @DohertyMissionMarker
    end    
    if
        DohertyEnex > 1
    then
        $SAVE_PICKUPS_X[15] = -2045.517
        $SAVE_PICKUPS_Y[15] = 153.479
        $SAVE_PICKUPS_Z[15] = 28.543
        $SAVE_POINTS_X[15] = -2043.823
        $SAVE_POINTS_Y[15] = 155.998
        $SAVE_POINTS_Z[15] = 27.850
        $SAVE_POINTS_ANGLE[15] = 320.0
        gosub @DohertySavePickup
    end
end

if
    AP_Trailer > 1
then
    $X_ANGEL_PINE = -2047.43
    gosub @AngelPineMissionMarker
    
    if
        AP_Trailer > 2
    then
        // Trailer from LivelyTrailers Mod          
        $SAVE_PICKUPS_X[13] = -2046.2428  
        $SAVE_PICKUPS_Y[13] = -2518.7446 
        $SAVE_PICKUPS_Z[13] = 30.8703
        $SAVE_POINTS_X[13] = -2044.6578
        $SAVE_POINTS_Y[13] = -2520.0197  
        $SAVE_POINTS_Z[13] = 30.0703 
        $SAVE_POINTS_ANGLE[13] = 228.3602
    else
        // Angel Pine Lodge
        $SAVE_PICKUPS_X[13] = 0.4068  
        $SAVE_PICKUPS_Y[13] = 3.969 
        $SAVE_PICKUPS_Z[13] = 999.2297
        $SAVE_POINTS_X[13] = -0.2521 
        $SAVE_POINTS_Y[13] = 0.816  
        $SAVE_POINTS_Z[13] = 998.4297 
        $SAVE_POINTS_ANGLE[13] = 180.0
    end
    gosub @AngelPineSavePickup
end

if
    Toreno_House > 1
then
    $SAVE_PICKUPS_Z[16] = 0.1
    if
        88B7: not is_local_var_bit_set_const AccessFlags bit NewSaveHouses
    then
        $SAVE_PICKUPS_X[16] = 2311.139  
        $SAVE_PICKUPS_Y[16] = -1141.286  
        $SAVE_PICKUPS_Z[16] = 1054.0
        $SAVE_POINTS_X[16] = 2312.374 
        $SAVE_POINTS_Y[16] =  -1138.771 
        $SAVE_POINTS_Z[16] = 1053.3
        $SAVE_POINTS_ANGLE[16] = 270.0
    end      
    gosub @TorenoSavePickup
end

if
    iAmmunEnex > 0
then
    0E28: write_struct_offset iAmmunEnex offset 0x18 size 4 value 1.0
end

gosub @BaysideZone

28@ = 0
Reset_Flag = 0
GF_House = 0
iPickups = 0

31@ = 0
gosub @SetInteriors


//------------ Mainloop for normal mode 

while true
    wait 0
    if
        0256: is_player_playing $PLAYER_CHAR
    jf continue
    
    if
        Game_Saved > -1
    then
        wait 0
        wait 0
        TIMERB = 0
        if
            FDC_Door <> -1
        jf @ResetAfterSave
        jump @ResetFourDragons
    end
    
    if and
        TIMERB > 3000
        0E1D: is_on_mission
    then
        gosub @PetrolStation
        if
            Reset_Flag <> 1
        jf @CheckThisAlways
        if
            Reset_Flag == 2
        then
            wait 0
            wait 0
            wait 2000    
            jump @ResetAccess
        end   
        Reset_Flag = 1
    else
        if
            Reset_Flag <> 2
        jf @CheckThisAlways
        if
            Reset_Flag == 1
        then
            wait 0
            wait 0
            wait 2000
            
            if and
                0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR pos -2052.625 150.468 29.937 radius 80.0
                80A4: not is_char_in_area_3d $PLAYER_ACTOR sphere 0 in_cube_cornerA -2057.279 150.688 25.546 cornerB -2038.957 182.581 36.546
            then
                31@ = 0
                
                while 0EA8: get_any_car_no_save_recursive 31@ progress_to 31@ car_to 30@
                    if
                        01AC: is_car_stopped_in_area_3d 30@ sphere 0 in_cube_cornerA -2057.279 150.688 25.546 cornerB -2038.957 182.581 36.546
                    then
                        0840: set_vehicle_area_visible 30@ to_interior 0
                        020A: lock_car_doors 30@ 1
                        03AB: set_car_strong 30@ strong 0
                        053F: set_can_burst_car_tyres 30@ tires_vulnerability 1
                        09B0: set_vehicle_is_considered_by_player 30@ accessible_for_player_using_controller 1
                    end
                end
            end
            
            gosub @DisableVanillaMissionInteriors
            
            :ResetAccess
            Reset_Flag = 0
            GF_House = 0
            Gate = 0
            31@ = 0
            gosub @SetInteriors
            
            repeat
                wait 0
                if or
                    89E7: not is_player_control_on $PLAYER_CHAR
                    0EB7: is_on_scripted_cutscene
                then
                    TIMERA = 0
                end
            until TIMERA > 200
            continue
        end
        Reset_Flag = 2
    end
    
    gosub @OpenInteriors
    
//------------ Weapon Pickups

    :CheckThisAlways
    if
        88B7: not is_local_var_bit_set_const iPickups bit 0
    then
        if or
            DohertyWeapons < 1
            89D1: not does_pickup_exist $807            
        jf @ResetAccess
    end
    if
        88B7: not is_local_var_bit_set_const iPickups bit 1       
    then
        if or
            FDC_Door == -1 
            89D1: not does_pickup_exist $811
        jf @ResetAccess
    end
    
//------------ Girlfriends
  
    if
        $391 == 100663296   //She is "at home"
    then
        if
            GF_House > 0
        then
            07FB: switch_entry_exit 'E_GF1' access 0
            07FB: switch_entry_exit 'E_GF2' access 0
            07FB: switch_entry_exit 'E_GF3' access 0 
            07FB: switch_entry_exit 'E_GF4' access 0
            07FB: switch_entry_exit 'E_GF6' access 0
            GF_House = 0
        end
    else
            
//------------ Denise's house

        0652: 31@ = get_int_stat 252
        if and
            31@ >= 15
            002C: $GIRL_PROGRESS[0] >= $GIRL_DESIRED_PROGRESS_TO_INVITE[0]  // (int)
        then
            if
                88B7: not is_local_var_bit_set_const GF_House bit Denise
            then
                07FB: switch_entry_exit 'E_GF1' access 1 
                08BD: set_local_var_bit_const GF_House bit Denise
            end
        else
            if
                08B7: is_local_var_bit_set_const GF_House bit Denise
            then
                07FB: switch_entry_exit 'E_GF1' access 0 
                08C3: clear_local_var_bit_const GF_House bit Denise
            end
        end

//------------ Katie's house  

        0652: 31@ = get_int_stat 256
        if and
            31@ >= 15
            002C: $GIRL_PROGRESS[4] >= $GIRL_DESIRED_PROGRESS_TO_INVITE[4]  // (int)
        then
            if
                88B7: not is_local_var_bit_set_const GF_House bit Katie
            then
                07FB: switch_entry_exit 'E_GF2' access 1 
                08BD: set_local_var_bit_const GF_House bit Katie
            end
        else
            if
                08B7: is_local_var_bit_set_const GF_House bit Katie
            then
                07FB: switch_entry_exit 'E_GF2' access 0 
                08C3: clear_local_var_bit_const GF_House bit Katie
            end
        end

//------------ Helena's house  

        0652: 31@ = get_int_stat 254
        if and
            31@ >= 15
            002C: $GIRL_PROGRESS[2] >= $GIRL_DESIRED_PROGRESS_TO_INVITE[2]  // (int)
        then
            if
                88B7: not is_local_var_bit_set_const GF_House bit Helena
            then
                07FB: switch_entry_exit 'E_GF3' access 1 
                08BD: set_local_var_bit_const GF_House bit Helena
            end
        else
            if
                08B7: is_local_var_bit_set_const GF_House bit Helena
            then
                07FB: switch_entry_exit 'E_GF3' access 0 
                08C3: clear_local_var_bit_const GF_House bit Helena
            end
        end

//------------ Michelle's house

        0652: 31@ = get_int_stat 253
        if and
            31@ >= 15
            002C: $GIRL_PROGRESS[1] >= $GIRL_DESIRED_PROGRESS_TO_INVITE[1]  // (int)
        then
            if
                88B7: not is_local_var_bit_set_const GF_House bit Michelle
            then
                07FB: switch_entry_exit 'E_GF4' access 1 
                08BD: set_local_var_bit_const GF_House bit Michelle
            end
        else
            if
                08B7: is_local_var_bit_set_const GF_House bit Michelle
            then
                07FB: switch_entry_exit 'E_GF4' access 0 
                08C3: clear_local_var_bit_const GF_House bit Michelle
            end
        end   

//------------ Millie's house  

        0652: 31@ = get_int_stat 257
        if and
            31@ > 0
            $PICKUP_KEYCARD_COLLECTED == 1
            Reset_Flag == 2
        then
            if
                88B7: not is_local_var_bit_set_const GF_House bit Millie
            then
                077E: get_area_visible 31@
                if or
                    31@ == 0
                    $HEIST_TOTAL_PASSED_MISSIONS >= 6                    
                then
                    07FB: switch_entry_exit 'E_GF6' access 1 
                    08BD: set_local_var_bit_const GF_House bit Millie
                end
            end
        else
            if
                08B7: is_local_var_bit_set_const GF_House bit Millie
            then
                07FB: switch_entry_exit 'E_GF6' access 0 
                08C3: clear_local_var_bit_const GF_House bit Millie
            end
        end
    end
    
//------------ Wooden Brothel Lap Dance
    
    if
        0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 954.569 -5.269 1001.171 radius 60.0
    then        
        while 0102: locate_stopped_char_on_foot_3d $PLAYER_ACTOR in_sphere 954.569 -5.269 1001.171 radius 1.0 1.0 1.0 sphere 1
            wait 0
            if
                0256: is_player_playing $PLAYER_CHAR
            jf break
            if
                28@ <> 4
            jf continue
            if
                810A: not is_score_greater $PLAYER_CHAR > 99
            then
                if
                    28@ <> 2
                jf continue
                0512: print_help_forever 'STR_03'
                28@ = 2
                continue
            end
            if
                28@ <> 1
            then    
                0512: print_help_forever 'STR_01'
                28@ = 1
            end
            if
                00E1: is_button_pressed 0 pressed_key 15
            jf continue
            
            0AAA: 31@ = get_script_struct_named 'STRIP'
            if and
                31@ <> 0
                31@ <> -1
            then
                0D2E: set_script_var 31@ index 4 value 0
            end
            094C: get_position_of_entry_exit_char_used $PLAYER_ACTOR currently_used_EnEx_3D_coord_to 29@ 30@ 31@ number_to 32@
            016A: do_fade 0 time 500
            
            while true
                wait 0
                if
                    0256: is_player_playing $PLAYER_CHAR
                jf break
                if
                    28@ == 1
                then
                    if
                        816B: not fading
                    jf continue
                    01B4: set_player_control $PLAYER_CHAR 0
                    00A1: set_char_coordinates $PLAYER_ACTOR 1207.59 -40.646 999.9531
                    08AD: set_char_has_used_entry_exit $PLAYER_ACTOR to_enex_marker_at 693.632 1966.4 radius 5.0
                    28@ = 2
                end
                if
                    28@ == 2
                then
                    0A8C: write_memory 0xB73476 size 2 value 0xFF virtual_protect 0
                    0A8C: write_memory 0xB734EE size 2 value 0xFF virtual_protect 0                           
                    if
                        0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 1207.366 -43.595 998.925 radius 2.0
                    jf continue
                    28@ = 3
                end
                if
                    28@ == 3
                then
                    if
                        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR 1207.366 -43.595 998.925 radius 2.0
                    jf continue
                    00A1: set_char_coordinates $PLAYER_ACTOR 954.569 -5.269 1000.171
                    0173: set_char_heading $PLAYER_ACTOR 180.0
                    08AD: set_char_has_used_entry_exit $PLAYER_ACTOR to_enex_marker_at 29@ 30@ radius 5.0
                    28@ = 4
                end
                if
                    28@ == 4
                then
                    if
                        09E7: is_player_control_on $PLAYER_CHAR
                    jf continue
                    0AAA: 31@ = get_script_struct_named 'STRIP'
                    if and
                        31@ <> 0
                        31@ <> -1
                    then
                        0D2E: set_script_var 31@ index 4 value 7
                    end
                    break
                end                   
            end   
        end
        if
            28@ <> 0
        jf continue    
        03E6: clear_help
        28@ = 0
        continue
    end
    
//------------ Tiki Theater bar sign

    if
        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR 745.001 1440.505 1102.726 radius 30.0    // Fanny Batter's
    jf @continue500
    if
        0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 2353.202 -1463.453 23.000 radius 80.0
    then
        if
            88B7: not is_local_var_bit_set_const Gate bit 9
        jf @continue500
        29@ = 1
        gosub @SetBarSign
        08BD: set_local_var_bit_const Gate bit 9
        jump @continue500
    end
    if
        08B7: is_local_var_bit_set_const Gate bit 9
    then
        29@ = 0
        gosub @SetBarSign
        08C3: clear_local_var_bit_const Gate bit 9
    end
        
//------------ Four Dragon's Apartment Door

    if
        03CA: does_object_exist FDC_Door
    then
        if
            8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR 256.783 -41.574 1002.023 radius 10.0    // Wardrobe
        jf @continue500
        if 
            0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 2213.888 -1078.172 1049.484 radius 30.0     // Apartment Room
        then
            if and
                08B7: is_local_var_bit_set_const Gate bit 7
                88B7: not is_local_var_bit_set_const Gate bit 8
            jf @continue500
            08AD: set_char_has_used_entry_exit $PLAYER_ACTOR to_enex_marker_at 2019.891 988.393 radius 5.0
            08BD: set_local_var_bit_const Gate bit 8
            jump @continue500
        end
        if or
            0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 2019.891 988.393 9.820 radius 80.0      // Four Dragon's outside area
            0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 1964.935 1019.851 1018.833 radius 80.0  // Interior area
        then      
            if and
                88B7: not is_local_var_bit_set_const Gate bit 7
                $TOTAL_AVAILABLE_SAVE_PICKUPS >= 18
            jf @continue500
            07FB: switch_entry_exit 'E_FDCA' access 1
            0750: set_object_visible FDC_Door 1
            08BD: set_local_var_bit_const Gate bit 7
            jump @continue500
        end
        if
            08B7: is_local_var_bit_set_const Gate bit 7
        then
            07FB: switch_entry_exit 'E_FDCA' access 0
            0750: set_object_visible FDC_Door 0
            08C3: clear_local_var_bit_const Gate bit 7
            08C3: clear_local_var_bit_const Gate bit 8
        end
    end
    
//------------ Preserve Sanchez / Washington when entering Angel Pine Trailer / Toreno Ranch
   
    if or
        0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR -2045.232 -2522.631 30.067 radius 15.0
        0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR -684.079 939.561 12.633 radius 30.0
    then
        if and
            856E: not does_vehicle_exist iSavecar
            8449: not is_char_sitting_in_any_car $PLAYER_ACTOR
        jf @continue500
        
        31@ = 0           
        while 0EA8: get_any_car_no_save_recursive 31@ progress_to 31@ car_to 30@
            if or
                0137: is_car_model 30@ == 468   // Sanchez
                0137: is_car_model 30@ == 421   // Washington
            then
                if and
                    01C1: is_car_stopped 30@
                    8ABE: not is_car_engine_on 30@
                then
                    if or
                        0EEB: locate_car_distance_to_coordinates 30@ -2048.750 -2521.250 30.288 radius 2.0
                        0EEB: locate_car_distance_to_coordinates 30@ -680.5 948.125 12.032 radius 2.0
                    then
                        0085: set_lvar_int_to_lvar_int iSavecar = 30@ 
                        0E09: mark_car_as_needed iSavecar
                        jump @continue500
                    end
                end
            end
        end
        jump @continue500
    end
    if
        iSavecar <> -1
    then
        if and
            8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR $SAVE_POINTS_X[13] $SAVE_POINTS_Y[13] $SAVE_POINTS_Z[13] radius 15.0
            8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR 2324.425 -1141.827 1050.983 radius 30.0
        jf @continue500    
        if
            056E: does_vehicle_exist iSavecar 
        then
            01C3: mark_car_as_no_longer_needed iSavecar
        end
        iSavecar = -1
        jump @continue500
    end
    
//------------ Area69 Gate 
    
    if
        $TORENO_TOTAL_PASSED_MISSIONS >= 8     
    then 
        if
            0583: is_player_in_info_zone $PLAYER_CHAR in_zone 'REST'
        then
            if and
                03CA: does_object_exist A51_Gate_1
                03CA: does_object_exist A51_Gate_2
            then
                if
                    0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 135.095 1941.32 20.101 radius 11.0
                then
                    30@ = 145.475
                    31@ = 124.715
                else
                    30@ = 139.075
                    31@ = 131.115
                end
                if and
                    034E: slide_object A51_Gate_1 to 30@ 1941.32 20.101 speed 0.08 0.0 0.0 collision_check 0 
                    034E: slide_object A51_Gate_2 to 31@ 1941.32 20.101 speed 0.08 0.0 0.0 collision_check 0
                then
                    if
                        08B7: is_local_var_bit_set_const Gate bit 3
                    then
                        097A: report_mission_audio_event_at_position 135.095 1941.32 20.101 event 1101
                        08C3: clear_local_var_bit_const Gate bit 3
                    end
                else
                    if
                        88B7: not is_local_var_bit_set_const Gate bit 3
                    then
                        097A: report_mission_audio_event_at_position 135.095 1941.32 20.101 event 1101
                        08BD: set_local_var_bit_const Gate bit 3
                    end
                end 
            end
            if and
                03CA: does_object_exist A51_Gate_3
                03CA: does_object_exist A51_Gate_4
            then 
                if
                    0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR 285.641 1821.39 18.428 radius 11.0
                then
                    30@ = 1811.81
                    31@ = 1830.97
                else
                    30@ = 1817.41
                    31@ = 1825.37
                end
                if and
                    034E: slide_object A51_Gate_3 to 285.641 30@ 18.428 speed 0.0 0.08 0.0 collision_check 0 
                    034E: slide_object A51_Gate_4 to 285.641 31@ 18.428 speed 0.0 0.08 0.0 collision_check 0
                then
                    if
                        08B7: is_local_var_bit_set_const Gate bit 4
                    then
                        097A: report_mission_audio_event_at_position 285.641 1821.39 18.428 event 1101
                        08C3: clear_local_var_bit_const Gate bit 4
                    end
                else
                    if
                        88B7: not is_local_var_bit_set_const Gate bit 4
                    then
                        097A: report_mission_audio_event_at_position 285.641 1821.39 18.428 event 1101
                        08BD: set_local_var_bit_const Gate bit 4
                    end
                end 
            end
            if
                Reset_Flag == 2
            jf continue
            if and
                03CA: does_object_exist $OBJECT_M_A51_BLASTDOORR
                03CA: does_object_exist $OBJECT_M_A51_BLASTDOORL
            then
                if
                    00FE: locate_char_any_means_3d $PLAYER_ACTOR sphere 0 in_sphere 214.034 1874.667 13.140 radius 9.0 9.0 2.0           
                then
                    30@ = 219.941
                    31@ = 207.842
                else
                    30@ = 215.941
                    31@ = 211.842
                end 
                if and
                    034E: slide_object $OBJECT_M_A51_BLASTDOORR to 30@ 1874.571 13.903 speed 0.03 0.0 0.0 collision_check 0
                    034E: slide_object $OBJECT_M_A51_BLASTDOORL to 31@ 1874.571 13.903 speed 0.03 0.0 0.0 collision_check 0
                then
                    if
                        08B7: is_local_var_bit_set_const Gate bit 5
                    then
                        097A: report_mission_audio_event_at_position 213.85 1874.68 14.12 event 1166
                        08C3: clear_local_var_bit_const Gate bit 5
                    end
                else
                    if
                        88B7: not is_local_var_bit_set_const Gate bit 5
                    then
                        097A: report_mission_audio_event_at_position 213.85 1874.68 14.12 event 1165
                        08BD: set_local_var_bit_const Gate bit 5
                    end
                end
            end
            if
                03CA: does_object_exist $2670
            then  
                if
                    01A6: is_char_in_area_on_foot_3d $PLAYER_ACTOR sphere 0 in_cube_cornerA 272.577 1887.670 17.3 cornerB 264.717 1880.248 0.0                
                then
                    034E: slide_object $2670 to 276.14 1884.06 15.924 speed 0.08 0.0 0.0 collision_check 0
                else
                    034E: slide_object $2670 to 268.664 1884.06 15.925 speed 0.08 0.0 0.0 collision_check 0
                end
            end
            continue
        end
        
//------------ Aircraft Carrier Door (SF)
 
        if
            0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR pos -1465.797 501.289 1.0 radius 80.0
        then
            if and
                $MANSION_TOTAL_PASSED_MISSIONS >= 2                
                03CA: does_object_exist $5290 
            jf @continue500
            if
                0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR pos -1465.797 501.289 1.0 radius 24.0
            then
                30@ = -1468.492
                31@ = 11.109
            else
                30@ = -1465.797
                31@ = 1.145
            end
            if
                034E: slide_object $5290 to 30@ 501.289 31@ speed 0.014 0.0 0.05 collision_check 0
            then
                if
                    08B7: is_local_var_bit_set_const Gate bit 6
                then
                    097A: report_mission_audio_event_at_position 30@ 501.289 31@ event 1166
                    08C3: clear_local_var_bit_const Gate bit 6
                end
            else
                if
                    88B7: not is_local_var_bit_set_const Gate bit 6
                then
                    097A: report_mission_audio_event_at_position 30@ 501.289 31@ event 1165
                    08BD: set_local_var_bit_const Gate bit 6
                end
            end
            continue
        end
    end
    
//------------ Doherty Garage (SF)

    if or
        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR pos -2052.625 150.468 29.937 radius 80.0
        Reset_Flag == 1
    then
        gosub @ResetGarageDoor
    else
        gosub @ChooseGarageDoor
        if
            $GARAGE_TOTAL_PASSED_MISSIONS < 1
        jf @CheckDoherty
        if and
            DohertyWeapons > 0
            DohertyEnex <> 1
            DohertyEnex <> 2
            09D1: does_pickup_exist $807
        then
            :CheckDoherty            
            0AAA: 31@ = get_script_struct_named 'TFEN'
            if and
                31@ <> 0
                31@ <> -1
            then
                0ABA: terminate_all_custom_scripts_with_this_name "TFEN"
            else
                0AAA: 30@ = get_script_struct_named 'DohGrge'   // Doherty Garage by vladvo
                if and
                    30@ <> 0
                    30@ <> -1
                then
                    gosub @GarageSound
                    jump @continue500
                end
                if or
                    DohertyEnex > 0
                    DohertyWeapons > 0
                    001E: is_int_var_greater_than_int_lvar $STEAL_TOTAL_PASSED_MISSIONS > Required_Doherty_Missions
                then
                    30@ = 7.0                                      
            		if and
            			0449: is_char_sitting_in_any_car $PLAYER_ACTOR
            			001E: is_int_var_greater_than_int_lvar $STEAL_TOTAL_PASSED_MISSIONS > Required_Doherty_Missions
            		then
            		    30@ = 11.0
            			03C0: 29@ = store_car_char_is_in_no_save $PLAYER_ACTOR
            			if and
            				8969: not is_big_vehicle 29@
                			84A7: not is_char_in_any_boat $PLAYER_ACTOR
                			84C8: not is_char_in_flying_vehicle $PLAYER_ACTOR
                			89AE: not is_char_in_any_train $PLAYER_ACTOR
                            8241: not is_player_in_remote_mode $PLAYER_CHAR
                            8A32: not is_char_attached_to_any_car $PLAYER_ACTOR
            			then
                            if and
                                03EE: can_player_start_mission $PLAYER_CHAR
                                0103: locate_stopped_char_in_car_3d $PLAYER_ACTOR in_sphere -2052.625 157.199 28.551 radius 4.0 4.0 4.0 sphere 1
            				then
            					if
            						8AAB: not does_file_exist "cleo/TuningA.cm"
            					then
            					    0ACD: print_string_now "~y~cleo/TuningA.cm~s~ ~r~not found.~s~" 3000
            					else
            						016A: do_fade 0 750
            						wait 800
                                               						
            						31@ = 340.0
            						if
            						    DG_Backdoor == -11359
            						then
            						    0174: 28@ = get_car_heading 29@
            						    if and
            						        28@ > 90.0
            						        28@ < 270.0
            						    then
            						        31@ = 180.0
            						    end
            						end    
                                    0175: set_car_heading 29@ 31@                                    
                                    if
                                        31@ == 340.0
                                    then
                                        0361: close_garage 'HBGDSFS'
                                        08C3: clear_local_var_bit_const Gate bit 1
                                    end
                                    088C: set_car_coordinates_no_offset 29@ at -2052.625 157.199 28.551
            						02EB: restore_camera_jumpcut
            						
            						0A92: stream_custom_script "TuningA.cm"
            						wait 2000
            						016A: do_fade 1 750
            						
                                    repeat					
                    				    wait 0
                    				    if
                                            89E7: not is_player_control_on $PLAYER_CHAR
                                        gosub_if_false @ControlGarageDoors
                                    until 8100: not locate_char_in_car_3d $PLAYER_ACTOR in_sphere -2052.625 155.199 28.851 radius 7.0 7.0 7.0 sphere 0
                                                                                                         
                    				0ABA: terminate_all_custom_scripts_with_this_name "TFEN"
                    				continue
            					end
            				end
                    	end
                    end
                    gosub @ControlGarageDoors
                    continue
                end
            end   
        end
    end
    
    Gate = 0
    
    :continue500
    wait 500
    // why do the wait put here?
    // to adapt with more radar icons mod           
end


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


:OnlyRawFeatures
if
    0AAB: does_file_exist "cleo\EHI_Enex_Tweaks.s"
then
    0A92: stream_custom_script "EHI_Enex_Tweaks.s" 29@
end
0ED1: set_script_event_save_confirmation 1 label @ResetInteriors var_slot Game_Saved
Game_Saved = -1
if
    DohertyEnex < 1
gosub_if_false @GarageCamFix
Reset_Flag = 2
31@ = 1
wait 0

:SetInteriors
if
    8AAB: not does_file_exist "cleo\RBRD.cs"    // Rusty Brown's Ring Donuts Overhaul mod
then
    07FB: switch_entry_exit 'FDDONUT' access 31@
end

07FB: switch_entry_exit 'AMMUN4' access 31@     // 2 Floor AmmuNation

// These are open in the story beginning
07FB: switch_entry_exit 'SEXSHOP' access 31@
07FB: switch_entry_exit 'BURHOUS' access 31@    // Colonel Fuhrbergers's
07FB: switch_entry_exit 'MOTEL1' access 31@     // Jefferson Motel
07FB: switch_entry_exit 'DAMIN' access 31@      // Sherman Dam
07FB: switch_entry_exit 'CARTER' access 31@     // Big Smoke's Crack Factory

:SetInteriors2
07FB: switch_entry_exit 'E_BROT2' access 31@    // Second Room in Strip Club
07FB: switch_entry_exit 'E_BROTH' access 31@    // Fanny Batter's / Tiki Theater
07FB: switch_entry_exit 'E_FDCR1' access 31@    // Four Dragon's Roof

// Girlfriend Houses
07FB: switch_entry_exit 'E_GF1' access 31@ 
07FB: switch_entry_exit 'E_GF2' access 31@ 
07FB: switch_entry_exit 'E_GF3' access 31@ 
07FB: switch_entry_exit 'E_GF4' access 31@

// Luxury houses, LS
07FB: switch_entry_exit 'E_MULV1' access 31@
07FB: switch_entry_exit 'E_MULV2' access 31@
07FB: switch_entry_exit 'E_MULV3' access 31@
07FB: switch_entry_exit 'E_MULV4' access 31@
07FB: switch_entry_exit 'E_MULV5' access 31@
07FB: switch_entry_exit 'K_HORI' access 31@

// Madd Dogg's mansion, LS
07FB: switch_entry_exit 'E_MDDG1' access 0
07FB: switch_entry_exit 'E_MDDG2' access 0
07FB: switch_entry_exit 'E_MDDG3' access 0
09B4: set_closest_entry_exit_flag 1292.420 -786.082 radius 5.0 bitmask 16384 flag 0     // Madd Dogg's mansion wardrobe

09B4: set_closest_entry_exit_flag -2193.423 -2254.219 radius 5.0 bitmask 16384 flag 31@ // Angel Pine Motel

if or
    31@ == 0
    AP_Trailer == 1
    AP_Trailer == 2
then
    07FB: switch_entry_exit 'E_CAMPI' access 31@    // Angel Pine Trailer
end
if or
    31@ == 0
    DohertyEnex == 1
    DohertyEnex == 2
then
    07FB: switch_entry_exit 'E_DOHGR' access 31@    // Doherty Garage, SF
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit SweetsHouse
then
    07FB: switch_entry_exit 'E_SWEET' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit RydersHouse
then 
    07FB: switch_entry_exit 'E_RYDER' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit OGLocsHouse
then 
    07FB: switch_entry_exit 'E_OGLOC' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit BlastinFoolsRecords
then
    07FB: switch_entry_exit 'E_STUDI' access 31@
    07FB: switch_entry_exit 'E_STUD2' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit WooziesBettingShop
then 
    07FB: switch_entry_exit 'E_WUZIB' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit FourDragManagement
then
    07FB: switch_entry_exit 'E_FDC' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit FourDragJanitor
then 
    07FB: switch_entry_exit 'E_JANIT' access 31@
    07FB: switch_entry_exit 'E_FDCR2' access 31@
    07FB: switch_entry_exit 'E_FDCR3' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit CaligulasManagement
then
    07FB: switch_entry_exit 'E_CALIG' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit Airports
then
    07FB: switch_entry_exit 'E_AIRP2' access 31@
end

:SpecialMissionInteriors
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit ShamalInterior
then    
    07FB: switch_entry_exit 'E_SHAMA' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit LibertyCity
then
    07FB: switch_entry_exit 'E_SHALC' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit SaintMarksBistro
then
    07FB: switch_entry_exit 'E_LCMB' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit BDupsApartment
then
    07FB: switch_entry_exit 'E_BDUP' access 31@
end
if or
    31@ == 0
    08B7: is_local_var_bit_set_const AccessFlags bit BDupsCrackPalace
then
    07FB: switch_entry_exit 'E_BDUP1' access 31@
end 

07FB: switch_entry_exit 'E_GF6' access 31@      // Millie's house
07FB: switch_entry_exit 'E_JEFMO' access 31@    // Jefferson Motel Roof, LS
07FB: switch_entry_exit 'E_CALIB' access 31@    // Caligula's Basement

// Sherman Dam, Bone County
07FB: switch_entry_exit 'E_DAM1' access 31@
07FB: switch_entry_exit 'E_DAM2' access 31@
07FB: switch_entry_exit 'E_DAM3' access 31@

if
    Game_Saved == -1
then
    if
        0E1D: is_on_mission
    then
        // Petrol Station Dillimore
        0AAA: 30@ = get_script_struct_named 'CATALIN'   // Catalina missions
        if and
            30@ <> 0
            30@ <> -1
        then
            08BD: set_local_var_bit_const AccessFlags bit PetrolCheck
        end
    else
        08C3: clear_local_var_bit_const AccessFlags bit PetrolCheck
        if
            08B7: is_local_var_bit_set_const AccessFlags bit PetrolDoors
        then
            for 30@ = 0x14 to 0x18 step 0x04
                0D4E: 29@ = read_struct_offset iBuffer offset 30@ size 4
                if
                    03CA: does_object_exist 29@
                then
                    0108: delete_object 29@
                end
                0E28: write_struct_offset iBuffer offset 30@ size 4 value -1
            end            
            08C3: clear_local_var_bit_const AccessFlags bit PetrolDoors
        end
        
        // Bayside Didier Sachs
        if
            08B7: is_local_var_bit_set_const iIconFlags bit 5
        then
            0652: get_int_stat 30@ = integer_stat 329   // Saint Mark's Bistro passed (CASINO10)
            if
                30@ > 0
            then
                09B4: set_closest_entry_exit_flag -2492.166 2363.161 radius 5.0 bitmask 16384 flag 1
                0E2A: add_cleo_blip 45 position -2492.166 2363.161 is_short 1 RGBA 255 255 255 255 store_to 30@
                08C3: clear_local_var_bit_const iIconFlags bit 5
            end
        end        
        // Bayside Burger Shot
        if
            08B7: is_local_var_bit_set_const iIconFlags bit 6
        then
            0652: get_int_stat 30@ = integer_stat 200   // MealsEaten (not fully accurate but good enough)
            if
                30@ > 0
            then
                09B4: set_closest_entry_exit_flag -2477.121 2286.480 radius 5.0 bitmask 16384 flag 1
                0E2A: add_cleo_blip 10 position -2477.121 2286.480 is_short 1 RGBA 255 255 255 255 store_to 30@
                08C3: clear_local_var_bit_const iIconFlags bit 6
            end
        end
    end
end

if
    31@ == 0
then
    return
end

if
    08B7: is_local_var_bit_set_const AccessFlags bit CrackDen
then
    07FB: switch_entry_exit 'LACRAK' access 1   // Crack Den
end
if
    08B7: is_local_var_bit_set_const AccessFlags bit VagosHouse
then
    07FB: switch_entry_exit 'GANG' access 1     // Vagos Gang House
end
if
    08B7: is_local_var_bit_set_const AccessFlags bit PleasureDomes
then
    07FB: switch_entry_exit 'PDOMES' access 1   // Pleasure Domes
    07FB: switch_entry_exit 'PDOMES2' access 1  // Pleasure Domes Roof
end
if
    08B7: is_local_var_bit_set_const AccessFlags bit Abattoir
then
    07FB: switch_entry_exit 'ABATOIR' access 1  // Sindacco Abattoir
end
if
    08B7: is_local_var_bit_set_const AccessFlags bit Planning
then
    07FB: switch_entry_exit 'PAPER' access 1    // Planning Department
end


//------------ Mainloop for OnlyRawFeatures mode

while true
    wait 0
    if
        28@ == 2
    then
        0AD1: print_formatted_now "EHI Repatched: RepairGTA required. Please check the ReadMe file and install the requirements before you continue." time 1000
    end
    if
        28@ == 1
    then
        0AD1: print_formatted_now "EHI Repatched: Higher enex limit required. Please check the ReadMe file to increase it." time 1000
    end
    
    if
        Game_Saved > -1
    then
        wait 0
        wait 0
        Game_Saved = -1
        gosub @BaysideZone
        31@ = 1
        jump @SetInteriors
    end
    
    if or
        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR pos -2052.625 150.468 29.937 radius 80.0
        0E1D: is_on_mission
    then
        gosub @ResetGarageDoor
        Gate = 0
        wait 500
    else
        gosub @ChooseGarageDoor    
        0AAA: 30@ = get_script_struct_named 'DohGrge'   // Doherty Garage by vladvo
        if and
            30@ <> 0
            30@ <> -1
        then
            gosub @GarageSound
        else
            30@ = 7.0
            if
                0449: is_char_sitting_in_any_car $PLAYER_ACTOR
            then
                30@ = 11.0
            end           
            gosub @ControlGarageDoors
        end
    end
    
    if
        0E1D: is_on_mission
    then
        gosub @PetrolStation
        if
            Reset_Flag <> 1
        jf continue
        wait 0
        wait 0
        wait 2000
        Reset_Flag = 1
        31@ = 0
        gosub @SpecialMissionInteriors
        31@ = 1
        continue
    end
    if
        Reset_Flag <> 2
    then
        wait 0
        wait 0
        wait 2000
        Reset_Flag = 2
        jump @SpecialMissionInteriors
    end
end


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


:DisableVanillaMissionInteriors
07FB: switch_entry_exit 'LACRAK' access 0   // Crack Den
07FB: switch_entry_exit 'GANG' access 0     // Vagos Gang House
07FB: switch_entry_exit 'PDOMES' access 0   // Pleasure Domes
07FB: switch_entry_exit 'PDOMES2' access 0  // Pleasure Domes Roof
07FB: switch_entry_exit 'ABATOIR' access 0  // Sindacco Abattoir
07FB: switch_entry_exit 'PAPER' access 0    // Planning Department
return



:ControlGarageDoors
if
    DG_Backdoor == -11416
then           
    if
        0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR pos -2039.083 178.795 28.843 radius 30@
    jf @CloseGarage
    jump @OpenGarage
end              
if or
    0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR pos -2039.083 178.795 28.843 radius 30@
    0EEA: locate_char_distance_to_coordinates $PLAYER_ACTOR pos -2052.625 150.468 28.843 radius 30@
then
    :OpenGarage
    if
        03B1: is_garage_closed 'HBGDSFS'
    then
        if or
            88B7: not is_local_var_bit_set_const Gate bit 0
            88B7: not is_local_var_bit_set_const Gate bit 1
        then
            0360: open_garage 'HBGDSFS'
            08BD: set_local_var_bit_const Gate bit 1
            08BD: set_local_var_bit_const Gate bit 0
        end
    end
else
    :CloseGarage
    if
        80A4: not is_char_in_area_3d $PLAYER_ACTOR sphere 0 in_cube_cornerA -2057.279 150.688 25.546 cornerB -2038.957 182.581 36.546                
    then
        if
            03B0: is_garage_open 'HBGDSFS'
        then
            if or
                88B7: not is_local_var_bit_set_const Gate bit 0
                08B7: is_local_var_bit_set_const Gate bit 1
            then
                0361: close_garage 'HBGDSFS'
                08C3: clear_local_var_bit_const Gate bit 1
                08BD: set_local_var_bit_const Gate bit 0
            end
        end
    end
end

:GarageSound
if and
    83B0: not is_garage_open 'HBGDSFS'
    83B1: not is_garage_closed 'HBGDSFS'
then
    if
        88B7: not is_local_var_bit_set_const Gate bit 2
    then
        if
            DG_Backdoor == -11359
        then    
            097A: report_mission_audio_event_at_position -2052.625 150.468 29.937 1153
        end
        08BD: set_local_var_bit_const Gate bit 2
    end
else
    if
        08B7: is_local_var_bit_set_const Gate bit 2
    then
        097A: report_mission_audio_event_at_position -2052.625 150.468 29.937 1154
        08C3: clear_local_var_bit_const Gate bit 2
    end
end
return



:ChooseGarageDoor
if  
    DG_Backdoor > 0
return_if_false
0363: set_visibility_of_closest_object_of_type -2052.625 150.468 29.937 radius 6.0 model DG_Backdoor visibility 0
088D: set_uses_collision_of_closest_object_of_type -2052.625 150.468 29.937 radius 6.0 with_model DG_Backdoor collision_detection 0
DG_Backdoor *= -1
return



:ResetGarageDoor
if  
    DG_Backdoor < 0
return_if_false
if or
    016B: fading
    80A4: not is_char_in_area_3d $PLAYER_ACTOR sphere 0 in_cube_cornerA -2057.279 150.688 25.546 cornerB -2038.957 182.581 36.546
return_if_false
DG_Backdoor *= -1
0363: set_visibility_of_closest_object_of_type -2052.625 150.468 29.937 radius 6.0 model DG_Backdoor visibility 1
088D: set_uses_collision_of_closest_object_of_type -2052.625 150.468 29.937 radius 6.0 with_model DG_Backdoor collision_detection 1
return



:SetBarSign
for 30@ = 0x00 to 0x10 step 0x04
    0D4E: 31@ = read_struct_offset iBuffer offset 30@ size 4
    if
        03CA: does_object_exist 31@
    then
        0750: set_object_visible 31@ 29@
    end
end
return


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//------------ Airports

:OpenInteriors
if
    08B7: is_local_var_bit_set_const AccessFlags bit Airports
then
    07FB: switch_entry_exit 'E_AIRP2' access 1
end

//------------ Sweet's house 

if and
    $INTRO_TOTAL_PASSED_MISSIONS >= 1
    08B7: is_local_var_bit_set_const AccessFlags bit SweetsHouse 
then
    if or
        $SWEET_TOTAL_PASSED_MISSIONS == 7
        875C: not does_blip_exist $MARKER_SWEET_HOUSE        
    then
        07FB: switch_entry_exit 'E_SWEET' access 1
    end
end

//------------ Ryder's house & Rusty Brown's Ring Donuts

if
    $INTRO_TOTAL_PASSED_MISSIONS >= 2
then
    if and
        $LS_FINAL_TOTAL_PASSED_MISSIONS <= 1
        875C: not does_blip_exist $436
        08B7: is_local_var_bit_set_const AccessFlags bit RydersHouse
    then
        07FB: switch_entry_exit 'E_RYDER' access 1
    end
    if
        8AAB: not does_file_exist "cleo\RBRD.cs"    // Rusty Brown's Ring Donuts Overhaul mod
    then
        07FB: switch_entry_exit 'FDDONUT' access 1
    end  
end

//------------ B Dup's Apartment and Crack Den  

if and
    $SWEET_TOTAL_PASSED_MISSIONS >= 2
    Reset_Flag == 2
then
    if
        08B7: is_local_var_bit_set_const AccessFlags bit BDupsApartment
    then
        if or
            $LS_FINAL_TOTAL_PASSED_MISSIONS <= 1
            $GROVE_TOTAL_PASSED_MISSIONS >= 1
        then        
            07FB: switch_entry_exit 'E_BDUP' access 1
        end
    end
    if
        08B7: is_local_var_bit_set_const AccessFlags bit CrackDen
    then
        if or
            $LS_FINAL_TOTAL_PASSED_MISSIONS <= 1
            $GROVE_TOTAL_PASSED_MISSIONS >= 2
        then    
            07FB: switch_entry_exit 'LACRAK' access 1  // Crack Den
        end
    end
end

//------------ Fanny Batter's / Tiki Theater

if
    $SWEET_TOTAL_PASSED_MISSIONS >= 6
then
    07FB: switch_entry_exit 'E_BROTH' access 1  
end    

//------------ Colonel Fuhrberger's house 

if
    $RYDER_TOTAL_PASSED_MISSIONS >= 1 
then
    07FB: switch_entry_exit 'BURHOUS' access 1 
end

//------------ OG Loc's house 

if and
    $OG_LOC_TOTAL_PASSED_MISSIONS >= 5
    08B7: is_local_var_bit_set_const AccessFlags bit OGLocsHouse 
then
    if or
        $LS_FINAL_TOTAL_PASSED_MISSIONS <= 1
        $MANSION_TOTAL_PASSED_MISSIONS >= 4
    then
        07FB: switch_entry_exit 'E_OGLOC' access 1
    end
end

//------------ Vagos gang house & Sexshop

if
    $CRASH_LS_TOTAL_PASSED_MISSIONS >= 1 
then
    07FB: switch_entry_exit 'SEXSHOP' access 1    
    if
        08B7: is_local_var_bit_set_const AccessFlags bit VagosHouse
    then
        if or
            $LS_FINAL_TOTAL_PASSED_MISSIONS <= 1
            $RIOT_TOTAL_PASSED_MISSIONS >= 2
        then
            07FB: switch_entry_exit 'GANG' access 1  // Vagos Gang House
            
            0D4E: 29@ = read_struct_offset iBuffer offset 0x1C size 4
            0D4E: 30@ = read_struct_offset iBuffer offset 0x20 size 4
            0D4E: 31@ = read_struct_offset iBuffer offset 0x24 size 4
            if or
                83CA: not does_object_exist 29@
                83CA: not does_object_exist 30@
                83CA: not does_object_exist 31@
            then
                0247: request_model 3064
                0247: request_model 2950
                0247: request_model 3063              
                038B: load_all_models_now
                if and
                    0248: has_model_loaded 3064
                    83CA: not does_object_exist 29@
                then
                    0E01: create_object_no_save 3064 at 2338.119 -1181.924 1030.975 offset true ground true to 29@
                    0566: set_object_area_visible 29@ to_interior 5
                    0177: set_object_heading 29@ 90.0
                end
                if and
                    0248: has_model_loaded 2950
                    83CA: not does_object_exist 30@
                then
                    0E01: create_object_no_save 2950 at 2330.393 -1179.16 1030.55 offset true ground true to 30@
                    0566: set_object_area_visible 30@ to_interior 5
                end
                if and
                    0248: has_model_loaded 3063
                    83CA: not does_object_exist 31@
                then
                    0E01: create_object_no_save 3063 at 2340.281 -1182.294 1026.977 offset true ground true to 31@
                    0566: set_object_area_visible 31@ to_interior 5
                    0177: set_object_heading 31@ 90.0
                end
                0249: mark_model_as_no_longer_needed 3064
                0249: mark_model_as_no_longer_needed 2950
                0249: mark_model_as_no_longer_needed 3063
                0E28: write_struct_offset iBuffer offset 0x1C size 4 value 29@
                0E28: write_struct_offset iBuffer offset 0x20 size 4 value 30@
                0E28: write_struct_offset iBuffer offset 0x24 size 4 value 31@
            end
        end
    end
end

//------------ 2 Floor AmmuNation 

0AAA: 31@ = get_script_struct_named 'CRASH4'    // Doberman

if or
    31@ > 0
    31@ < -1
    $SWEET_TOTAL_PASSED_MISSIONS >= 8 
then
    07FB: switch_entry_exit 'AMMUN4' access 1   // 2 Floor AmmuNation
end

//------------ Jefferson Motel

0AAA: 31@ = get_script_struct_named 'DRUGS4'    // Reuniting the Families
    
if or
    31@ > 0
    31@ < -1
    $LS_FINAL_TOTAL_PASSED_MISSIONS >= 1
then
    07FB: switch_entry_exit 'MOTEL1' access 1
    if
        $LS_FINAL_TOTAL_PASSED_MISSIONS >= 1
    then
        07FB: switch_entry_exit 'E_JEFMO' access 1
    end        
end

//------------ Angel Pine Trailer Lodge  

if and
    $LS_FINAL_TOTAL_PASSED_MISSIONS >= 2
    AP_Trailer > 0
then
    if
        AP_Trailer == 1
    then
        if or
            875C: not does_blip_exist $484
            $TRUTH_TOTAL_PASSED_MISSIONS == 0
        then    
            07FB: switch_entry_exit 'E_CAMPI' access 1
        end
    else
        if
            AP_Trailer == 2
        then
            07FB: switch_entry_exit 'E_CAMPI' access 1                  
            if
                AP_Icon == -1
            then
                0E2A: add_cleo_blip 35 position -2045.232 -2522.631 is_short 1 RGBA 255 255 255 255 store_to AP_Icon
            end
        else
            if
                AP_Icon == -1
            then
                0E2A: add_cleo_blip 35 position -2046.242 -2518.744 is_short 1 RGBA 255 255 255 255 store_to AP_Icon
            end
        end
        if
            075C: does_blip_exist $1730
        then
            0164: remove_blip $1730 // Disables original marker
        end
    end
end 

//------------ Palomino Creek Bank

if
    $MISSION_SMALL_TOWN_BANK_PASSED == 1
then
    if or
        83CA: not does_object_exist $2662
        83CA: not does_object_exist $2663
    then
        0247: request_model 2946
        038B: load_all_models_now     
        if
            83CA: not does_object_exist $2662 
        then
            029B: create_object_no_offset $2662 2946 2304.2571 -17.744 25.742
            0550: freeze_object_position $2662 1
            07F7: set_object_collision_damage_effect $2662 0
        end
        if
            83CA: not does_object_exist $2663
        then
            029B: create_object_no_offset $2663 2946 2304.2571 -14.583 25.742
            0550: freeze_object_position $2663 1
            07F7: set_object_collision_damage_effect $2663 0
        end
        0249: mark_model_as_no_longer_needed 2946
    end
    0177: set_object_heading $2662 280.0
    0177: set_object_heading $2663 255.0
end

//------------ Angel Pine Motel

if
    $TRUTH_TOTAL_PASSED_MISSIONS >= 1
then
    09B4: set_closest_entry_exit_flag -2193.423 -2254.219 radius 5.0 bitmask 16384 flag 1
end

//------------ Doherty Garage

if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 16
then
    if and
        $GARAGE_TOTAL_PASSED_MISSIONS >= 1
        001E: is_int_var_greater_than_int_lvar $STEAL_TOTAL_PASSED_MISSIONS > Required_Doherty_Missions
        Garage_Icon == -1
    then
        0E2A: add_cleo_blip 27 position -2056.619 178.625 is_short 1 RGBA 255 255 255 255 store_to Garage_Icon
    end
    if
        DohertyEnex > 0
    then
        if
            DohertyEnex == 1               
        then
            if or
                $GARAGE_TOTAL_PASSED_MISSIONS >= 1
                09D1: does_pickup_exist $807                   
            then
                07FB: switch_entry_exit 'E_DOHGR' access 1
            end
        else
            if
                DohertyEnex == 2                
            then
                07FB: switch_entry_exit 'E_DOHGR' access 1
            end
            if
                075C: does_blip_exist $1732
            then
                0164: remove_blip $1732 // Disables original marker
            end
            if
                Doherty_Icon == -1
            then
                0E2A: add_cleo_blip 35 position -2039.922 153.479 is_short 1 RGBA 255 255 255 255 store_to Doherty_Icon
            end
        end
    end               
else
    if and
        DohertyEnex > 0
        DohertyEnex < 3
        DohertyWeapons > 0
        09D1: does_pickup_exist $807
    then
        07FB: switch_entry_exit 'E_DOHGR' access 1
    end
end     
    
//------------ Woozie's betting shop 

if and
    $WUZIMU_TOTAL_PASSED_MISSIONS >= 5
    08B7: is_local_var_bit_set_const AccessFlags bit WooziesBettingShop
then
    07FB: switch_entry_exit 'E_WUZIB' access 1 
end

//------------ Pleasure Domes 

if and
    $SYNDICATE_TOTAL_PASSED_MISSIONS >= 7
    08B7: is_local_var_bit_set_const AccessFlags bit PleasureDomes
then
    07FB: switch_entry_exit 'PDOMES' access 1
    07FB: switch_entry_exit 'PDOMES2' access 1
end

//------------ Toreno's Ranch Safehouse

if
    Toreno_House > 0
then
    if
        $TORENO_TOTAL_PASSED_MISSIONS >= 2
    then    
        09B4: set_closest_entry_exit_flag -684.079 939.561 radius 5.0 bitmask 16384 flag 1
    end
    if
        Toreno_House > 1
    then
        if
            075C: does_blip_exist $1733
        then
            0164: remove_blip $1733 // Disables original marker
        end
        if and
            $TORENO_TOTAL_PASSED_MISSIONS >= 2
            Toreno_Icon == -1
        then
            0E2A: add_cleo_blip 35 position -684.079 939.561 is_short 1 RGBA 255 255 255 255 store_to Toreno_Icon
        end
    end
end
    
//------------ Area 69  

if       
    $TORENO_TOTAL_PASSED_MISSIONS >= 8
then
    if and
        03CA: does_object_exist $OBJECT_M_A51_VENTCOVERB
        8AAB: not does_file_exist "cleo\Urbanize\DE\ARA69.cs"
    then
        01BC: set_object_coordinates $OBJECT_M_A51_VENTCOVERB at 245.968 1862.843 16.5
    end
end

//------------ Four Dragon's Casino

if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 18
then
    07FB: switch_entry_exit 'E_FDCR1' access 1
    if    
        FDC_Door <> -1
    then
        07FB: switch_entry_exit 'E_FDC' access 1
        09B4: set_closest_entry_exit_flag 2019.891 988.393 radius 5.0 bitmask 16384 flag 1
    else
        if and
            $CASINO_TOTAL_PASSED_MISSIONS >= 3
            $CASINO_TOTAL_PASSED_MISSIONS <> 6
            08B7: is_local_var_bit_set_const AccessFlags bit FourDragManagement
        then
            if or
                $CASINO_TOTAL_PASSED_MISSIONS <= 8 
                875C: not does_blip_exist $582
            then
                07FB: switch_entry_exit 'E_FDC' access 1 
            end
        end
    end
else
    if and
        FDC_Door <> -1
        09D1: does_pickup_exist $811
    then
        07FB: switch_entry_exit 'E_FDC' access 1
    end
end

//------------ Golf Club in Four Dragons Management Room  

if
    $CASINO_TOTAL_PASSED_MISSIONS >= 2 
then
    if
        89D1: not does_pickup_exist iGolfClub
    then
        0213: create_pickup iGolfClub 333 15 2008.627 1011.566 1031.179
    end
end      

//------------ Sindacco Plastic Factory  

if
    $CASINO_TOTAL_PASSED_MISSIONS >= 3 
then
    if
        03CA: does_object_exist $2664 
    then
        01BC: set_object_coordinates $2664 at 1055.629 2087.67 0.299
    end   
end

//------------ Sindacco Abattoir

if and
    $CASINO_TOTAL_PASSED_MISSIONS >= 6
    08B7: is_local_var_bit_set_const AccessFlags bit Abattoir 
then   
    07FB: switch_entry_exit 'ABATOIR' access 1
end

//------------ Management room in Caligula's Palace

if and
    $CASINO_TOTAL_PASSED_MISSIONS >= 6
    08B7: is_local_var_bit_set_const AccessFlags bit CaligulasManagement 
then
    if and
        $CASINO_TOTAL_PASSED_MISSIONS <> 7
        $CASINO_TOTAL_PASSED_MISSIONS <> 8
        $HEIST_TOTAL_PASSED_MISSIONS <= 5
    then 
        07FB: switch_entry_exit 'E_CALIG' access 1
    end 
end

//------------ Planning Department

if and
    $HEIST_TOTAL_PASSED_MISSIONS >= 1
    08B7: is_local_var_bit_set_const AccessFlags bit Planning 
then
    07FB: switch_entry_exit 'PAPER' access 1
end

//------------ Sherman Dam

if
    $HEIST_TOTAL_PASSED_MISSIONS >= 3
then  
    07FB: switch_entry_exit 'DAMIN' access 1
    07FB: switch_entry_exit 'E_DAM1' access 1
    07FB: switch_entry_exit 'E_DAM2' access 1
    07FB: switch_entry_exit 'E_DAM3' access 1
end

//------------ Caligula's Basement & Janitor's room in Four Dragons Casino 

if
    $HEIST_TOTAL_PASSED_MISSIONS >= 6
then
    07FB: switch_entry_exit 'E_CALIB' access 1
    if
        03CA: does_object_exist $2703
    then
        0177: set_object_heading $2703 177.0
    end 
    if
        08B7: is_local_var_bit_set_const AccessFlags bit FourDragJanitor
    then  
        07FB: switch_entry_exit 'E_JANIT' access 1
        07FB: switch_entry_exit 'E_FDCR2' access 1 
        07FB: switch_entry_exit 'E_FDCR3' access 1
    end
end

//------------ Luxury Houses & additional entrances at Madd Dogg's mansion

if
    $MANSION_TOTAL_PASSED_MISSIONS >= 1 
then
    if
        08B7: is_local_var_bit_set_const AccessFlags bit MD_Mansion
    then    
        07FB: switch_entry_exit 'E_MDDG1' access 1
        07FB: switch_entry_exit 'E_MDDG2' access 1
        07FB: switch_entry_exit 'E_MDDG3' access 1
        09B4: set_closest_entry_exit_flag 1292.420 -786.082 radius 5.0 bitmask 16384 flag 1
    end
    if
        08B7: is_local_var_bit_set_const AccessFlags bit LuxuryHouses
    then
        07FB: switch_entry_exit 'E_MULV1' access 1
        07FB: switch_entry_exit 'E_MULV2' access 1
        07FB: switch_entry_exit 'E_MULV3' access 1
        07FB: switch_entry_exit 'E_MULV4' access 1
        07FB: switch_entry_exit 'E_MULV5' access 1
        07FB: switch_entry_exit 'K_HORI' access 1
    end
end

//------------ Blastin' Fools Records  

if and
    $MANSION_TOTAL_PASSED_MISSIONS >= 4
    08B7: is_local_var_bit_set_const AccessFlags bit BlastinFoolsRecords 
then
    07FB: switch_entry_exit 'E_STUDI' access 1
    07FB: switch_entry_exit 'E_STUD2' access 1
end

//------------ B Dup's crack palace   

if and
    $GROVE_TOTAL_PASSED_MISSIONS >= 1
    08B7: is_local_var_bit_set_const AccessFlags bit BDupsCrackPalace
    Reset_Flag == 2
then
    07FB: switch_entry_exit 'E_BDUP1' access 1 
end

//------------ Big Smoke's Crack Factory (Los Flores)   

0AAA: 31@ = get_script_struct_named 'FINALEA'

if or
    31@ > 0
    31@ < -1
    $1354 == 1
    $RIOT_TOTAL_PASSED_MISSIONS >= 3 
then
    07FB: switch_entry_exit 'CARTER' access 1 
end

//------------ After story completing

if
    $RIOT_TOTAL_PASSED_MISSIONS >= 5
then
    if
        08B7: is_local_var_bit_set_const AccessFlags bit RydersHouse
    then
        07FB: switch_entry_exit 'E_RYDER' access 1
    end
    if
        08B7: is_local_var_bit_set_const AccessFlags bit CaligulasManagement
    then
        07FB: switch_entry_exit 'E_CALIG' access 1
    end
    if
        08B7: is_local_var_bit_set_const AccessFlags bit ShamalInterior
    then
        07FB: switch_entry_exit 'E_SHAMA' access 1
    end
    if
        08B7: is_local_var_bit_set_const AccessFlags bit LibertyCity
    then
        07FB: switch_entry_exit 'E_SHALC' access 1
    end
    if
        08B7: is_local_var_bit_set_const AccessFlags bit SaintMarksBistro
    then
        07FB: switch_entry_exit 'E_LCMB' access 1
    end
end

//------------ Second Room in the LS strip club

if  
    $MISSION_PIMPING_PASSED >= 1
then
    07FB: switch_entry_exit 'E_BROT2' access 1
end

//------------ Weapon Pickups

if and
    DohertyWeapons > 0
    88B7: not is_local_var_bit_set_const iPickups bit 0
then
    if
        09D1: does_pickup_exist $807
    then
        08BD: set_local_var_bit_const iPickups bit 0       
        0215: remove_pickup $807
        032B: create_pickup_with_ammo $807 = create_weapon_pickup 358 type 15 ammo 60 at -2040.752 163.527 28.3359
    end
    if
        09D1: does_pickup_exist $808
    then
        0215: remove_pickup $808
        032B: create_pickup_with_ammo $808 = create_weapon_pickup 352 type 15 ammo 120 at -2043.752 163.527 28.3359
    end
    if
        09D1: does_pickup_exist $809
    then
        0215: remove_pickup $809
        032B: create_pickup_with_ammo $809 = create_weapon_pickup 349 type 15 ammo 120 at -2040.752 166.527 28.3359
    end
    if
        09D1: does_pickup_exist $810
    then
        0215: remove_pickup $810
        032B: create_pickup_with_ammo $810 = create_weapon_pickup 342 type 15 ammo 20 at -2043.752 166.527 28.3359
    end
end
if and
    FDC_Door <> -1
    88B7: not is_local_var_bit_set_const iPickups bit 1
then
    if
        09D1: does_pickup_exist $811
    then
        08BD: set_local_var_bit_const iPickups bit 1
        0215: remove_pickup $811
        032B: create_pickup_with_ammo $811 = create_weapon_pickup 356 type 15 ammo 60 at 2000.936 1024.612 1031.196
    end
    if
        09D1: does_pickup_exist $812
    then
        0215: remove_pickup $812
        032B: create_pickup_with_ammo $812 = create_weapon_pickup 353 type 15 ammo 120 at 2003.936 1024.612 1031.196
    end
    if
        09D1: does_pickup_exist $813
    then
        0215: remove_pickup $813
        032B: create_pickup_with_ammo $813 = create_weapon_pickup 351 type 15 ammo 120 at 2000.936 1027.612 1031.196
    end
    if
        09D1: does_pickup_exist $814
    then
        0215: remove_pickup $814
        032B: create_pickup_with_ammo $814 = create_weapon_pickup 363 type 15 ammo 20 at 2003.936 1027.612 1031.396
    end
end

// Move the Toreno Ranch Weapons to the interior because they are too close to the enex.   
if and
    Toreno_House > 0
    88B7: not is_local_var_bit_set_const iPickups bit 2
then
    if
        09D1: does_pickup_exist $2873
    then
        08BD: set_local_var_bit_const iPickups bit 2
        0215: remove_pickup $2873
        Pickup.Create($2873, 359, 15, 2316.0, -1148.2, 1050.3)
    end
    if
        09D1: does_pickup_exist $2874
    then
        0215: remove_pickup $2874
        Pickup.Create($2874, 360, 15, 2320.0, -1148.2, 1050.3)
    end
    if
        09D1: does_pickup_exist $2875
    then
        0215: remove_pickup $2875
        Pickup.Create($2875, 362, 15, 2328.8, -1148.2, 1050.3)
    end
    if
        09D1: does_pickup_exist $2876
    then
        0215: remove_pickup $2876
        Pickup.Create($2876, 361, 15, 2332.8, -1148.2, 1050.3)
    end
end

//------------ Marker Distance Fix
 
if
    MarkerDistX2 > 0
return_if_false
if
    MarkerDistX2 == 1
then
    0AAA: 31@ = get_script_struct_named 'SYN1'  // Photo Opportunity
    if and
        31@ <> 0
        31@ <> -1
    return_if_false
    0A8C: write_memory 0x440F09 size 4 value 0x859600 virtual_protect 0 // revert normal marker distance
    0A8C: write_memory 0x72507B size 4 value 0x858A28 virtual_protect 0
    MarkerDistX2 = 2
    return
end

:MarkerDistX2
0A8C: write_memory 0x440F09 size 4 value 0x863E14 virtual_protect 0 //1600.0->80000.0
0A8C: write_memory 0x72507B size 4 value 0x8599D8 virtual_protect 0 //150.0->289.0
MarkerDistX2 = 1
return


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


:MaddDoggMissionMarker
if
    075C: does_blip_exist $584
return_if_false
0164: remove_blip $584
Marker.CreateIconAndSphere($584, 24, $X_ROYAL_CASINO, $Y_ROYAL_CASINO, $Z_ROYAL_CASINO)
return   



:DohertyMissionMarker
if and
    $SYNDICATE_TOTAL_PASSED_MISSIONS <> 7
    075C: does_blip_exist $MARKER_TRIADS_SF_GARAGE
return_if_false
0164: remove_blip $MARKER_TRIADS_SF_GARAGE
31@ = 15
if
    $GARAGE_TOTAL_PASSED_MISSIONS >= 2
then
    31@ = 43
end
if and
    $CRASH_SF_TOTAL_PASSED_MISSIONS == 1
    $SYNDICATE_TOTAL_PASSED_MISSIONS == 6
then
    31@ = 16
end    
Marker.CreateIconAndSphere($MARKER_TRIADS_SF_GARAGE, 31@, $X_SF_GARAGE, $Y_SF_GARAGE, $Z_SF_GARAGE)
return



:DohertySave
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 16
return_if_false 
if
    075C: does_blip_exist $1732
then
    0164: remove_blip $1732 // Disables original marker
end
0570: add_short_range_sprite_blip_for_contact_point $1732 = create_asset_radar_marker_with_icon 35 at $SAVE_PICKUPS_X[15] $SAVE_PICKUPS_Y[15] $SAVE_PICKUPS_Z[15]
018B: change_blip_display $1732 radar_mode 2

:DohertySavePickup
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 16
return_if_false    
if
    09D1: does_pickup_exist $SAVE_PICKUPS[15]
then
    0215: remove_pickup $SAVE_PICKUPS[15]
end  
Pickup.Create($SAVE_PICKUPS[15], 1277, 3, $SAVE_PICKUPS_X[15], $SAVE_PICKUPS_Y[15], $SAVE_PICKUPS_Z[15])
return



:AngelPineMissionMarker
if
    075C: does_blip_exist $484
return_if_false
0164: remove_blip $484
if
    $TRUTH_TOTAL_PASSED_MISSIONS >= 1
then
    Marker.CreateIconAndSphere($484, 13, $X_ANGEL_PINE, $Y_ANGEL_PINE, $Z_ANGEL_PINE)
    return
end    
Marker.CreateIconAndSphere($484, 16, $X_ANGEL_PINE, $Y_ANGEL_PINE, $Z_ANGEL_PINE)
return



:AngelPineSave
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 14
return_if_false
if
    075C: does_blip_exist $1730
then
    0164: remove_blip $1730
end
0570: add_short_range_sprite_blip_for_contact_point $1730 = create_asset_radar_marker_with_icon 35 at $SAVE_PICKUPS_X[13] $SAVE_PICKUPS_Y[13] $SAVE_PICKUPS_Z[13]
018B: change_blip_display $1730 radar_mode 2

:AngelPineSavePickup
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 14
return_if_false
if
    09D1: does_pickup_exist $SAVE_PICKUPS[13]
then
    0215: remove_pickup $SAVE_PICKUPS[13]
end
Pickup.Create($SAVE_PICKUPS[13], 1277, 3, $SAVE_PICKUPS_X[13], $SAVE_PICKUPS_Y[13], $SAVE_PICKUPS_Z[13])
return



:TorenoSave
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 17
return_if_false
if
    075C: does_blip_exist $1733
then
    0164: remove_blip $1733 // Disables original marker
end
0570: add_short_range_sprite_blip_for_contact_point $1733 = create_asset_radar_marker_with_icon 35 at $SAVE_PICKUPS_X[16] $SAVE_PICKUPS_Y[16] $SAVE_PICKUPS_Z[16]
018B: change_blip_display $1733 radar_mode 2

:TorenoSavePickup
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 17
return_if_false  
if
    09D1: does_pickup_exist $SAVE_PICKUPS[16]
then
    0215: remove_pickup $SAVE_PICKUPS[16]
end  
Pickup.Create($SAVE_PICKUPS[16], 1277, 3, $SAVE_PICKUPS_X[16], $SAVE_PICKUPS_Y[16], $SAVE_PICKUPS_Z[16])
return



:FourDragonsSave
if
    $TOTAL_AVAILABLE_SAVE_PICKUPS >= 18
return_if_false
if
    075C: does_blip_exist $582
then
    if or
        $CASINO_TOTAL_PASSED_MISSIONS <= 2
        $CASINO_TOTAL_PASSED_MISSIONS >= 9
        $CASINO_TOTAL_PASSED_MISSIONS == 6
    then    
        0164: remove_blip $582
        Marker.CreateIconAndSphere($582, 44, $X_THE_FOUR_DRAGONS_CASINO, $Y_THE_FOUR_DRAGONS_CASINO, $Z_THE_FOUR_DRAGONS_CASINO)
        08DC: set_blip_entry_exit $582 at 2026.603 1007.735 radius 20.0
    end
end  
if
    09D1: does_pickup_exist $SAVE_PICKUPS[17]
then
    0215: remove_pickup $SAVE_PICKUPS[17]
end  
Pickup.Create($SAVE_PICKUPS[17], 1277, 3, $SAVE_PICKUPS_X[17], $SAVE_PICKUPS_Y[17], $SAVE_PICKUPS_Z[17])
return



:BaysideZone
0A8D: 31@ = read_memory 0xBA37D0 size 2 virtual_protect 0   // Y-coord of right upper corner
if
    31@ == 2475
return_if_false
0A8C: write_memory 0xBA37D0 size 2 value 2548 virtual_protect 0 // Adjust zone size of Bayside Marina for Bayside beach restaurant
return



:GarageCamFix
0A8D: 31@ = read_memory 0x96D325 size 2 virtual_protect 0    // Disables immobile garage camera in Doherty Garage
08BD: set_local_var_bit_const 31@ bit 13
0A8C: write_memory 0x96D325 size 2 value 31@ virtual_protect 0
return



:PetrolStation
if
    08B7: is_local_var_bit_set_const AccessFlags bit PetrolCheck
return_if_false
0AAA: 30@ = get_script_struct_named 'CAT3'  // Tanker Commander                       
if or
    30@ > 0
    30@ < -1          
return_if_false
30@ = 0
while 0EA9: get_any_object_no_save_recursive 30@ progress_to 30@ object_to 29@
    if
        09CC: does_object_have_this_model 29@ model 1523
    then
        if
            0EEC: locate_object_distance_to_coordinates 29@ pos 661.965 -573.402 14.981 radius 2.0
        then
            0905: lock_door 29@ lock 1
            if
                88B7: not is_local_var_bit_set_const AccessFlags bit PetrolDoors
            jf break
            08BD: set_local_var_bit_const AccessFlags bit PetrolDoors
        end
    end                        
end
0247: request_model 1523
038B: load_all_models_now
0E01: create_object_no_save 1523 at 661.933 -571.971 14.982 offset false ground false to 29@
0177: set_object_heading 29@ 270.0
0905: lock_door 29@ lock 1
0E28: write_struct_offset iBuffer offset 0x14 size 4 value 29@
0E01: create_object_no_save 1523 at 661.993 -574.831 14.982 offset false ground false to 29@
0177: set_object_heading 29@ 90.0
0905: lock_door 29@ lock 1
0E28: write_struct_offset iBuffer offset 0x18 size 4 value 29@
0249: mark_model_as_no_longer_needed 1523                
08BD: set_local_var_bit_const AccessFlags bit PetrolDoors
08C3: clear_local_var_bit_const AccessFlags bit PetrolCheck
return


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


:RestoreBeforeSave
// Palomino Creek Bank doors
if
    $MISSION_SMALL_TOWN_BANK_PASSED == 1
then
    if and
        03CA: does_object_exist $2662
        03CA: does_object_exist $2663
    then
        0177: set_object_heading $2662 0.0
        0177: set_object_heading $2663 180.0
    end
end

// Area 69
if
    $TORENO_TOTAL_PASSED_MISSIONS >= 8
then
    if and
        03CA: does_object_exist $OBJECT_M_A51_VENTCOVERB
        8AAB: not does_file_exist "cleo\Urbanize\DE\ARA69.cs"
    then
        01BC: set_object_coordinates $OBJECT_M_A51_VENTCOVERB at 245.968 1862.843 19.49
        0453: set_object_rotation $OBJECT_M_A51_VENTCOVERB XYZ_rotation 0.0 0.0 -140.998
    end
end

// Sindacco Plastic Factory
if
    $CASINO_TOTAL_PASSED_MISSIONS >= 3 
then
    if
        03CA: does_object_exist $2664
    then
        01BC: set_object_coordinates $2664 at 1055.629 2087.67 12.469
    end
end

// Caligulas Basement
if
    $HEIST_TOTAL_PASSED_MISSIONS >= 6
then
    if
        03CA: does_object_exist $2703
    then
        0177: set_object_heading $2703 270.0
    end
end

$X_ROYAL_CASINO = 2090.0
$Y_ROYAL_CASINO = 1451.0
gosub @MaddDoggMissionMarker

if
    DohertyEnex > 0
then
    $Y_SF_GARAGE_3 = 148.8279
    $Y_SF_GARAGE = 148.8279
    gosub @DohertyMissionMarker
    
    if and
        DohertyEnex > 1
        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR pos $SAVE_PICKUPS_X[15] $SAVE_PICKUPS_Y[15] $SAVE_PICKUPS_Z[15] radius 4.0
    then
        $SAVE_PICKUPS_X[15] = -2026.481
        $SAVE_PICKUPS_Y[15] = 156.8028
        $SAVE_PICKUPS_Z[15] = 28.5391
        $SAVE_POINTS_X[15] = -2021.036
        $SAVE_POINTS_Y[15] = 156.0674
        $SAVE_POINTS_Z[15] = 27.6521
        $SAVE_POINTS_ANGLE[15] = 272.5815
        gosub @DohertySave
    end
end

// Angel Pine Trailer
if
    AP_Trailer > 1
then
    $X_ANGEL_PINE = -2043.34
    gosub @AngelPineMissionMarker
    
    if
        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR pos $SAVE_PICKUPS_X[13] $SAVE_PICKUPS_Y[13] $SAVE_PICKUPS_Z[13] radius 4.0
    then
        $SAVE_PICKUPS_X[13] = -2037.89
        $SAVE_PICKUPS_Y[13] = -2526.96
        $SAVE_PICKUPS_Z[13] = 30.13
        $SAVE_POINTS_X[13] = -2039.78
        $SAVE_POINTS_Y[13] = -2529.1
        $SAVE_POINTS_Z[13] = 29.63
        $SAVE_POINTS_ANGLE[13] = 21.23
        gosub @AngelPineSave
    end
end

if
    Toreno_House > 0
then    
    // Toreno Safe House
    if and
        Toreno_House > 1
        8EEA: not locate_char_distance_to_coordinates $PLAYER_ACTOR pos $SAVE_PICKUPS_X[16] $SAVE_PICKUPS_Y[16] $SAVE_PICKUPS_Z[16] radius 4.0
    then          
        $SAVE_PICKUPS_X[16] = -693.3776
        $SAVE_PICKUPS_Y[16] = 957.824
        $SAVE_PICKUPS_Z[16] = 11.7829
        $SAVE_POINTS_X[16] = -700.3663
        $SAVE_POINTS_Y[16] = 966.0852
        $SAVE_POINTS_Z[16] = 11.3575
        $SAVE_POINTS_ANGLE[16] = 90.0           
        gosub @TorenoSave  
    end
        
    // Toreno Ranch Weapons
    if
        08B7: is_local_var_bit_set_const iPickups bit 2
    then    
        if
            09D1: does_pickup_exist $2873
        then
            0215: remove_pickup $2873
        end
        Pickup.Create($2873, 359, 15, -686.0, 934.0, 13.5)
        if
            09D1: does_pickup_exist $2874
        then
            0215: remove_pickup $2874
        end
        Pickup.Create($2874, 360, 15, -690.0, 934.0, 13.5)
        if
            09D1: does_pickup_exist $2875
        then
            0215: remove_pickup $2875
        end
        Pickup.Create($2875, 362, 15, -690.0, 939.0, 13.5)
        if
            09D1: does_pickup_exist $2876
        then
            0215: remove_pickup $2876
        end
        Pickup.Create($2876, 361, 15, -686.0, 939.0, 13.5)
    end
end

if and
    DohertyWeapons > 0
    08B7: is_local_var_bit_set_const iPickups bit 0
then   
    if
        09D1: does_pickup_exist $807
    then
        0215: remove_pickup $807
        032B: create_pickup_with_ammo $807 = create_weapon_pickup 358 type 15 ammo 60 at -2035.773 139.4337 28.3359
    end
    if
        09D1: does_pickup_exist $808
    then
        0215: remove_pickup $808
        032B: create_pickup_with_ammo $808 = create_weapon_pickup 352 type 15 ammo 120 at -2038.43 139.6281 28.3359
    end
    if
        09D1: does_pickup_exist $809
    then
        0215: remove_pickup $809
        032B: create_pickup_with_ammo $809 = create_weapon_pickup 349 type 15 ammo 120 at -2038.664 137.4694 28.3359
    end
    if
        09D1: does_pickup_exist $810
    then
        0215: remove_pickup $810
        032B: create_pickup_with_ammo $810 = create_weapon_pickup 342 type 15 ammo 20 at -2035.474 137.2511 28.3359
    end
end

if
    FDC_Door <> -1
then
    if
        88B7: not is_local_var_bit_set_const Gate bit 7
    then
        $X_THE_FOUR_DRAGONS_CASINO = 1962.432
        $Y_THE_FOUR_DRAGONS_CASINO = 974.675
        $SAVE_PICKUPS_Z[17] = 10.3203
        gosub @FourDragonsSave
    end
    if
        08B7: is_local_var_bit_set_const iPickups bit 1
    then
        if
            09D1: does_pickup_exist $811
        then
            0215: remove_pickup $811
            032B: create_pickup_with_ammo $811 = create_weapon_pickup 356 type 15 ammo 60 at 2021.879 1001.467 10.3203
        end
        if
            09D1: does_pickup_exist $812
        then
            0215: remove_pickup $812
            032B: create_pickup_with_ammo $812 = create_weapon_pickup 353 type 15 ammo 120 at 2025.286 1001.496 10.3203
        end
        if
            09D1: does_pickup_exist $813
        then
            0215: remove_pickup $813
            032B: create_pickup_with_ammo $813 = create_weapon_pickup 351 type 15 ammo 120 at 2021.327 1013.349 10.3203
        end
        if
            09D1: does_pickup_exist $814
        then
            0215: remove_pickup $814
            032B: create_pickup_with_ammo $814 = create_weapon_pickup 363 type 15 ammo 20 at 2023.775 1013.527 10.5203
        end
    end
end

if
    09D1: does_pickup_exist iGolfClub
then
    0215: remove_pickup iGolfClub
end
iGolfClub = -1

if
    iAmmunEnex > 0
then
    0E28: write_struct_offset iAmmunEnex offset 0x18 size 4 value 13.55 // Revert old Z Coord
end

:ResetInteriors
31@ = 0
gosub @SetInteriors2
gosub @DisableVanillaMissionInteriors
07FB: switch_entry_exit 'SEXSHOP' access 1
07FB: switch_entry_exit 'BURHOUS' access 1  // Colonel Fuhrbergers's
07FB: switch_entry_exit 'MOTEL1' access 1   // Jefferson Motel
07FB: switch_entry_exit 'DAMIN' access 1    // Sherman Dam
07FB: switch_entry_exit 'CARTER' access 1   // Big Smoke's Crack Factory

gosub @ResetGarageDoor
Gate = 0

// Reset zone size of Bayside Marina for Bayside beach restaurant
0A8D: 31@ = read_memory 0xBA37D0 size 2 virtual_protect 0
if
    31@ == 2548
then
    0A8C: write_memory 0xBA37D0 size 2 value 2475 virtual_protect 0
end
return_script_event


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


:Buffer
hex
    00(40)
end


:ILoveMyGirlfriendJessica
TIMERA = 0

repeat
    wait 0
    if
        0@ == 2
    then
        0AD1: print_formatted_now "EHI Repatched: CLEO+ 1.2.0 or newer required. It's a plugin for CLEO. Please download and install it." time 200
    end
    if
        0@ == 1
    then
        0AD1: print_formatted_now "EHI Repatched: You forgot to delete an old version of EHI. Find and uninstall it and try again." time 200
    end
    if
        0@ == 0
    then
        0AD1: print_formatted_now "EHI Repatched: Failed to find the file 'EHI.ini'. Check the file path or file name." time 200
    end
    if and
        09E7: is_player_control_on $PLAYER_CHAR
        816B: not fading
    jf @ILoveMyGirlfriendJessica
until TIMERA > 10000 

0A93: terminate_this_custom_script