//New turret bullet firing code.  Replaces fire_bullet and fire_rail
//Fires at ent->enemy
//direction = 1 (left), 2 (center), 3 (right)
void turret_fire_bullet (edict_t *ent, int direction)
{

	vec3_t		forward, right, start, target, dir;
	vec3_t		from;
	vec3_t		end;
	trace_t		tr;
	edict_t		*ignore;
	int			mask;
	qboolean	water;
	int			kick = 0;
	int			mod = MOD_SENTRY;
	int			damage;
	float		chance_of_hit;
	float		maxvelocity;
	float		r;

if (wfdebug) gi.dprintf("turret_fire_bullet\n");

	if(ent->light_level<1)  //do we have ammo?
		return;

	if (!ent->enemy)
		return;

	UpdateSentryAmmo(ent, -1);

	//Calculate starting position
	AngleVectors (ent->s.angles, forward, right, NULL);

	if (direction == DIRECTION_LEFT)
	{
		start[0] = ent->s.origin[0] + forward[0] * 3 + right[0] * -0.5;
		start[1] = ent->s.origin[1] + forward[1] * 3 + right[1] * -0.5;
		start[2] = ent->s.origin[2] + forward[2] * 3 + right[2] * -0.5 +4;
	}
	else if (direction == DIRECTION_CENTER)
	{
		start[0] = ent->s.origin[0] + forward[0] * 3 ;
		start[1] = ent->s.origin[1] + forward[1] * 3 ;
		start[2] = ent->s.origin[2] + forward[2] * 3+4;
	}
	else		// Right
	{
		start[0] = ent->s.origin[0] + forward[0] * 3 + right[0] * 0.5;
		start[1] = ent->s.origin[1] + forward[1] * 3 + right[1] * 0.5;
		start[2] = ent->s.origin[2] + forward[2] * 3 + right[2] * 0.5+4;
	}

	// calc direction to where we targetd
	VectorMA (ent->enemy->s.origin, -0.05, ent->enemy->velocity, target);

	//Adjust for height
//	target[2] += ent->enemy->viewheight/1.5;

	VectorSubtract (target, start, dir);
	VectorNormalize (dir);

	VectorMA (start, 8192, dir, end);
	VectorCopy (start, from);
	ignore = ent;
	water = false;
	mask = MASK_SHOT|CONTENTS_SLIME|CONTENTS_LAVA;

	tr = gi.trace (from, NULL, NULL, end, ignore, mask);

	if (tr.contents & (CONTENTS_SLIME|CONTENTS_LAVA))
	{
		mask &= ~(CONTENTS_SLIME|CONTENTS_LAVA);
		water = true;
	}

	//Do damage

	//Damage based on sentry level
	chance_of_hit = 1;
	if (ent->count == 1)
	{
		damage = LEVEL1_DAMAGE;
		chance_of_hit = .85;
	}
	else if (ent->count == 2)
	{
		damage = LEVEL2_DAMAGE;
		chance_of_hit = .90;
	}
	else if (ent->count == 3)
	{
		damage = LEVEL3_DAMAGE;
		chance_of_hit = .95;
	}
	else
	{
		damage = 1;		//don't know what level it is
		chance_of_hit = 1.0;		//always hit
	}

	//Moving reduces chance of being hit
	maxvelocity = max( abs (ent->enemy->velocity[0]), abs (ent->enemy->velocity[1]) );

	if (maxvelocity >= 320)		//wierd setting for grappling
	{
		chance_of_hit = chance_of_hit  * .65;
	}
	else if (maxvelocity >= 250)		//running
	{
		chance_of_hit = chance_of_hit  * .74;
	}
	else if (maxvelocity >= 150) //walking
	{
		chance_of_hit = chance_of_hit  * .90;
	}
	r = random();

if (wfdebug) gi.dprintf("chance =%f, rnd = %f, takedamage? %d\n",	chance_of_hit, r, ent->enemy->takedamage);

	if ((ent->enemy->takedamage) && (r <= chance_of_hit))
//		T_Damage (ent->enemy, ent, ent->creator, dir, tr.endpos, tr.plane.normal, damage, 0, 0, MOD_SENTRY);
        T_Damage (ent->enemy, ent, ent, dir, tr.endpos, tr.plane.normal, damage, 0, 0, MOD_SENTRY);

	//Do gunshot effect
	gi.WriteByte (svc_temp_entity);
	gi.WriteByte (TE_GUNSHOT);
	gi.WritePosition (tr.endpos);
	gi.WriteDir (tr.plane.normal);
	gi.multicast (tr.endpos, MULTICAST_PVS);

	if (ent->client)
		PlayerNoise(ent, tr.endpos, PNOISE_IMPACT);

	// send muzzle flash
	gi.WriteByte (svc_muzzleflash);
	gi.WriteShort (ent - g_edicts);
	gi.WriteByte (MZ_SHOTGUN );
	gi.multicast (start, MULTICAST_PVS);
//	gi.sound(ent, CHAN_VOICE, gi.soundindex("boss3/xfire.wav"), 1, ATTN_NORM, 0);
}