//------------------------------------------------------------
// Carbine ammo class
//------------------------------------------------------------
Actor CarbineAmmo : Ammo
{
	+SERVERNETID
	Inventory.MaxAmount 60
}

//------------------------------------------------------------
// Carbine Alt-Fire cooldown class
//------------------------------------------------------------
Actor CarbineAltCD : Ammo
{
	+SERVERNETID 
	Inventory.MaxAmount 210
}

//------------------------------------------------------------
// Carbine weapon class
//------------------------------------------------------------
ACTOR PlasmaCarbine : LSWeaponBase
{
	// Vertical recoil constant.
	// Recoil resets before the next shot is fired, thus this is visual-only
	const float CARBINE_RECOIL = 0.4;

	// Properties
	Tag "Plasma Carbine"
	Weapon.AmmoType1 "CarbineAmmo"
	Weapon.AmmoType2 "CarbineAmmo"
	Weapon.AmmoUse1 1
	Weapon.AmmoUse2 5
	Weapon.SlotNumber 1
	Inventory.Icon "CARPA0"

	// States
	States
	{
	Spawn:
		CARP A 1 bright A_SetAngle(angle + 1.0)
		loop

	Ready:
		CARR A 1 A_WeaponReady
		loop

	Select:
		CARR AA 0 A_Raise
		CARR A 1 A_Raise
		loop

	Deselect:
		TNT1 A 0 A_ZoomFactor(1.0)
		CARR AA 0 A_Lower
		CARR A 1 A_Lower
		loop

	Fire:
		TNT1 A 0 A_GiveInventory("MainAmmoRegenCD", 999)
		TNT1 A 0 A_SpawnItemEX("BlueLightShoot")
		TNT1 A 0 A_PlaySound("PlasmaCarbine/Fire", CHAN_WEAPON)
		TNT1 A 0 A_PlaySound("PlasmaCarbine/FireFar", CHAN_6)
		TNT1 A 0 A_FireCustomMissile("PlasmaProjectile", 0.0, true, 4.0, 6.0, 0, 0.0)
		CARF A 1 A_SetPitch(pitch - CARBINE_RECOIL)
		CARF B 1 A_SetPitch(pitch + CARBINE_RECOIL * 0.5)
		CARF C 1 A_SetPitch(pitch + CARBINE_RECOIL * 0.5)
		CARF D 1
		goto Ready

	AltFire:
		TNT1 A 0 A_JumpIfInventory("MainAltCD", 1, "AltFireFail")
		Goto AltFireSuccess
	
	AltFireFail:
		TNT1 A 0 ACS_NamedExecuteAlways("AltFireFail")
		CARR A 1 A_WeaponReady(WRF_NOPRIMARY|WRF_NOSECONDARY)
		Goto Ready

	AltFireSuccess:
		TNT1 A 0 A_GiveInventory("MainAmmoRegenCD", 999)
		TNT1 A 0 A_GiveInventory("MainAltCD", 6)
		TNT1 A 0 A_SpawnItemEX("BlueLightShoot")
		TNT1 A 0 A_PlaySound("PlasmaCarbine/AltFire", CHAN_WEAPON)
		TNT1 A 0 A_PlaySound("PlasmaCarbine/AltFireFar", CHAN_6)
		TNT1 A 0 A_Recoil(2.0)
		TNT1 A 0 A_FireCustomMissile("PlasmaGrenade", 0.0, true, 4.0, 6.0, 0, 0.0)
		CARF ABCD 2 bright
		goto Ready
	}
}

//------------------------------------------------------------
// Carbine projectile
//------------------------------------------------------------
Actor PlasmaProjectile : FastProjectile
{
	// Properties
	Projectile
	Radius 8
	Height 12
	Scale 2.0
	Speed 256
	Damage (12)
	Decal "CarbineMainDecal"
	RenderStyle Add
	Obituary "%k > \cvPlasma Carbine\c- > %o"
	MissileType "PlasmaProjectileTrail"
	MissileHeight 12
	
	// Flags
	+BRIGHT
	
	// States
	States
	{
	Spawn:
		BSPK A 1
		loop

	XDeath:
		TNT1 A 0 A_GiveToTarget("HitSoundMedium", 1)
		Goto Death

	Death:
		TNT1 AAAAAAAAAAAAAAAA 0 A_SpawnItemEx("PlasmaDeathFX", 0, 0, 0, frandom(-4.0, 4.0), frandom(-4.0, 4.0), frandom(-4.0, 4.0), 0)//, SXF_NOUNLAGGED|SXF_SKIPOWNER)
		TNT1 A 0 A_PlaySound("PlasmaCarbine/Hit", CHAN_AUTO, 0.8)
		TNT1 A 0 A_SetScale(scalex * 0.75, scaley * 0.75)
		BSPK AAAAAAAAAA 1 A_FadeOut(0.1)
		TNT1 A 35
		stop
	}
}

//------------------------------------------------------------
// Carbine projectile visual effects
//------------------------------------------------------------
Actor PlasmaDeathFX : LSParticleBase
{
	// Properties
	Scale 0.6
	
	// Flags
	-NOGRAVITY
	-NOINTERACTION
	
	// States
	States
	{
	Spawn:
		TNT1 A 0 A_SetScale(scalex - 0.05, scaley - 0.05)
		BSPK A 1 A_FadeOut(0.05)
		loop
	}
}

