class FloaterblastActive : Inventory { Default { Inventory.MaxAmount 1; } }
class AnotherIronblast : Inventory { Default { Inventory.MaxAmount 1; } }

// Item given upon strat activation; the one that resides in your inventory when haunted Ironblast is doing its business.
class IronblastRevert : CustomInventory
{
	Default
	{
		Inventory.Amount 1;
		Inventory.MaxAmount 1;
		Inventory.Icon "ARTIPIRN";
		Inventory.PickupSound "";
		Inventory.UseSound "";
		Inventory.PickupMessage "You weren't supposed to pick it up like that, cringe-worthy mortal.";
		Tag "Ironblast Return";
		+INVENTORY.INVBAR
	}
	
	States
	{
	Spawn:
		IRNG X -1;
		Stop;
	Use:
		TNT1 A 0 A_TakeInventory("IronblastRevert");
		Stop;
	}
}

// Item given upon strat activation/deactivation.
class IronblastMissionControl : CustomInventory
{
	Default
	{
		Inventory.Amount 1;
		Inventory.MaxAmount 1;
		Inventory.UseSound "";
		Inventory.PickupMessage "How the hell did you do that, silly mortal?";
		+INVENTORY.UNDROPPABLE
	}
	
	States
	{
	Spawn:
		TNT1 A -1;
		Stop;
	Use:
		TNT1 A 0 A_JumpIf(CountInv("IronblastRevert"),"Use2");
		TNT1 A 0 A_JumpIf(!CountInv("FloaterblastActive") && CountInv("Ironblast") && CountInv("IronblastStrat"),1);
		Fail;
		TNT1 A 0 {
			A_SetBlend("Black",1.0,25);
			if(GetCvar("GC_QuakeRecoil")) { A_QuakeEx(8, 8, 8, 15, 0, 64, "none", QF_SCALEDOWN); }
			A_FireProjectile("FloatingIronblast",0,0,0,0);
			A_GiveInventory("IronblastStratActive");
			A_TakeInventory("Ironblast");
		}
		Fail;
	Use2:
		TNT1 A 0 A_TakeInventory("IronblastRevert");
		Fail;
	}
}	

class FloatingIronblast : Actor
{
	Default
	{
		FloatingIronblast.XPos 0.0; // Unless you like disasters you don't understand. Non-defined number sure as hell ain't null.
		FloatingIronblast.YPos -55.0;
		FloatingIronblast.ZPos 0.0;
		FloatingIronblast.SecondOne 0; // Notifies the gun that it's second one, so it doesn't play voicelines and changes weapon position.
		FloatingIronblast.Mute 0;
		Speed 0;
		Scale 0.7;
		Projectile;
		+NOINTERACTION
		+NOGRAVITY
		+NOCLIP
		+FLOATBOB
		+SEEKERMISSILE
		+FORCEXYBILLBOARD
		+NOTIMEFREEZE
		+DONTBLAST
	}
	
	// Why use this? Well, I need those variables right off the bat,
	// and I hope these are around soon enough for the actor to catch up on it.
	property XPos: User_XPos;
	property YPos: User_YPos;
	property ZPos: User_ZPos;
	property SecondOne: User_Brudda;
	property Mute: User_Muted;
	
	int User_Cycle;			// Used for delaying the weapon where necessary.
	int User_Idling;		// If inactive for long enough - crack a oneliner.
	float User_XPos;		// I think this is pretty self-explanatory.
	float User_YPos;
	float User_ZPos;
	int User_Shots;			// Internal counter for the projectile and casing spawning.
	int User_ShotCount;		// Counter for how many times it fired so it can taunt eventually.
	int User_SupermagShots;	// Supermag Johnson's clip.
	int User_Brudda;		// Is it its carbon copy?
	int User_Muted;			// Repeat after me: MHMPHMPHHH I'M DEAD!
	
