/*

 ProxGrenade 1.0 Copyright 1996 Niklas Angare (NiAn on undernet) angare@kuai.se.

 This code is released for all to learn from, but please do not abuse my
 goodwill by copying large chunks of it. If you want to distribute my code as a
 part of a package, ask me first!

 This is a part of PLUSGrenade.

*/

//=============================================================================

void() BecomeExplosion;
void() W_FireGrenade;

void() ProxGrenadeExplode =
{
	self.owner = self.proxowner;
	T_RadiusDamage (self, self.owner, 120, world);

	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
	WriteByte (MSG_BROADCAST, TE_EXPLOSION);
	WriteCoord (MSG_BROADCAST, self.origin_x);
	WriteCoord (MSG_BROADCAST, self.origin_y);
	WriteCoord (MSG_BROADCAST, self.origin_z);

	BecomeExplosion ();
};

void() ProxGrenadePickup =
{
	other.ammo_rockets = other.ammo_rockets + self.ammo_rockets;
	sprint(other, "You got ");
	local string tempstring;
	tempstring = ftos(self.ammo_rockets);
	sprint(other, tempstring);
//	sprint(other, ftos(self.ammo_rockets));
	sprint(other, " rockets\n");
	sound (other, CHAN_ITEM, "weapons/lock4.wav", 1, ATTN_NORM);
	stuffcmd (other, "bf\n");
	remove(self);
	self = other;
	W_SetCurrentAmmo ();
};

void() ProxGrenadeTouch =
{
	if (other.takedamage == DAMAGE_AIM)
		if(self.hasbounced) {
			ProxGrenadePickup();
			return;
		}
	if (other == self.proxowner)
		return;		// don't explode on owner
	if (other.takedamage == DAMAGE_AIM)
	{
		ProxGrenadeExplode();
		return;
	}
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);	// bounce sound
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
	self.hasbounced = TRUE;
};

void() GrenadeProximity =
{
	local	entity	head;
	local	float	proxrange;

	if (self.ttl < time) {
		ProxGrenadeExplode();
		return;
	}

	self.nextthink = time + 0.1;

	proxrange = 150;

	head = findradius(self.origin, proxrange); // Find everything within range
	
	// Search the list for stuff that takes damage
	while (head)
	{
		if (head.takedamage == DAMAGE_AIM)
			if(CanDamage (head, self)) {
				ProxGrenadeExplode();
				return;
			}
		head = head.chain;
	}
};

void() ProxGrenadeGoLive =
{
	sound (self, CHAN_WEAPON, "weapons/tink1.wav", 1, ATTN_NORM); // it's live!
	self.think = GrenadeProximity;
	GrenadeProximity();
};

/*
================
W_FireProxGrenade
================
*/
void() W_FireProxGrenade =
{
	local	entity missile, mpuff;
	
	if(self.ammo_rockets < 5) {
		sprint(self, "ammo low\n");
		W_FireGrenade();
		return;
	}
	self.currentammo = self.ammo_rockets = self.ammo_rockets - 5;
	
	sound (self, CHAN_WEAPON, "weapons/grenade.wav", 1, ATTN_NORM);

	self.punchangle_x = -2;

	missile = spawn ();
	missile.proxowner = self;		// This is instead of .owner, so that
	missile.movetype = MOVETYPE_BOUNCE;	//  the grenade can touch its 'owner'
	missile.solid = SOLID_BBOX;		//  If .owner was set, it wouldn't touch
	missile.classname = "grenade";
		
// set missile speed	

	makevectors (self.v_angle);

	if (self.v_angle_x)
		missile.velocity = v_forward*600 + v_up * 200 + crandom()*v_right*10 + crandom()*v_up*10;
	else
	{
		missile.velocity = aim(self, 10000);
		missile.velocity = missile.velocity * 600;
		missile.velocity_z = 200;
	}

	missile.avelocity = '300 300 300';

	missile.angles = vectoangles(missile.velocity);
	
	missile.touch = ProxGrenadeTouch;
	
// set missile duration
	missile.nextthink = time + 4;
/*NiAn*/missile.ttl = time + 30;		// explode anyway after 30 secs
/*NiAn*/missile.think = ProxGrenadeGoLive;
/*NiAn*/missile.ammo_rockets = 5;

	setmodel (missile, "progs/grenade.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin);
};


//=============================================================================
