/*More changes than you can shake a stick at.
*/

void () player_run;
void() SuperDamageSound;
void(vector org, vector vel, float damage) SpawnBlood;
void(string gibname, float dm) ThrowGib; //For explosions
void (entity targ, entity inflictor, entity attacker, float damage) T_Damage;
void(entity bomb, entity attacker, float rad, entity ignore) T_RadiusDamage;

// called by worldspawn
void() W_Precache =
{
        precache_sound2 ("blob/land1.wav");     // chain go splorch!
        precache_sound ("plats/medplat1.wav");  // chain cranking out
        precache_sound ("doors/ddoor1.wav");    // chain cranking in
	precache_sound ("items/protect3.wav");
	precache_sound ("items/damage3.wav");
        precache_model ("progs/throwaxe.mdl");  // thrown axe model
        precache_sound ("weapons/woosh.wav");  // WOOSH sound for throwing axe
        precache_sound ("weapons/taxhit1.wav");  // new ax hit sound (FROM QTEST)
        precache_sound ("weapons/taxhit2.wav");  // new ax hit sound (FROM QTEST)
        precache_sound ("weapons/taxhit3.wav");  // new ax hit sound (FROM QTEST)
        precache_sound ("weapons/arm.wav");  // ProxBomb Arming sound
        precache_sound ("weapons/r_exp3.wav");  // new rocket explosion
	precache_sound ("weapons/rocket1i.wav");	// spike gun
	precache_sound ("weapons/sgun1.wav");
	precache_sound ("weapons/shellhit.wav"); //Shell ejection
	precache_sound ("weapons/guncock.wav");	// player shotgun
	precache_sound ("weapons/ric1.wav");	// ricochet (used in c code)
	precache_sound ("weapons/ric2.wav");	// ricochet (used in c code)
	precache_sound ("weapons/ric3.wav");	// ricochet (used in c code)
	precache_sound ("weapons/spike2.wav");	// super spikes
	precache_sound ("weapons/tink1.wav");	// spikes tink (used in c code)
	precache_sound ("weapons/grenade.wav");	// grenade launcher
	precache_sound ("weapons/bounce.wav");		// grenade bounce
        precache_model ("progs/h_demon.mdl");
        precache_model ("progs/h_dog.mdl");
        precache_sound ("items/Inv1.wav");              //used in hologram
        precache_sound ("items/Itembk2.wav");              //used in hologram
        precache_sound ("misc/R_tele5.wav");              //used in hologram
        precache_model ("progs/laser.mdl");
        precache_model ("progs/shelcase.mdl"); //Shell ejection
        precache_sound ("enforcer/enfire.wav"); // player shotgun
        precache_sound ("weapons/shotgn2.wav"); // super shotgun
        precache_sound ("zombie/z_hit.wav");
};

float() crandom =
{
	return 2*(random() - 0.5);
};

void(entity attacker, float recoil_val) Recoil =
{
        local float friction,gravity;
        local vector dir,friction_vec;

        dir = aim(attacker,recoil_val);
        dir = normalize (dir);
        recoil_val =  -10*recoil_val;
        gravity = cvar ("SV_GRAVITY");
        friction_vec = gravity * '0 0 1';
        if (self.flags & FL_ONGROUND)
        {
                friction = cvar ("SV_FRICTION");
                friction_vec = friction * dir + friction_vec;
        }
        attacker.velocity = recoil_val * dir + attacker.velocity + friction_vec;
};

void() W_FireAxe =
{
	local	vector	source;
	local	vector	org;

	source = self.origin + '0 0 16';
	traceline (source, source + v_forward*64, FALSE, self);
	if (trace_fraction == 1.0)
		return;
	
	org = trace_endpos - v_forward*4;

	if (trace_ent.takedamage)
	{
		trace_ent.axhitme = 1;
		SpawnBlood (org, '0 0 0', 20);
		sound (self, CHAN_WEAPON, "zombie/z_hit.wav", 1, ATTN_NORM);
		T_Damage (trace_ent, self, self, 20);
	}
	else
	{	// hit wall
		sound (self, CHAN_WEAPON, "player/axhit2.wav", 1, ATTN_NORM);
		WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
		WriteByte (MSG_BROADCAST, TE_GUNSHOT);
		WriteCoord (MSG_BROADCAST, org_x);
		WriteCoord (MSG_BROADCAST, org_y);
		WriteCoord (MSG_BROADCAST, org_z);
	}
};

vector() wall_velocity =
{
	local vector	vel;
	
	vel = normalize (self.velocity);
	vel = normalize(vel + v_up*(random()- 0.5) + v_right*(random()- 0.5));
	vel = vel + 2*trace_plane_normal;
	vel = vel * 200;
	return vel;
};