	// SHOTGUNS ARE GOOD, THE PINGAS IS BAD~
	override void Tick() {
		A_SeekerMissile(0,0,SMF_LOOK|SMF_CURSPEED,256,4);	// This is what makes shotgnu see and pick targets.
		if(User_Cycle > 0) { User_Cycle--; }				// Delay cycler.
		A_Warp(AAPTR_TARGET,								// What warp to, pointer-wise?
			45.0 + User_XPos,								// X position.
			User_YPos,										// Y position.
			35.0 + (User_ZPos + CountInv("PowerDeadmaker") * 24.0),	//Z position.
			0,												// Angle.
			WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION|WARPF_BOB); // Warp flags.
		A_SpawnItemEx("IronblastPossession",0,0,0,0,0,0,0,SXF_CLIENTSIDE); //Effect spawn.
		If(!User_Brudda &&									// It's not another Ironblast.
		   !CountInv("AnotherIronblast",AAPTR_TARGET) &&	// We didn't get another Ironblast spawned.
		   (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking",AAPTR_TARGET))) { //We have gunzerk active.
				A_SpawnItemEx("FloatingIronblast2",0,0,0,0,0,0,0,SXF_TRANSFERPOINTERS,0); // Summon the mate, mate.
				A_GiveInventory("AnotherIronblast",1,AAPTR_TARGET);
		}
		A_FaceTracer();										// Face the maker.
		Super.Tick();
		return;
	}
		
	States
	{
	Spawn:
		"####" "#" 0 NoDelay {
			User_SupermagShots = 0;
			A_GiveInventory("IronblastRevert",1,AAPTR_TARGET);
			A_GiveInventory("FloaterblastActive",1,AAPTR_TARGET);
			A_SpawnItemEx("IronblastRelease",0,0,0,0,0,0,0,SXF_CLIENTSIDE);
			A_PlaySound("Ironspirit/Spawn",6);
			If((Target && !Target.GetCvar("GC_MuteShotgun")) && !User_Muted) { A_PlaySound("Ironspirit/Ready",7,0.8); }
			A_PlaySound("Ironspirit/Loop",4,0.2,1);
		}
	Main:
		"####" "#" 0 { If(User_Brudda && !(CountInv("PowerGunzerking", AAPTR_TARGET) || GetCvar("GC_Gunslinger"))) { Return ResolveState("Death"); } Return ResolveState(Null); }
		"####" "#" 0 A_JumpIf(CountInv("IronblastRevert",AAPTR_TARGET) || (User_Brudda && CountInv("AnotherIronblast")),1); // Despite the name, this is your "it's active" item, not a one that kills it. Just a friendly reminder to yourself.
		Goto Death;
	Idle:
		PIRN A 0 {
			If((Target && !Target.GetCvar("GC_MuteShotgun")) && !User_Muted) { User_Idling++; }
			if((Target && Target is "Draugh") ||
			   ((CountInv("HexenCounter",AAPTR_TARGET) && CountInv("GreenMana",AAPTR_TARGET) >= 8)) ||
			   (!CountInv("HexenCounter",AAPTR_TARGET) && CountInv("ShotgunAmmo",AAPTR_TARGET) >= 8))
			{
				Return CurState+2;
			}
			Return ResolveState(Null);
		}
		PIRN F 0;
		"####" "#" 1 A_JumpIf(User_Idling > 420,"IdleChatter");
		"####" "#" 0 A_JumpIf(CheckIfTargetInLOS(0, JLOSF_PROJECTILE, 64*6, 0) && Tracer && Tracer.bFRIENDLY == 0 && !(Tracer is "PlayerPawn"), "FireYourGuns");
		"####" "#" 0 A_RearrangePointers(AAPTR_DEFAULT,AAPTR_DEFAULT,AAPTR_NULL);
		Goto Main;
	IdleChatter:
		PIRN A 0 {
			if((Target && Target is "Draugh") ||
			  (CountInv("HexenCounter",AAPTR_TARGET) && CountInv("GreenMana",AAPTR_TARGET) >= 8) ||
			  (!CountInv("HexenCounter",AAPTR_TARGET) && CountInv("ShotgunAmmo",AAPTR_TARGET) >= 8))
			{
				if(random(1,0) == 1)
				{
					A_PlaySound("Ironspirit/Idle",7,0.8);
				}
				User_Idling = 0;
				Return ResolveState(Null);
			}
			if(random(1,0) == 1)
			{
				A_PlaySound("Ironspirit/Nope",7,0.8);
			}
			User_Idling = 0;
			Return ResolveState(Null);
		}
		Goto Main;
	FireYourGuns:
		"####" "#####" 1;
		"####" "#" 0 {
			If((Target && Target is "Draugh") ||
			  (CountInv("HexenCounter",AAPTR_TARGET) && CountInv("GreenMana",AAPTR_TARGET) >= 8) ||
			  (!CountInv("HexenCounter",AAPTR_TARGET) && CountInv("ShotgunAmmo",AAPTR_TARGET) >= 8))
			{
				Return ResolveState("ImmaShootingMalaysia");
			}
			Return ResolveState("Main");
		}
		Goto Main;
	ImmaShootingMalaysia:
		PIRN E 1 Bright {
			If(CountInv("HexenCounter",AAPTR_TARGET))
			{
				A_TakeInventory("GreenMana",8,TIF_NOTAKEINFINITE,AAPTR_TARGET);
			}
			Else
			{
				A_TakeInventory("ShotgunAmmo",8,TIF_NOTAKEINFINITE,AAPTR_TARGET);
			}
			if(CountInv("SupermagJohnson",AAPTR_TARGET))
			{
				User_SupermagShots++;
			}
			A_AlertMonsters();
			A_FaceTracer();
			User_Idling = 0;
			If((Target && !Target.GetCvar("GC_MuteShotgun")) && !User_Muted) { User_ShotCount++; }
			A_PlaySound("Ironblast/Fire",1,0.5);
			A_PlaySound("Ironblast/Add",5,0.5);
			for(User_Shots = 0; User_Shots < 22; User_Shots++)
			{
				A_SpawnProjectile("IronblastProjectile",0,0,frandom(8.5,-8.5),CMF_TRACKOWNER|CMF_OFFSETPITCH,frandom(3.0,-3.0),AAPTR_TRACER);
			}
		}
		PIRN E 1 Bright { User_XPos = -14.0; }
		PIRN E 1 Bright { User_XPos = -9.0; }
		PIRN E 1 Bright { User_XPos = -6.0; }
		PIRN E 1 Bright { User_XPos = -3.0; }
		PIRN E 1 Bright { User_XPos = -2.0; }
		PIRN E 1 Bright { User_XPos = -1.0; }
		PIRN E 1 Bright { User_XPos =  0.0; }
	FiringCycleA:
		"####" A 0 { User_Cycle = 14; }
	FiringA:
		"####" "#" 1 { User_Cycle--; }
		"####" "#" 0 A_JumpIf(User_Cycle > 0, "FiringA");
	FiringCycleB:
		"####" B 0 {
			User_Cycle = 10;
			If(User_ShotCount >= 4) {
				A_PlaySound("Ironspirit/Attack",7,1.0);
				User_ShotCount = 0;
			}
		}
	FiringB:
		"####" "#" 1 { User_Cycle--; }
		"####" "#" 0 A_JumpIf(User_Cycle > 0, "FiringB");
	FiringCycleC:
		"####" C 0 { User_Cycle = 7; }
	FiringC:
		"####" "#" 1 { User_Cycle--; }
		"####" "#" 0 A_JumpIf(User_Cycle > 0, "FiringC");
		"####" "#" 0 A_JumpIf(CountInv("SupermagJohnson",AAPTR_TARGET),1);
		Goto ReloadCycle1;
		"####" "#" 0 A_JumpIf(User_SupermagShots >= 3, 1);
		Goto Main;
	ReloadCycle1:
		"####" D 0 {
			A_PlaySound("Ironblast/Pump/In",6,0.6);
			if(CountInv("SupermagJohnson",AAPTR_TARGET))
			{
				User_SupermagShots = 0;
				User_Shots = 12;
				User_Cycle = 28;
			}
			else
			{
				User_Shots = 4;
				User_Cycle = 14;
			}
		}
	Reload1:
		"####" "#" 1 {
			if(CountInv("SupermagJohnson",AAPTR_TARGET))
			{
				User_ZPos = User_ZPos - 0.71;
			}
			else
			{
				User_ZPos = User_ZPos - 1.42;
			}
			if(User_Shots) {
				for(int user_butt = 0; user_butt < 2; user_butt++)
				{
					A_SpawnItemEx("ShottyCasing",0,0,0,frandom(2.0,-2.0),0,frandom(8.0,3.0),frandom(0.0,360.0),SXF_CLIENTSIDE);
				}
				User_Shots--;
			}
			User_Cycle--;
		}
		"####" "#" 0 A_JumpIf(User_Cycle > 0, "Reload1");
		"####" "#" 10;
	ReloadCycle2:
		"####" C 0 { 
			A_PlaySound("Ironblast/Pump/Out",6,0.6);
			User_Cycle = 10;
		}
	Reload2:
		"####" "#" 1 {
			User_ZPos = User_ZPos + 2.0;
			User_Cycle--;
		}
		"####" "#" 0 A_JumpIf(User_Cycle > 0, "Reload2");
	ReloadCycle3:
		"####" B 0 { User_Cycle = 9; }
	Reload3:
		"####" "#" 1 { User_Cycle--; }
		"####" "#" 0 A_JumpIf(User_Cycle > 0, "Reload3");
		"####" "#" 0 A_RearrangePointers(AAPTR_DEFAULT,AAPTR_DEFAULT,AAPTR_NULL);
		Goto Main;
	Death:
		"####" "#" 0 {
			A_PlaySound("Ironspirit/Release",4,1.0);
			If((Target && !Target.GetCvar("GC_MuteShotgun")) && !User_Muted) { A_PlaySound("Ironspirit/End",7,0.8); }
			A_SpawnItemEx("IronblastRelease",0,0,0,0,0,0,0,SXF_CLIENTSIDE);	
			If(User_Brudda) { A_TakeInventory("AnotherIronblast",1,0,AAPTR_TARGET); }		// Item determining if the ironblast copy is in the field.
			If(!User_Brudda) {
				A_TakeInventory("FloaterblastActive",1,0,AAPTR_TARGET);		// Item determining if the ironblast is in the field.
				A_TakeInventory("IronblastStratActive",1,0,AAPTR_TARGET);	// Item determining if the ironblast strat is active.
				A_TakeInventory("IronblastRevert",1,0,AAPTR_TARGET);		// Item that pops up in your inventory to return the ironblast.
				If(CountInv("Ironblast",AAPTR_TARGET))
				{
					A_SpawnItemEx("IronblastDropped"); // Same as above, except for anything but Hexen.
					Return ResolveState(Null);
				}
				Else
				{
					A_GiveInventory("IronblastDropped",1,AAPTR_TARGET);
					Return ResolveState(Null);
				}
			}
			Return ResolveState(Null);
		}
		Stop;
	}
}

class FloatingIronblast2 : FloatingIronblast
{
	Default
	{
		FloatingIronblast.SecondOne 1;
		FloatingIronblast.Mute 1;
		FloatingIronblast.YPos 55.0;
	}
}

//Draugh version.
class FloatingIronblastD : FloatingIronblast
{
	Default
	{
		FloatingIronblast.Mute 1;
	}
}

class IronblastDropped : WeaponGiver
{
	Default
	{
		DropItem "Ironblast";
		Weapon.AmmoGive 0;
		Inventory.PickupMessage "$GOTIRONBLAST";
		Scale 0.8;
	}
	
