void () DeathBubblesSpawn;
void(float damage) spawn_touchblood;
void(float num, vector org) flamebubbles;
void ()W_FireGrenade;
void ()W_FireRocket;
void ()GrenadeExplode;
void () SuperLaserTouch;

void()  f_explode1      =       [0,             f_explode2] {if(pointcontents(self.origin) == CONTENT_WATER) remove(self);};
void()  f_explode2      =       [1,             f_explode3] {if(pointcontents(self.origin) == CONTENT_WATER) remove(self);};
void()  f_explode3      =       [2,             f_explode4] {if(pointcontents(self.origin) == CONTENT_WATER) remove(self);};
void()  f_explode4      =       [3,             f_explode5] {if(pointcontents(self.origin) == CONTENT_WATER) remove(self);};
void()  f_explode5      =       [4,             f_explode6] {if(pointcontents(self.origin) == CONTENT_WATER) remove(self);};
void()  f_explode6      =       [5,             SUB_Remove] {};

void () DarkPrecache = {

//   precache_model("progs/vortex.mdl");
  // precache_model("progs/vortex2.mdl");
   precache_model("progs/pds.mdl");
   precache_sound("cata_exp.wav");
   
};

void(vector org) SpawnFlame =
{
        local   entity  newfire;

        newfire = spawn();
        newfire.movetype = MOVETYPE_FLYMISSILE;
        newfire.solid = SOLID_NOT;
        newfire.owner = self.owner;

        newfire.velocity = '0 0 40';
        newfire.velocity_x = newfire.velocity_x + 40 - (random() * 80);
        newfire.velocity_y = newfire.velocity_y + 40 - (random() * 80);
        newfire.velocity_z = newfire.velocity_z + random() * 20;

        newfire.classname = "BurnFlame";
        newfire.think = f_explode1;
        newfire.nextthink = time + 0.1;
        setsize (newfire, '0 0 0', '0 0 0');
        setorigin (newfire, org);
        setmodel (newfire, "progs/s_explod.spr");
};

void() flamerstick =
{
        self.wait = self.wait - 1;
        if ((self.wait <= 0) || (pointcontents(self.origin) == CONTENT_WATER))
        {
                if (pointcontents(self.origin) == CONTENT_WATER)
                        flamebubbles(1, self.origin);

                if(self.enemy.effects & EF_DIMLIGHT)
                        self.enemy.effects = self.enemy.effects - self.enemy.effects & EF_DIMLIGHT;

                remove(self);
                return;
	}

        T_Damage (self.enemy, self, self.owner, 2+random()*1.5);
        spawn_touchblood (10);

        self.velocity = '0 0 0';
        self.avelocity = '0 0 0';

        self.origin_x = self.enemy.origin_x + 10 - (random() * 20);
        self.origin_y = self.enemy.origin_y + 10 - (random() * 20);
        self.origin_z = self.enemy.origin_z + 10 - (random() * 20);

        //if(self.enemy.health <= 0)
        //        if (random() <= 0.3)
        //                SpawnFlame (self.origin);
        //else
                if (random() <= 0.4)
                        SpawnFlame (self.origin);

        if((self.wait > 6) && (self.enemy.health <= 0))
                self.wait = 6;

        self.think = flamerstick;
        self.nextthink = time + 0.25;
};

void() flamer_touch =
{
	if (other == self.owner)
		return;

        if (other.classname == "ShotFlame")
		return;

        if (other.classname == "napalm")
		return;

	if (other.solid == SOLID_TRIGGER)
		return;	// trigger field, do nothing

	if (pointcontents(self.origin) == CONTENT_SKY)
	{
		remove(self);
		return;
	}

        if(other.takedamage)
	{
                T_Damage (other, self, self.owner, 5);
                spawn_touchblood (10);
                // 75% chance of flamer sticking to victim:
                if(random() <= 0.75)
                {
                        self.classname = "timer";
                        self.wait = 40 - (random() * 25);
                        self.enemy = other;
                        self.think = flamerstick;
                        self.touch = SUB_Null;
                        setmodel (self, "");
                        self.nextthink = time + 0.1;
                        other.effects = other.effects | EF_DIMLIGHT;
                }
        }
        else if (other.classname == "worldspawn")
        {
                // Stick to walls, etc!
                self.velocity = '0 0 0';
        }
        else
	{               
                remove(self);
	}

};


