{$CLEO}        
{$USE CLEO+}
// ---------------------------------------
// Enex Tweaks (converted to SB syntax)
// Original author: Junior_Djjr
// Expanded by CatchyKetchup
// ---------------------------------------
0000: nop
03A4: script_name 'ENEXTWK'
wait 0
wait 0

int iIconFlagsPtr, iAmmunEnexPtr, i2Floor   // Received from main script, don't change order!
int iIconFlags
int pEnexPool, pEnexArray, i, iCounter
int pEnex, pEnexEnd, iStrLen
float fX, fY, fZ, fDist

if
    iIconFlagsPtr == 0
then
    0A93: terminate_this_custom_script
end

string sAmmun = 'AMMUN4'
if
    i2Floor > 0
then
    sAmmun = 'AMMUN1'
end

// CEntryExitPool* at 0x96A7D8
0A8D: pEnexPool = read_memory 0x96A7D8 size 4 virtual_protect 0

// pool->m_pEntryExitArray (0x0)
0A8D: pEnexArray = read_memory pEnexPool size 4 virtual_protect 0

// pool->m_nEntryExits (0x8)
0D4E: i = read_struct_offset pEnexPool offset 0x8 size 4

// original script workaround (avoid crash)
i -= 1

0EF7: clamp_int i min 0 max 1000 store_to i

// end pointer = array + count * sizeof(CEntryExit)
// sizeof(CEntryExit) = 0x3C
0A90: int_mul pEnexEnd = i * 0x3C
pEnexEnd += pEnexArray

iCounter = 0

{
0x00	8	m_szName
0x08	16	m_recEntrance
0x18	4	m_fEntranceZ
0x1C	4	m_fEntranceAngleRad
0x20	16	m_vecExitPos
0x2C	4	m_fExitAngle
}

