#region Artifacts
//$Color 9

//Pillow: AWWWW YISS MY FIRST REAL DEAL ZSCRIPT THING,
//With a lil' (very lil' yea sure) bit of Keks' assistance.

//Don't mind the shitfest here. Just ported over from decorate.
class ManaShieldTomeBringer : BaseBringer
{
	Default
	{
		//$Title Mana Shield Tome
		+COUNTITEM +INVENTORY.AUTOACTIVATE +FLOATBOB +DONTGIB //+ALWAYSPICKUP
		Tag "\c[blue]Mana \c[yellow]Shield";
		Inventory.PickupSound "Tome/Pickup";
		Inventory.PickupMessage "You've got the tome of \c[blue]Mana \c[yellow]Shield\c-!";
		Inventory.Amount 5000;
		BaseBringer.ItemToBring "ManaShieldTome";
		BaseBringer.ItemToUnlock Gcs_BaseShop.manashield;
	}
	
	override void ToucherEffects(Actor toucher)
	{
		toucher.A_SetBlend("Blue",0.2,25);
	}
	
	States
	{
	Spawn:
		MSBK A 1 Bright 
		{
			If(!(A_CheckSight("Null") || GetCVar("GC_PickupParticleChoker")))
			{
				A_SpawnItemEx("MidasTomeFade",0,0,0,0,0,0,0,SXF_CLIENTSIDE);
					
				if(random(0,1) == 1)
				{
				A_SpawnItemEx("TomeParticleYellow",
					frandom(15.0,-15.0),frandom(15.0,-15.0),30+frandom(15.0,-15.0),
					frandom(0.6,-0.6),frandom(0.6,-0.6),frandom(1.2,0.4),
					frandom(0.0,360.0),SXF_CLIENTSIDE,64);
				}
				else
				{
				A_SpawnItemEx("TomeParticleBlue",
					frandom(15.0,-15.0),frandom(15.0,-15.0),30+frandom(15.0,-15.0),
					frandom(0.6,-0.6),frandom(0.6,-0.6),frandom(1.2,0.4),
					frandom(0.0,360.0),SXF_CLIENTSIDE,64);
				}
			}
		}
		Loop;
	}
}

class ManaShieldTome : TogglerArtefact
{
	Default
	{
		Tag "\c[blue]Mana \c[yellow]Shield";
		Inventory.PickupSound "Tome/Pickup";
		Inventory.PickupMessage "You've got the tome of \c[blue]Mana \c[yellow]Shield\c-!";
		Inventory.Icon "ARTIMSBK";
		Inventory.Amount 1;
		Inventory.MaxAmount 3;
		Inventory.InterHubAmount 3;
		Inventory.UseSound "";
		TogglerArtefact.Token "PowerManaShield";
		Tag "Mana Shield Tome";
		+INVENTORY.INVBAR +FLOATBOB +DONTGIB //+KEEPDEPLETED
	}

	//Pillow: some lil' leveling system for this, based on amount of tomes~
	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("PowerManaShield")) { bUNTOSSABLE = 1; }
		else { bUNTOSSABLE = 0; }
	
		//Shield ratio handle.
		let MShieldPowerItem = PowerManaShield(Owner.FindInventory("PowerManaShield"));
		if(MShieldPowerItem)
		{
			switch(Owner.CountInv("ManaShieldTome"))
			{
				default:
				case 1:
					MShieldPowerItem.AbsorbRatio = 0.2;
					break;
				case 2:
					MShieldPowerItem.AbsorbRatio = 0.4;
					break;
				case 3:
					MShieldPowerItem.AbsorbRatio = 0.6;
					break;
				case 4:
					MShieldPowerItem.AbsorbRatio = 0.8;
					break;
				case 5:
					MShieldPowerItem.AbsorbRatio = 1.0;
					break;
			}
		}
		Super.DoEffect();
		return ;
	}
	
	States
	{  
	Spawn:
		MSBK A 1 NoDelay Bright {
			If(!(A_CheckSight("Null") || GetCVar("GC_PickupParticleChoker")))
			{
				A_SpawnItemEx("MidasTomeFade",0,0,0,0,0,0,0,SXF_CLIENTSIDE);
				
				if(random(0,1) == 1)
				{
				A_SpawnItemEx("TomeParticleYellow",
					frandom(15.0,-15.0),frandom(15.0,-15.0),30+frandom(15.0,-15.0),
					frandom(0.6,-0.6),frandom(0.6,-0.6),frandom(1.2,0.4),
					frandom(0.0,360.0),SXF_CLIENTSIDE,64);
				}
				else
				{
				A_SpawnItemEx("TomeParticleBlue",
					frandom(15.0,-15.0),frandom(15.0,-15.0),30+frandom(15.0,-15.0),
					frandom(0.6,-0.6),frandom(0.6,-0.6),frandom(1.2,0.4),
					frandom(0.0,360.0),SXF_CLIENTSIDE,64);
				}
			}
		}
		Loop;
		
	Use:
		TNT1 A 0 A_JumpIfInventory("PowerManaShield",1,"Deactivate");
		TNT1 A 0 {
			if(CountInv("ManaShieldTome") >= 3) {
				invoker.owner.bNOBLOOD = 1;
			}
			A_PlaySound("ManaShield/Pick",3);
			A_GiveInventory("ManaShieldGiver",1);
			A_SetBlend("Gold",0.4,25);
		}
		Fail;
		
	Deactivate:
		TNT1 A 0 {
			if(!CountInv("PowerSuitToken")) {
				invoker.owner.bNOBLOOD = 0;
			}
			A_TakeInventory("PowerManaShield");
			A_SetBlend("Gold",0.05,25);
		}
		Fail;
	}
}