void(vector org, vector vel) SpawnMeatSpray =
{
	local	entity missile, mpuff;
	local	vector	org;

	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_BOUNCE;
	missile.solid = SOLID_NOT;

	makevectors (self.angles);
	missile.velocity = vel;
	missile.velocity_z = missile.velocity_z + 250 + 50*random();
	missile.avelocity = '3000 1000 2000';
	missile.nextthink = time + 1;
	missile.think = SUB_Remove;
	setmodel (missile, "progs/zom_gib.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, org);
};

void(vector org, vector vel, float damage) SpawnBlood =
{
	particle (org, vel*0.1, 73, damage*2);
        particle (org, vel*0.2, 73, damage*4);
        particle (org, vel*0.3, 73, damage*8);
        particle (org, vel*0.4, 73, damage*16);
};

void(float damage) spawn_touchblood =
{
	local vector	vel;

	vel = wall_velocity () * 0.2;
	SpawnBlood (self.origin + vel*0.01, vel, damage);
        SpawnBlood (self.origin + vel*0.04, vel, damage+15);
};

void(vector org, vector vel) SpawnChunk =
{
	particle (org, vel*0.02, 0, 10);
        particle (org, vel*0.05, 0, 40);
};

entity	multi_ent;
float	multi_damage;

void() ClearMultiDamage =
{
	multi_ent = world;
	multi_damage = 0;
};

void() ApplyMultiDamage =
{
	if (!multi_ent)
		return;
	T_Damage (multi_ent, self, self, multi_damage);
};

void(entity hit, float damage) AddMultiDamage =
{
	if (!hit)
		return;
	
	if (hit != multi_ent)
	{
		ApplyMultiDamage ();
		multi_damage = damage;
		multi_ent = hit;
	}
	else
		multi_damage = multi_damage + damage;
};

void(float damage, vector dir) TraceAttack =
{
	local	vector	vel, org;
	
	vel = normalize(dir + v_up*crandom() + v_right*crandom());
	vel = vel + 2*trace_plane_normal;
	vel = vel * 200;
	org = trace_endpos - dir*4;
	if (trace_ent.takedamage)
	{
		SpawnBlood (org, vel*0.2, damage);
		AddMultiDamage (trace_ent, damage);
	}
	else
	{
		WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
		WriteByte (MSG_BROADCAST, TE_GUNSHOT);
		WriteCoord (MSG_BROADCAST, org_x);
		WriteCoord (MSG_BROADCAST, org_y);
		WriteCoord (MSG_BROADCAST, org_z);
	}
};

void(float shotcount, vector dir, vector spread) FireBullets =
{
	local	vector direction;
	local	vector	src;
	
	makevectors(self.v_angle);
        src = self.origin + v_forward*10;
	src_z = self.absmin_z + self.size_z * 0.7;
	ClearMultiDamage ();
	while (shotcount > 0)
	{
		direction = dir + crandom()*spread_x*v_right + crandom()*spread_y*v_up;

		traceline (src, src + direction*2048, FALSE, self);
		if (trace_fraction != 1.0)
			TraceAttack (4, direction);

		shotcount = shotcount - 1;
	}
	ApplyMultiDamage ();
};

void(vector org,vector dir) eject_shell; //For shell ejection
void(vector org, vector dir) Launch_Laser;

void() W_FireShotgun =
{
       local vector org,dir;
        
       self.currentammo = self.ammo_shells = self.ammo_shells - 1;
       self.effects = self.effects | EF_MUZZLEFLASH;
       self.attack_finished = time + 0.5;
       makevectors (self.v_angle);
       org = self.origin + v_forward * 7.5 + '0 0 16';
       dir = aim (self, 1800);
       Launch_Laser(org, dir);
       self.punchangle_x = -2;
       Recoil (self,3); // Not too much recoil
       eject_shell (self.origin + '0 0 16' + v_forward*3,dir); //eject shell
};

void() W_FireSuperShotgun =
{
	local vector dir;

	if (self.currentammo == 1)
	{
		W_FireShotgun ();
		return;
	}
		
	sound (self ,CHAN_WEAPON, "weapons/shotgn2.wav", 1, ATTN_NORM);	
	self.punchangle_x = -4;
	self.currentammo = self.ammo_shells = self.ammo_shells - 2;
	dir = aim (self, 100000);
	FireBullets (14, dir, '0.14 0.08 0');
        Recoil (self,10);
        eject_shell (self.origin + '0 0 16' + v_forward*3,dir); //eject shell
        eject_shell (self.origin + '0 0 16' + v_forward*3,dir);
};

void()	s_explode1	=	[0,		s_explode2] {};
void()	s_explode2	=	[1,		s_explode3] {};
void()	s_explode3	=	[2,		s_explode4] {};
void()	s_explode4	=	[3,		s_explode5] {};
void()	s_explode5	=	[4,		s_explode6] {};
void()	s_explode6	=	[5,		SUB_Remove] {};

void() BecomeExplosion =
{
	self.movetype = MOVETYPE_NONE;
	self.solid = SOLID_NOT;
        ThrowGib ("progs/zom_gib.mdl", -500);
        ThrowGib ("progs/zom_gib.mdl", -250);
        ThrowGib ("progs/zom_gib.mdl", -50);
        ThrowGib ("progs/zom_gib.mdl", -25);
        if ( !(self.velocity = '0 0 0') ) // And Blood too
        {
                SpawnBlood (self.origin,self.velocity,500);
                SpawnBlood (self.origin,-1*self.velocity,500);
                self.velocity = '0 0 0';
        }
        else
        {
                SpawnBlood (self.origin,'0 0 100',500);
                SpawnBlood (self.origin,'0 100 0',500);
                SpawnBlood (self.origin,'0 -100 0',500);
                SpawnBlood (self.origin,'-100 0 0',500);
        }
	self.touch = SUB_Null;
	sound (self, CHAN_WEAPON, "weapons/r_exp3.wav", 1, ATTN_NORM);
        setmodel (self, "progs/s_explod.spr");  //A little fire goes a long way
        s_explode1 ();
};

void() T_MissileTouch =
{
	local float	damg;

	if (other == self.owner)
		return;		// don't explode on owner
	if (pointcontents(self.origin) == CONTENT_SKY)
	{
		remove(self);
		return;
	}
	damg = 100 + random()*20;
	if (other.health)
	{
		if (other.classname == "monster_shambler")
			damg = damg * 0.5;	// mostly immune
		T_Damage (other, self, self.owner, damg );
	}
	T_RadiusDamage (self, self.owner, 120, other);
	self.origin = self.origin - 8*normalize(self.velocity);
	BecomeExplosion ();
};

void() W_FireRocket =
{
	local	entity missile, mpuff;
	
	self.currentammo = self.ammo_rockets = self.ammo_rockets - 1;
	sound (self, CHAN_WEAPON, "weapons/sgun1.wav", 1, ATTN_NORM);
        self.punchangle_x = -2;
 	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_FLYMISSILE;
	missile.solid = SOLID_BBOX;
        missile.effects = EF_DIMLIGHT;
	makevectors (self.v_angle);
        missile.velocity = aim(self, 1500);
        missile.velocity = missile.velocity * 1500;
	missile.angles = vectoangles(missile.velocity);
	missile.touch = T_MissileTouch;
	missile.nextthink = time + 5;
	missile.think = SUB_Remove;
        setmodel (missile, "progs/h_demon.mdl");
        setsize (missile, '0 0 0', '0 0 0');     
	setorigin (missile, self.origin + v_forward*8 + '0 0 16');
        Recoil (self,20);       //Now that is some kick
};

void(vector p1, vector p2, entity from, float damage) LightningDamage =
{
	local entity		e1, e2;
	local vector		f;
	
	f = p2 - p1;
	normalize (f);
	f_x = 0 - f_y;
	f_y = f_x;
	f_z = 0;
	f = f*16;
	e1 = e2 = world;
	traceline (p1, p2, FALSE, self);
	if (trace_ent.takedamage)
	{
		particle (trace_endpos, '0 0 100', 225, damage*4);
		T_Damage (trace_ent, from, from, damage);
		if (self.classname == "player")
		{
			if (other.classname == "player")
				trace_ent.velocity_z = trace_ent.velocity_z + 400;
		}
	}
	e1 = trace_ent;
	traceline (p1 + f, p2 + f, FALSE, self);
	if (trace_ent != e1 && trace_ent.takedamage)
	{
		particle (trace_endpos, '0 0 100', 225, damage*4);
		T_Damage (trace_ent, from, from, damage);
	}
	e2 = trace_ent;
	traceline (p1 - f, p2 - f, FALSE, self);
	if (trace_ent != e1 && trace_ent != e2 && trace_ent.takedamage)
	{
		particle (trace_endpos, '0 0 100', 225, damage*4);
		T_Damage (trace_ent, from, from, damage);
	}
};


void() W_FireLightning =
{
	local	vector		org;

	if (self.ammo_cells < 1)
	{
		self.weapon = W_BestWeapon ();
		W_SetCurrentAmmo ();
		return;
	}
	if (self.waterlevel > 1)
	{
		T_RadiusDamage (self, self, 35*self.ammo_cells, world);
		self.ammo_cells = 0;
		W_SetCurrentAmmo ();
		return;
	}
	if (self.t_width < time)
	{
		sound (self, CHAN_WEAPON, "weapons/lhit.wav", 1, ATTN_NORM);
		self.t_width = time + 0.6;
	}
	self.punchangle_x = -2;
	self.currentammo = self.ammo_cells = self.ammo_cells - 1;
	org = self.origin + '0 0 16';
	traceline (org, org + v_forward*600, TRUE, self);
	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
	WriteByte (MSG_BROADCAST, TE_LIGHTNING2);
	WriteEntity (MSG_BROADCAST, self);
	WriteCoord (MSG_BROADCAST, org_x);
	WriteCoord (MSG_BROADCAST, org_y);
	WriteCoord (MSG_BROADCAST, org_z);
	WriteCoord (MSG_BROADCAST, trace_endpos_x);
	WriteCoord (MSG_BROADCAST, trace_endpos_y);
	WriteCoord (MSG_BROADCAST, trace_endpos_z);
	LightningDamage (self.origin, trace_endpos + v_forward*4, self, 30);
};

void() GrenadeExplode =
{
	T_RadiusDamage (self, self.owner, 120, world);
	BecomeExplosion ();
};

void() GrenadeTouch =
{
	if (other == self.owner)
		return;		// don't explode on owner
	if (other.takedamage == DAMAGE_AIM)
	{
		GrenadeExplode();
		return;
	}
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);	// bounce sound
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
};

