#region Gnus
//$color 14

/*
fun fact: At the beginning, this gun was a classic, drum-fed assault rifle.
Then it got turned into same thing but polished. Upon Zhs2's suggestion,
we ended up with a literal minigun.
*/

class PulverizerSelected : Inventory { Default { Inventory.MaxAmount 1; } }

class LeadBallReleased : PulverizerSelected {}

class LeadBallHeld : PulverizerSelected {}
class LeadBallSize : Inventory { Default { Inventory.MaxAmount 20; } }

class LLeadBallHeld : PulverizerSelected {}
class LLeadBallSize : LeadBallSize {}

#include "Zscript/Guns/Projectile/Pulverizer.txt"

//Hexen version.
class PulverizerHexen : RandomSpawner replaces CWeapStaff
{
	Default
	{
		DropItem "Pulverizer";
	}
}

//Heretic version.
class PulverizerHeretic : RandomSpawner replaces Blaster
{
	Default
	{
		DropItem "Pulverizer";
	}
}

class Pulverizer : GuncasterWeapon replaces Chaingun
{
    Default
    {
		Inventory.PickupSound "Guncaster/Laugh1";
		Inventory.PickupMessage "$GOTPULVERIZER";
		Obituary "$KILLPULVERIZER";
		Weapon.UpSound "Pulverizer/Up";
		Weapon.SelectionOrder 4;
		Weapon.AmmoGive 40;
		Weapon.AmmoType "PulverizerAmmo";
		Weapon.AmmoUse 1;
		Weapon.SlotNumber 4;
		Weapon.BobStyle 'InverseSmooth';
		Weapon.BobSpeed 1.7;
		Weapon.BobRangeY 0.6;
		Weapon.BobRangeX 1.1;
		Scale 0.8;
	}
	
	//Strat drop.
	override void PostBeginPlay()
	{
		if(!bDROPPED && random(0,20) == 0)
		{
			A_DropItem("PulverizerStratGiver",-1,256);
		}
		Super.PostBeginPlay();
		Return;
	}
	
	//Hexen handling.
	override Void AttachToOwner (actor user) {
		if(gameinfo.gametype == Game_HEXEN) {
			AmmoGive1 = 30;
			AmmoType1 = "BlueMana";
			AmmoUse1 = 1;
		}
		Super.AttachToOwner(user);
	}
	
	int GunzerkShot; //So the gun won't change the position mid-through a firing anim.
	int GunzerkShot2;
	
	double GunzX; //A lil' positioning modifier when the gunzerk is on.
	double GunzY;
	
	int PulvRight; //Shotcycle counter.
	int PulvLeft;
	
	//Check ammo/firing function.
	//int oid - OverlayID (Determines the Overlay layer.)
	//bool checkzerking - Check if Gunzerk is active/inactive.
	//int buttoncheck - what kind of button it is?
	
	action bool canContinueFiring(int oid, bool checkzerking, int buttoncheck) {
    return !invoker.WeapSwitch
		&& !CountInv("MenuWheelActive")
        && OverlayID() == oid
        && checkzerking == (
            GetCvar("GC_Gunslinger")
            || CountInv("PowerGunzerking"))
        && (
            (CountInv("HexenCounter") && CountInv("BlueMana") >= 2)
            || (!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 5))
        && GetPlayerInput(INPUT_BUTTONS) & buttoncheck;
	}
	
	//Return firing function. Puts the above in practice.
	
	action state getContinueFiringState() {
    if (canContinueFiring(-3, false, BT_ATTACK) // Right refire.
        || canContinueFiring(-3, true, BT_ALTATTACK) // Left refire.
        || canContinueFiring(3, true, BT_ATTACK)) {
        Return ResolveState("RightReFire");
    }
    Return ResolveState(Null);
	}
	
