Hey guys. I’ve been working on this idea for about 3 days.
Basically I did not know how to generate a maze, so I thought, why not try?
Except, I want to use random rectangles instead of fixed cells for tile traversal.
I use a binary space partition structure to generate random slices of rectangular space, then I have the following rules:
- if there are no rectangles on the stack, there are no more positions to move. The maze is done.
- If the rectangle adjacent to our current position has been checked, do nothing.
- If the rectangle adjacent to our current position (a) hasn’t been checked, but all rectangles adjacent to (a) have been checked, add (a) to the checked list. The current rectangle becomes that first one off of the stack, and this whole process repeats.
- If the rectangle adjacent to our current position hasn’t been checked, add all rectangles across from (a) to a list. Out of that list, pick one to keep (b), add it to the checked rectangles, and to the stack. Add the adjacent and across to the set of rectangles that are of the path. Get the adjacent tile opposite of the direction we just checked, and add it to the checked list.
When the maze can no longer add rectangles, I have it erase dead ends until all rectangles have 2 or move neighbors.
Splitting the whole rectangular world into smaller rectangles: http://pastebin.com/U6A1Evuh
and the class to make the rectangles a-MAZE-ing: http://pastebin.com/aSaAiBWJ
When I display the result, it looks something like this
This is without the removal of dead ends
My next goal will probably be checking of adjacent rectangles, and combining the ones that have matching sides in the direction they are being checked.
So if I check A and B and A is above B, and B’s width is equal is A’s width, with their x positions being the same, they combine their area.
So that rooms are flush (and we don’t have a corridoor that leads into a corridor.
The door placement is possible with this:
Red = rooms with 1 doors.
Yellow = rooms with 2 doors.
Green = rooms with 3 doors.
Magenta = rooms with 4 doors.
White = rooms with 5 doors.
Cyan = rooms with 6 doors.
public SetPairMap<Rectangle,Rectangle> doormap = new SetPairMap<>();
public void placeDoors(){
for(Rectangle r: path){
for(Rectangle adj: getAdjacent(r, false).getValues()){
if(path.contains(adj)){
doormap.put(r, adj);
doormap.put(adj, r);
}
}
}
}
Just to clarify, the rooms that are red only see they have one door that THEY themselves generate. They can have more than one, but they don’t store their parent door. I may update this later when I show rooms that have their total doors displayed by color. As of now, it looks like red rooms could isolate from the rest of the path.
Finished Product pretty much:
Without sticking squares in. (Squares filling in the spaces makes the mazy look. This is better imo).