float() crandom;
void() BecomeExplosion;
void(float dmg) spawn_touchblood;

float WP_BOUNCER = 2;
float IM_BOUNCER = 71;

void() PopperTouch =
{
	PopperExplode();
};

void() PopperExplode =
{
	local entity spike;
	local float tmp=0;
	while (tmp<20)
	{
		tmp=tmp+1;
		spike=spawn();
		spike.owner=self.owner;
		spike.movetype=MOVETYPE_FLYMISSILE;
		spike.solid=SOLID_BBOX;
		self.touch=SUB_Null;
		spike.classname="shrapnel";
		spike.think=SUB_Remove;
		spike.nextthink=time+6;
		spike.velocity_x=crandom()*1000;
		spike.velocity_y=crandom()*1000;
		spike.velocity_z=0;
		spike.angles=vectoangles(spike.velocity);
		spike.avelocity='300 300 300';
		spike.touch=ShrapnelTouch;
		setmodel(spike,"progs/spike.mdl");
		setsize(spike,'0 0 0','0 0 0');
		setorigin(spike,self.origin);
	}
	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);
	self.solid=SOLID_NOT;
	BecomeExplosion();
};

void() BouncerDud =
{
	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);
	self.solid=SOLID_NOT;
	BecomeExplosion();
};

void() BouncerDie =
{
	self.think=BouncerDud;
	self.nextthink=time+0.1;
};
	
void() BouncerFirstThink =
{
	self.nextthink=time+0.1;
	if (self.velocity == '0 0 0') 
	{
		self.avelocity='0 0 0';
		self.think=BouncerThink;
		self.blasttimer=time+10;
	}
};
		
void() BouncerThink =
{
	local entity head;
	local float expfl;
	head = findradius(self.origin,60);
	self.nextthink=time+0.1;
	while (head)
	{
		if ((head.takedamage)&&(head != self))
			expfl=1;
		head=head.chain;
	}
	if (expfl)
	{
		self.think=BouncerExplode;
		self.nextthink=time+0.1;
	}
	if (self.blasttimer<time)
	{
		self.think=BouncerDud;
		self.nextthink=time+0.1;
	}
};

void() BouncerExplode =
{
	local entity popper;
	popper=spawn();
	popper.owner=self.owner;
	popper.movetype=MOVETYPE_BOUNCE;
	popper.solid=SOLID_BBOX;
	popper.classname="popper";
	popper.velocity='0 0 600';
	popper.angles=vectoangles(popper.velocity);
	popper.touch=PopperTouch;
	popper.nextthink=time+0.1;
	popper.think=PopperExplode;
	setmodel(popper,"progs/grenade.mdl");
	setsize(popper,'0 0 0','0 0 0');
	setorigin(popper,self.origin);
//  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);
	self.solid=SOLID_NOT;
	BecomeExplosion();
};

void() BouncerTouch =
{
	sound(self,CHAN_WEAPON,"weapons/bounce.wav",1,ATTN_NORM);
};

void() W_FireBouncer =
{
	local entity bouncer;
	local float tmp;
	// pulls 1 rocket and 30 nails
	if (self.currentammo<1)
	{
		self.weapon=W_BestWeapon();
		self.ef=0;
		W_SetCurrentAmmo();
		return;
	}
	self.ammo_rockets = self.ammo_rockets - 1;
	self.ammo_nails = self.ammo_nails - 20;
	sound(self,CHAN_WEAPON,"weapons/grenade.wav",1,ATTN_NORM);
	tmp=floor(self.ammo_nails/20);
	self.currentammo=min(self.ammo_rockets,tmp);
	bouncer=spawn();
	bouncer.owner=self;
	bouncer.movetype=MOVETYPE_BOUNCE;
	bouncer.solid=SOLID_BBOX;
	bouncer.classname="bouncer";
	makevectors(self.v_angle);
	if (self.v_angle_x)
		bouncer.velocity=v_forward*600+v_up*200+crandom()*v_right*10+crandom()*v_up*10;
	else
	{
		bouncer.velocity=aim(self,10000);
		bouncer.velocity=bouncer.velocity*600;
		bouncer.velocity_z=200;
	}
	bouncer.avelocity='300 300 300';
	bouncer.angles=vectoangles(bouncer.velocity);
	bouncer.touch=BouncerTouch;
	bouncer.think=BouncerFirstThink;
	bouncer.nextthink=time+0.1;
	bouncer.th_die=BouncerDie;
	bouncer.takedamage=DAMAGE_YES;
	bouncer.health=1;
	bouncer.blasttimer=time+30;
	setmodel(bouncer,"progs/grenade.mdl");
	setsize(bouncer,'0 0 0','0 0 0');
	setorigin(bouncer,self.origin);
};
