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

KNIGHT

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

$cd id1/models/knight2
$origin 0 0 24
$base base
$skin skin

$frame hknightc
$frame hknightd

void() hknight_char_a1;
void() hknight_run1;
void() hk_idle_sound;

void(float offset) hknight_shot =
{
	local	vector	offang;
	local	vector	org, vec;
	
	offang = vectoangles (self.enemy.origin - self.origin);
	offang_y = offang_y + offset * 6;
	
	makevectors (offang);

	org = self.origin + self.mins + self.size*0.5 + v_forward * 20;

// set missile speed
	vec = normalize (v_forward);
	vec_z = 0 - vec_z + (random() - 0.5)*0.1;
	
	launch_spike (org, vec);
	newmis.classname = "knightspike";
//	setmodel (newmis, "progs/k_spike.mdl");
	setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);		
	newmis.velocity = vec*300;
	sound (self, CHAN_WEAPON, "hknight/attack1.wav", 1, ATTN_NORM);
};

void() CheckForCharge =
{
// check for mad charge
if (!enemy_vis)
	return;
if (time < self.attack_finished)
	return;	
if ( fabs(self.origin_z - self.enemy.origin_z) > 20)
	return;		// too much height change
if ( vlen (self.origin - self.enemy.origin) < 80)
	return;		// use regular attack

// charge		
	SUB_AttackFinished (2);
	hknight_char_a1 ();

};

void() CheckContinueCharge =
{
	if (time > self.attack_finished)
	{
		SUB_AttackFinished (3);
		hknight_run1 ();
		return;		// done charging
	}
	if (random() > 0.5)
		sound (self, CHAN_WEAPON, "knight/sword2.wav", 1, ATTN_NORM);
	else
		sound (self, CHAN_WEAPON, "knight/sword1.wav", 1, ATTN_NORM);
};

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

void()	hknight_stand1	=[	$hknightc,	hknight_stand2	] {ai_stand();};
void()	hknight_stand2	=[	$hknightc,	hknight_stand3	] {ai_stand();};
void()	hknight_stand3	=[	$hknightc,	hknight_stand4	] {ai_stand();};
void()	hknight_stand4	=[	$hknightc,	hknight_stand5	] {ai_stand();};
void()	hknight_stand5	=[	$hknightc,	hknight_stand6	] {ai_stand();};
void()	hknight_stand6	=[	$hknightc,	hknight_stand7	] {ai_stand();};
void()	hknight_stand7	=[	$hknightc,	hknight_stand8	] {ai_stand();};
void()	hknight_stand8	=[	$hknightc,	hknight_stand9	] {ai_stand();};
void()	hknight_stand9	=[	$hknightc,	hknight_stand1	] {ai_stand();};

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

void()	hknight_walk1	=[	$hknightc,		hknight_walk2	] {
hk_idle_sound();
ai_walk(2);};
void()	hknight_walk2	=[	$hknightc,		hknight_walk3	] {ai_walk(5);};
void()	hknight_walk3	=[	$hknightc,		hknight_walk4	] {ai_walk(5);};
void()	hknight_walk4	=[	$hknightc,		hknight_walk5	] {ai_walk(4);};
void()	hknight_walk5	=[	$hknightc,		hknight_walk6	] {ai_walk(4);};
void()	hknight_walk6	=[	$hknightc,		hknight_walk7	] {ai_walk(2);};
void()	hknight_walk7	=[	$hknightc,		hknight_walk8	] {ai_walk(2);};
void()	hknight_walk8	=[	$hknightc,		hknight_walk9	] {ai_walk(3);};
void()	hknight_walk9	=[	$hknightc,		hknight_walk10	] {ai_walk(3);};
void()	hknight_walk10	=[	$hknightc,	hknight_walk11	] {ai_walk(4);};
void()	hknight_walk11	=[	$hknightc,	hknight_walk12	] {ai_walk(3);};
void()	hknight_walk12	=[	$hknightc,	hknight_walk13	] {ai_walk(4);};
void()	hknight_walk13	=[	$hknightc,	hknight_walk14	] {ai_walk(6);};
void()	hknight_walk14	=[	$hknightc,	hknight_walk15	] {ai_walk(2);};
void()	hknight_walk15	=[	$hknightc,	hknight_walk16	] {ai_walk(2);};
void()	hknight_walk16	=[	$hknightc,	hknight_walk17	] {ai_walk(4);};
void()	hknight_walk17	=[	$hknightc,	hknight_walk18	] {ai_walk(3);};
void()	hknight_walk18	=[	$hknightc,	hknight_walk19	] {ai_walk(3);};
void()	hknight_walk19	=[	$hknightc,	hknight_walk20	] {ai_walk(3);};
void()	hknight_walk20	=[	$hknightc,	hknight_walk1	] {ai_walk(2);};

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

