Wednesday, December 26, 2007

A Christmas Bug Tale

So here I am on boxing day fixing bugs. It's my own fault, I can't relax knowing there is a bug in the game I haven't tracked down. Luckily it wasn't *that* evil.
The bug symptom was this: If you reduce the number of smokers to zero, you still earn the same amount from tobacco tax!

Why?

Well the way it *should* work is this -> The income from the tax is set by the player moving a slider. This gets multiplied by various values, such as a global 'wealth' modifier for he whole country (USA vs Belgium in terms of size for example). it also gets multiplied by a special 'neuron' called Tobbacotax_income. This 'neuron' (everything in the game is a neuron) had just 1 input, and that input was from another neuron that stores the size of the smokers group. The input is an equation which in this case just simply sets the multiplier to be the same as the size of the group.

Cool.

But it wasn't working. Stepping through the code, everything seemed ok. The voter group each turn worked out how big it was, and would tell its 'percentage' neuron that value. later, the finance code would ask the tax how much it earned, it would look at the slider, and ask the income neuron how much to multiply it by. And here was the weirdness. The income neuron would be set at the right value, but the 'effect' acted as its input was still containing a wrong value. In other words, all my brain cells worked, but the wires had the wrong data in them.

It turns out that in the blanket code that gets every neuron to recalculate itself, that pesky 'percentage' neuron was doing its calculations, and just setting itself to its default (it has no inputs, and gets told directly what value to be). It then told all its outputs this value and forgot about them. The voter group code then trundles along and says "forget all that crap, here is your real value mate!" Which is fine, but the wrong data is still stored in the percentage neuron's outputs. Ironically, the system automatically propagates the right value out next turn, but before that happens, the value will get reset again in the same way...

Anyway................ All I needed to do was change the code that said "ahoy there percentage neuron your value is 0.12" to say "Ahoy there, your value is 0.12, and maketh that your flipping default value as well, so there are no mistakes if you think about recalculating yourself".
And all was well...

I need to do some playtesting before releasing this, because it may have caused balancing nightmares...