class BabelPainElemental : ChainEnabledMonster Replaces PainElemental
{
	int user_loopCounter;
	default
	{
		Health 400;
		Radius 31;
		Height 56;
		Mass 400;
		Speed 8;
		PainChance 128;
		Monster;
		+FLOAT 
		+NOGRAVITY
		SeeSound "pain/sight";
		PainSound "pain/pain";
		DeathSound "pain/death";
		ActiveSound "pain/active";
		Species "PainElemental";
		PainChance "PlayerPistol", 160;
		DamageType "Monster";
		Tag "Pain Elemental (Babel)";
		
		BabelMonster.FearEnabled		true;
		BabelMonster.FearThreshold		450.0;
		BabelMonster.FearMaxValue		550.0;
		BabelMonster.FearFallOff		2.0;
		BabelMonster.FearRecovery		3.0;
		BabelMonster.FearResistChance	13.0;
	}
	
	States
	{
	Spawn:
		TNT1 A 0 Nodelay 
		{
			return ResolveState("Look");
		}
	Wander:
		PAIN AABBCC 30 Fast 
		{
			A_Wander();
			A_Look();
		}
		Loop;
	Look:
		PAIN A 10 A_Look();
		Loop;
	See:
		TNT1 A 0 
		{
			ReleaseState();
			return JumpIfAfraid("Fear");
		}
		PAIN AABBCC 3 Fast
		{
			A_Chase();
		}
		Goto See;
	Fear:
		PAIN AABBCC 3 
		{
			bFRIGHTENED = true;
			isAfraid = true;
			A_SetSpeed(10);
			A_Chase("Melee", null);
		}
		TNT1 A 0 JumpIfHasFear("Fear");
		TNT1 A 0 
		{
			currentFear = 0;
			isAfraid = false;
			bFRIGHTENED = false;
			ResetGroupFear();
			A_SetSpeed(8);
		}
		Goto See;
	Melee:
		PAIN D 5 
		{
			HoldState();
			A_FaceTarget();
		}
		PAIN E 5 A_FaceTarget();
		PAIN F 5 Bright 
		{
			A_CustomMeleeAttack(4*random(1,10), "imp/melee", "", "Monster"); //scratchy noise
			A_PlaySound("pain/attack", CHAN_AUTO, 1.0, false, ATTN_NORM);
			int extraDense = CVar.FindCVar("egr_particle_density").GetInt();
			if(CVar.FindCVar("egr_particle_toggle").GetInt())
			{
				double v, vx, vy, vz, dx, dy;
				for(int i = 0; i < 6+extraDense; i++) //Test count down
				{
					dx = cos(angle + frandom(-20, 20));
					dy = sin(angle + frandom(-20, 20));
					v = frandom(4, 5);
					vx = v * dx;
					vy = v * dy;
					vz = frandom(-2, 2);
					//				 Color		Flags			lifetime		size		angle	xoff			yoff				zoff					velocity	ax,ay,az alpha fade
					A_SpawnParticle (0xee0000, SPF_FULLBRIGHT, random(15,20), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  vx, vy, vz,  0,0,0, 0.98, -1);
				}
				for(int i = 0; i < 6+extraDense; i++) //Test count down
				{
					dx = cos(angle + frandom(-20, 20));
					dy = sin(angle + frandom(-20, 20));
					v = frandom(4, 5);
					vx = v * dx;
					vy = v * dy;
					vz = frandom(-2, 2);
					//				 Color		Flags			lifetime		size		angle	xoff			yoff				zoff					velocity	ax,ay,az alpha fade
					A_SpawnParticle (0xff6600, SPF_FULLBRIGHT, random(15,20), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  vx, vy, vz,  0,0,0, 0.98, -1);
				}
				for(int i = 0; i < 6+extraDense; i++) //Test count down
				{
					dx = cos(angle + frandom(-20, 20));
					dy = sin(angle + frandom(-20, 20));
					v = frandom(4, 5);
					vx = v * dx;
					vy = v * dy;
					vz = frandom(-2, 2);
					//				 Color		Flags			lifetime		size		angle	xoff			yoff				zoff					velocity	ax,ay,az alpha fade
					A_SpawnParticle (0xffaa00, SPF_FULLBRIGHT, random(15,20), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  vx, vy, vz,  0,0,0, 0.98, -1);
				}
			}
			TriggerSync("BabelLostSoul", 75.0);
		}
		Goto See;
	Missile:
		PAIN D 5
		{
			TriggerSync("BabelLostSoul", 75.0);
			HoldState();
			A_FaceTarget();
		}
		PAIN E 5 A_FaceTarget();
		PAIN F 5 Bright A_FaceTarget();
	SingleAttack:
		PAIN F 0 Bright 
		{
			TriggerChain(self.GetClassName(), 30.0);
			A_PainAttack();
		}
		Goto See;
	Pain:
		PAIN G 6;
		PAIN G 6 A_BabelPain();
		TNT1 A 0 AddFear(random(0, 30));
		TNT1 A 0 JumpIfAfraid("Fear");
		Goto See;
	Death:
		TNT1 A 0 A_Jump(160, "Death.Monster");
		TNT1 A 0 TriggerSync("BabelLostSoul", 75.0);
	Death.Monster:
	Death.Massacre:
		PAIN H 8 Bright;
		PAIN I 8 Bright A_Scream();
		PAIN J 8 Bright;
		PAIN KKKK 2 Bright
		{
			if(CVar.FindCVar("egr_particle_toggle").GetInt())
			{
				for(user_loopCounter = random(4,5); user_loopCounter > 0; user_loopCounter--) //Test count down
				{
					A_SpawnParticle (0xee0000, SPF_FULLBRIGHT, random(25,35), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  frandom(-7.0, 7.0), frandom(-7.0, 7.0), frandom(-7.0, 7.0),  0,0,0, 0.98, -1);
					A_SpawnParticle (0xff6600, SPF_FULLBRIGHT, random(25,35), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  frandom(-7.0, 7.0), frandom(-7.0, 7.0), frandom(-7.0, 7.0),  0,0,0, 0.98, -1);
					A_SpawnParticle (0xffaa00, SPF_FULLBRIGHT, random(25,35), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  frandom(-7.0, 7.0), frandom(-7.0, 7.0), frandom(-7.0, 7.0),  0,0,0, 0.98, -1);
				}
			}
		}
		PAIN L 8 Bright 
		{
			A_PainDie();
			if(CVar.FindCVar("egr_particle_toggle").GetInt())
			{
				for(user_loopCounter = random(15,20); user_loopCounter > 0; user_loopCounter--) //Test count down
				{
					A_SpawnParticle (0xee0000, SPF_FULLBRIGHT, random(25,35), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  frandom(-7.0, 7.0), frandom(-7.0, 7.0), frandom(-7.0, 7.0),  0,0,0, 0.98, -1);
					A_SpawnParticle (0xff6600, SPF_FULLBRIGHT, random(25,35), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  frandom(-7.0, 7.0), frandom(-7.0, 7.0), frandom(-7.0, 7.0),  0,0,0, 0.98, -1);
					A_SpawnParticle (0xffaa00, SPF_FULLBRIGHT, random(25,35), random(6,10), 0, frandom(-2.0, 2.0), frandom(-2.0, 2.0), frandom(-2.0, 2.0)+28,  frandom(-7.0, 7.0), frandom(-7.0, 7.0), frandom(-7.0, 7.0),  0,0,0, 0.98, -1);
				}
			}
		}
		PAIN M 8 Bright;
		Stop;
	}
}