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

SHAL-RATH

==============================================================================
*/
$cd UG/models/shalrath
$origin 0 0 24
$base base
$skin skin
$scale 0.7

$frame shalrathc
$frame shalrathd

void() shalrath_pain;
void() ShalMissile;
void() shal_stand     =[      $shalrathc,       shal_stand    ] {ai_stand();};

void() shal_walk1     =[      $shalrathc,       shal_walk2    ] {
if (random() < 0.2)
	sound (self, CHAN_VOICE, "shalrath/idle.wav", 1, ATTN_IDLE);
ai_walk(6);};
void() shal_walk2     =[      $shalrathc,       shal_walk3    ] {ai_walk(4);};
void() shal_walk3     =[      $shalrathc,       shal_walk4    ] {ai_walk(0);};
void() shal_walk4     =[      $shalrathc,       shal_walk5    ] {ai_walk(0);};
void() shal_walk5     =[      $shalrathc,       shal_walk6    ] {ai_walk(0);};
void() shal_walk6     =[      $shalrathc,       shal_walk7    ] {ai_walk(0);};
void() shal_walk7     =[      $shalrathc,       shal_walk8    ] {ai_walk(5);};
void() shal_walk8     =[      $shalrathc,       shal_walk9    ] {ai_walk(6);};
void() shal_walk9     =[      $shalrathc,       shal_walk10    ] {ai_walk(5);};
void() shal_walk10    =[      $shalrathc,       shal_walk11    ] {ai_walk(0);};
void() shal_walk11    =[      $shalrathc,       shal_walk12    ] {ai_walk(4);};
void() shal_walk12    =[      $shalrathc,       shal_walk1    ] {ai_walk(5);};

void() shal_run1     =[      $shalrathc,       shal_run2    ] {
if (random() < 0.2)
	sound (self, CHAN_VOICE, "shalrath/idle.wav", 1, ATTN_IDLE);
ai_run(6);};
void() shal_run2     =[      $shalrathc,       shal_run3    ] {ai_run(4);};
void() shal_run3     =[      $shalrathc,       shal_run4    ] {ai_run(0);};
void() shal_run4     =[      $shalrathc,       shal_run5    ] {ai_run(0);};
void() shal_run5     =[      $shalrathc,       shal_run6    ] {ai_run(0);};
void() shal_run6     =[      $shalrathc,       shal_run7    ] {ai_run(0);};
void() shal_run7     =[      $shalrathc,       shal_run8    ] {ai_run(5);};
void() shal_run8     =[      $shalrathc,       shal_run9    ] {ai_run(6);};
void() shal_run9     =[      $shalrathc,       shal_run10    ] {ai_run(5);};
void() shal_run10    =[      $shalrathc,       shal_run11    ] {ai_run(0);};
void() shal_run11    =[      $shalrathc,       shal_run12    ] {ai_run(4);};
void() shal_run12    =[      $shalrathc,       shal_run1    ] {ai_run(5);};

void() shal_attack1     =[      $shalrathc,       shal_attack2    ] {
sound (self, CHAN_VOICE, "shalrath/attack.wav", 1, ATTN_NORM);
ai_face();
};
void() shal_attack2     =[      $shalrathc,       shal_attack3    ] {ai_face();};
void() shal_attack3     =[      $shalrathc,       shal_attack4    ] {ai_face();};
void() shal_attack4     =[      $shalrathc,       shal_attack5    ] {ai_face();};
void() shal_attack5     =[      $shalrathc,       shal_attack6    ] {ai_face();};
void() shal_attack6     =[      $shalrathc,       shal_attack7    ] {ai_face();};
void() shal_attack7     =[      $shalrathc,       shal_attack8    ] {ai_face();};
void() shal_attack8     =[      $shalrathc,       shal_attack9    ] {ai_face();};
void() shal_attack9     =[      $shalrathc,       shal_attack10   ] {ShalMissile();};
void() shal_attack10    =[      $shalrathc,      shal_attack11   ] {ai_face();};
void() shal_attack11    =[      $shalrathc,      shal_run1   ] {};

void() shal_pain1       =[      $shalrathc, shal_pain2      ] {};
void() shal_pain2       =[      $shalrathc, shal_pain3      ] {};
void() shal_pain3       =[      $shalrathc, shal_pain4      ] {};
void() shal_pain4       =[      $shalrathc, shal_pain5      ] {};
void() shal_pain5       =[      $shalrathc, shal_run1      ] {};

