class CrateSpeedMedium : PowerSpeed
{
	Default
	{
		Powerup.Duration 0x7FFFFFFD; // about two years, give or take a tic
		Speed 0.66;
	}
}

class CrateSpeedSlow : PowerSpeed
{
	Default
	{
		Powerup.Duration 0x7FFFFFFD; // about two years, give or take a tic
		Speed 0.50;
	}
}

class CrateWeaponGiver : CustomInventory
{
	// [XA] Need this so the weapon is always selected even if the "neverswitchonpickup" cvar is set.
	States
	{
	Spawn:
		TNT1 A -1;
		Stop;
	Pickup:
		TNT1 A 0 A_GiveInventory("CrateWeapon");
		TNT1 A 0 A_SelectWeapon("CrateWeapon");
		Stop;
	}
}

class CrateWeapon : SquareDummyWeapon
{
	Default
	{
		+WEAPON.NOALERT;
		Weapon.BobRangeX 0;
		Weapon.BobSpeed 2.0;
		Weapon.BobStyle "Smooth";
	}
	States
	{
	Spawn:
		TNT1 A -1;
		Stop;
	Select:
		BAX0 A 0 A_GiveInventory("CrateSpeedMedium");
		Goto DoSelect;
	DoSelect:
		#### # 0 A_Raise();
		#### # 1 A_Raise();
		Loop;
	Deselect:
		#### # 0 A_GiveInventory("CrateDontSwitch");
		Goto TossCheck;
	Ready:
		BAX0 A 1 A_WeaponReady();
		Goto Ready;
	Fire:
		#### # 0 A_TakeInventory("CrateDontSwitch");
	TossCheck:
		#### # 0 A_JumpIf(CallACS("Crate_PositionCheck", 32), "TossSuccess");
	TossFail:
		#### # 1 A_WeaponReady(WRF_NOFIRE | WRF_NOSWITCH);
		#### # 0 A_Jump(256, "Ready");
		Goto Ready;
	TossSuccess:
		#### # 0 A_JumpIfInventory("CrateDontSwitch", 1, "TossSuccessDontSwitch");
		#### # 0 ACS_NamedExecuteWithResult("Crate_Drop", 32, 0);
		Goto TossSuccessContinue;
	TossSuccessDontSwitch:
		#### # 0 ACS_NamedExecuteWithResult("Crate_Drop", 32, 1);
	TossSuccessContinue:
		#### # 0 A_TakeInventory("CrateSpeedSlow");
		#### # 0 A_TakeInventory("CrateSpeedMedium");
		#### # 0 A_PlaySound("*release");
		#### # 0 A_Jump(256, "Toss");
	Toss:
		TNT1 A 0 A_TakeInventory("CrateWeapon");
		Goto End;
	End:
		TNT1 A 1; // [XA] Dummy state. Weapon is already taken away at this point.
		Loop;
	}
}
class CrateDontSwitch : Inventory { Default { +INVENTORY.UNTOSSABLE; } }

class TNTCrateWeaponGiver : CrateWeaponGiver
{
	States
	{
	Pickup:
		TNT1 A 0 A_GiveInventory("TNTCrateWeapon");
		TNT1 A 0 A_SelectWeapon("TNTCrateWeapon");
		Stop;
	}
}

class TNTCrateWeapon : CrateWeapon
{
	States
	{
	Select:
		BAX1 A 0 A_GiveInventory("CrateSpeedMedium");
		Goto DoSelect;
	Ready:
		BAX1 A 1 A_WeaponReady();
		Goto Ready;
	Toss:
		TNT1 A 0 A_TakeInventory("TNTCrateWeapon");
		Goto End;
	}
}

class NukageCrateWeaponGiver : CrateWeaponGiver
{
	States
	{
	Pickup:
		TNT1 A 0 A_GiveInventory("NukageCrateWeapon");
		TNT1 A 0 A_SelectWeapon("NukageCrateWeapon");
		Stop;
	}
}

class NukageCrateWeapon : CrateWeapon
{
	States
	{
	Select:
		BAX2 A 0 A_GiveInventory("CrateSpeedSlow"); // [XA] these are heavy :P
		Goto DoSelect;
	Ready:
		BAX2 A 1 A_WeaponReady();
		Goto Ready;
	Toss:
		TNT1 A 0 A_TakeInventory("NukageCrateWeapon");
		Goto End;
	}
}

class FizzoPopCrateWeaponGiver : CrateWeaponGiver
{
	States
	{
	Pickup:
		TNT1 A 0 A_GiveInventory("FizzoPopCrateWeapon");
		TNT1 A 0 A_SelectWeapon("FizzoPopCrateWeapon");
		Stop;
	}
}

class FizzoPopCrateWeapon : CrateWeapon
{
	States
	{
	Select:
		BAX3 A 0 A_GiveInventory("CrateSpeedMedium");
		Goto DoSelect;
	Ready:
		BAX3 A 1 A_WeaponReady();
		Goto Ready;
	Toss:
		TNT1 A 0 A_TakeInventory("FizzoPopCrateWeapon");
		Goto End;
	}
}
