Down to the Wire โ€” World Generator

I’ve finally moved on to the elephant in the room. (Well, in my room)

The world generator.

It will work as thus:

The world is a square grid that is 600 tiles on each side. The outer 50-cell border is always filled with wall tiles.

The grid is stored as a two-dimensional array of ints. These represent the various objects (Or lack thereof) that may be located on that space.

The generator started by carving out 10 rooms, which are rectangles that are 20-80 tiles on each side.

Each room is limited to its own area in the map, and those areas only have a little bit of overlap. This keeps rooms from clumping together into an uninteresting level.

Then, twenty large tunnels are carved between the rooms. The generator will take care to make sure every room is connected in this way. These tunnels are either vertical or horizontal, for simplicity.

After that, twenty-five small tunnels are carved. Fifteen of these go between rooms, and ten go between large tunnels. These are also either vertical or horizontal. These tunnels are too small for enemies to get into, but the player can easily fit into them.

Then, the rooms and tunnels are carved into the grid.

The generator then puts 1 or 2 enemies in each room, except for one. That one will not overlap other rooms, and will always be 20-40 tiles on each side.

Then, it decides where the player starts. The player will always start in the room without enemies, but the generator decides where in that room the player starts.

Finally, the generator will pick between 50 and 200 tiles to place a passable floor tile on, to add a bit of detail to the cave.

Once the grid is laid out, the generator parses it into a world. It runs through the grid left-to-right and top-to-bottom.

When it encounters a wall tile, it creates a terrain tile object, then checks the area around the point it found the tile at.

If the tile is bordered on every side and corner, it is placed into a vector of passable terrain tiles. These will not be checked for collisions in-game.

Otherwise, the tile is placed into a vector of massive terrain tiles. These are the ones that will be checked for collisions.

The outer edge are always placed into the passable vector. After this, everything is handled by the parsing code.

When it encounters an enemy spawn point, it makes a baddie at that point, and then sticks it into a vector of baddies. These are, of course, your enemies.

When the parser encounters a floor tile, it makes one, and places it into the vector of passable terrain. This simply makes sense.

When it encounters the player start point, it records the point. This is passed to the main loop with the objects, as the main loop builds the player itself. The main loop also records the spawn point for each enemy.

Then, the game begins.

If you die, the terrain is saved, and enemies are placed in their original points. The terrain is preserved.

If you defeat all of the baddies in the cave, there is a short delay and a victory sound plays, then the game generates a new world, and the cycle starts anew.

In addition, if I have time, I will put in a health bar for the player. This should be useful. :-)

Here’s to pulling through in the face of the deadline! ๐Ÿ˜€

I hope…

—Akake