void() shal_death1      =[      $shalrathd,        shal_death2     ] {};
void() shal_death2      =[      $shalrathd,        shal_death3     ] {};
void() shal_death3      =[      $shalrathd,        shal_death4     ] {};
void() shal_death4      =[      $shalrathd,        shal_death5     ] {};
void() shal_death5      =[      $shalrathd,        shal_death6     ] {};
void() shal_death6      =[      $shalrathd,        shal_death7     ] {};
void() shal_death7      =[      $shalrathd,        shal_death7    ] {};


void() shalrath_pain =
{
	if (self.pain_finished > time)
		return;

	sound (self, CHAN_VOICE, "shalrath/pain.wav", 1, ATTN_NORM);
	shal_pain1();
	self.pain_finished = time + 3;
};

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

	sound (self, CHAN_VOICE, "shalrath/death.wav", 1, ATTN_NORM);
	shal_death1();
	self.solid = SOLID_NOT;
	// insert death sounds here
};

/*
================
ShalMissile
================
*/
void() ShalMissileTouch;
void() ShalHome;
void() ShalMissile =
{
	local	entity 	missile;
	local	vector	dir;
	local	float	dist, flytime;

	dir = normalize((self.enemy.origin + '0 0 10') - self.origin);
	dist = vlen (self.enemy.origin - self.origin);
	flytime = dist * 0.002;
	if (flytime < 0.1)
		flytime = 0.1;

//	self.effects = self.effects | EF_MUZZLEFLASH;
	sound (self, CHAN_WEAPON, "shalrath/attack2.wav", 1, ATTN_NORM);

	missile = spawn ();
	missile.owner = self;

	missile.solid = SOLID_BBOX;
	missile.movetype = MOVETYPE_FLYMISSILE;
//	setmodel (missile, "progs/v_spike.mdl");

	setsize (missile, '0 0 0', '0 0 0');		

	missile.origin = self.origin + '0 0 10';
	missile.velocity = dir * 400;
	missile.avelocity = '300 300 300';
	missile.nextthink = flytime + time;
	missile.think = ShalHome;
	missile.enemy = self.enemy;
	missile.touch = ShalMissileTouch;
};

void() ShalHome =
{
	local vector	dir, vtemp;
	vtemp = self.enemy.origin + '0 0 10';
	if (self.enemy.health < 1)
	{
		remove(self);
		return;
	}
	dir = normalize(vtemp - self.origin);
	if (skill == 3)
		self.velocity = dir * 350;
	else
		self.velocity = dir * 250;
	self.nextthink = time + 0.2;
	self.think = ShalHome;	
};

void() ShalMissileTouch =
{
	if (other == self.owner)
		return;		// don't explode on owner

	if (other.classname == "monster_zombie")
		T_Damage (other, self, self, 110);	
	T_RadiusDamage (self, self.owner, 40, world);
	sound (self, CHAN_WEAPON, "weapons/r_exp3.wav", 1, ATTN_NORM);

//	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);

	self.velocity = '0 0 0';
	self.touch = SUB_Null;
//	setmodel (self, "progs/s_explod.spr");
	self.solid = SOLID_NOT;
	s_explode1 ();
};

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

/*UNTITLED-GAMED monster_shalrath (1 0 0) (-32 -32 -24) (32 32 48) Ambush
*/
void() monster_shalrath =
{
	if (deathmatch)
	{
		remove(self);
		return;
	}
	precache_model2 ("progs/shalrath.mdl");
	precache_model2 ("progs/h_shal.mdl");
	precache_model2 ("progs/v_spike.mdl");
	
	precache_sound2 ("shalrath/attack.wav");
	precache_sound2 ("shalrath/attack2.wav");
	precache_sound2 ("shalrath/death.wav");
	precache_sound2 ("shalrath/idle.wav");
	precache_sound2 ("shalrath/pain.wav");
	precache_sound2 ("shalrath/sight.wav");
	
	self.solid = SOLID_SLIDEBOX;
	self.movetype = MOVETYPE_STEP;
	
	setmodel (self, "progs/shalrath.mdl");
	setsize (self, VEC_HULL2_MIN, VEC_HULL2_MAX);
	self.health = 400;

	self.th_stand = shal_stand;
	self.th_walk = shal_walk1;
	self.th_run = shal_run1;
	self.th_die = shalrath_die;
	self.th_pain = shalrath_pain;
	self.th_missile = shal_attack1;

	self.think = walkmonster_start;
	self.nextthink = time + 0.1 + random ()*0.1;	

};