Connecting rooms in grid map

I have a map that is full of 4+ rooms each with one randomly selected door on one of its sides. I need to create a tunnel connecting all of the doors together. I'm not sure the best way to do this A* seems to complicated for me to do at the moment and I cannot think of a way to connect these rooms. Any suggestions on how I should do it?

My world is currently a vector<vector<Tiles>> and the tiles contain entities like tunnel, wall, stair, floor, etc.
Sounds like typical roguelike tunnel carving. After you have your rooms, just start "carving" out tunnels from one room until you run into another one (or another tunnel).

The concept is pretty simple:

1) Start at point X,Y (in one of your rooms)
2) pick a random direction
3) pick a random number of spaces to move (N)
4) carve out N walls in that direction, starting with X,Y
5) repeat from step 2

Stop when you connect with another room, or another tunnel (but be careful not to stop if you run into the origin room or the tunnel you're currently carving, or else you'll end up with isolated rooms)

Make sure you don't go out of bounds of your map.

The "random" can be biased to make the maps a bit better. For example you might want to give your tunnels an 80% chance of choosing a direction that heads them towards a target point (another room you want to connect to), and 20% chance of going the "wrong" way (away from the target point). That'll give the tunnels a more of a zig-zaggy feel, but still having an overall direction.
Thanks this is a lot easier than what I was trying before. Any suggestions on how to know if it hit its own room, I figured out its own tunnel but I'm still stuck on the room.
The easiest way to do it would probably be to carve out the room after you carve out the tunnel. That way it'd be impossible to hit the starting room because it doesn't exist yet.

EDIT: Actually that wouldn't work because then you wouldn't collide with other rooms either.

Another idea: If the rooms are rectangular you can keep track of their bounds and if you run into a room tile within those bounds you know it's the starting room.
Last edited on
Next problem is that not all rooms are connected. They are always at least connected to one other room but every room is not connected to every other.
Er... wait... so... you could have isolated regions? Do you mean something like this:


############            ############
#..........#            #..........#
#..........#------------#..........#
#..................................#
#..........#------------#..........#
#..........#            #..........#
############            ############


############            ############
#..........#            #..........#
#..........#------------#..........#
#..................................#
#..........#------------#..........#
#..........#            #..........#
############            ############



Why would that be desirable? Wouldn't that result in large portions of the map simply going unused? And/or the player getting trapped in an area with no exit?
Last edited on
It is like your picture above, the rooms are connected to at least 1 other room. But those 2 rooms may be isolated from the rest of the dungeon. How can I get those isolated sets of rooms to connect with the rest of the dungeon.
OOooohh... you're saying that's what's happening. I thought you were saying that's what you wanted.

One way I can think of to solve this is to alternate between room/tunnel carving, rather than carving all rooms, then all tunnels.

ie:

- Carve out first room
- Carve out 2nd room
- Carve tunnel connecting them
- Carve out 3rd room
- Carve tunnel connecting 3rd room
... etc
Thanks so much for your help Disch. This is about what most my maps look like.

                  /-----\#####                                     /---------\
                  |>....##   #                                     |.........|
                  |.....|    #       ###### #  ###                 |.........|
   /----------\   |.....|    #########    ########                 |.........|
   |..........|   |.....|        ###             #                 |.........|
   |..........|   |.....|          #######       #/------\         |.........|
   |..........|   |.....|                ###     ##......|        ##.........|
   |..........|   |.....|                  ##     |......|        #|.........|
   |..........|   |.....|                   #     |......|      ###|.........|
   |..........|   |.....|                   #     \------/      #  \---------/
   |..........|   |.....|                   #####               #
   |..........|   |.....|                       #               #
   |..........| ###.....|                       ###   /---------#\
   |..........### \-----/                         #####..........|
   |..........|                                       |....<.....|
   |..........|                                       |..........|
   \----------/                                       \----------/

Topic archived. No new replies allowed.