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
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.
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:
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)))
No level is complete without some monsters. Next we will add a single rat:
(add-character level (.find-free-space level) (creature-generator "rat"))
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"))
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.