def link_entrances(world, player):
    connect_two_way(world, 'Links House', 'Links House Exit', player)
    connect_exit(world, 'Chris Houlihan Room Exit', 'Links House', player)
    Dungeon_Exits = 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 mandatory_connections:
        connect_simple(world, exitname, regionname, player)
    if (world.shuffle[player] == 'vanilla'):
        for (exitname, regionname) in default_connections:
            connect_simple(world, exitname, regionname, player)
        for (exitname, regionname) in default_dungeon_connections:
            connect_simple(world, exitname, regionname, player)
    elif (world.shuffle[player] == 'dungeonssimple'):
        for (exitname, regionname) in default_connections:
            connect_simple(world, exitname, regionname, player)
        simple_shuffle_dungeons(world, player)
    elif (world.shuffle[player] == 'dungeonsfull'):
        for (exitname, regionname) in default_connections:
            connect_simple(world, exitname, regionname, player)
        skull_woods_shuffle(world, player)
        dungeon_exits = list(Dungeon_Exits)
        lw_entrances = list(LW_Dungeon_Entrances)
        dw_entrances = list(DW_Dungeon_Entrances)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        elif (world.doorShuffle[player] != 'vanilla'):
            lw_entrances.append('Hyrule Castle Entrance (South)')
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            dw_entrances.append('Ganons Tower')
            dungeon_exits.append('Ganons Tower Exit')
        if (world.mode[player] == 'standard'):
            connect_mandatory_exits(world, lw_entrances, [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], list(LW_Dungeon_Entrances_Must_Exit), player)
        elif (world.doorShuffle[player] != 'vanilla'):
            connect_mandatory_exits(world, lw_entrances, [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)', 'Hyrule Castle Exit (South)')], list(LW_Dungeon_Entrances_Must_Exit), player)
        else:
            connect_mandatory_exits(world, lw_entrances, dungeon_exits, list(LW_Dungeon_Entrances_Must_Exit), player)
        connect_mandatory_exits(world, dw_entrances, dungeon_exits, list(DW_Dungeon_Entrances_Must_Exit), player)
        connect_caves(world, lw_entrances, dw_entrances, dungeon_exits, player)
    elif (world.shuffle[player] == 'simple'):
        simple_shuffle_dungeons(world, player)
        old_man_entrances = list(Old_Man_Entrances)
        caves = list(Cave_Exits)
        three_exit_caves = list(Cave_Three_Exits)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        two_door_caves = list(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(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)
        remaining_entrances = ['Old Man Cave (West)', 'Old Man House (Bottom)', 'Death Mountain Return Cave (West)', 'Paradox Cave (Bottom)', 'Paradox Cave (Middle)', 'Paradox Cave (Top)', 'Fairy Ascension Cave (Bottom)', 'Fairy Ascension Cave (Top)', 'Spiral Cave', 'Spiral Cave (Bottom)']
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        remaining_entrances.extend(old_man_entrances)
        random.shuffle(remaining_entrances)
        old_man_entrance = remaining_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        caves.extend(list(Old_Man_House))
        caves.extend(list(three_exit_caves))
        connect_caves(world, remaining_entrances, [], caves, player)
        scramble_holes(world, player)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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(((LW_Entrances + LW_Single_Cave_Doors) + Old_Man_Entrances))
        dw_entrances = list((DW_Entrances + DW_Single_Cave_Doors))
        dw_must_exits = list(DW_Entrances_Must_Exit)
        old_man_entrances = list(Old_Man_Entrances)
        caves = list((Cave_Exits + Cave_Three_Exits))
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Single_Cave_Targets)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        old_man_entrances = [door for door in old_man_entrances if (door in 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)
        lw_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, '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(lw_entrances)
        old_man_entrance = lw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, lw_entrances, [], list(Old_Man_House), player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_holes(world, player)
        doors = (lw_entrances + dw_entrances)
        connect_doors(world, doors, door_targets, player)
    elif (world.shuffle[player] == 'restricted_legacy'):
        simple_shuffle_dungeons(world, player)
        lw_entrances = list(LW_Entrances)
        dw_entrances = list(DW_Entrances)
        dw_must_exits = list(DW_Entrances_Must_Exit)
        old_man_entrances = list(Old_Man_Entrances)
        caves = list(Cave_Exits)
        three_exit_caves = list(Cave_Three_Exits)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        caves.extend(three_exit_caves)
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        lw_entrances.extend(old_man_entrances)
        random.shuffle(lw_entrances)
        old_man_entrance = lw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        connect_caves(world, lw_entrances, [], Old_Man_House, player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_holes(world, player)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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] == 'full'):
        skull_woods_shuffle(world, player)
        lw_entrances = list((((LW_Entrances + LW_Dungeon_Entrances) + LW_Single_Cave_Doors) + Old_Man_Entrances))
        dw_entrances = list(((DW_Entrances + DW_Dungeon_Entrances) + DW_Single_Cave_Doors))
        dw_must_exits = list((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit))
        lw_must_exits = list(LW_Dungeon_Entrances_Must_Exit)
        old_man_entrances = list((Old_Man_Entrances + ['Tower of Hera']))
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        bomb_shop_doors = list((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Single_Cave_Targets)
        old_man_house = list(Old_Man_House)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        elif (world.doorShuffle[player] != 'vanilla'):
            lw_entrances.append('Hyrule Castle Entrance (South)')
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            dw_entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
        if (random.randint(0, 1) == 0):
            caves += old_man_house
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
            try:
                caves.remove(old_man_house[0])
            except ValueError:
                pass
            else:
                connect_caves(world, lw_entrances, [], old_man_house, player)
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        else:
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
            caves += old_man_house
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
            try:
                caves.remove(old_man_house[0])
            except ValueError:
                pass
            else:
                connect_caves(world, lw_entrances, [], old_man_house, player)
        if (world.mode[player] == 'standard'):
            connect_caves(world, lw_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
        elif (world.doorShuffle[player] != 'vanilla'):
            connect_caves(world, lw_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)', 'Hyrule Castle Exit (South)')], player)
        old_man_entrances = [door for door in old_man_entrances if (door in 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)
        lw_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, '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)
        old_man_entrance = lw_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_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(((((((LW_Entrances + LW_Dungeon_Entrances) + LW_Single_Cave_Doors) + Old_Man_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + DW_Single_Cave_Doors))
        must_exits = list(((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit) + LW_Dungeon_Entrances_Must_Exit))
        old_man_entrances = list((Old_Man_Entrances + ['Tower of Hera']))
        caves = list((((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits) + Old_Man_House))
        bomb_shop_doors = list((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Single_Cave_Targets)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
        connect_mandatory_exits(world, entrances, caves, must_exits, player)
        if (world.mode[player] == 'standard'):
            connect_caves(world, entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
        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.extend(blacksmith_doors)
        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, '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_holes(world, player)
        connect_doors(world, entrances, door_targets, player)
    elif (world.shuffle[player] == 'full_legacy'):
        skull_woods_shuffle(world, player)
        lw_entrances = list(((LW_Entrances + LW_Dungeon_Entrances) + Old_Man_Entrances))
        dw_entrances = list((DW_Entrances + DW_Dungeon_Entrances))
        dw_must_exits = list((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit))
        lw_must_exits = list(LW_Dungeon_Entrances_Must_Exit)
        old_man_entrances = list((Old_Man_Entrances + ['Tower of Hera']))
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            dw_entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
        if (random.randint(0, 1) == 0):
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        else:
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
        if (world.mode[player] == 'standard'):
            connect_caves(world, lw_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
        old_man_entrances = [door for door in old_man_entrances if (door in lw_entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        lw_entrances.remove(old_man_exit)
        random.shuffle(lw_entrances)
        old_man_entrance = lw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        connect_caves(world, lw_entrances, [], list(Old_Man_House), player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_holes(world, player)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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] == 'madness_legacy'):
        lw_entrances = list(((LW_Entrances + LW_Dungeon_Entrances) + Old_Man_Entrances))
        dw_entrances = list((DW_Entrances + DW_Dungeon_Entrances))
        dw_entrances_must_exits = list((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit))
        lw_doors = ((list(((LW_Entrances + LW_Dungeon_Entrances) + LW_Dungeon_Entrances_Must_Exit)) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave']) + list(Old_Man_Entrances))
        dw_doors = (list((((DW_Entrances + DW_Dungeon_Entrances) + DW_Entrances_Must_Exit) + DW_Dungeon_Entrances_Must_Exit)) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)'])
        random.shuffle(lw_doors)
        random.shuffle(dw_doors)
        dw_entrances_must_exits.append('Skull Woods Second Section Door (West)')
        dw_entrances.append('Skull Woods Second Section Door (East)')
        dw_entrances.append('Skull Woods First Section Door')
        lw_entrances.extend(['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave'])
        lw_entrances_must_exits = list(LW_Dungeon_Entrances_Must_Exit)
        old_man_entrances = (list(Old_Man_Entrances) + ['Tower of Hera'])
        mandatory_light_world = ['Old Man House Exit (Bottom)', 'Old Man House Exit (Top)']
        mandatory_dark_world = []
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        lw_hole_entrances = ['Kakariko Well Drop', 'Bat Cave Drop', 'North Fairy Cave Drop', 'Lost Woods Hideout Drop', 'Lumberjack Tree Tree', 'Sanctuary Grave']
        dw_hole_entrances = ['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 Exit', 'Kakariko Well (top)'), ('Bat Cave Exit', 'Bat Cave (right)'), ('North Fairy Cave Exit', 'North Fairy Cave'), ('Lost Woods Hideout Exit', 'Lost Woods Hideout (top)'), ('Lumberjack Tree Exit', 'Lumberjack Tree (top)'), (('Skull Woods Second Section Exit (East)', 'Skull Woods Second Section Exit (West)'), 'Skull Back Drop')]
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Secret Entrance Drop', 'Hyrule Castle Secret Entrance', player)
            connect_exit(world, 'Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance Stairs', player)
            connect_entrance(world, 'Hyrule Castle Secret Entrance Stairs', 'Hyrule Castle Secret Entrance Exit', player)
        else:
            lw_hole_entrances.append('Hyrule Castle Secret Entrance Drop')
            hole_targets.append(('Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance'))
            lw_doors.append('Hyrule Castle Secret Entrance Stairs')
            lw_entrances.append('Hyrule Castle Secret Entrance Stairs')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Ganons Tower', 'Ganons Tower Exit', player)
            connect_two_way(world, 'Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Pyramid Hole', 'Pyramid', player)
        else:
            dw_entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
            dw_hole_entrances.append('Pyramid Hole')
            hole_targets.append(('Pyramid Exit', 'Pyramid'))
            dw_entrances_must_exits.append('Pyramid Entrance')
            dw_doors.extend(['Ganons Tower', 'Pyramid Entrance'])
        random.shuffle(lw_hole_entrances)
        random.shuffle(dw_hole_entrances)
        random.shuffle(hole_targets)
        sw_light = (random.randint(0, 1) == 0)
        if sw_light:
            sw_hole_pool = lw_hole_entrances
            mandatory_light_world.append('Skull Woods First Section Exit')
        else:
            sw_hole_pool = dw_hole_entrances
            mandatory_dark_world.append('Skull Woods First Section Exit')
        for target in ['Skull Left Drop', 'Skull Pinball', 'Skull Pot Circle']:
            connect_entrance(world, sw_hole_pool.pop(), target, player)
        connect_entrance(world, lw_hole_entrances.pop(), 'Sewer Drop', player)
        mandatory_light_world.append('Sanctuary Exit')
        for hole in dw_hole_entrances:
            (exits, target) = hole_targets.pop()
            mandatory_dark_world.append(exits)
            connect_entrance(world, hole, target, player)
        for hole in lw_hole_entrances:
            (exits, target) = hole_targets.pop()
            mandatory_light_world.append(exits)
            connect_entrance(world, hole, target, player)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
            connect_exit(world, 'Hyrule Castle Exit (South)', 'Hyrule Castle Entrance (South)', player)
            mandatory_light_world.append(('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        else:
            lw_doors.append('Hyrule Castle Entrance (South)')
            lw_entrances.append('Hyrule Castle Entrance (South)')
            caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))

        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, general, worldspecific, worldoors):
            if (random.randint(0, 1) == 0):
                primary = general
                secondary = worldspecific
            else:
                primary = worldspecific
                secondary = general
            try:
                cave = extract_reachable_exit(primary)
            except RuntimeError:
                cave = extract_reachable_exit(secondary)
            exit = cave[(- 1)]
            cave = cave[:(- 1)]
            connect_exit(world, exit, entrance, player)
            connect_entrance(world, worldoors.pop(), exit, player)
            worldspecific.append(cave)
        if (random.randint(0, 1) == 0):
            for entrance in lw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_light_world, lw_doors)
            for entrance in dw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_dark_world, dw_doors)
        else:
            for entrance in dw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_dark_world, dw_doors)
            for entrance in lw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_light_world, lw_doors)
        old_man_entrances = [entrance for entrance in old_man_entrances if (entrance in lw_entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        lw_entrances.remove(old_man_exit)
        connect_exit(world, 'Old Man Cave Exit (East)', old_man_exit, player)
        connect_entrance(world, lw_doors.pop(), 'Old Man Cave Exit (East)', player)
        mandatory_light_world.append('Old Man Cave Exit (West)')
        for mandatory in mandatory_light_world:
            if (not isinstance(mandatory, tuple)):
                mandatory = (mandatory,)
            for exit in mandatory:
                connect_exit(world, exit, lw_entrances.pop(), player)
                connect_entrance(world, lw_doors.pop(), exit, player)
        for mandatory in mandatory_dark_world:
            if (not isinstance(mandatory, tuple)):
                mandatory = (mandatory,)
            for exit in mandatory:
                connect_exit(world, exit, dw_entrances.pop(), player)
                connect_entrance(world, dw_doors.pop(), exit, player)
        while caves:
            cave_candidate = (None, 0)
            for (i, cave) in enumerate(caves):
                if isinstance(cave, str):
                    cave = (cave,)
                if (len(cave) > cave_candidate[1]):
                    cave_candidate = (i, len(cave))
            cave = caves.pop(cave_candidate[0])
            place_lightworld = (random.randint(0, 1) == 0)
            if place_lightworld:
                target_doors = lw_doors
                target_entrances = lw_entrances
            else:
                target_doors = dw_doors
                target_entrances = dw_entrances
            if isinstance(cave, str):
                cave = (cave,)
            if (len(target_doors) < len(cave)):
                if (not place_lightworld):
                    target_doors = lw_doors
                    target_entrances = lw_entrances
                else:
                    target_doors = dw_doors
                    target_entrances = dw_entrances
            for exit in cave:
                connect_exit(world, exit, target_entrances.pop(), player)
                connect_entrance(world, target_doors.pop(), exit, player)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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] == 'insanity'):
        entrances = (((((LW_Entrances + LW_Dungeon_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + Old_Man_Entrances) + ['Skull Woods Second Section Door (East)', 'Skull Woods First Section Door', 'Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave'])
        entrances_must_exits = (((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit) + LW_Dungeon_Entrances_Must_Exit) + ['Skull Woods Second Section Door (West)'])
        doors = (((((((((((LW_Entrances + LW_Dungeon_Entrances) + LW_Dungeon_Entrances_Must_Exit) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave']) + Old_Man_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + DW_Entrances_Must_Exit) + DW_Dungeon_Entrances_Must_Exit) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)']) + LW_Single_Cave_Doors) + DW_Single_Cave_Doors)
        bomb_shop_doors = list(((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors) + ['Desert Palace Entrance (East)', '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(Single_Cave_Targets)
        random.shuffle(doors)
        old_man_entrances = (list(Old_Man_Entrances) + ['Tower of Hera'])
        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)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Secret Entrance Drop', 'Hyrule Castle Secret Entrance', player)
            connect_exit(world, 'Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance Stairs', player)
            connect_entrance(world, 'Hyrule Castle Secret Entrance Stairs', 'Hyrule Castle Secret Entrance Exit', player)
        else:
            hole_entrances.append('Hyrule Castle Secret Entrance Drop')
            hole_targets.append('Hyrule Castle Secret Entrance')
            doors.append('Hyrule Castle Secret Entrance Stairs')
            entrances.append('Hyrule Castle Secret Entrance Stairs')
            caves.append('Hyrule Castle Secret Entrance Exit')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Ganons Tower', 'Ganons Tower Exit', player)
            connect_two_way(world, 'Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Pyramid Hole', 'Pyramid', player)
        else:
            entrances.append('Ganons Tower')
            caves.extend(['Ganons Tower Exit', 'Pyramid Exit'])
            hole_entrances.append('Pyramid Hole')
            hole_targets.append('Pyramid')
            entrances_must_exits.append('Pyramid Entrance')
            doors.extend(['Ganons Tower', '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)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
            connect_exit(world, 'Hyrule Castle Exit (South)', 'Hyrule Castle Entrance (South)', player)
            caves.append(('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        else:
            doors.append('Hyrule Castle Entrance (South)')
            entrances.append('Hyrule Castle Entrance (South)')
            caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))

        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, '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)
    elif (world.shuffle[player] == 'insanity_legacy'):
        world.fix_fake_world[player] = False
        entrances = (((((LW_Entrances + LW_Dungeon_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + Old_Man_Entrances) + ['Skull Woods Second Section Door (East)', 'Skull Woods First Section Door', 'Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave'])
        entrances_must_exits = (((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit) + LW_Dungeon_Entrances_Must_Exit) + ['Skull Woods Second Section Door (West)'])
        doors = (((((((((LW_Entrances + LW_Dungeon_Entrances) + LW_Dungeon_Entrances_Must_Exit) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave']) + Old_Man_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + DW_Entrances_Must_Exit) + DW_Dungeon_Entrances_Must_Exit) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)'])
        random.shuffle(doors)
        old_man_entrances = (list(Old_Man_Entrances) + ['Tower of Hera'])
        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']
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Secret Entrance Drop', 'Hyrule Castle Secret Entrance', player)
            connect_exit(world, 'Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance Stairs', player)
            connect_entrance(world, 'Hyrule Castle Secret Entrance Stairs', 'Hyrule Castle Secret Entrance Exit', player)
        else:
            hole_entrances.append('Hyrule Castle Secret Entrance Drop')
            hole_targets.append('Hyrule Castle Secret Entrance')
            doors.append('Hyrule Castle Secret Entrance Stairs')
            entrances.append('Hyrule Castle Secret Entrance Stairs')
            caves.append('Hyrule Castle Secret Entrance Exit')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Ganons Tower', 'Ganons Tower Exit', player)
            connect_two_way(world, 'Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Pyramid Hole', 'Pyramid', player)
        else:
            entrances.append('Ganons Tower')
            caves.extend(['Ganons Tower Exit', 'Pyramid Exit'])
            hole_entrances.append('Pyramid Hole')
            hole_targets.append('Pyramid')
            entrances_must_exits.append('Pyramid Entrance')
            doors.extend(['Ganons Tower', '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)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
            connect_exit(world, 'Hyrule Castle Exit (South)', 'Hyrule Castle Entrance (South)', player)
            caves.append(('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        else:
            doors.append('Hyrule Castle Entrance (South)')
            entrances.append('Hyrule Castle Entrance (South)')
            caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))

        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)')
        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)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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)
    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('Pyramid Hole', player).connected_region.name != 'Pyramid'):
        world.ganon_at_pyramid[player] = False
    if (world.get_entrance('Ganons Tower', player).connected_region.name != 'GT Lobby'):
        world.ganonstower_vanilla[player] = False
def link_entrances(world, player):
    connect_two_way(world, 'Links House', 'Links House Exit', player)
    connect_exit(world, 'Chris Houlihan Room Exit', 'Links House', player)
    Dungeon_Exits = 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 mandatory_connections:
        connect_simple(world, exitname, regionname, player)
    if (world.shuffle[player] == 'vanilla'):
        for (exitname, regionname) in default_connections:
            connect_simple(world, exitname, regionname, player)
        for (exitname, regionname) in default_dungeon_connections:
            connect_simple(world, exitname, regionname, player)
    elif (world.shuffle[player] == 'dungeonssimple'):
        for (exitname, regionname) in default_connections:
            connect_simple(world, exitname, regionname, player)
        simple_shuffle_dungeons(world, player)
    elif (world.shuffle[player] == 'dungeonsfull'):
        for (exitname, regionname) in default_connections:
            connect_simple(world, exitname, regionname, player)
        skull_woods_shuffle(world, player)
        dungeon_exits = list(Dungeon_Exits)
        lw_entrances = list(LW_Dungeon_Entrances)
        dw_entrances = list(DW_Dungeon_Entrances)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        elif (world.doorShuffle[player] != 'vanilla'):
            lw_entrances.append('Hyrule Castle Entrance (South)')
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            dw_entrances.append('Ganons Tower')
            dungeon_exits.append('Ganons Tower Exit')
        if (world.mode[player] == 'standard'):
            connect_mandatory_exits(world, lw_entrances, [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], list(LW_Dungeon_Entrances_Must_Exit), player)
        elif (world.doorShuffle[player] != 'vanilla'):
            connect_mandatory_exits(world, lw_entrances, [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)', 'Hyrule Castle Exit (South)')], list(LW_Dungeon_Entrances_Must_Exit), player)
        else:
            connect_mandatory_exits(world, lw_entrances, dungeon_exits, list(LW_Dungeon_Entrances_Must_Exit), player)
        connect_mandatory_exits(world, dw_entrances, dungeon_exits, list(DW_Dungeon_Entrances_Must_Exit), player)
        connect_caves(world, lw_entrances, dw_entrances, dungeon_exits, player)
    elif (world.shuffle[player] == 'simple'):
        simple_shuffle_dungeons(world, player)
        old_man_entrances = list(Old_Man_Entrances)
        caves = list(Cave_Exits)
        three_exit_caves = list(Cave_Three_Exits)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        two_door_caves = list(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(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)
        remaining_entrances = ['Old Man Cave (West)', 'Old Man House (Bottom)', 'Death Mountain Return Cave (West)', 'Paradox Cave (Bottom)', 'Paradox Cave (Middle)', 'Paradox Cave (Top)', 'Fairy Ascension Cave (Bottom)', 'Fairy Ascension Cave (Top)', 'Spiral Cave', 'Spiral Cave (Bottom)']
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        remaining_entrances.extend(old_man_entrances)
        random.shuffle(remaining_entrances)
        old_man_entrance = remaining_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        caves.extend(list(Old_Man_House))
        caves.extend(list(three_exit_caves))
        connect_caves(world, remaining_entrances, [], caves, player)
        scramble_holes(world, player)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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(((LW_Entrances + LW_Single_Cave_Doors) + Old_Man_Entrances))
        dw_entrances = list((DW_Entrances + DW_Single_Cave_Doors))
        dw_must_exits = list(DW_Entrances_Must_Exit)
        old_man_entrances = list(Old_Man_Entrances)
        caves = list((Cave_Exits + Cave_Three_Exits))
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Single_Cave_Targets)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        old_man_entrances = [door for door in old_man_entrances if (door in 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)
        lw_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, '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(lw_entrances)
        old_man_entrance = lw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_caves(world, lw_entrances, [], list(Old_Man_House), player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_holes(world, player)
        doors = (lw_entrances + dw_entrances)
        connect_doors(world, doors, door_targets, player)
    elif (world.shuffle[player] == 'restricted_legacy'):
        simple_shuffle_dungeons(world, player)
        lw_entrances = list(LW_Entrances)
        dw_entrances = list(DW_Entrances)
        dw_must_exits = list(DW_Entrances_Must_Exit)
        old_man_entrances = list(Old_Man_Entrances)
        caves = list(Cave_Exits)
        three_exit_caves = list(Cave_Three_Exits)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        caves.extend(three_exit_caves)
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        lw_entrances.extend(old_man_entrances)
        random.shuffle(lw_entrances)
        old_man_entrance = lw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        connect_caves(world, lw_entrances, [], Old_Man_House, player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_holes(world, player)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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] == 'full'):
        skull_woods_shuffle(world, player)
        lw_entrances = list((((LW_Entrances + LW_Dungeon_Entrances) + LW_Single_Cave_Doors) + Old_Man_Entrances))
        dw_entrances = list(((DW_Entrances + DW_Dungeon_Entrances) + DW_Single_Cave_Doors))
        dw_must_exits = list((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit))
        lw_must_exits = list(LW_Dungeon_Entrances_Must_Exit)
        old_man_entrances = list((Old_Man_Entrances + ['Tower of Hera']))
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        bomb_shop_doors = list((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Single_Cave_Targets)
        old_man_house = list(Old_Man_House)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        elif (world.doorShuffle[player] != 'vanilla'):
            lw_entrances.append('Hyrule Castle Entrance (South)')
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            dw_entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
        if (random.randint(0, 1) == 0):
            caves += old_man_house
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
            try:
                caves.remove(old_man_house[0])
            except ValueError:
                pass
            else:
                connect_caves(world, lw_entrances, [], old_man_house, player)
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        else:
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
            caves += old_man_house
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
            try:
                caves.remove(old_man_house[0])
            except ValueError:
                pass
            else:
                connect_caves(world, lw_entrances, [], old_man_house, player)
        if (world.mode[player] == 'standard'):
            connect_caves(world, lw_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
        elif (world.doorShuffle[player] != 'vanilla'):
            connect_caves(world, lw_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)', 'Hyrule Castle Exit (South)')], player)
        old_man_entrances = [door for door in old_man_entrances if (door in 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)
        lw_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, '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)
        old_man_entrance = lw_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_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(((((((LW_Entrances + LW_Dungeon_Entrances) + LW_Single_Cave_Doors) + Old_Man_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + DW_Single_Cave_Doors))
        must_exits = list(((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit) + LW_Dungeon_Entrances_Must_Exit))
        old_man_entrances = list((Old_Man_Entrances + ['Tower of Hera']))
        caves = list((((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits) + Old_Man_House))
        bomb_shop_doors = list((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors))
        blacksmith_doors = list((Blacksmith_Single_Cave_Doors + Blacksmith_Multi_Cave_Doors))
        door_targets = list(Single_Cave_Targets)
        connect_doors(world, ['Tavern North'], ['Tavern'], player)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
        connect_mandatory_exits(world, entrances, caves, must_exits, player)
        if (world.mode[player] == 'standard'):
            connect_caves(world, entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
        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.extend(blacksmith_doors)
        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, '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_holes(world, player)
        connect_doors(world, entrances, door_targets, player)
    elif (world.shuffle[player] == 'full_legacy'):
        skull_woods_shuffle(world, player)
        lw_entrances = list(((LW_Entrances + LW_Dungeon_Entrances) + Old_Man_Entrances))
        dw_entrances = list((DW_Entrances + DW_Dungeon_Entrances))
        dw_must_exits = list((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit))
        lw_must_exits = list(LW_Dungeon_Entrances_Must_Exit)
        old_man_entrances = list((Old_Man_Entrances + ['Tower of Hera']))
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        if (world.mode[player] == 'standard'):
            connect_two_way(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
        else:
            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, 'Ganons Tower', 'Ganons Tower Exit', player)
        else:
            dw_entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
        if (random.randint(0, 1) == 0):
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
        else:
            connect_mandatory_exits(world, dw_entrances, caves, dw_must_exits, player)
            connect_mandatory_exits(world, lw_entrances, caves, lw_must_exits, player)
        if (world.mode[player] == 'standard'):
            connect_caves(world, lw_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
        old_man_entrances = [door for door in old_man_entrances if (door in lw_entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        lw_entrances.remove(old_man_exit)
        random.shuffle(lw_entrances)
        old_man_entrance = lw_entrances.pop()
        connect_two_way(world, old_man_entrance, 'Old Man Cave Exit (West)', player)
        connect_two_way(world, old_man_exit, 'Old Man Cave Exit (East)', player)
        connect_caves(world, lw_entrances, [], list(Old_Man_House), player)
        connect_caves(world, lw_entrances, dw_entrances, caves, player)
        scramble_holes(world, player)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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] == 'madness_legacy'):
        lw_entrances = list(((LW_Entrances + LW_Dungeon_Entrances) + Old_Man_Entrances))
        dw_entrances = list((DW_Entrances + DW_Dungeon_Entrances))
        dw_entrances_must_exits = list((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit))
        lw_doors = ((list(((LW_Entrances + LW_Dungeon_Entrances) + LW_Dungeon_Entrances_Must_Exit)) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave']) + list(Old_Man_Entrances))
        dw_doors = (list((((DW_Entrances + DW_Dungeon_Entrances) + DW_Entrances_Must_Exit) + DW_Dungeon_Entrances_Must_Exit)) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)'])
        random.shuffle(lw_doors)
        random.shuffle(dw_doors)
        dw_entrances_must_exits.append('Skull Woods Second Section Door (West)')
        dw_entrances.append('Skull Woods Second Section Door (East)')
        dw_entrances.append('Skull Woods First Section Door')
        lw_entrances.extend(['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave'])
        lw_entrances_must_exits = list(LW_Dungeon_Entrances_Must_Exit)
        old_man_entrances = (list(Old_Man_Entrances) + ['Tower of Hera'])
        mandatory_light_world = ['Old Man House Exit (Bottom)', 'Old Man House Exit (Top)']
        mandatory_dark_world = []
        caves = list(((Cave_Exits + Dungeon_Exits) + Cave_Three_Exits))
        lw_hole_entrances = ['Kakariko Well Drop', 'Bat Cave Drop', 'North Fairy Cave Drop', 'Lost Woods Hideout Drop', 'Lumberjack Tree Tree', 'Sanctuary Grave']
        dw_hole_entrances = ['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 Exit', 'Kakariko Well (top)'), ('Bat Cave Exit', 'Bat Cave (right)'), ('North Fairy Cave Exit', 'North Fairy Cave'), ('Lost Woods Hideout Exit', 'Lost Woods Hideout (top)'), ('Lumberjack Tree Exit', 'Lumberjack Tree (top)'), (('Skull Woods Second Section Exit (East)', 'Skull Woods Second Section Exit (West)'), 'Skull Back Drop')]
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Secret Entrance Drop', 'Hyrule Castle Secret Entrance', player)
            connect_exit(world, 'Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance Stairs', player)
            connect_entrance(world, 'Hyrule Castle Secret Entrance Stairs', 'Hyrule Castle Secret Entrance Exit', player)
        else:
            lw_hole_entrances.append('Hyrule Castle Secret Entrance Drop')
            hole_targets.append(('Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance'))
            lw_doors.append('Hyrule Castle Secret Entrance Stairs')
            lw_entrances.append('Hyrule Castle Secret Entrance Stairs')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Ganons Tower', 'Ganons Tower Exit', player)
            connect_two_way(world, 'Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Pyramid Hole', 'Pyramid', player)
        else:
            dw_entrances.append('Ganons Tower')
            caves.append('Ganons Tower Exit')
            dw_hole_entrances.append('Pyramid Hole')
            hole_targets.append(('Pyramid Exit', 'Pyramid'))
            dw_entrances_must_exits.append('Pyramid Entrance')
            dw_doors.extend(['Ganons Tower', 'Pyramid Entrance'])
        random.shuffle(lw_hole_entrances)
        random.shuffle(dw_hole_entrances)
        random.shuffle(hole_targets)
        sw_light = (random.randint(0, 1) == 0)
        if sw_light:
            sw_hole_pool = lw_hole_entrances
            mandatory_light_world.append('Skull Woods First Section Exit')
        else:
            sw_hole_pool = dw_hole_entrances
            mandatory_dark_world.append('Skull Woods First Section Exit')
        for target in ['Skull Left Drop', 'Skull Pinball', 'Skull Pot Circle']:
            connect_entrance(world, sw_hole_pool.pop(), target, player)
        connect_entrance(world, lw_hole_entrances.pop(), 'Sewer Drop', player)
        mandatory_light_world.append('Sanctuary Exit')
        for hole in dw_hole_entrances:
            (exits, target) = hole_targets.pop()
            mandatory_dark_world.append(exits)
            connect_entrance(world, hole, target, player)
        for hole in lw_hole_entrances:
            (exits, target) = hole_targets.pop()
            mandatory_light_world.append(exits)
            connect_entrance(world, hole, target, player)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
            connect_exit(world, 'Hyrule Castle Exit (South)', 'Hyrule Castle Entrance (South)', player)
            mandatory_light_world.append(('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        else:
            lw_doors.append('Hyrule Castle Entrance (South)')
            lw_entrances.append('Hyrule Castle Entrance (South)')
            caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))

        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, general, worldspecific, worldoors):
            if (random.randint(0, 1) == 0):
                primary = general
                secondary = worldspecific
            else:
                primary = worldspecific
                secondary = general
            try:
                cave = extract_reachable_exit(primary)
            except RuntimeError:
                cave = extract_reachable_exit(secondary)
            exit = cave[(- 1)]
            cave = cave[:(- 1)]
            connect_exit(world, exit, entrance, player)
            connect_entrance(world, worldoors.pop(), exit, player)
            worldspecific.append(cave)
        if (random.randint(0, 1) == 0):
            for entrance in lw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_light_world, lw_doors)
            for entrance in dw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_dark_world, dw_doors)
        else:
            for entrance in dw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_dark_world, dw_doors)
            for entrance in lw_entrances_must_exits:
                connect_reachable_exit(entrance, caves, mandatory_light_world, lw_doors)
        old_man_entrances = [entrance for entrance in old_man_entrances if (entrance in lw_entrances)]
        random.shuffle(old_man_entrances)
        old_man_exit = old_man_entrances.pop()
        lw_entrances.remove(old_man_exit)
        connect_exit(world, 'Old Man Cave Exit (East)', old_man_exit, player)
        connect_entrance(world, lw_doors.pop(), 'Old Man Cave Exit (East)', player)
        mandatory_light_world.append('Old Man Cave Exit (West)')
        for mandatory in mandatory_light_world:
            if (not isinstance(mandatory, tuple)):
                mandatory = (mandatory,)
            for exit in mandatory:
                connect_exit(world, exit, lw_entrances.pop(), player)
                connect_entrance(world, lw_doors.pop(), exit, player)
        for mandatory in mandatory_dark_world:
            if (not isinstance(mandatory, tuple)):
                mandatory = (mandatory,)
            for exit in mandatory:
                connect_exit(world, exit, dw_entrances.pop(), player)
                connect_entrance(world, dw_doors.pop(), exit, player)
        while caves:
            cave_candidate = (None, 0)
            for (i, cave) in enumerate(caves):
                if isinstance(cave, str):
                    cave = (cave,)
                if (len(cave) > cave_candidate[1]):
                    cave_candidate = (i, len(cave))
            cave = caves.pop(cave_candidate[0])
            place_lightworld = (random.randint(0, 1) == 0)
            if place_lightworld:
                target_doors = lw_doors
                target_entrances = lw_entrances
            else:
                target_doors = dw_doors
                target_entrances = dw_entrances
            if isinstance(cave, str):
                cave = (cave,)
            if (len(target_doors) < len(cave)):
                if (not place_lightworld):
                    target_doors = lw_doors
                    target_entrances = lw_entrances
                else:
                    target_doors = dw_doors
                    target_entrances = dw_entrances
            for exit in cave:
                connect_exit(world, exit, target_entrances.pop(), player)
                connect_entrance(world, target_doors.pop(), exit, player)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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] == 'insanity'):
        entrances = (((((LW_Entrances + LW_Dungeon_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + Old_Man_Entrances) + ['Skull Woods Second Section Door (East)', 'Skull Woods First Section Door', 'Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave'])
        entrances_must_exits = (((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit) + LW_Dungeon_Entrances_Must_Exit) + ['Skull Woods Second Section Door (West)'])
        doors = (((((((((((LW_Entrances + LW_Dungeon_Entrances) + LW_Dungeon_Entrances_Must_Exit) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave']) + Old_Man_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + DW_Entrances_Must_Exit) + DW_Dungeon_Entrances_Must_Exit) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)']) + LW_Single_Cave_Doors) + DW_Single_Cave_Doors)
        bomb_shop_doors = list(((Bomb_Shop_Single_Cave_Doors + Bomb_Shop_Multi_Cave_Doors) + ['Desert Palace Entrance (East)', '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(Single_Cave_Targets)
        random.shuffle(doors)
        old_man_entrances = (list(Old_Man_Entrances) + ['Tower of Hera'])
        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)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Secret Entrance Drop', 'Hyrule Castle Secret Entrance', player)
            connect_exit(world, 'Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance Stairs', player)
            connect_entrance(world, 'Hyrule Castle Secret Entrance Stairs', 'Hyrule Castle Secret Entrance Exit', player)
        else:
            hole_entrances.append('Hyrule Castle Secret Entrance Drop')
            hole_targets.append('Hyrule Castle Secret Entrance')
            doors.append('Hyrule Castle Secret Entrance Stairs')
            entrances.append('Hyrule Castle Secret Entrance Stairs')
            caves.append('Hyrule Castle Secret Entrance Exit')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Ganons Tower', 'Ganons Tower Exit', player)
            connect_two_way(world, 'Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Pyramid Hole', 'Pyramid', player)
        else:
            entrances.append('Ganons Tower')
            caves.extend(['Ganons Tower Exit', 'Pyramid Exit'])
            hole_entrances.append('Pyramid Hole')
            hole_targets.append('Pyramid')
            entrances_must_exits.append('Pyramid Entrance')
            doors.extend(['Ganons Tower', '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)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
            connect_exit(world, 'Hyrule Castle Exit (South)', 'Hyrule Castle Entrance (South)', player)
            caves.append(('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        else:
            doors.append('Hyrule Castle Entrance (South)')
            entrances.append('Hyrule Castle Entrance (South)')
            caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))

        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, '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)
    elif (world.shuffle[player] == 'insanity_legacy'):
        world.fix_fake_world[player] = False
        entrances = (((((LW_Entrances + LW_Dungeon_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + Old_Man_Entrances) + ['Skull Woods Second Section Door (East)', 'Skull Woods First Section Door', 'Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave'])
        entrances_must_exits = (((DW_Entrances_Must_Exit + DW_Dungeon_Entrances_Must_Exit) + LW_Dungeon_Entrances_Must_Exit) + ['Skull Woods Second Section Door (West)'])
        doors = (((((((((LW_Entrances + LW_Dungeon_Entrances) + LW_Dungeon_Entrances_Must_Exit) + ['Kakariko Well Cave', 'Bat Cave Cave', 'North Fairy Cave', 'Sanctuary', 'Lost Woods Hideout Stump', 'Lumberjack Tree Cave']) + Old_Man_Entrances) + DW_Entrances) + DW_Dungeon_Entrances) + DW_Entrances_Must_Exit) + DW_Dungeon_Entrances_Must_Exit) + ['Skull Woods First Section Door', 'Skull Woods Second Section Door (East)', 'Skull Woods Second Section Door (West)'])
        random.shuffle(doors)
        old_man_entrances = (list(Old_Man_Entrances) + ['Tower of Hera'])
        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']
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Secret Entrance Drop', 'Hyrule Castle Secret Entrance', player)
            connect_exit(world, 'Hyrule Castle Secret Entrance Exit', 'Hyrule Castle Secret Entrance Stairs', player)
            connect_entrance(world, 'Hyrule Castle Secret Entrance Stairs', 'Hyrule Castle Secret Entrance Exit', player)
        else:
            hole_entrances.append('Hyrule Castle Secret Entrance Drop')
            hole_targets.append('Hyrule Castle Secret Entrance')
            doors.append('Hyrule Castle Secret Entrance Stairs')
            entrances.append('Hyrule Castle Secret Entrance Stairs')
            caves.append('Hyrule Castle Secret Entrance Exit')
        if (not world.shuffle_ganon):
            connect_two_way(world, 'Ganons Tower', 'Ganons Tower Exit', player)
            connect_two_way(world, 'Pyramid Entrance', 'Pyramid Exit', player)
            connect_entrance(world, 'Pyramid Hole', 'Pyramid', player)
        else:
            entrances.append('Ganons Tower')
            caves.extend(['Ganons Tower Exit', 'Pyramid Exit'])
            hole_entrances.append('Pyramid Hole')
            hole_targets.append('Pyramid')
            entrances_must_exits.append('Pyramid Entrance')
            doors.extend(['Ganons Tower', '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)
        if (world.mode[player] == 'standard'):
            connect_entrance(world, 'Hyrule Castle Entrance (South)', 'Hyrule Castle Exit (South)', player)
            connect_exit(world, 'Hyrule Castle Exit (South)', 'Hyrule Castle Entrance (South)', player)
            caves.append(('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))
        else:
            doors.append('Hyrule Castle Entrance (South)')
            entrances.append('Hyrule Castle Entrance (South)')
            caves.append(('Hyrule Castle Exit (South)', 'Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)'))

        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)')
        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)
        single_doors = list(Single_Cave_Doors)
        bomb_shop_doors = list(Bomb_Shop_Single_Cave_Doors)
        blacksmith_doors = list(Blacksmith_Single_Cave_Doors)
        door_targets = list(Single_Cave_Targets)
        random.shuffle(blacksmith_doors)
        blacksmith_hut = blacksmith_doors.pop()
        connect_entrance(world, blacksmith_hut, 'Blacksmiths Hut', player)
        bomb_shop_doors.extend(blacksmith_doors)
        random.shuffle(bomb_shop_doors)
        bomb_shop = bomb_shop_doors.pop()
        connect_entrance(world, bomb_shop, '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)
    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('Pyramid Hole', player).connected_region.name != 'Pyramid'):
        world.ganon_at_pyramid[player] = False
    if (world.get_entrance('Ganons Tower', player).connected_region.name != 'GT Lobby'):
        world.ganonstower_vanilla[player] = False