void() GrenadeExplode;
void() GrenadeTouch;
void() BecomeExplosion;
float() crandom;

float WP_CLUSTER = 1;
float IM_CLUSTER = 70;

void() BabyExplode =
{
	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);
	
	T_NewRadiusDamage(self,self.owner,50,world,160);
	BecomeExplosion();
};

void() BabyTouch =
{
	if (other==self.owner)
		return;
	if (other.takedamage == DAMAGE_AIM)
	{
		BabyExplode();
		return;
	}
	sound (self,CHAN_WEAPON,"weapons/bounce.wav",1,ATTN_NORM);
	if (self.velocity=='0 0 0')
		self.avelocity='0 0 0';
};

void() ClusterExplode = 
{
	local entity missile;
	local float tmp;
	
	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);
	
	tmp =0;
	while (tmp <5)
	{
		missile=spawn();
		missile.owner = self.owner;
		missile.movetype = MOVETYPE_BOUNCE;
		missile.solid = SOLID_BBOX;
		missile.classname = "grenade";
		missile.think = BabyExplode;
		missile.nextthink = time+1.5;
		missile.velocity_z=500;
		missile.velocity_x=400*random() - 200;
		missile.velocity_y=400*random() - 200;
		missile.avelocity= '300 300 300';
		missile.angles=vectoangles(missile.velocity);
		missile.touch=BabyTouch;
		setmodel(missile,"progs/grenade.mdl");
		setsize(missile,'0 0 0','0 0 0');
		setorigin(missile,self.origin);
		tmp=tmp+1;
	}
	BecomeExplosion();
};
	
void() ClusterTouch =
{
	if (other == self.owner)
		return;
	if (other.takedamage == DAMAGE_AIM)
	{
		ClusterExplode();
		return;
	}
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
};

void() W_FireCluster =
{
	local entity missile;
	if (self.currentammo<1)
	{
		self.weapon=W_BestWeapon();
		W_SetCurrentAmmo();
		self.ef=0;
		return;
	}
	self.ammo_rockets = self.ammo_rockets - 10;
	self.currentammo=floor(self.ammo_rockets/10);
	sound (self,CHAN_WEAPON,"weapons/grenade.wav", 1, ATTN_NORM);
	self.punchangle_x= -2;
	missile=spawn();
	missile.owner=self;
	missile.movetype=MOVETYPE_BOUNCE;
	missile.solid=SOLID_BBOX;
	missile.classname="clusterbomb";
	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=ClusterTouch;
	missile.nextthink=time+1.5;
	missile.think=ClusterExplode;
	setmodel(missile,"progs/grenade.mdl");
	setsize(missile,'0 0 0','0 0 0');
	setorigin(missile,self.origin);
};
