Suicide Bug (or Never Use Magic Numbers)

If you are a developer you should be familiar with the concept of magic numbers, or magic constants. In short these are values written “in clear” inside your source code. Also you should know that you never, EVER, use them, otherwise you may end up with your boss monsters going suicidal!

This is a little bug that I immediately corrected while implementing the Summon spell for Alpha 0.2 (that you can download here). This was the code that I was using up until then:

override public void OnRound () {
var range = this.Parent.CurrentActiveRange;
var skill = this.Parent.GetPrioritySkillInRange (range);
var enemiesInRange = Simulator.Instance.GetEnemiesAt ("PLAYER", this.Parent.X, this.Parent.Y, range);
[...]
}

As you may notice there is a “PLAYER” written in there. Well that is the faction of the enemy, and that is why our summoned Efreet decided to self-terminate: since its own faction was “PLAYER” (because he was summoned by the player) he considered himself an enemy!

Now the function is correct:

override public void OnRound () {
var range = this.Parent.CurrentActiveRange;
var skill = this.Parent.GetPrioritySkillInRange (range);
var enemyFaction = Simulator.Instance.GetEnemyFaction(this.Parent.Faction);
var enemiesInRange = Simulator.Instance.GetEnemiesAt (enemyFaction, this.Parent.X, this.Parent.Y, range);
[...]
}

There is a lesson to be learned, again, in there: laziness is not the way to go, most of the time (you may also be able to spot a singleton, most say that this is also a mistake, maybe I will write a bit about this topic another time).

If you want to take a look at the source code of Wizard of Unica you can find it hosted on GitHub.

Thanks for reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Google+ photo

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

Connecting to %s