void(float num, vector org) flamebubbles =
{
        local   entity  newbubble;
	
        newbubble = spawn();
        setorigin (newbubble, org);
        newbubble.movetype = MOVETYPE_NONE;
        newbubble.solid = SOLID_NOT;
        newbubble.owner = self;
        newbubble.bubble_count = num;
        newbubble.think = DeathBubblesSpawn;
        newbubble.nextthink = time + 0.05;
};

// Firewall
/*
void()  fw_explode1      =       [0,             fw_explode2]
{
        if((pointcontents(self.origin) == CONTENT_WATER) || (pointcontents(self.origin) == CONTENT_SLIME))
        {
                flamebubbles(1, self.origin);
                remove(self);
        }
};
void()  fw_explode2      =       [1,             fw_explode3] {};
void()  fw_explode3      =       [2,             fw_explode4] {};
void()  fw_explode4      =       [3,             fw_explode5] {};
void()  fw_explode5      =       [4,             fw_explode6] {};
void()  fw_explode6      =       [5,             SUB_Remove] {};

void() FWExploThink =
{
        self.think = SUB_Remove;
        self.nextthink = time + 3;
        fw_explode1 ();
};

void() FWExploTouch =
{
        remove(self);
};

void() FWDeployDn =
{
        local entity missile;

        missile = spawn ();
        missile.owner = self.owner;
        missile.movetype = MOVETYPE_FLYMISSILE;
        missile.solid = SOLID_BBOX;
        missile.classname = "";
        missile.think = FWExploThink;
        missile.nextthink = time + 0.1;
        missile.velocity = '0 0 125';
        missile.angles = '0 0 0';
        missile.touch = FWExploTouch;
        setmodel (missile, "progs/s_explod.spr");
        setsize (missile, '-8 -8 -8', '8 8 8');
        setorigin (missile, self.origin + '0 0 8');

        T_RadiusDamage (self, self.owner, 60, world);
        if(random() < 0.3)
        {
                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);
        }
        //
        setmodel(self, "");
        self.think = FWDeployDn;
        self.nextthink = time + 0.25;
        self.velocity = '0 0 0';

        self.wait = self.wait - 0.25;
        if(self.wait <= 0)
                remove(self);
};
       
void() FWMainThink =
{
        local entity missile;

        self.think = FWMainThink;
        self.nextthink = time + 0.15;
        //
        missile = spawn ();
        missile.owner = self.owner;
        missile.movetype = MOVETYPE_FLYMISSILE;
        missile.solid = SOLID_BBOX;
        missile.classname = "";
        missile.think = SUB_Remove;
        missile.nextthink = time + 6;
        missile.velocity_z = -400;
        missile.velocity_x = 0;
        missile.velocity_y = 0;
        missile.angles = vectoangles(missile.velocity);
        missile.touch = FWDeployDn;
        // Seconds to live:
        missile.wait = 5;
        setmodel (missile, "progs/laser.mdl");
        setsize (missile, '0 0 0', '0 0 0');
        setorigin (missile, self.origin);
        //
        self.wait = self.wait - 1;
        if(self.wait <= 0)
                remove(self);
};

void ()FWTouch;

void() napalm_think;
void() NapalmTouch;

void() FWTouch =
{
        local   entity  missile;

        makevectors(self.angles);
        setorigin(self, self.origin - v_forward*4);
        self.wait = self.wait * 0.5;

        while(self.wait >= 0)
        {
                missile = spawn();
                missile.owner = self.owner;
                missile.solid = SOLID_TRIGGER;
                missile.movetype = MOVETYPE_BOUNCE;
                setmodel (missile, "progs/s_explod.spr");
                setorigin (missile, self.origin);
                missile.velocity = v_up*(200*random()+200) + v_right*(600*crandom()) - v_forward*(300*random()+50);
                missile.think = napalm_think;
                missile.nextthink = time + 0.2;
                missile.touch = NapalmTouch;
                missile.wait = 2.5;
                missile.classname = "napalm";
                missile.effects = EF_DIMLIGHT;
                if(pointcontents(missile.origin) == CONTENT_WATER)
                {
                        if (random() <= 0.5)
                                sound(self, CHAN_WEAPON, "misc/water1.wav", 1, ATTN_NORM);
                        else
                                sound(self, CHAN_WEAPON, "misc/water2.wav", 1, ATTN_NORM);
                        remove(missile);
                }
                
                self.wait = self.wait - 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);

        BecomeExplosion();
};

void() W_FireFirewall =
{
	local	entity missile;

   if ((self.ammo_rockets < 5))
   {
      W_FireRocket();
      return;
   }

        self.punchangle_x = -9;

	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_FLYMISSILE;
	missile.solid = SOLID_BBOX;
		
// set missile speed	

	makevectors (self.v_angle);
        missile.velocity = aim(self, 2000);
	missile.velocity = missile.velocity * 1000;
	missile.angles = vectoangles(missile.velocity);
                self.currentammo = self.ammo_rockets = self.ammo_rockets - 5;
                missile.touch = FWTouch;
                missile.think = FWMainThink;
                missile.nextthink = time + 0.3;
                setmodel (missile, "progs/missile.mdl");
                missile.classname = "";
                missile.avelocity = '0 0 0';
                missile.velocity = missile.velocity * 0.35;
                setsize (missile, VEC_ORIGIN, VEC_ORIGIN);
                // # of flames:
                missile.wait = 12;
                sound (self, CHAN_WEAPON, "weapons/sgun1.wav", 1, ATTN_NORM);
       setorigin (missile, self.origin + v_forward*8 + '0 0 16');
       setsize (missile, '0 0 0', '0 0 0');
};


// VORTEX //

void() VtPtThink =
{
        particle(self.origin, '0 0 0', 39, 4);
        self.think = VtPtThink;
        self.nextthink = time + 0.05;

        // Cool lightning:
        if((random() < 0.1) && (random() < 0.1))
        {
                sound(self, CHAN_WEAPON, "weapons/lstart.wav", 1, ATTN_NORM);
                WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
                WriteByte (MSG_BROADCAST, TE_LIGHTNING2);
                WriteEntity (MSG_BROADCAST, self);
                WriteCoord (MSG_BROADCAST, self.origin_x);
                WriteCoord (MSG_BROADCAST, self.origin_y);
                WriteCoord (MSG_BROADCAST, self.origin_z);
                WriteCoord (MSG_BROADCAST, self.owner.origin_x);
                WriteCoord (MSG_BROADCAST, self.owner.origin_y);
                WriteCoord (MSG_BROADCAST, self.owner.origin_z);
        }

        self.wait = self.wait - 1;
        if(self.wait <= 0)
                remove(self);
};

void() VtDetailThink =
{
        local   entity  spark;

        spark = spawn();
        // spark.movetype = MOVETYPE_FLY;
        spark.movetype = MOVETYPE_NOCLIP;
        spark.solid = SOLID_TRIGGER;
        spark.touch = SUB_Remove;
        spark.owner = self;
        setsize(spark, '0 0 0', '0 0 0');
        makevectors(self.angles);
        setorigin(spark, self.origin - v_forward * 4);
        setmodel(spark, "progs/vortex.mdl");
        spark.classname = "vtspark";
        spark.avelocity = '1000 1000 1000';
        spark.owner = self.owner;
        spark.think = VtPtThink;
        spark.nextthink = time + 0.1;
        if(random() < 0.3)
                spark.effects = EF_DIMLIGHT;
        spark.wait = 5;

        // spark.velocity = v_forward * -350 + v_right * 600;
        spark.velocity = v_forward * -50 + v_right * 600;

        self.think = VtDetailThink;
        self.nextthink = time + 0.2;

        self.wait = self.wait - 0.2;
        if(self.wait <= 0)
                remove(self);
};

void() VortexTouch =
{
        if(other.takedamage)
                T_Damage(other, self, self.owner, 200);
};

void() VortexThink =
{
        local   entity  head;
        local   vector  vel,    to_player;
        local   float   v,      found,  delta;
        local   string  d_fx;

        // Autonomous function! whee!
        if(self.wait == 6.5)
        {
                self.touch = VortexTouch;
                sound (self, CHAN_AUTO, "weapons/lstart.wav", 1, ATTN_NORM);
        }

        if(self.t_width < time)
        {
                sound (self, CHAN_WEAPON, "misc/power.wav", 1, ATTN_NORM);
                self.t_width = time + 0.7;
        }

        head = findradius(self.origin, 550);
        while(head)
        {
                if(((head.takedamage) && (visible(head))) || ((head.classname == "vtspark") && (head.owner == self)))
                {
                        vel = normalize(self.origin - head.origin);
                        v = vlen(self.origin - head.origin);
                        vel = vel * (550 - v);

                        if(head.classname == "vtspark")
                                head.velocity = head.velocity + vel*2;
                        else
                                head.velocity = head.velocity + vel*0.7;
                        if((head.flags & FL_MONSTER) || (head.classname == "gib"))
                                head.flags = head.flags - head.flags & FL_ONGROUND;
                }
                head = head.chain;
        }

        self.think = VortexThink;
        self.nextthink = time + 0.1;
        self.wait = self.wait - 0.1;

        if(self.wait <= 0)
                remove(self);
};

void() VortexStop =
{
        local   float   vtp;
        local   entity  ent;
        local   vector  ang;

        self.velocity = '0 0 0';
        self.avelocity = '600 600 600';
        self.angles = '0 0 0';
        self.solid = SOLID_TRIGGER;
        setmodel(self, "progs/vortex2.mdl");
        self.wait = 6.5;

        self.think = VortexThink;
        self.nextthink = time + 0.3;
        // self.effects = self.effects | EF_DIMLIGHT;

        vtp = 5;
        while(vtp>=0)
        {
                ent = spawn();
                ent.movetype = MOVETYPE_NONE;
                ent.solid = SOLID_NOT;
                ent.owner = self;

                self.angles = '0 0 0';
                self.angles_y = vtp * 60;
                makevectors(self.angles);
                traceline(self.origin, self.origin + v_forward * 128, FALSE, self);
                setorigin(ent, trace_endpos);
                setmodel(ent, "");

                ent.angles = vectoangles(ent.origin - self.origin);
                ent.think = VtDetailThink;
                ent.nextthink = time + 0.1;
                ent.wait = 6.5;

                vtp = vtp - 1;
        }
};

void() VortexDeploy =
{
        self.velocity = '0 0 150';
        self.movetype = MOVETYPE_NOCLIP;
        self.classname = "vortex";

        self.think = VortexStop;
        self.nextthink = time + 0.35;
};
*/

