/*
+--------+
|CorpDeth|
+--------+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
| Scratch                                      Http://www.admdev.com/scratch |
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
| Player death animation and functions                                       |
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
*/

$frame normal1
$frame deatha1 deatha2 deatha3 deatha4 deatha5 deatha6 deatha7 deatha8 deatha9 deatha10
$frame deathb1 deathb2 deathb3 deathb4 deathb5 deathb6 deathb7 deathb8 deathb9 deathb10 deathb11
$frame deathc1 deathc2 deathc3 deathc4 deathc5 deathc6 deathc7 deathc8 deathc9 deathc10 deathc11
$frame wfall1 wfall2 wfall3 wfall4 wfall5 wfall6
$frame deathd1 deathd2 deathd3 deathd4 deathd5 deathd6 deathd7 deathd8
$frame deathe1 deathe2 deathe3 deathe4
$frame airdeth1 airdeth2 airdeth3 airdeth4 airdeth5 airdeth6 airdeth7 airdeth8 airdeth9 airdeth10
$frame telefrag1 telefrag2 telefrag3 telefrag4 telefrag5 telefrag6 telefrag7 telefrag8

void() PlayerDead;

void() DeadThink =
{
	if (self.frame == $airdeth10)
		droptofloor(0,0); // fix hang up
	tilt(); // angle dead body with geometry repeat
	self.nextthink = time + 0.1;
};

//
// pistol death
//

void() player_deatha1	=[	$deatha1,	player_deatha2	] {};
void() player_deatha2	=[	$deatha2,	player_deatha3	] {};
void() player_deatha3	=[	$deatha3,	player_deatha4	] {};
void() player_deatha4	=[	$deatha4,	player_deatha5	] {};
void() player_deatha5	=[	$deatha5,	player_deatha6	] {};
void() player_deatha6	=[	$deatha6,	player_deatha7	] {};
void() player_deatha7	=[	$deatha7,	player_deatha8	] {};
void() player_deatha8	=[	$deatha8,	player_deatha9	] {};
void() player_deatha9	=[	$deatha9,	player_deatha10	] 
{
	if (self.flags & FL_ONGROUND)
		sound (self, CHAN_BODY, "misc/floorhit.wav", 1, ATTN_NORM);	
};
void() player_deatha10	=[	$deatha10,	player_deatha10	] 
{
	PlayerDead();
};

//
// akimbo pistol death
//

void() player_deathb1	=[	$deathb1,	player_deathb2	] {};
void() player_deathb2	=[	$deathb2,	player_deathb3	] {};
void() player_deathb3	=[	$deathb3,	player_deathb4	] {};
void() player_deathb4	=[	$deathb4,	player_deathb5	] {};
void() player_deathb5	=[	$deathb5,	player_deathb6	] {};
void() player_deathb6	=[	$deathb6,	player_deathb7	] {};
void() player_deathb7	=[	$deathb7,	player_deathb8	] {};
void() player_deathb8	=[	$deathb8,	player_deathb9	] {};
void() player_deathb9	=[	$deathb9,	player_deathb10	] {};
void() player_deathb10	=[	$deathb10,	player_deathb11	] 
{
	if (self.flags & FL_ONGROUND)
		sound (self, CHAN_BODY, "misc/floorhit.wav", 1, ATTN_NORM);	
};
void() player_deathb11	=[	$deathb11,	player_deathb11	] 
{
	PlayerDead();
};

//
//shotgun death
//

void() player_wfall1;
//smacked up on the wall
void() player_deathc11	=[	$deathc11,	player_deathc11	] 
{
		if (self.wfall_finished < time)
			player_wfall1();
};

void() player_deathc2;
void() player_deathc4;
void() player_deathc1	=[	$deathc1,	player_deathc1	] 
{
	local	vector	source;

	if (self.velocity_z < 0 || self.flags & FL_ONGROUND)
	{
		player_deathc2();
		return;
	}

	if (self.flags & FL_ONGROUND || self.velocity == '0 0 0')
		player_deathc4();
	else
	{
		makevectors (self.angles);
		source = self.origin + '0 0 16';
		traceline (source, source - v_forward*35, FALSE, self);
		if (trace_fraction == 1.0)
			return;
	
		if (trace_ent.solid == SOLID_BSP && self.smacked == FALSE)
		{
			self.smacked = TRUE;
			self.movetype = MOVETYPE_FLY;
			self.velocity = '0 0 0';
			self.wfall_finished = time + 0.5 + random() * 1;
			sound (self, CHAN_BODY, "misc/wallhit.wav", 1, ATTN_NORM);
			self.angles_y = vectoyaw(trace_plane_normal); // adjust players corpse to y angle of the wall
			self.fixangle = TRUE;
			player_deathc11();
		}
	}
	self.nextthink = time + 0.01;
};
void() player_deathc2	=[	$deathc2,	player_deathc3	] {self.nextthink = time + 0.2;};
void() player_deathc3	=[	$deathc3,	player_deathc3	] 
{
	if (self.flags & FL_ONGROUND || self.velocity == '0 0 0')
		player_deathc4();
};
void() player_deathc4	=[	$deathc4,	player_deathc5	] 
{
	if (self.flags & FL_ONGROUND)
		sound (self, CHAN_BODY, "misc/floorhit.wav", 1, ATTN_NORM);
};
void() player_deathc5	=[	$deathc5,	player_deathc6	] {};
void() player_deathc6	=[	$deathc6,	player_deathc7	] {};
void() player_deathc7	=[	$deathc7,	player_deathc8	] {};
void() player_deathc8	=[	$deathc8,	player_deathc9	] {};
void() player_deathc9	=[	$deathc9,	player_deathc10	] {};
void() player_deathc10	=[	$deathc10,	player_deathc10	] 
{
	PlayerDead();
};