for pEnex = pEnexArray to pEnexEnd step 0x3C

    iCounter += 1
    0D4C: iStrLen get_string_length pEnex
    
    if or
        iCounter < 650
        iStrLen > 0
    jf break
    
    if
        iStrLen >= 5
    jf continue
    
    // Bayside Didier Sachs Icon
    if
        0E80: is_string_equal pEnex "CSEXL" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ -2492.166 2363.161 9.273        
        if
            fDist < 10.0
        then
            0652: get_int_stat i = integer_stat 329   // Saint Mark's Bistro passed (CASINO10)
            if
                i > 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 i
            else
                08BD: set_local_var_bit_const iIconFlags bit 5               
                0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            end
        end
        continue
    end
    
    // Secret Valley
    if
        0E80: is_string_equal pEnex "REST2" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords
        
        // Bayside Beach Restaurant Icon       
        050A: fDist = get_distance_between_coords_3d fX fY fZ -2342.350 2532.693 4.915        
        if
            fDist < 10.0
        then
            08BD: set_local_var_bit_const iIconFlags bit 4                
            0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            continue
        end
        
        // Spawning fix at Downtown, SF        
        050A: fDist = get_distance_between_coords_3d fX fY fZ -1887.43 749.592 44.4658        
        if
            fDist < 10.0
        then           
            0E28: write_struct_offset pEnex offset 0x2C size 4 value 90.0
        end
        continue
    end
    
    if
        iStrLen >= 6
    jf continue
    
    // Bayside Burger Shot Icon
    if
        0E80: is_string_equal pEnex "FDBURG" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ -2477.121 2286.480 3.984        
        if
            fDist < 10.0
        then
            0652: get_int_stat i = integer_stat 200   // MealsEaten (not fully accurate but good enough)
            if
                i > 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 i
            else
                08BD: set_local_var_bit_const iIconFlags bit 6                
                0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            end 
        end
        continue
    end
    
    // DINER1 - Montgomery & Las Payasadas Restaurant Icon + Interior spawning fix
    if
        0E80: is_string_equal pEnex "DINER1" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ 1294.688 235.702 18.554 // Montgomery        
        if
            fDist < 10.0
        then
            08BD: set_local_var_bit_const iIconFlags bit 0                
            0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            continue
        end
        050A: fDist = get_distance_between_coords_3d fX fY fZ -254.806 2603.212 61.858 // Las Payasadas       
        if
            fDist < 10.0
        then
            08BD: set_local_var_bit_const iIconFlags bit 1                
            0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            continue
        end
        
        // Interior spawn fix
        050A: fDist = get_distance_between_coords_3d fX fY fZ 460.1 -88.4285 998.622
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x20 size 4 value 458.1
            0E28: write_struct_offset pEnex offset 0x28 size 4 value 999.55
        end
        continue        
    end
    
    // DINER2
    if
        0E80: is_string_equal pEnex "DINER2" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords
        
        // Angel Pine Restaurant Icon       
        050A: fDist = get_distance_between_coords_3d fX fY fZ -2103.958 -2342.111 29.617        
        if
            fDist < 10.0
        then
            08BD: set_local_var_bit_const iIconFlags bit 2                
            0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            continue
        end
                
        // Enex position fix at East Beach     
        050A: fDist = get_distance_between_coords_3d fX fY fZ 2863.23 -1439.44 10.0083        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x08 size 4 value 2858.817
            0E28: write_struct_offset pEnex offset 0x0C size 4 value -1439.796
            0E28: write_struct_offset pEnex offset 0x20 size 4 value 2864.543
            0E28: write_struct_offset pEnex offset 0x24 size 4 value -1440.157
            continue
        end
        
        // Spawning fix at Starfish Casino   
        050A: fDist = get_distance_between_coords_3d fX fY fZ 2368.06 1983.19 10.003        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x20 size 4 value 2365.061
            0E28: write_struct_offset pEnex offset 0x24 size 4 value 1980.872
            0E28: write_struct_offset pEnex offset 0x28 size 4 value 10.81
            continue
        end
        
        // Interior enex position fix
        050A: fDist = get_distance_between_coords_3d fX fY fZ 459.351 -111.005 998.718        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x08 size 4 value 457.571
            0E28: write_struct_offset pEnex offset 0x20 size 4 value 458.969
            0E28: write_struct_offset pEnex offset 0x24 size 4 value -109.184
            0E28: write_struct_offset pEnex offset 0x28 size 4 value 999.55
        end
        continue
    end
    
    // FDREST1
    if
        0E80: is_string_equal pEnex "FDREST1" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords
        
        // Regular Tom Restaurant Icon       
        050A: fDist = get_distance_between_coords_3d fX fY fZ -314.358 1774.541 42.640        
        if
            fDist < 10.0
        then
            08BD: set_local_var_bit_const iIconFlags bit 3                
            0A8C: write_memory iIconFlagsPtr size 4 value iIconFlags virtual_protect 1
            continue
        end
        
        // Rodeo spawn fix
        050A: fDist = get_distance_between_coords_3d fX fY fZ 419.191 -1428.71 31.8959
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x28 size 4 value 32.43
            continue
        end
        
        // Queens spawn fix
        050A: fDist = get_distance_between_coords_3d fX fY fZ -2463.06 132.287 34.198
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x20 size 4 value -2461.275
            0E28: write_struct_offset pEnex offset 0x24 size 4 value 134.084
            0E28: write_struct_offset pEnex offset 0x2C size 4 value 316.239
            continue
        end
        
        // Restaurant interior sky fix       
        050A: fDist = get_distance_between_coords_3d fX fY fZ 452.89 -18.1797 1000.18      
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x33 size 1 value 0
        end
        continue
    end
    
    // BROTHL2  Second room of Strip club double entrance fix
    if
        0E80: is_string_equal pEnex "BROTHL2" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords
        050A: fDist = get_distance_between_coords_3d fX fY fZ 967.107 -53.5055 1000.18       
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x18 size 4 value 990.0
        end        
        continue
    end

    // Ryder's double entrance fix
    if
        0E80: is_string_equal pEnex "RYDERS" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ 2468.29 -1698.1 1012.51        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x18 size 4 value 1000.0
        end
        continue
    end
    
    // Sweet's double entrance fix
    if
        0E80: is_string_equal pEnex "SWEETS" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ 2524.01 -1679.53 1014.5        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x18 size 4 value 1000.0
        end
        continue
    end
    
    // Studio double entrance fix
    if
        0E80: is_string_equal pEnex "STUDIO" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ 1038.51 -3.86375 1000.09       
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x18 size 4 value 990.0
        end
        continue
    end
    
    // Door fix for SF Police department
    if
        0E80: is_string_equal pEnex "POLICE2" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords       
        050A: fDist = get_distance_between_coords_3d fX fY fZ 246.376 107.546 1002.28        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x08 size 4 value 244.872
        end
        continue
    end
    
    // BROTHL1  Wooden brothel interior spawning position & sky fix
    if
        0E80: is_string_equal pEnex "BROTHL1" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords
        050A: fDist = get_distance_between_coords_3d fX fY fZ 940.922 -18.307 1000.18       
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x24 size 4 value -16.307
            0E28: write_struct_offset pEnex offset 0x28 size 4 value 1000.95
            0E28: write_struct_offset pEnex offset 0x33 size 1 value 1
        end        
        continue
    end
            
    // AIRPOR2  Airport interior Z fix
    if
        0E80: is_string_equal pEnex "AIRPOR2" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        gosub @GetEnexCoords        
        050A: fDist = get_distance_between_coords_3d fX fY fZ -1861.68 58.8643 1054.2        
        if
            fDist < 10.0
        then
            0E28: write_struct_offset pEnex offset 0x18 size 4 value 1000.0
        end        
        continue
    end
    
    // Ammu-Nation Z fix
    if
        0E80: is_string_equal pEnex sAmmun max_size 8 case_sensitive 1 ignore_charactere ''
    then
        gosub @GetEnexCoords        
        050A: fDist = get_distance_between_coords_3d fX fY fZ 1368.35 -1279.06 12.55        
        if
            fDist < 10.0
        jf continue
        0E28: write_struct_offset pEnex offset 0x18 size 4 value 1.0
        if and
            i2Floor > 0
            iAmmunEnexPtr <> 0
        jf continue
        0A8C: write_memory iAmmunEnexPtr size 4 value pEnex virtual_protect 1
    end