void () NullGrenadeTouch = {

   if ( (other == self.owner) ) {

      return ;

   }
   sound (self,CHAN_WEAPON,"weapons/bounce.wav",TRUE,ATTN_NORM);
   if ( (self.velocity == VEC_ORIGIN) ) {

      self.avelocity = VEC_ORIGIN;

   }

};

/*
void () W_FireVortex = {

   local entity missile;
   local entity mpuff;

   if ((self.ammo_rockets < 7))
   {
      W_FireGrenade();
      return;
   }
   
   self.ammo_rockets = (self.ammo_rockets - 7);
   self.currentammo = (self.ammo_rockets - 7);
      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 = "grenade";
   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 = VortexDeploy;
   setmodel (missile,"progs/grenade.mdl");
   setsize (missile,VEC_ORIGIN,VEC_ORIGIN);
   setorigin (missile,self.origin);
};



// Blender Spikes

void() BlenderThink =
{
        if(random() <= 0.5)
                sound (self, CHAN_WEAPON, "player/tornoff2.wav", 1, ATTN_NORM);

        self.wait = self.wait - 0.1;
        if ((self.wait < 0))
	{
                // Gib them!!
                if (self.enemy.health <= 5)
                        T_Damage (self.enemy, self, self.owner, 500);

                remove(self);
                return;
	}

	
        T_Damage (self.enemy, self, self.owner, 1);
        spawn_touchblood(6);

        self.origin = self.enemy.origin + '0 0 8';
        self.origin_z = self.origin_z + (random() * 5);
        self.nextthink = time + 0.2;
};




void() blender_touch =
{

	if (other == self.owner)
		return;

	if (other.solid == SOLID_TRIGGER)
		return;	// trigger field, do nothing

	if (pointcontents(self.origin) == CONTENT_SKY)
	{
		remove(self);
		return;
	}
	
        // Hit something that bleeds...
        // Activate The Blender:
	if (other.takedamage)
	{
                spawn_touchblood (20);
                T_Damage (other, self, self.owner, 1);
		sound (self, CHAN_WEAPON, "player/tornoff2.wav", 1, ATTN_NORM);

                self.enemy = other;
		self.origin = other.origin + '0 0 12';
                // Time until The Blender stops killing its victim:
                self.wait = 1;

                self.nextthink = time + 0.1;
                self.think = BlenderThink;
		self.movetype = MOVETYPE_NOCLIP;
		self.velocity = '0 0 0' ;
                self.avelocity = '0 1000 1000';
		return;
	}
	else
	{
		WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
                WriteByte (MSG_BROADCAST, TE_SPIKE);
		WriteCoord (MSG_BROADCAST, self.origin_x);
		WriteCoord (MSG_BROADCAST, self.origin_y);
		WriteCoord (MSG_BROADCAST, self.origin_z);
                remove(self);
	}     

};

// Napalm

void() SpawnNapalmFlame =
{
        local   entity  freak;

        freak = spawn();
        freak.movetype = MOVETYPE_FLYMISSILE;
        freak.solid = SOLID_TRIGGER;
        freak.owner = self.owner;

        freak.velocity_x = 50 - (random() * 100);
        freak.velocity_y = 50 - (random() * 100);
        freak.velocity_z = 25 + (random() * 75);

        freak.classname = "ShotFlame";
        freak.think = f_explode1;
        freak.nextthink = time + 0.1;
        freak.touch = flamer_touch;
        setsize (freak, '-1 -1 -1', '1 1 1');
        setorigin (freak, self.origin);
        setmodel (freak, "progs/s_explod.spr");
};

void() napalm_think =
{
        SpawnNapalmFlame();

        self.think = napalm_think;
        self.nextthink = time + 0.15;
        self.wait = self.wait - 0.15;

        self.frame = self.frame + 1;
        if(self.frame > 5)
                self.frame = 0;

        if(self.wait <= 0)
                remove(self);

        if((pointcontents(self.origin) == CONTENT_WATER) || (pointcontents(self.origin) == CONTENT_SLIME))
        {
                flamebubbles(1, self.origin);
                remove(self);
        }
};

void() napalm_stick =
{
        SpawnNapalmFlame();

        setorigin(self, self.enemy.origin);

        T_Damage (self.enemy, self, self.owner, 2 + random()*3);
       
        self.think = napalm_stick;
        self.nextthink = time + 0.25;
        self.wait = self.wait - 0.25;

        self.frame = self.frame + 1;
        if(self.frame > 5)
                self.frame = 0;

        if((self.wait <= 0) || (pointcontents(self.origin) == CONTENT_WATER) || (self.enemy.health <= 0))
                remove(self);
};

void() NapalmTouch =
{
        if (other == self.owner)
                return;         // don't explode on owner

        if(other.takedamage)
        {
                self.wait = 3;
                self.enemy = other;
                self.touch = SUB_Null;
                self.think = napalm_stick;
                self.velocity = '0 0 0';
                self.nextthink = time + 0.1;
        }
};
*/
//this function copied from rockbomb.qc

