#region Artifacts
//$Color 9

class PowerAutoCannonBringer : BaseBringer
{
	Default
	{
		//$Title Powersuit Auto Cannon
		+COUNTITEM +INVENTORY.AUTOACTIVATE +DONTGIB
		Tag "\c[red]Powersuit Auto Cannon";
		Inventory.PickupSound "Stratocaster/Pickup";
		Inventory.PickupMessage "You've got the \c[red]Powersuit Auto Cannon!\c-";
		Inventory.Amount 6000;
		BaseBringer.ItemToBring "PowerAutoCannon";
		BaseBringer.RequisiteItem "PowersuitItem";
	}
	
	override void ToucherEffects(Actor toucher)
	{
			toucher.A_QuakeEx(1,1,1,15,0,6,"",QF_SCALEDOWN|QF_RELATIVE);
			toucher.A_SetBlend("red",0.15,45);
	}
	
	int Loopler;
	
	States
	{
	Spawn:
		PSAC A -1;
		Stop;
	}
}

class PowerAutoCannonActive : Inventory { Default { Inventory.MaxAmount 3; } }

class PowerAutoCannon : TogglerArtefact
{
	Default
	{
		Tag "\c[red]Powersuit Auto Cannon";
		Inventory.PickupMessage "You've got the \c[red]Powersuit Auto Cannon!\c-";
		Inventory.Amount 1;
		Inventory.MaxAmount 1;
		Inventory.InterHubAmount 1;
		Inventory.UseSound "";
		Inventory.Icon "ARTIPAC2";
		Inventory.PickupSound "Stratocaster/Pickup";
		TogglerArtefact.Token "PowerAutoCannonActive";
		Tag "Auto Cannon Toggler";
		+INVENTORY.INVBAR
	}
	
	actor shouldercannon;
	
	//Activates the turret upon pickup by default.
	override void AttachToOwner(Actor user)
	{
		User.A_GiveInventory("PowerAutoCannonActive",1);
		Super.AttachToOwner(User);
		return;
	}

	override void DoEffect()
	{
		//Dropping handle. Probably will switch to OnDrop() or whatever once I'll get my ass moved to 3.6.0 or beyond.
		if(owner.CountInv("PowerAutoCannonActive")) { bUNTOSSABLE = 1; }
		else { bUNTOSSABLE = 0; }
	
		//Icon handle.
		if(owner.CountInv("PowerAutoCannonActive"))
		{
			Icon = TexMan.CheckForTexture("ARTIPAC1", TexMan.Type_Sprite);
		}
		else
		{
			Icon = GetDefaultByType("PowerAutoCannon").Icon;
		}
		
		//Turret handle.
		if(!shouldercannon && owner.CountInv("PowerSuitToken"))
		{
			shouldercannon = Spawn("PowersuitShoulderCannon");
			shouldercannon.target = owner.player.mo;
		}
		
		Super.DoEffect();
		return;
	}

	States
	{  
	Spawn:
		PSAC A -1;
		Stop;
	Use:
		TNT1 A 0 A_PlaySound("Bishop/GaussToggle",5);
		TNT1 A 0 A_JumpIf(CountInv("PowerAutoCannonActive") >= 3,"Deactivate");
		TNT1 A 0 A_JumpIf(CountInv("PowerAutoCannonActive") >= 2,"Manual");
		TNT1 A 0 A_JumpIf(CountInv("PowerAutoCannonActive") == 1,"Reactive");
		TNT1 A 0 {
			A_SetBlend("red",0.15,25);
			A_GiveInventory("PowerAutoCannonActive",1);
			A_Print("\c[red]Powersuit Auto Cannon \c[white]active.\nUse it again to switch firing modes.");
		}
		fail;
	Reactive:
		TNT1 A 0 {
			A_SetBlend("red",0.15,25);
			A_GiveInventory("PowerAutoCannonActive",1);
			A_Print("\c[white]Proxy mode.\n(Shoot only when target's in sights.)");
		}
		fail;
	Manual:
		TNT1 A 0 {
			A_SetBlend("red",0.15,25);
			A_GiveInventory("PowerAutoCannonActive",1);
			A_Print("\c[white]Manual mode.\n(Shoot only when target's in sights\nand holding use / open doors key.)");
		}
		fail;
	Deactivate:
		TNT1 A 0 {
			A_SetBlend("red",0.15,25);
			A_TakeInventory("PowerAutoCannonActive",0);
			A_Print("\c[red]Powersuit Auto Cannon \c[white]inactive.");
		}
		fail;
	}
}

