// LAVA Weapon Routines

/*
===============
launch_lava_spike

Used for both the player and the ogre
===============
*/
void(vector org, vector dir) launch_lava_spike =
{
	newmis = spawn ();
newmis.cansplash = TRUE;
	newmis.owner = self;
	newmis.movetype = MOVETYPE_FLYMISSILE;
	newmis.solid = SOLID_BBOX;

	newmis.angles = vectoangles(dir);
	
	newmis.touch = lavaspike_touch;
	newmis.classname = "lava_spike";
	newmis.think = SUB_Remove;
newmis.traileffectnum = particleeffectnum("lavaspike_bubble_trail");
	newmis.nextthink = time + 6;
	setmodel (newmis, "progs/lspike.mdl");	
	setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);		
	setorigin (newmis, org);

	newmis.velocity = dir * 1000;
};

void() W_FireSuperLavaSpikes =
{
	local vector	dir;
	local entity	old;
	
	sound (self, CHAN_WEAPON, "weapons/spike2.wav", 1, ATTN_NORM);

var float autocvar_muzzleflash = 1;     	// set cvar 'muzzleflash' default to 1
if (autocvar_muzzleflash == 1)		// if 'muzzleflash' doesnt exist in autoexec.cfg, or if 'muzzleflash' is set via autoexec.cfg to '1' 
	pointparticles(particleeffectnum("superlavanailgun_muzzleflash"), self.origin + self.view_ofs + v_up * -8 + v_forward*20, self.velocity*50, 1);
else
	{						// if 'muzzleflash' is set via autoexec.cfg to another value (example '0'), do nothing !
	}

	self.attack_finished = time + 0.2;
	self.currentammo = self.ammo_lava_nails = self.ammo_lava_nails - 2;
	UpdateAmmoCounts (self);
	
	dir = aim (self, 1000);
	launch_lava_spike (self.origin + self.view_ofs + v_up * -8 , dir);
	newmis.touch = superlavaspike_touch;

//	setmodel (newmis, "progs/lspike.mdl");			

	setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);		
	self.punchangle_x = -2;
			
///////// pushback LSNG
var float autocvar_pushback = 0;     	// set cvar 'pushback' default to 0
	if (autocvar_pushback != 0)		// if 'pushback' is other than '0', pushback is enabled
		{
		var float autocvar_pushbacklavanail2 = 30;    // set cvar 'pushbacklavanail2' default to 30
		local vector pushback1, direc;
		direc = aim (self, 100);
		traceline (self.origin, self.origin + direc*autocvar_pushbacklavanail2, FALSE, self);
		pushback1 = (trace_endpos - self.origin); // + (normalize(direc) * 80);
		self.velocity = self.velocity - pushback1;
		}

	
////  added GYRO below

	local entity nail;
	local	vector align, nailvec;

	makevectors(self.v_angle);
	align = dir + v_up*0.27;
	traceline (self.origin, self.origin + align*200, FALSE, self);
	nailvec = (trace_endpos - self.origin) + (normalize(dir) * 80);
	nail = Gyro_Force_Create("nailhit", self.origin);
	Gyro_Force_ApplyFalloff_ConeLinear(nail, nailvec, 16, 16);
	Gyro_Force_ApplyAffector_Directional(nail, nailvec, 400, FALSE);
	Gyro_Force_AddProperty_Instant(nail);
	Gyro_Force_Ignore(nail, self); 
};

