#region Poweroops
//$Color 10

class Medbag : CustomInventory
{
	Default
	{
		//$Title Medic Bag
		Inventory.Amount 100;
		Inventory.MaxAmount 500;
		Inventory.InterHubAmount 0x7FFFFFFF;
		Inventory.PickupSound "Health/Pickup";
		Inventory.UseSound "";
		Inventory.PickupMessage "You are in possession of a \c[red]medic bag\c-!";
		Inventory.Icon 'ARTIMBAG';
		Tag "Medbag";
		+INVENTORY.INVBAR
		+INVENTORY.UNDROPPABLE
	}
	
	States
	{
	Spawn:
		MBAG A -1;
		Stop;
	Use:
		TNT1 A 0 A_JumpIfInventory("PowerMedbagFake",1,"Nope");
		TNT1 A 0 //ACS_NamedExecuteAlways("MedbagHealing");
		{
			if(invoker.owner.Health < PlayerPawn(invoker.owner).MaxHealth)
			{
				int HealthDifference = PlayerPawn(invoker.owner).MaxHealth - invoker.owner.Health;
				if(HealthDifference >= CountInv("Medbag"))
				{
					// Not enough medbag left to cover the difference, deplete ourselves after being used
					A_GiveInventory("Health", CountInv("Medbag"));
					A_SetInventory("Medbag", 0);
				}
				else
				{
					// Cover the difference and call it a day.
					A_GiveInventory("Health", HealthDifference);
					A_SetInventory("Medbag", CountInv("Medbag") - HealthDifference);
				}
				A_GiveInventory("MedbagEffect", 1);
				A_GiveInventory("MedbagDelay", 1);
			}
		}
		Fail;
	Nope:
		TNT1 A 0;
		Fail;
	} 
}

class MedbagDelay : PowerupGiver
{
	Default
	{
		Inventory.Amount 0;
		Powerup.Type "PowerMedbagFake";
		Powerup.Duration -15;
		+INVENTORY.AUTOACTIVATE
		+INVENTORY.ADDITIVETIME
	}
}

class PowerMedbagFake : PowerDamage
{	
	Default
	{
		Inventory.Icon "HEALED0";
		DamageFactor "Normal", 1.0;
		Powerup.Duration -15; // kd: Was needed for the cooldown on HUD
	}
}

class MedbagEffect : CustomInventory
{
	Default
	{
		+INVENTORY.AUTOACTIVATE
	}
	
	States
	{	
		Spawn:
			TNT1 A 0;
			Stop;
		Pickup:
			TNT1 A 0 A_PlaySound("Health/Use",3);
			TNT1 A 0 A_SpawnItemEx("MedbagHealEffect",0,0,0,0,0,0,0,SXF_CLIENTSIDE);
			TNT1 A 0 A_SetBlend("Red",0.15,25);
			Stop;
	}
}
/* Kinda useless.
class MedbagHealth : Health
{
	Default
	{
		Inventory.Amount 1;
	}
}
*/
class MedbagGiver : CustomInventory
{
	Default
	{
		Inventory.MaxAmount 0;
		+INVENTORY.AUTOACTIVATE
		+INVENTORY.ALWAYSPICKUP
	}
	States
	{
	Spawn:
		TNT1 A -1;
		Stop;
	Pickup:
		TNT1 A 0 A_GiveInventory("Medbag",100);
		Stop;
	}
}

class MedbagHealEffect : Actor
{
	Default
	{
		ReactionTime 50;
		+NOINTERACTION
	}
	
	States
	{
	Spawn:
		TNT1 A 0 NoDelay A_SpawnItemEx("MedbagCross",frandom(20.0,-20.0),
			frandom(10.0,-10.0),frandom(50.0,0.0),frandom(0.75,0.5),
			frandom(0.75,0.5),frandom(0.75,0.5),frandom(0.0,360.0),SXF_CLIENTSIDE);
		TNT1 A 0 A_Countdown;
		Loop;
	Death:
		TNT1 A 30;
		Stop;
	}
}

class MedbagCross : Actor
{
	Default
	{
		Renderstyle 'Add';
		Alpha 0.0;
		+NOINTERACTION
		+NOTONAUTOMAP
		+FORCEXYBILLBOARD
	}
	
	States
	{
	Spawn:
		MBAG B 0 NoDelay A_SetScale(frandom(0.1,0.3));
		MBAG B 0 A_FadeIn(frandom(0.7,1.0));
	Looplet:
		MBAG B 1 Bright {
			A_ChangeVelocity(frandom(0.3,-0.3),frandom(0.3,-0.3),frandom(0.3,-0.1),CVF_RELATIVE);
			A_SetScale(Scale.X - 0.005);
			A_FadeOut(0.04);
			Return A_JumpIf(Scale.X <= 0, "Null");
		}
		Loop;
	}
}

#endregion