end

wait 0

// Increase auzo zone boxes

{
+0x00 - 8 Bytes - m_szName[8]
+0x08 - 2 Bytes - m_nAudioZone - Audio-Zone-ID (int16)
+0x0A - 1 Byte - m_IsActive
+0x0B - 1 Byte - Padding
+0x0C - 6 Bytes - m_Box.m_vecMin
+0x12 - 6 Bytes - m_Box.m_vecMax
}

int pAuzo

for pAuzo = 0xB6DCD0 to 0xB6EB88 step 0x18

    // Carl's house - expand to Sweet's and Ryder's
    if
        0E80: is_string_equal pAuzo "Carls" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        0E28: write_struct_offset pAuzo offset 0xC size 2 value 19570
        0E28: write_struct_offset pAuzo offset 0x12 size 2 value 20322
        0E28: write_struct_offset pAuzo offset 0x14 size 2 value 52177
        continue
    end
    
    // seven11 - expand to Angel Pine Trailer
    if
        0E80: is_string_equal pAuzo "seven11" max_size 8 case_sensitive 1 ignore_charactere ""
    then
        0D4E: i = read_struct_offset pAuzo offset 0xC size 2
        if
            i == 65402
        then
            0E28: write_struct_offset pAuzo offset 0x10 size 2 value 7976
            0E28: write_struct_offset pAuzo offset 0x14 size 2 value 65
        end
    end
end

wait 0

// Fix mirrors

int pMirror, cX, cY, v1Y, v2X

for pMirror = 0xC815C0 to 0xC81C68 step 0x18

    0D4E: cX = read_struct_offset pMirror offset 0x0 size 2
    0D4E: cY = read_struct_offset pMirror offset 0x2 size 2
    0D4E: v1Y = read_struct_offset pMirror offset 0x6 size 2
    0D4E: v2X = read_struct_offset pMirror offset 0x8 size 2

    // Sweet's House
    if and
        cX == 2520
        cY == 63852
        v1Y == 15
        v2X == 18
    then
        0E28: write_struct_offset pMirror offset 0x0 size 2 value 2519
        0E28: write_struct_offset pMirror offset 0x2 size 2 value 63851
        0E28: write_struct_offset pMirror offset 0x6 size 2 value 17
        0E28: write_struct_offset pMirror offset 0x8 size 2 value 20
        continue
    end

    // Fanny Batter's
    if and
        cX == 742
        cY == 1436
        v1Y == 7
        v2X == 20
    then
        0E28: write_struct_offset pMirror offset 0x0 size 2 value 740
        0E28: write_struct_offset pMirror offset 0x2 size 2 value 1434
        0E28: write_struct_offset pMirror offset 0x6 size 2 value 11
        0E28: write_struct_offset pMirror offset 0x8 size 2 value 24
        0E28: write_struct_offset pMirror offset 0xE size 2 value 1108
    end
end

0A93: terminate_this_custom_script


:GetEnexCoords
0EE2: read_struct_offset_multi pEnex offset 0x08 total 2 size 4 fX fY
0D4E: fZ = read_struct_offset pEnex offset 0x18 size 4
return


0A93: terminate_this_custom_script