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

DEMON

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

$cd id1/models/demon3
$scale	0.8
$origin 0 0 24
$base base
$skin base

$frame demonc
$frame demond

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

void()	Demon_JumpTouch;

void()	demon1_stand1	=[	$demonc,	demon1_stand2	] {ai_stand();};
void()	demon1_stand2	=[	$demonc,	demon1_stand3	] {ai_stand();};
void()	demon1_stand3	=[	$demonc,	demon1_stand4	] {ai_stand();};
void()	demon1_stand4	=[	$demonc,	demon1_stand5	] {ai_stand();};
void()	demon1_stand5	=[	$demonc,	demon1_stand6	] {ai_stand();};
void()	demon1_stand6	=[	$demonc,	demon1_stand7	] {ai_stand();};
void()	demon1_stand7	=[	$demonc,	demon1_stand8	] {ai_stand();};
void()	demon1_stand8	=[	$demonc,	demon1_stand9	] {ai_stand();};
void()	demon1_stand9	=[	$demonc,	demon1_stand10	] {ai_stand();};
void()	demon1_stand10	=[	$demonc,	demon1_stand11	] {ai_stand();};
void()	demon1_stand11	=[	$demonc,	demon1_stand12	] {ai_stand();};
void()	demon1_stand12	=[	$demonc,	demon1_stand13	] {ai_stand();};
void()	demon1_stand13	=[	$demonc,	demon1_stand1	] {ai_stand();};

void()	demon1_walk1	=[	$demonc,		demon1_walk2	] {
if (random() < 0.2)
    sound (self, CHAN_VOICE, "demon/idle1.wav", 1, ATTN_IDLE);
ai_walk(8);
};
void()	demon1_walk2	=[	$demonc,		demon1_walk3	] {ai_walk(6);};
void()	demon1_walk3	=[	$demonc,		demon1_walk4	] {ai_walk(6);};
void()	demon1_walk4	=[	$demonc,		demon1_walk5	] {ai_walk(7);};
void()	demon1_walk5	=[	$demonc,		demon1_walk6	] {ai_walk(4);};
void()	demon1_walk6	=[	$demonc,		demon1_walk7	] {ai_walk(6);};
void()	demon1_walk7	=[	$demonc,		demon1_walk8	] {ai_walk(10);};
void()	demon1_walk8	=[	$demonc,		demon1_walk1	] {ai_walk(10);};

void()	demon1_run1	=[	$demonc,		demon1_run2	] {
if (random() < 0.2)
    sound (self, CHAN_VOICE, "demon/idle1.wav", 1, ATTN_IDLE);
ai_run(20);};
void()	demon1_run2	=[	$demonc,		demon1_run3	] {ai_run(15);};
void()	demon1_run3	=[	$demonc,		demon1_run4	] {ai_run(36);};
void()	demon1_run4	=[	$demonc,		demon1_run5	] {ai_run(20);};
void()	demon1_run5	=[	$demonc,		demon1_run6	] {ai_run(15);};
void()	demon1_run6	=[	$demonc,		demon1_run1	] {ai_run(36);};

void()	demon1_jump1	=[	$demonc,		demon1_jump2	] {ai_face();};
void()	demon1_jump2	=[	$demonc,		demon1_jump3	] {ai_face();};
void()	demon1_jump3	=[	$demonc,		demon1_jump4	] {ai_face();};
void()	demon1_jump4	=[	$demonc,		demon1_jump5	]
{
	ai_face();
	
	self.touch = Demon_JumpTouch;
	makevectors (self.angles);
	self.origin_z = self.origin_z + 1;
	self.velocity = v_forward * 600 + '0 0 250';
	if (self.flags & FL_ONGROUND)
		self.flags = self.flags - FL_ONGROUND;
};
void()	demon1_jump5	=[	$demonc,		demon1_jump6	] {};
void()	demon1_jump6	=[	$demonc,		demon1_jump7	] {};
void()	demon1_jump7	=[	$demonc,		demon1_jump8	] {};
void()	demon1_jump8	=[ 	$demonc,		demon1_jump9	] {};
void()	demon1_jump9	=[ 	$demonc,		demon1_jump10	] {};
void()	demon1_jump10	=[ 	$demonc,	demon1_jump1	] {
self.nextthink = time + 3;
// if three seconds pass, assume demon is stuck and jump again
};

