Oak Tutorial

Back to Tutorials

Next Lesson

 

Oak Tutorial 3: Spawning and beyond

Lesson Requirements
A C compiler, and the Quake2 Source code

Instructions on using the LCCWin32 Compiler.

Example source code

Lesson Summary
More on spawning a bot in Quake2. This builds on th first and second tutorial.

Ok this tutorial is based on how I`ve been writing my bot, you may choose to do it differently, this is only a guideline :) Well I`ve been buried under emails from people having trouble compiling the source code from the earlier tutorials, most of the problems stem from the fact that the authors dont have a clue what C or C++ is, let alone how to program in it (Hint: read the C tutorials on this site before trying to edit any source code). Anyway to reduce the number of problems people have I`m going to post a zip of the entire example source code as a MSVC++ 5.0 project (though you might need to change the output paths), though obviously those using Borland or LCCWin32 can still compile the code just I`m not going to do the makefile for you. Please remember that this is a ongoing tutorial series, and if something is not explained or done in this one then its probably going to be in the next one.

 Lesson

Right the code I`ve provied in the zip extends the bot to being able to spawn, and then run its animation frames for standing. If you approach the front of a bot it will see you and turn to face you and try to walk towards you. When it gets close enough it`ll stop and stand, waiting for you to move again. Theres nothing particulalry clever about the code, it is just a simple working example for you to play with. These are not the think functions used by Oak II but some simple ones to demonstrate what you can do.

Ok so whats new? Well firstly you should note this line in the respawn fucntion:

self->takedamage = DAMAGE_AIM; 

 somehow I left that out of the previous tutorial, oops. The most noteowrthy addition is the function oak_stand:

void oak_stand(edict_t *self)
{
	edict_t	*target;

//	gi.bprintf(PRINT_HIGH, "Oak: in oak_stand\n");

	self->enemy = NULL;

	// look for a target
	target = findradius(NULL, self->s.origin, OAK_FIND_RANGE);
	while(target)
	{
		if (visible(self, target))
		{
			if (infront(self, target))
			{
				if (strcmp(target->classname, "player") == 0)
				{
					//gi.bprintf(PRINT_HIGH, "Oak: in oak_stand selecting you as target\n");
					self->enemy = target;
				}
			}
		}

		// next taget
		target = findradius(target, self->s.origin, OAK_FIND_RANGE);
	}
	
	if (self->enemy != NULL)
	{
		//gi.bprintf(PRINT_HIGH, "Oak: in oak_stand turning to face you\n");	
		OakAI_FaceEnemy(self);
		OakAI_RunFrames(self, FRAME_run1, FRAME_run6);
		self->think = oak_run;
	}
	else
	{
		// run the anim frames
		OakAI_RunFrames(self, FRAME_stand01, FRAME_stand40);
	}
	self->nextthink = level.time + 0.1;
}

The first part of this function (the while loop) searches for all the entities with a certain radius, and checks if they are visible and infront of the bot, next it determines if they are a player, if so it selects them as a enemy. The second half of the function checks wether a enemy has been seen and if so turns to face them and approach, otherwise it runs the standing animation frames. This function can easily be extended to look for items and/or weapons. (exercise for reader).

The next think we want to make note of is the past line if the function:

self->nextthink = level.time + 0.1;

All monsters in quake "think" every so often, its during their thinking that the AI for them is executed. Thinking is accomplished by means of think functions (oak_stand above is one such function). So it is important we set the entities think data, there are two fields, think which specifies what function the bot should execute next time it thinks, and nextthink which is the game time when the bot should next execute its think function. This is usually set to the current time + the ammount of time into the future you want it to think, i.e. in the above code it is set to next think in 0.1 seconds from now. This could quite easily be modified to simulate a bot having a set ping, though its worth noting the animations will become jerky.

That should give you something to be getting on with, I`ll further explain what some of the other code does in my next tutorial and why I`ve chosen to do things the way I have.

Ok if you compile the code you can then spawn a bot by typing cmd oak in the console. Remember you must run quake with the command line:

quake2 +set game oak +set deathmatch 1

Assuming you put the new gamex86.dll in the dir called oak.

 

Back to Tutorials

Next Lesson


Copyright © 1997, John Crickett.
Legal Information