void(float ox) W_FireLavaSpikes =
{
	local vector	dir;
	local entity	old;
	
	makevectors (self.v_angle);
	
	if (self.ammo_lava_nails >= 2 && self.weapon == IT_LAVA_SUPER_NAILGUN)
	{
		W_FireSuperLavaSpikes ();
		return;
	}

	if (self.ammo_lava_nails < 1)
	{
		sprint (self, "Out of Lava Nails\n");
		self.weapon = W_BestWeapon ();
		W_SetCurrentAmmo ();
		return;
	}

	sound (self, CHAN_WEAPON, "weapons/rocket1i.wav", 1, ATTN_NORM);
	
var float autocvar_muzzleflash = 1;     	// set cvar 'muzzleflash' default to 1
if (autocvar_muzzleflash == 1)		// if 'muzzleflash' doesnt exist in autoexec.cfg, or if 'muzzleflash' is set via autoexec.cfg to '1' 
	{
	pointparticles(particleeffectnum("lavanailgun_muzzleflash"), self.origin + self.view_ofs + v_up * -7.8 + v_right*2.7 + v_forward*20, self.velocity*50, 1);
	pointparticles(particleeffectnum("lavanailgun_muzzleflash"), self.origin + self.view_ofs + v_up * -7.8 + v_right*-2.7 + v_forward*20, self.velocity*50, 1);
	}
else
	{						// if 'muzzleflash' is set via autoexec.cfg to another value (example '0'), do nothing !
	}

	self.attack_finished = time + 0.2;
	self.currentammo = self.ammo_lava_nails = self.ammo_lava_nails - 1;
	UpdateAmmoCounts (self);
	
	dir = aim (self, 1000);
	launch_lava_spike (self.origin + self.view_ofs + v_up * -8 + v_right*ox, dir);

	self.punchangle_x = -2;
		
///////// pushback LNG
var float autocvar_pushback = 0;     	// set cvar 'pushback' default to 0
	if (autocvar_pushback != 0)		// if 'pushback' is other than '0', pushback is enabled
		{
		var float autocvar_pushbacklavanail1 = 20;    // set cvar 'pushbacklavanail1' default to 20
		local vector pushback1, direc;
		direc = aim (self, 100);
		traceline (self.origin, self.origin + direc*autocvar_pushbacklavanail1, FALSE, self);
		pushback1 = (trace_endpos - self.origin); // + (normalize(direc) * 80);
		self.velocity = self.velocity - pushback1;
		}


////  added GYRO below

	local entity nail;
	local	vector align, nailvec;

	makevectors(self.v_angle);
	align = dir + v_up*0.27;
	traceline (self.origin, self.origin + align*200, FALSE, self);
	nailvec = (trace_endpos - self.origin) + (normalize(dir) * 80);
	nail = Gyro_Force_Create("nailhit", self.origin);
	Gyro_Force_ApplyFalloff_ConeLinear(nail, nailvec, 16, 16);
	Gyro_Force_ApplyAffector_Directional(nail, nailvec, 300, FALSE);
	Gyro_Force_AddProperty_Instant(nail);
	Gyro_Force_Ignore(nail, self); 
};

// =============== Lava Spike Touch Routines

