The dwarf that could see more

This blog tells a story about how I was able to track down an elusive performance issue thanks to an unusual set of circumstances surrounding a dwarf named Briffa Lummox.

In the late-game of Caverns Mode, once one’s clan has accrued 30 dwarves and there are other large clans around, the game’s performance begins to dip. This is a problem that has taken us many months to track down. While there are multiple factors that still affect performance, we have at last found the primary culprit.

I was in my chair, pouring through code, theorising, checking running time statistics, when I noticed something about the AI performance readings. Having stared at them for many minutes looking for a pattern, I noticed that one dwarf in particular kept using up more performance than any other. I assumed it was stuck behind some obstacle while trying to attack an enemy; that scenario had been a performance-hog in the past. However when I checked which dwarf it was, to my surprise, it was a miner who was doing nothing what-so-ever!

A dwarf named Briffa Lummox… I stared at them for nearly a minute, bewildered. They had no jobs to do, there was nothing significant nearby, and they were just standing there like a lemon! How is it possible that this miner could be thinking so much harder than anyone else in the game?! Believe me, these dwarves are no brain-stormers, when they have nothing to do, there’s not a lot going on in their noodles!

I started delving into the ‘idle’ behaviour to be sure, but as I suspected, there was nothing out of the ordinary. Another good half an hour of analysing the performance of individual sections of code still left me with no leads! But I knew there was something unusual going on here. What was so special about this one particular dwarf? It was only when I studied their attributes, that I noticed they had “tunnel-vision”. Tunnel-vision is a miner ability that grants a dwarf a greater sight-radius (ironically). Combine this with another trait, “candle-bright”, and you’ve got a dwarf that can see as far as an elf! Well, maybe… The point is, the greater the radius you can see, the exponentially more ’tiles’ fall within your vision. Could this be why they were thinking so hard?

So I asked myself, what AI relates to vision? There’s checking for enemies, but I’ve optimised that code beyond anything the game could possibly throw at it. What about memory? You may not realise, but units in this game keep a record of everything they know and see! This time it didn’t take me long before I spotted the problem. The code that re-evaluates their memory was executing on every frame! Not only that, but it was executing for every tile in sight, even if nothing had moved there! So a dwarf that could see more would spend a lot more time thinking, simply to keep their memories up to date! Ahah!

So I got to work on the code. Units now only update memories where and when things move. Easier said than done, since it involved revisiting some of the dusty old chambers of the engine, but in the end it was more straight-forward than I anticipated (thanks past me!). Once my basic sight & memory tests had passed, I revisited the miner. Yep, still standing there like a lemon, but this time lo and behold, they were now thinking less than any other! Now that is exactly what I’d expect from a lazy bugger named Briffa Lummox.

That’s all for now, until the next blog! (when it eventually gets here)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.