class PowersuitShoulderCannon : Actor
{
	Default
	{
		Projectile;
		+SEEKERMISSILE
		+DONTBLAST
		+NOINTERACTION
		-ACTIVATEIMPACT
	}
	
	int cooldown;
	
	States
	{
	Spawn:
		TNT1 A 35; // some delay after, say, map change or just putting powersuit on so it won't be like, fuggin' instant.
		TNT1 A 0 {
			cooldown = 0;
		}
	Main:
		TNT1 A 1 {
			tracer = null;	// Reset the target.
			
			// Turret offseting. First is for crouch, second for standing.
			If(GetPlayerInput(INPUT_BUTTONS,AAPTR_TARGET) & BT_CROUCH) { A_Warp(AAPTR_TARGET,0,35,40,0,WARPF_NOCHECKPOSITION); }
			Else { A_Warp(AAPTR_TARGET,0,35,70-(GetCvar("GC_NoExtraHeight")*30),0,WARPF_NOCHECKPOSITION); }
			
			// Target acquiring.
			If(CountInv("PowerAutoCannonActive",AAPTR_TARGET) < 2) { A_SeekerMissile(1,1,SMF_LOOK,256,25); }
			
			// Reload sound timing and casing spawning.
			If(cooldown == 60) { A_PlaySound("Bishop/GaussReload",6); }
			If(cooldown == 25) { A_SpawnItemEx("AugurCasing",0,0,0,0,6,7,0,SXF_CLIENTSIDE); }
			
			// Time cycler.
			If(cooldown) { cooldown--; }
			
			// Ammo check.
			If(GetCvar("GC_HardOn")) {
				If(gameinfo.gametype == Game_HEXEN && CountInv("BlueMana",AAPTR_TARGET) < 4) { Return ResolveState(Null); }	// If there's no ammo, don't fire.
				Else If(CountInv("LonghornAmmo",AAPTR_TARGET) < 2) 							 { Return ResolveState(Null); }
			}
			
			// Remove if you end up with no gun or powersuit active.
			If(!(CountInv("PowerSuitToken",AAPTR_TARGET) && CountInv("PowerAutoCannon",AAPTR_TARGET))) { Return ResolveState("Null"); } 
			
			// A small bit powered by SCIEEEENCE to prioritize targets you hover your crosshair over.
			let player_owner = PlayerPawn(target);
			Actor crosshair_target = null;
			If(player_owner) crosshair_target = ActorEx.BetterAimTarget(player_owner);
			If(crosshair_target) { tracer = crosshair_target; }
			
			// Fire checks.
			If(CountInv("PowerAutoCannonActive",AAPTR_TARGET) >= 3 && tracer
				&& !cooldown && GetPlayerInput(INPUT_BUTTONS,AAPTR_TARGET) & BT_USE) { Return ResolveState("Feuer2"); } // Manual fire.
				
			If(target && target.health > 0 && 	  // Do you exist and are you still alive?
			   tracer && tracer.bFRIENDLY == 0 && // Does your target exists and is it not friendly?
			   CountInv("PowerAutoCannonActive",AAPTR_TARGET) > 0 && CountInv("PowerAutoCannonActive",AAPTR_TARGET) < 3 && // Is your cannon active?
			  (!deathmatch && !(tracer is "PlayerPawn")) && !cooldown) { Return ResolveState("Feuer"); } // Is it not player in deathmatch and isn't the gun still reloading?
			
			Return ResolveState(Null);
		}
		Loop;
	Feuer:
		TNT1 A 5;
		TNT1 A 0 A_JumpIf(tracer && tracer.health > 0 && CheckSight(tracer),2); // if you have a target, it's alive and it is in sight - proceed to fire.
		TNT1 A 0 { tracer = null; }
		Goto Main;
	Feuer2:
		TNT1 A 0 {
			// Is user still around? He technically is the source of shaking, so call it on him.
			if(target && target.GetCvar("GC_QuakeRecoil")) {
				target.A_QuakeEx(2,2,2,35,0,128,"none",QF_SCALEDOWN);
				target.A_QuakeEx(2,2,7,25,0,128,"none",QF_SCALEDOWN|QF_WAVE,1.0,1.0,3.0);
			}
			
			// Just to make sure it's in place while firing.
			//If(GetPlayerInput(INPUT_BUTTONS,AAPTR_TARGET) & BT_CROUCH) { A_Warp(AAPTR_TARGET,0,35,40,0,WARPF_NOCHECKPOSITION); }
			//Else { A_Warp(AAPTR_TARGET,0,35,70-(GetCvar("GC_NoExtraHeight")*30),0,WARPF_NOCHECKPOSITION); }

			A_PlaySound("Bishop/GaussRifle",5);
			//A_PlaySound("Bishop/GaussRifle",7,0.5);
			A_AlertMonsters();
			A_FaceTracer();
			
			// Ammo use.
			If(GetCvar("GC_HardOn")) {
				If(gameinfo.gametype == Game_HEXEN ) { A_TakeInventory("BlueMana",4,TIF_NOTAKEINFINITE,AAPTR_TARGET); }
				Else 								 { A_TakeInventory("LonghornAmmo",2,TIF_NOTAKEINFINITE,AAPTR_TARGET); }
			}
			
			// Projectile.
			A_SpawnProjectile("PowersuitShoulderProjectile",0,0,0,CMF_TRACKOWNER,0,AAPTR_TRACER);
			
			// Firing effects.
			A_SpawnProjectile("PowerAuto_Flare",0,0,0,0,0,AAPTR_TRACER);
			for(int KanoneMeister = 0; KanoneMeister <= 15; KanoneMeister++)
			{
				A_SpawnProjectile("PowerAuto_Ember",2,2,frandom(22.0,-22.0),CMF_OFFSETPITCH,frandom(22.0,-12.0),AAPTR_TRACER);
				A_SpawnProjectile("PowerAuto_Cloud",2,2,frandom(22.0,-22.0),CMF_OFFSETPITCH,frandom(18.0,-18.0),AAPTR_TRACER);
				A_SpawnProjectile("Gunsmoke_Large",2,2,frandom(22.0,-22.0),CMF_OFFSETPITCH,frandom(12.0,-12.0),AAPTR_TRACER);
			}
			
			//What says on the tin. And reset the target.
			cooldown = 80;
			tracer = null;
		}
		Goto Main;
	}
}

