So when I was panicking at not having a patch/feature release on Saturday, I was treated to a funny bug. I call it, “Don’t hit yourself, stop hitting yourself!” bug.
It all started when I was optimizing the battle code. I was using something like this
Life = range_clamp( (Life-DamageDone),0,MaxLife);
When you do that, on the character object. It makes them, hit them self.
Here is what it should have looked like.
Target->Life = range_clamp( (Target->Life-DamageDone),0,Target->MaxLife);
What made the bug evident was when I tested that new iron sword item. The player life was reducing very fast. I thought some invisible mob was pounding on him as it was not registering in the battle log. Hits were only set up to register when you hit something else.
The funny thing is, all the creatures were being affected by this bug too; it became a battle of attrition. Whoever had the most life and hit for the least won the battle.
Now I have a TakeDamage function on all characters. So when an attack takes place and damage is done it plays out like this.
Target->TakeDamage(DamageAmount,DamageType);
The mob is hitting itself with damage now. hehe. I took a bug and made it into a feature and can tease the mob when I hit it… “Stop hitting yourself! Stop hitting yourself. HAHA”.