void()	hknight_run1	=[	$hknightc,		hknight_run2	] {
hk_idle_sound();
ai_run (20); CheckForCharge (); };
void()	hknight_run2	=[	$hknightc,		hknight_run3	] {ai_run(25);};
void()	hknight_run3	=[	$hknightc,		hknight_run4	] {ai_run(18);};
void()	hknight_run4	=[	$hknightc,		hknight_run5	] {ai_run(16);};
void()	hknight_run5	=[	$hknightc,		hknight_run6	] {ai_run(14);};
void()	hknight_run6	=[	$hknightc,		hknight_run7	] {ai_run(25);};
void()	hknight_run7	=[	$hknightc,		hknight_run8	] {ai_run(21);};
void()	hknight_run8	=[	$hknightc,		hknight_run1	] {ai_run(13);};

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

void()	hknight_pain1	=[	$hknightc,		hknight_pain2	] {sound (self, CHAN_VOICE, "hknight/pain1.wav", 1, ATTN_NORM);};
void()	hknight_pain2	=[	$hknightc,		hknight_pain3	] {};
void()	hknight_pain3	=[	$hknightc,		hknight_pain4	] {};
void()	hknight_pain4	=[	$hknightc,		hknight_pain5	] {};
void()	hknight_pain5	=[	$hknightc,		hknight_run1	] {};

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

void()	hknight_die1	=[	$hknightd,	hknight_die2	] {ai_forward(10);};
void()	hknight_die2	=[	$hknightd,	hknight_die3	] {ai_forward(8);};
void()	hknight_die3	=[	$hknightd,	hknight_die4	]
{self.solid = SOLID_NOT; ai_forward(7);};
void()	hknight_die4	=[	$hknightd,	hknight_die5	] {};
void()	hknight_die5	=[	$hknightd,	hknight_die6	] {};
void()	hknight_die6	=[	$hknightd,	hknight_die7	] {};
void()	hknight_die7	=[	$hknightd,	hknight_die8	] {};
void()	hknight_die8	=[	$hknightd,	hknight_die9	] {ai_forward(10);};
void()	hknight_die9	=[	$hknightd,	hknight_die10	] {ai_forward(11);};
void()	hknight_die10	=[	$hknightd,	hknight_die11	] {};
void()	hknight_die11	=[	$hknightd,	hknight_die12	] {};
void()	hknight_die12	=[	$hknightd,	hknight_die12	] {};

void()	hknight_dieb1	=[	$hknightd,	hknight_dieb2	] {};
void()	hknight_dieb2	=[	$hknightd,	hknight_dieb3	] {};
void()	hknight_dieb3	=[	$hknightd,	hknight_dieb4	]
{self.solid = SOLID_NOT;};
void()	hknight_dieb4	=[	$hknightd,	hknight_dieb5	] {};
void()	hknight_dieb5	=[	$hknightd,	hknight_dieb6	] {};
void()	hknight_dieb6	=[	$hknightd,	hknight_dieb7	] {};
void()	hknight_dieb7	=[	$hknightd,	hknight_dieb8	] {};
void()	hknight_dieb8	=[	$hknightd,	hknight_dieb9	] {};
void()	hknight_dieb9	=[	$hknightd,	hknight_dieb9	] {};

void() hknight_die =
{
// check for gib
	if (self.health < -40)
	{
		sound (self, CHAN_VOICE, "player/udeath.wav", 1, ATTN_NORM);
		ThrowHead ("progs/h_hellkn.mdl", self.health);
		ThrowGib ("progs/gib1.mdl", self.health);
		ThrowGib ("progs/gib2.mdl", self.health);
		ThrowGib ("progs/gib3.mdl", self.health);
		return;
	}

// regular death
	sound (self, CHAN_VOICE, "hknight/death1.wav", 1, ATTN_NORM);
	if (random() > 0.5)
		hknight_die1 ();
	else
		hknight_dieb1 ();
};


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