	States
	{
	Spawn:
		IRNG X -1;
		Stop;
	}
}

class IronblastPossession : Actor
{
	Default
	{
		+NOTONAUTOMAP
		+NOINTERACTION
	}
	
	int User_TM;
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay {
			User_TM = gc_toastermode ? 96 : 0;
			A_SpawnItemEx("IronEffectParticle",0,0,0,frandom(2.0,-2.0),frandom(2.0,-2.0),frandom(2.0,-2.0),frandom(0.0,360.0),SXF_CLIENTSIDE,64 + User_TM);
			A_SpawnItemEx("IronEffectLightning",frandom(10.0,-10.0),frandom(10.0,-10.0),frandom(10.0,-10.0),0,0,0,0,SXF_CLIENTSIDE,64 + User_TM);
			for(int Kebop = 0; Kebop < 2; Kebop++) {
				A_SpawnItemEx("IronEffectCloud",frandom(10.0,-10.0),frandom(10.0,-10.0),frandom(10.0,-10.0),frandom(1.0,-1.0),frandom(1.0,-1.0),frandom(1.0,-1.0),frandom(0.0,360.0),SXF_CLIENTSIDE, User_TM);
			}
			//A_Warp(AAPTR_MASTER,0,0,0,angle,WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION);
		}
		Stop;
	Death:
		TNT1 A 0;
		Stop;
	}
}