void() W_FireGrenade =
{
	local	entity missile, mpuff;
	
	self.currentammo = self.ammo_rockets = self.ammo_rockets - 1;
	sound (self, CHAN_WEAPON, "weapons/grenade.wav", 1, ATTN_NORM);
	self.punchangle_x = -2;
	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 + v_up * 200 + crandom()*v_right*10 + crandom()*v_up*10;
	else
	{
		missile.velocity = aim(self, 10000);
		missile.velocity = missile.velocity * 600;
		missile.velocity_z = 200;
	}
	missile.avelocity = '300 300 300';
	missile.angles = vectoangles(missile.velocity);
	missile.touch = GrenadeTouch;
	missile.nextthink = time + 2.5;
	missile.think = GrenadeExplode;
        setmodel (missile, "progs/h_dog.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin);
        Recoil (self,12);  //Pretty nice recoil huh?
};

void () ProximityTouch =
{
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
};

void () Proximity =
{
	local entity head;

        head = findradius(self.origin,150);
        while(head)
	{
		if ( (head.health > 1) && (head.classname == "player") && !(head.items & IT_INVISIBILITY) )
		{
			sound (self, CHAN_WEAPON, "weapons/arm.wav", 1, ATTN_NORM);
                        self.nextthink = time + 0.2;
			self.think = GrenadeExplode;
			return;
		}
		head = head.chain;
	}
        self.nextthink = time + 0.25;
};

void() W_DropBomb =
{
	local entity item;

        if (self.ammo_rockets <= 2)
		return;
	self.ammo_rockets = self.ammo_rockets - 2;
	self.flags = self.flags | FL_ATTACKED;
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);
	item = spawn();
	item.owner = self;
	makevectors(self.v_angle);
        setorigin(item,self.origin - '0 0 17');
        item.velocity= '0 0 0';
        item.avelocity='0 800 800';
	item.flags = FL_ITEM;
        item.solid = SOLID_BBOX;
        item.movetype = MOVETYPE_FLY;
	item.classname = "proximity";
        item.health = 20;
        item.th_die = GrenadeExplode;
        item.takedamage = DAMAGE_AIM;
	setmodel (item, "progs/grenade.mdl");
        setsize (item, '-48 -48 -4', '48 48 4');            
        item.nextthink = time + 3;
	item.think = Proximity;
	W_SetCurrentAmmo();
};

