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

SHAMBLER

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

$cd UG/models/shams
$origin 0 0 24
$base base		
$skin base

$frame shamblerc
$frame shamblerd


void() sham_stand1	=[	$shamblerc,	sham_stand2	] {ai_stand();};
void() sham_stand2	=[	$shamblerc,	sham_stand3	] {ai_stand();};
void() sham_stand3	=[	$shamblerc,	sham_stand4	] {ai_stand();};
void() sham_stand4	=[	$shamblerc,	sham_stand5	] {ai_stand();};
void() sham_stand5	=[	$shamblerc,	sham_stand6	] {ai_stand();};
void() sham_stand6	=[	$shamblerc,	sham_stand7	] {ai_stand();};
void() sham_stand7	=[	$shamblerc,	sham_stand8	] {ai_stand();};
void() sham_stand8	=[	$shamblerc,	sham_stand9	] {ai_stand();};
void() sham_stand9	=[	$shamblerc,	sham_stand10] {ai_stand();};
void() sham_stand10	=[	$shamblerc,	sham_stand11] {ai_stand();};
void() sham_stand11	=[	$shamblerc,	sham_stand12] {ai_stand();};
void() sham_stand12	=[	$shamblerc,	sham_stand13] {ai_stand();};
void() sham_stand13	=[	$shamblerc,	sham_stand14] {ai_stand();};
void() sham_stand14	=[	$shamblerc,	sham_stand15] {ai_stand();};
void() sham_stand15	=[	$shamblerc,	sham_stand16] {ai_stand();};
void() sham_stand16	=[	$shamblerc,	sham_stand17] {ai_stand();};
void() sham_stand17	=[	$shamblerc,	sham_stand1	] {ai_stand();};

void() sham_walk1		=[      $shamblerc,        sham_walk2 ] {ai_walk(10);};
void() sham_walk2       =[      $shamblerc,        sham_walk3 ] {ai_walk(9);};
void() sham_walk3       =[      $shamblerc,        sham_walk4 ] {ai_walk(9);};
void() sham_walk4       =[      $shamblerc,        sham_walk5 ] {ai_walk(5);};
void() sham_walk5       =[      $shamblerc,        sham_walk6 ] {ai_walk(6);};
void() sham_walk6       =[      $shamblerc,        sham_walk7 ] {ai_walk(12);};
void() sham_walk7       =[      $shamblerc,        sham_walk8 ] {ai_walk(8);};
void() sham_walk8       =[      $shamblerc,        sham_walk9 ] {ai_walk(3);};
void() sham_walk9       =[      $shamblerc,        sham_walk10] {ai_walk(13);};
void() sham_walk10      =[      $shamblerc,       sham_walk11] {ai_walk(9);};
void() sham_walk11      =[      $shamblerc,       sham_walk12] {ai_walk(7);};
void() sham_walk12      =[      $shamblerc,       sham_walk1 ] {ai_walk(7);
if (random() > 0.8)
	sound (self, CHAN_VOICE, "shambler/sidle.wav", 1, ATTN_IDLE);};

void() sham_run1       =[      $shamblerc,        sham_run2      ] {ai_run(20);};
void() sham_run2       =[      $shamblerc,        sham_run3      ] {ai_run(24);};
void() sham_run3       =[      $shamblerc,        sham_run4      ] {ai_run(20);};
void() sham_run4       =[      $shamblerc,        sham_run5      ] {ai_run(20);};
void() sham_run5       =[      $shamblerc,        sham_run6      ] {ai_run(24);};
void() sham_run6       =[      $shamblerc,        sham_run1      ] {ai_run(20);
if (random() > 0.8)
	sound (self, CHAN_VOICE, "shambler/sidle.wav", 1, ATTN_IDLE);
};

void() sham_smash1     =[      $shamblerc,       sham_smash2    ] {
sound (self, CHAN_VOICE, "shambler/melee1.wav", 1, ATTN_NORM);
ai_charge(2);};
void() sham_smash2     =[      $shamblerc,       sham_smash3    ] {ai_charge(6);};
void() sham_smash3     =[      $shamblerc,       sham_smash4    ] {ai_charge(6);};
void() sham_smash4     =[      $shamblerc,       sham_smash5    ] {ai_charge(5);};
void() sham_smash5     =[      $shamblerc,       sham_smash6    ] {ai_charge(4);};
void() sham_smash6     =[      $shamblerc,       sham_smash7    ] {ai_charge(1);};
void() sham_smash7     =[      $shamblerc,       sham_smash8    ] {ai_charge(0);};
void() sham_smash8     =[      $shamblerc,       sham_smash9    ] {ai_charge(0);};
void() sham_smash9     =[      $shamblerc,       sham_smash10   ] {ai_charge(0);};
void() sham_smash10    =[      $shamblerc,      sham_smash11   ] {
local vector	delta;
local float 	ldmg;

	if (!self.enemy)
		return;
	ai_charge(0);

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

	if (vlen(delta) > 100)
		return;
	if (!CanDamage (self.enemy, self))
		return;
		
	ldmg = (random() + random() + random()) * 4;
	T_Damage (self.enemy, self, self, ldmg);
	sound (self, CHAN_VOICE, "shambler/smack.wav", 1, ATTN_NORM);

//	SpawnMeatSpray (self.origin + v_forward*16, crandom() * 100 * v_right);
//	SpawnMeatSpray (self.origin + v_forward*16, crandom() * 100 * v_right);
};
void() sham_smash11    =[      $shamblerc,      sham_smash12   ] {ai_charge(5);};
void() sham_smash12    =[      $shamblerc,      sham_run1	   ] {ai_charge(4);};

