Roguelike AoC – part 11

An eleventh lesson of roguelike game tutorial creates an additional layer that represents the whole level. I was thinking about such solution a couple of entries ago. Below I describe how I’ve developed this concept using my existing code.

So what are we actually saving?

In the previous lesson I’ve actually fixed a couple of bugs. However, I did not implement the solution that the author suggested, which was to store all the positions in the two-dimensional array. With this lesson I think that right now a time has come to do that, but not with storing Position instances, but with Location. My plan is to actually generate the dungeon as it was, then parse the whole output, and put locations in the Level struct.

This actually creates a somehow mixed model – first we use object-like concepts to create the output, and then we put everything into Level as separate Locations. Level struct also has some additional metadata (like suggested room numbers) which can be useful, but still the main concept and rendering will be based on aforementioned two-dimensional array.

After giving, it some thought, I think that for our purposes it will be sufficient. If I ever will like to make eg. not-equal door generation algorithm (that there is at least 1 door in one room, and exactly 3 in the next one), that will still be happening in the createRoom function. However, after it has been done we do not care about this rule/limitation and just treat every room as a gathering of locations.


Preparation phase

This is the job that was done in the previous lesson (on Youtube). Now we’re doing it in our code.

First of all – we have to create level.c file and add it to the src folder. Next we must add to the add_executable method in CMakeLists.txt file. So far so good. A third step is to rename mapSetup to roomsSetup. In order to make everything more customisable we declare new macro in rogue.h like this:


And we change the signature and method contents of roomsSetup.

Room **roomsSetup(int numberOfRooms);


New level structures and functions

Due to the fact that I do things differently 😉 my implementation of Level struct is a little different. I’ve decided that all I need is actually just a number of rooms and level number (as metadata) and a whole list of all the locations. So the definition looks like this.

typedef struct Level {
Location **locations;
int numberOfRooms;
int levelNumber;
} Level;

And the created code to handle level initialisation is this:

Level *createLevel(int level, int numberOfRooms)
    Level *newLevel = malloc(sizeof(Level));
    roomsSetup(numberOfRooms); // We setup all the rooms and connections on the screen

    newLevel->levelNumber = level;
    newLevel->numberOfRooms = numberOfRooms;
    newLevel->locations = saveLevelLocations();

    return newLevel;

Location **saveLevelLocations(void)
    int x, y;
    Location **locations  = malloc(sizeof(Location *) * 35);

    for (y = 0; y < 35; y++) {

        locations[y] = malloc(sizeof(Location) * 50);

        for (x = 0; x < 50; x++) {
            locations[y][x].displaySign = unctrl(mvinch(y, x));
            locations[y][x].position.y = y;
            locations[y][x].position.x = x;

    return locations;

That’s all for this lesson. In the next one it seems that monsters will come to live.



Here is the direct link to the GitHub repository.

You Might Also Like

Leave a Reply

Back to top