Spellcaster Studios

Make it happen…

Enemy base generation

Well, some people came me to me and asked me how I was generating the procedural bases, so I decided to put this up to explain my ideas…

Note that this isn’t ground-breaking stuff! I’m using it to build the pirate lairs (and space bases, etc), but it can be applied to any kind of structure, and it’s easy to add logic for staircases, multiple floors, etc)…

Also note that although Chrome Hunter is a voxel-based game for the environment, nothing in the system is tied to that representation (specially because when I generate the pirate lairs, the system doesn’t even have voxels in it!).

So, steps to build a floor plan:

1) Generate X times Y rooms, in a regular grid… These numbers can be adjusted depending if you want larger or smaller rooms… Note also that if you have a specific need, nothing in the algorithm really need the grid to be square (although a grid is necessary… you can probably extend the algorithm, but it becomes much harder)

screen61

Next, generate doors between rooms… I randomly generate between 0 to 4 doors, with random directions:

screen62

Note that this might generate dead rooms, but that’s no problem…

In my algorithm, I then kill random rooms, and remove all that don’t have a door to them… In the screenshot below, if the room has been killed but it’s “internal”, it shows up as a solid blue block, otherwise, if it’s external, I just remove the walls:

screen63

Next step is guaranteeing that all rooms are accessible… To do this, I use a “painting algorithm”, and expect it to yield only one area… if more than one area appears, I scan the map and open a door between different areas… in the case above, I only had to open one door:

screen64

Then, things start losing the “grid” look: I merge adjacent rooms that are connected… For this, I ask for a random room and see if I can merge it with an adjacent one… To be able to merge them, they have to have a door to one another, and have to have the same size on the other dimension (so if it’s a east/west door, they have to have the same north/south size)… I repeat this for some random number of times:

screen65

Then, to break even more the regular grid look, I move walls around… That means that I move a wall perpendicular to it’s direction until I find a wall. Again, this is randomized:

screen66

Finally, I analyze the map and decide where to place traps:

screen67

All of this works through a "room/door structure”, which in my case looks like this (I’m lazy):

struct plDoor
{
    int dir;
    int target_id;
    int size;
    int pos_x,pos_y;
};
typedef std::vector<plDoor> plDoors;

struct plRoom
{
    int    id;
    int    x,y;
    int    sx,sy;
    int    index_x,index_y;
    int    area_id;
    int    distance;
    plDoors    doors;
};

So, there’s no reference to voxels, just some reference to a static grid-like system… This could be probably changed by using local reference frames and rules to merge that are slightly different, but that’s probably a lot of work!

So, hope this helps in someone’s endeavors in the world of procedural generation!

There’s one million awesome papers would a lot of information on how to generate more realistic stuff, but for more purposes, this was great!

Comment