Turrets:Part 2


This could be considered a fairly large change. It doesn't effect the actual operation of the turret, just the way it comes into being.

I've tried to get it to be tossed, but I havn't managed, but what it does is acceptable. It drops the base at your current location, and then creates the gun for it about 5 seconds later. The gun then goes active and the base solidifies (although its small enough to be walked over) The model isn't particulary attractive, but its acceptable to my mind (damn it Jim, I'm a coder, not a modeller!).

Just follow the instructions and download the model pack. (the models come with a path, just unzip them to your mod directory)


First off, we no longer need the seturretlocation command, so delete the code below

void Cmd_SetTurLoc_f( gentity_t *ent ){
/*
This just sets the variable holding the turrets location and prints it out on screen (for debugging. you can drop the trap servercommand line if you want)
*/

VectorCopy(ent->r.currentOrigin,ent->turloc);
trap_SendServerCommand( ent-g_entities, va("print \"Location Set. %f %f %f \n\"",ent->r.currentOrigin[0],ent->r.currentOrigin[1],ent->r.currentOrigin[2]));

}

and

	else if (Q_stricmp (cmd, "setturretlocation") == 0)
		Cmd_SetTurLoc_f( ent );

You may as well just get rid of Cmd_SpawnTurret_f as this tutorial replaces it.

Replace it with

void Cmd_SpawnTurret_f( gentity_t *ent ){


	gentity_t	*base;


	base=G_Spawn();
	base->parent=ent;
	base->s.modelindex = G_ModelIndex("models/objects/turret/base.md3");
	base->model = "models/objects/turret/base.md3";
	base->s.modelindex2 = G_ModelIndex("models/objects/turret/base.md3");
	G_SetOrigin( base, ent->r.currentOrigin ); // sets where the turret is
	base->think=createturretgun;
	base->nextthink=level.time+5000;
	VectorSet( base->r.mins, -15, -15, -20 );
	VectorSet( base->r.maxs, 35, 15, -5);
	trap_LinkEntity (base);

}

The model has its top at the origin, the reason the z max is set to a negative number is because the plasma would hit it if it wasn't

Now we get a new function, one that creates the gun. paste it in above Cmd_SpawnTurret_f

void createturretgun(gentity_t *ent){
	gentity_t *turret; 	// The object to hold the turrets details.

	int			num;
	int			touch[MAX_GENTITIES];

// code to check there is noone within the base before making it solid
	vec3_t		mins, maxs;

	VectorAdd( ent->r.currentOrigin, ent->r.mins, mins );
	VectorAdd( ent->r.currentOrigin, ent->r.maxs, maxs );
	num = trap_EntitiesInBox( mins, maxs, touch, MAX_GENTITIES );
	if (num>1)  //something other than the turret exists here
	{
	ent->nextthink=level.time+1000;
	return;
	}
// end of checking code.

	ent->clipmask = CONTENTS_SOLID | CONTENTS_PLAYERCLIP;
	ent->r.contents = CONTENTS_SOLID;
	ent->s.pos.trType = TR_STATIONARY;
	turret=G_Spawn();
	turret->parent=ent->parent;
	turret->eventTime=200;
	turret->s.weapon=WP_PLASMAGUN;
	turret->classname="turret";	
	turret->s.modelindex = G_ModelIndex("models/objects/turret/gun1.md3");
	turret->model = "models/objects/turret/gun1.md3";
	turret->s.modelindex2 = G_ModelIndex("models/objects/turret/gun1.md3");
	turret->think=turret_think;
	turret->nextthink=level.time+100;
	G_SetOrigin( turret, ent->r.currentOrigin );
	trap_LinkEntity (turret);
}

Fairly simple, the model for the gun has its base on the origin so it looks right. don't worry about the effects looking wrong, because they all start a short way out. The references to ent are there to solidify the base. the gun isn't solid, but it doesn't need to be. The code that checks to see if another entity exists will stop you creating a turret that coexists with an item (or a flag) I may change this later to remove the base if its been there for a long time and hasn't been able to spawn a gun

Thats it. Enjoy.



To the next tutorial in this series
Back to the tutorials
Mail me