void() bubble_bob;

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

PLAYER

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

$cd /raid/quake/id1/models/player_4
$origin 0 -6 24
$base base		
$skin skin

//
// running
//
$frame axrun1 axrun2 axrun3 axrun4 axrun5 axrun6

$frame rockrun1 rockrun2 rockrun3 rockrun4 rockrun5 rockrun6

//
// standing
//
$frame stand1 stand2 stand3 stand4 stand5

$frame axstnd1 axstnd2 axstnd3 axstnd4 axstnd5 axstnd6
$frame axstnd7 axstnd8 axstnd9 axstnd10 axstnd11 axstnd12


//
// pain
//
$frame axpain1 axpain2 axpain3 axpain4 axpain5 axpain6

$frame pain1 pain2 pain3 pain4 pain5 pain6


//
// death
//

$frame axdeth1 axdeth2 axdeth3 axdeth4 axdeth5 axdeth6
$frame axdeth7 axdeth8 axdeth9

$frame deatha1 deatha2 deatha3 deatha4 deatha5 deatha6 deatha7 deatha8
$frame deatha9 deatha10 deatha11

$frame deathb1 deathb2 deathb3 deathb4 deathb5 deathb6 deathb7 deathb8
$frame deathb9

$frame deathc1 deathc2 deathc3 deathc4 deathc5 deathc6 deathc7 deathc8
$frame deathc9 deathc10 deathc11 deathc12 deathc13 deathc14 deathc15

$frame deathd1 deathd2 deathd3 deathd4 deathd5 deathd6 deathd7
$frame deathd8 deathd9

$frame deathe1 deathe2 deathe3 deathe4 deathe5 deathe6 deathe7
$frame deathe8 deathe9

//
// attacks
//
$frame nailatt1 nailatt2

$frame light1 light2

$frame rockatt1 rockatt2 rockatt3 rockatt4 rockatt5 rockatt6

$frame shotatt1 shotatt2 shotatt3 shotatt4 shotatt5 shotatt6

$frame axatt1 axatt2 axatt3 axatt4 axatt5 axatt6

$frame axattb1 axattb2 axattb3 axattb4 axattb5 axattb6

$frame axattc1 axattc2 axattc3 axattc4 axattc5 axattc6

$frame axattd1 axattd2 axattd3 axattd4 axattd5 axattd6


/*
==============================================================================
PLAYER
==============================================================================
*/

// BYOM -- Fiend attack functions -- thanks to John & Josh Spickes
void() player_demon_jump =[     $axstnd11,  player_demon_jump  ]
{
        if (self.walkframe >= 12)
                self.walkframe = 11;
        self.frame = $axstnd11 + self.walkframe;
        self.walkframe = self.walkframe + 1;
        if (self.flags & FL_ONGROUND)
                player_stand1();
};

void()  player_demon_melee = [$deatha5, player_dattack2 ] {self.view_ofs = '0 0 8';};
void()  player_dattack2 = [$deatha6, player_dattack3 ] {self.view_ofs = '0 0 16';};
void()  player_dattack3 = [$deatha7, player_dattack4 ] {self.view_ofs = '0 0 25';};
void()  player_dattack4 = [$deatha8, player_dattack5 ] {self.view_ofs = '0 0 23';};
void()  player_dattack5 = [$deatha9, player_dattack6 ] {self.view_ofs = '0 0 21'; Player_Demon_Attack(200);};
void()  player_dattack6 = [$deatha10, player_dattack7 ] {self.view_ofs = '0 0 23';};
void()  player_dattack7 = [$deatha11, player_dattack8 ] {self.view_ofs = '0 0 24';};
void()  player_dattack8 = [$deathb1, player_dattack9 ] {self.view_ofs = '0 0 25';};              
void()  player_dattack9 = [$deathb2, player_dattack10 ] {self.view_ofs = '0 0 24';};
void()  player_dattack10 = [$deathb3, player_dattack11 ] {self.view_ofs = '0 0 23';};
void()  player_dattack11 = [$deathb4, player_dattack12 ] {self.view_ofs = '0 0 21'; Player_Demon_Attack(-200);};
void()  player_dattack12 = [$deathb5, player_dattack13 ] {self.view_ofs = '0 0 16';};
void()  player_dattack13 = [$deathb6, player_dattack14 ] {self.view_ofs = '0 0 12';};
void()  player_dattack14 = [$deathb7, player_dattack15 ] {self.view_ofs = '0 0 8';};
void()  player_dattack15 = [$deathb8, player_stand1 ] {self.view_ofs = '0 0 4';};

void(float side) Player_Demon_Attack =
{
	local	vector	source;
	local	vector	org;
    local   float   ldmg;

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

    source = self.origin + '0 0 4';
    makevectors (self.angles);
	traceline (source, source + v_forward*64, FALSE, self);
    if (!trace_ent.takedamage)
        traceline (source, source + v_forward*64 + v_right*30, FALSE, self);
    if (!trace_ent.takedamage)
        traceline (source, source + v_forward*64 - v_right*30, FALSE, self);

    if (trace_fraction == 1.0)
        return;
	
	org = trace_endpos - v_forward*4;
    if (trace_ent.takedamage) {
        sound (self, CHAN_WEAPON, "demon/dhit2.wav", 1, ATTN_NORM);
        ldmg = 40 + 10*random();

        SpawnMeatSpray (self.origin + v_forward*16, side * v_right);
        T_Damage (trace_ent, self, self, ldmg); 
    }
};

void() Player_Demon_JumpTouch =
{
        local float ldmg;

        if(other.takedamage)
        {
                ldmg = 40 + 10*random();
                T_Damage(other, self, self, ldmg);
                SpawnMeatSpray (self.origin + v_forward*20,  100* v_forward);
                SpawnMeatSpray (self.origin + v_forward*20,  50* v_forward);
        }

        self.touch = SUB_Null;
};

