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

BOSS-ONE

==============================================================================
*/
$cd id1/models/boss1
$origin 0 0 -15
$base base
$skin skin
$scale 5

$frame bossc
$frame bossd



void(vector p) boss_missile;

void() boss_face =
{
	
// go for another player if multi player
	if (self.enemy.health <= 0 || random() < 0.02)
	{
		self.enemy = find(self.enemy, classname, "player");
		if (!self.enemy)
			self.enemy = find(self.enemy, classname, "player");
	}
	ai_face();
};

void() boss_rise1	=[	$bossc, boss_rise2 ] {
sound (self, CHAN_WEAPON, "boss1/out1.wav", 1, ATTN_NORM);
};
void() boss_rise2	=[	$bossc, boss_rise3 ] {
sound (self, CHAN_VOICE, "boss1/sight1.wav", 1, ATTN_NORM);
};
void() boss_rise3	=[	$bossc, boss_rise4 ] {};
void() boss_rise4	=[	$bossc, boss_rise5 ] {};
void() boss_rise5	=[	$bossc, boss_rise6 ] {};
void() boss_rise6	=[	$bossc, boss_rise7 ] {};
void() boss_rise7	=[	$bossc, boss_rise8 ] {};
void() boss_rise8	=[	$bossc, boss_rise9 ] {};
void() boss_rise9	=[	$bossc, boss_rise10 ] {};
void() boss_rise10	=[	$bossc, boss_rise11 ] {};
void() boss_rise11	=[	$bossc, boss_rise12 ] {};
void() boss_rise12	=[	$bossc, boss_rise13 ] {};
void() boss_rise13	=[	$bossc, boss_rise14 ] {};
void() boss_rise14	=[	$bossc, boss_rise15 ] {};
void() boss_rise15	=[	$bossc, boss_rise16 ] {};
void() boss_rise16	=[	$bossc, boss_rise17 ] {};
void() boss_rise17	=[	$bossc, boss_missile1 ] {};

void() boss_idle1	=[	$bossc, boss_idle2 ]
{
// look for other players
};
void() boss_idle2	=[	$bossc, boss_idle3 ] {boss_face();};
void() boss_idle3	=[	$bossc, boss_idle4 ] {boss_face();};
void() boss_idle4	=[	$bossc, boss_idle5 ] {boss_face();};
void() boss_idle5	=[	$bossc, boss_idle6 ] {boss_face();};
void() boss_idle6	=[	$bossc, boss_idle7 ] {boss_face();};
void() boss_idle7	=[	$bossc, boss_idle8 ] {boss_face();};
void() boss_idle8	=[	$bossc, boss_idle9 ] {boss_face();};
void() boss_idle9	=[	$bossc, boss_idle10 ] {boss_face();};
void() boss_idle10	=[	$bossc, boss_idle11 ] {boss_face();};
void() boss_idle11	=[	$bossc, boss_idle12 ] {boss_face();};
void() boss_idle12	=[	$bossc, boss_idle13 ] {boss_face();};
void() boss_idle13	=[	$bossc, boss_idle14 ] {boss_face();};
void() boss_idle14	=[	$bossc, boss_idle15 ] {boss_face();};
void() boss_idle15	=[	$bossc, boss_idle16 ] {boss_face();};
void() boss_idle16	=[	$bossc, boss_idle17 ] {boss_face();};
void() boss_idle17	=[	$bossc, boss_idle18 ] {boss_face();};
void() boss_idle18	=[	$bossc, boss_idle19 ] {boss_face();};
void() boss_idle19	=[	$bossc, boss_idle20 ] {boss_face();};
void() boss_idle20	=[	$bossc, boss_idle21 ] {boss_face();};
void() boss_idle21	=[	$bossc, boss_idle22 ] {boss_face();};
void() boss_idle22	=[	$bossc, boss_idle23 ] {boss_face();};
void() boss_idle23	=[	$bossc, boss_idle24 ] {boss_face();};
void() boss_idle24	=[	$bossc, boss_idle25 ] {boss_face();};
void() boss_idle25	=[	$bossc, boss_idle26 ] {boss_face();};
void() boss_idle26	=[	$bossc, boss_idle27 ] {boss_face();};
void() boss_idle27	=[	$bossc, boss_idle28 ] {boss_face();};
void() boss_idle28	=[	$bossc, boss_idle29 ] {boss_face();};
void() boss_idle29	=[	$bossc, boss_idle30 ] {boss_face();};
void() boss_idle30	=[	$bossc, boss_idle31 ] {boss_face();};
void() boss_idle31	=[	$bossc, boss_idle1 ] {boss_face();};

