AI Tutorial
Fleeing

 

      This lesson is pretty darn straight-forward: the bot will learn how to flee when injured. The catch is that bots aren't really good at fleeing. That is, they are not good at getting from room to room easily. If you have spent any time observing bots, you know what I mean.
      Anyway, we will give it a try. This is a decent subroutine. And of course, you can spend all the time you want tweaking it. Go ahead and open up tutor.qc and scroll down to the subroutine bot_run(), which looks kind of like so:


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void() bot_run =
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{

// his fighting thoughts. after a short while, he'll give up 
// on his enemy, but if he can see him, he'll always attack

	if (!(self.flags & FL_ONGROUND))
		return;

	check_for_water();

	if (visible(self.enemy))
		self.search_time = time + 6;

	if (time > self.search_time || self.enemy.health <= 0)
		{
		self.goalentity = world;
		self.enemy = world;
		self.pausetime = time + 4;
		self.th_stand();
		return;
		}

      As you know, this is his main attack routine, his combat brain. If his health is under a certain value, we want him to turn around and get the hell outta Dodge. Very simple to do. After the previous code, add this:

	if (self.health < 25)
		{
		self.angles_y = vectoyaw(self.origin - self.enemy.origin);
		if (walkmove(self.angles_y, 20) == FALSE)
			bot_run_slide();
		return;
		}

      Ahem, that's it. I might not be know across the world as a great coder, but I'm sure one of the briefest. A fleeing routine in seven lines. Not bad.
      I guess you'll want to know how those seven lines work. If his health is under 25, he will face the exact opposite direction of his enemy (take a look at the routine bot_face(), this is just flip-flopped from that).
      Then he tries to walkmove in the new direction he's facing. If he cannot -- that is, if he ran into an obstacle -- then he will start strafing. Maybe he can then skirt the obstacle by strafing. Of course, this is a very simple navigation routine; if you want to change it, go ahead. Just insult me, why don't you. See if I care. Ingrate.
      The two important notes here are as such: make sure you use the line about self.angles_y, and make sure you do not call bot_face() before this. That would just turn him back towards his enemy. You can even change the injure value, 25, to something higher of lower.
      We are now officially done. You may accuse me of many things, but you cannot rightfully say that I waste your time.

 


What We Learned

1. Self.angles_y or self.angles_x is the direction a bot faces
2. A bot's direction is obviously very important
3. Self.angles_y and walkmove() together can be quite powerful
4. Read number three again

 


Author: Coffee
Questions: coffee@planetquake.com
AI chat on ICQ: 2716002