class IronblastRelease : Actor
{
	Default
	{
		+NOTONAUTOMAP
		+NOINTERACTION
	}
	
	int User_Cycle;
	int User_MainCycle;
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay { User_MainCycle = 7; }
	CycleParticle:
		TNT1 A 0 {
			if(GetCvar("GC_ToasterMode")) { User_Cycle = 20; }
			else 						  { User_Cycle = 40; }
		}
	Particle:
		TNT1 A 0 {
			A_SpawnItemEx("IronEffectParticle2",0,0,0,frandom(4.0,-4.0),frandom(4.0,-4.0),frandom(4.0,-4.0),frandom(0.0,360.0),SXF_CLIENTSIDE,20);
			User_Cycle--;
			Return A_JumpIf(User_Cycle <= 0, "CycleLightning");
		}
		Loop;
	CycleLightning:
		TNT1 A 0 {
			if(GetCvar("GC_ToasterMode")) { User_Cycle = 5; }
			else 						  { User_Cycle = 15; }
		}
	Lightning:
		TNT1 A 0 {
			A_SpawnItemEx("IronEffectLightning",frandom(30.0,-30.0),frandom(30.0,-30.0),frandom(30.0,-30.0),frandom(4.0,1.5),frandom(4.0,1.5),frandom(4.0,-4.0),frandom(0.0,360.0),SXF_CLIENTSIDE,20);
			User_Cycle--;
			Return A_JumpIf(User_Cycle <= 0, "CycleCloud");
		}
		Loop;
	CycleCloud:
		TNT1 A 0 {
			if(GetCvar("GC_ToasterMode")) { User_Cycle = 7; }
			else 						  { User_Cycle = 15; }
		}
	Cloud:
		TNT1 A 0 {
			A_SpawnItemEx("IronEffectCloud",frandom(30.0,-30.0),frandom(30.0,-30.0),frandom(30.0,-30.0),frandom(5.5,1.5),frandom(5.5,1.5),frandom(5.5,-5.5),frandom(0.0,360.0),SXF_CLIENTSIDE,20);
			User_Cycle--;
			Return A_JumpIf(User_Cycle <= 0, "RinseAndRepeat");
		}
		Loop;
	RinseAndRepeat:
		TNT1 A random(1,2) {
			User_MainCycle--;
			Return A_JumpIf(User_MainCycle <= 0, "Null");
		}
		Goto CycleParticle;
	}
}