.float hit_z;
void() spike_touch =
{
local float rand;
	if (other == self.owner)
		return;
	if (other.solid == SOLID_TRIGGER)
		return;	// trigger field, do nothing
	if (pointcontents(self.origin) == CONTENT_SKY)
	{
		remove(self);
		return;
	}
	if (other.takedamage)
	{
		spawn_touchblood (9);
		T_Damage (other, self, self.owner, 9);
	}
	else
	{
		WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
		if (self.classname == "wizspike")
			WriteByte (MSG_BROADCAST, TE_WIZSPIKE);
		else if (self.classname == "knightspike")
			WriteByte (MSG_BROADCAST, TE_KNIGHTSPIKE);
		else
			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);
};

void() superspike_touch =
{
local float rand;
	if (other == self.owner)
		return;
	if (other.solid == SOLID_TRIGGER)
		return;	// trigger field, do nothing
	if (pointcontents(self.origin) == CONTENT_SKY)
	{
		remove(self);
		return;
	}
	if (other.takedamage)
	{
		spawn_touchblood (18);
		T_Damage (other, self, self.owner, 18);
	}
	else
	{
		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);
};

void(vector org, vector dir) launch_hologram;

void(float ox) W_FireHologram =      //Used to setup and launch the HOLOGRAM
{

	local vector    dir;
	
	makevectors (self.v_angle);
	self.attack_finished = time + 0.5;
	dir = aim (self, 1000);
        launch_hologram (self.origin + '0 0 24' + v_right*ox, dir);
};

void(vector org, vector dir) launch_spike =
{
	newmis = spawn ();
	newmis.owner = self;
	newmis.movetype = MOVETYPE_FLYMISSILE;
	newmis.solid = SOLID_BBOX;
	newmis.angles = vectoangles(dir);
	newmis.touch = spike_touch;
	newmis.classname = "spike";
	newmis.think = SUB_Remove;
	newmis.nextthink = time + 6;
	setmodel (newmis, "progs/spike.mdl");
	setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);		
	setorigin (newmis, org);
	newmis.velocity = dir * 1000;
};

void() W_FireSuperSpikes =
{
	local vector	dir;
	local entity	old;
	
	sound (self, CHAN_WEAPON, "weapons/spike2.wav", 1, ATTN_NORM);
	self.attack_finished = time + 0.2;
	self.currentammo = self.ammo_nails = self.ammo_nails - 2;
	dir = aim (self, 1000);
	launch_spike (self.origin + '0 0 16', dir);
	newmis.touch = superspike_touch;
	setmodel (newmis, "progs/s_spike.mdl");
	setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);		
	self.punchangle_x = -2;
        Recoil (self,5);        //Just hold that attack button down
};