class PowersuitShoulderProjectile : FastProjectile
{
	Default
	{
		Radius 7;
		Height 5;
		MissileHeight 8;
		Speed 150;
		Projectile;
		Obituary "%o got a hole blown open by %k's power armor-mounted rifle.";
		Renderstyle "Add";
		DamageFunction (100+25*random(0,2));
		Decal "LonghornStamp";
		+EXTREMEDEATH
		+NOGRAVITY
		+FORCERADIUSDMG
		+MTHRUSPECIES
		+RIPPER
	}
	
	States
	{
	Spawn:
		TRAC A 1 Bright NoDelay {
			A_FaceMovementDirection();
		}
		Loop;
	Death:
		TNT1 A 1 {
			A_Explode(25*random(1,3),56,0);
			A_PlaySound("LeadBullet/Hit",4,0.2);
			A_SpawnItemEx("LonghornImpact",0,0,0,0,0,0,0,SXF_CLIENTSIDE,0);
			A_SpawnItemEx("HuegPuff",0,0,0,0,0,0,0,SXF_CLIENTSIDE,0);
		}
		Stop;
	XDeath:
		TNT1 A 0;
		Stop;
	}
}

//Firing cruft
class PowerAuto_Ember : Actor
{
	Default
	{
		Speed 8;
		Projectile;
		Renderstyle 'Add';
		Scale 0.0;
		Alpha 0.99;
		+NOINTERACTION
		+FORCEXYBILLBOARD
	}
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay A_Jump(256, "SpriteA", "SpriteB", "SpriteC");
	SpriteA:
		EMBR A 0 A_Jump(256, "FadeIn");
	SpriteB:
		EMBR B 0 A_Jump(256, "FadeIn");
	SpriteC:
		EMBR C 0 A_Jump(256, "FadeIn");
	FadeIn:
		"####" "#" 0 {
			A_ChangeVelocity(0,0,frandom(0.25, 1.25),CVF_RELATIVE);
			A_SetScale(frandom(0.035, 0.045));
			A_ScaleVelocity(frandom(0.6,1.4));
		}
	FadeOut:
		"####" "#" 1 Bright {
			A_ChangeVelocity(frandom(-0.2, 0.2),frandom(-0.2, 0.2),frandom(-0.05, -0.1),CVF_RELATIVE);
			A_SetScale(Scale.X - frandom(0.0015, 0.001));
			Return A_JumpIf(Scale.X <= 0.0, "Null");
		}
		Loop;
	}
}