void()	hknight_magica1 =[	$hknightc,	hknight_magica2	] {ai_face();};
void()	hknight_magica2 =[	$hknightc,	hknight_magica3	] {ai_face();};
void()	hknight_magica3 =[	$hknightc,	hknight_magica4	] {ai_face();};
void()	hknight_magica4 =[	$hknightc,	hknight_magica5	] {ai_face();};
void()	hknight_magica5 =[	$hknightc,	hknight_magica6	] {ai_face();};
void()	hknight_magica6 =[	$hknightc,	hknight_magica7	] {ai_face();};
void()	hknight_magica7 =[	$hknightc,	hknight_magica8	] {hknight_shot(-2);};
void()	hknight_magica8 =[	$hknightc,	hknight_magica9	] {hknight_shot(-1);};
void()	hknight_magica9 =[	$hknightc,	hknight_magica10] {hknight_shot(0);};
void()	hknight_magica10 =[	$hknightc,	hknight_magica11] {hknight_shot(1);};
void()	hknight_magica11 =[	$hknightc,	hknight_magica12] {hknight_shot(2);};
void()	hknight_magica12 =[	$hknightc,	hknight_magica13] {hknight_shot(3);};
void()	hknight_magica13 =[	$hknightc,	hknight_magica14] {ai_face();};
void()	hknight_magica14 =[	$hknightc,	hknight_run1	] {ai_face();};

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

void()	hknight_magicb1 =[	$hknightc,	hknight_magicb2	] {ai_face();};
void()	hknight_magicb2 =[	$hknightc,	hknight_magicb3	] {ai_face();};
void()	hknight_magicb3 =[	$hknightc,	hknight_magicb4	] {ai_face();};
void()	hknight_magicb4 =[	$hknightc,	hknight_magicb5	] {ai_face();};
void()	hknight_magicb5 =[	$hknightc,	hknight_magicb6	] {ai_face();};
void()	hknight_magicb6 =[	$hknightc,	hknight_magicb7	] {ai_face();};
void()	hknight_magicb7 =[	$hknightc,	hknight_magicb8	] {hknight_shot(-2);};
void()	hknight_magicb8 =[	$hknightc,	hknight_magicb9	] {hknight_shot(-1);};
void()	hknight_magicb9 =[	$hknightc,	hknight_magicb10] {hknight_shot(0);};
void()	hknight_magicb10 =[	$hknightc,	hknight_magicb11] {hknight_shot(1);};
void()	hknight_magicb11 =[	$hknightc,	hknight_magicb12] {hknight_shot(2);};
void()	hknight_magicb12 =[	$hknightc,	hknight_magicb13] {hknight_shot(3);};
void()	hknight_magicb13 =[	$hknightc,	hknight_run1] {ai_face();};

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

void()	hknight_magicc1 =[	$hknightc,	hknight_magicc2	] {ai_face();};
void()	hknight_magicc2 =[	$hknightc,	hknight_magicc3	] {ai_face();};
void()	hknight_magicc3 =[	$hknightc,	hknight_magicc4	] {ai_face();};
void()	hknight_magicc4 =[	$hknightc,	hknight_magicc5	] {ai_face();};
void()	hknight_magicc5 =[	$hknightc,	hknight_magicc6	] {ai_face();};
void()	hknight_magicc6 =[	$hknightc,	hknight_magicc7	] {hknight_shot(-2);};
void()	hknight_magicc7 =[	$hknightc,	hknight_magicc8	] {hknight_shot(-1);};
void()	hknight_magicc8 =[	$hknightc,	hknight_magicc9	] {hknight_shot(0);};
void()	hknight_magicc9 =[	$hknightc,	hknight_magicc10] {hknight_shot(1);};
void()	hknight_magicc10 =[	$hknightc,	hknight_magicc11] {hknight_shot(2);};
void()	hknight_magicc11 =[	$hknightc,	hknight_run1] {hknight_shot(3);};

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