// BYOM Shambler melee attacks!

void() player_shambler_smash1     =[      $pain1,       player_shambler_smash2    ] {
sound (self, CHAN_VOICE, "shambler/melee1.wav", 1, ATTN_NORM);};
void() player_shambler_smash2     =[      $pain2,       player_shambler_smash3    ] {};
void() player_shambler_smash3     =[      $pain3,       player_shambler_smash4    ] {};
void() player_shambler_smash4     =[      $pain4,       player_shambler_smash5    ] {};
void() player_shambler_smash5     =[      $pain5,       player_shambler_smash6    ] {};
void() player_shambler_smash6     =[      $pain6,       player_shambler_smash7    ] {};
void() player_shambler_smash7     =[      $axdeth1,       player_shambler_smash8    ] {};
void() player_shambler_smash8     =[      $axdeth2,       player_shambler_smash9    ] {};
void() player_shambler_smash9     =[      $axdeth3,       player_shambler_smash10   ] {};
void() player_shambler_smash10    =[      $axdeth4,      player_shambler_smash11   ] {
	local	vector	source;
	local	vector	org;
    local   float   ldmg;

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

    source = self.origin + '0 0 4';
    makevectors (self.angles);
	traceline (source, source + v_forward*64, FALSE, self);
    if (!trace_ent.takedamage)
        traceline (source, source + v_forward*64 + v_right*30, FALSE, self);
    if (!trace_ent.takedamage)
        traceline (source, source + v_forward*64 - v_right*30, FALSE, self);

    if (trace_fraction == 1.0)
        return;
	
	org = trace_endpos - v_forward*4;
    if (trace_ent.takedamage) {
        sound (self, CHAN_VOICE, "shambler/smack.wav", 1, ATTN_NORM);
        ldmg = (random() + random() + random()) * 40;

        SpawnMeatSpray (self.origin + v_forward*16, crandom() * 100 * v_right);
        SpawnMeatSpray (self.origin + v_forward*16, crandom() * 100 * v_right);
        T_Damage (trace_ent, self, self, ldmg); 
    }
};
void() player_shambler_smash11    =[      $axdeth5,      player_shambler_smash12   ] {};
void() player_shambler_smash12    =[      $axdeth6,      player_run    ] {};

void() player_shambler_swingr1;

void(float side) player_shambler_claw =
{
	local	vector	source;
	local	vector	org;
    local   float   ldmg;

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

    source = self.origin + '0 0 4';
    makevectors (self.angles);
	traceline (source, source + v_forward*64, FALSE, self);
    if (!trace_ent.takedamage)
        traceline (source, source + v_forward*64 + v_right*30, FALSE, self);
    if (!trace_ent.takedamage)
        traceline (source, source + v_forward*64 - v_right*30, FALSE, self);

    if (trace_fraction == 1.0)
        return;
	
	org = trace_endpos - v_forward*4;
    if (trace_ent.takedamage) {
        sound (self, CHAN_VOICE, "shambler/smack.wav", 1, ATTN_NORM);
        ldmg = (random() + random() + random()) * 20;

        SpawnMeatSpray (self.origin + v_forward*16, crandom() * 100 * v_right);
        SpawnMeatSpray (self.origin + v_forward*16, crandom() * 100 * v_right);
        T_Damage (trace_ent, self, self, ldmg); 
    }
};

void() player_shambler_swingl1 =[      $deatha7,      player_shambler_swingl2   ] {
sound (self, CHAN_VOICE, "shambler/melee2.wav", 1, ATTN_NORM);};
void() player_shambler_swingl2 =[      $deatha8,      player_shambler_swingl3   ] {};
void() player_shambler_swingl3 =[      $deatha9,      player_shambler_swingl4   ] {};
void() player_shambler_swingl4 =[      $deatha10,      player_shambler_swingl5   ] {};
void() player_shambler_swingl5 =[      $deatha11,      player_shambler_swingl6   ] {};
void() player_shambler_swingl6 =[      $deathb1,      player_shambler_swingl7   ] {};
void() player_shambler_swingl7 =[      $deathb2,      player_shambler_swingl8   ] {player_shambler_claw(250);};
void() player_shambler_swingl8 =[      $deathb3,      player_shambler_swingl9   ] {};
void() player_shambler_swingl9 =[      $deathb4,      player_run     ] {};

void() player_shambler_swingr1 =[      $axdeth7,      player_shambler_swingr2   ] {
sound (self, CHAN_VOICE, "shambler/melee1.wav", 1, ATTN_NORM);};
void() player_shambler_swingr2 =[      $axdeth8,      player_shambler_swingr3   ] {};
void() player_shambler_swingr3 =[      $axdeth9,      player_shambler_swingr4   ] {};
void() player_shambler_swingr4 =[      $deatha1,      player_shambler_swingr5   ] {};
void() player_shambler_swingr5 =[      $deatha2,      player_shambler_swingr6   ] {};
void() player_shambler_swingr6 =[      $deatha3,      player_shambler_swingr7   ] {};
void() player_shambler_swingr7 =[      $deatha4,      player_shambler_swingr8   ] {player_shambler_claw(-250);};
void() player_shambler_swingr8 =[      $deatha5,      player_shambler_swingr9   ] {};
void() player_shambler_swingr9 =[      $deatha6,      player_run     ] {};

void() player_shambler_melee =
{
	local float chance;
	
	chance = random();
	if (chance > 0.6 || self.health == 600)
        player_shambler_smash1 ();
	else if (chance > 0.3)
        player_shambler_swingr1 ();
	else
        player_shambler_swingl1 ();
};

