class BabelHellKnight : SyncEnabledMonster Replaces HellKnight
{
	//Since this doesn't derive from the base monster we have to re-define all its defaults
	int user_loopCounter;
	bool leftHandAttack;
	default
	{
		Radius 24;
		Height 64;
		Mass 1000;
		Monster;
		+FLOORCLIP
		Health 500;
		Speed 10;
		PainChance 50;
		-BOSSDEATH;
		+MISSILEEVENMORE;
		BloodColor "green";
		Species "Baron";
		SeeSound "knight/sight";
		ActiveSound "knight/active";
		PainSound "knight/pain";
		DeathSound "knight/death";
		HitObituary "$OB_KNIGHTHIT";
		Obituary "$OB_KNIGHT";
		PainChance "PlayerPistol", 75;
		DamageType "Monster";
		Tag "Hell Knight (Babel)";
		
		BabelMonster.FearEnabled		true;
		BabelMonster.FearThreshold		700.0;
		BabelMonster.FearMaxValue		950.0;
		BabelMonster.FearFallOff		2.0;
		BabelMonster.FearRecovery		6.0;
		BabelMonster.FearResistChance	12.0;
		BabelMonster.SyncChainCheckLOS	true;
	}
	
	override void BeginPlay()
	{
		super.BeginPlay();
		leftHandAttack = false;
	}
	