void(float ox) W_FireSpikes =
{
	local vector	dir;
	local entity	old;
	
	makevectors (self.v_angle);
	if (self.ammo_nails >= 2 && self.weapon == IT_SUPER_NAILGUN)
	{
		W_FireSuperSpikes ();
		return;
	}
	if (self.ammo_nails < 1)
	{
		self.weapon = W_BestWeapon ();
		W_SetCurrentAmmo ();
		return;
	}
	sound (self, CHAN_WEAPON, "weapons/rocket1i.wav", 1, ATTN_NORM);
	self.attack_finished = time + 0.2;
	self.currentammo = self.ammo_nails = self.ammo_nails - 1;
	dir = aim (self, 1000);
	launch_spike (self.origin + '0 0 16' + v_right*ox, dir);
	self.punchangle_x = -2;
        Recoil (self,2);   //Seems small now, go to E1M8
};

void(vector org, vector dir) launch_flare;

void() W_FireFlare =
{
        local vector org,dir;
        
        self.attack_finished = time + 0.5;
        makevectors (self.v_angle);
        org = self.origin + v_forward * 7.5 + '0 0 16';
        dir = aim (self, 1800);
        launch_flare(org, dir);
	self.punchangle_x = -2;
};

void() DetPipeBombs =
{
        local entity    head;

        self.flags = self.flags | FL_ATTACKED; //says that you have attacked for teamplay
        head = findradius (self.origin, 1000);
        while(head)
        {
                if(head.classname == "pipebomb" || head.classname == "proximity")
                        if (head.owner == self)
                        {
                                sound (self, CHAN_WEAPON, "weapons/r_exp3.wav", 1, ATTN_NORM);
                                head.nextthink = time;
                        }
                head = head.chain;
        }
};

void() PipeBombTouch =
{
	if (other == self.owner)
		return;		// don't explode on owner
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);	// bounce sound
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
};

void() W_FirePipeBomb =
{
	local	entity missile, mpuff;
	
        if(self.ammo_rockets < 4)       // have to have enough ammo
	  return;
        self.currentammo = self.ammo_rockets = self.ammo_rockets - 4;
        self.flags = self.flags | FL_ATTACKED; //says that you have attacked for teamplay
        sound (self, CHAN_WEAPON, "weapons/grenade.wav", 1, ATTN_NORM);
        self.punchangle_x = -2;
	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_BOUNCE;
	missile.solid = SOLID_BBOX;
	missile.classname = "pipebomb";
	makevectors (self.v_angle);
	if (self.v_angle_x)
		missile.velocity = v_forward*600 + v_up * 200 + crandom()*v_right*10 + crandom()*v_up*10;
	else
	{
		missile.velocity = aim(self, 10000);
		missile.velocity = missile.velocity * 600;
		missile.velocity_z = 200;
	}
	missile.avelocity = '300 300 300';
	missile.angles = vectoangles(missile.velocity);
	missile.touch = PipeBombTouch;
	missile.think = GrenadeExplode;
	setmodel (missile, "progs/grenade.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin);
};

void() W_SetCurrentAmmo =
{
 if(self.classname != "player")
   return;
	player_run ();		// get out of any weapon firing states

	self.items = self.items - ( self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS) );
	
	if (self.weapon == IT_AXE)
	{
		self.currentammo = 0;
		self.weaponmodel = "progs/v_axe.mdl";
		self.weaponframe = 0;
	}
        else if (self.weapon == IT_THROWING_AXE)
	{
                self.currentammo = self.num_axes;
		self.weaponmodel = "progs/v_axe.mdl";
		self.weaponframe = 0;
                
	}
        else if (self.weapon == IT_MORNINGSTAR)
        {
                self.currentammo = 0;
                self.weaponmodel = "progs/v_star.mdl";
                self.weaponframe = 0;
        }
	else if (self.weapon == IT_SHOTGUN)
	{
		self.currentammo = self.ammo_shells;
		self.weaponmodel = "progs/v_shot.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_SHELLS;
	}
	else if (self.weapon == IT_SUPER_SHOTGUN)
	{
		self.currentammo = self.ammo_shells;
		self.weaponmodel = "progs/v_shot2.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_SHELLS;
	}
	else if (self.weapon == IT_NAILGUN)
	{
		self.currentammo = self.ammo_nails;
		self.weaponmodel = "progs/v_nail.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_NAILS;
	}
	else if (self.weapon == IT_SUPER_NAILGUN)
	{
		self.currentammo = self.ammo_nails;
		self.weaponmodel = "progs/v_nail2.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_NAILS;
	}
	else if (self.weapon == IT_GRENADE_LAUNCHER)
	{
		self.currentammo = self.ammo_rockets;
		self.weaponmodel = "progs/v_rock.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_ROCKETS;
	}
	else if (self.weapon == IT_ROCKET_LAUNCHER)
	{
		self.currentammo = self.ammo_rockets;
		self.weaponmodel = "progs/v_rock2.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_ROCKETS;
	}
	else if (self.weapon == IT_LIGHTNING)
	{
		self.currentammo = self.ammo_cells;
		self.weaponmodel = "progs/v_light.mdl";
		self.weaponframe = 0;
		self.items = self.items | IT_CELLS;
	}
	else
	{
		self.currentammo = 0;
		self.weaponmodel = "";
		self.weaponframe = 0;
	}
};

