Procedural Dungeons

I’m working on a dungeon crawl and so far I have sort of procedural dungeons, here’s the basics:

  • import models from Cinema4D - these are similar to morrowind/oblivion dungeon tiles
  • add entrance and exit(s) in Unity - basically non-rendering spheres placed in the middle of the portal, oriented with the x-axis pointing in the direction of the portal (so for the entrance pointing into the room, for the exit, the x axis points out of the room)
  • oh also these room pieces all start around 0,0,0 - and all face into the x-axis (except for the caps)

So using this I can make a long, snake like dungeon. Basically it chooses a random piece, aligns the new piece to the exit transform of the preceeding room (remember they’re all aligned the same, using 0,0,0). This part works great - I could have an exit at any angle/location (so far the portals are the same size). So downward corridors are no problem, I can also have multiple exits, though I haven’t coded for this yet.

Also I’ve started adding a few details, in this case, I have lights (little glowing crystals, with a light attached and my torch script for flickering). Now this might be a bit backwards, but for some things it will work: I place lights in every possible place I want them in each dungeon tile. Then there’s a despawn script that gives it a 1 in x chance to despawn on Awake. So any given room could have no lights (most likely), or multiple lights, including rare cases of larger lights. I can extend this to other details. Also I’ll create random spawn stuff as well.

So obviously there’s lots of work, but I can add in the multiple exit code, and basically add unused exits to a stack (the code currently caps the last room when maximum number of rooms is hit).

Here’s where I’m having conceptual issues - I’d like for more complicated mazes - currently I basically track how many right or left turns with a counter and make sure I don’t turn left or right more than once - but I’d like to be able to turn more than that, but also especially create loops with exits linked. I haven’t quite figured out how to do this, but have some ideas:

  • some scheme where by I check for bounding boxes being in the bounding boxes of existing meshes when I place my piece - (oh I should note I yield after creating each room to give the new room time to initialize) - I could remove an offending new room and try alternatives for a set number of tries before capping the room off and moving on to the next exit. This wouldn’t, however, really let me know what exits are capped by closing a loop - maybe some sort of collision of the actual exit spheres?

  • I was also thinking of tracking based on testing line segment intersections - basically between the entrance/midpoint and each exit/midpoint of every existing room - might get way slow after iterations, but also doesn’t solve the issue of whether exits are closed off.

  • do not try and close loops at all - just fudge it with prefabs that contain loops, just snake out from any additional exits - would limit complexity

Anyways the bit I have going so far actually looks very promising (heh had trouble sleeping last night as my mind was racing with possibilities). And of course there’s just the issue of monsters, combat code, inventory code, etc once I have the generator complete. The end idea should be a rogue like. I will create an entrance and an exit, and when you hit the exit it will generator a nastier next level (hopefully with slowly shifting tilesets as you go deeper).

What board games of this kind use is simply a tile system. If all your exits/entrances are the same size and place (i.e. they all fit on each other), then you can build up pretty much every 2D grid you want and it’ll be a maze.

Yeah I thought of that, but I wanted to use at least somewhat arbitrary tiles…not really tiles so much as prefabricated rooms corridors. Though maybe I should at least look at certain sizes. So set a minimum of 9x9 Unity units, and make sure all my rooms are a factor of this? (Even if they are longer etc…hmm, or possibly always lock to a square grid?)

I’d be inclined to make it a grid/tile like thing also. (doesn’t have to be square at all)

but instead of starting straight away dropping in your rooms. Do a preset round just a matrix of Os and Xs representing where rooms could be. (You could leave Xs out completely if want so just have Os and blank spaces)

For example

oxxxxxxxxx
oxooooooxx
oxoxxoxoxx
ooooooooxx
xxoooxxoxx
xxxoxxxooo
xxxoooooxx
xxxxxxxxxx

so make you algorithm to place an o (or whatever identifiers you need). When it places a branching path, or a room with multiple exists it flags that there needs to come back and do the second path also.
You can iteratively or recursively let it randomly build you level.

Once it has the matrix made go and place in rooms that make sense. Like it you have 4 Os in a square it becomes a slightly larger room. Or even easier…instead of dropping in rooms on Os you will in the Xs with walls

The benefit of this is you can gnerate your random building algorithm quickly and fast. Check results by jsut printing the matrix in debug to see if the layouts are making sence. Once the generator makes sence then work on substituting in real geo.

-LW

Hey guys, got it somewhat working, moved to showcase thread.