Maze Generator

240x240, debug parts enabled

500x100

Vexedly asked me how to generate a maze and I didn’t have any code lying around so I decided I’d give it a shot. The best method I found was recursive backtracking, which I gave up on after I couldn’t implement it properly. Decided to just write my own maze generation code from scratch based on the best way I could imagine making a maze. Use the GenerateV3 function to generate a maze between two Vector3s. The first vector’s height will be used as the floor height and the dimensions of the maze will be rounded to be divisible by your cell size. It should be noted that the dimensions you give will not be used as the corners of the maze, they will be used as the positions of the cells at the corners (so, the center of the cell).

If you have any questions you can ask here. Thanks!

3 Likes

I went in wanting to defeat it but I don’t know where the finish thing is so :stuck_out_tongue:

May I ask how you determine the cells ? Did you put all the nodes in and then you let the code generate the maze ?

That looks really neat o.o

You should mark the end/start and insert a spawn at the start

I create all the cells and then I have the code generate the maze. The way I designed it was based on my experience with A* pathfinding. I have an open table, full of cells that haven’t been generated yet. Once I generate those cells I remove them from the open table and I add all their adjacent cells to the open table in a random order. Occasionally, based on the curly factor, some of the cells in the open table will be taken out and then added back in on the next cycle. This allows each line to generate at different speeds.

There’s no start/end because I wanted to leave the possibilities open for you to do whatever you want with it.

I was going to say—I thought A* pathfinding was the way to go with this. To make it harder I would probably use pathfinding to identify every single “easy” route and make sure there’s only one way to get to to the end (and it’s really really long).

Recursive backtracking isn’t that hard, and is a tried and true method. You can easily find an implementation of it in Lua online, as well. There’s also all of these ones, too.