/*
*/
void(vector org, vector vel, float damage) SpawnBlood;
void() SuperDamageSound;


// called by worldspawn
void() W_Precache =
{
	precache_sound ("weapons/r_exp3.wav");	// new rocket explosion
	precache_sound ("weapons/rocket1i.wav");	// spike gun
	precache_sound ("weapons/sgun1.wav");
	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_sound ("weapons/shotgn2.wav");	// super shotgun
	precache_sound("player/land2.wav");
	precache_sound("player/fire.wav");
	precache_sound("player/fire2.wav");
	precache_sound("weapons/ching.wav");
	precache_sound("weapons/whoosh.wav");
	precache_sound("weapons/fire.wav");
	precache_sound("weapons/draw.wav");
	precache_sound("weapons/arr2wood.wav");
	precache_sound("weapons/arr2flsh.wav");
	precache_sound("weapons/met2flsh.wav");
	precache_sound("weapons/hitwall.wav");
	precache_sound("weapons/swing.wav");
	precache_sound("misc/none.wav");
};

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

/*
================
W_FireAxe
================
*/
void() W_FireAxe =
{
	local	vector	source;
	local	vector	org;
	local entity oself;
	local float d;

	makevectors (self.v_angle);
	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)
	{
		sound (self, CHAN_WEAPON, "weapons/fisthit.wav", 1, ATTN_NORM);
		oself = self;
		self = trace_ent;
		if(!(visible(oself) && infront(oself)))
			T_Damage (self, oself, oself, (FIST_BACKDAMAGE + random()*FIST_DAMAGE_ADDRANDOM), 0, "backfist", "");
		else 
			T_Damage (self, oself, oself, (FIST_DAMAGE + random()*FIST_DAMAGE_ADDRANDOM), 0, "fist", "");
		self.axhitme = 1;
		SpawnBlood (org, '0 0 0', d);
		T_Damage (self, oself, oself, d, 0, "fist", "");
		self = oself;
	}
	else
	{	// hit wall
		sound (self, CHAN_WEAPON, "weapons/fistwall.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;
};


/*
================
SpawnMeatSpray
================
*/
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';
	
// set missile duration
	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);
};

/*
================
SpawnBlood
================
*/
void(vector org, vector vel, float damage) SpawnBlood =
{
//	if((other.classname == "player") || (other.flags & FL_MONSTER))
		particle (org, vel*0.1, 73, damage*2);
//	else
//		particle (org, vel*0.1, 111, damage*2);
};

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

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


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

/*
==============================================================================

MULTI-DAMAGE

Collects multiple small damages into a single damage

==============================================================================
*/

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

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

/*
==============================================================================

BULLETS

==============================================================================
*/

/*
================
TraceAttack
================
*/
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);
	}
};

/*
================
FireBullets

Used by shotgun, super shotgun, and enemy soldier firing
Go to the trouble of combining multiple pellets into a single damage call.
================
*/
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 ();
};

/*
================
W_FireShotgun
================
*/
void() W_FireShotgun =
{
	local vector dir;

	sound (self, CHAN_WEAPON, "weapons/guncock.wav", 1, ATTN_NORM);	

	self.punchangle_x = -2;
	
	self.currentammo = self.ammo_shells = self.ammo_shells - 1;
	dir = aim (self, 100000);
	FireBullets (6, dir, '0.04 0.04 0');
};


/*
================
W_FireSuperShotgun
================
*/
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');
};


/*
==============================================================================

ROCKETS

==============================================================================
*/

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.velocity = '0 0 0';
	self.touch = SUB_Null;
	setmodel (self, "progs/s_explod.spr");
	self.solid = SOLID_NOT;
	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, 0, "", "");
	}

	// don't do radius damage to the other, because all the damage
	// was done in the impact
	T_RadiusDamage (self, self.owner, 120, other, "", "");

//	sound (self, CHAN_WEAPON, "weapons/r_exp3.wav", 1, ATTN_NORM);
	self.origin = self.origin - 8*normalize(self.velocity);

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



/*
================
W_FireRocket
================
*/
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.classname = "missile";
		
// set missile speed	

	makevectors (self.v_angle);
	missile.velocity = aim(self, 1000);
	missile.velocity = missile.velocity * 1000;
	missile.angles = vectoangles(missile.velocity);
	
	missile.touch = T_MissileTouch;
	