class PowerAuto_Cloud : PowerAuto_Ember
{
	Default
	{
		Speed 6;
		Alpha 0.66;
	}
	
	States
	{
		Spawn:
			TNT1 A 0 NoDelay A_Jump(256, "SpriteA", "SpriteB", "SpriteC", "SpriteD",
										 "SpriteE", "SpriteF", "SpriteG", "SpriteH");
		SpriteA:
			CLOD A 0 A_Jump(256, "FadeIn");
		SpriteB:
			CLOD B 0 A_Jump(256, "FadeIn");
		SpriteC:
			CLOD C 0 A_Jump(256, "FadeIn");
		SpriteD:
			CLOD D 0 A_Jump(256, "FadeIn");
		SpriteE:
			CLOD E 0 A_Jump(256, "FadeIn");
		SpriteF:
			CLOD F 0 A_Jump(256, "FadeIn");
		SpriteG:
			CLOD G 0 A_Jump(256, "FadeIn");
		SpriteH:
			CLOD H 0 A_Jump(256, "FadeIn");
		FadeIn:
			"####" "#" 0 Bright {
				A_SetScale(frandom(0.7, 0.8));
				A_ScaleVelocity(frandom(0.5,1.5));
			}
		FadeOut:
			"####" "#" 1 Bright {
				A_FadeOut(0.025);
				A_ScaleVelocity(frandom(0.97, 0.98));
				A_SetScale(Scale.X - frandom(0.01, 0.02));
				Return A_JumpIf(Scale.X <= 0, "Null");
			}
			Loop;
	}
}

class PowerAuto_Flare : PowerAuto_Ember
{
	Default
	{
		Speed 4;
		Alpha 0.0;
		Scale 0.3;
		+ROLLSPRITE
		+ROLLCENTER
	}
	
	States
	{
		Spawn:
			FLER A 0 Bright NoDelay A_SetRoll(frandom(0.0,360.0));
		Looplet:
			FLER A 1 Bright {
				A_FadeIn(0.2);
				A_SetScale(Scale.X + 0.05);
				Return A_JumpIf(Alpha >= 1.0, "Looplet2");
			}
			Loop;
		Looplet2:
			FLER A 1 Bright {
				A_FadeOut(0.1);
				A_SetScale(Scale.X - frandom(0.015, 0.02));
				Return A_JumpIf(Scale.X <= 0.0, "Null");
			}
			Loop;
	}
}

#endregion
