AI Tutorial
Co-op Support

 

     My pal and supporter ArwingX recently told me that he finally convinced the author of the GuardBot to release his source code. In my selfish and fame-hungry way, I said, "Traitor! Heretic! Turncoat! Why aren't you using my Tutor Bot!" Well, I didn't actually say that.
     You see, ArwingX needed a bot with co-op support. So, in my usual selfless, helpful way, I asked "If I write a tutorial on co-op support, will you use my source code?" And he said yes.
     (I should also note that my boy Ze0 from the Epidemic asked for co-op support as well. I must be political now.)      So here we are. With this lesson your bot will learn how to look for an attack monsters. Also, you will be able to teleport your bot back to you.
     First step. We will write a simple routine which the bot can use to search for those nasty critters. Scroll down to bot_look_for_players() or thereabouts, and shove the following before it:


// ------------------------------------------------
void() bot_search_for_monsters =
// ------------------------------------------------
{
local entity mon;

	if (self.enemy)
		return;

// checks a radius around him for creatures
	mon = findradius(self.origin, 1500);

	while(mon)
		{
		if ( (mon.flags & FL_MONSTER) && visible(mon) && mon.health > 0)
			{
			self.search_time = time + 30;
			self.goalentity = mon;
			self.enemy = mon;
			self.th_run();
			return;
			}
		mon = mon.chain;
		}

};

     Simple, eh? Basically I copied bot_search_for_items() and changed a couple things. The bot will check all the entities around him within a 1500 unit radius. We don't need to check their classnames, because all creatures are flagged, or marked, with the label FL_MONSTER.
     If he sees one, he will make it his enemy, enter his fighting state, and leave the current routine. It's bad form to exit the routine from within a while() loop, but we're not in a beauty pageant here.
     Second step. Scroll down to bot_walk(). You will see there that the bot makes a call to bot_look_for_players(). Just add a call to bot_search_for_monsters(). Go down to bot_stand() and do the same. Your bot will now look for and hopefully kill monsters.
     Third step. For full and tasty co-op goodness, we want to add a routine with which you can teleport your helper bot back to you. Go into WEAPONS.QC and move down to ImpulseCommands(). Before that routine, paste this one:

// ------------------------------------------------
void() teleport_bot =
// ------------------------------------------------
{
local entity bot;

	bot = find(world, classname, "bot");
	if (!bot)
		{
		bprint("no bot to teleport\n");
		return;
		}

	makevectors(self.v_angle);
	if (POINT_CONTENTS(self.origin + v_forward*100) != CONTENT_EMPTY)
		{
		bprint("step away from the wall\n")
		return;
		}

	if (vlen(self.origin - bot.origin) > 50)
		{
		spawn_tfog(self.origin + v_forward*100);
		spawn_tdeath(self.origin + v_forward*100, self);
		setorigin(bot, self.origin + v_forward*100);
		}
};

     Here we look for an entity named "bot". If we don't find one, we just leave. We call makevectors() to find the value v_forward and check for a wall in front of the player. If the bot is not close to the player, we teleport the bot 100 units in front of him. If the bot is near him, we obviously don't need to bring him over.
     Fourth step. Go into ImpulseCommands() and underneath your create_bot() lines, add this:

	if (self.impulse == 102)
		teleport_bot();

     If you can't tell me what this does, I'm gonna hit you. Anyway, those are the fundamentals of a co-op bot. Any questions?

 


What We Learned

1. All monsters are marked with FL_MONSTER
2. If there is a shortcut, take it
3. A bot can check around him for any type of entity
4. Coffee is a sucker for a good tutorial

 


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