Generating a random map for my game
I’ve dabbled with this a bit in the past, using a grid to randomly generated rooms in a 2d game.
I used a very overly complicated process before, I started with a two dimensional grid and I converted each room into a symbol that stood for a different type of room, then when I tunnel through that room, converted it into another variable ie “T” = a room with only a top wall, and X would be a room with no walls etc…
I completely scrapped that idea this time around! And I couldn’t be happier about it. Instead I made a 3 dimensional array of bools. two dimensions for X / Y (or x / z as it is in unity) of the map, and then the 3rd held 5 bools, one for floor, and then one for each wall. North / East / South / West etc. I then start up with a new array that is all false, start off somewhere, and start tunneling my way one section at a time. I made a function that will go from one tile to a next, if there is no floor, it generates all the walls + floor, and then it breaks down walls connecting the two rooms. I found this so much easier than my old method, as I didn’t have to make a ton of cases to handle changing different rooms.
Also this time, I made a system that adds on some branches after the first tunneling function runs . I look for a random tile that has a floor, then I check for any tiles next to it that have no floors, put each result with no floor into a hastable, and then randomly choose one of those to move to- and continue doing that to make some winding tunnels that go until they get stuck, which added some very nice variation to the maze. The best part about this system, is I’m pretty sure I could easily make a 3 dimensional maze by just adding one more bool and dimension to the array and changing the randomization around a bit, which I will most likely try out sometime in the future.