First, I’d like to say that I love the concept and am pleased with most of the work so far.
However, the stuttering bug is something I believe may have a simple solution.
Normally, Stonehearth runs like glass on my machine, with frame rates well above 60fps. However, every few seconds, the game will pause for about a quarter of a second. (Please let me know if I’m the only one who experiences this, as I might be crazy…)
As a developer, I was curious what was causing it, so I set to bug hunting.
What I’ve discovered so far is that the stutter occurs when a villager finishes a task and takes up a new one. There are likely a bunch of things going through the villager’s mind at that point, but most should take on the order of micro seconds. Pathfinding, however, takes a long time. When you’ve got a 3-dimensional space of 400,000 pieces, pathfinding takes an eternity.
This, I suspect, is where the problem lies. After deciding on a task, a villager must ask the pathfinding algorithm how to reach its destination. If this is a blocking algorithm, then all other update logic will stop until the algorithm returns - hence, I believe, the stutter.
Unfortunately, asynchronous logic is a tricky business. I can propose 2 solutions, but without seeing the code base it is impossible to suggest which is better.
One option would be to spawn a new thread containing the pathfinding algorithm whenever a villager wishes to travel. This would likely be the simpler of the two approaches, as it only requires the villager to hold onto the thread handle between update frames, until such time as the algorithm returns.
The other option would be to create every villager in their own thread, where they can spin their blocky wheels all day and night without affecting the rest of the simulation. This is more complex as it would require the use of semaphores, and the draw and update functions would need to be able to communicate with villagers between threads. However, this solution may open new possibilities further down the development road, as different entities could potentially communicate without the need for a central commander (for example, combat could take place between a villager thread and an enemy thread, where the only communication to the outside world would be to read the terrain).
If this has already been addressed and/or I’m totally off the mark, please feel free to delete this.
I look forward to seeing how Stonehearth develops!