void (vector org, vector vel) LaunchRocket = {

   local entity missile;

      //sound (self,CHAN_WEAPON,"weapons/sgun1.wav",TRUE,ATTN_NORM);
	sound (self,CHAN_WEAPON,"enforcer/enfire.wav",TRUE,ATTN_NORM);
   self.punchangle_x = CONTENT_SOLID;
   missile = spawn ();
   missile.owner = self;
   missile.movetype = MOVETYPE_FLYMISSILE;
   missile.solid = SOLID_BBOX;
   missile.classname = "missile";
   makevectors (self.v_angle);
   missile.velocity = vel;
   missile.velocity = (missile.velocity * 1000.000);
   missile.avelocity = '0 0 0';
   missile.angles = vectoangles (missile.velocity);
	//missile.angles_x = missile.angles_x + 90;

  	missile.touch = SuperLaserTouch;

   missile.nextthink = (time + MOVETYPE_FLY);
   missile.think = SUB_Remove;
   setmodel (missile,"progs/laser.mdl");
	//missile.frame = 1; //added by bondo
   setsize (missile,VEC_ORIGIN,VEC_ORIGIN);
   //setorigin (missile,((self.origin + (v_forward * FL_CLIENT)) + '0.000 0.000 16.000'));
	setorigin (missile,(self.origin + (v_forward * FL_CLIENT)));
};