void() player_shambler_lightning =
{
	local	vector	org, dir;
	
	self.effects = self.effects | EF_MUZZLEFLASH;
    if (self.ammo_cells < 40) return;
    self.ammo_cells = self.ammo_cells - 40;

	if (self.waterlevel > 1)
	{
		T_RadiusDamage (self, self, 35*self.ammo_cells, world);
		self.ammo_cells = 0;
		return;
	}

	org = self.origin + '0 0 40';
    traceline (org, self.origin + v_forward*600, TRUE, self);

	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
	WriteByte (MSG_BROADCAST, TE_LIGHTNING1);
	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 (org, trace_endpos, self, 10);
};

void() player_shambler_bolt =[      $deathb5,       player_shambler_bolt2    ] {
	sound (self, CHAN_WEAPON, "shambler/sattck1.wav", 1, ATTN_NORM);
};
void() player_shambler_bolt2     =[      $deathb6,       player_shambler_bolt3    ] {};
void() player_shambler_bolt3     =[      $deathb7,       player_shambler_bolt4    ] {
    local entity o;

    self.effects = self.effects | EF_MUZZLEFLASH;
    self.owner = spawn();
    o = self.owner;
    setmodel (o, "progs/s_light.mdl");
    setorigin (o, self.origin);
    o.angles = self.angles;
    o.nextthink = time + 0.7;
    o.think = SUB_Remove;
};
void() player_shambler_bolt4     =[      $deathb8,       player_shambler_bolt5    ]
{
self.effects = self.effects | EF_MUZZLEFLASH;
self.owner.frame = 1;
};
void() player_shambler_bolt5     =[      $deathb9,       player_shambler_bolt6    ]
{
self.effects = self.effects | EF_MUZZLEFLASH;
self.owner.frame = 2;
};
void() player_shambler_bolt6     =[      $deathc1,       player_shambler_bolt9    ]
{
remove (self.owner);
player_shambler_lightning();
sound (self, CHAN_WEAPON, "shambler/sboom.wav", 1, ATTN_NORM);
};
void() player_shambler_bolt9     =[      $deathc2,       player_shambler_bolt10   ] {player_shambler_lightning();};
void() player_shambler_bolt10    =[      $deathc3,      player_shambler_bolt11   ] {player_shambler_lightning();};
void() player_shambler_bolt11    =[      $deathc4,      player_shambler_bolt12   ] {player_shambler_lightning();};
void() player_shambler_bolt12    =[      $deathc5,      player_run   ] {};

// BYOM Wizard attack
void() player_wizard_fastfire =
{
    local vector        dir;
	local vector		dst;

	if (self.owner.health > 0)
	{
		self.owner.effects = self.owner.effects | EF_MUZZLEFLASH;
		sound (self, CHAN_WEAPON, "wizard/wattack.wav", 1, ATTN_NORM);
        dir = aim (self, 1000);
        launch_spike (self.origin, dir);
        newmis.velocity = dir*600;
		newmis.owner = self.owner;
		newmis.classname = "wizspike";
		setmodel (newmis, "progs/w_spike.mdl");
        setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);
	}
	remove (self);
};

void() player_wizard_startfast =
{
	local	entity	missile;
	
	sound (self, CHAN_WEAPON, "wizard/wattack.wav", 1, ATTN_NORM);
	self.v_angle = self.angles;
	makevectors (self.angles);

    if (self.ammo_cells < 10) return;
    self.ammo_cells = self.ammo_cells - 10;

	missile = spawn ();
	missile.owner = self;
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin + '0 0 30' + v_forward*14 + v_right*14);
	missile.nextthink = time + 0.8;
    missile.think = player_wizard_fastfire;
	missile.movedir = v_right;

	missile = spawn ();
	missile.owner = self;
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin + '0 0 30' + v_forward*14 + v_right* -14);
    missile.nextthink = time + 1;
    missile.think = player_wizard_fastfire;
	missile.movedir = VEC_ORIGIN - v_right;

};

void()  player_wizard_shot =[  $axpain1,       player_wizard_shot2   ] {player_wizard_startfast();};
void()  player_wizard_shot2   =[  $axpain2,       player_wizard_shot3   ] {};
void()  player_wizard_shot3   =[  $axpain3,       player_wizard_shot4   ] {};
void()  player_wizard_shot4   =[  $axpain4,       player_wizard_shot5   ] {};
void()  player_wizard_shot5   =[  $axpain5,       player_wizard_shot6   ] {};
void()  player_wizard_shot6   =[  $axpain6,       player_wizard_shot7   ] {};
void()  player_wizard_shot7   =[  $pain1,       player_wizard_shot8   ] {};
void()  player_wizard_shot8   =[  $pain2,       player_wizard_shot9   ] {};
void()  player_wizard_shot9   =[  $pain3,       player_wizard_shot10  ] {};
void()  player_wizard_shot10  =[  $pain4,       player_run  ] {};

void() Wiz_idlesound;
/*void() Wiz_idlesound =
{
    local float wr;
	wr = random() * 5;

	if (self.waitmin < time)
	{
	 	self.waitmin = time + 2;
	 	if (wr > 4.5) 
	 		sound (self, CHAN_VOICE, "wizard/widle1.wav", 1,  ATTN_IDLE);
	 	if (wr < 1.5)
	 		sound (self, CHAN_VOICE, "wizard/widle2.wav", 1, ATTN_IDLE);
	}
	return;
};*/



void() player_run;

