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:
def 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 = Level((80, 40), tiles.FLOOR_ROCK, tiles.WALL_GROUND)
This call will instantiate a Level object, set it size to be 80 times 40, create floor of rock and fill the whole level will ground wall. After this the generator can create structure of the level as wanted.
for y_loc in range(1, 39):
for x_loc in range(1, 79):
new_level.walls[x_loc][y_loc] = tiles.WALL_EMPTY
Adding monsters¶
No level is complete without some monsters. Next we will add a single rat:
monster = self.creature_generator.generate_creature(
model.tables, {'name':'rat'})
new_level.add_creature(monster, new_level.find_free_space())
This will instruct pyherc.generators.creature.CreatureGenerator
to
use supplied monster tables and create a monster called ‘rat’. After this,
the rat is added at a random free location.
Adding items¶
Our brave adventurer needs items to loot. Following piece of code will add a single random food item:
new_item = self.item_generator.generateItem(model.tables, {'type':'food'})
new_item.location = new_level.find_free_space()
new_level.items.append(new_item)
This will instruct pyherc.generators.item.ItemGenerator
to use
supplied item tables and create random food type item. After this the item
is added to the level. This portion of the Level interface will most likely
change in the future, to match better to the interface used to add monsters.
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:
if portal != None:
new_portal = Portal()
new_portal.model = model
new_level.add_portal(new_portal, new_level.find_free_space(), portal)
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.