One of the earlier examples of this was the game Rogue ( 1980 ). Rogue used a series of colored ASCII characters to define different objects and characters in the game. However, for the purpose of this blog we are more interested in the creation of one of the games levels. In Rogue, a level consisted of a series of rooms connected by hallways. The layout of a level was always random and always unique. The allowed the game to be played over and over always having a different play experience. Below is an example of your average Rogue level.
Here we can see distinct rooms and different hallways to connect them all. So lets make our own way to do this!
First we need to define what a room is. We do this in a Room class where we will define the two corners in an x/y coordinate system like below.
In the code above we can also see that we defined some methods that will help us later on.
Next we need to actually make some rooms on our map. Our map is simply defined as a vector of characters. When we add a room or hallway it just replaces the character to something else. First we will see the code and then we will explain it.
In the above code we first see our declaration of the RandomDungeon class. Here we define our variables for the width and height of the dungeon level and create the vector for the map. We also define our method headers here. When we go the the .cpp file we can see the actual implementation of generate_dungeon_room_map. First we loop through each room. We create a room with random side lengths of 5-10 characters. We then create them in a random location and check to make sure there are no other rooms at these locations. I ran this to create a map with side lengths of 45 characters and with 9 rooms and got an output that looks like this.
We're starting to get something now. We see clearly defined rooms, however, we have no hallways that connect them so we're going to need to make them. Below we have two methods, horizontal_hallway and vertical_hallway that will generate halls for us.
These two method simply take the location of the rooms and generates L-shaped hallways that connect them. In the generate_dungeon_room_map method there was a section commented out. When uncommented and the code is run we get a map that looks like this, for example.
This has allowed us to create the very basic parts of a roguelike level. From here we can create special rooms, different hallways, or any number of levels all by changing a few parts of our code.
Hope you learned something new. Since you only saw snippets of code you can view the full code here: https://github.com/CMilby/PCG-Blog. Next time we will be covering another form of procedural generation, expect this time, we will be talking about make caves! See you next time.
No comments:
Post a Comment