void()	player_stand1 =[	$axstnd1,	player_stand1	]
{
	self.weaponframe=0;
	if (self.velocity_x || self.velocity_y)
	{
		self.walkframe=0;
		player_run();
		return;
	}

    // BYOM -- changed for other classes
    if (self.class == CL_FIEND) {
        self.view_ofs = '0 0 4';
        if (self.walkframe >= 13)
            self.walkframe = 0;
        self.frame = $axrun1 + self.walkframe;
    }
    else if (self.class == CL_SHAMBLER) {
        if (self.walkframe >= 17)
            self.walkframe = 0;
        self.frame = $axrun1 + self.walkframe;
    }
    else if (self.class == CL_WIZARD) {
        if (self.walkframe >= 15)
            self.walkframe = 0;
        self.frame = $axrun1 + self.walkframe;
        Wiz_idlesound();
    }
    else if (self.weapon == IT_AXE)
	{
		if (self.walkframe >= 12)
			self.walkframe = 0;
		self.frame = $axstnd1 + self.walkframe;
	}
	else
	{
		if (self.walkframe >= 5)
			self.walkframe = 0;
		self.frame = $stand1 + self.walkframe;
	}
	self.walkframe = self.walkframe + 1;	
};

void()	player_run =[	$rockrun1,	player_run	]
{
	self.weaponframe=0;
	if (!self.velocity_x && !self.velocity_y)
	{
		self.walkframe=0;
		player_stand1();
		return;
	}

    // BYOM -- changed for other classes
    if (self.class == CL_FIEND) {
        self.view_ofs = '0 0 4';
        if (self.walkframe >= 8)
            self.walkframe = 0;
        self.frame = $stand2 + self.walkframe;
    }
    else if (self.class == CL_SHAMBLER) {
        if (self.walkframe >= 6)
            self.walkframe = 0;
        self.frame = $axpain1 + self.walkframe;
    }
    else if (self.class == CL_WIZARD) {
        if (self.walkframe >= 14)
            self.walkframe = 0;
        self.frame = $stand4 + self.walkframe;
    }
    else if (self.weapon == IT_AXE)
	{
		if (self.walkframe == 6)
			self.walkframe = 0;
		self.frame = $axrun1 + self.walkframe;
	}
	else
	{
		if (self.walkframe == 6)
			self.walkframe = 0;
		self.frame = self.frame + self.walkframe;
	}
	self.walkframe = self.walkframe + 1;
};


void()	player_shot1 =	[$shotatt1, player_shot2	] {self.weaponframe=1;
self.effects = self.effects | EF_MUZZLEFLASH;};
void()	player_shot2 =	[$shotatt2, player_shot3	] {self.weaponframe=2;};
void()	player_shot3 =	[$shotatt3, player_shot4	] {self.weaponframe=3;};
void()	player_shot4 =	[$shotatt4, player_shot5	] {self.weaponframe=4;};
void()	player_shot5 =	[$shotatt5, player_shot6	] {self.weaponframe=5;};
void()	player_shot6 =	[$shotatt6, player_run	] {self.weaponframe=6;};

void()	player_axe1 =	[$axatt1, player_axe2	] {self.weaponframe=1;};
void()	player_axe2 =	[$axatt2, player_axe3	] {self.weaponframe=2;};
void()	player_axe3 =	[$axatt3, player_axe4	] {self.weaponframe=3;W_FireAxe();};
void()	player_axe4 =	[$axatt4, player_run	] {self.weaponframe=4;};

void()	player_axeb1 =	[$axattb1, player_axeb2	] {self.weaponframe=5;};
void()	player_axeb2 =	[$axattb2, player_axeb3	] {self.weaponframe=6;};
void()	player_axeb3 =	[$axattb3, player_axeb4	] {self.weaponframe=7;W_FireAxe();};
void()	player_axeb4 =	[$axattb4, player_run	] {self.weaponframe=8;};

void()	player_axec1 =	[$axattc1, player_axec2	] {self.weaponframe=1;};
void()	player_axec2 =	[$axattc2, player_axec3	] {self.weaponframe=2;};
void()	player_axec3 =	[$axattc3, player_axec4	] {self.weaponframe=3;W_FireAxe();};
void()	player_axec4 =	[$axattc4, player_run	] {self.weaponframe=4;};

void()	player_axed1 =	[$axattd1, player_axed2	] {self.weaponframe=5;};
void()	player_axed2 =	[$axattd2, player_axed3	] {self.weaponframe=6;};
void()	player_axed3 =	[$axattd3, player_axed4	] {self.weaponframe=7;W_FireAxe();};
void()	player_axed4 =	[$axattd4, player_run	] {self.weaponframe=8;};


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

void() player_nail1   =[$nailatt1, player_nail2  ] 
{
	self.effects = self.effects | EF_MUZZLEFLASH;

	if (!self.button0)
		{player_run ();return;}
	self.weaponframe = self.weaponframe + 1;
	if (self.weaponframe == 9)
		self.weaponframe = 1;
	SuperDamageSound();
	W_FireSpikes (4);
	self.attack_finished = time + 0.2;
};
void() player_nail2   =[$nailatt2, player_nail1  ]
{
	self.effects = self.effects | EF_MUZZLEFLASH;

	if (!self.button0)
		{player_run ();return;}
	self.weaponframe = self.weaponframe + 1;
	if (self.weaponframe == 9)
		self.weaponframe = 1;
	SuperDamageSound();
	W_FireSpikes (-4);
	self.attack_finished = time + 0.2;
};

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