//------------------------------------------------------------
// Projectile trail
//------------------------------------------------------------
Actor PlasmaProjectileTrail : LSParticleBase
{
	// Properties
	Scale 0.6

	// States
	States
	{
	Spawn:
		TNT1 A 0 A_SetScale(scalex * 0.8, scaley * 0.8)
		BSPK A 1 A_FadeOut(0.25)
		loop
	}
}

//------------------------------------------------------------
// Alt-fire grenade projectile
//------------------------------------------------------------
Actor PlasmaGrenade
{
	// Properties
	Projectile
	Radius 8
	Height 12
	Scale 0.4
	Speed 40
	Gravity 0.8
	Damage (0)
	Decal "CarbineAltDecal"
	ProjectileKickBack 200
	RenderStyle Add
	Obituary "%k > \cvPlasma Carbine (Alt)\c- > %o"

	// Flags
	-NOGRAVITY
	+BRIGHT
	+SEEKERMISSILE  // For hit sounds
	
	// Constants
	const int RADIUS_OUTER = 128;
	const int RADIUS_INNER = 32;

	// States
	States
	{
	Spawn:
		BGRN A 2 A_SpawnItemEx("PlasmaGrenadeTrail")
		loop

	XDeath:
		TNT1 A 0 A_GiveToTarget("HitSoundLarge", 1)
		goto Death

	Death:
		TNT1 A 0 A_SpawnItemEx("ExplosionSpawner")
		TNT1 A 0 A_SpawnItemEx("PlasmaGrenadeDeathFX")
		TNT1 AAAAAAAAAAAAAAAA 0 A_SpawnItemEx("PlasmaGrenadeDeathDot", 0, 0, 0, frandom(-16.0, 16.0), frandom(-16.0, 16.0), frandom(-16.0, 16.0), 0)
		TNT1 AAAAAAAAAAAAAAAA 0 A_SpawnItemEx("PlasmaGrenadeDeathSpark", 0, 0, 0, frandom(-16.0, 16.0), frandom(-16.0, 16.0), frandom(-16.0, 16.0), 0)
		TNT1 A 0 A_Quake(2, 8, 0, 384)
		TNT1 A 0 A_Explode(50, RADIUS_OUTER, XF_HURTSOURCE, true, RADIUS_INNER)
		TNT1 A 0 A_SeekerMissile(360, 360, SMF_LOOK, 256, RADIUS_OUTER / 128)
		TNT1 A 0 A_RearrangePointers(AAPTR_DEFAULT, AAPTR_TRACER)
		TNT1 A 0 A_JumpIfMasterCloser(RADIUS_INNER - radius, "HitMedium")
		TNT1 A 0 A_JumpIfMasterCloser(RADIUS_OUTER - radius, "HitSoft")
		goto FinishDeath

	HitMedium:
		TNT1 A 0 A_GiveToTarget("HitSoundMedium", 1)
		goto FinishDeath

	HitSoft:
		TNT1 A 0 A_GiveToTarget("HitSoundSmall", 1)
		goto FinishDeath

	FinishDeath:
		TNT1 A 105  // Required for unlagged
		stop
	}
}

//------------------------------------------------------------
// Grenade trail (visual effect)
//------------------------------------------------------------
Actor PlasmaGrenadeTrail : LSParticleBase
{
	// Properties
	Scale 0.2
	
	// States
	States
	{
	Spawn:
		BRNG AAAAAA 1 nodelay A_SetScale(scalex * 1.025, scaley * 1.025)
		goto Looped

	Looped:
		TNT1 A 0 A_SetScale(scalex * 1.025, scaley * 1.025)
		BRNG A 1 A_FadeOut(0.02)
		loop
	}
}

//------------------------------------------------------------
// Grenade death explosion ring
//------------------------------------------------------------
Actor PlasmaGrenadeDeathFX : LSParticleBase
{
	// Properties
	Scale 0.8
	
	// States
	States
	{
	Spawn:
		TNT1 A 0 A_SetScale(scalex * 1.2, scaley * 1.2)
		BRNG A 1 A_FadeOut(0.1)
		loop
	}
}

//------------------------------------------------------------
// Grenade death spark
//------------------------------------------------------------
Actor PlasmaGrenadeDeathSpark : LSParticleBase
{
	// Properties
	Scale 1.0
	
	// States
	States
	{
	Spawn:
		TNT1 A 0 A_SetScale(scalex - 0.05, scaley - 0.05)
		BSPK A 1 A_FadeOut(0.05)
		loop
	}
}

//------------------------------------------------------------
// Grenade death dot (lingers and bounces)
//------------------------------------------------------------
Actor PlasmaGrenadeDeathDot : LSBouncingParticleBase
{
	// Properties
	Scale 0.4
	Gravity 1.0
	BounceFactor 0.6
	BounceCount 3
	
	// States
	States
	{
	Spawn:
		BSPK A 1
		loop

	Death:
		BSPK A 35
		BSPK AAAAAAAAAAAAAAAAAAAA 1 A_FadeOut(0.05)
		TNT1 A 1
		stop
	}
}