void() boss_missile1	=[	$bossc, boss_missile2 ] {boss_face();};
void() boss_missile2	=[	$bossc, boss_missile3 ] {boss_face();};
void() boss_missile3	=[	$bossc, boss_missile4 ] {boss_face();};
void() boss_missile4	=[	$bossc, boss_missile5 ] {boss_face();};
void() boss_missile5	=[	$bossc, boss_missile6 ] {boss_face();};
void() boss_missile6	=[	$bossc, boss_missile7 ] {boss_face();};
void() boss_missile7	=[	$bossc, boss_missile8 ] {boss_face();};
void() boss_missile8	=[	$bossc, boss_missile9 ] {boss_face();};
void() boss_missile9	=[	$bossc, boss_missile10 ] {boss_missile('100 100 200');};
void() boss_missile10	=[	$bossc, boss_missile11 ] {boss_face();};
void() boss_missile11	=[	$bossc, boss_missile12 ] {boss_face();};
void() boss_missile12	=[	$bossc, boss_missile13 ] {boss_face();};
void() boss_missile13	=[	$bossc, boss_missile14 ] {boss_face();};
void() boss_missile14	=[	$bossc, boss_missile15 ] {boss_face();};
void() boss_missile15	=[	$bossc, boss_missile16 ] {boss_face();};
void() boss_missile16	=[	$bossc, boss_missile17 ] {boss_face();};
void() boss_missile17	=[	$bossc, boss_missile18 ] {boss_face();};
void() boss_missile18	=[	$bossc, boss_missile19 ] {boss_face();};
void() boss_missile19	=[	$bossc, boss_missile20 ] {boss_face();};
void() boss_missile20	=[	$bossc, boss_missile21 ] {boss_missile('100 -100 200');};
void() boss_missile21	=[	$bossc, boss_missile22 ] {boss_face();};
void() boss_missile22	=[	$bossc, boss_missile23 ] {boss_face();};
void() boss_missile23	=[	$bossc, boss_missile1 ] {boss_face();};

void() boss_shocka1 =[	$bossc, boss_shocka2 ] {};
void() boss_shocka2 =[	$bossc, boss_shocka3 ] {};
void() boss_shocka3 =[	$bossc, boss_shocka4 ] {};
void() boss_shocka4 =[	$bossc, boss_shocka5 ] {};
void() boss_shocka5 =[	$bossc, boss_shocka6 ] {};
void() boss_shocka6 =[	$bossc, boss_shocka7 ] {};
void() boss_shocka7 =[	$bossc, boss_shocka8 ] {};
void() boss_shocka8 =[	$bossc, boss_shocka9 ] {};
void() boss_shocka9 =[	$bossc, boss_shocka10 ] {};
void() boss_shocka10 =[	$bossc, boss_missile1 ] {};

void() boss_shockb1 =[	$bossc, boss_shockb2 ] {};
void() boss_shockb2 =[	$bossc, boss_shockb3 ] {};
void() boss_shockb3 =[	$bossc, boss_shockb4 ] {};
void() boss_shockb4 =[	$bossc, boss_shockb5 ] {};
void() boss_shockb5 =[	$bossc, boss_shockb6 ] {};
void() boss_shockb6 =[	$bossc, boss_shockb7 ] {};
void() boss_shockb7 =[	$bossc, boss_shockb8 ] {};
void() boss_shockb8 =[	$bossc, boss_shockb9 ] {};
void() boss_shockb9 =[	$bossc, boss_shockb10 ] {};
void() boss_shockb10 =[	$bossc, boss_missile1 ] {};

