This week did not go as expected, but it went well!
I initially planned to work on the idle behavior. When left with nothing to do, greenfeet would stay still, and it felt unnatural. As the world of Greenfeet Haven must feel dynamic and alive, I decided to add some random actions. Idling greenfeet would be walking around, sitting somewhere, or talking to each other.
It might sound funny, but after an encouraging start, I realized that the game engine provided no easy and efficient way to scan for nearby random things. In order to decide what job to do, greenfeet would always need to explore all reachable jobs, compute a score based on distance and necessity, and then take the highest one. This costs a lot. And on top of all that, the way my spatial index was designed didn't allow me to explore available items by nearest first: I'd have to scan the entire list of positions and then try to compute a path to each. I could have kept things that way and just computed distances "as the crow flies," but then cases like this would have been problematic:
In the screenshot above, it is obvious that the tool on the right is the nearest. But by ordering the spatial index using distance "as the crow flies," the tool on the left would come first.
Luckily, I had something in store that would solve it all: the region system. This is not something I invented. I discovered the concept by watching an old RimWorld video made by Tynan Sylvester himself. It had been sitting at the bottom of my backlog for a while as I had no urgent cases it would help with. I encourage you to go watch the video for a good introduction to the subject.
I spent the week implementing the concept in Greenfeet Haven, and it is now fully operational.
The world is divided into regions with a maximum size of 8x8. Any position within a region is accessible from anywhere within the same region. This means that if you share the same region as an item, it is reachable! It now takes only a few microseconds to get the list of available entities nearby. It even allowed some optimizations in other parts of the game.
In the end, I couldn't manage to implement all the random idle actions I wanted, but I laid strong foundations for them and also improved the overall performance of the game! And it even allowed diagonal walking, as the old open source pathfinding library was replaced by my own region-based one. I plan to finish implementing the idle actions next week, and then start working on the visual assets to make them production-ready.
That's a wrap for this week, thank you for reading!
PS: don't forget you can wishlist Greenfeet Haven on Steam, so you don't miss when it's out!