void(vector org, vector dir) launch_spike;
void() PointDefReset;

void() PointDefThink =
{
        local   entity  head;
        local   vector  dir;
        local   float   fired;

        // Look for bad people...
        head = findradius(self.origin, 1500);

        while(head)
        {
                if (!fired)
                {
                        // Can I kill it? More importantly, is it not me?
                        if ((head.takedamage) && (head != self))
                        {
                                // Yes! No use fragging zombies, and shooting my owner is bad... Also don't kill dead bodies:
                                if ((head != self.owner) && (head.classname != "monster_zombie") && (head.classname != "bodyque") && (head.owner != self.owner) && (head.classname != "ProxMine") && (head.classname != "lasmine"))
                                {
                                        // ...but the potential victim isn't any of them.
                                        // Better not kill a player in coop, either.
                                        if (!(coop && (head.classname == "player")))
                                        {
                                                // Dont kill friendly stuff in teamplay:
                                                if((!teamplay) || ((teamplay == 1) && (head.team != self.owner.team)))
                                                {
                                                        // Most important - can I see it?
                                                        if ((visible(head)) && (!fired))
                                                        {
                
                                                                // Time to KILL!!!
                                                                dir = normalize(head.origin - self.origin);
                                                                self.angles = vectoangles(dir);
                                                                self.effects = self.effects | EF_MUZZLEFLASH;
                                                                self.frame = 1;
	                                                             sound (self, CHAN_WEAPON, "catfire.wav", 1, ATTN_NORM);
                                                                //launch_spike (self.origin, dir);
                                                                LaunchRocket(self.origin,dir);
                                                                // Set flag to stop PDS firing at more than one target:
                                                                fired = 1;
                                                        }
                                                }
                                        }
                                }
                        }
                }
                // Look at next target:
                head = head.chain;
        }

        self.wait = self.wait - 0.1;

        if (self.wait < 0)
        {
                // Time to explode!
                self.movetype = MOVETYPE_BOUNCE;
                self.velocity_x = 20 - random() * 40;
                self.velocity_y = 20 - random() * 40;
                if(self.flags & FL_ONGROUND)
                        self.flags = self.flags - self.flags & FL_ONGROUND;
                self.think = SUB_Remove;
                self.nextthink = time + 5;
                self.touch = BecomeExplosion;
        }
        else
        {
                self.nextthink = time + 0.1;
                // 0.25 sec reload time:
                if(fired)
                        self.think = PointDefReset;
                else
                {
                        self.think = PointDefThink;
                        self.angles_y = anglemod(self.angles_y - 5);
                }

        }
};