void() lavaspike_touch =
{
local float rand;
local float old_armortype;
local float old_armorvalue;
local float old_armormask;

	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
	if (other.takedamage)
	{
		var float autocvar_shootablebuttonsandsecrets = 1;     	// set cvar 'shootablebuttonsandsecrets' default to 1
		if (autocvar_shootablebuttonsandsecrets != 0)		// if 'shootablebuttonsandsecrets' is other than '0'
		{
			if (trace_ent.takedamage != DAMAGE_AIM) 	// prevents shootable buttons and secret walls/meshes to spawn blood
			{
				T_Damage (other, self, self.owner, 15);
		        	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY); 
				WriteByte (MSG_BROADCAST, TE_SUPERSPIKE);
			        WriteCoord (MSG_BROADCAST, self.origin_x); 
			        WriteCoord (MSG_BROADCAST, self.origin_y); 
		        	WriteCoord (MSG_BROADCAST, self.origin_z); 
		                remove(self);   //add this so they don't float... 
        		        return;         //add this to avoid errors 
			}
		}
	
var float autocvar_fleshsplat = 0;     	// set cvar 'fleshsplat' default to 0
if (autocvar_fleshsplat == 1)		
{
   self.angles_x = self.angles_x*-1; //account for error in makevectors with non-player entities
   makevectors(self.angles); //find direction nail is facing

	if (other.classname == "monster_wizard")
	{        
		ThrowBloodSplat(v_forward, self.origin, other, 1, 1); 
	}
	else if (other.classname == "monster_army")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
	else if (other.classname == "monster_ogre")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
	else if (other.classname == "monster_hell_knight")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1); 
		if (!(other.spawnflags & 2))
			pointparticles(particleeffectnum("sparks"), self.origin, '0 0 0', 1); 
	}
	else if (other.classname == "monster_knight")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
		if (!(other.spawnflags & 2))
			pointparticles(particleeffectnum("sparks"), self.origin, '0 0 0', 1); 
	}
	else if (other.classname == "monster_demon1")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 3, 1);
	}
	else if (other.classname == "monster_dog")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1); 
	}
	else if (other.classname == "monster_shalrath")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 4, 1); 
	}
	else if (other.classname == "monster_tarbaby")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 5, 1); 
	}
	else if (other.classname == "monster_shambler")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
	else 
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
}

		spawn_touchblood (9);
		if(other.classname == "player")
		{
			old_armortype = other.armortype;
			old_armorvalue = other.armorvalue;
			old_armormask = other.items2 & (IT2_ARMOR1|IT2_ARMOR2|IT2_ARMOR3);
			other.armortype = 0;
			other.armorvalue = 0;
			T_Damage (other, self, self.owner, 9);
			other.armortype = old_armortype;
			other.armorvalue = old_armorvalue;
			other.items2 = other.items2 | old_armormask;
		}
		else		// is a monster
		{
			if ( other.classname != "monster_lava_man")
			{
				T_Damage (other, self, self.owner, 15);
			}
		}

                remove(self);   //add this so they don't float... 
                return;         //add this to avoid errors 

	}

        else if (other.solid != SOLID_BSP)
	{
                remove(self); 
                return; 
        } 
	
	
		if (self.classname == "wizspike")
		{	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
			WriteByte (MSG_BROADCAST, TE_WIZSPIKE);
			WriteCoord (MSG_BROADCAST, self.origin_x);
			WriteCoord (MSG_BROADCAST, self.origin_y);
			WriteCoord (MSG_BROADCAST, self.origin_z);
		}
		else if (self.classname == "knightspike")
		{	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
			WriteByte (MSG_BROADCAST, TE_KNIGHTSPIKE);
			WriteCoord (MSG_BROADCAST, self.origin_x);
			WriteCoord (MSG_BROADCAST, self.origin_y);
			WriteCoord (MSG_BROADCAST, self.origin_z);
		}
		else
			pointparticles(particleeffectnum("TE_LAVASPIKE"), self.origin, '0 0 0', 1);
			// WriteByte (MSG_BROADCAST, TE_SUPERSPIKE);
			// WriteCoord (MSG_BROADCAST, self.origin_x);
			// WriteCoord (MSG_BROADCAST, self.origin_y);
			// WriteCoord (MSG_BROADCAST, self.origin_z);

        self.solid = SOLID_NOT; 
        self.movetype = MOVETYPE_NONE; 

        setorigin (self, self.origin - normalize(self.velocity) * 3); 
        self.velocity = '0 0 0';         //add this, it makes the nail stop 

      if (other.solid == SOLID_BSP && other != world) 
      {
         self.owner = other; 
         self.pos1 = self.origin; 
         self.pos2 = other.origin; 
         self.aflag = time + 10; 
         self.think = spike_stuck_think; 
         self.nextthink = time; 
	}
      else 
      {
        self.nextthink = time + 17;
         self.think = SUB_Remove;        //add this for same reason as above 
      } 
};