void() player_light1   =[$light1, player_light2  ] 
{
	self.effects = self.effects | EF_MUZZLEFLASH;

	if (!self.button0)
		{player_run ();return;}
	self.weaponframe = self.weaponframe + 1;
	if (self.weaponframe == 5)
		self.weaponframe = 1;
	SuperDamageSound();
	W_FireLightning();
	self.attack_finished = time + 0.2;
};
void() player_light2   =[$light2, player_light1  ]
{
	self.effects = self.effects | EF_MUZZLEFLASH;

	if (!self.button0)
		{player_run ();return;}
	self.weaponframe = self.weaponframe + 1;
	if (self.weaponframe == 5)
		self.weaponframe = 1;
	SuperDamageSound();
	W_FireLightning();
	self.attack_finished = time + 0.2;
};

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


void() player_rocket1   =[$rockatt1, player_rocket2  ] {self.weaponframe=1;
self.effects = self.effects | EF_MUZZLEFLASH;};
void() player_rocket2   =[$rockatt2, player_rocket3  ] {self.weaponframe=2;};
void() player_rocket3   =[$rockatt3, player_rocket4  ] {self.weaponframe=3;};
void() player_rocket4   =[$rockatt4, player_rocket5  ] {self.weaponframe=4;};
void() player_rocket5   =[$rockatt5, player_rocket6  ] {self.weaponframe=5;};
void() player_rocket6   =[$rockatt6, player_run  ] {self.weaponframe=6;};
void(float num_bubbles) DeathBubbles;

void() PainSound =
{
local float		rs;

	if (self.health < 0)
		return;

	if (damage_attacker.classname == "teledeath")
	{
		sound (self, CHAN_VOICE, "player/teledth1.wav", 1, ATTN_NONE);
		return;
	}

// water pain sounds
	if (self.watertype == CONTENT_WATER && self.waterlevel == 3)
	{
		DeathBubbles(1);
		if (random() > 0.5)
			sound (self, CHAN_VOICE, "player/drown1.wav", 1, ATTN_NORM);
		else
			sound (self, CHAN_VOICE, "player/drown2.wav", 1, ATTN_NORM);
		return;
	}

// slime pain sounds
	if (self.watertype == CONTENT_SLIME)
	{
// FIX ME	put in some steam here
		if (random() > 0.5)
			sound (self, CHAN_VOICE, "player/lburn1.wav", 1, ATTN_NORM);
		else
			sound (self, CHAN_VOICE, "player/lburn2.wav", 1, ATTN_NORM);
		return;
	}

	if (self.watertype == CONTENT_LAVA)
	{
		if (random() > 0.5)
			sound (self, CHAN_VOICE, "player/lburn1.wav", 1, ATTN_NORM);
		else
			sound (self, CHAN_VOICE, "player/lburn2.wav", 1, ATTN_NORM);
		return;
	}

	if (self.pain_finished > time)
	{
		self.axhitme = 0;
		return;
	}
	self.pain_finished = time + 0.5;

// don't make multiple pain sounds right after each other

// ax pain sound
	if (self.axhitme == 1)
	{
		self.axhitme = 0;
		sound (self, CHAN_VOICE, "player/axhit1.wav", 1, ATTN_NORM);
		return;
	}

    // BYOM -- add appropiate pain sounds
    if (self.class == CL_SHAMBLER) {
        sound(self, CHAN_VOICE, "shambler/shurt2.wav", 1, ATTN_NORM);
    }
    else if (self.class == CL_WIZARD) {
        sound(self, CHAN_VOICE, "wizard/wpain.wav", 1, ATTN_NORM);
    }
    else if (self.class == CL_FIEND) {
        sound(self, CHAN_VOICE, "demon/dpain1.wav", 1, ATTN_NORM);
    }
    else {
        rs = rint((random() * 5) + 1);

        self.noise = "";
        if (rs == 1)
            self.noise = "player/pain1.wav";
        else if (rs == 2)
            self.noise = "player/pain2.wav";
        else if (rs == 3)
            self.noise = "player/pain3.wav";
        else if (rs == 4)
            self.noise = "player/pain4.wav";
        else if (rs == 5)
            self.noise = "player/pain5.wav";
        else
            self.noise = "player/pain6.wav";

        sound (self, CHAN_VOICE, self.noise, 1, ATTN_NORM);
        return;
    }
};

void()	player_pain1 =	[	$pain1,	player_pain2	] {PainSound();self.weaponframe=0;};
void()	player_pain2 =	[	$pain2,	player_pain3	] {};
void()	player_pain3 =	[	$pain3,	player_pain4	] {};
void()	player_pain4 =	[	$pain4,	player_pain5	] {};
void()	player_pain5 =	[	$pain5,	player_pain6	] {};
void()	player_pain6 =	[	$pain6,	player_run	] {};

// BYOM -- pain for other classes function
// Shambler
void() player_shambler_pain = [$deathc8, player_shambler_pain2] {PainSound();};
void() player_shambler_pain2 = [$deathc9, player_shambler_pain3] {};
void() player_shambler_pain3 = [$deathc10, player_shambler_pain4] {};
void() player_shambler_pain4 = [$deathc11, player_shambler_pain5] {};
void() player_shambler_pain5 = [$deathc12, player_shambler_pain6] {};
void() player_shambler_pain6 = [$deathc13, player_run] {};
// Wizard
void() player_wizard_pain = [$axdeth3, player_wizard_pain2] {PainSound();};
void() player_wizard_pain2 = [$axdeth4, player_wizard_pain3] {};
void() player_wizard_pain3 = [$axdeth5, player_wizard_pain4] {};
void() player_wizard_pain4 = [$axdeth6, player_run] {};
// Fiend
void() player_demon_pain = [$pain5, player_demon_pain2] {PainSound();};
void() player_demon_pain2 = [$pain6, player_demon_pain3] {};
void() player_demon_pain3 = [$axdeth1, player_demon_pain4] {};
void() player_demon_pain4 = [$axdeth2, player_demon_pain5] {};
void() player_demon_pain5 = [$axdeth3, player_demon_pain6] {};
void() player_demon_pain6 = [$axdeth4, player_run] {};

