/*
Version 1.2

PLASMA.QC contains main functions for my plasma gun. 
It needed some modifications in following files:
 
(progs.src of course)
WEAPONS.QC   : weapon selections, firing, precache
DEFS.QC      : variables
ITEMS.QC     : raised max cells to 200, changed weapon_touch and backpacks.
CLIENT.QC    : parm10 contains bitflag for plasma gun possession

Don't forget these if you combine this stuff with other patches.

I have made all the new models from scratch. I think the plasma gun
goes pretty well with the style of other Quake guns... Though it has
more different colors than a typical Quake weapon.

Asking what editors I used? Hehe, how the ms-dos EDIT sounds to you?-)
Ok, I did use meddle 1.7 to convert my handwritten .asc files to mdl...
Lotsa thanks to Brian.

In this version I have added a new sprite for the explosion, and a cool
new trigger... Run the example map and just wait. :)  (Don't kill anybody
at first)
*/

float () crandom;

void() PlasmaPrecache =
{
	precache_sound2 ("enforcer/enfstop.wav");   // electric zap (for water hit)
	precache_sound ("dsfirxpl.wav");   // for plasma hit
	precache_sound ("dsplasma.wav");    // plasma firing
  precache_model ("progs/pbullet.mdl");      // Plasma bullet model
  precache_model ("progs/v_plasma.mdl");     // Plasma being used
  precache_model ("progs/s_plasma.spr");     // Plasma explosion sprite. 
};

void()  s_pexplode1  = [1,   s_pexplode2] {};
void()  s_pexplode2  = [2,   s_pexplode3] {};
void()  s_pexplode3  = [3,   s_pexplode4] {};
void()  s_pexplode4  = [4,   s_pexplode5] {};
void()  s_pexplode5  = [5,   SUB_Remove] {};

void() PlasmaExplosion =
{
	self.movetype = MOVETYPE_NONE;
	self.velocity = '0 0 0';
	self.touch = SUB_Null;
    setmodel (self, "progs/s_plasma.spr");
	self.solid = SOLID_NOT;
  s_pexplode1 ();
};

void() Plasma_Watercheck =
{
  // The routine modified from BLAZE.QC included in the killer87 package.
  // Basically, plasma bullets explode when hitting liquids.
  // In this explosion, the electic charge of the plasma bullet is spread
  // into the said liquid...
  
  local float  pc;

  pc = pointcontents (self.origin);
  if ((pc == CONTENT_WATER) || (pc == CONTENT_SLIME) || (pc == CONTENT_LAVA))
  {
    sound (self, CHAN_WEAPON, "enforcer/enfstop.wav", 1, ATTN_NORM);

    T_RadiusDamage (self, self.owner, 60, world);   // Damage
    PlasmaExplosion();                         // Explosion sprite
   self.think = SUB_Remove;
   self.nextthink = time;
    return;
  }

/*
   if ( (time > self.attack_finished) ) {

      PlasmaExplosion ();
      return ;

   }
*/

  self.think = Plasma_Watercheck;
  self.nextthink = time;
};

void() T_PlasmaTouch =
{
  local float damg;

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

  if (pointcontents(self.origin) == CONTENT_SKY)
  {
    remove(self);
    return;
  }
  damg = 20  + random()*10;   // Possibility of gibbing small enemies :)

  if (other.classname == "monster_zombie")
    if (random() < 0.7)        // 50% change of gibbing a zombie...
      damg = 1000;             // Otherwise you could never kill them.
  
	if (other.classname == "big_enforcer") //enforcers somewhat immune to plasma
		damg = damg * 0.35;

  if (other.health)
  {
    T_Damage (other, self, self.owner, damg );
  }

  // Moves the plasma bullet backwards after hitting.. Otherwise the 
  // explosion sprite would often be inside a wall or something. 
  self.origin = self.origin - 8*normalize(self.velocity);

  // Sound from Doom...
 	  sound (self, CHAN_WEAPON, "dsfirxpl.wav", 1, ATTN_NORM);
  PlasmaExplosion ();
//  self.think = SUB_Remove;
//  self.nextthink = time;

};

/*
================
W_FirePlasma
================
*/

