void()  n_explode1      =       [0,             n_explode2] {};
void()  n_explode2      =       [1,             n_explode3] {};
void()  n_explode3      =       [2,             n_explode4] {};
void()  n_explode4      =       [3,             n_explode5] {};
void()  n_explode5      =       [4,             n_explode6] {};
void()  n_explode6      =       [5,             n_explode1]
{
        self.wait = self.wait - 1;
        if(self.wait <= 0)
                remove(self);
};

void() NukeExploTouch =
{
        local   float   damg;
        local   entity  head;

	if ((other.classname == "explo") || (other.classname == "dhead"))
   	return;
	sound(self, CHAN_WEAPON, "cata_exp.wav", 1, ATTN_NORM);
	if (pointcontents(self.origin) == CONTENT_SKY)
   {
		remove(self);
		return;
	}
	damg = P6DAMAGE; //monsters to M3
//	if (other.classname == "monster_super_shambler") 
//		damg = damg * 0.5;
	if (other.health) 
		T_Damage (other, self, self.owner, damg);
	T_RadiusDamage (self, self.owner, P5DAMAGE, other);
	// Shake:
   head = find(world, classname, "player");
   while(head != world)
   {
		if(CanDamage(head, self))
      {
      	head.punchangle_x = 8*crandom();
         head.punchangle_y = 8*crandom();
         head.punchangle_z = 8*crandom();
		}
		head = find(head, classname, "player");
	}
	remove(self);
};

/*
void() GammaThink =
{
        local   string  intens;

        // Arg! Trying to stuffcmd a ftos() result crashed Quake, so do
        // it like this:
        intens = ftos(self.wait);
        stuffcmd(self.enemy, "v_cshift 255 255 255 ");
        stuffcmd(self.enemy, intens);
        stuffcmd(self.enemy, "\n");

        self.think = GammaThink;
        self.nextthink = time + 0.1;

        self.wait = self.wait * 0.8;
        if(self.wait <= 1)
                remove(self);
};
*/

//touch function for SuperNuke
void() NukeDeploy =
{
	local float expl,r,num1,num2;
   local entity missile, head, glowworm,cloud;

	//added by bondo
   cloud = spawn ();
   cloud.owner = self.owner;
   cloud.movetype = MOVETYPE_NONE;
   cloud.solid = SOLID_TRIGGER;
   setsize (cloud, '0 0 0', '0 0 0');
   setorigin (cloud, self.origin + '0 0 10');
   cloud.classname = "explo";
   setmodel (cloud, "progs/nuke.mdl");
	cloud.think = nu_explode1;
   cloud.nextthink = time + 0.1;
   cloud.angles = '0 0 0';
   cloud.angles_y = 0;
   cloud.angles_x = 0;
   cloud.angles_z = 0;
   makevectors(cloud.angles);
   cloud.wait = 5;
   cloud.touch = NukeExploTouch;
	nu_explode1 ();	
	//end added by bondo
	
	self.effects = EF_BRIGHTLIGHT;

   // Gamma and head shake
	head = find(world, classname, "player");
   while(head != world)
   {
		if(CanDamage(head, self))
      {
      	/*
         //the nuke is so bright... blah blah blah
         glowworm = spawn();
			glowworm.classname = "timer";
			glowworm.enemy = head;
			glowworm.wait = 255;
			glowworm.think = GammaThink;
			glowworm.nextthink = time;
			stuffcmd(head, "v_cshift 255 255 255 255\n");
			*/
			//shake the head
			head.punchangle_x = 8*random();
			head.punchangle_y = 16*crandom();
			head.punchangle_z = 16*crandom();
      }
		head = find(head, classname, "player");
   }
	
	num1 = 40; 							//here you get num1 fireballs
	num2 = 9;
	
	expl = 0;
	while(expl < num1) //num1 was 60
   {
   	missile = spawn ();
		missile.owner = self.owner;
		missile.movetype = MOVETYPE_FLYMISSILE;
		missile.solid = SOLID_TRIGGER;
		setmodel (missile, "progs/s_explod.spr");
		setsize (missile, '0 0 0', '0 0 0');
		setorigin (missile, self.origin + '0 0 10');
		missile.classname = "explo";
		r = random();
		if(r<0.333)
			missile.think = n_explode1;
		else if(r<0.666)
		   missile.think = n_explode2;
		else
		   missile.think = n_explode3;
		missile.nextthink = time + 0.1;
		missile.angles = '0 0 0';
		missile.angles_y = expl * num2;	//num2 was 6
		if(random() < 0.7)
			missile.effects = EF_DIMLIGHT;
		//missile.angles_z = 6 - random() * 12;
		makevectors(missile.angles);
		missile.velocity = v_forward * 500;
		missile.wait = 5;
		missile.avelocity = '0 0 0';
		missile.touch = NukeExploTouch;
		expl = expl + 1;
   }
	self.think = SUB_Remove;
   self.nextthink = time + 0.1;
	
	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);
	
	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
   WriteByte (MSG_BROADCAST, TE_LAVASPLASH);
   WriteCoord (MSG_BROADCAST, self.origin_x);
   WriteCoord (MSG_BROADCAST, self.origin_y);
   WriteCoord (MSG_BROADCAST, self.origin_z);

};

