Holding fire

Sometimes your monsters won’t let up. You’re writing a custom CheckAttack function for a creature which has a decent missile attack, but a better melee attack. By the time you get to handling mid-range combat, you’d like her to choose between attacking right now with the missile or running towards the player with melee in mind. You’d probably start with something like:

if(random() > 0.4)
   return FALSE;
//else
self.th_missile();
return TRUE;

With this code, you find your monster barely spends any time running before firing another shot. The problem is that your CheckAttack function runs ten times a second, so your monster is like a young child continually¬†asking “are we nearly there?” The chance of 10 random number all being above 0.4 is tiny, meaning we will rarely run for a full second.

One way to combat this might be to crank down the probability of a missile attack to 0.1 or 0.05, but there’s actually an alternative way: use attack_finished. No doubt near the top of your CheckAttack function you return without attacking if self.attack_finished is in the future. We can set attack_finished in the run-at-the-player branch:

if(random() > 0.4)
{
   self.attack_finished = time + 1;
   return FALSE;
}
//else
self.th_missile();
return TRUE;

Now 60% of the time we run at the player for a whole second, and 40% of the time we start a missile attack. It makes the decision not to attack a positive change in the state of the monster (for the next second), rather than just deferring the decision another frame until we give up and shoot.

If you do this, be careful of where your attack_finished test is in your function. The shambler will melee even if his attack_finished is in the future. If you don’t do the same, your monster might keep running at the player without attacking even when he’s close enough to strike.

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