void()	player_axpain1 =	[	$axpain1,	player_axpain2	] {PainSound();self.weaponframe=0;};
void()	player_axpain2 =	[	$axpain2,	player_axpain3	] {};
void()	player_axpain3 =	[	$axpain3,	player_axpain4	] {};
void()	player_axpain4 =	[	$axpain4,	player_axpain5	] {};
void()	player_axpain5 =	[	$axpain5,	player_axpain6	] {};
void()	player_axpain6 =	[	$axpain6,	player_run	] {};

void() player_pain =
{
	if (self.weaponframe)
		return;

	if (self.invisible_finished > time)
		return;		// eyes don't have pain frames

	if (self.weapon == IT_AXE)
		player_axpain1 ();
	else
		player_pain1 ();
};

void() player_diea1;
void() player_dieb1;
void() player_diec1;
void() player_died1;
void() player_diee1;
void() player_die_ax1;

void() DeathBubblesSpawn =
{
local entity	bubble;
	if (self.owner.waterlevel != 3)
		return;
	bubble = spawn();
	setmodel (bubble, "progs/s_bubble.spr");
	setorigin (bubble, self.owner.origin + '0 0 24');
	bubble.movetype = MOVETYPE_NOCLIP;
	bubble.solid = SOLID_NOT;
	bubble.velocity = '0 0 15';
	bubble.nextthink = time + 0.5;
	bubble.think = bubble_bob;
	bubble.classname = "bubble";
	bubble.frame = 0;
	bubble.cnt = 0;
	setsize (bubble, '-8 -8 -8', '8 8 8');
	self.nextthink = time + 0.1;
	self.think = DeathBubblesSpawn;
	self.air_finished = self.air_finished + 1;
	if (self.air_finished >= self.bubble_count)
		remove(self);
};

void(float num_bubbles) DeathBubbles =
{
local entity	bubble_spawner;
	
	bubble_spawner = spawn();
	setorigin (bubble_spawner, self.origin);
	bubble_spawner.movetype = MOVETYPE_NONE;
	bubble_spawner.solid = SOLID_NOT;
	bubble_spawner.nextthink = time + 0.1;
	bubble_spawner.think = DeathBubblesSpawn;
	bubble_spawner.air_finished = 0;
	bubble_spawner.owner = self;
	bubble_spawner.bubble_count = num_bubbles;
	return;
};


void() DeathSound =
{
local float		rs;

	// water death sounds
	if (self.waterlevel == 3)
	{
		DeathBubbles(20);
		sound (self, CHAN_VOICE, "player/h2odeath.wav", 1, ATTN_NONE);
		return;
	}

    // BYOM -- change to appropiate death sound for each class
    //             default to normal death sequence
    if (self.class == CL_SHAMBLER)
        sound(self, CHAN_VOICE, "shambler/sdeath.wav", 1, ATTN_NONE);
    else if (self.class == CL_WIZARD)
        sound(self, CHAN_VOICE, "wizard/wdeath.wav", 1, ATTN_NONE);
    else if (self.class == CL_FIEND)
        sound(self, CHAN_VOICE, "demon/ddeath.wav", 1, ATTN_NONE);
    else {
        rs = rint ((random() * 4) + 1);
        if (rs == 1)
            self.noise = "player/death1.wav";
        if (rs == 2)
            self.noise = "player/death2.wav";
        if (rs == 3)
            self.noise = "player/death3.wav";
        if (rs == 4)
            self.noise = "player/death4.wav";
        if (rs == 5)
            self.noise = "player/death5.wav";

        sound (self, CHAN_VOICE, self.noise, 1, ATTN_NONE);
    }
	return;
};


void() PlayerDead =
{
	self.nextthink = -1;
// allow respawn after a certain time
	self.deadflag = DEAD_DEAD;
};

vector(float dm) VelocityForDamage =
{
	local vector v;

	v_x = 100 * crandom();
	v_y = 100 * crandom();
	v_z = 200 + 100 * random();

	if (dm > -50)
	{
//		dprint ("level 1\n");
		v = v * 0.7;
	}
	else if (dm > -200)
	{
//		dprint ("level 3\n");
		v = v * 2;
	}
	else
		v = v * 10;

	return v;
};

void(string gibname, float dm) ThrowGib =
{
	local	entity new;

	new = spawn();
	new.origin = self.origin;
	setmodel (new, gibname);
	setsize (new, '0 0 0', '0 0 0');
	new.velocity = VelocityForDamage (dm);
	new.movetype = MOVETYPE_BOUNCE;
	new.solid = SOLID_NOT;
	new.avelocity_x = random()*600;
	new.avelocity_y = random()*600;
	new.avelocity_z = random()*600;
	new.think = SUB_Remove;
	new.ltime = time;
	new.nextthink = time + 10 + random()*10;
	new.frame = 0;
	new.flags = 0;
};

void(string gibname, float dm) ThrowHead =
{
	setmodel (self, gibname);
	self.frame = 0;
	self.nextthink = -1;
	self.movetype = MOVETYPE_BOUNCE;
	self.takedamage = DAMAGE_NO;
	self.solid = SOLID_NOT;
	self.view_ofs = '0 0 8';
	setsize (self, '-16 -16 0', '16 16 56');
	self.velocity = VelocityForDamage (dm);
	self.origin_z = self.origin_z - 24;
	self.flags = self.flags - (self.flags & FL_ONGROUND);
	self.avelocity = crandom() * '0 600 0';
};