// set missile duration
	missile.nextthink = time + 5;
	missile.think = SUB_Remove;

	setmodel (missile, "progs/missile.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin + v_forward*8 + '0 0 16');
};

/*
===============================================================================

LIGHTNING

===============================================================================
*/

/*
=================
LightningDamage
=================
*/
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, 0, "", "");
		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, 0, "", "");
	}
	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, 0, "", "");
	}
};


void() W_FireLightning =
{
	local	vector		org;
	local	float		cells;

	if (self.ammo_cells < 1)
	{
		self.weapon = W_BestWeapon ();
		W_SetCurrentAmmo ();
		return;
	}

// explode if under water
	if (self.waterlevel > 1)
	{
		cells = self.ammo_cells;
		self.ammo_cells = 0;
		W_SetCurrentAmmo ();
		T_RadiusDamage (self, self, 35*cells, world, "", "");
		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, "", "");

	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() 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';
};

/*
================
W_FireGrenade
================
*/
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";
		
// set missile speed	

	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;
	
// set missile duration
	missile.nextthink = time + 2.5;
	missile.think = GrenadeExplode;

	setmodel (missile, "progs/grenade.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin);
};


//=============================================================================

void() spike_touch;
void() superspike_touch;


/*
===============
launch_spike

Used for both the player and the ogre
===============
*/
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;
};

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



.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;
	}
	
// hit something that bleeds
	if (other.takedamage)
	{
		spawn_touchblood (9);
		T_Damage (other, self, self.owner, 9, 0, "", "");
	}
	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;
	}
	
// hit something that bleeds
	if (other.takedamage)
	{
		spawn_touchblood (18);
		T_Damage (other, self, self.owner, 18, 0, "", "");
	}
	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);

};


/*
===============================================================================

PLAYER WEAPON USE

===============================================================================
*/

void() W_SetCurrentAmmo =
{
  player_run();
  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_fist.mdl";
    self.weaponframe = 0;
  }
  else if (self.weapon == IT_GRAPPLINGHOOK) 
  {
    self.currentammo = 0;
    self.weaponmodel = "";
    self.weaponframe = 0;
  }
  else if (self.weapon == IT_SHOTGUN) 
  {
    self.currentammo = self.ammo_nails;
    self.weaponmodel = "progs/v_star.mdl";
    self.weaponframe = 0;
    self.items = self.items | IT_NAILS;
  }
  else if (self.weapon == IT_SUPER_SHOTGUN) 
  {
    self.currentammo = 0;
    self.weaponmodel = "";
    self.weaponframe = 0;
  }
  else if (self.weapon == IT_NAILGUN) 
  {
    self.currentammo = 0;
    self.weaponmodel = "";
    self.weaponframe = 0;
  }
  else if (self.weapon == IT_SUPER_NAILGUN) 
  {
    self.currentammo = 0;
    self.weaponmodel = "";
    self.weaponframe = 0;
  }
  else if (self.weapon == IT_GRENADE_LAUNCHER) 
  {
    self.currentammo = self.ammo_shells;
    self.weaponmodel = "progs/crossbow.mdl";
    self.weaponframe = 0;
    self.items = self.items | IT_SHELLS;
  }
  else if (self.weapon == IT_ROCKET_LAUNCHER) 
  {
    self.currentammo = 0;
    if (!self.ninja) 
      self.weaponmodel = "progs/v_katana.mdl";
    else 
      self.weaponmodel = "progs/v_katan2.mdl";
    if(self.state & S_SBERSERK)
      self.weaponframe = 11;
    else
      self.weaponframe = 0;
  }
  else if (self.weapon == IT_LIGHTNING) 
  {
    self.currentammo = 0;
    self.weaponframe = 0;
  }
  else 
  {
    self.currentammo = 0;
    self.weaponmodel = "";
    self.weaponframe = 0;
  }
};

float() W_BestWeapon =
{
	local	float	it;
	
	it = self.items;

	if (it & IT_ROCKET_LAUNCHER)
		return IT_ROCKET_LAUNCHER;
	if(self.ammo_shells >= 1 && (it & IT_GRENADE_LAUNCHER))
		return IT_GRENADE_LAUNCHER;
	if(self.ammo_nails >= 1)
		return IT_SHOTGUN;
	return IT_AXE;
};