void() superlavaspike_touch =
{
local float rand;
local float old_armortype;
//local float old_armorvalue;

	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
	if (other.takedamage)
	{
		var float autocvar_shootablebuttonsandsecrets = 1;     	// set cvar 'shootablebuttonsandsecrets' default to 1
		if (autocvar_shootablebuttonsandsecrets != 0)		// if 'shootablebuttonsandsecrets' is other than '0'
		{
			if (trace_ent.takedamage != DAMAGE_AIM) 	// prevents shootable buttons and secret walls/meshes to spawn blood
			{
				T_Damage (other, self, self.owner, 15);
		        	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY); 
				WriteByte (MSG_BROADCAST, TE_SUPERSPIKE);
			        WriteCoord (MSG_BROADCAST, self.origin_x); 
			        WriteCoord (MSG_BROADCAST, self.origin_y); 
		        	WriteCoord (MSG_BROADCAST, self.origin_z); 
		                remove(self);   //add this so they don't float... 
        		        return;         //add this to avoid errors 
			}
		}
		
var float autocvar_fleshsplat = 0;     	// set cvar 'fleshsplat' default to 0
if (autocvar_fleshsplat == 1)		
{
    self.angles_x = self.angles_x*-1; //account for error in makevectors with non-player entities
    makevectors(self.angles); //find direction nail is facing

	if (other.classname == "monster_wizard")
	{        
		ThrowBloodSplat(v_forward, self.origin, other, 1, 1); 
	}
	else if (other.classname == "monster_army")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
	else if (other.classname == "monster_ogre")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
	else if (other.classname == "monster_hell_knight")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1); 
		if (!(other.spawnflags & 2))
			pointparticles(particleeffectnum("sparks"), self.origin, '0 0 0', 1); 
	}
	else if (other.classname == "monster_knight")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
		if (!(other.spawnflags & 2))
			pointparticles(particleeffectnum("sparks"), self.origin, '0 0 0', 1); 
	}
	else if (other.classname == "monster_demon1")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 3, 1);
	}
	else if (other.classname == "monster_dog")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1); 
	}
	else if (other.classname == "monster_shalrath")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 4, 1); 
	}
	else if (other.classname == "monster_tarbaby")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 5, 1); 
	}
	else if (other.classname == "monster_shambler")
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
	else 
	{
		ThrowBloodSplat(v_forward, self.origin, other, 2, 1);
	}
}

		spawn_touchblood (18);
		
		// halve the effectiveness of the armor for players..
		if(other.classname == "player")
		{
			// save the old armor values...
			old_armortype = other.armortype;
//			old_armorvalue = other.armorvalue;
			other.armortype = other.armortype * 0.5;
//			other.armorvalue = 0;
			T_Damage (other, self, self.owner, 18);
			
			// if the damage didn't wipe out the armor, armortype 
			if(other.armortype != 0)
			{
				other.armortype = old_armortype;
//				other.armorvalue = old_armorvalue;
			}
		}
		else		// is a monster, do 50% more damage
		{
			if ( other.classname != "monster_lava_man")
			{
				T_Damage (other, self, self.owner, 30);
			}
		}

                remove(self);   //add this so they don't float... 
                return;         //add this to avoid errors 
	}

        else if (other.solid != SOLID_BSP) 
        { 
                remove(self); 
                return; 
        }
		pointparticles(particleeffectnum("TE_LAVASUPERSPIKE"), self.origin, '0 0 0', 1);
	/*	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
		WriteByte (MSG_BROADCAST, TE_SUPERSPIKE);
		WriteCoord (MSG_BROADCAST, self.origin_x);
		WriteCoord (MSG_BROADCAST, self.origin_y);
		WriteCoord (MSG_BROADCAST, self.origin_z);
	*/
	
        self.solid = SOLID_NOT; 
        self.movetype = MOVETYPE_NONE; 

        setorigin (self, self.origin - normalize(self.velocity) * 3); 
        self.velocity = '0 0 0';         //add this, it makes the nail stop 

      if (other.solid == SOLID_BSP && other != world) 
      { 
         self.owner = other; 
         self.pos1 = self.origin; 
         self.pos2 = other.origin; 
         self.aflag = time + 10; 
         self.think = spike_stuck_think; 
         self.nextthink = time; 
      } 
      else 
      { 
        self.nextthink = time + 17;
         self.think = SUB_Remove;        //add this for same reason as above 
      } 
};
