Jump to content
  • Advertisement
  • 08/28/19 12:26 PM

    A Labyrinth Generation Algorithm

    General and Gameplay Programming

    GameDev.net

    This was for a Dungeon Explorer home project I was working on. When researching labyrinth, maze, or dungeon generation algorithms I found many that would create hub or tree-style dungeons, but none that would 'loop back' on themselves.

    I created this algorithm with the intention of designers or artists still having full control over the look and contents of rooms and corridors. As long as certain rules are followed (e.g. attachpoints are assigned to rooms and snapped to a grid, rooms have a 'footprint' object that bounds their size) rooms and corridors can be any size or shape desired.

    This video demonstrates the process, which is also outlined below:

    I did go on to make a small game using this algorithm and bar some silly behaviour (like making corridors to from a room to itself), it's worked great!

    A short excerpt about the algorithm, for those that might like to re-create it!

    Steps

    1. Randomly Place rooms within the defined area
    2. Attempt to connect all rooms together in a large 'chain'
    3. Starting with random rooms, make smaller 'chains' of rooms within the network
    4. Prune room connections from rooms that have more connections than attach points
    5. Go through all room connections and connect actual attach points
    6. Use A* pathing to create corridors between the attach points
    7. Mark all of the corridors onto the grid
    8. Actually place straight, corner, T-Junction, and crossroad corridors oriented in the correct way

    There's a whole bunch more complexity in each of these steps, but that's the basic breakdown!

     

    The objects you see in the gif are 3d rooms complete with art and stuff inside them, but the camera is top down and lighting set to make the gif easier to see!

    Each room is a prefab game object. They have their own scripts that manage the stuff contained within them, in this case flickering torches, furnaces, and other puzzles.

    Each room has attach points that are defined by me (in this case acting as the designer), and the dungeon generator is given a list of all rooms it should use to generate, again defined by the designer.

    The rooms provide information like their size, shape, available attach points etc that the generator uses to position and connect them.

    There are different corridor types, straight, t junction, crossroad, and corners. Each has a different setup of walls. The algorithm places them just after the steps seen in the gif, choosing which to place base on what's adjacent to each grid position and orients them. Again these are prefab game objects with other things in them (e.g. Torches).

    I've just used debug lines to draw out the connections for the purpose of showing it in gif format, the original version doesn't do this at all!

     

    Another example but with 200 rooms rather than 50, at increased speed:

     

    And another 200 room demo, with no speed increase:

     

    Note: This article was originally shared via Reddit, and is recreated here with the kind permission of the author.



      Report Article


    User Feedback


    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!