	States
	{
	Spawn:
		TNT1 A 0 Nodelay 
		{
			return ResolveState("Look");
		}
	Wander:
		BOS2 AABBCCDD 3 Fast 
		{
			A_Wander();
			A_Look();
		}
		Loop;
	Look:
		BOS2 AB 10 A_Look();
		Loop;
	See:
		TNT1 A 0 
		{
			ReleaseState(); //Always always ALWAYS release your state lock when you start moving, or else you'll be unable to sync fire
			return JumpifAfraid("Fear");
		}
		BOS2 AABBCCDD 3 Fast
		{
			bNOPAIN = false; //Hell Knights can resist pain sometimes, so we make sure to reset the NOPAIN flag here
			A_Chase();
		}
		Goto See;
	//Fear state, required for any monster with fear
	Fear:
		BOS2 AABBCCDD 3 Fast
		{
			//This sets the monster to run away
			bFRIGHTENED = true;
			//This tells the Babel AI that the monster is afraid
			isAfraid = true;
			//When running away we move faster
			A_SetSpeed(13);
			//Monsters that are afraid should not shoot, but can melee
			A_Chase("Melee", null);
		}
		//If we're still afraid, keep running
		TNT1 A 0 JumpIfHasFear("Fear");
		//If not, set everything back
		TNT1 A 0 
		{
			currentFear = 0;
			isAfraid = false;
			bFRIGHTENED = false;
			ResetGroupFear(); //This resets the group fear stagger, see babel_monster_base.txt
			A_SetSpeed(10);
		}
		Goto See;
	Melee:
	Missile:
		//Hell knight attack enrages lower enemies
		TNT1 A 0 
		{
			TriggerSync("BabelImp", 45.0);
			TriggerSync("BabelDemon", 40.0);
			TriggerSync("BabelLostSoul", 75.0);
		}
	//MissileNoSignal state is required for enemies which can sync fire, and should not ever contain TriggerSync() calls to avoid infinite loops
	//They should also always contain checks to see if it's possible to see our target. If not, cancel the attack
	MissileNoSignal:
		//These next two lines will be condensed later, but for now what they do is ensure we can actually see the target when sync firing.
		TNT1 A 0 JumpIfCannotSee("See");
		TNT1 A 0 
		{
			HoldState(); //We're attacking now, so hold our state and don't re-sync
			bNOPAIN = true; //Hell knight attacks cannot be interrupted
		}
		TNT1 A 0
		{
			return leftHandAttack ? ResolveState("LeftMissile") : ResolveState("RightMissile"); //Depending on if we did a left or right attack last, pick a hand to attack with
		}
		//A_Jump(256, "RightMissile", "LeftMissile"); //Jump to a left-handed or right-handed attack
	RightMissile:
		BOS2 EF 6 A_FaceTarget();
		BOS2 G 6
		{
			/* Removes 100 fear */
			GroupFearReduction(100, "Babelmonster");
			A_CustomComboAttack("ParticleKnightBall", 32, 10 * random(1, 8), "baron/melee", "Monster");
			leftHandAttack = !leftHandAttack;
		}
		TNT1 A 0 A_MonsterRefire(32, "See"); //Do a refire check, if we can still see our opponent, go to next line, if not, roll a low-chance roll to continue firing anyway
		TNT1 A 0 A_Jump(96, "LeftMissile"); // 96/256 chance to follow up attack
		Goto See;
	LeftMissile:
		BOS2 PQ 6 A_FaceTarget();
		BOS2 R 6
		{
			/* Removes 100 fear */
			GroupFearReduction(100, "Babelmonster");
			A_CustomComboAttack("ParticleKnightBall", 32, 10 * random(1, 8), "baron/melee", "Monster");
			leftHandAttack = !leftHandAttack;
		}
		TNT1 A 0 A_MonsterRefire(32, "See"); //Do a refire check, if we can still see our opponent, go to next line, if not, roll a low-chance roll to continue firing anyway
		TNT1 A 0 A_Jump(96, "RightMissile"); // 96/256 chance to follow up attack
		Goto See;
	Pain:
		BOS2 H 2 Fast;
		BOS2 H 2 Fast A_BabelPain();
		TNT1 A 0 AddFear(random(0,95));
		TNT1 A 0 JumpifAfraid("Fear");
		Goto See;
	Death.Massacre: //Skip any effects like gore if we've MDK'd
		BOS2 I 8;
		BOS2 J 8 A_Scream();
		BOS2 K 8;
		BOS2 L 8 A_NoBlocking;
		BOS2 MN 8;
		BOS2 O -1;
		Stop;
	//Non-monster damage types cause death events to trigger fear and, in the case of the hell knight, sync attacks
	Death:
		TNT1 A 0 A_Jump(224, "Death.Monster");
		TNT1 A 0 
		{
			TriggerSync(self.GetClassName(), 80.0);
			TriggerSync("BabelImp", 50.0);
			TriggerSync("BabelDemon", 40.0);
			TriggerSync("BabelCacodemon", 50.0);
			TriggerSync("BabelZombieMan", 20.0);
			TriggerSync("BabelChaingunGuy", 25.0);
			TriggerSync("BabelShotgunGuy", 25.0);
			TriggerSync("BabelNazi", 75.0);
		}
	Death.Monster:
		//In here are a bunch of gore effects, which get progressively more extreme based on user settings
		BOS2 I 8
		{
			if(CVar.FindCVar("egr_gore").GetInt())
			{
				for(user_loopCounter = 0; user_loopCounter < 3; user_loopCounter++)
				{
					A_SpawnItemEx("GreenBloodSpew", random(-10, 10), random(-10, 10), random(48, 64));
				}
			}
		}
		BOS2 J 8 
		{
			A_Scream();
			if(CVar.FindCVar("egr_gore").GetInt())
			{
				for(user_loopCounter = 0; user_loopCounter < 3; user_loopCounter++)
				{
					A_SpawnItemEx("GreenBloodSpew", random(-10, 10), random(-10, 10), random(32, 56));
				}
			}
		}
		BOS2 K 8
		{
			if(CVar.FindCVar("egr_gore").GetInt())
			{
				for(user_loopCounter = 0; user_loopCounter < 3; user_loopCounter++)
				{
					A_SpawnItemEx("GreenBloodSpew", random(-10, 10), random(-10, 10), random(24, 48));
				}
			}
		}
		BOS2 L 8 A_NoBlocking;
		BOS2 MN 8;
		BOS2 O -1;
		Stop;
	Raise:
		BOS2 O 8
		{
			A_SetSpeed(10); //If we got killed while afraid this might have been too high, so reset it
		}
		BOS2 NMLKJI 8;
		Goto See;
	}
}


class ParticleKnightBall : ParticleBaronBall
{
	default
	{
		Damage 14;
		+STRIFEDAMAGE;
	}
}