class IronEffect : Actor
{
	Default
	{
		Scale 0;
		Alpha 0.99;
		Renderstyle "Translucent";
		+NOTONAUTOMAP
		+NOINTERACTION
		+NOGRAVITY
		+FORCEXYBILLBOARD
		+NOTIMEFREEZE
	}
}

class IronEffectLightning : IronEffect
{
	States
	{
	Spawn:
		TNT1 A 0 NoDelay {
			A_ScaleVelocity(frandom(0.9,1.0));
			A_SetScale(Scale.X + frandom(0.075,0.1));
			Return A_Jump(256,"SpriteA","SpriteB","SpriteC","SpriteD",
							  "SpriteE","SpriteF","SpriteG","SpriteH");
		}
	SpriteA:
		BLIT A 0 A_Jump(256,"Main");
	SpriteB:
		BLIT B 0 A_Jump(256,"Main");
	SpriteC:
		BLIT C 0 A_Jump(256,"Main");
	SpriteD:
		BLIT D 0 A_Jump(256,"Main");
	SpriteE:
		BLIT E 0 A_Jump(256,"Main");
	SpriteF:
		BLIT F 0 A_Jump(256,"Main");
	SpriteG:
		BLIT G 0 A_Jump(256,"Main");
	SpriteH:
		BLIT H 0 A_Jump(256,"Main");
	Main:
		"####" "#" 1 Bright {
			A_ChangeVelocity(frandom(-0.5,0.5),frandom(-0.5,0.5),frandom(-0.5,0.5),CVF_RELATIVE);
			A_SetScale(Scale.X + frandom(0.004,0.002));
			A_FadeOut(frandom(0.1,0.05));
		}
		Loop;
	}
}

