This weekend wasn’t the most productive since starting this project. Due to a “family emergency” I was afk the whole Saturday. But I tried to squeeze in as much time as possible on Sunday.

dungeon generator

The dungeon generator is now finished and works great. A whole dungeon floor at a resolution of 80×80 tiles will be generated, decorated, populated and tilemapped in about 0.9 seconds. It features:

  • Rooms, caverns and caves
  • Chasms and cracks the player can fall through to the next floor
  • Water and lava pits and streams
  • Doors that can be opened, closed, locked, unlocked and hidden
  • A solid lock & key mechanism
  • An algorithm to generate potential positions for loot at the trickiest locations

pathfinding

There was nothing I reworked and completely changed more this week than the pathfinding system. Each time it gained performance and sometimes even a feature or two.

Currently it works on a shared world state for the current dungeon floor. The player, npcs and enemies can all use the same algorithm and are just working with other features. For example a bat can fly, the player maybe not. The player can open a door, a bat lacks hands in most cases and cannot open said door.

Using the pathfinding algorithm, I implemented an auto explore mechanism which queries the explored tiles using the fog of war system from last week and queues a more or less optimized path through the dungeon and tries to reach all unexplored cells. This works only for those rooms and caves where the player a) a door is not locked or b) has the key to the door.

I also added a player action to quickly go to the level entry or exit as long as those locations have been already explored.

seed system

After I considered the dungeon generator done for now, I thought about implementing an overworld for the game. I talked about a world generator in a post on my personal blog before and will implement most of the stuff I already did, with some minor changes. The most important change is that I won’t supply each cell of the overworld with a pregenerated seed.

I plemented my random number generator in a way that it takes strings as the random seed, where the string will be converted to it’s hash. This is quite risky because of the collision rate (getting the same integer for different strings), but it will work for now… I guess. I tried different implementations to generate the hash for a string and currently I’m slowly ditching the built-in hash function, because it is the least favorable imho. I also did a custom SHA256 and FNV-1A implementation, SHA256 has a much stronger collision resistance, FNV-1A is faster on the other hand.

uuid system to generate the random seeds for each tile

Here is how I will use the seeds. When the game starts and the world is generated, I create a standard verison 4 UUID to act as the world seed. The overworld will be generated with x×y tiles, most likely 500×500 or 1000×500, depending on final generation speed. Each of the overworld tiles, let’s call them region tiles rt, has a batch of 3×3 subregions or srt. Each subregion can contain a number of features srf. One of those features can be a dungeon with a number of floors. A seed for one of those dungeon floors would be:

9b696d22-431f-43e2-8c23-2cc086aa99b9-010004-23-02-15

The first chunk of the seed is the uuid of the world, in this case we have 9b696d22-431f-43e2-8c23-2cc086aa99b9. Next comes the xy position of the region tile 010004 padded with 3 zeros. The following chunk is 23, the subregion tile in the 3×3 raster, the feature id 02 within the subregion and the floor id 15 of the dungeon.

[uuid-v4]-[rt_xy]-[srt_xy]-[srf]-[floor]

So much for the theroy. Now in practice: The player can travel the overworld and press on any walkable tile a key to zoom in on the map. In our example, the player will be spawned in the center of the subregion batch on tile 11. This subregion tile will be generated using the following seed:

9b696d22-431f-43e2-8c23-2cc086aa99b9-010004-11

Now the player walks to the east and enters tile 21 with will be generated using a seed analogue to the above. Because we know the world seed, the rt we are currently in, and the srt we are moving in, we can easily reconstruct the seed and don’t have to store a seed for each tile in the world state.

If the player moves out of subregion tile 21 to the east, they will span at the western edge of subregion tile 01 in region 011004. This way the player can walk each cell of the worldmap to search for interesting stuff and the algorithm always generates a consisten verison of the environment using the corresponding biomes in the world map.

This system operates up to the floor level of a dungeon. Within the dungeon floor, a chest would get a separate version 4 UUID to be stored in the world state along with its current position and content. Each item gets its own UUID again. This way I can quickly query the world state to locate an item with a specific UUID and get it’s current location within the world.

May the wyrm have mercy on you.