void() PointDefReset =
{
        self.frame = 0;
        self.think = PointDefThink;
        self.nextthink = time + 0.2;
};



void() PointDefStop =
{
        current_yaw = current_yaw * 0;
        self.velocity = '0 0 0';
        self.avelocity = '0 0 0';
        self.angles_x = 0;
        self.angles_z = 0;

        self.wait = 30;
        self.think = PointDefThink;
        self.nextthink = time + 0.1;
        self.solid = SOLID_BBOX;
        self.takedamage = DAMAGE_AIM;
};


void() PointDefDeploy =
{
        setorigin(self, self.origin + '0 0 2');
        self.velocity = '0 0 150';
        self.movetype = MOVETYPE_FLY;
        if(self.flags & FL_ONGROUND)
                self.flags = self.flags - self.flags & FL_ONGROUND;
        self.classname = "PDS";

        self.think = PointDefStop;
        self.nextthink = time + 0.4;
};

void () W_FirePDS = {

   local entity missile;
   local entity mpuff;

   if (self.ammo_rockets < 10)
   {
      W_FireGrenade();
      return;
   }
   
   self.ammo_rockets = (self.ammo_rockets - 10);
   self.currentammo = (self.ammo_rockets - 10);
      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 = "pds";
   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.health = 50;
   missile.th_die = GrenadeExplode;
   missile.nextthink = (time + 1.000);
   missile.think = PointDefDeploy;
   setmodel (missile,"progs/pds.mdl");
   setsize (missile,VEC_ORIGIN,VEC_ORIGIN);
   setorigin (missile,self.origin);

};

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

        sound(self, CHAN_WEAPON, "cata_exp.wav", 1, ATTN_NORM);

	if (pointcontents(self.origin) == CONTENT_SKY)
        {
		remove(self);
		return;
	}

        damg = 500; 
	if (other.health)
                T_Damage (other, self, self.owner, damg);

        T_RadiusDamage (self, self.owner, 250, other);

        // Shake:
        head = find(world, classname, "player");
        while(head != world)
        {
                if(CanDamage(head, self))
                {
                        head.punchangle_x = 4*crandom();
                        head.punchangle_y = 4*crandom();
                        head.punchangle_z = 4*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);
};

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;
   setmodel (cloud, "progs/nuke.mdl");
   setsize (cloud, '0 0 0', '0 0 0');     
   setorigin (cloud, self.origin + '0 0 10');
   cloud.classname = "nukecloud";
	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:
			/*
        head = find(world, classname, "player");
        while(head != world)
        {
                if(CanDamage(head, self))
                {
                        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");
                        head.punchangle_x = 8*random();
                        head.punchangle_y = 16*crandom();
                        head.punchangle_z = 16*crandom();
                }
                head = find(head, classname, "player");
        }
        //
			*/ //removed for screenshot purposes

				num1 = 40; 							//here you get num1 fireballs
				num2 = 9;

        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 = "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);
};

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

        if ((other.classname == "godfire") || (other == self.owner))
                return;

        sound(self, CHAN_WEAPON, "cata_exp.wav", 1, ATTN_NORM);

	if (pointcontents(self.origin) == CONTENT_SKY)
        {
		remove(self);
		return;
	}

        damg = 100 + random()*25; 
	if (other.health)
                T_Damage (other, self, self.owner, damg);

        T_RadiusDamage (self, self.owner, 100, self.owner);

        // Shake:
        head = find(world, classname, "player");
        while(head != world)
        {
                if(CanDamage(head, self))
                {
                        head.punchangle_x = 4*crandom();
                        head.punchangle_y = 4*crandom();
                        head.punchangle_z = 4*crandom();
                }
                head = find(head, classname, "player");
        }
        //

        remove(self);
};