class IronEffectCloud : IronEffect
{
	Default
	{
		Alpha 0.7;
	}
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay {
			A_ScaleVelocity(frandom(0.9,1.0));
			A_SetScale(Scale.X + frandom(0.3,0.4));
			Return A_Jump(256,"SpriteA","SpriteB","SpriteC","SpriteD");
		}
	SpriteA:
		BLTD A 0 A_Jump(256,"Main");
	SpriteB:
		BLTD B 0 A_Jump(256,"Main");
	SpriteC:
		BLTD C 0 A_Jump(256,"Main");
	SpriteD:
		BLTD D 0 A_Jump(256,"Main");
	Main:
		"####" "#" 1 Bright {
			A_SetScale(Scale.X + frandom(0.004,0.002));
			A_ChangeVelocity(frandom(-0.2,0.2),frandom(-0.2,0.2),frandom(-0.2,0.2),CVF_RELATIVE);
			A_FadeOut(frandom(0.075,0.05));
		}
		Loop;
	}
}


class IronEffectParticle : IronEffect
{
	Default
	{
		Alpha 0;
	}
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay A_SetScale(Scale.X + frandom(0.025,0.035));
	Sprite:
		BPRT A 0 A_Jump(256,"Main");
	Main:
		"####" "#" 1 Bright {
			A_SetScale(Scale.X + frandom(0.005,0.0002));
			//A_Warp(AAPTR_MASTER,0,0,0,angle,WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION|WARPF_BOB|WARPF_COPYVELOCITY);
			A_FadeIn(frandom(0.1,0.04));
			Return A_JumpIf(Alpha >= 0.98,"Main2");
		}
		Loop;
	Main2:
		"####" "#" 0 A_ScaleVelocity(frandom(-1.05,-1.02));
	Main3:
		"####" "#" 1 Bright {
			 A_SetScale(Scale.X - frandom(0.005,0.0002));
			//A_Warp(AAPTR_MASTER,0,0,0,angle,WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION|WARPF_BOB|WARPF_COPYVELOCITY);
			A_FadeOut(frandom(0.05,0.03));
			Return A_JumpIf(Scale.X <= 0,"Null");
		}
		Loop;
	}
}

class IronEffectParticle2 : IronEffect
{
	Default
	{
		Alpha 0;
	}
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay A_SetScale(Scale.X + frandom(0.025,0.035));
	Sprite:
		BPRT A 0 A_Jump(256,"Main");
	Main:
		"####" "#" 1 Bright {
			A_ChangeVelocity(0,0,0.1);
			A_SetScale(Scale.X + frandom(0.005,0.0002));
			//A_Warp(AAPTR_MASTER,0,0,0,angle,WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION|WARPF_BOB|WARPF_COPYVELOCITY);
			A_FadeIn(frandom(0.1,0.04));
			Return A_JumpIf(Alpha >= 0.98,"Main2");
		}
		Loop;
	Main2:
		"####" "#" 1 Bright {
			A_ChangeVelocity(0,0,0.1);
			A_SetScale(Scale.X - frandom(0.005,0.0002));
			//A_Warp(AAPTR_MASTER,0,0,0,angle,WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION|WARPF_BOB|WARPF_COPYVELOCITY);
			A_FadeOut(frandom(0.05,0.03));
			Return A_JumpIf(Scale.X <= 0,"Null");
		}
		Loop;
	}
}