float() W_CheckNoAmmo =
{
	if (self.currentammo > 0)
		return TRUE;

	if (self.weapon == IT_GRAPPLINGHOOK)
		return TRUE;
	if (self.weapon == IT_AXE)
		return TRUE;
	if (self.weapon == IT_ROCKET_LAUNCHER)
		return TRUE;
	if (self.weapon == IT_LIGHTNING)
		return TRUE;
	
	self.weapon = W_BestWeapon ();

	W_SetCurrentAmmo ();
	
// drop the weapon down
	return FALSE;
};

/*
============
W_Attack

An attack impulse can be triggered now
============
*/
void()	player_axe1;
void()	player_axeb1;
void()	player_axec1;
void()	player_axed1;
void()	player_shot1;
void()	player_rocket1;
void()	player_arrow1;
void()	player_sword1;
void()	player_swordb1;
void()	player_shuriken1;

void() W_Attack =
{
	local	float	r, rt;

	if(self.state & S_REALINVIS)
	{
		if(self.attack_finished < time)
			sprint(self, "You cannot attack while cloaked.\n");
		self.attack_finished = time + 0.5;
		return;
	}

	if (!W_CheckNoAmmo ())
		return;

	makevectors	(self.v_angle);			// calculate forward angle for velocity

	if (!(self.weapon == IT_AXE)) 
	{
		if (self.waterlevel > 2) 
			DeathBubbles(1);
	}
	self.show_hostile = time + 1;

  if (self.weapon == IT_AXE) 
  {
	sound (self, CHAN_WEAPON, "weapons/whoosh.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 ();
	rt = FIST_RELOADTIME;
  }
  if (self.weapon == IT_GRAPPLINGHOOK) 
  {
	W_FireGrapple();
	rt = 0.2;
	if (self.super_damage_finished > time)
		rt = rt * 2;
  }
  else if (self.weapon == IT_SHOTGUN) 
  {
    player_shuriken1();
    W_FireStar();
    rt = SHURIKEN_RELOADTIME+mrand()*0.03;
    if (self.super_damage_finished > time)
      rt = 0.28;
  }
  else if (self.weapon == IT_SUPER_SHOTGUN) 
    return;
  else if (self.weapon == IT_NAILGUN) 
    return;
  else if (self.weapon == IT_SUPER_NAILGUN)
    return;
  else if (self.weapon == IT_GRENADE_LAUNCHER)
  {
    if(self.mode == M_BERSERK)
    {
      W_CbBerserk();
      return;
    }
    else
    {
      W_FireCrossbow(1);
      player_arrow1();
      if (self.ammo_shells < 1)
        rt = 0.5;
      else 
        rt = CROSSBOW_RELOADTIME;
      if (self.waterlevel > 2) 
        sound(self, CHAN_WEAPON, "misc/water2.wav", 1, ATTN_NORM);
      else
        sound(self, CHAN_WEAPON, "weapons/fire.wav", 1, ATTN_NORM);
    }
  }
  else if (self.weapon == IT_ROCKET_LAUNCHER) 
  {
    if(self.mode == M_BERSERK)
    {
      W_SwordBerserk();
      return;
    }
    else
    {
      r = random();
      if (r < 0.5)
        player_sword1();
      else
        player_swordb1();
      rt = KATANA_RELOADTIME;
      if (self.waterlevel > 2)
        sound(self, CHAN_WEAPON, "misc/water2.wav", WEAPON_SHOTGUN, ATTN_NORM);
      else 
        sound(self, CHAN_WEAPON, "weapons/swing.wav", WEAPON_SHOTGUN, ATTN_NORM);
    }
  }
  else if (self.weapon == IT_LIGHTNING)
  {
//    W_FireGrapple();
    rt = 0;
  }
  if (self.super_damage_finished > time)
  {
    self.attack_finished = time + rt * 0.5;
    if(rt < 0.1)
      rt = 0.1;
  }
  else
    self.attack_finished = time + rt;
};

/*
============
W_ChangeWeapon

============
*/
void() W_ChangeWeapon =
{
	local	float	it, am, fl, ow;
	
	it = self.items;
	am = 0;
	ow = self.weapon;
	if (self.impulse == 1)
	{
		if(self.weapon == IT_AXE)
		{
			fl = IT_GRAPPLINGHOOK;
			centerprint(self, "Grappling hook selected");
		}
		else
		{
			fl = IT_AXE;
			centerprint(self, "Fists selected");
		}
	}
	else if (self.impulse == 2)
	{
		fl = IT_SHOTGUN;
		if(self.ammo_nails < 1)
			am = 1;
	}
	else if (self.impulse == 3)
	{
		fl = IT_SUPER_SHOTGUN;
		if (self.ammo_rockets < 1)
			am = 1;
	}		
	else if (self.impulse == 4)
	{
		fl = IT_NAILGUN;
	}
	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_shells < 1)
			am = 1;
	}
	else if (self.impulse == 7)
	{
		fl = IT_ROCKET_LAUNCHER;
	}
	else if (self.impulse == 8)
	{
		fl = IT_LIGHTNING;
	}

	self.impulse = 0;
	
	if (!(self.items & fl))
	{	// don't have the weapon or the ammo
		sprint (self, "no weapon.\n");
		return;
	}

	if((fl == IT_ROCKET_LAUNCHER) && (self.weapon != IT_ROCKET_LAUNCHER) && (self.waterlevel < 3))
          sound(self, CHAN_AUTO, "weapons/ching.wav", 1, ATTN_NORM);

	if (am)
	{	// don't have the ammo
		sprint (self, "not enough ammo.\n");
		return;
	}