float() W_BestWeapon =
{
	local	float	it;
	
	it = self.items;
        if(self.ammo_cells >= 1 && (it & IT_LIGHTNING) && (self.waterlevel <= 1))
		return IT_LIGHTNING;
	else if(self.ammo_nails >= 2 && (it & IT_SUPER_NAILGUN) )
		return IT_SUPER_NAILGUN;
	else if(self.ammo_shells >= 2 && (it & IT_SUPER_SHOTGUN) )
		return IT_SUPER_SHOTGUN;
	else if(self.ammo_nails >= 1 && (it & IT_NAILGUN) )
		return IT_NAILGUN;
	else if(self.ammo_shells >= 1 && (it & IT_SHOTGUN) )
		return IT_SHOTGUN;
	return IT_AXE;
};

float() W_CheckNoAmmo =
{
	if (self.currentammo > 0)
		return TRUE;
        if (self.weapon == IT_AXE || self.weapon == IT_MORNINGSTAR)
		return TRUE;
        self.weapon = W_BestWeapon ();
	W_SetCurrentAmmo ();
	return FALSE;
};

void()	player_axe1;
void()	player_axeb1;
void()	player_axec1;
void()	player_axed1;
void()	player_shot1;
void()	player_nail1;
void()	player_light1;
void()	player_rocket1;
void()  player_tax1; //thrown axes
void()	player_chain1;
void()  player_chain3;

void() W_Attack =
{
	local	float	r;

	if (!W_CheckNoAmmo ())
		return;

	makevectors	(self.v_angle);			// calculate forward angle for velocity
	self.show_hostile = time + 1;	// wake monsters up
        self.flags = self.flags | FL_ATTACKED; //says that you have attacked for teamplay
	if (self.weapon == IT_AXE)
	{
		sound (self, CHAN_WEAPON, "weapons/ax1.wav", 1, ATTN_NORM);
		r = random();
		if (r < 0.25)
			player_axe1 ();
		else if (r<0.5)
			player_axeb1 ();
		else if (r<0.75)
			player_axec1 ();
		else
			player_axed1 ();
		self.attack_finished = time + 0.5;
	}
        else if (self.weapon == IT_THROWING_AXE)
	{
                player_tax1 ();

                self.attack_finished = time + 1;
	}
        else if (self.weapon == IT_MORNINGSTAR)
        {
                if (!self.hook_out) {player_chain1();}
                else {player_chain3();}
                self.attack_finished = time + 0.1;
        }
	else if (self.weapon == IT_SHOTGUN)
	{
		player_shot1 ();
		W_FireShotgun ();
		self.attack_finished = time + 0.5;
	}
	else if (self.weapon == IT_SUPER_SHOTGUN)
	{
		player_shot1 ();
		W_FireSuperShotgun ();
		self.attack_finished = time + 0.7;
	}
	else if (self.weapon == IT_NAILGUN)
	{
		player_nail1 ();
	}
	else if (self.weapon == IT_SUPER_NAILGUN)
	{
		player_nail1 ();
	}
	else if (self.weapon == IT_GRENADE_LAUNCHER)
	{
		player_rocket1();
		W_FireGrenade();
		self.attack_finished = time + 0.6;
	}
	else if (self.weapon == IT_ROCKET_LAUNCHER)
	{
		player_rocket1();
		W_FireRocket();
		self.attack_finished = time + 0.8;
	}
	else if (self.weapon == IT_LIGHTNING)
	{
		player_light1();
		self.attack_finished = time + 0.1;
		sound (self, CHAN_AUTO, "weapons/lstart.wav", 1, ATTN_NORM);
	}
};

void() W_ChangeWeapon =
{
	local	float	it, am, fl;
 
	it = self.items;
	am = 0;
	if (self.impulse == 1)
	{
                if (self.weapon == IT_AXE)
                {
                        sprint(self,"Throwing Axe!\n");
                        fl=IT_THROWING_AXE;
                        if (self.num_axes <1)
                                am = 1;
                }
                else if (self.weapon == IT_THROWING_AXE)
                {
                        sprint(self,"Morning Star!\n");
                        fl=IT_MORNINGSTAR;
                }
                else
                {
                        sprint(self,"Normal Axe!\n");
                        fl = IT_AXE;
                }                
	}
	else if (self.impulse == 2)
	{
		fl = IT_SHOTGUN;
		if (self.ammo_shells < 1)
			am = 1;
	}
	else if (self.impulse == 3)
	{
		fl = IT_SUPER_SHOTGUN;
		if (self.ammo_shells < 2)
			am = 1;
	}		
	else if (self.impulse == 4)
	{
		fl = IT_NAILGUN;
		if (self.ammo_nails < 1)
			am = 1;
	}
	else if (self.impulse == 5)
	{
		fl = IT_SUPER_NAILGUN;
		if (self.ammo_nails < 2)
			am = 1;
	}
	else if (self.impulse == 6)
	{
		fl = IT_GRENADE_LAUNCHER;
		if (self.ammo_rockets < 1)
			am = 1;
	}
	else if (self.impulse == 7)
	{
		fl = IT_ROCKET_LAUNCHER;
		if (self.ammo_rockets < 1)
			am = 1;
	}
	else if (self.impulse == 8)
	{
		fl = IT_LIGHTNING;
		if (self.ammo_cells < 1)
			am = 1;
	}
	self.impulse = 0;
	if (!(self.items & fl))
	{	// don't have the weapon or the ammo
		sprint (self, "no weapon.\n");
		return;
	}
	if (am)
	{	// don't have the ammo
		sprint (self, "not enough ammo.\n");
		return;
	}
	self.weapon = fl;		
	W_SetCurrentAmmo ();
};