void() sham_swingr1;

void(float side) ShamClaw =
{
local vector	delta;
local float 	ldmg;

	if (!self.enemy)
		return;
	ai_charge(10);

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

	if (vlen(delta) > 100)
		return;
		
	ldmg = (random() + random() + random()) * 2;
	T_Damage (self.enemy, self, self, ldmg);
	sound (self, CHAN_VOICE, "shambler/smack.wav", 1, ATTN_NORM);

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

void() sham_swingl1	=[      $shamblerc,      sham_swingl2   ] {
sound (self, CHAN_VOICE, "shambler/melee2.wav", 1, ATTN_NORM);
ai_charge(5);};
void() sham_swingl2 =[      $shamblerc,      sham_swingl3   ] {ai_charge(3);};
void() sham_swingl3 =[      $shamblerc,      sham_swingl4   ] {ai_charge(7);};
void() sham_swingl4 =[      $shamblerc,      sham_swingl5   ] {ai_charge(3);};
void() sham_swingl5 =[      $shamblerc,      sham_swingl6   ] {ai_charge(7);};
void() sham_swingl6 =[      $shamblerc,      sham_swingl7   ] {ai_charge(9);};
void() sham_swingl7 =[      $shamblerc,      sham_swingl8   ] {ai_charge(5); ShamClaw(250);};
void() sham_swingl8 =[      $shamblerc,      sham_swingl9   ] {ai_charge(4);};
void() sham_swingl9 =[      $shamblerc,      sham_run1  ] {
ai_charge(8);
if (random()<0.5)
	self.think = sham_swingr1;
};

void() sham_swingr1	=[      $shamblerc,      sham_swingr2   ] {
sound (self, CHAN_VOICE, "shambler/melee1.wav", 1, ATTN_NORM);
ai_charge(1);};
void() sham_swingr2	=[      $shamblerc,      sham_swingr3   ] {ai_charge(8);};
void() sham_swingr3 =[      $shamblerc,      sham_swingr4   ] {ai_charge(14);};
void() sham_swingr4 =[      $shamblerc,      sham_swingr5   ] {ai_charge(7);};
void() sham_swingr5 =[      $shamblerc,      sham_swingr6   ] {ai_charge(3);};
void() sham_swingr6 =[      $shamblerc,      sham_swingr7   ] {ai_charge(6);};
void() sham_swingr7 =[      $shamblerc,      sham_swingr8   ] {ai_charge(6); ShamClaw(-250);};
void() sham_swingr8 =[      $shamblerc,      sham_swingr9   ] {ai_charge(3);};
void() sham_swingr9 =[      $shamblerc,      sham_run1  ] {ai_charge(1);
ai_charge(10);
if (random()<0.5)
	self.think = sham_swingl1;
};

void() sham_melee =
{
	local float chance;
	
	chance = random();
	if (chance > 0.6 || self.health == 600)
		sham_smash1 ();
	else if (chance > 0.3)
		sham_swingr1 ();
	else
		sham_swingl1 ();
};


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

void() CastLightning =
{
	local	vector	org, dir;
	

	ai_face ();

	org = self.origin + '0 0 40';

	dir = self.enemy.origin + '0 0 16' - org;
	dir = normalize (dir);

	traceline (org, self.origin + dir*60, TRUE, self);

//	WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
//	WriteByte (MSG_BROADCAST, TE_LIGHTNING1);
//	WriteEntity (MSG_BROADCAST, self);
//	WriteCoord (MSG_BROADCAST, org_x);
//	WriteCoord (MSG_BROADCAST, org_y);
//	WriteCoord (MSG_BROADCAST, org_z);
//	WriteCoord (MSG_BROADCAST, trace_endpos_x);
//	WriteCoord (MSG_BROADCAST, trace_endpos_y);
//	WriteCoord (MSG_BROADCAST, trace_endpos_z);

	LightningDamage (org, trace_endpos, self, 10);
};

void() sham_magic1     =[      $shamblerc,       sham_magic2    ] {ai_face();
	sound (self, CHAN_WEAPON, "shambler/sattck1.wav", 1, ATTN_NORM);
};
void() sham_magic2     =[      $shamblerc,       sham_magic3    ] {ai_face();};
void() sham_magic3     =[      $shamblerc,       sham_magic4    ] {ai_face();self.nextthink = self.nextthink + 0.2;
local entity o;


ai_face();
self.owner = spawn();
o = self.owner;
// setmodel (o, "progs/s_light.mdl");
setorigin (o, self.origin);
o.angles = self.angles;
o.nextthink = time + 0.7;
o.think = SUB_Remove;
};
void() sham_magic4     =[      $shamblerc,       sham_magic5    ]
{

self.owner.frame = 1;
};
void() sham_magic5     =[      $shamblerc,       sham_magic6    ]
{

self.owner.frame = 2;
};
void() sham_magic6     =[      $shamblerc,       sham_magic9    ]
{
remove (self.owner);

CastLightning();
sound (self, CHAN_WEAPON, "shambler/sboom.wav", 1, ATTN_NORM);
};
void() sham_magic9     =[      $shamblerc,       sham_magic10   ]
{CastLightning();};
void() sham_magic10    =[      $shamblerc,      sham_magic11   ]
{CastLightning();};
void() sham_magic11    =[      $shamblerc,      sham_magic12   ]
{
if (skill == 3)
CastLightning();
};
void() sham_magic12    =[      $shamblerc,      sham_run1	   ] {};



void() sham_pain1       =[      $shamblerc, sham_pain2      ] {};
void() sham_pain2       =[      $shamblerc, sham_pain3      ] {};
void() sham_pain3       =[      $shamblerc, sham_pain4      ] {};
void() sham_pain4       =[      $shamblerc, sham_pain5      ] {};
void() sham_pain5       =[      $shamblerc, sham_pain6      ] {};
void() sham_pain6       =[      $shamblerc, sham_run1      ] {};

void(entity attacker, float damage)	sham_pain =
{
	sound (self, CHAN_VOICE, "shambler/shurt2.wav", 1, ATTN_NORM);

	if (self.health <= 0)
		return;		// allready dying, don't go into pain frame

	if (random()*40 > damage)
		return;		// didn't flinch

	if (self.pain_finished > time)
		return;
	self.pain_finished = time + 2;
		
	sham_pain1 ();
};


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

void() sham_death1      =[      $shamblerd,       sham_death2     ] {};
void() sham_death2      =[      $shamblerd,       sham_death3     ] {};
void() sham_death3      =[      $shamblerd,       sham_death4     ] {self.solid = SOLID_NOT;};
void() sham_death4      =[      $shamblerd,       sham_death5     ] {};
void() sham_death5      =[      $shamblerd,       sham_death6     ] {};
void() sham_death6      =[      $shamblerd,       sham_death7     ] {};
void() sham_death7      =[      $shamblerd,       sham_death8     ] {};
void() sham_death8      =[      $shamblerd,       sham_death9     ] {};
void() sham_death9      =[      $shamblerd,       sham_death10    ] {};
void() sham_death10     =[      $shamblerd,      sham_death11    ] {};
void() sham_death11     =[      $shamblerd,      sham_death11    ] {};

void() sham_die =
{
// check for gib
	if (self.health < -60)
	{
		sound (self, CHAN_VOICE, "player/udeath.wav", 1, ATTN_NORM);
		ThrowHead ("progs/h_shams.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, "shambler/sdeath.wav", 1, ATTN_NORM);
	sham_death1 ();
};

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


/*UNTITLED-GAMED monster_shambler (1 0 0) (-32 -32 -24) (32 32 64) Ambush
*/
void() monster_shambler =
{
	if (deathmatch)
	{
		remove(self);
		return;
	}
	precache_model ("progs/shambler.mdl");
	precache_model ("progs/s_light.mdl");
	precache_model ("progs/h_shams.mdl");
	precache_model ("progs/bolt.mdl");
	
	precache_sound ("shambler/sattck1.wav");
	precache_sound ("shambler/sboom.wav");
	precache_sound ("shambler/sdeath.wav");
	precache_sound ("shambler/shurt2.wav");
	precache_sound ("shambler/sidle.wav");
	precache_sound ("shambler/ssight.wav");
	precache_sound ("shambler/melee1.wav");
	precache_sound ("shambler/melee2.wav");
	precache_sound ("shambler/smack.wav");

	self.solid = SOLID_SLIDEBOX;
	self.movetype = MOVETYPE_STEP;
	setmodel (self, "progs/shambler.mdl");

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

	self.th_stand = sham_stand1;
	self.th_walk = sham_walk1;
	self.th_run = sham_run1;
	self.th_die = sham_die;
	self.th_melee = sham_melee;
	self.th_missile = sham_magic1;
	self.th_pain = sham_pain;
	
	walkmonster_start();
};