void() boss_shockc1 =[	$bossc, boss_shockc2 ] {};
void() boss_shockc2 =[	$bossc, boss_shockc3 ] {};
void() boss_shockc3 =[	$bossc, boss_shockc4 ] {};
void() boss_shockc4 =[	$bossc, boss_shockc5 ] {};
void() boss_shockc5 =[	$bossc, boss_shockc6 ] {};
void() boss_shockc6 =[	$bossc, boss_shockc7 ] {};
void() boss_shockc7 =[	$bossc, boss_shockc8 ] {};
void() boss_shockc8 =[	$bossc, boss_shockc9 ] {};
void() boss_shockc9 =[	$bossc, boss_shockc10 ] {};
void() boss_shockc10 =[	$bossc, boss_death1 ] {};

void() boss_death1 = [$bossd, boss_death2] {
sound (self, CHAN_VOICE, "boss1/death.wav", 1, ATTN_NORM);
};
void() boss_death2 = [$bossd, boss_death3] {};
void() boss_death3 = [$bossd, boss_death4] {};
void() boss_death4 = [$bossd, boss_death5] {};
void() boss_death5 = [$bossd, boss_death6] {};
void() boss_death6 = [$bossd, boss_death7] {};
void() boss_death7 = [$bossd, boss_death8] {};
void() boss_death8 = [$bossd, boss_death9] {};
void() boss_death9 = [$bossd, boss_death10]
{
	sound (self, CHAN_BODY, "boss1/out1.wav", 1, ATTN_NORM);
//	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
//	WriteByte (MSG_BROADCAST, TE_LAVASPLASH);
//	WriteCoord (MSG_BROADCAST, self.origin_x);
//	WriteCoord (MSG_BROADCAST, self.origin_y);
//	WriteCoord (MSG_BROADCAST, self.origin_z);
};

void() boss_death10 = [$bossd, boss_death10]
{
	killed_monsters = killed_monsters + 1;
//	WriteByte (MSG_ALL, SVC_KILLEDMONSTER);	// FIXME: reliable broadcast
	SUB_UseTargets ();
	remove (self);
};

void(vector p) boss_missile =
{
	local	vector	offang;
	local	vector	org, vec, d;
	local	float	t;

	offang = vectoangles (self.enemy.origin - self.origin);	
	makevectors (offang);

	org = self.origin + p_x*v_forward + p_y*v_right + p_z*'0 0 1';
	
// lead the player on hard mode
	if (skill > 1)
	{
		t = vlen(self.enemy.origin - org) / 300;
		vec = self.enemy.velocity;
		vec_z = 0;
		d = self.enemy.origin + t * vec;		
	}
	else
	{
		d = self.enemy.origin;
	}
	
	vec = normalize (d - org);

	launch_spike (org, vec);
//	setmodel (newmis, "progs/lavaball.mdl");
	newmis.avelocity = '200 100 300';
	setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);		
	newmis.velocity = vec*300;
	newmis.touch = T_MissileTouch; // rocket explosion
	sound (self, CHAN_WEAPON, "boss1/throw.wav", 1, ATTN_NORM);

// check for dead enemy
	if (self.enemy.health <= 0)
		boss_idle1 ();
};


