AI Tutorial
Homework Assignment No. 1 Answer

 

      Alright, school is back in session. Time to hand in your first homework assignment. And don't you tell me the bot ate it.
      Your project was to create a sort of time-delay message of the day, with one part printing after the other was done. This can be accomplished in just one file in just one subroutine. That is client.qc and PlayerPostThink() respectively.
      At the end of that routine, you could have posted something like this:


	if (time < 3)
		centerprint(self, "Now playing the WonkaBot\n");
	if (time >= 3 && time < 6)
		centerprint(self, "Written by Mr. Wonka\n");

      That's it. Remember, PlayerPostThink() is called after every frame of animation; basically, it is called almost continuously. Remember something else: you cannot print() anything when the world first spawns. It's just a little weird thing about QuakeC.
      Did you come up with that? No? That's okay, because there is usually more than one way to skin a bot, so to speak. Just for a perverse thrill, let's look at another way of doing that.
      At the very end of PutClientInServer(), you could have added this:

	create_message_machine();

      Then, before that subroutine, you could paste this:

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void() create_message_machine =
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
local entity mach;

	if (time > 3)
		return;

	mach = spawn();
	mach.owner = self;
	mach.think = message_think;
	mach.nextthink = time + 0.1;
};

      Right. Recall that we said that we cannot print a message immediately after the world spawns. So here we create an independent "machine" to print a message in 0.1 seconds.
      Before that routine, paste this one:

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void() message_think =
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
	self.nextthink = time + 0.1;

	if (time < 3)
		centerprint(self.owner, "Now playing the WonkaBot\n");
	if (time >= 3 && time < 6)
		centerprint(self.owner, "Written by Mr. Wonka\n");
	if (time > 6)
		remove(self);

};

      The self here is the machine entity; the self.owner is the player. This routine prints the same message as before to that player's screen. It thinks every 0.1 seconds. After six seconds, it removes itself, never to be seen nor heard again.
      This seems unusual, I know. But I often use external "machine" entities like this to execute certain functions at certain times. They operate independently from any player or bot and thus can be used to regulate aspects of the game.
      That ends the first homework assignment. How did you do? I'm sure you did well. I hope you did, because the next one will be a bit more challenging.

 


What We Learned

1. Entities can be used for anything
2. QuakeC doesn't always operate as you think it should
3. Simple solutions rock
 


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