class BabelDemon : SyncEnabledMonster Replaces Demon
{
	bool rage;
	bool alwaysPain;
	int wait;
	default
	{
		Health 150;
		PainChance 180;
		Speed 10;
		Radius 30;
		Height 56;
		Mass 400;
		Monster;
		+FLOORCLIP;
		SeeSound "demon/sight";
		AttackSound "demon/melee";
		PainSound "demon/pain";
		DeathSound "demon/death";
		ActiveSound "demon/active";
		Obituary "$OB_DEMONHIT"; // "%o was bit by a demon."
		Species "Demon";
		//PainChance "PlayerSaw", 224;
		PainChance "PlayerPistol", 240;
		DamageType "Monster";
		Tag "Demon (Babel)";
		
		BabelMonster.FearEnabled		true;
		BabelMonster.FearThreshold		280.0;
		BabelMonster.FearMaxValue		350.0;
		BabelMonster.FearFallOff		2.0;
		BabelMonster.FearRecovery		2.5;
		BabelMonster.FearResistChance	30.0;
		BabelMonster.AlternatePainSound	"demon/pain2";
	}
	
	override void BeginPlay()
	{
		super.BeginPlay();
		alwaysPain = false;
	}
	
	override void Tick()
	{
		if(wait > 0)
		{
			wait--;
		}
		super.Tick();
	}
	
	void SetAlwaysPain(bool b)
	{
		alwaysPain = b;
	}
	
	override int DamageMobj(Actor inflictor, Actor source, int damage, Name mod, int flags, double angle)
	{
		if(Doomplayer(source)) //optimization to avoid extraneous string checking
		{
			if(alwaysPain && Distance2D(source) < 48)
			{
				TriggerPainChance(mod, true);
			}
		}
		return super.DamageMobj(inflictor, source, damage, mod, flags, angle);
	}
	
	States
	{
	Spawn:
		TNT1 A 0 Nodelay 
		{
			return ResolveState("Look");
		}
	Wander:
		SARG AABBCCDD 2 Fast
		{
			A_Wander();
			A_Look();
		}
		Loop;
	Look:
		SARG AB 10 A_Look();
		Loop;
	/* Each Frame in See should have its own state, so it can be jumped back to */
	See:
		TNT1 A 0 
		{
			SetAlwaysPain(false);
			ReleaseState();
			return JumpIfAfraid("Fear");
		}
		SARG AABBCCDD 2 Fast 
		{
			bNOPAIN = false;
			if(wait > 0)
			{
				A_Chase(null, null);
			}
			else
			{
				A_Chase();
			}
		}
		Goto See;
	//Slightly different from Imps
	Fear:
		SARG AABBCCDD 2 fast 
		{
			bFRIGHTENED = true;
			isAfraid = true;
			A_SetSpeed(13);
			if(wait > 0)
			{
				A_Chase(null, null);
			}
			else
			{
				A_Chase();
			}
		}
		TNT1 A 0 JumpIfHasFear("Fear");
		TNT1 A 0 
		{
			currentFear = 0;
			isAfraid = false;
			bFRIGHTENED = false;
			ResetGroupFear();
			A_SetSpeed(10);
		}
		Goto See;
	
	SeeFast:
		TNT1 A 0 
		{
			TriggerSync(self.GetClassName(), 25.0);
		}
	MissileNoSignal:
		TNT1 A 0
		{
			bNOPAIN = true;
			HoldState();
		}
	Rage:
		SARG AABBCCDD 1 
		{
			if(wait > 0)
			{
				A_Chase(null, null);
			}
			else
			{
				A_Chase();
			}
		}
		TNT1 A 0 A_Jump(8, "See");
		Loop;
	Melee:
		//twice as fast
		TNT1 A 0 
		{
			wait = 24;
			HoldState();
			bNOPAIN = false;
			SetAlwaysPain(true);
		}
		SARG EF 4 A_FaceTarget();
		SARG G 4 A_CustomMeleeAttack(4*random[Sarg](1,10), AttackSound, "", "Monster", true);
		Goto See;
	Pain:
		SARG H 2 Fast
		{
			SetAlwaysPain(false);
			if(wait >= 12)
			{
				wait = 8;
			}
		}
		SARG H 2 Fast A_BabelPain();
		TNT1 A 0 
		{
			currentFear += random(0, 35);
		}
		TNT1 A 0 JumpIfAfraid("Fear");
		TNT1 A 0 A_Jump(224, "SeeFast"); //small chance not to rage
		Goto See;
	Pain.PlayerSaw:
		SARG H 2 Fast;
		SARG H 2 Fast A_BabelPain();
		TNT1 A 0 
		{
			currentFear += random(0, 35);
		}
		TNT1 A 0 JumpIfAfraid("Fear");
		TNT1 A 0 A_Jump(16, "SeeFast"); //small chance to rage, chainsaw is more effective vs demons
		Goto See;
	Death:
		TNT1 A 0 A_Jump(96, "Death.Monster");
		
		TNT1 A 0 
		{
			TriggerSync(self.GetClassName(), 40.0);
			TriggerSync("BabelImp", 35.0);
			TriggerSync("BabelZombieMan", 20.0);
			TriggerSync("BabelChaingunGuy", 25.0);
			TriggerSync("BabelShotgunGuy", 30.0);
			TriggerSync("BabelNazi", 75.0);
		}
	Death.Monster:
	Death.Massacre:
		SARG I 8;
		SARG J 8 A_Scream();
		SARG K 4;
		SARG L 4 A_NoBlocking;
		SARG M 4;
		SARG N -1
		{
			SetAlwaysPain(false);
			ReleaseState();
		}
		Stop;
	Raise:
		SARG N 5
		{
			A_SetSpeed(10);
		}
		SARG MLKJI 5;
		Goto See;
	}
}