//
// set weapon, set ammo
//
	if(fl == IT_SUPER_NAILGUN)
	{
		if(self.attack_finished > time)
			return;
		if(self.state & S_REALINVIS)
		{
			sprint(self, "You cannot throw a powder barrel while camouflaged.\n");
			return;
		}
		W_ThrowBomb();
		if (self.super_damage_finished > time)
			self.attack_finished = time + BOMB_RELOADTIME/2;
		else
			self.attack_finished = time + BOMB_RELOADTIME;
		return;
	}
	if(fl == IT_NAILGUN)
	{
		if(self.attack_finished > time)
			return;
		if(self.state & S_REALINVIS)
		{
			sprint(self, "You cannot set a wire trap while camouflaged.\n");
			return;
		}
		W_FireTrap();
		if (self.super_damage_finished > time)
			self.attack_finished = time + TRAP_RELOADTIME/2;
		else
			self.attack_finished = time + TRAP_RELOADTIME;
		return;
	}
	if(fl == IT_SUPER_SHOTGUN)
	{
		if(self.attack_finished > time)
			return;
		W_Flashbomb();
		if (self.super_damage_finished > time)
			self.attack_finished = time + FLASH_RELOADTIME/2;
		else
			self.attack_finished = time + FLASH_RELOADTIME;
		return;
	}
	if(fl == IT_LIGHTNING)
	{
		if(self.dagger_finished > time)
			return;
		if(self.state & S_REALINVIS)
		{
			sprint(self, "You cannot throw daggers while camouflaged.\n");
			return;
		}
		W_ThrowDaggers();
		if (self.super_damage_finished > time)
			self.dagger_finished = time + DAGGER_RELOADTIME/2;
		else
			self.dagger_finished = time + DAGGER_RELOADTIME;
		return;
	}
	self.weapon = fl;		
	W_SetCurrentAmmo ();
	SetSpeed(self);
};

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

	self.ammo_rockets = 100;
	self.ammo_nails = 200;
	self.ammo_shells = 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 ();
};

/*
============
CycleWeaponCommand

Go to the next weapon with ammo
============
*/
void() CycleWeaponCommand =
{
	local	float	it, am;
	
	if (time < self.attack_finished)
		return;
	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_GRAPPLINGHOOK;
		}
		else if (self.weapon == IT_GRAPPLINGHOOK)
		{
			self.weapon = IT_SHOTGUN;
			if (self.ammo_nails < 1)
				am = 1;
		}
		else if (self.weapon == IT_SHOTGUN)
		{
			self.weapon = IT_SUPER_SHOTGUN;
			if (self.ammo_rockets < 1)
				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_shells < 1)
				am = 1;
		}
		else if (self.weapon == IT_GRENADE_LAUNCHER)
		{
			self.weapon = IT_ROCKET_LAUNCHER;
		}
		else if (self.weapon == IT_ROCKET_LAUNCHER)
		{
			self.weapon = IT_LIGHTNING;
			if (self.ammo_cells < 1)
				am = 1;
		}
	
		if ( (it & self.weapon) && am == 0)
		{
			W_SetCurrentAmmo ();
			return;
		}
	}

};