//grenade super nuke
/*
void () W_FireNuke1 = {

   local entity missile;
   local entity mpuff;

	   if ((self.ammo_rockets < 20))
   	{
      	W_FireRocket();
	      return;
   	}
	self.currentammo = self.ammo_rockets = (self.ammo_rockets - 20);
	sound (self,CHAN_WEAPON,"weapons/grenade.wav",TRUE,ATTN_NORM);
   self.punchangle_x = CONTENT_SOLID;
   missile = spawn ();
   missile.owner = self;
   missile.movetype = MOVETYPE_BOUNCE;
   missile.solid = SOLID_BBOX;
   missile.classname = "explo";
   makevectors (self.v_angle);
   if ( self.v_angle_x ) {

      missile.velocity = ((((v_forward * 600.000) + (v_up * 200.000)) + ((crandom () * v_right) * MOVETYPE_BOUNCE)) + ((crandom () * v_up) * MOVETYPE_BOUNCE));

   } else {

      missile.velocity = aim (self,10000.000);
      missile.velocity = (missile.velocity * 600.000);
      missile.velocity_z = 200.000;

   }
   missile.avelocity = '300.000 300.000 300.000';
   missile.angles = vectoangles (missile.velocity);
   missile.touch = NullGrenadeTouch;
   missile.nextthink = (time + 2.500);
   missile.think = NukeDeploy;
   setmodel (missile,"progs/pds.mdl");
   setsize (missile,VEC_ORIGIN,VEC_ORIGIN);
   setorigin (missile,self.origin);

};
*/

//missile super nuke
void () W_FireNuke2 = {

   local entity missile;
   local entity mpuff;

	   if (self.ammo_rockets < 25)
   	{
      	centerprint(self,"not enough rockets\n");
      	self.weapon = W_BestWeapon();
      	W_SetCurrentAmmo();
	      return;
   	}
	   if (!(self.ringofpower_time)) self.ammo_rockets = self.currentammo = self.ammo_rockets - 25;


      sound (self,CHAN_WEAPON,"weapons/sgun1.wav",TRUE,ATTN_NORM);
   self.punchangle_x = CONTENT_SOLID;
   
   local float n;
   n = 3;
   
   do {
   missile = spawn ();
   missile.owner = self;
   missile.movetype = MOVETYPE_FLYMISSILE;
   missile.solid = SOLID_BBOX;
   missile.classname = "nuke";
   makevectors (self.v_angle);
   //missile.velocity = aim (self,1000.000);
   missile.velocity = b_aim(self.origin);
   missile.velocity = (missile.velocity * 1000.000);
   if (self.multiprojectile_time) missile.velocity = missile.velocity + v_right*crandom()*100;
   missile.angles = vectoangles (missile.velocity);
   missile.touch = NukeDeploy;
   missile.nextthink = (time + MOVETYPE_FLY);
   missile.think = SUB_Remove;
   setmodel (missile,"progs/missile3.mdl");
   setsize (missile,VEC_ORIGIN,VEC_ORIGIN);
   setorigin (missile,((self.origin + (v_forward * FL_CLIENT)) + '0.000 0.000 16.000' + v_right*8));
   n = n - 1;
   
   } while (self.multiprojectile_time && n > 0);
};

//called from player_selfdestruct
/*
void() NukePlayer =
{
        local   float   expl,   r, num1,num2;
        local   entity  missile,        head,   glowworm,cloud;
	//self is the temp entity created in player_selfdestruct

	//added by bondo
   cloud = spawn ();
   cloud.owner = self.owner;
   cloud.movetype = MOVETYPE_NONE;
   cloud.solid = SOLID_TRIGGER;
   setmodel (cloud, "progs/nuke.mdl");
   setsize (cloud, '0 0 0', '0 0 0');
   setorigin (cloud, self.origin + '0 0 10');
   cloud.classname = "nuke";
	cloud.think = nu_explode1;
   cloud.nextthink = time + 0.1;
   cloud.angles = '0 0 0';
   cloud.angles_y = 0;
   cloud.angles_x = 0;
   cloud.angles_z = 0;
   makevectors(cloud.angles);
   cloud.wait = 5;
   cloud.touch = NukeExploTouch;
	nu_explode1 ();	
	//end added by bondo

        self.effects = EF_BRIGHTLIGHT;


        // Gamma and head shake
        head = find(world, classname, "player");
        while(head != world)
        {
		if(CanDamage(head, self))
		{
		        //shake the head
		        head.punchangle_x = 8*random();
		        head.punchangle_y = 16*crandom();
		        head.punchangle_z = 16*crandom();
		}
		head = find(head, classname, "player");
        }
        //

				num1 = 20; 							//here you get num1 fireballs
				num2 = 18;

        expl = 0;
        // Explos:
        while(expl < num1) //num1 was 60
        {
		missile = spawn ();

		missile.owner = self.owner;
		missile.movetype = MOVETYPE_FLYMISSILE;
		missile.solid = SOLID_TRIGGER;
		setmodel (missile, "progs/s_explod.spr");
		setsize (missile, '0 0 0', '0 0 0');
		setorigin (missile, self.origin + '0 0 10');

		missile.classname = "nuke";
		r = random();
		if(r<0.333)
		        missile.think = n_explode1;
		else if(r<0.666)
		        missile.think = n_explode2;
		else
		        missile.think = n_explode3;

		missile.nextthink = time + 0.1;
		missile.angles = '0 0 0';
		missile.angles_y = expl * num2;	//num2 was 6
		if(random() < 0.7)
		        missile.effects = EF_DIMLIGHT;
		//missile.angles_z = 6 - random() * 12;
		makevectors(missile.angles);
		missile.velocity = v_forward * 500;
		missile.wait = 5;
		missile.avelocity = '0 0 0';
		missile.touch = NukeExploTouch;
		expl = expl + 1;
        }

	sound (self,CHAN_VOICE,"cata_exp.wav",TRUE,ATTN_NONE);
	//self.think = SUB_Remove;
	//self.nextthink = time + 0.2;

        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);

};

*/