void () W_FireNuke1 = {

   local entity missile;
   local entity mpuff;

	   if ((self.ammo_rockets < 20))
   	{
      	W_FireRocket();
	      return;
   	}
	   self.ammo_rockets = (self.ammo_rockets - 20);
   	self.currentammo = (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 = "nuke";
   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);
	
};


void () W_FireNuke2 = {

   local entity missile;
   local entity mpuff;

	   if ((self.ammo_rockets < 20))
   	{
      	W_FireRocket();
	      return;
   	}
	   self.ammo_rockets = (self.ammo_rockets - 20);
   	self.currentammo = (self.ammo_rockets - 20);


      sound (self,CHAN_WEAPON,"weapons/sgun1.wav",TRUE,ATTN_NORM);
   self.punchangle_x = CONTENT_SOLID;
   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 = (missile.velocity * 1000.000);
   missile.angles = vectoangles (missile.velocity);
   missile.touch = NukeDeploy;
   missile.nextthink = (time + MOVETYPE_FLY);
   missile.think = SUB_Remove;
   setmodel (missile,"progs/missile.mdl");
   setsize (missile,VEC_ORIGIN,VEC_ORIGIN);
   setorigin (missile,((self.origin + (v_forward * FL_CLIENT)) + '0.000 0.000 16.000'));
};