void()	demon1_jump11	=[ 	$demonc,	demon1_jump12	] {};
void()	demon1_jump12	=[ 	$demonc,	demon1_run1	] {};


void()	demon1_atta1	=[	$demonc,		demon1_atta2	] {ai_charge(4);};
void()	demon1_atta2	=[	$demonc,		demon1_atta3	] {ai_charge(0);};
void()	demon1_atta3	=[	$demonc,		demon1_atta4	] {ai_charge(0);};
void()	demon1_atta4	=[	$demonc,		demon1_atta5	] {ai_charge(1);};
void()	demon1_atta5	=[	$demonc,		demon1_atta6	] {ai_charge(2); Demon_Melee(200);};
void()	demon1_atta6	=[	$demonc,		demon1_atta7	] {ai_charge(1);};
void()	demon1_atta7	=[	$demonc,		demon1_atta8	] {ai_charge(6);};
void()	demon1_atta8	=[	$demonc,		demon1_atta9	] {ai_charge(8);};
void()	demon1_atta9	=[	$demonc,		demon1_atta10] {ai_charge(4);};
void()	demon1_atta10	=[	$demonc,		demon1_atta11] {ai_charge(2);};
void()	demon1_atta11	=[	$demonc,		demon1_atta12] {Demon_Melee(-200);};
void()	demon1_atta12	=[	$demonc,		demon1_atta13] {ai_charge(5);};
void()	demon1_atta13	=[	$demonc,		demon1_atta14] {ai_charge(8);};
void()	demon1_atta14	=[	$demonc,		demon1_atta15] {ai_charge(4);};
void()	demon1_atta15	=[	$demonc,		demon1_run1] {ai_charge(4);};

void()	demon1_pain1	=[	$demonc,		demon1_pain2	] {};
void()	demon1_pain2	=[	$demonc,		demon1_pain3	] {};
void()	demon1_pain3	=[	$demonc,		demon1_pain4	] {};
void()	demon1_pain4	=[	$demonc,		demon1_pain5	] {};
void()	demon1_pain5	=[	$demonc,		demon1_pain6	] {};
void()	demon1_pain6	=[	$demonc,		demon1_run1	] {};

void(entity attacker, float damage)	demon1_pain =
{
	if (self.touch == Demon_JumpTouch)
		return;

	if (self.pain_finished > time)
		return;

	self.pain_finished = time + 1;
    sound (self, CHAN_VOICE, "demon/dpain1.wav", 1, ATTN_NORM);

	if (random()*200 > damage)
		return;		// didn't flinch
		
	demon1_pain1 ();
};

void()	demon1_die1		=[	$demond,		demon1_die2	] {
sound (self, CHAN_VOICE, "demon/ddeath.wav", 1, ATTN_NORM);};
void()	demon1_die2		=[	$demond,		demon1_die3	] {};
void()	demon1_die3		=[	$demond,		demon1_die4	] {};
void()	demon1_die4		=[	$demond,		demon1_die5	] {};
void()	demon1_die5		=[	$demond,		demon1_die6	] {};
void()	demon1_die6		=[	$demond,		demon1_die7	]
{self.solid = SOLID_NOT;};
void()	demon1_die7		=[	$demond,		demon1_die8	] {};
void()	demon1_die8		=[	$demond,		demon1_die9	] {};
void()	demon1_die9		=[	$demond,		demon1_die9 ] {};

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

// regular death
	demon1_die1 ();
};


void() Demon_MeleeAttack =
{
	demon1_atta1 ();
};