void()	hknight_char_a1	=[	$hknightc,	hknight_char_a2	] {ai_charge(20);};
void()	hknight_char_a2	=[	$hknightc,	hknight_char_a3	] {ai_charge(25);};
void()	hknight_char_a3	=[	$hknightc,	hknight_char_a4	] {ai_charge(18);};
void()	hknight_char_a4	=[	$hknightc,	hknight_char_a5	] {ai_charge(16);};
void()	hknight_char_a5	=[	$hknightc,	hknight_char_a6	] {ai_charge(14);};
void()	hknight_char_a6	=[	$hknightc,	hknight_char_a7	] {ai_charge(20); ai_melee();};
void()	hknight_char_a7	=[	$hknightc,	hknight_char_a8	] {ai_charge(21); ai_melee();};
void()	hknight_char_a8	=[	$hknightc,	hknight_char_a9	] {ai_charge(13); ai_melee();};
void()	hknight_char_a9	=[	$hknightc,	hknight_char_a10	] {ai_charge(20); ai_melee();};
void()	hknight_char_a10=[	$hknightc,	hknight_char_a11	] {ai_charge(20); ai_melee();};
void()	hknight_char_a11=[	$hknightc,	hknight_char_a12	] {ai_charge(18); ai_melee();};
void()	hknight_char_a12=[	$hknightc,	hknight_char_a13	] {ai_charge(16);};
void()	hknight_char_a13=[	$hknightc,	hknight_char_a14	] {ai_charge(14);};
void()	hknight_char_a14=[	$hknightc,	hknight_char_a15	] {ai_charge(25);};
void()	hknight_char_a15=[	$hknightc,	hknight_char_a16	] {ai_charge(21);};
void()	hknight_char_a16=[	$hknightc,	hknight_run1	] {ai_charge(13);};

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

void()	hknight_char_b1	=[	$hknightc,	hknight_char_b2	]
{CheckContinueCharge (); ai_charge(23); ai_melee();};
void()	hknight_char_b2	=[	$hknightc,	hknight_char_b3	] {ai_charge(17); ai_melee();};
void()	hknight_char_b3	=[	$hknightc,	hknight_char_b4	] {ai_charge(12); ai_melee();};
void()	hknight_char_b4	=[	$hknightc,	hknight_char_b5	] {ai_charge(22); ai_melee();};
void()	hknight_char_b5	=[	$hknightc,	hknight_char_b6	] {ai_charge(18); ai_melee();};
void()	hknight_char_b6	=[	$hknightc,	hknight_char_b1	] {ai_charge(8); ai_melee();};

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

void()	hknight_slice1	=[	$hknightc,	hknight_slice2	] {ai_charge(9);};
void()	hknight_slice2	=[	$hknightc,	hknight_slice3	] {ai_charge(6);};
void()	hknight_slice3	=[	$hknightc,	hknight_slice4	] {ai_charge(13);};
void()	hknight_slice4	=[	$hknightc,	hknight_slice5	] {ai_charge(4);};
void()	hknight_slice5	=[	$hknightc,	hknight_slice6	] {ai_charge(7); ai_melee();};
void()	hknight_slice6	=[	$hknightc,	hknight_slice7	] {ai_charge(15); ai_melee();};
void()	hknight_slice7	=[	$hknightc,	hknight_slice8	] {ai_charge(8); ai_melee();};
void()	hknight_slice8	=[	$hknightc,	hknight_slice9	] {ai_charge(2); ai_melee();};
void()	hknight_slice9	=[	$hknightc,	hknight_slice10	] {ai_melee();};
void()	hknight_slice10	=[	$hknightc,	hknight_run1	] {ai_charge(3);};

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

void()	hknight_smash1	=[	$hknightc,	hknight_smash2	] {ai_charge(1);};
void()	hknight_smash2	=[	$hknightc,	hknight_smash3	] {ai_charge(13);};
void()	hknight_smash3	=[	$hknightc,	hknight_smash4	] {ai_charge(9);};
void()	hknight_smash4	=[	$hknightc,	hknight_smash5	] {ai_charge(11);};
void()	hknight_smash5	=[	$hknightc,	hknight_smash6	] {ai_charge(10); ai_melee();};
void()	hknight_smash6	=[	$hknightc,	hknight_smash7	] {ai_charge(7); ai_melee();};
void()	hknight_smash7	=[	$hknightc,	hknight_smash8	] {ai_charge(12); ai_melee();};
void()	hknight_smash8	=[	$hknightc,	hknight_smash9	] {ai_charge(2); ai_melee();};
void()	hknight_smash9	=[	$hknightc,	hknight_smash10	] {ai_charge(3); ai_melee();};
void()	hknight_smash10	=[	$hknightc,	hknight_smash11	] {ai_charge(0);};
void()	hknight_smash11	=[	$hknightc,	hknight_run1	] {ai_charge(0);};

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