void() CheatCommand =
{
	if (deathmatch || coop)
		return;

	self.ammo_rockets = 100;
	self.ammo_nails = 200;
	self.ammo_shells = 100;
	self.num_axes = 100;
	self.items = self.items | 
		IT_AXE |
		IT_SHOTGUN |
		IT_SUPER_SHOTGUN |
		IT_NAILGUN |
		IT_SUPER_NAILGUN |
		IT_GRENADE_LAUNCHER |
		IT_ROCKET_LAUNCHER |
		IT_KEY1 | IT_KEY2;

	self.ammo_cells = 200;
	self.items = self.items | IT_LIGHTNING;
	self.weapon = IT_ROCKET_LAUNCHER;
	self.impulse = 0;
	W_SetCurrentAmmo ();
};

void() CycleWeaponCommand =
{
	local	float	it, am;
	
	it = self.items;
	self.impulse = 0;
	while (1)
	{
		am = 0;
		if (self.weapon == IT_LIGHTNING)
		{
			self.weapon = IT_AXE;
		}
                else if (self.weapon == IT_AXE || self.weapon == IT_THROWING_AXE || self.weapon == IT_MORNINGSTAR)
		{
			self.weapon = IT_SHOTGUN;
			if (self.ammo_shells < 1)
				am = 1;
		}
		else if (self.weapon == IT_SHOTGUN)
		{
			self.weapon = IT_SUPER_SHOTGUN;
			if (self.ammo_shells < 2)
				am = 1;
		}		
		else if (self.weapon == IT_SUPER_SHOTGUN)
		{
			self.weapon = IT_NAILGUN;
			if (self.ammo_nails < 1)
				am = 1;
		}
		else if (self.weapon == IT_NAILGUN)
		{
			self.weapon = IT_SUPER_NAILGUN;
			if (self.ammo_nails < 2)
				am = 1;
		}
		else if (self.weapon == IT_SUPER_NAILGUN)
		{
			self.weapon = IT_GRENADE_LAUNCHER;
			if (self.ammo_rockets < 1)
				am = 1;
		}
		else if (self.weapon == IT_GRENADE_LAUNCHER)
		{
			self.weapon = IT_ROCKET_LAUNCHER;
			if (self.ammo_rockets < 1)
				am = 1;
		}
		else if (self.weapon == IT_ROCKET_LAUNCHER)
		{
			self.weapon = IT_LIGHTNING;
			if (self.ammo_cells < 1)
				am = 1;
		}
	
		if ( (self.items & self.weapon) && am == 0)
		{
			W_SetCurrentAmmo ();
			return;
		}
	}
};

void() ServerflagsCommand =
{
	serverflags = serverflags * 2 + 1;
};

void() QuadCheat =
{
	if (deathmatch || coop)
		return;
	self.super_time = 1;
	self.super_damage_finished = time + 30;
	self.items = self.items | IT_QUAD;
};

void() backpack_explode;
void() ThrowBackpack =
{
	local entity 	item;

	if ( (self.ammo_shells + self.ammo_nails + self.ammo_rockets + self.ammo_cells) == 0)
		return;
	item = spawn();
        if ( self.ammo_shells >= 20)
	{
		item.ammo_shells = 20;
		self.ammo_shells = self.ammo_shells - 20;
	}
	else
	{
		item.ammo_shells = self.ammo_shells;
		self.ammo_shells = 0;
	}
	
        if ( self.ammo_nails >= 20)
	{
		item.ammo_nails = 20;
                self.ammo_nails = self.ammo_nails - 20;
	}
	else
	{
		item.ammo_nails = self.ammo_nails;
		self.ammo_nails = 0;
	}

	if ( self.ammo_rockets >= 10)
	{
		item.ammo_rockets = 10;
		self.ammo_rockets = self.ammo_rockets - 10;
	}
	else
	{
		item.ammo_rockets = self.ammo_rockets;
                self.ammo_rockets = 0;
	}

	if (self.ammo_cells >= 20)
	{	
		item.ammo_cells = 20;
		self.ammo_cells = self.ammo_cells - 20;
	}
	else
	{
		item.ammo_cells = self.ammo_cells;
		self.ammo_cells = 0;
	}
	makevectors(self.v_angle);
	item.velocity = aim(self, 1000);
        item.velocity = 500 * item.velocity;
        setorigin(item, self.origin + '0 0 40');
	item.flags = FL_ITEM;
        item.solid = SOLID_SLIDEBOX;
        item.movetype = MOVETYPE_TOSS;
        setmodel (item, "progs/backpack.mdl");
        setsize(item, '-16 -16 0', '16 16 56');
	item.touch = BackpackTouch;
        if (item.ammo_rockets)
        {
                item.health = 30;
                item.th_die = backpack_explode;
                item.takedamage = DAMAGE_AIM;
        }
        else
                item.takedamage = DAMAGE_NO;
	item.nextthink = time + 120;	// remove after 2 minutes
        item.think = SUB_Remove;
	W_SetCurrentAmmo();
};