void() boss_awake =
{
	self.solid = SOLID_SLIDEBOX;
	self.movetype = MOVETYPE_STEP;
	self.takedamage = DAMAGE_NO;
	
	setmodel (self, "progs/boss.mdl");
	setsize (self, '-128 -128 -24', '128 128 256');
	
	if (skill == 0)
		self.health = 1;
	else
		self.health = 3;

	self.enemy = activator;

	//WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
	//WriteByte (MSG_BROADCAST, TE_LAVASPLASH);
	//WriteCoord (MSG_BROADCAST, self.origin_x);
	//WriteCoord (MSG_BROADCAST, self.origin_y);
	//WriteCoord (MSG_BROADCAST, self.origin_z);

	self.yaw_speed = 20;
	boss_rise1 ();
};


/*UNTITLED-GAME/D monster_boss (1 0 0) (-128 -128 -24) (128 128 256)
*/
void() monster_boss =
{
	if (deathmatch)
	{
		remove(self);
		return;
	}
	precache_model ("progs/boss.mdl");
	precache_model ("progs/lavaball.mdl");

	precache_sound ("weapons/rocket1i.wav");
	precache_sound ("boss1/out1.wav");
	precache_sound ("boss1/sight1.wav");
	precache_sound ("misc/power.wav");
	precache_sound ("boss1/throw.wav");
	precache_sound ("boss1/pain.wav");
	precache_sound ("boss1/death.wav");

	total_monsters = total_monsters + 1;

	self.use = boss_awake;
};

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

entity	le1, le2;
float	lightning_end;

void() lightning_fire =
{
	local vector p1, p2;
	
	if (time >= lightning_end)
	{	// done here, put the terminals back up
		self = le1;
		door_go_down ();
		self = le2;
		door_go_down ();
		return;
	}
	
	p1 = (le1.mins + le1.maxs) * 0.5;
	p1_z = le1.absmin_z - 16;
	
	p2 = (le2.mins + le2.maxs) * 0.5;
	p2_z = le2.absmin_z - 16;
	
	// compensate for length of bolt
	p2 = p2 - normalize(p2-p1)*100;

	self.nextthink = time + 0.1;
	self.think = lightning_fire;

	//WriteByte (MSG_ALL, SVC_TEMPENTITY);
	//WriteByte (MSG_ALL, TE_LIGHTNING3);
	//WriteEntity (MSG_ALL, world);
	//WriteCoord (MSG_ALL, p1_x);
	//WriteCoord (MSG_ALL, p1_y);
	//WriteCoord (MSG_ALL, p1_z);
	//WriteCoord (MSG_ALL, p2_x);
	//WriteCoord (MSG_ALL, p2_y);
	//WriteCoord (MSG_ALL, p2_z);
};

void() lightning_use =
{
	if (lightning_end >= time + 1)
		return;

	le1 = find( world, target, "lightning");
	le2 = find( le1, target, "lightning");
	if (!le1 || !le2)
	{
		dprint ("------- ------- -------\n");
		return;
	}
	
	if (
	 (le1.state != STATE_TOP && le1.state != STATE_BOTTOM)
	|| (le2.state != STATE_TOP && le2.state != STATE_BOTTOM)
	|| (le1.state != le2.state) )
	{
//		dprint ("not aligned\n");
		return;
	}

// don't let the electrodes go back up until the bolt is done
	le1.nextthink = -1;
	le2.nextthink = -1;
	lightning_end = time + 1;

	sound (self, CHAN_VOICE, "misc/power.wav", 1, ATTN_NORM);
	lightning_fire ();		

// advance the boss pain if down
	self = find (world, classname, "monster_boss");
	if (!self)
		return;
	self.enemy = activator;
	if (le1.state == STATE_TOP && self.health > 0)
	{
		sound (self, CHAN_VOICE, "boss1/pain.wav", 1, ATTN_NORM);
		self.health = self.health - 1;
		if (self.health >= 2)
			boss_shocka1();
		else if (self.health == 1)
			boss_shockb1();
		else if (self.health == 0)
			boss_shockc1();
	}	
};


/*UNTITLED-GAME/D event_lightning (0 1 1) (-16 -16 -16) (16 16 16)
Just for boss level.
*/
void() event_lightning =
{
	self.use = lightning_use;
};