void() player_wfall2;
void() player_wfall1	=[	$wfall1,	player_wfall1	] 
{
	self.movetype = MOVETYPE_TOSS;
	if (self.flags & FL_ONGROUND || !self.velocity)
		player_wfall2();
};
void() player_wfall2	=[	$wfall2,	player_wfall3	] {};
void() player_wfall3	=[	$wfall3,	player_wfall4	] {};
void() player_wfall4	=[	$wfall4,	player_wfall5	] {};
void() player_wfall5	=[	$wfall5,	player_wfall6	] 
{
	if (self.flags & FL_ONGROUND)
		sound (self, CHAN_BODY, "misc/floorhit.wav", 1, ATTN_NORM);	
};
void() player_wfall6	=[	$wfall6,	player_wfall6	] 
{
	PlayerDead();
};

void() player_deathd1	=[	$deathd1,	player_deathd2	] {};
void() player_deathd2	=[	$deathd2,	player_deathd3	] {};
void() player_deathd3	=[	$deathd3,	player_deathd4	] {};
void() player_deathd4	=[	$deathd4,	player_deathd5	] {};
void() player_deathd5	=[	$deathd5,	player_deathd6	] 
{
	self.nextthink = time + 0.2;
};
void() player_deathd6	=[	$deathd6,	player_deathd7	] 
{
	self.solid = SOLID_NOT;
};
void() player_deathd7	=[	$deathd7,	player_deathd8	] 
{
	if (self.flags & FL_ONGROUND)
		sound (self, CHAN_BODY, "misc/floorhit.wav", 1, ATTN_NORM);	
};
void() player_deathd8	=[	$deathd8,	player_deathd8	] 
{
	PlayerDead();
};

// uzi death sequence
void() player_deathe1	=[	$deathe1,	player_deathd5	] {};
void() player_deathe2	=[	$deathe2,	player_deathd5	] {};
void() player_deathe3	=[	$deathe3,	player_deathd5	] {};
void() player_deathe4	=[	$deathe4,	player_deathd5	] {};

// each time the player is shot between these frames he'll twitch
// only starts with the uzi death.
void() UziDeathCheck =
{
	if (self.frame >= $deathd1 && self.frame <= $deathd5)
	{
		if (random() < 0.25)
			player_deathe1();
		else if (random() < 0.50)
			player_deathe2();
		else if (random() < 0.75)
			player_deathe3();
		else
			player_deathe4();
	}
};

void() player_airdeth9	=[	$airdeth9,	player_airdeth10	] {};
void() player_airdeth10	=[	$airdeth10,	player_airdeth10	] 
{
	PlayerDead();
};

void() airdeth_check =
{
	if (self.flags & FL_ONGROUND || self.velocity == '0 0 0')
	{
		sound (self, CHAN_BODY, "misc/floorhit.wav", 1, ATTN_NORM);
		player_airdeth9();
	}
};

// killed in air
void() player_airdeth1	=[	$airdeth1,	player_airdeth2	] {airdeth_check();};
void() player_airdeth2	=[	$airdeth2,	player_airdeth3	] {airdeth_check();};
void() player_airdeth3	=[	$airdeth3,	player_airdeth4	] {airdeth_check();};
void() player_airdeth4	=[	$airdeth4,	player_airdeth5	] {airdeth_check();};
void() player_airdeth5	=[	$airdeth5,	player_airdeth6	] {airdeth_check();};
void() player_airdeth6	=[	$airdeth6,	player_airdeth7	] {airdeth_check();};
void() player_airdeth7	=[	$airdeth7,	player_airdeth8	] {airdeth_check();};
void() player_airdeth8	=[	$airdeth8,	player_airdeth1	] {airdeth_check();};

void() particle_think =
{
	self.cnt = self.cnt + 1;
	particle (self.origin, '0 0 0', 40, 4);
	particle (self.origin, '0 0 0', 37, 4);
	particle (self.origin, '0 0 0', 40, 4);
	particle (self.origin, '0 0 0', 37, 4);
	if (self.cnt > 19)
		remove(self);
	self.nextthink = time + 0.1;
};

void() teledeath_particle =
{
	local entity part;

	part = spawn ();
	part.owner = self;
	part.movetype = MOVETYPE_NOCLIP;
	part.solid = SOLID_NOT;
	part.classname = "particle";
	setmodel (part, string_null);
	setsize (part, '0 0 0', '0 0 0');             
	setorigin (part, self.origin);
	part.velocity_z = 35;
	part.think = particle_think;
	part.nextthink = time + 0.1;
};

void() player_teledeth1	=[	$telefrag1,	player_teledeth2	] {};
void() player_teledeth2	=[	$telefrag2,	player_teledeth3	] {};
void() player_teledeth3	=[	$telefrag3,	player_teledeth4	] {};
void() player_teledeth4	=[	$telefrag4,	player_teledeth5	] {};
void() player_teledeth5	=[	$telefrag5,	player_teledeth6	] {};
void() player_teledeth6	=[	$telefrag6,	player_teledeth7	] {};
void() player_teledeth7	=[	$telefrag7,	player_teledeth8	] {};
void() player_teledeth8	=[	$telefrag8,	player_teledeth9	] 
{
	teledeath_particle();
};
void() player_teledeth9	=[	$telefrag8,	player_teledeth9	] 
{
	PlayerDead();
	setmodel (self, string_null);
};
