Anything generated on the fly presents a challenge in maintaining game balance. If levels are designed by humans, those designers will ensure that the levels are appropriately challenging, and pre-release testing will ensure that the level designers have done their job in creating levels that are challenging but not impossible. When generating levels dynamically at run time, this testing isn't available. Furthermore, since there's no linear flow to Idamu Caverns, a player could easily take any one of multiple routes, thus arriving at a point with different equipment and buffs accumulated.
It seemed logical to me that there needed to be an explicit section of code for maintaining this balance -- a game Director, similar to what was designed for Left 4 Dead. The purpose of the Director code is twofold:
- Keep the game as challenging as the player requested.
- Keep the game fun and interesting.
The code is called primarily at level generation time. Once the map generation code has finished, the map contains rooms, hallways, stairs, and spawn points.
For each spawn point, the director is consulted at multiple times during the population process.
First, the Director is consulted to see if the spawn point should spawn a treasure. Here the director is fufilling the role of keeping the game at the requested challenge level. To determine this the Director keeps track of how many treasures have already been populated on the current map, and compares that number to a number derived from the requested difficulty, factoring in a small random element to keep things from being too predictable. A higher difficulty setting results in treasure spawning less often, while a lower difficulty setting means more often.
When it's time to populate a treasure, the director is consulted again to determine what type of treasure to populate. Here the primary goal is to keep the game fun. The code has already determined to present you with something helpful at this point, so the goal is to ensure that it's populating something useful. The Director essentially rifles through your inventory to see what you're currently carrying, and tries to present you with something that you need. This is why you won't see backpacks spawning once you already have a backpack (because there's no value to having multiple backpacks). The code here is still imperfect, so you will occasionally find items that you have no need of, but it does fair job for the most part.
Next the Director determines whether to spawn a monster. The chance of a monster spawning is always 100% if no treasure was spawned, but a factor of the difficulty level otherwise. While it seems odd to have unguarded treasure lying around, it's a matter of keeping the game interesting, as there aren't enough items yet in the game to have every monster drop something. This is code that will have to be adjusted as the catalog of game items increases.
Selecting which monster to spawn brings up another factor in the Director's operation. Each creature in the game (of which the player is one) has a challenge rating that can be computed at any time. It takes into account every factor that makes the creature dangerous, such as damage resistance, maximum health, and potential damage dealt; and returns it as a single number.
The player's challenge rating is calculated, and the requested difficulty is added. This results in the desired challenge rating of the monster to be populated. To mix things up a bit, a random element (based on the requested difficulty) may increase the target challenge rating, resulting in a monster that's a bit more challenging than would be expected on that level. The list of possible monsters is searched to find the one closest to the target challenge rating, and that monster is placed at the spawn point.
The Director does a good job of keeping the game on track as far as being fun and challenging, but it's too random. Monsters and items are spread completely at random across each level, which removes any sense of coherency from levels. I have plans to improve on this in the future. Another challenge is that every new element added to the game upsets the balance of the Director's equations and requires some tuning. This is a problem only because Idamu Caverns is being developed incrementally, and ensuring that each version is playable requires careful testing to ensure the Director is still doing it's job properly.
You should download Idamu Caverns and try playing at different difficulty levels to see how all this translates into a game. Please also consider becoming a Patron to help ensure I can keep making games and giving them away for free.
The next post in this series discusses generated quests.