void() feign;   // feign death
void(entity me)     CameraOn;
void(entity me)     CameraOff;
void(entity ex) hologram_explode;

void() ImpulseCommands =
{
      if (self.impulse == 150)
      {
            feign ();
            self.impulse = 0;
      }
      if (self.flags & FL_ISFEIGN)
            return;
      if (self.impulse >= 1 && self.impulse <= 8)
            W_ChangeWeapon ();
      if (self.impulse == 9)
            CheatCommand ();
      if (self.impulse == 10)
            CycleWeaponCommand ();
      if (self.impulse == 11)
            ServerflagsCommand ();
      if (self.impulse == 20)
            ThrowBackpack();
      if (self.impulse == 21)
            W_DropBomb();
      if (self.impulse == 30)
            W_FireFlare ();  
      if (self.impulse == 31)  // Activate/Deactivate hologram
      {
        if (self.holo > 0)
        {
                hologram_explode(self.myhologram);
                sprint (self, "Hologram off...\n");
                self.holo = 0;
        }
        else
                W_FireHologram ();
        }
        if (self.impulse == 33)  // Use for right turning of hologram
        {
                if (self.holo > 0)
                        hologram.angles_y = hologram.angles_y - 5;
        }
        if (self.impulse == 34)  // Use for left turning of hologram
        {
                if (self.holo > 0)
                        hologram.angles_y = hologram.angles_y + 5;
        }
        if (self.impulse == 36)
        {
                if (self.holo <=0)
                        sprint (self,"Hologram must be activated first...\n");
                else if (self.camera <= 0)
                {
                        self.camera = 1;
                        CameraOn(self);
                }
                else
                {
                        self.camera = 0;
                        CameraOff(self);  //NezuCamera(self);
                }
        }
        if(self.impulse == 61)
                W_FirePipeBomb();
        if(self.impulse == 62)
                DetPipeBombs();
        if ((self.impulse == 200) || (self.impulse == 201))
        {
                if ( (self.flags & FL_ATTACKED) && (teamplay) )
                {
                        self.classname = "skinchange";
                        T_Damage (self, self, self, 50000);
                        return;
                }else if (self.impulse == 200)
		{
                        self.skin = self.skin + 1;
                        if (self.skin == 18) self.skin = 0;
                } else if (self.impulse == 201)
		{
                        self.skin = self.skin - 1;
                        if (self.skin == -1) self.skin = 17;
		}
                if (self.skin == 0) sprint(self, "SKIN: Quake himself (1)\n"); else
                if (self.skin == 1) sprint(self,"SKIN: BioMan (2)\n"); else
                if (self.skin == 2) sprint(self, "SKIN: Duke Nukem 3d (3)\n"); else
                if (self.skin == 3) sprint(self,"SKIN: NYPD Cop (4)\n"); else
                if (self.skin == 4) sprint(self,"SKIN: Mr. Rage (5)\n"); else
                if (self.skin == 5) sprint(self,"SKIN: MoleMan (6)\n"); else
                if (self.skin == 6) sprint(self,"SKIN: The Evil Eye (7)\n"); else
                if (self.skin == 7) sprint(self,"SKIN: Ninja (8)\n"); else
                if (self.skin == 8) sprint(self, "SKIN: Mr. Toad (9)\n"); else
                if (self.skin == 9) sprint(self,"SKIN: the Predator (10)\n"); else
                if (self.skin == 10) sprint(self, "SKIN: Judge Dredd (11)\n"); else
                if (self.skin == 11) sprint(self, "SKIN: the Terminator (12)\n"); else
                if (self.skin == 12) sprint(self, "SKIN: C-3PO (13)\n"); else
                if (self.skin == 13) sprint(self, "SKIN: the Stormtrooper (14)\n"); else
                if (self.skin == 14) sprint(self,"SKIN: Boba (15)\n"); else
                if (self.skin == 15) sprint(self, "SKIN: Bossk (16)\n"); else
                if (self.skin == 16) sprint(self,"SKIN: Han Solo (17)\n"); else
                if (self.skin == 17) sprint(self,"SKIN: Crusader (18)\n"); 
	}
        if (self.impulse == 255)
                QuadCheat ();
        self.impulse = 0;
};

void() W_WeaponFrame =
{
	if (time < self.attack_finished)
		return;

	ImpulseCommands ();
	
	if (self.button0)
	{
		SuperDamageSound ();
		W_Attack ();
	}
};

void() SuperDamageSound =
{
	if (self.super_damage_finished > time)
	{
		if (self.super_sound < time)
		{
			self.super_sound = time + 1;
			sound (self, CHAN_BODY, "items/damage3.wav", 1, ATTN_NORM);
		}
	}
	return;
};