/*UNTITLED-GAME/D monster_demon1 (1 0 0) (-32 -32 -24) (32 32 64) Ambush

*/
void() monster_demon1 =
{
	if (deathmatch)
	{
		remove(self);
		return;
	}
	precache_model ("progs/demon.mdl");
	precache_model ("progs/h_demon.mdl");

	precache_sound ("demon/ddeath.wav");
	precache_sound ("demon/dhit2.wav");
	precache_sound ("demon/djump.wav");
	precache_sound ("demon/dpain1.wav");
	precache_sound ("demon/idle1.wav");
	precache_sound ("demon/sight2.wav");

	self.solid = SOLID_SLIDEBOX;
	self.movetype = MOVETYPE_STEP;

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

	setsize (self, VEC_HULL2_MIN, VEC_HULL2_MAX);
	self.health = 300;

	self.th_stand = demon1_stand1;
	self.th_walk = demon1_walk1;
	self.th_run = demon1_run1;
	self.th_die = demon_die;
	self.th_melee = Demon_MeleeAttack;		// one of two attacks
	self.th_missile = demon1_jump1;			// jump attack
	self.th_pain = demon1_pain;
		
	walkmonster_start();
};


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

DEMON

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

/*
==============
CheckDemonMelee

Returns TRUE if a melee attack would hit right now
==============
*/
float()	CheckDemonMelee =
{
	if (enemy_range == RANGE_MELEE)
	{	// FIXME: check canreach
		self.attack_state = AS_MELEE;
		return TRUE;
	}
	return FALSE;
};

/*
==============
CheckDemonJump

==============
*/
float()	CheckDemonJump =
{
	local	vector	dist;
	local	float	d;

	if (self.origin_z + self.mins_z > self.enemy.origin_z + self.enemy.mins_z
	+ 0.75 * self.enemy.size_z)
		return FALSE;
		
	if (self.origin_z + self.maxs_z < self.enemy.origin_z + self.enemy.mins_z
	+ 0.25 * self.enemy.size_z)
		return FALSE;
		
	dist = self.enemy.origin - self.origin;
	dist_z = 0;
	
	d = vlen(dist);
	
	if (d < 100)
		return FALSE;
		
	if (d > 200)
	{
		if (random() < 0.9)
			return FALSE;
	}
		
	return TRUE;
};

float()	DemonCheckAttack =
{
	local	vector	vec;
	
// if close enough for slashing, go for it
	if (CheckDemonMelee ())
	{
		self.attack_state = AS_MELEE;
		return TRUE;
	}
	
	if (CheckDemonJump ())
	{
		self.attack_state = AS_MISSILE;
        sound (self, CHAN_VOICE, "demon/djump.wav", 1, ATTN_NORM);
		return TRUE;
	}
	
	return FALSE;
};


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

void(float side)	Demon_Melee =
{
	local	float	ldmg;
	local vector	delta;
	
	ai_face ();
	walkmove (self.ideal_yaw, 12);	// allow a little closing

	delta = self.enemy.origin - self.origin;

	if (vlen(delta) > 100)
		return;
	if (!CanDamage (self.enemy, self))
		return;
		
    sound (self, CHAN_WEAPON, "demon/dhit2.wav", 1, ATTN_NORM);
	ldmg = 10 + 5*random();
	T_Damage (self.enemy, self, self, ldmg);	

//	makevectors (self.angles);
//	SpawnMeatSpray (self.origin + v_forward*16, side * v_right);
};


void()	Demon_JumpTouch =
{
	local	float	ldmg;

	if (self.health <= 0)
		return;
		
	if (other.takedamage)
	{
		if ( vlen(self.velocity) > 400 )
		{
			ldmg = 40 + 10*random();
			T_Damage (other, self, self, ldmg);	
		}
	}

	if (!checkbottom(self))
	{
		if (self.flags & FL_ONGROUND)
		{	// jump randomly to not get hung up
//dprint ("popjump\n");
	self.touch = SUB_Null;
	self.think = demon1_jump1;
	self.nextthink = time + 0.1;

//			self.velocity_x = (random() - 0.5) * 600;
//			self.velocity_y = (random() - 0.5) * 600;
//			self.velocity_z = 200;
//			self.flags = self.flags - FL_ONGROUND;
		}
		return;	// not on ground yet
	}

	self.touch = SUB_Null;
	self.think = demon1_jump11;
	self.nextthink = time + 0.1;
};