	States
	{
	Spawn:
		PLVG X -1;
		Stop;

	SelectStuff:
		TNT1 A 0 {
			invoker.PulvRight = 0;
			invoker.PulvLeft = 0;
			invoker.GunzerkShot = 0;
			invoker.GunzerkShot2 = 0;
			A_GiveInventory("PulverizerSelected",1);
			//A_Overlay(99,"Debug");
			A_SetCrosshair(53);
		}
		Goto SelectLoop;
		
	DeselectStuff:
		TNT1 A 0 {
			A_TakeInventory("PulverizerSelected",1);
		}
		Goto DeselectLoop;
		
	/*Debug:
		TNT1 A 35 {
			A_Log(String.Format("WeapAction %i", invoker.WeapAction));
			A_Log(String.Format("LeftHand %i", invoker.LeftHand));
		}
		Loop;*/
		
	//STRAT
	
	StratSwitch:
		"####" "#" 0 A_JumpIf(GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking"), "StratSwitchGunzerk");
		"####" "#" 0 A_Overlay(3,"PushDieButton");
		CHSG A 0 A_JumpIf(CountInv("PulverizerStratActive"),2);
		CHNG A 0;
		"####" "#" 1 A_OverlayOffset(OverlayID(),1,1,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),2,2,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),3,4,WOF_INTERPOLATE);
		"####" "#" 1 {
			A_OverlayOffset(OverlayID(),3,5,WOF_INTERPOLATE);
			A_PlaySound("Pulverizer/Button",5,0.7);
			If(CountInv("PulverizerStratActive")) { A_TakeInventory("PulverizerStratActive"); }
			Else								  { A_GiveInventory("PulverizerStratActive"); }
		}
		CHSG A 0 A_JumpIf(CountInv("PulverizerStratActive"),2);
		CHNG A 0;
		"####" "#" 1 A_OverlayOffset(OverlayID(),2,2,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),1,1,WOF_INTERPOLATE);
		"####" "#" 5 A_OverlayOffset(OverlayID(),0,0,WOF_INTERPOLATE);
		Goto RightActive;
		
	PushDieButton:
		"####" "#" 0 {
			invoker.LeftHand = 1;
			A_OverlayFlags(OverlayID(),PSPF_FLIP|PSPF_MIRROR,1);
		}
		CYDH G 0 A_JumpIf(CountInv("PowerDeadmaker") && CountInv("PowerSuitDraugh"),6);
		CYNH G 0 A_JumpIf(CountInv("PowerDeadmaker"),5);
		CYCH G 0 A_JumpIf(CountInv("PowerSuitToken"),4);
		CYUH G 0 A_JumpIf(CountInv("PowerSuitProtect"),3);
		CYYH G 0 A_JumpIf(CountInv("PowerSuitDraugh"),2);
		CYGH G 0;
		"####" "#" 1 A_OverlayOffset(OverlayID(),90,165);					//I should probably array'ify this one.
		"####" "#" 1 A_OverlayOffset(OverlayID(),99,149,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),107,135,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),104,128,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),101,130,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),93,139,WOF_INTERPOLATE);
		"####" "#" 1 A_OverlayOffset(OverlayID(),86,165,WOF_INTERPOLATE);
		TNT1 A 5 {
			A_OverlayOffset(OverlayID(),0,0);
			invoker.LeftHand = 0;
		}
		Goto LeftActive;
	
	StratSwitchGunzerk:
		CHNG A 0 A_JumpIf(CountInv("PulverizerStratActive"),"StratSwitchGunzerkOff");
		CHNG A 0 {
			invoker.LeftHand = 1;
			A_GiveInventory("PulverizerStratActive");
			A_PlaySound("Pulverizer/Button",5,0.7);
			A_Overlay(3,"LeftStratSwitch");
			invoker.LoopRight = 0;
		}
		CHNG AAAAAAAAA 1 {
			static const double pulvx[] = { 1.0, 3.0, 6.0, 10.0, 12.0, 13.0, 14.0,
											15.0, 14.0 };
			static const double pulvy[] = { 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 14.0,
											15.0, 14.0 };
			int g = invoker.LoopRight++;				
			A_OverlayOffset(OverlayID(), pulvx[g] + invoker.GunzX, pulvy[g] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHSG A 0 { invoker.LoopRight = 0; }
		CHSG AAAAA 1 {
			static const double pulvx2[] = { 13.0, 9.0, 4.0, 2.0, 1.0 };
			static const double pulvy2[] = { 10.0, 6.0, 3.0, 2.0, 1.0 };
			int g = invoker.LoopRight++;
			A_OverlayOffset(OverlayID(), pulvx2[g] + invoker.GunzX, pulvy2[g] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHSG A 0 {
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY,WOF_INTERPOLATE);
			invoker.LoopRight = 0;
			invoker.LeftHand = 0;
			A_GiveInventory("PulverizerStratActive",1);
		}
		Goto RightActive;
	LeftStratSwitch:
		CHNG A 0 {
			A_OverlayFlags(OverlayID(),PSPF_FLIP|PSPF_MIRROR,1);
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY);
			invoker.LoopLeft = 0;
		}
		CHNG AAAAAAAAA 1 {
			static const double pulvx3[] = { 1.0, 3.0, 6.0, 10.0, 12.0, 13.0, 14.0,
											 15.0, 14.0 };
			static const double pulvy3[] = { 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 14.0,
											 15.0, 14.0 };
			int g2 = invoker.LoopLeft++;
			A_OverlayOffset(OverlayID(), pulvx3[g2] + invoker.GunzX, pulvy3[g2] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHSG A 0 { invoker.LoopLeft = 0; }
		CHSG AAAAA 1 {
			static const double pulvx4[] = { 13.0, 9.0, 4.0, 2.0, 1.0 };
			static const double pulvy4[] = { 10.0, 6.0, 3.0, 2.0, 1.0 };
			int g2 = invoker.LoopLeft++;
			A_OverlayOffset(OverlayID(), pulvx4[g2] + invoker.GunzX, pulvy4[g2] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHSG A 0 {
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY,WOF_INTERPOLATE);
			invoker.LoopLeft = 0;
		}
		Goto LeftActive;
		
	StratSwitchGunzerkOff:
		CHSG A 0 {
			invoker.LeftHand = 1;
			A_TakeInventory("PulverizerStratActive");
			A_PlaySound("Pulverizer/Button",5,0.7);
			A_Overlay(3,"LeftStratSwitchOff");
			invoker.LoopRight = 0;
		}
		CHSG AAAAAAAAA 1 {
			static const double pulvx5[] = { 1.0, 3.0, 6.0, 10.0, 12.0, 13.0, 14.0,
											 15.0, 14.0 };
			static const double pulvy5[] = { 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 14.0,
											 15.0, 14.0 };
			int g = invoker.LoopRight++;
			A_OverlayOffset(OverlayID(), pulvx5[g] + invoker.GunzX, pulvy5[g] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHNG A 0 { invoker.LoopRight = 0; }
		CHNG AAAAA 1 {
			static const double pulvx6[] = { 13.0, 9.0, 4.0, 2.0, 1.0 };
			static const double pulvy6[] = { 10.0, 6.0, 3.0, 2.0, 1.0 };
			int g = invoker.LoopRight++;
			A_OverlayOffset(OverlayID(), pulvx6[g] + invoker.GunzX, pulvy6[g] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHNG A 0 {
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY,WOF_INTERPOLATE);
			invoker.LeftHand = 0;
			invoker.LoopRight = 0;
			A_TakeInventory("PulverizerStratActive",1);
		}
		Goto RightActive;
	LeftStratSwitchOff:
		CHSG A 0 {
			A_OverlayFlags(OverlayID(),PSPF_FLIP|PSPF_MIRROR,1);
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY);
			invoker.LoopLeft = 0;
		}
		CHSG AAAAAAAAA 1 {
			static const double pulvx7[] = { 1.0, 3.0, 6.0, 10.0, 12.0, 13.0, 14.0,
											 15.0, 14.0 };
			static const double pulvy7[] = { 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 14.0,
											 15.0, 14.0 };
			int g2 = invoker.LoopLeft++;
			A_OverlayOffset(OverlayID(), pulvx7[g2] + invoker.GunzX, pulvy7[g2] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHNG A 0 { invoker.LoopLeft = 0; }
		CHNG AAAAA 1 {
			static const double pulvx8[] = { 13.0, 9.0, 4.0, 2.0, 1.0 };
			static const double pulvy8[] = { 10.0, 6.0, 3.0, 2.0, 1.0 };
			int g2 = invoker.LoopLeft++;
			A_OverlayOffset(OverlayID(), pulvx8[g2] + invoker.GunzX, pulvy8[g2] + invoker.GunzY, WOF_INTERPOLATE);
		}
		CHNG A 0 {
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY,WOF_INTERPOLATE);
			invoker.LoopLeft = 0;
		}
		Goto LeftActive;
	
	//RIGHT SIDE
	
	RightActive:
		"####" "#" 0 {
			if(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) {
				invoker.GunzX = 30;
				invoker.GunzY = 10;
			} else {
				//if(!invoker.LeftHand) { A_Overlay(-4,"RightActive2"); }
				invoker.GunzX = 0;
				invoker.GunzY = 0;
			}
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY);
		}
		CHSG A 0 A_JumpIf(CountInv("PulverizerStratActive"),2);
		CHNG A 0;
		"####" "#" 1; //You WANT this delay here or else when you run out of ammo - ker-crash due to infinite loop~
        "####" "#" 0 {
			//Strat switch.
			if(CountInv("PulverizerStrat") &&!CountInv("MenuWheelActive") && 
				!invoker.weapSwitch && !invoker.LeftHand &&
				GetPlayerInput(INPUT_BUTTONS) & BT_RELOAD) {
				Return ResolveState("StratSwitch");
			}
			
			//Fire.
			If(!(GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking")) &&
				!CountInv("MenuWheelActive") && !invoker.weapSwitch &&
				GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) {
				Return ResolveState("MainFire");
			}
			
			//Gunzerk fire.
			If((GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking")) &&
				!CountInv("MenuWheelActive") && !invoker.WeapSwitch &&
				GetPlayerInput(INPUT_BUTTONS) & BT_ALTATTACK) {
				Return ResolveState("MainFire");
			}
			Return ResolveState(Null);
		}
		Loop;
		
	//LEFT SIDE
	
	LeftActive:
		TNT1 A 0 {
			if(GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking")) {
				A_OverlayFlags(3,PSPF_MIRROR|PSPF_FLIP,1);
			} else {
				A_OverlayFlags(3,PSPF_MIRROR|PSPF_FLIP,0);
			}
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY);
		}
		TNT1 A 0 A_JumpIf(GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking"),3);
		TNT1 A 1;
		TNT1 A 0 {
			If(!invoker.WeapSwitch && !CountInv("MenuWheelActive") && !invoker.LeftHand &&
				!(GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking")) &&
				GetPlayerInput(INPUT_BUTTONS) & BT_ALTATTACK) {
					Return ResolveState("Cast");
			}
			If(!invoker.WeapSwitch && !CountInv("MenuWheelActive") && !invoker.LeftHand &&
				!(GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking")) && GetCvar("GC_Trogdor") &&
				GetPlayerInput(INPUT_BUTTONS) & BT_ZOOM) {
					Return ResolveState("DeadPunchStart");
			}
			Return ResolveState(Null);
		}
		Loop;
		CHSG A 0 A_JumpIf(CountInv("PulverizerStratActive"),2);
		CHNG A 0;
		"####" "#" 1;
		CHNG A 0 A_JumpIf(!invoker.WeapSwitch && !CountInv("MenuWheelActive")&&
						  GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK, "MainFire");
        Loop;
	
	//FIRE
	
	MainFire:
		"####" A 0 A_JumpIf(CountInv("PulverizerStratActive"),"StratFire");
		"####" A 0 {
			invoker.WeapAction++;
			if(OverlayID() == 3) { invoker.LeftHand = 1; }
			A_PlaySound("Pulverizer/Add",6,0.55);
		}
	RightReFire:
		"####" A 0 A_JumpIf(OverlayID() == 3,3);
		"####" A 0 A_JumpIfNoAmmo("RightActive");
		"####" A 0 A_Jump(256,2);
		"####" A 0 A_JumpIfNoAmmo("LeftActive");
		"####" A 0 {
			If(OverlayID() == 3) {
				invoker.LeftHand = 1;
				invoker.LoopLeft = 0;
			} else {
				invoker.LoopRight = 0;
			}
			if(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) {
				if(OverlayID() == -3) {
					invoker.GunzerkShot = 1;
				} else {
					invoker.GunzerkShot2 = 1;
				}
				invoker.LoopRight = 0;
			}
		}
	FireLoop:
		CHNF ABCDEABCDE 1 Bright {
			//Position.
			static const double pulvpos[] = { 8.0, 9.0, 11.0, 13.0, 15.0,
											 14.0, 13.0, 12.0, 11.0, 9.0 };
			//Gunzerk position.
			static const double pulvx9[] = { 38.0, 39.0, 41.0, 43.0, 45.0,
											 44.0, 43.0, 42.0, 41.0, 39.0 };
			static const double pulvy9[] = { 18.0, 19.0, 21.0, 23.0, 25.0,
											 24.0, 23.0, 22.0, 21.0, 19.0 };
									   
			//Position appliance.
			if(OverlayID() == -3) {
				int q = invoker.LoopRight++;
				if(invoker.GunzerkShot) {
				A_OverlayOffset(OverlayID(), pulvx9[q] + frandom(5,-5), pulvy9[q] + frandom(5,-4), WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos[q] + frandom(5,-5), pulvpos[q] + frandom(5,-4), WOF_INTERPOLATE);
				}
			} else {
				int q2 = invoker.LoopLeft++;
				if(invoker.GunzerkShot2) {
				A_OverlayOffset(OverlayID(), pulvx9[q2] + frandom(5,-5), pulvy9[q2] + frandom(5,-4), WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos[q2] + frandom(5,-5), pulvpos[q2] + frandom(5,-4), WOF_INTERPOLATE);
				}
			}
			
			//Sound.
			A_PlaySound("Pulverizer/Fire",5,0.8,1);
			if((invoker.LoopRight % 2 == 0 && OverlayID() == -3) ||
			   (invoker.LoopLeft % 2 == 0  && OverlayID() == 3)) {
				A_PlaySound("Pulverizer/Add",6,0.55);
			}
			
			//Actual attack.
			A_AlertMonsters();
			
			//Splitter shield junk.
			if(CountInv("SplitterShield")) {
				int Lolnein;
				if(CountInv("PowerGunzerking") || GetCvar("GC_Gunslinger")) { Lolnein = OverlayID() == -3 ? 3 : -3; }
				else { Lolnein = 0; }
				A_FireBullets(3,1.25,-1,5*frandom(1.0,3.0),"HBulletPuff",FBF_NORANDOM,8192,"PhalanxBullet",0,OverlayID() == -3 ? 6 : -6 + Lolnein);
			} Else {
				A_FireBullets(3,1.25,-1,5*frandom(1.0,3.0),"HBulletPuff",FBF_NORANDOM);
			}
			
			//Recoil.
			invoker.GMod = GetCVar("GC_Gunslinger") || CountInv("PowerGunzerking") ? 0.5 : 1.0;
			if(GetCvar("GC_ZoomRecoil")) { A_ZoomFactor(0.95); }
			if(GetCvar("GC_QuakeRecoil")) { A_QuakeEx(2, 2, 2, 5, 0, 4, "none", QF_SCALEDOWN); }
			A_SetPitch(pitch-0.25 * GetCvar("GC_WeaponRecoil") * invoker.Gmod, SPF_INTERPOLATE);
			A_SetAngle(angle + (OverlayID() == -3 ? -0.25 : 0.25) * GetCvar("GC_WeaponRecoil") * invoker.Gmod, SPF_INTERPOLATE);
			
			//Gun firing stuff.
			A_SetBlend("Yellow",frandom(0.02,0.03),3);
			
			//for(invoker.Loopler = 0; invoker.Loopler <= 3; invoker.Loopler++)
            //{
             A_FireProjectile("Gunsmoke_Small",
				frandom(3.0,-3.0),0,											//Angle.
				(OverlayID() == -3 ? 2.0 : -2.0) + 								//Xpos.
				((OverlayID() == -3 ? invoker.GunzX : -invoker.GunzX) * 0.1)
				+ frandom(1.0,-1.0),	
				3.0 - (invoker.GunzY * 0.2)	+ frandom(1.0,-1.0),				//Ypos.
				FPF_NOAUTOAIM,													//Flags.
				frandom(2.0,-2.0));												//Pitch.
            //}
			
			//Casings.
			if(OverlayID() == -3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking"))) {
				A_FireProjectile("BuellerCasingSpawner",0,0,30,-18,FPF_NOAUTOAIM);
			} else if (OverlayID() == 3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking"))){
				A_FireProjectile("BuellerCasingSpawnerLeft",0,0,-30,-18,FPF_NOAUTOAIM);
			} else {
				A_FireProjectile("BuellerCasingSpawner",0,0,20,-16,FPF_NOAUTOAIM);
			}
			
			//Ammo tearing.
			invoker.PulvRight++;
			if(CountInv("HexenCounter"))
			{
				if(invoker.PulvRight >= 5)
				{
					A_TakeInventory("BlueMana",1,TIF_NOTAKEINFINITE);
					invoker.PulvRight = 0;
				}
			}
			else
			{
				if(invoker.PulvRight >= 2)
				{
					A_TakeInventory("PulverizerAmmo",1,TIF_NOTAKEINFINITE);
					invoker.PulvRight = 0;
				}
			}
			Return A_JumpIfNoAmmo("FireEnd");
		}
		//Reset frames.
		CHNG A 0 { 
			if(OverlayID() == -3) {
				invoker.LoopRight = 0;
			} else {
				invoker.LoopLeft = 0;
			}
			
			//Right Refire.
			return getContinueFiringState();
		}
		Goto FireEnd;
	FireEnd:
		CHNG A 0 {
			A_PlaySound("Pulverizer/Down",5,0.9);
			//A_StopSound(6);
			A_SetBlend("Yellow",0.02,8);
		
			if(OverlayID() == -3) {
				invoker.LoopRight = 0;
				invoker.LoopRecoil = 0;
			} else {
				invoker.LeftHand = 1;
				invoker.LoopLeft = 0;
				invoker.LoopRecoil2 = 0;
			}
		}
		CHNG ABCDE 1 {
			//Position.
			static const double pulvpos2[] = { 8.0, 7.0, 5.0, 3.0, 1.0 };
			
			//Gunzerk position.
			static const double pulvx10[] = { 38.0, 37.0, 35.0, 33.0, 31.0 };
			static const double pulvy10[] = { 18.0, 17.0, 15.0, 13.0, 11.0 };
			
			//Zoom.
			static const double pulvzoom[] = { 0.96, 0.97, 0.98, 0.99, 1.0 }; 
			
			//Position/Zoom appliance.
			if(OverlayID() == -3) {
				int q = invoker.LoopRight++;
				if(GetCvar("GC_ZoomRecoil")) { A_ZoomFactor(pulvzoom[q]); }
				if(invoker.GunzerkShot) {
				A_OverlayOffset(OverlayID(), pulvx10[q], pulvy10[q], WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos2[q], pulvpos2[q], WOF_INTERPOLATE);
				}
			} else {
				int q2 = invoker.LoopLeft++;
				if(GetCvar("GC_ZoomRecoil")) { A_ZoomFactor(pulvzoom[q2]); }
				if(invoker.GunzerkShot2) {
				A_OverlayOffset(OverlayID(), pulvx10[q2], pulvy10[q2], WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos2[q2], pulvpos2[q2], WOF_INTERPOLATE);
				}
			}
			
			//Stopfire recoil.
			static const double pulvfactor[] = { -0.25, -0.2, -0.15, -0.1, -0.05 };
			if(OverlayID() == -3) {
				double shift = pulvfactor[invoker.LoopRecoil++] * GetCVar("GC_WeaponRecoil") * invoker.GMod;
				A_SetPitch(pitch + shift, SPF_INTERPOLATE);
				A_SetAngle(angle + shift, SPF_INTERPOLATE);
			} else {
				double shift2 = pulvfactor[invoker.LoopRecoil2++] * GetCVar("GC_WeaponRecoil") * invoker.GMod;
				A_SetPitch(pitch + shift2, SPF_INTERPOLATE);
				A_SetAngle(angle - shift2, SPF_INTERPOLATE);
			}

			//Refire.
			return getContinueFiringState();
		}
		
		//Flush.
		CHNG A 0 {
			A_OverlayOffset(OverlayID(),invoker.GunzX,invoker.GunzY,WOF_INTERPOLATE);
			invoker.WeapAction--;
			if(OverlayID() == -3) {
				
				invoker.GunzerkShot = 0;
				invoker.LoopRight = 0;
				invoker.LoopRecoil = 0;
			} else {
				invoker.GunzerkShot2 = 0;
				invoker.LeftHand = 0;
				invoker.LoopLeft = 0;
				invoker.LoopRecoil2 = 0;
			}
		}
		
		//Winddown.
		CHNG AABBCCDDEEAABBCCDDDEEEE 1 {
			//Refire.
			return getContinueFiringState();
		}
		
		//Return to starting overlay.
		CHNG A 0 A_JumpIf(OverlayID() == 3, "LeftActive");
		Goto RightActive;
		
	//STRAT FIRE
	
	StratFire:
		"####" A 0 {
			if(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) {
				if(OverlayID() == -3) {
					invoker.GunzerkShot = 1;
				} else {
					invoker.LeftHand = 1;
					invoker.GunzerkShot2 = 1;
				}
			}
			If( (!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 5) ||
				(CountInv("HexenCounter") && CountInv("BlueMana") >= 3) )
			{
				Return ResolveState("BulletLasagne");
			}
			Else If((CountInv("LeadBallHeld") && OverlayID() == -3) ||
					(CountInv("LLeadBallHeld") && OverlayID() == 3))
			{
				Return ResolveState("BulletLasagne");
			}
			Return A_JumpIf(OverlayID() == 3, "LeftActive");
		}
		Goto RightActive;
		
	OutOfAmmoOrFullStack:
		//CHSG A 0 A_JumpIfNoAmmo("ReleaseTheDachshund");
		CHSG A 0 {
			A_PlaySound("Pulverizer/Loop",5,0.8,1);
			//A_StopSound(6);
			A_ZoomFactor(1.0);
		}
		CHSG ABCDE 1 A_OverlayOffset(OverlayID(),frandom(4.0,-4.0)+invoker.GunzX,frandom(4.0,0.0)+invoker.GunzY, WOF_INTERPOLATE);
		CHSG A 0 {
			If(OverlayID() == -3 && !(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 5) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 3)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("OutOfAmmoOrFullStack"); }
			If(OverlayID() == -3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ALTATTACK) { Return ResolveState("OutOfAmmoOrFullStack"); }
			If(OverlayID() == 3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("OutOfAmmoOrFullStack"); }
			Return ResolveState(Null);
		}
		
	ReleaseTheDachshund:
		CHSG A 0 {
			A_PlaySound("Pulverizer/Down",5,0.9);
			A_PlaySound("LeadBall/Release",6,1.0);
			A_SetBlend("Yellow",0.2,20);
			A_Recoil(10.0 * (CountInv("PowerDeadmaker") ? 0.1 : 1.0) * (CountInv("PowerSuitToken") ? 0.1 : 1.0) * GetCvar("GC_PhysicalRecoil"));
		
			if(OverlayID() == -3) {
				A_TakeInventory("LeadBallSize");
				A_TakeInventory("LeadBallHeld");
				invoker.LoopRight = 0;
				invoker.LoopZoom = 0;
				invoker.LoopRecoil = 0;
			} else {
				A_TakeInventory("LLeadBallSize");
				A_TakeInventory("LLeadBallHeld");
				invoker.LoopLeft = 0;
				invoker.LoopZoom2 = 0;
				invoker.LoopRecoil2 = 0;
			}
		}
		CHSG ABCDEABC 1 {
			//Position.
			static const double pulvpos3[] = { 8.0, 7.0, 5.0, 3.0, 1.0, 0.0 };
			
			//Gunzerk position.
			static const double pulvx11[] = { 38.0, 37.0, 35.0, 33.0, 31.0, 30.0 };
			static const double pulvy11[] = { 18.0, 17.0, 15.0, 13.0, 11.0, 10.0 };
			
			//Position appliance.
			if(OverlayID() == -3 && invoker.LoopRight < 6) {
				int q = invoker.LoopRight++;
				if(invoker.GunzerkShot) {
				A_OverlayOffset(OverlayID(), pulvx11[q], pulvy11[q], WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos3[q], pulvpos3[q], WOF_INTERPOLATE);
				}
			} else if(OverlayID() == 3 && invoker.LoopLeft < 6) {
				int q2 = invoker.LoopLeft++;
				if(invoker.GunzerkShot2) {
				A_OverlayOffset(OverlayID(), pulvx11[q2], pulvy11[q2], WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos3[q2], pulvpos3[q2], WOF_INTERPOLATE);
				}
			}
			
			//Stopfire recoil.
			static const double pulvfactor2[] = { -2.5, -2.0, -1.0, -0.75, -0.5,
												  -0.25, -0.1, -0.05 };
			if(OverlayID() == -3) {
				double shift = pulvfactor2[invoker.LoopRecoil++] * GetCVar("GC_WeaponRecoil") * invoker.GMod;
				A_SetPitch(pitch + shift, SPF_INTERPOLATE);
				A_SetAngle(angle + shift, SPF_INTERPOLATE);
			} else {
				double shift2 = pulvfactor2[invoker.LoopRecoil2++] * GetCVar("GC_WeaponRecoil") * invoker.GMod;
				A_SetPitch(pitch + shift2, SPF_INTERPOLATE);
				A_SetAngle(angle - shift2, SPF_INTERPOLATE);
			}
			
			//Zoom recoil.
			static const double pulvzoom2[] = { 0.945, 0.95, 0.96, 0.97, 0.98,
												0.99, 0.995, 1.0 };
			if(GetCvar("GC_ZoomRecoil") && OverlayID() == -3) {
				A_ZoomFactor(pulvzoom2[invoker.LoopZoom++]);
			} else if(GetCvar("GC_ZoomRecoil") && OverlayID() == 3) {
				A_ZoomFactor(pulvzoom2[invoker.LoopZoom2++]);
			}
			
			//Refire.
			If(OverlayID() == -3 && !(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 5) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 3)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("StratFire"); }
			If(OverlayID() == -3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ALTATTACK) { Return ResolveState("StratFire"); }
			If(OverlayID() == 3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("StratFire"); }
			Return ResolveState(Null);
		}
		
		//Flush.
		CHNG A 0 {
			if(OverlayID() == -3) {
				invoker.GunzerkShot = 0;
				invoker.LoopRight = 0;
				invoker.LoopZoom = 0;
				invoker.LoopRecoil = 0;
			} else {
				invoker.GunzerkShot2 = 0;
				invoker.LeftHand = 0;
				invoker.LoopLeft = 0;
				invoker.LoopZoom2 = 0;
				invoker.LoopRecoil2 = 0;
			}
		}
		
		//Winddown.
		CHSG DEAABBCCDDEEAABBCCDDDEEEE 1 {
			//Refire.
			If(OverlayID() == -3 && !(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 5) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 3)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("StratFire"); }
			If(OverlayID() == -3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ALTATTACK) { Return ResolveState("StratFire"); }
			If(OverlayID() == 3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("StratFire"); }
			Return ResolveState(Null);
		}
		CHSG A 0 { If(OverlayID() == 3) { Return ResolveState("LeftActive"); } Return ResolveState(Null); }
		Goto RightActive;
		
	BulletLasagne:	
		CHSG A 0 {
			if(OverlayID() == -3 && !CountInv("LeadBallHeld"))
			{
				A_FireProjectile("LeadBall",0,0,0,0);
				A_GiveInventory("LeadBallHeld",1);
			}
			else if(OverlayID() == 3 && !CountInv("LLeadBallHeld"))
			{
				A_FireProjectile("LLeadBall",0,0,0,0);
				A_GiveInventory("LLeadBallHeld",1);
			}
			A_PlaySound("Pulverizer/Fire",5,0.8,1);
		}
		
	LasagneLooplet:
		CHSF A 0 {
			if(OverlayID() == -3) {
				invoker.LoopRight = 0;
			} else {
				invoker.LoopLeft = 0;
			}
		}
		CHSF ABCDEABCDE 1 Bright {
			static const double pulvpos4[] = { 8.0, 9.0, 11.0, 13.0, 15.0,
											 14.0, 13.0, 12.0, 11.0, 9.0 };
			//Gunzerk position.
			static const double pulvx12[] = { 38.0, 39.0, 41.0, 43.0, 45.0,
											  44.0, 43.0, 42.0, 41.0, 39.0 };
			static const double pulvy12[] = { 18.0, 19.0, 21.0, 23.0, 25.0,
											  24.0, 23.0, 22.0, 21.0, 19.0 };
			
			//Position appliance.
			if(OverlayID() == -3) {
				int q = invoker.LoopRight++;
				if(invoker.GunzerkShot) {
				A_OverlayOffset(OverlayID(), pulvx12[q] + frandom(2,-2), pulvy12[q] + frandom(2,-2), WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos4[q] + frandom(2,-2), pulvpos4[q] + frandom(2,-2), WOF_INTERPOLATE);
				}
			} else {
				int q2 = invoker.LoopLeft++;
				if(invoker.GunzerkShot2) {
				A_OverlayOffset(OverlayID(), pulvx12[q2] + frandom(2,-2), pulvy12[q2] + frandom(2,-2), WOF_INTERPOLATE);
				} else {
				A_OverlayOffset(OverlayID(), pulvpos4[q2] + frandom(2,-2), pulvpos4[q2] + frandom(2,-2), WOF_INTERPOLATE);
				}
			}
			
			//Recoil.
			if(GetCvar("GC_ZoomRecoil")) { A_ZoomFactor(0.94); }
			if(GetCvar("GC_QuakeRecoil")) { A_QuakeEx(2, 2, 2, 10, 0, 24, "none"); }
			A_SetPitch(pitch - 0.25 * GetCvar("GC_WeaponRecoil") * invoker.Gmod, SPF_INTERPOLATE);
			A_SetAngle(angle + (OverlayID() == -3 ? -0.25 : 0.25) * GetCvar("GC_WeaponRecoil") * invoker.Gmod, SPF_INTERPOLATE);
			
			//Sound.
			A_PlaySound("Pulverizer/Fire",5,0.8,1);
			if((invoker.LoopRight % 2 == 0 && OverlayID() == -3) ||
			   (invoker.LoopLeft % 2 == 0  && OverlayID() == 3)) {
				A_PlaySound("Pulverizer/Add",6,0.55);
			}
			
			//Gun firing stuff.
			A_AlertMonsters();
			A_SetBlend("Yellow",frandom(0.02,0.03),3);
            A_FireProjectile("Gunsmoke_Small",
				frandom(3.0,-3.0),0,											//Angle.
				(OverlayID() == -3 ? 2.0 : -2.0) + 								//Xpos.
				((OverlayID() == -3 ? invoker.GunzX : -invoker.GunzX) * 0.1)
				+ frandom(1.0,-1.0),	
				3.0 - (invoker.GunzY * 0.2)	+ frandom(1.0,-1.0),				//Ypos.
				FPF_NOAUTOAIM,													//Flags.
				frandom(2.0,-2.0));												//Pitch.	
					
			If(OverlayID() == -3 && A_JumpIfInventory("LeadBallSize",0,"Null")) { Return ResolveState("OutOfAmmoOrFullStack"); }
			If(OverlayID() == 3 && A_JumpIfInventory("LLeadBallSize",0,"Null")) { Return ResolveState("OutOfAmmoOrFullStack"); }
			Return ResolveState(Null);
		}
		"####" A 0 {
			If(OverlayID() == -3) { invoker.LoopRight = 0; }
			If(OverlayID() == 3) { invoker.LoopLeft = 0; }
			
			If(OverlayID() == -3 && !(GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 5) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 3)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("LasagneLooplet"); }
			If(OverlayID() == -3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ALTATTACK) { Return ResolveState("LasagneLooplet"); }
			If(OverlayID() == 3 && (GetCvar("GC_Gunslinger") || CountInv("PowerGunzerking")) && ((!CountInv("HexenCounter") && CountInv("PulverizerAmmo") >= 10) || (CountInv("HexenCounter") && CountInv("BlueMana") >= 6)) && GetPlayerInput(INPUT_BUTTONS) & BT_ATTACK) { Return ResolveState("LasagneLooplet"); }
			Return ResolveState(Null);
		}
		Goto ReleaseTheDachshund;
	}
}

#endregion
