def link_inverted_entrances(world, player):
    Dungeon_Exits = Inverted_Dungeon_Exits_Base.copy()
    Cave_Exits = Cave_Exits_Base.copy()
    Old_Man_House = Old_Man_House_Base.copy()
    Cave_Three_Exits = Cave_Three_Exits_Base.copy()
    unbias_some_entrances(Dungeon_Exits, Cave_Exits, Old_Man_House, Cave_Three_Exits)
    for (exitname, regionname) in inverted_mandatory_connections:
        connect_simple(world, exitname, regionname, player)
    if (world.shuffle[player] == 'vanilla'):
        for (exitname, regionname) in inverted_default_connections:
            connect_simple(world, exitname, regionname, player)
        for (exitname, regionname) in inverted_default_dungeon_connections:
            connect_simple(world, exitname, regionname, player)
    elif (world.shuffle[player] == 'dungeonssimple'):
        for (exitname, regionname) in inverted_default_connections:
            connect_simple(world, exitname, regionname, player)
        simple_shuffle_dungeons(world, player)
    elif (world.shuffle[player] == 'dungeonsfull'):
        for (exitname, regionname) in inverted_default_connections:
            connect_simple(world, exitname, regionname, player)
        skull_woods_shuffle(world, player)
        dungeon_exits = list(Dungeon_Exits)
        lw_entrances = list(Inverted_LW_Dungeon_Entrances)
        lw_dungeon_entrances_must_exit = list(Inverted_LW_Dungeon_Entrances_Must_Exit)
        dw_entrances = list(Inverted_DW_Dungeon_Entrances)
        if (random.randint(0, 1) == 0):
            lw_dungeon_entrances_must_exit.append('Desert Palace Entrance (North)')
            dp_must_exit = 'Desert Palace Entrance (North)'
            lw_entrances.append('Desert Palace Entrance (West)')
        else:
            lw_dungeon_entrances_must_exit.append('Desert Palace Entrance (West)')
            dp_must_exit = 'Desert Palace Entrance (West)'
            lw_entrances.append('Desert Palace Entrance (North)')
        dungeon_exits.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        lw_entrances.append('Hyrule Castle Entrance (South)')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)']
        else:
            lw_entrances.append('Inverted Ganons Tower')
            dungeon_exits.append('Inverted Ganons Tower Exit')
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)', 'Inverted Ganons Tower']
        all_entrances_aga = (lw_entrances + dw_entrances)
        aga_doors = [i for i in all_entrances_aga]
        random.shuffle(aga_doors)
        aga_door = aga_doors.pop()
        if (aga_door in hc_ledge_entrances):
            lw_entrances.remove(aga_door)
            hc_ledge_entrances.remove(aga_door)
            random.shuffle(hc_ledge_entrances)
            hc_ledge_must_exit = hc_ledge_entrances.pop()
            lw_entrances.remove(hc_ledge_must_exit)
            lw_dungeon_entrances_must_exit.append(hc_ledge_must_exit)
        if (aga_door in lw_entrances):
            lw_entrances.remove(aga_door)
        elif (aga_door in dw_entrances):
            dw_entrances.remove(aga_door)
        connect_two_way(world, aga_door, 'Inverted Agahnims Tower Exit', player)
        dungeon_exits.remove('Inverted Agahnims Tower Exit')
        all_dungeon_entrances = (dw_entrances + lw_entrances)
        connect_mandatory_exits(world, all_dungeon_entrances, dungeon_exits, lw_dungeon_entrances_must_exit, player, dp_must_exit)
        remaining_dw_entrances = [i for i in all_dungeon_entrances if (i in dw_entrances)]
        remaining_lw_entrances = [i for i in all_dungeon_entrances if (i in lw_entrances)]
        connect_caves(world, remaining_lw_entrances, remaining_dw_entrances, dungeon_exits, player)
    elif (world.shuffle[player] == 'simple'):
        simple_shuffle_dungeons(world, player)
        old_man_entrances = list(Inverted_Old_Man_Entrances)
        caves = list(Cave_Exits)
        three_exit_caves = list(Cave_Three_Exits)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Inverted_Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Inverted_Single_Cave_Targets)
        two_door_caves = list(Inverted_Two_Door_Caves_Directional)
        random.shuffle(two_door_caves)
        random.shuffle(caves)
        while two_door_caves:
            (entrance1, entrance2) = two_door_caves.pop()
            (exit1, exit2) = caves.pop()
            connect_two_way(world, entrance1, exit1, player)
            connect_two_way(world, entrance2, exit2, player)
        two_door_caves = list(Inverted_Two_Door_Caves)
        random.shuffle(two_door_caves)
        while two_door_caves:
            (entrance1, entrance2) = two_door_caves.pop()
            (exit1, exit2) = caves.pop()
            connect_two_way(world, entrance1, exit1, player)
            connect_two_way(world, entrance2, exit2, player)
        links_house_doors = [i for i in (bomb_shop_doors + blacksmith_doors) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in bomb_shop_doors):
            bomb_shop_doors.remove(links_house)
        if (links_house in blacksmith_doors):
            blacksmith_doors.remove(links_house)
        if (links_house in old_man_entrances):
            old_man_entrances.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in bomb_shop_doors)]
        sanc_door = random.choice(sanc_doors)
        bomb_shop_doors.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        lw_dm_entrances = ['Paradox Cave (Bottom)', 'Paradox Cave (Middle)', 'Paradox Cave (Top)', 'Old Man House (Bottom)', 'Fairy Ascension Cave (Bottom)', 'Fairy Ascension Cave (Top)', 'Spiral Cave (Bottom)', 'Old Man Cave (East)', 'Death Mountain Return Cave (East)', 'Spiral Cave', 'Old Man House (Top)', 'Spectacle Rock Cave', 'Spectacle Rock Cave Peak', 'Spectacle Rock Cave (Bottom)']
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, 'Bumper Cave (Bottom)', 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        if (old_man_exit == 'Spike Cave'):
            bomb_shop_doors.remove('Spike Cave')
            bomb_shop_doors.extend(old_man_entrances)
        caves.extend(list(Old_Man_House))
        caves.extend(list(three_exit_caves))
        connect_caves(world, lw_dm_entrances, [], caves, player)
        scramble_inverted_holes(world, player)
        blacksmith_doors = [door for door in blacksmith_doors[:]]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        bomb_shop_doors = [door for door in bomb_shop_doors[:]]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        single_doors.extend(bomb_shop_doors)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_doors(world, single_doors, door_targets, player)
    elif (world.shuffle[player] == 'restricted'):
        simple_shuffle_dungeons(world, player)
        lw_entrances = list((Inverted_LW_Entrances + Inverted_LW_Single_Cave_Doors))
        dw_entrances = list(((Inverted_DW_Entrances + Inverted_DW_Single_Cave_Doors) + Inverted_Old_Man_Entrances))
        lw_must_exits = list(Inverted_LW_Entrances_Must_Exit)
        old_man_entrances = list(Inverted_Old_Man_Entrances)
        caves = list(((Cave_Exits + Cave_Three_Exits) + Old_Man_House))
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        links_house_doors = [i for i in ((lw_entrances + dw_entrances) + lw_must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in lw_entrances):
            lw_entrances.remove(links_house)
        elif (links_house in dw_entrances):
            dw_entrances.remove(links_house)
        elif (links_house in lw_must_exits):
            lw_must_exits.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in dw_entrances)]
        sanc_door = random.choice(sanc_doors)
        dw_entrances.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
        old_man_entrances = [door for door in old_man_entrances if (door in dw_entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        dw_entrances.remove(old_man_exit)
        all_entrances = (lw_entrances + dw_entrances)
        blacksmith_doors = [door for door in blacksmith_doors if (door in all_entrances)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        if (blacksmith_hut in lw_entrances):
            lw_entrances.remove(blacksmith_hut)
        if (blacksmith_hut in dw_entrances):
            dw_entrances.remove(blacksmith_hut)
        bomb_shop_doors.extend(blacksmith_doors)
        all_entrances = (lw_entrances + dw_entrances)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in all_entrances)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        if (bomb_shop in lw_entrances):
            lw_entrances.remove(bomb_shop)
        if (bomb_shop in dw_entrances):
            dw_entrances.remove(bomb_shop)
        random.shuffle(dw_entrances)
        old_man_entrance = dw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_inverted_holes(world, player)
        doors = (lw_entrances + dw_entrances)
        connect_doors(world, doors, door_targets, player)
    elif (world.shuffle[player] == 'full'):
        skull_woods_shuffle(world, player)
        lw_entrances = list(((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_LW_Single_Cave_Doors))
        dw_entrances = list((((Inverted_DW_Entrances + Inverted_DW_Dungeon_Entrances) + Inverted_DW_Single_Cave_Doors) + Inverted_Old_Man_Entrances))
        lw_must_exits = list((Inverted_LW_Dungeon_Entrances_Must_Exit + Inverted_LW_Entrances_Must_Exit))
        old_man_entrances = list(((Inverted_Old_Man_Entrances + Old_Man_Entrances) + ['Inverted Agahnims Tower', 'Tower of Hera']))
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        bomb_shop_doors = list((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        old_man_house = list(Old_Man_House)
        if (random.randint(0, 1) == 0):
            lw_must_exits.append('Desert Palace Entrance (North)')
            dp_must_exit = 'Desert Palace Entrance (North)'
            lw_entrances.append('Desert Palace Entrance (West)')
        else:
            lw_must_exits.append('Desert Palace Entrance (West)')
            dp_must_exit = 'Desert Palace Entrance (West)'
            lw_entrances.append('Desert Palace Entrance (North)')
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        caves.append(tuple(random.sample(['Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'], 3)))
        lw_entrances.append('Hyrule Castle Entrance (South)')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)']
        else:
            lw_entrances.append('Inverted Ganons Tower')
            caves.append('Inverted Ganons Tower Exit')
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)', 'Inverted Ganons Tower']
        all_entrances_aga = (lw_entrances + dw_entrances)
        aga_doors = [i for i in all_entrances_aga]
        random.shuffle(aga_doors)
        aga_door = aga_doors.pop()
        if (aga_door in hc_ledge_entrances):
            lw_entrances.remove(aga_door)
            hc_ledge_entrances.remove(aga_door)
            random.shuffle(hc_ledge_entrances)
            hc_ledge_must_exit = hc_ledge_entrances.pop()
            lw_entrances.remove(hc_ledge_must_exit)
            lw_must_exits.append(hc_ledge_must_exit)
        if (aga_door in lw_entrances):
            lw_entrances.remove(aga_door)
        elif (aga_door in dw_entrances):
            dw_entrances.remove(aga_door)
        connect_two_way(world, aga_door, 'Inverted Agahnims Tower Exit', player)
        caves.remove('Inverted Agahnims Tower Exit')
        links_house_doors = [i for i in ((lw_entrances + dw_entrances) + lw_must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in lw_entrances):
            lw_entrances.remove(links_house)
        if (links_house in dw_entrances):
            dw_entrances.remove(links_house)
        if (links_house in lw_must_exits):
            lw_must_exits.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in dw_entrances)]
        sanc_door = random.choice(sanc_doors)
        dw_entrances.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        if (random.randint(0, 1) == 0):
            caves += old_man_house
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player, dp_must_exit)
            try:
                caves.remove(old_man_house[0])
            except ValueError:
                pass
            else:
                connect_caves(world, lw_entrances, [], old_man_house, player)
        else:
            connect_caves(world, dw_entrances, [], old_man_house, player)
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player, dp_must_exit)
        old_man_entrances = [door for door in old_man_entrances if (door in (dw_entrances + lw_entrances))]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        if (old_man_exit in dw_entrances):
            dw_entrances.remove(old_man_exit)
            old_man_world = 'dark'
        elif (old_man_exit in lw_entrances):
            lw_entrances.remove(old_man_exit)
            old_man_world = 'light'
        all_entrances = (lw_entrances + dw_entrances)
        blacksmith_doors = [door for door in blacksmith_doors if (door in all_entrances)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        if (blacksmith_hut in lw_entrances):
            lw_entrances.remove(blacksmith_hut)
        if (blacksmith_hut in dw_entrances):
            dw_entrances.remove(blacksmith_hut)
        bomb_shop_doors.extend(blacksmith_doors)
        all_entrances = (lw_entrances + dw_entrances)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in all_entrances)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        if (bomb_shop in lw_entrances):
            lw_entrances.remove(bomb_shop)
        if (bomb_shop in dw_entrances):
            dw_entrances.remove(bomb_shop)
        if (old_man_world == 'light'):
            random.shuffle(lw_entrances)
            old_man_entrance = lw_entrances.pop()
            connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        elif (old_man_world == 'dark'):
            random.shuffle(dw_entrances)
            old_man_entrance = dw_entrances.pop()
            connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_inverted_holes(world, player)
        doors = (lw_entrances + dw_entrances)
        connect_doors(world, doors, door_targets, player)
    elif (world.shuffle[player] == 'crossed'):
        skull_woods_shuffle(world, player)
        entrances = list(((((((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_LW_Single_Cave_Doors) + Inverted_Old_Man_Entrances) + Inverted_DW_Entrances) + Inverted_DW_Dungeon_Entrances) + Inverted_DW_Single_Cave_Doors))
        must_exits = list((Inverted_LW_Entrances_Must_Exit + Inverted_LW_Dungeon_Entrances_Must_Exit))
        old_man_entrances = list(((Inverted_Old_Man_Entrances + Old_Man_Entrances) + ['Inverted Agahnims Tower', 'Tower of Hera']))
        caves = list((((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits) + Old_Man_House))
        bomb_shop_doors = list((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        if (random.randint(0, 1) == 0):
            must_exits.append('Desert Palace Entrance (North)')
            dp_must_exit = 'Desert Palace Entrance (North)'
            entrances.append('Desert Palace Entrance (West)')
        else:
            must_exits.append('Desert Palace Entrance (West)')
            dp_must_exit = 'Desert Palace Entrance (West)'
            entrances.append('Desert Palace Entrance (North)')
        caves.append(tuple(random.sample(['Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'], 3)))
        entrances.append('Hyrule Castle Entrance (South)')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)']
        else:
            entrances.append('Inverted Ganons Tower')
            caves.append('Inverted Ganons Tower Exit')
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)', 'Inverted Ganons Tower']
        aga_door = random.choice(list(entrances))
        if (aga_door in hc_ledge_entrances):
            hc_ledge_entrances.remove(aga_door)
            random.shuffle(hc_ledge_entrances)
            hc_ledge_must_exit = hc_ledge_entrances.pop()
            entrances.remove(hc_ledge_must_exit)
            must_exits.append(hc_ledge_must_exit)
        entrances.remove(aga_door)
        connect_two_way(world, aga_door, 'Inverted Agahnims Tower Exit', player)
        caves.remove('Inverted Agahnims Tower Exit')
        links_house_doors = [i for i in (entrances + must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in entrances):
            entrances.remove(links_house)
        elif (links_house in must_exits):
            must_exits.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in entrances)]
        sanc_door = random.choice(sanc_doors)
        entrances.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_mandatory_exits(world, entrances, caves, must_exits, player, dp_must_exit)
        old_man_entrances = [door for door in old_man_entrances if (door in entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        entrances.remove(old_man_exit)
        blacksmith_doors = [door for door in blacksmith_doors if (door in entrances)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        entrances.remove(blacksmith_hut)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in entrances)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        entrances.remove(bomb_shop)
        random.shuffle(entrances)
        old_man_entrance = entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, entrances, [], caves, player)
        scramble_inverted_holes(world, player)
        connect_doors(world, entrances, door_targets, player)
    elif (world.shuffle[player] == 'insanity'):
        entrances = ((((((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_DW_Entrances) + Inverted_DW_Dungeon_Entrances) + Inverted_Old_Man_Entrances) + Old_Man_Entrances) + ['Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)', 'Skull Woods First Section Door', 'Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave', 'Hyrule Castle Entrance (South)'])
        entrances_must_exits = (Inverted_LW_Entrances_Must_Exit + Inverted_LW_Dungeon_Entrances_Must_Exit)
        doors = (((((((((((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_LW_Entrances_Must_Exit) + Inverted_LW_Dungeon_Entrances_Must_Exit) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave', 'Hyrule Castle Secret Entrance Stairs']) + Inverted_Old_Man_Entrances) + Inverted_DW_Entrances) + Inverted_DW_Dungeon_Entrances) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)']) + Inverted_LW_Single_Cave_Doors) + Inverted_DW_Single_Cave_Doors) + ['Desert Palace Entrance (West)', 'Desert Palace Entrance (North)'])
        if (random.randint(0, 1) == 0):
            entrances_must_exits.append('Desert Palace Entrance (North)')
            entrances.append('Desert Palace Entrance (West)')
        else:
            entrances_must_exits.append('Desert Palace Entrance (West)')
            entrances.append('Desert Palace Entrance (North)')
        bomb_shop_doors = list(((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors) + ['Turtle Rock Isolated Ledge Entrance', 'Bumper Cave (Top)', 'Hookshot Cave Back Entrance']))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        random.shuffle(doors)
        old_man_entrances = (list((Inverted_Old_Man_Entrances + Old_Man_Entrances)) + ['Tower of Hera', 'Inverted Agahnims Tower'])
        caves = (((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits) + ['Old Man House Exit (Bottom)', 'Old Man House Exit (Top)', 'Skull Woods First Section Exit', 'Skull Woods Second Section Exit (East)', 'Skull Woods Second Section Exit (West)', 'Kakariko Well Exit', 'Bat Cave Exit', 'North Fairy Cave Exit', 'Lost Woods Hideout Exit', 'Lumberjack Tree Exit', 'Sanctuary Exit'])
        hole_entrances = ['Kakariko Well Drop', 'Bat Cave Drop', 'North Fairy Cave Drop', 'Lost Woods Hideout Drop', 'Lumberjack Tree Tree', 'Sanctuary Grave', 'Skull Woods First Section Hole (East)', 'Skull Woods First Section Hole (West)', 'Skull Woods First Section Hole (North)', 'Skull Woods Second Section Hole']
        hole_targets = ['Kakariko Well (top)', 'Bat Cave (right)', 'North Fairy Cave', 'Lost Woods Hideout (top)', 'Lumberjack Tree (top)', 'Sewer Drop', 'Skull Back Drop', 'Skull Left Drop', 'Skull Pinball', 'Skull Pot Circle']
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        hole_entrances.append('Hyrule Castle Secret Entrance Drop')
        hole_targets.append('Hyrule Castle Secret Entrance')
        entrances.append('Hyrule Castle Secret Entrance Stairs')
        caves.append('Hyrule Castle Secret Entrance Exit')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            connect_two_way(world, 'Inverted Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Inverted Pyramid Hole', 'Pyramid', player)
        else:
            entrances.append('Inverted Ganons Tower')
            caves.extend(['Inverted Ganons Tower Exit', 'Pyramid Exit'])
            hole_entrances.append('Inverted Pyramid Hole')
            hole_targets.append('Pyramid')
            doors.extend(['Inverted Ganons Tower', 'Inverted Pyramid Entrance'])
        random.shuffle(hole_entrances)
        random.shuffle(hole_targets)
        random.shuffle(entrances)
        for hole in hole_entrances:
            connect_entrance(world, hole, hole_targets.pop(), player)
        doors.append('Hyrule Castle Entrance (South)')
        caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        links_house_doors = [i for i in (entrances + entrances_must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in entrances):
            entrances.remove(links_house)
        elif (links_house in entrances_must_exits):
            entrances_must_exits.remove(links_house)
        doors.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in entrances)]
        sanc_door = random.choice(sanc_doors)
        entrances.remove(sanc_door)
        doors.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)

        def extract_reachable_exit(cavelist):
            random.shuffle(cavelist)
            candidate = None
            for cave in cavelist:
                if (isinstance(cave, tuple) and (len(cave) > 1)):
                    if ((cave[0] in ['Turtle Rock Exit (Front)', 'Spectacle Rock Cave Exit (Peak)']) and (len(cave) == 2)):
                        continue
                    candidate = cave
                    break
            if (candidate is None):
                raise RuntimeError('No suitable cave.')
            cavelist.remove(candidate)
            return candidate

        def connect_reachable_exit(entrance, caves, doors):
            cave = extract_reachable_exit(caves)
            exit = cave[(- 1)]
            cave = cave[:(- 1)]
            connect_exit(world, exit, entrance, player)
            connect_entrance(world, doors.pop(), exit, player)
            caves.append(cave)
        for entrance in entrances_must_exits:
            connect_reachable_exit(entrance, caves, doors)
        old_man_entrances = [entrance for entrance in old_man_entrances if (entrance in entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        entrances.remove(old_man_exit)
        connect_exit(world, 'Old Man Cave Exit (East)', old_man_exit, player)
        connect_entrance(world, doors.pop(), 'Old Man Cave Exit (East)', player)
        caves.append('Old Man Cave Exit (West)')
        blacksmith_doors = [door for door in blacksmith_doors if (door in doors)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        doors.remove(blacksmith_hut)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in doors)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        doors.remove(bomb_shop)
        for cave in caves:
            if isinstance(cave, str):
                cave = (cave,)
            for exit in cave:
                connect_exit(world, exit, entrances.pop(), player)
                connect_entrance(world, doors.pop(), exit, player)
        connect_doors(world, doors, door_targets, player)
    else:
        raise NotImplementedError('Shuffling not supported yet')
    if ((world.get_entrance('Dam', player).connected_region.name != 'Dam') or (world.get_entrance('Swamp Palace', player).connected_region.name != 'Swamp Lobby')):
        world.swamp_patch_required[player] = True
    if (world.get_entrance('Potion Shop', player).connected_region.name != 'Potion Shop'):
        world.powder_patch_required[player] = True
    if (world.get_entrance('Inverted Pyramid Hole', player).connected_region.name != 'Pyramid'):
        world.ganon_at_pyramid[player] = False
    if (world.get_entrance('Inverted Ganons Tower', player).connected_region.name != 'GT Lobby'):
        world.ganonstower_vanilla[player] = False
def link_inverted_entrances(world, player):
    Dungeon_Exits = Inverted_Dungeon_Exits_Base.copy()
    Cave_Exits = Cave_Exits_Base.copy()
    Old_Man_House = Old_Man_House_Base.copy()
    Cave_Three_Exits = Cave_Three_Exits_Base.copy()
    unbias_some_entrances(Dungeon_Exits, Cave_Exits, Old_Man_House, Cave_Three_Exits)
    for (exitname, regionname) in inverted_mandatory_connections:
        connect_simple(world, exitname, regionname, player)
    if (world.shuffle[player] == 'vanilla'):
        for (exitname, regionname) in inverted_default_connections:
            connect_simple(world, exitname, regionname, player)
        for (exitname, regionname) in inverted_default_dungeon_connections:
            connect_simple(world, exitname, regionname, player)
    elif (world.shuffle[player] == 'dungeonssimple'):
        for (exitname, regionname) in inverted_default_connections:
            connect_simple(world, exitname, regionname, player)
        simple_shuffle_dungeons(world, player)
    elif (world.shuffle[player] == 'dungeonsfull'):
        for (exitname, regionname) in inverted_default_connections:
            connect_simple(world, exitname, regionname, player)
        skull_woods_shuffle(world, player)
        dungeon_exits = list(Dungeon_Exits)
        lw_entrances = list(Inverted_LW_Dungeon_Entrances)
        lw_dungeon_entrances_must_exit = list(Inverted_LW_Dungeon_Entrances_Must_Exit)
        dw_entrances = list(Inverted_DW_Dungeon_Entrances)
        if (random.randint(0, 1) == 0):
            lw_dungeon_entrances_must_exit.append('Desert Palace Entrance (North)')
            dp_must_exit = 'Desert Palace Entrance (North)'
            lw_entrances.append('Desert Palace Entrance (West)')
        else:
            lw_dungeon_entrances_must_exit.append('Desert Palace Entrance (West)')
            dp_must_exit = 'Desert Palace Entrance (West)'
            lw_entrances.append('Desert Palace Entrance (North)')
        dungeon_exits.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        lw_entrances.append('Hyrule Castle Entrance (South)')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)']
        else:
            lw_entrances.append('Inverted Ganons Tower')
            dungeon_exits.append('Inverted Ganons Tower Exit')
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)', 'Inverted Ganons Tower']
        all_entrances_aga = (lw_entrances + dw_entrances)
        aga_doors = [i for i in all_entrances_aga]
        random.shuffle(aga_doors)
        aga_door = aga_doors.pop()
        if (aga_door in hc_ledge_entrances):
            lw_entrances.remove(aga_door)
            hc_ledge_entrances.remove(aga_door)
            random.shuffle(hc_ledge_entrances)
            hc_ledge_must_exit = hc_ledge_entrances.pop()
            lw_entrances.remove(hc_ledge_must_exit)
            lw_dungeon_entrances_must_exit.append(hc_ledge_must_exit)
        if (aga_door in lw_entrances):
            lw_entrances.remove(aga_door)
        elif (aga_door in dw_entrances):
            dw_entrances.remove(aga_door)
        connect_two_way(world, aga_door, 'Inverted Agahnims Tower Exit', player)
        dungeon_exits.remove('Inverted Agahnims Tower Exit')
        all_dungeon_entrances = (dw_entrances + lw_entrances)
        connect_mandatory_exits(world, all_dungeon_entrances, dungeon_exits, lw_dungeon_entrances_must_exit, player, dp_must_exit)
        remaining_dw_entrances = [i for i in all_dungeon_entrances if (i in dw_entrances)]
        remaining_lw_entrances = [i for i in all_dungeon_entrances if (i in lw_entrances)]
        connect_caves(world, remaining_lw_entrances, remaining_dw_entrances, dungeon_exits, player)
    elif (world.shuffle[player] == 'simple'):
        simple_shuffle_dungeons(world, player)
        old_man_entrances = list(Inverted_Old_Man_Entrances)
        caves = list(Cave_Exits)
        three_exit_caves = list(Cave_Three_Exits)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Inverted_Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Inverted_Single_Cave_Targets)
        two_door_caves = list(Inverted_Two_Door_Caves_Directional)
        random.shuffle(two_door_caves)
        random.shuffle(caves)
        while two_door_caves:
            (entrance1, entrance2) = two_door_caves.pop()
            (exit1, exit2) = caves.pop()
            connect_two_way(world, entrance1, exit1, player)
            connect_two_way(world, entrance2, exit2, player)
        two_door_caves = list(Inverted_Two_Door_Caves)
        random.shuffle(two_door_caves)
        while two_door_caves:
            (entrance1, entrance2) = two_door_caves.pop()
            (exit1, exit2) = caves.pop()
            connect_two_way(world, entrance1, exit1, player)
            connect_two_way(world, entrance2, exit2, player)
        links_house_doors = [i for i in (bomb_shop_doors + blacksmith_doors) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in bomb_shop_doors):
            bomb_shop_doors.remove(links_house)
        if (links_house in blacksmith_doors):
            blacksmith_doors.remove(links_house)
        if (links_house in old_man_entrances):
            old_man_entrances.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in bomb_shop_doors)]
        sanc_door = random.choice(sanc_doors)
        bomb_shop_doors.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        lw_dm_entrances = ['Paradox Cave (Bottom)', 'Paradox Cave (Middle)', 'Paradox Cave (Top)', 'Old Man House (Bottom)', 'Fairy Ascension Cave (Bottom)', 'Fairy Ascension Cave (Top)', 'Spiral Cave (Bottom)', 'Old Man Cave (East)', 'Death Mountain Return Cave (East)', 'Spiral Cave', 'Old Man House (Top)', 'Spectacle Rock Cave', 'Spectacle Rock Cave Peak', 'Spectacle Rock Cave (Bottom)']
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, 'Bumper Cave (Bottom)', 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        if (old_man_exit == 'Spike Cave'):
            bomb_shop_doors.remove('Spike Cave')
            bomb_shop_doors.extend(old_man_entrances)
        caves.extend(list(Old_Man_House))
        caves.extend(list(three_exit_caves))
        connect_caves(world, lw_dm_entrances, [], caves, player)
        scramble_inverted_holes(world, player)
        blacksmith_doors = [door for door in blacksmith_doors[:]]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        bomb_shop_doors = [door for door in bomb_shop_doors[:]]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        single_doors.extend(bomb_shop_doors)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_doors(world, single_doors, door_targets, player)
    elif (world.shuffle[player] == 'restricted'):
        simple_shuffle_dungeons(world, player)
        lw_entrances = list((Inverted_LW_Entrances + Inverted_LW_Single_Cave_Doors))
        dw_entrances = list(((Inverted_DW_Entrances + Inverted_DW_Single_Cave_Doors) + Inverted_Old_Man_Entrances))
        lw_must_exits = list(Inverted_LW_Entrances_Must_Exit)
        old_man_entrances = list(Inverted_Old_Man_Entrances)
        caves = list(((Cave_Exits + Cave_Three_Exits) + Old_Man_House))
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        links_house_doors = [i for i in ((lw_entrances + dw_entrances) + lw_must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in lw_entrances):
            lw_entrances.remove(links_house)
        elif (links_house in dw_entrances):
            dw_entrances.remove(links_house)
        elif (links_house in lw_must_exits):
            lw_must_exits.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in dw_entrances)]
        sanc_door = random.choice(sanc_doors)
        dw_entrances.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
        old_man_entrances = [door for door in old_man_entrances if (door in dw_entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        dw_entrances.remove(old_man_exit)
        all_entrances = (lw_entrances + dw_entrances)
        blacksmith_doors = [door for door in blacksmith_doors if (door in all_entrances)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        if (blacksmith_hut in lw_entrances):
            lw_entrances.remove(blacksmith_hut)
        if (blacksmith_hut in dw_entrances):
            dw_entrances.remove(blacksmith_hut)
        bomb_shop_doors.extend(blacksmith_doors)
        all_entrances = (lw_entrances + dw_entrances)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in all_entrances)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        if (bomb_shop in lw_entrances):
            lw_entrances.remove(bomb_shop)
        if (bomb_shop in dw_entrances):
            dw_entrances.remove(bomb_shop)
        random.shuffle(dw_entrances)
        old_man_entrance = dw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_inverted_holes(world, player)
        doors = (lw_entrances + dw_entrances)
        connect_doors(world, doors, door_targets, player)
    elif (world.shuffle[player] == 'full'):
        skull_woods_shuffle(world, player)
        lw_entrances = list(((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_LW_Single_Cave_Doors))
        dw_entrances = list((((Inverted_DW_Entrances + Inverted_DW_Dungeon_Entrances) + Inverted_DW_Single_Cave_Doors) + Inverted_Old_Man_Entrances))
        lw_must_exits = list((Inverted_LW_Dungeon_Entrances_Must_Exit + Inverted_LW_Entrances_Must_Exit))
        old_man_entrances = list(((Inverted_Old_Man_Entrances + Old_Man_Entrances) + ['Inverted Agahnims Tower', 'Tower of Hera']))
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        bomb_shop_doors = list((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        old_man_house = list(Old_Man_House)
        if (random.randint(0, 1) == 0):
            lw_must_exits.append('Desert Palace Entrance (North)')
            dp_must_exit = 'Desert Palace Entrance (North)'
            lw_entrances.append('Desert Palace Entrance (West)')
        else:
            lw_must_exits.append('Desert Palace Entrance (West)')
            dp_must_exit = 'Desert Palace Entrance (West)'
            lw_entrances.append('Desert Palace Entrance (North)')
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        caves.append(tuple(random.sample(['Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'], 3)))
        lw_entrances.append('Hyrule Castle Entrance (South)')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)']
        else:
            lw_entrances.append('Inverted Ganons Tower')
            caves.append('Inverted Ganons Tower Exit')
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)', 'Inverted Ganons Tower']
        all_entrances_aga = (lw_entrances + dw_entrances)
        aga_doors = [i for i in all_entrances_aga]
        random.shuffle(aga_doors)
        aga_door = aga_doors.pop()
        if (aga_door in hc_ledge_entrances):
            lw_entrances.remove(aga_door)
            hc_ledge_entrances.remove(aga_door)
            random.shuffle(hc_ledge_entrances)
            hc_ledge_must_exit = hc_ledge_entrances.pop()
            lw_entrances.remove(hc_ledge_must_exit)
            lw_must_exits.append(hc_ledge_must_exit)
        if (aga_door in lw_entrances):
            lw_entrances.remove(aga_door)
        elif (aga_door in dw_entrances):
            dw_entrances.remove(aga_door)
        connect_two_way(world, aga_door, 'Inverted Agahnims Tower Exit', player)
        caves.remove('Inverted Agahnims Tower Exit')
        links_house_doors = [i for i in ((lw_entrances + dw_entrances) + lw_must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in lw_entrances):
            lw_entrances.remove(links_house)
        if (links_house in dw_entrances):
            dw_entrances.remove(links_house)
        if (links_house in lw_must_exits):
            lw_must_exits.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in dw_entrances)]
        sanc_door = random.choice(sanc_doors)
        dw_entrances.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        if (random.randint(0, 1) == 0):
            caves += old_man_house
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player, dp_must_exit)
            try:
                caves.remove(old_man_house[0])
            except ValueError:
                pass
            else:
                connect_caves(world, lw_entrances, [], old_man_house, player)
        else:
            connect_caves(world, dw_entrances, [], old_man_house, player)
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player, dp_must_exit)
        old_man_entrances = [door for door in old_man_entrances if (door in (dw_entrances + lw_entrances))]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        if (old_man_exit in dw_entrances):
            dw_entrances.remove(old_man_exit)
            old_man_world = 'dark'
        elif (old_man_exit in lw_entrances):
            lw_entrances.remove(old_man_exit)
            old_man_world = 'light'
        all_entrances = (lw_entrances + dw_entrances)
        blacksmith_doors = [door for door in blacksmith_doors if (door in all_entrances)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        if (blacksmith_hut in lw_entrances):
            lw_entrances.remove(blacksmith_hut)
        if (blacksmith_hut in dw_entrances):
            dw_entrances.remove(blacksmith_hut)
        bomb_shop_doors.extend(blacksmith_doors)
        all_entrances = (lw_entrances + dw_entrances)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in all_entrances)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        if (bomb_shop in lw_entrances):
            lw_entrances.remove(bomb_shop)
        if (bomb_shop in dw_entrances):
            dw_entrances.remove(bomb_shop)
        if (old_man_world == 'light'):
            random.shuffle(lw_entrances)
            old_man_entrance = lw_entrances.pop()
            connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        elif (old_man_world == 'dark'):
            random.shuffle(dw_entrances)
            old_man_entrance = dw_entrances.pop()
            connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_inverted_holes(world, player)
        doors = (lw_entrances + dw_entrances)
        connect_doors(world, doors, door_targets, player)
    elif (world.shuffle[player] == 'crossed'):
        skull_woods_shuffle(world, player)
        entrances = list(((((((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_LW_Single_Cave_Doors) + Inverted_Old_Man_Entrances) + Inverted_DW_Entrances) + Inverted_DW_Dungeon_Entrances) + Inverted_DW_Single_Cave_Doors))
        must_exits = list((Inverted_LW_Entrances_Must_Exit + Inverted_LW_Dungeon_Entrances_Must_Exit))
        old_man_entrances = list(((Inverted_Old_Man_Entrances + Old_Man_Entrances) + ['Inverted Agahnims Tower', 'Tower of Hera']))
        caves = list((((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits) + Old_Man_House))
        bomb_shop_doors = list((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        if (random.randint(0, 1) == 0):
            must_exits.append('Desert Palace Entrance (North)')
            dp_must_exit = 'Desert Palace Entrance (North)'
            entrances.append('Desert Palace Entrance (West)')
        else:
            must_exits.append('Desert Palace Entrance (West)')
            dp_must_exit = 'Desert Palace Entrance (West)'
            entrances.append('Desert Palace Entrance (North)')
        caves.append(tuple(random.sample(['Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'], 3)))
        entrances.append('Hyrule Castle Entrance (South)')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)']
        else:
            entrances.append('Inverted Ganons Tower')
            caves.append('Inverted Ganons Tower Exit')
            hc_ledge_entrances = ['Hyrule Castle Entrance (West)', 'Hyrule Castle Entrance (East)', 'Inverted Ganons Tower']
        aga_door = random.choice(list(entrances))
        if (aga_door in hc_ledge_entrances):
            hc_ledge_entrances.remove(aga_door)
            random.shuffle(hc_ledge_entrances)
            hc_ledge_must_exit = hc_ledge_entrances.pop()
            entrances.remove(hc_ledge_must_exit)
            must_exits.append(hc_ledge_must_exit)
        entrances.remove(aga_door)
        connect_two_way(world, aga_door, 'Inverted Agahnims Tower Exit', player)
        caves.remove('Inverted Agahnims Tower Exit')
        links_house_doors = [i for i in (entrances + must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in entrances):
            entrances.remove(links_house)
        elif (links_house in must_exits):
            must_exits.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in entrances)]
        sanc_door = random.choice(sanc_doors)
        entrances.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_mandatory_exits(world, entrances, caves, must_exits, player, dp_must_exit)
        old_man_entrances = [door for door in old_man_entrances if (door in entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        entrances.remove(old_man_exit)
        blacksmith_doors = [door for door in blacksmith_doors if (door in entrances)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        entrances.remove(blacksmith_hut)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in entrances)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        entrances.remove(bomb_shop)
        random.shuffle(entrances)
        old_man_entrance = entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, entrances, [], caves, player)
        scramble_inverted_holes(world, player)
        connect_doors(world, entrances, door_targets, player)
    elif (world.shuffle[player] == 'insanity'):
        entrances = ((((((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_DW_Entrances) + Inverted_DW_Dungeon_Entrances) + Inverted_Old_Man_Entrances) + Old_Man_Entrances) + ['Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)', 'Skull Woods First Section Door', 'Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave', 'Hyrule Castle Entrance (South)'])
        entrances_must_exits = (Inverted_LW_Entrances_Must_Exit + Inverted_LW_Dungeon_Entrances_Must_Exit)
        doors = (((((((((((Inverted_LW_Entrances + Inverted_LW_Dungeon_Entrances) + Inverted_LW_Entrances_Must_Exit) + Inverted_LW_Dungeon_Entrances_Must_Exit) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave', 'Hyrule Castle Secret Entrance Stairs']) + Inverted_Old_Man_Entrances) + Inverted_DW_Entrances) + Inverted_DW_Dungeon_Entrances) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)']) + Inverted_LW_Single_Cave_Doors) + Inverted_DW_Single_Cave_Doors) + ['Desert Palace Entrance (West)', 'Desert Palace Entrance (North)'])
        if (random.randint(0, 1) == 0):
            entrances_must_exits.append('Desert Palace Entrance (North)')
            entrances.append('Desert Palace Entrance (West)')
        else:
            entrances_must_exits.append('Desert Palace Entrance (West)')
            entrances.append('Desert Palace Entrance (North)')
        bomb_shop_doors = list(((Inverted_Bomb_Shop_Single_Cave_Doors + Inverted_Bomb_Shop_Multi_Cave_Doors) + ['Turtle Rock Isolated Ledge Entrance', 'Bumper Cave (Top)', 'Hookshot Cave Back Entrance']))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Inverted_Single_Cave_Targets)
        random.shuffle(doors)
        old_man_entrances = (list((Inverted_Old_Man_Entrances + Old_Man_Entrances)) + ['Tower of Hera', 'Inverted Agahnims Tower'])
        caves = (((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits) + ['Old Man House Exit (Bottom)', 'Old Man House Exit (Top)', 'Skull Woods First Section Exit', 'Skull Woods Second Section Exit (East)', 'Skull Woods Second Section Exit (West)', 'Kakariko Well Exit', 'Bat Cave Exit', 'North Fairy Cave Exit', 'Lost Woods Hideout Exit', 'Lumberjack Tree Exit', 'Sanctuary Exit'])
        hole_entrances = ['Kakariko Well Drop', 'Bat Cave Drop', 'North Fairy Cave Drop', 'Lost Woods Hideout Drop', 'Lumberjack Tree Tree', 'Sanctuary Grave', 'Skull Woods First Section Hole (East)', 'Skull Woods First Section Hole (West)', 'Skull Woods First Section Hole (North)', 'Skull Woods Second Section Hole']
        hole_targets = ['Kakariko Well (top)', 'Bat Cave (right)', 'North Fairy Cave', 'Lost Woods Hideout (top)', 'Lumberjack Tree (top)', 'Sewer Drop', 'Skull Back Drop', 'Skull Left Drop', 'Skull Pinball', 'Skull Pot Circle']
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        hole_entrances.append('Hyrule Castle Secret Entrance Drop')
        hole_targets.append('Hyrule Castle Secret Entrance')
        entrances.append('Hyrule Castle Secret Entrance Stairs')
        caves.append('Hyrule Castle Secret Entrance Exit')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Inverted Ganons Tower', 'Inverted Ganons Tower Exit', player)
            connect_two_way(world, 'Inverted Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Inverted Pyramid Hole', 'Pyramid', player)
        else:
            entrances.append('Inverted Ganons Tower')
            caves.extend(['Inverted Ganons Tower Exit', 'Pyramid Exit'])
            hole_entrances.append('Inverted Pyramid Hole')
            hole_targets.append('Pyramid')
            doors.extend(['Inverted Ganons Tower', 'Inverted Pyramid Entrance'])
        random.shuffle(hole_entrances)
        random.shuffle(hole_targets)
        random.shuffle(entrances)
        for hole in hole_entrances:
            connect_entrance(world, hole, hole_targets.pop(), player)
        doors.append('Hyrule Castle Entrance (South)')
        caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        links_house_doors = [i for i in (entrances + entrances_must_exits) if (i not in (Inverted_Dark_Sanctuary_Doors + Isolated_LH_Doors))]
        links_house = random.choice(list(links_house_doors))
        connect_two_way(world, links_house, 'Inverted Links House Exit', player)
        if (links_house in entrances):
            entrances.remove(links_house)
        elif (links_house in entrances_must_exits):
            entrances_must_exits.remove(links_house)
        doors.remove(links_house)
        sanc_doors = [door for door in Inverted_Dark_Sanctuary_Doors if (door in entrances)]
        sanc_door = random.choice(sanc_doors)
        entrances.remove(sanc_door)
        doors.remove(sanc_door)
        connect_entrance(world, sanc_door, 'Inverted Dark Sanctuary', player)
        world.get_entrance('Inverted Dark Sanctuary Exit', player).connect(world.get_entrance(sanc_door, player).parent_region)

        def extract_reachable_exit(cavelist):
            random.shuffle(cavelist)
            candidate = None
            for cave in cavelist:
                if (isinstance(cave, tuple) and (len(cave) > 1)):
                    if ((cave[0] in ['Turtle Rock Exit (Front)', 'Spectacle Rock Cave Exit (Peak)']) and (len(cave) == 2)):
                        continue
                    candidate = cave
                    break
            if (candidate is None):
                raise RuntimeError('No suitable cave.')
            cavelist.remove(candidate)
            return candidate

        def connect_reachable_exit(entrance, caves, doors):
            cave = extract_reachable_exit(caves)
            exit = cave[(- 1)]
            cave = cave[:(- 1)]
            connect_exit(world, exit, entrance, player)
            connect_entrance(world, doors.pop(), exit, player)
            caves.append(cave)
        for entrance in entrances_must_exits:
            connect_reachable_exit(entrance, caves, doors)
        old_man_entrances = [entrance for entrance in old_man_entrances if (entrance in entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        entrances.remove(old_man_exit)
        connect_exit(world, 'Old Man Cave Exit (East)', old_man_exit, player)
        connect_entrance(world, doors.pop(), 'Old Man Cave Exit (East)', player)
        caves.append('Old Man Cave Exit (West)')
        blacksmith_doors = [door for door in blacksmith_doors if (door in doors)]
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        doors.remove(blacksmith_hut)
        bomb_shop_doors = [door for door in bomb_shop_doors if (door in doors)]
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, 'Inverted Big Bomb Shop', player)
        doors.remove(bomb_shop)
        for cave in caves:
            if isinstance(cave, str):
                cave = (cave,)
            for exit in cave:
                connect_exit(world, exit, entrances.pop(), player)
                connect_entrance(world, doors.pop(), exit, player)
        connect_doors(world, doors, door_targets, player)
    else:
        raise NotImplementedError('Shuffling not supported yet')
    if ((world.get_entrance('Dam', player).connected_region.name != 'Dam') or (world.get_entrance('Swamp Palace', player).connected_region.name != 'Swamp Lobby')):
        world.swamp_patch_required[player] = True
    if (world.get_entrance('Potion Shop', player).connected_region.name != 'Potion Shop'):
        world.powder_patch_required[player] = True
    if (world.get_entrance('Inverted Pyramid Hole', player).connected_region.name != 'Pyramid'):
        world.ganon_at_pyramid[player] = False
    if (world.get_entrance('Inverted Ganons Tower', player).connected_region.name != 'GT Lobby'):
        world.ganonstower_vanilla[player] = False