Procedural generation of square rooms?

I am attempting to create a square room layout generator so that I can generate random levels with different types of square rooms which I have made, though I am stumped. I am unsure how to create an effective algorithm so that I can place rooms and also detect if a certain route is blocked so that the generator doesn’t create two rooms in the same place or so that it doesn’t place rooms in the way of doorways.

Image of square rooms plan:
image

Any help would be appreciated on how I could do this.

1 Like

I think collection service would be very useful here. I think the best way of going about this would be to create a wall with an entrance and a wall without. These could be placed into replicated storage. They could be tagged via collection service and tag editor so you know where there is an entrance and where there isn’t. I would also make a grid of same sized ‘plots’ which could be tagged when ‘occupied’. A for loop which runs through the ‘plots’ which aren’t occupied could randomly assign a wall with an entrance or without would work. I definitely think you would have to be careful in your checks, though, so that each ‘room’ has at least one entrance.

You could also do an if check for if there is already a wall where the new wall has been placed. Maybe .Touched? Probably a better way of doing this though.

1 Like

I’ve tried these but I still cannot get it to detect closed walls. Im pretty sure ill have to create an algorithm for it or something for it to generate properly.

1 Like

To determine if two rooms are in the same group, the problem can be reduced to the union-find algorithm and solved by a disjoint-set data structure in near O(1) for a single lookup. Disjoint-set data structure - Wikipedia

For making the rooms, you can partition the space into a fine grid, and then randomly pick a cell / room and expand out in any direction. Performing this on a cell creates a two cell room, and performing this on a room expands out the entire wall adding either a column or row of cells to the room. If a cell / room cannot be expanded, then we take it out of the list of cells / rooms for expansion. Once all cells / rooms are of out the list for expansion, we determine which are adjacent. We then form doors between randomly picked cells / rooms until all rooms are connected. This is inspired by the randomized Kruskal’s algorithm for maze generation: Maze generation algorithm - Wikipedia

If any cells remain or rooms smaller than desirable, then I would remove them from the list of available rooms / cells for doors half way through the algorithm. Unfortunately this may make clusters of rooms unreachable. You may provide teleporters between, or add the rooms back after all possible clusters have been made without them and perform the door algorithm again. Finally, prune any undesirable rooms which don’t form a connection between two valid groups of rooms.

3 Likes