class ManaShieldGiver : PowerupGiver
{
	Default
	{
		Powerup.Duration -0x7FFFFFFF;
		Inventory.PickupMessage "BROCCOLI!";
		Inventory.PickupSound "ManaShield/Pick";
		Powerup.Type "PowerManaShield";
		Powerup.Color 'Gold', 0.0;
		+INVENTORY.AUTOACTIVATE
	}
}

class PowerManaShield : Powerup
{
	Default
	{
		PowerManaShield.AbsorbRatio 0.5;				//How much damage gets converted.
		PowerManaShield.AbsorbType 'IDontReallyCare'; 	//The specific damagetype you want it to take care of.
		PowerManaShield.Absorbant 'Mana';				//The item that gets 'burned' from damage.
		PowerManaShield.HitFlash "Yellow", 0.1, 20; 	//When you get hit, you can get a visual cue. (Flash type, intensity, duration)
		PowerManaShield.HitSound "ManaShield/Hit";		//I guess that's self-explanatory.
		PowerManaShield.HoverActor "ManaShieldBall";	//The thing to hover above your head.
		Inventory.Icon "MNASHLD0";
		+INVENTORY.ADDITIVETIME
		+INVENTORY.PERSISTENTPOWER
	}
	
	//variable definitions.
	
	
	//Damage control.
	
	name absorbtype;	//Damage type absorption.
	name burntoken;		//What gets burned.
	float absorbratio;	//The burn ratio.
	
	//Hit flash control.
	
	color flashcolor;
	float flashpower;
	int flashtime;

	//The sound to play if you get hit while the powerup is active.

	String hitsound;
	
	//Balls away! AHH-AHHAHA-HA!
	
	name ballactor;
	actor ball;

	//Property definitions.
	
	
	//Absorbed into FATMAN'S FOLDS. DISGUSTING.
	
	property AbsorbRatio: absorbratio;
	property AbsorbType: absorbtype;
	property Absorbant: burntoken;
	property HitFlash: flashcolor, flashpower, flashtime;
	property HitSound: hitsound;
	property HoverActor: ballactor;
	
	//OVERRIDES, SON.
	
	//reset the ball if you switched levels, given I made it persistent power.
	override void DoEffect()
	{
		if(!ball)
		{
			ball = Spawn(ballactor);
			ball.target = owner.player.mo;
		}
		Super.DoEffect();
		return;
	}
	
	override void AbsorbDamage(int damage, name damageType, out int newdamage)
	{
		bool doresist;
		
		if(absorbtype == 'IDontReallyCare' || absorbtype == damageType)
		{
			doresist = true;
		}
		
		if(doresist)
		{
			int reduce;
			int tokens;

			reduce = ceil(absorbratio * damage);
			tokens = owner.player.mo.CountInv(burntoken);
			newdamage = max(0, max(damage - tokens, damage - reduce));
			
			if(0 < reduce) //Lol at A_TakeInventory taking everything with 0.
			{
				A_TakeInventory(burntoken, reduce);
				owner.player.mo.A_PlaySound(hitsound, 4, 0.5);
				owner.player.mo.A_SetBlend(flashcolor, flashpower, flashtime);
			}
		}
	}
	
	override void InitEffect()
	{
		super.InitEffect();
		ball = Spawn(ballactor);
		if(ball)
		{
			ball.target = owner.player.mo;
		}
	}
	
	override void EndEffect()
	{
		super.EndEffect();
		if(ball)
		{
			ball.SetState(FindState("Death"));
		}

		//Here I'd wager I unload the balls.
		//Good lord the amount of filthy puns I can just~ goddamnit.
	}
}

class ManaShieldBall : Actor
{
	Default
	{
		Projectile;
		Scale 0.2;
		Alpha 0.8;
		Renderstyle 'Add';
		+NOINTERACTION
		+NOCLIP
		+NOTRIGGER
		+NOTONAUTOMAP
	}
	
	int PowerHeight;
	int DeadHeight;
	
	States
	{
	Spawn:
		MSBL A 1 Bright {
			if(CountInv("PowerDeadmaker",AAPTR_TARGET)) {
				DeadHeight = 48;
			} else {
				DeadHeight = 0;
			}
			if(CountInv("PowerSuitToken",AAPTR_TARGET) && !target.GetCvar("GC_NoExtraHeight")) {
				PowerHeight = 20;
			} else {
				PowerHeight = 0;
			}
			A_Warp(AAPTR_TARGET,0,0,82.0 + PowerHeight + DeadHeight,0,
			WARPF_INTERPOLATE|WARPF_NOCHECKPOSITION);
			Return A_JumpIf(!CountInv("PowerManaShield",AAPTR_TARGET),"Null");
		}
		Loop;
	Death:
		MSBL A 1 Bright {
			A_FadeOut(0.1);
			A_SetScale(Scale.X * 0.95);
			A_JumpIf(Scale.X <= 0, "Null");
		}
		Loop;
	}
}

#endregion
