Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 01:19:50 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Randomly generated dungeon algorithm(?)
Pages: [1]
Print
Author Topic: Randomly generated dungeon algorithm(?)  (Read 1002 times)
Eggy
Level 0
**


View Profile WWW
« on: August 17, 2017, 03:42:07 AM »

Alright, so chances are I might be diving right in unnecessarily as I haven't done my own experimenting, but... I'm not much of a programmer (I did try things here and there outside of Game Maker but all of it is more or less simple console stuff with the exception of one Allegro image masking test) and I have no idea how to handle algorithms in general, so at the very least please help me with those Beg
So let's say I want to create an engine that generates a random PMD-esque dungeon, one with rectangular rooms of varying sizes that are connected by corridors with items sprawled around and a staircase placed in one of the rooms, in Game Maker (either 8 or Studio, preferably the former). While I'm not entirely sure how to program that, I have a plan of how I want the (bare-bones) process to go. Basically this:

Step 1: Prepare a big generic block for the wall tiles. This will be the max size of the dungeon floor that's x tiles wide and y tiles high.


Step 2: Lay out a randomly generated number of rectangular blocks onto the bigger block. These will vary in size and will serve as rooms, so they are where ground tiles will be placed (i.e. we're carving hollow areas in a solid block). They must have an offset that prevents the rooms from touching, coming close to or going over the big block's edges.


Step 3: Create corridors. They are virtually the same as rooms, except they have a fixed value in 1 direction (i.e. a corridor going horizontally has to be 1 tile high and a corridor going vertically has to be 1 tile wide) and they have to connect to each other, i.e. a corridor tile has to be adjacent to either a room, a room and another corridor tile, or up to 4 other corridor tiles, and in each case it can't be in both x and y direction i.e. diagonally. All corridors have to be connected to at least one room and all rooms have to be connected to at least one corridor that leads to another room. In addition, there need to be enough corridors to allow access to every generated room.


Step 4: Place objects into rooms. At the bare minimum there must be a staircase object placed in one of the rooms, an enemy spawn object (so that enemies can spawn on room tiles periodically), and a spawn point for the player in one of the rooms for when the dungeon floor is finished generating and the player starts playing it. Additionally, room tiles may also spawn other objects such as items, traps and pre-spawned enemies to greet the player.


Step 5: Apply the tileset on the generated dungeon floor layout. For a case where a ground tile and a wall tile connects, both kinds will have a different image depending on where and how many of them connect.

I hope you can see what I want to achieve. The only problem is that I am not entirely sure how to go about it in GML. In other words, I know the process I want to use but not how to translate this into code that the programming language understands. Game Maker's grid data structure function looks like the way to generate the bare-bones layout but after that I don't know how to continue from there.
So my question is, how do I tell Game Maker to use the grid to create something that looks like this (I hope this pic is sufficient)? How do I implement the rules so that the generator generates the above and not a mess of randomly placed tiles? Any tips on implementing algorithms in general?
By the way, I would just like to try replicating (what I see as) PMD's apparently simple dungeon generator process before I try going for other people's existing algorithms. Hopefully I'm not biting off more than I can chew Shrug
Also sorry for possibly making this post a huge one guys! I've tried to elaborate my problem in a way that helps you help me, but if you have any questions of your own (e.g. if I wasn't clear somewhere) feel free to ask. Thanks in advance
Logged
Foxwarrior
Level 1
*



View Profile WWW
« Reply #1 on: August 21, 2017, 10:23:44 AM »

To place down the graphical tiles for your dungeon, you can use tile_add. To determine whether to place down a corner tile, front wall tile, floor tile, etc, you can use a sort of cellular automata-like approach. You go through each grid square, and look at the adjacent grid squares to determine what type of tile to put down.
Logged
Eggy
Level 0
**


View Profile WWW
« Reply #2 on: August 21, 2017, 11:32:16 AM »

Thanks man, I should try doing that once/if I figure out the rest of the algorithm Smiley I actually used draw_sprite to draw the (placeholder) tiles (but as you mentioned I could use tile_add instead) and I was thinking of doing a system that layers the wall edges properly so that I only have 9 different wall tiles per tileset at minimum to worry about (4 for corners, 1 for the middle part and rest for the outer parts).
Also, in the meantime I've tried experimenting on my own and I've managed to get the first two steps down, using regular arrays instead of ds_grid (it looks a bit more complicated so I'm sticking with arrays for now). Basically I got the max room block generation step just right, and I've managed to get step two more or less solved especially concerning rooms; I had trouble with writing code that checks for free spaces to prevent overlapping rooms, but a more experienced programmer in another community helped me out during one of his streaming sessions and wrote code that properly tests whether the block/room that's about to be placed overlaps any tiles that are already marked as floor tiles. The only thing is that it doesn't test for whether there's any room for the block, so if there's no room for it the program hangs. I am not sure how to solve this, though right now it's not a big deal. I'll give you guys the script code - or just the check code if that's sufficient - if you need it to help me figure stuff out.
For step 3, generating corridors, I was thinking of a code that searches for a room's edge (i.e. room floor tile that's adjacent to a few wall tiles) and then moves itself one tile horizontally or vertically, checks for adjacent tiles, places the corridor floor tile (to be later turned into a normal floor tile) and repeats this process until it reaches another room floor tile. The thing is not only am I not certain how would this translate into code that works properly, I also don't know how to make sure the corridors will reach every room so that every floor tile is accessible. I'll try something although tips on what I can do would be very welcome.
Logged
bombjack
Level 3
***

That's me :)


View Profile WWW
« Reply #3 on: August 21, 2017, 12:28:04 PM »

Here is a very interesting link on procedural generation in Space Grunt: https://techblog.orangepixel.net/2015/02/turn-based-level-generator/
Basicly he first place a room than choose the direction and size of the corridor before placing the next room.
That's maybe simpler than placing all room and try to connect them afterwards.
I hope it will help you.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic