Not all the creatures in the caves are mindless monsters that will attack until one of you is dead — though some particularly stubborn, foolish or brave ones will. Most creatures in the game have a fear value that can cause them to run away if it gets too high.
When a creature gets damaged, its fear goes up by the % of health it lost compared to its max health. For example, if a creature has 50 max health, and gets hit for 10 damage, that’s 20% of its total health, so its fear goes up by 20. Most creatures then have a set bravery value, and if their fear goes above that value, they’ll run away. Here I’ve set the caretaker’s bravery to 5 to show how this works:
Certain abilities can increase fear (for example, there are banshees in the game who have a wail ability that causes fear), and having certain conditions (poisoned, cursed, etc.) can increase or decrease fear or bravery. Creatures that like to keep distance between them and their enemies (usually spellcasters or other ranged attackers) also have their fear increase when enemies are too close.
When a creature runs away, it uses the so-called “Dijkstra maps” I talked about earlier, but instead of moving to the tile that’s closest to an enemy, they pick a tile that’s the farthest away from them.
Creatures’ fear decreases by one every turn, if it’s not near any enemies it decreases by two, and if it hasn’t seen any enemies in a long time, it decreases by three. So, a creature that is afraid of you and runs away will eventually stop being afraid (even if it doesn’t heal).
There are certain other things that increase fear, too, like seeing a nearby creature explode or get possessed. I wanted to implement a generic event system and have creatures react to those events, to have lots of things able to influence creatures’ fear. Unfortunately, I’ve had to cut that feature out, because it’s not necessary and it’s just something that would add move dev time, which is really not what I need right now, and the things that increase fear are just hard-coded instead.