void() GibPlayer =
{
    // BYOM -- Throw appropiate head and extra gibs
    if (self.class == CL_FIEND)
        ThrowHead ("progs/h_demon.mdl", self.health);
    else if (self.class == CL_SHAMBLER)
        ThrowHead ("progs/h_shams.mdl", self.health);
    else if (self.class == CL_WIZARD)
        ThrowHead ("progs/h_wizard.mdl", self.health);
    else
        ThrowHead ("progs/h_player.mdl", self.health);
	ThrowGib ("progs/gib1.mdl", self.health);
	ThrowGib ("progs/gib2.mdl", self.health);
	ThrowGib ("progs/gib3.mdl", self.health);
	ThrowGib ("progs/gib1.mdl", self.health);
	ThrowGib ("progs/gib2.mdl", self.health);
	ThrowGib ("progs/gib3.mdl", self.health);

	self.deadflag = DEAD_DEAD;

	if (damage_attacker.classname == "teledeath")
	{
		sound (self, CHAN_VOICE, "player/teledth1.wav", 1, ATTN_NONE);
		return;
	}

	if (damage_attacker.classname == "teledeath2")
	{
		sound (self, CHAN_VOICE, "player/teledth1.wav", 1, ATTN_NONE);
		return;
	}

	if (random() < 0.5)
		sound (self, CHAN_VOICE, "player/gib.wav", 1, ATTN_NONE);
	else
		sound (self, CHAN_VOICE, "player/udeath.wav", 1, ATTN_NONE);
};

void() PlayerDie =
{
	local	float	i;
    // BYOM -- Switch order of powerup cleanup to get rid of eyes
    //             but without it changing monsters to human
    //             easy since monsters can't use the ring of shadows
    if (self.items & IT_INVISIBILITY)
        self.modelindex = modelindex_player;    // don't use eyes
	self.items = self.items - (self.items & IT_INVISIBILITY);
	self.invisible_finished = 0;	// don't die as eyes
	self.invincible_finished = 0;
	self.super_damage_finished = 0;
	self.radsuit_finished = 0;

    // BYOM -- Don't drop a backpack if not a human
    if ((deathmatch || coop) && !self.class)
		DropBackpack();
	
    // BYOM -- FIXME -- Find out secret of solid bodies
	self.weaponmodel="";
	self.view_ofs = '0 0 -8';
	self.deadflag = DEAD_DYING;
	self.solid = SOLID_NOT;
	self.flags = self.flags - (self.flags & FL_ONGROUND);
	self.movetype = MOVETYPE_TOSS;
	if (self.velocity_z < 10)
		self.velocity_z = self.velocity_z + random()*300;

	if (self.health < -40)
	{
		GibPlayer ();
		return;
	}

	DeathSound();
	
	self.angles_x = 0;
	self.angles_z = 0;
	
    // BYOM -- Appropiate death sequence
    if (self.class == CL_SHAMBLER) {
        player_shambler_die();
        return;
    }
    else if (self.class == CL_FIEND) {
        player_demon_die();
        return;
    }
    else if (self.class == CL_WIZARD) {
        player_wizard_die();
        return;
    }
    else if (self.weapon == IT_AXE)
	{
		player_die_ax1 ();
		return;
	}
    else {
        i = cvar("temp1");
        if (!i)
            i = 1 + floor(random()*6);
	
        if (i == 1)
            player_diea1();
        else if (i == 2)
            player_dieb1();
        else if (i == 3)
            player_diec1();
        else if (i == 4)
            player_died1();
        else
            player_diee1();
    }
};

void() set_suicide_frame =
{	// used by klill command and diconnect command
    //if (self.model != "progs/player.mdl")
    //    return; // allready gibbed

    // BYOM -- appropiate frames for death
    if (self.class == CL_SHAMBLER)
        self.frame = $deathd9;
    else if (self.class == CL_WIZARD)
        self.frame = $deatha4;
    else if (self.class == CL_FIEND)
        self.frame = $deatha4;
    else
        self.frame = $deatha11;
	self.solid = SOLID_NOT;
	self.movetype = MOVETYPE_TOSS;
	self.deadflag = DEAD_DEAD;
	self.nextthink = -1;
};

//BYOM -- death sequences for other classes
// Shambler
void() player_shambler_die   =  [$deathc14, player_shambler_die2] {};
void() player_shambler_die2  =  [$deathc15, player_shambler_die3] {};
void() player_shambler_die3  =  [$deathd1, player_shambler_die4] {};
void() player_shambler_die4  =  [$deathd2, player_shambler_die5] {};
void() player_shambler_die5  =  [$deathd3, player_shambler_die6] {};
void() player_shambler_die6  =  [$deathd4, player_shambler_die7] {};
void() player_shambler_die7  =  [$deathd5, player_shambler_die8] {};
void() player_shambler_die8  =  [$deathd6, player_shambler_die9] {};
void() player_shambler_die9  =  [$deathd7, player_shambler_die10] {};
void() player_shambler_die10 =  [$deathd8, player_shambler_die11] {};
void() player_shambler_die11 =  [$deathd9, player_shambler_die11] {PlayerDead();};

// Wizard
void() player_wizard_die     =  [$axdeth6, player_wizard_die2] {};
void() player_wizard_die2    =  [$axdeth7, player_wizard_die3] {};
void() player_wizard_die3    =  [$axdeth8, player_wizard_die4] {};
void() player_wizard_die4    =  [$axdeth9, player_wizard_die5] {};
void() player_wizard_die5    =  [$deatha1, player_wizard_die6] {};
void() player_wizard_die6    =  [$deatha2, player_wizard_die7] {};
void() player_wizard_die7    =  [$deatha3, player_wizard_die8] {};
void() player_wizard_die8    =  [$deatha4, player_wizard_die8] {PlayerDead();};