void()	hknight_watk1	=[	$hknightc,	hknight_watk2	] {ai_charge(2);};
void()	hknight_watk2	=[	$hknightc,	hknight_watk3	] {ai_charge(0);};
void()	hknight_watk3	=[	$hknightc,	hknight_watk4	] {ai_charge(0);};
void()	hknight_watk4	=[	$hknightc,	hknight_watk5	] {ai_melee();};
void()	hknight_watk5	=[	$hknightc,	hknight_watk6	] {ai_melee();};
void()	hknight_watk6	=[	$hknightc,	hknight_watk7	] {ai_melee();};
void()	hknight_watk7	=[	$hknightc,	hknight_watk8	] {ai_charge(1);};
void()	hknight_watk8	=[	$hknightc,	hknight_watk9	] {ai_charge(4);};
void()	hknight_watk9	=[	$hknightc,	hknight_watk10	] {ai_charge(5);};
void()	hknight_watk10	=[	$hknightc,	hknight_watk11	] {ai_charge(3); ai_melee();};
void()	hknight_watk11	=[	$hknightc,	hknight_watk12	] {ai_charge(2); ai_melee();};
void()	hknight_watk12	=[	$hknightc,	hknight_watk13	] {ai_charge(2); ai_melee();};
void()	hknight_watk13	=[	$hknightc,	hknight_watk14	] {ai_charge(0);};
void()	hknight_watk14	=[	$hknightc,	hknight_watk15	] {ai_charge(0);};
void()	hknight_watk15	=[	$hknightc,	hknight_watk16	] {ai_charge(0);};
void()	hknight_watk16	=[	$hknightc,	hknight_watk17	] {ai_charge(1);};
void()	hknight_watk17	=[	$hknightc,	hknight_watk18	] {ai_charge(1); ai_melee();};
void()	hknight_watk18	=[	$hknightc,	hknight_watk19	] {ai_charge(3); ai_melee();};
void()	hknight_watk19	=[	$hknightc,	hknight_watk20	] {ai_charge(4); ai_melee();};
void()	hknight_watk20	=[	$hknightc,	hknight_watk21	] {ai_charge(6);};
void()	hknight_watk21	=[	$hknightc,	hknight_watk22	] {ai_charge(7);};
void()	hknight_watk22	=[	$hknightc,	hknight_run1	] {ai_charge(3);};

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

void() hk_idle_sound =
{
	if (random() < 0.2)
		sound (self, CHAN_VOICE, "hknight/idle.wav", 1, ATTN_NORM);
};

void(entity attacker, float damage)	hknight_pain =
{
	if (self.pain_finished > time)
		return;

	sound (self, CHAN_VOICE, "hknight/pain1.wav", 1, ATTN_NORM);

	if (time - self.pain_finished > 5)
	{	// allways go into pain frame if it has been a while
		hknight_pain1 ();
		self.pain_finished = time + 1;
		return;
	}
	
	if ((random()*30 > damage) )
		return;		// didn't flinch

	self.pain_finished = time + 1;
	hknight_pain1 ();
};

float	hknight_type;

void() hknight_melee =
{
	hknight_type = hknight_type + 1;

	sound (self, CHAN_WEAPON, "hknight/slash1.wav", 1, ATTN_NORM);
	if (hknight_type == 1)
		hknight_slice1 ();
	else if (hknight_type == 2)
		hknight_smash1 ();
	else if (hknight_type == 3)
	{
		hknight_watk1 ();
		hknight_type = 0;
	}
};

/*UNTITLED-GAME/D monster_hell_knight (1 0 0) (-16 -16 -24) (16 16 40) Ambush
*/
void() monster_hell_knight =
{
	if (deathmatch)
	{
		remove(self);
		return;
	}
	precache_model2 ("progs/hknight.mdl");
	precache_model2 ("progs/k_spike.mdl");
	precache_model2 ("progs/h_hellkn.mdl");

	
	precache_sound2 ("hknight/attack1.wav");
	precache_sound2 ("hknight/death1.wav");
	precache_sound2 ("hknight/pain1.wav");
	precache_sound2 ("hknight/sight1.wav");
	precache_sound ("hknight/hit.wav");		// used by C code, so don't sound2
	precache_sound2 ("hknight/slash1.wav");
	precache_sound2 ("hknight/idle.wav");
	precache_sound2 ("hknight/grunt.wav");

	precache_sound ("knight/sword1.wav");
	precache_sound ("knight/sword2.wav");
	
	self.solid = SOLID_SLIDEBOX;
	self.movetype = MOVETYPE_STEP;

	setmodel (self, "progs/hknight.mdl");

	setsize (self, '-16 -16 -24', '16 16 40');
	self.health = 250;

	self.th_stand = hknight_stand1;
	self.th_walk = hknight_walk1;
	self.th_run = hknight_run1;
	self.th_melee = hknight_melee;
	self.th_missile = hknight_magicc1;
	self.th_pain = hknight_pain;
	self.th_die = hknight_die;
	
	walkmonster_start ();
};