Generating a level

This section will have a look at level generation, how different parts of the software work together to create a new level and how to add new levels into the game.

Overview of generating dungeon

Dungeon is used to represent playing area of the game. It contains levels which player can explore.

Dungeon is generated by pyherc.generators.dungeon.DungeonGenerator.

Adding a new type of level

Adding a new level is quite straightforward procedure, when you know what you are doing. Following section will give a rough idea how it can be accomplished.

Level generator

In order to add a new type of level into the game, a level generator needs to be written first. It has a simple interface:

(fn generate-level [self portal]

Arguments supplied to this function are:

  • portal - Portal at an existing level, where this level should be connected

Shape of the level

One of the first things for our level generator to do, is to create a new Level object:

(new-level model)

This call will instantiate a Level object. Note that the level initially has no dimensions at all. The datastructure used will allow level to grow to any direction, as much as there is memory in the computer (more or less anyway). Now the level generator code can start modifying layout of the level:

(for [y (range 1 39)]
  (for [x (range 1 79)]
    (floor-tile #t(x y) :stone)))

Adding monsters

No level is complete without some monsters. Next we will add a single rat:

(add-character level (.find-free-space level)
               (creature-generator "rat"))

Adding items

Our brave adventurer needs items to loot. Following piece of code will add a single random food item:

(add-item level (.find-free-space level)
          (self.item-generator :item-type "food"))

Linking to previous level

Our level is almost ready, we still need to link it to level above it. This is done using the Portal object, that was passed to this generator in the beginning:

(when portal
  (let [[another-portal (Portal)]]
    (setv another-portal.model model)
    (.add-portal level another-portal
                 (.find-free-space level)

First we create a new Portal and link it to our Model. Then we add it to the new level at random location and link it to portal on a previous level.

Linking to further levels

If you want to this dungeon branch to continue further, you can create new Portal objects, place them on the level and repeat the process above to generate level.

Another option is to use proxy level generators, that will cause levels to be generated at the moment when somebody tries to walk through portal to enter them.

Adding level into the dungeon

Now you have a generator that can be used to generate new levels. Last step is to modify an existing level generator to place a portal and create a level using this new generator. If that step is skipped, new type of levels will never get generated.