I made good progress last weekend and finalized the pathfinding algorithm and worked on a data management solution for monsters, items and other things.

pathfinding

I completely rewrote the pathfinding algorithm and added some features that allow me to work with flying and swimming creatures (or players … who knows). Bitflags are now defining how the actor that uses the pathfinder moves through the world.

I also added a dynamic layer ontop of the pathfinding algorithm that pulls data from another TileMapLayer which holds the representation of blasts, gas and other temporary hazards. The algorithm checks whether the actor is not immune to a certain hazard and a high waypoint cost is applied to the cell. I also plan to include a check on “necessity” which overrides the costs. For Example if a much higher degree of danger forces the actor to flee the area and the only way out is through the lesser danger area.

data representation and storage

I implemented an object library which holds the data for monsters, items, weapons etc. This is just a JSON file that gets read at startup and the item/monster spawner pulls the relevant data according to the current id of the object to create. This works quite well.

The world state will be written to encrypted json files. A large container collects the visited parts of the world, like dungeon level etc. Those chunks hold the items, monsters and everything else present at the time the player left the region. I minimized the amount of data to be written by just storing object ids, cell position and changing values like hit points. All other information about the object can be pulled from the object library using the corresponding id.

monsters & ai

The monsters and enemies which are roaming the world are implemented and … roam. Each entity holds the data from the object library, a sprite with a region rect, the pathfinding agent, a collision shape, an inventory and a behaviour tree.

The collision shape is used to trigger a sensor sitting on the player object to reduce the amount of queries needed to register interactions. The inventory is mainly used for special quest gear like keys etc. When a monster or NPC dies, loot will be generated and stored in this inventory.

Most of the AI is done using behaviour trees. I prepared behaviours for different categories of enemies and NPCs. Which one to select is also stored in the object library for each monster. The main rules of each behaviour tree are:

  • Engage the player on sight
    • At any cost if you are a dumb critter
    • Is it necessary to engage the player or do I like to be alive?
  • Attack the player and chose the kind of weapon, skill or spell
  • Follow the player around until you lost visual contact
  • Seek for the player at the last known position
  • Wander around and do your “patrol”
  • Do your daily buisness in case you are a NPC

The first thing I did was to restrict the movement radius of an enemy. At first the behaviour tree told the monster to chase the player as soon as they are in sight for as long as possible. If the visual contact is interrupted, the agent seeks for a while to get the trace back. Seems ok on the first look but this could lead to completely empty sections on the one hand and overly crowded ones on the other hand. To prevent an agent to endlessly follow the player, I stored the initial spawn point and provided a movement radius. If the agent is leaving said radius it will cancel the hunt and move back towards the center of its designated area.

quest system

I started working on a quest system which can operate on hand-written and procedurally generated quests. Currently the system consists of the following building blocks:

  • Bring: Bring an item to a specified location or NPC
  • Collect: Collect a number of specified items
  • Condition: Is a custom condition met?
  • Kill: Kill a number of monsters, NPCs or a special entity
  • Reach Depth: Reach a specified dungeon level
  • Survive: Survive or wait a number of turns
  • Talk: Talk to a specified NPC about a given topic
  • Use: Use an item
  • Visit: Visit a location, optional in a given time-frame
  • Group: Selectors like “Any”, “All”, “Some”

The quests can be nested and each quest can give a reward.

Due to the procedural nature of the world, I had to implement a few methods that can generate location, item and NPC identifier on the fly. For example a quest to visit the City of Lankhmar would work if the world is static and be called Nehwon. My system first decides whether the location is a previously visited or an unknown one. Then it pulls the id of the location if it has already been registered to the world. Otherwise a resolver generates the id, name and position of the location and registeres it with the world state. The same happens if the quest then tells the player to talk to Fafhrd and the Gray Mouser who should wait at the location. Another resolver tells the world generator to queue the NPCs and generate them once the player arrives at the location.

To check if a quest has been completed, the system receives a singal on every event in the game: the player enters a location, kills a monster, talks to somebody. The receiver will check whether the signal is part of an open quest and adjust the progress.

So, that might be it for this week! May the wyrm have mercy on you.