Turrets:Part 4


This is more of a modification of the previous code than anything new. It is the changing of the way that the turret checks to see if something is within it's fire arc. No new variables (although you could add a few so that the fire arc becomes customizable. If you can't work out how, mail me, and I'll give you some pointers)

Currently the code allows for a variabtion of 90 degrees to either side of center, and a pitch of -45>45 degrees. This is fairly easy to change. you should see how.


Replace the checktarget function with the one below (plus the #defines above it)

#define RANGE 500
#define HARC 90  // left/right fire arc
#define DARC 45 // how far the gun will depress
#define UARC 45 // how far the gun will elevate


qboolean checktarget(gentity_t *firer,gentity_t *target){
vec3_t 		distance,forward;
trace_t         trace;
int		angle;

/*
returns qfalse if the target is not valid. returns qtrue if it is
*/

if (!target) // Do we have a target?
	return qfalse;
if (!target->inuse) // Does the target still exist?
	return qfalse;
if (target==firer) // is the target us?
	return qfalse;
if(!target->client) // is the target a bot or player?
	return qfalse;
if (target==firer->parent) // is the target the person that created the turret?
	return qfalse;
if (OnSameTeam(firer->parent, target)) // is the target one of us?
	return qfalse;

if (target->health<0) // is the target still alive?
	return qfalse;

VectorSubtract(target->r.currentOrigin,firer->r.currentOrigin,distance);
if (VectorLength(distance)>RANGE) // is the target within range?
	return qfalse;

trap_Trace (&trace, firer->s.pos.trBase, NULL, NULL, target->s.pos.trBase, firer->s.number, MASK_SHOT );
if ( trace.contents & CONTENTS_SOLID ) // can we see the target?
	return qfalse;
/*
The last two checks are done last as they require more processing power than the others.
this order is just better from a proccesing load perspective
*/

	vectoangles (distance,distance);
	VectorSubtract(firer->centerpoint,distance,distance);
	angle=abs((int)distance[1]);
	while (angle>=360)
	{
	angle-=360;
	}
	if ((angle>=HARC) && (angle<=(360-HARC))
		return qfalse;
	angle=abs((int)distance[0]);
	while (angle>=360)
	{
	angle-=360;
	}
	if ((angle>UARC) && (angle<(360-DARC))
		return qfalse;

return qtrue;
}

There you go. (I was kicking myself when I realized how easy it was)


Back to the tutorials
Mail me