/*
============
CycleWeaponReverseCommand

Go to the prev weapon with ammo
============
*/
void() CycleWeaponReverseCommand =
{
	local	float	it, am;
	
	if (time < self.attack_finished)
		return;
	it = self.items;
	self.impulse = 0;

	while (1)
	{
		am = 0;

		if (self.weapon == IT_LIGHTNING)
		{
			self.weapon = IT_ROCKET_LAUNCHER;
		}
		else if (self.weapon == IT_ROCKET_LAUNCHER)
		{
			self.weapon = IT_GRENADE_LAUNCHER;
			if (self.ammo_shells < 1)
				am = 1;
		}
		else if (self.weapon == IT_GRENADE_LAUNCHER)
		{
			self.weapon = IT_SUPER_NAILGUN;
			if (self.ammo_nails < 2)
				am = 1;
		}
		else if (self.weapon == IT_SUPER_NAILGUN)
		{
			self.weapon = IT_NAILGUN;
			if (self.ammo_nails < 1)
				am = 1;
		}
		else if (self.weapon == IT_NAILGUN)
		{
			self.weapon = IT_SUPER_SHOTGUN;
			if (self.ammo_rockets < 2)
				am = 1;
		}		
		else if (self.weapon == IT_SUPER_SHOTGUN)
		{
			self.weapon = IT_SHOTGUN;
			if (self.ammo_nails < 1)
				am = 1;
		}
		else if (self.weapon == IT_SHOTGUN)
		{
			self.weapon = IT_AXE;
		}
		else if (self.weapon == IT_AXE)
		{
			self.weapon = IT_GRAPPLINGHOOK;
		}
		else if (self.weapon == IT_GRAPPLINGHOOK)
		{
			self.weapon = IT_LIGHTNING;
			if (self.ammo_cells < 1)
				am = 1;
		}
	
		if ( (it & self.weapon) && am == 0)
		{
			W_SetCurrentAmmo ();
			return;
		}
	}

};

/*
============
ServerflagsCommand

Just for development
============
*/
void() ServerflagsCommand =
{
	serverflags = serverflags * 2 + 1;
};

void() QuadCheat =
{
	if (deathmatch)
		return;
	self.super_time = 1;
	self.super_damage_finished = time + 30;
	self.items = self.items | IT_QUAD;
	dprint ("quad cheat\n");
};

/*
============
ImpulseCommands

============
*/
void() ImpulseCommands =
{
	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 == 12)
		CycleWeaponReverseCommand ();

	if(self.impulse == 100)
		ThrowKatana ();

	if (self.impulse == 101)
	{
		self.grapple0 = TRUE;
		W_FireGrapple();
//		TieUp ();
	}

	if (self.impulse == 102)
		Ambush ();

	if(self.impulse == 103)
		self.grapple0 = FALSE;

	if (self.impulse == 255)
		QuadCheat ();
		
	self.impulse = 0;
};

/*
============
W_WeaponFrame

Called every frame so impulse events can be handled as well as possible
============
*/
void() W_WeaponFrame =
{
	if((self.button0) && (!self.button0held))
		self.button0held = time;
	else
		self.button0held = 0;

	if((self.weapon == IT_SHOTGUN) && (self.button0) && (!self.randomf))
		self.randomf = time;
	else if((self.weapon == IT_SHOTGUN) && (!self.button0) && (self.randomf))
		self.randomf = 0;

	if(self.impulse)
		ImpulseCommands ();

	if (time < self.attack_finished)
		return;
	
// check for attack
	if (self.button0)
	{
		SuperDamageSound ();
		W_Attack ();
	}
};

/*
========
SuperDamageSound

Plays sound if needed
========
*/
void() SuperDamageSound =
{
	return;
};


