class BabelSpectre : BabelMonster Replaces Spectre
{
	int wait;
	bool alwaysPain;
	default
	{
		RenderStyle "Translucent";
		Alpha 0.3;
		Species "Ghost";
		Health 150;
		PainChance 180;
		Speed 10;
		Radius 30;
		Height 56;
		Mass 400;
		Monster;
		+FLOORCLIP;
		SeeSound "spectre/sight";
		AttackSound "spectre/melee";
		PainSound "spectre/pain";
		DeathSound "spectre/death";
		ActiveSound "spectre/active";
		PainChance "PlayerSaw", 224;
		PainChance "PlayerPistol", 240;
		HitObituary "$OB_SPECTREHIT"; // "%o was eaten by a spectre."
		DamageType "Monster";
		Tag "Spectre (Babel)";
		
		BabelMonster.AlternatePainSound	"demon/pain2";
	}
	
	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:
		SCTR AABBCCDD 2 Fast 
		{
			A_Wander();
			A_Look();
		}
		Loop;
	Look:
		SCTR AB 10 A_Look();
		Loop;
	See:
		TNT1 A 0 
		{
			SetAlwaysPain(false);
			bNOPAIN = false;
		}
		SCTR AABBCCDD 2 Fast 
		{
			if(wait > 0)
			{
				A_Chase(null, null);
			}
			else
			{
				A_Chase();
			}
		}
		Loop;
	Melee:
		TNT1 A 0 
		{
			SetAlwaysPain(true);
			wait = 24;
		}
		SCTR EF 4 A_FaceTarget();
		SCTR G 4 A_CustomMeleeAttack(4*random[Sarg](1,10), AttackSound, "", "Monster", true);//A_SargAttack;
		Goto See;
	Pain:
		SCTR H 2 Fast
		{
			SetAlwaysPain(false);
			if(wait >= 12)
			{
				wait = 8;
			}
		}
		SCTR H 2 Fast A_BabelPain();
		TNT1 A 0 A_Jump(224, "Disappear");
		Goto See;
	Pain.PlayerSaw: //being chainsawed on prevents teleportation
		SCTR H 2 Fast;
		SCTR H 2 Fast A_BabelPain();
		Goto See;
	Disappear:
		SCTR "#" 0 
		{
			bNOPAIN = true;
		}
		SCTR "#" 1 A_SetTranslucent(0.25);
		SCTR "#" 1 A_SetTranslucent(0.2);
		SCTR "#" 1 A_SetTranslucent(0.15);
		SCTR "#" 1 A_SetTranslucent(0.1);
		SCTR "#" 1 A_SetTranslucent(0.05);
		SCTR "#" 1 A_SetTranslucent(0.0);
		
		SCTR "#" 0 
		{
			bFRIGHTENED = true;
		}
		SCTR "#####################" 0 A_Chase(null, null);
		SCTR "#" 0 
		{
			bFRIGHTENED = false;
			ResetGroupFear();
		}
		SCTR "#" 0 A_SpawnItemEx("SpectreClone",0,0,0,0,0,0,0,48|SXF_TRANSFERPOINTERS);
		SCTR "#" 0 A_Jump(192, 2);
		SCTR "#" 0 A_SpawnItemEx("SpectreClone",0,0,0,0,0,0,0,48|SXF_TRANSFERPOINTERS);
		SCTR "#####################" 0 A_Wander();
		SCTR "#" 1 A_SetTranslucent(0.05);
		SCTR "#" 1 A_SetTranslucent(0.1);
		SCTR "#" 1 A_SetTranslucent(0.15);
		SCTR "#" 1 A_SetTranslucent(0.2);
		SCTR "#" 1 A_SetTranslucent(0.25);
		SCTR "#" 1 A_SetTranslucent(0.3);
		Goto See;
	Death:
		TNT1 A 0 A_Jump(96, "Death.Monster");
		TNT1 A 0 
		{
			TriggerSync("BabelImp", 40.0);
			TriggerSync("BabelZombieMan", 25.0);
			TriggerSync("BabelChaingunGuy", 35.0);
			TriggerSync("BabelShotgunGuy", 30.0);
			TriggerSync("BabelNazi", 75.0);
		}
	Death.Monster:
	Death.Massacre:
		SCTR I 8 A_SetTranslucent(0.3); //Ensure that we reappear
		SCTR J 8 A_Scream();
		SCTR K 4 A_KillChildren;
		SCTR L 4 A_NoBlocking;
		SCTR M 4;
		SCTR N -1
		{
			SetAlwaysPain(false);
		}
		Stop;
	Raise:
		SCTR N 5;
		SCTR MLKJI 5;
		Goto See;
	}
}


class SpectreClone : BabelSpectre
{
	default
	{
		Health 1;
		Alpha 0;
		Species "Ghost";
		+NOICEDEATH;
		-COUNTKILL; 
		+NOBLOOD; 
		+LOOKALLAROUND; 
		-SOLID; 
		+THRUSPECIES; 
		-shootable; 
		+VULNERABLE; //Can be disintegrated by AoE
		//is silent
		SeeSound "";
		AttackSound "";
		PainSound "";
		DeathSound "";
		ActiveSound "";
		Tag "Spectre Clone (Babel)";
		
		BabelMonster.AlternatePainSound "";
	}
	states
	{
		Spawn:
			SCTR A 1 A_SetTranslucent(0.05);
			SCTR A 1 A_SetTranslucent(0.1);
			SCTR A 1 A_SetTranslucent(0.15);
			SCTR A 1 A_SetTranslucent(0.2);
			SCTR A 1 A_SetTranslucent(0.25);
			SCTR A 1 A_SetTranslucent(0.3);
			Goto See;
		See:
			SCTR AABBCCDD 2 Fast A_Chase();
			Loop;
		Melee:
			SCTR E 4 A_FaceTarget();
			SCTR F 4 A_FaceTarget();
			SCTR G 4;
			TNT1 A 0 A_JumpIfCloser(72, "Fade");
			Goto See;
		Death:
		Fade:
			SCTR "#" 3 A_SetTranslucent(0.2);
			SCTR "#" 3 A_SetTranslucent(0.1);
			SCTR "#" 3 A_SetTranslucent(0.0);
			Stop;
	}
}