void() W_FirePlasma =
{
  local entity missile;
  
  // You can shoot THREE times with one AMMO_CELL.
  // This was needed because the ammo_cells can only contain
  // numbers between 0...255 (!)
    self.currentammo = self.ammo_cells = self.ammo_cells - 0.5;

  // Taken from Doom...
	   sound (self, CHAN_WEAPON, "dsplasma.wav", 1, ATTN_NORM);

  self.punchangle_x = -2;

  missile = spawn ();
  missile.owner = self;
  missile.movetype = MOVETYPE_FLYMISSILE;    // Like a rocket
  missile.solid = SOLID_BBOX;
  missile.classname = "plasma";
    
  // set plasma bullet speed. (Basically AIM with some random)  

  makevectors (self.v_angle);
	if (self.classname == "player")
 		missile.velocity = aim(self, 1000);
 	else
 		missile.velocity = aim(self.owner, 1000);
  missile.velocity = missile.velocity * 1000;
  missile.velocity = missile.velocity + ( crandom() * 50 * v_right );
  missile.velocity = missile.velocity + ( crandom() * 50 * v_up );
  missile.angles = vectoangles(missile.velocity);
  missile.avelocity = '300 300 300';  

  missile.nextthink = time;
  missile.think = Plasma_Watercheck;        // If shot underwater -> razzp!
  missile.touch = T_PlasmaTouch;
  
  missile.effects = 8;           // Plasma bullets emit light...

  setmodel (missile, "progs/pbullet.mdl");   // Plasma bullet model
  setsize (missile, '0 0 0', '0 0 0');
  setorigin (missile, self.origin + v_forward*8 + '0 0 10' + v_right*10);
};

// Added the plasma gun... Drop it in like any other guns.

void() weapon_touch;
void() StartItem;

/* void() weapon_plasmagun =
{
	precache_model ("progs/g_plasma.mdl");
	setmodel (self, "progs/g_plasma.mdl");
	self.weapon = 3;
	self.netname = "Plasma Gun";
	self.touch = weapon_touch;
	setsize (self, '-16 -16 0', '16 16 56');
	StartItem ();
}; */

void() EnforcerFirePlasma =
{
  local entity missile,en;
	local vector dir;  

	// Taken from Doom...
	sound (self, CHAN_WEAPON, "dsplasma.wav", 1, ATTN_NORM);

  self.punchangle_x = -2;

  missile = spawn ();
  missile.owner = self;
  missile.movetype = MOVETYPE_FLYMISSILE;    // Like a rocket
  missile.solid = SOLID_BBOX;
  missile.classname = "plasma";
    
  // set plasma bullet speed. (Basically AIM with some random)  

   makevectors (self.v_angle);
   ai_face ();
   //missile.velocity = aim (self,1000.000);
   en = self.enemy;
   dir = (en.origin - (en.velocity * 0.200));
   dir = normalize ((dir - self.origin));
   missile.velocity = dir * 1000;
   missile.velocity = missile.velocity + ( crandom() * 50 * v_right );
   missile.velocity = missile.velocity + ( crandom() * 50 * v_up );
   missile.angles = vectoangles(missile.velocity);
   missile.avelocity = '300 300 300';  

  missile.nextthink = time;
  missile.think = Plasma_Watercheck;        // If shot underwater -> razzp!
  missile.touch = T_PlasmaTouch;
  
  missile.effects = 8;           // Plasma bullets emit light...

  setmodel (missile, "progs/pbullet.mdl");   // Plasma bullet model
  setsize (missile, '0 0 0', '0 0 0');
  setorigin (missile, self.origin + v_forward*8 + '0 0 10' + v_right*10);
};

void () W_EnforcerFirePlasmaShotgun = {

   local float lasercount;
	local entity laser;
   
	
	   sound (self, CHAN_AUTO, "dsplasma.wav", 1, ATTN_NORM);
	   sound (self, CHAN_AUTO, "dsplasma.wav", 1, ATTN_NORM);
	   sound (self, CHAN_AUTO, "dsplasma.wav", 1, ATTN_NORM);

	lasercount = 0;
	while (lasercount < 6) {
	
   self.ammo_shells = (self.ammo_shells - 3);
   self.currentammo = (self.ammo_shells - 3);
   self.punchangle_x = CONTENT_SOLID;
   laser = spawn ();
   laser.owner = self;
   laser.movetype = MOVETYPE_FLYMISSILE;
   laser.solid = SOLID_BBOX;
   laser.classname = "plasma";
   makevectors (self.v_angle);
   laser.velocity = aim (self,1000.000);
   laser.velocity = (laser.velocity * 1000.000);
   laser.velocity = ((laser.velocity + ((v_up * crandom ()) * 100.000)) + ((v_right * crandom ()) * 100.000));
   laser.angles = vectoangles (laser.velocity);
	laser.avelocity = '300 300 300';  
	laser.effects = 8;           // Plasma bullets emit light...

  	laser.touch = T_PlasmaTouch;

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


};