// Fiend
void() player_demon_die      =  [$axdeth5, player_demon_die2] {};
void() player_demon_die2     =  [$axdeth6, player_demon_die3] {};
void() player_demon_die3     =  [$axdeth7, player_demon_die4] {};
void() player_demon_die4     =  [$axdeth8, player_demon_die5] {};
void() player_demon_die5     =  [$axdeth9, player_demon_die6] {};
void() player_demon_die6     =  [$deatha1, player_demon_die7] {};
void() player_demon_die7     =  [$deatha2, player_demon_die8] {};
void() player_demon_die8     =  [$deatha3, player_demon_die9] {};
void() player_demon_die9     =  [$deatha4, player_demon_die9] {PlayerDead();};


void()	player_diea1	=	[	$deatha1,	player_diea2	] {};
void()	player_diea2	=	[	$deatha2,	player_diea3	] {};
void()	player_diea3	=	[	$deatha3,	player_diea4	] {};
void()	player_diea4	=	[	$deatha4,	player_diea5	] {};
void()	player_diea5	=	[	$deatha5,	player_diea6	] {};
void()	player_diea6	=	[	$deatha6,	player_diea7	] {};
void()	player_diea7	=	[	$deatha7,	player_diea8	] {};
void()	player_diea8	=	[	$deatha8,	player_diea9	] {};
void()	player_diea9	=	[	$deatha9,	player_diea10	] {};
void()	player_diea10	=	[	$deatha10,	player_diea11	] {};
void()	player_diea11	=	[	$deatha11,	player_diea11 ] {PlayerDead();};

void()	player_dieb1	=	[	$deathb1,	player_dieb2	] {};
void()	player_dieb2	=	[	$deathb2,	player_dieb3	] {};
void()	player_dieb3	=	[	$deathb3,	player_dieb4	] {};
void()	player_dieb4	=	[	$deathb4,	player_dieb5	] {};
void()	player_dieb5	=	[	$deathb5,	player_dieb6	] {};
void()	player_dieb6	=	[	$deathb6,	player_dieb7	] {};
void()	player_dieb7	=	[	$deathb7,	player_dieb8	] {};
void()	player_dieb8	=	[	$deathb8,	player_dieb9	] {};
void()	player_dieb9	=	[	$deathb9,	player_dieb9	] {PlayerDead();};

void()	player_diec1	=	[	$deathc1,	player_diec2	] {};
void()	player_diec2	=	[	$deathc2,	player_diec3	] {};
void()	player_diec3	=	[	$deathc3,	player_diec4	] {};
void()	player_diec4	=	[	$deathc4,	player_diec5	] {};
void()	player_diec5	=	[	$deathc5,	player_diec6	] {};
void()	player_diec6	=	[	$deathc6,	player_diec7	] {};
void()	player_diec7	=	[	$deathc7,	player_diec8	] {};
void()	player_diec8	=	[	$deathc8,	player_diec9	] {};
void()	player_diec9	=	[	$deathc9,	player_diec10	] {};
void()	player_diec10	=	[	$deathc10,	player_diec11	] {};
void()	player_diec11	=	[	$deathc11,	player_diec12	] {};
void()	player_diec12	=	[	$deathc12,	player_diec13	] {};
void()	player_diec13	=	[	$deathc13,	player_diec14	] {};
void()	player_diec14	=	[	$deathc14,	player_diec15	] {};
void()	player_diec15	=	[	$deathc15,	player_diec15 ] {PlayerDead();};

void()	player_died1	=	[	$deathd1,	player_died2	] {};
void()	player_died2	=	[	$deathd2,	player_died3	] {};
void()	player_died3	=	[	$deathd3,	player_died4	] {};
void()	player_died4	=	[	$deathd4,	player_died5	] {};
void()	player_died5	=	[	$deathd5,	player_died6	] {};
void()	player_died6	=	[	$deathd6,	player_died7	] {};
void()	player_died7	=	[	$deathd7,	player_died8	] {};
void()	player_died8	=	[	$deathd8,	player_died9	] {};
void()	player_died9	=	[	$deathd9,	player_died9	] {PlayerDead();};

void()	player_diee1	=	[	$deathe1,	player_diee2	] {};
void()	player_diee2	=	[	$deathe2,	player_diee3	] {};
void()	player_diee3	=	[	$deathe3,	player_diee4	] {};
void()	player_diee4	=	[	$deathe4,	player_diee5	] {};
void()	player_diee5	=	[	$deathe5,	player_diee6	] {};
void()	player_diee6	=	[	$deathe6,	player_diee7	] {};
void()	player_diee7	=	[	$deathe7,	player_diee8	] {};
void()	player_diee8	=	[	$deathe8,	player_diee9	] {};
void()	player_diee9	=	[	$deathe9,	player_diee9	] {PlayerDead();};

void()	player_die_ax1	=	[	$axdeth1,	player_die_ax2	] {};
void()	player_die_ax2	=	[	$axdeth2,	player_die_ax3	] {};
void()	player_die_ax3	=	[	$axdeth3,	player_die_ax4	] {};
void()	player_die_ax4	=	[	$axdeth4,	player_die_ax5	] {};
void()	player_die_ax5	=	[	$axdeth5,	player_die_ax6	] {};
void()	player_die_ax6	=	[	$axdeth6,	player_die_ax7	] {};
void()	player_die_ax7	=	[	$axdeth7,	player_die_ax8	] {};
void()	player_die_ax8	=	[	$axdeth8,	player_die_ax9	] {};
void()	player_die_ax9	=	[	$axdeth9,	player_die_ax9	] {PlayerDead();};
