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.
// ------------------------------------------------ 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.
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?