Class StingerAmmo : Ammo
{
	Default
	{
		Tag "$T_STINGERAMMO";
		Inventory.Icon "I_Stingr";
		Inventory.PickupMessage "";
		Inventory.Amount 40;
		Inventory.MaxAmount 200;
		Ammo.BackpackAmount 80;
		Ammo.BackpackMaxAmount 400;
		Ammo.DropAmount 10;
	}
	override String PickupMessage()
	{
		return String.Format("%s%d%s",StringTable.Localize("$I_STINGERAMMOL"),Amount,StringTable.Localize("$I_STINGERAMMOR"));
	}
	States
	{
	Spawn:
		SAMO A -1;
		Stop;
	}
}

Class StingerProjectile : Actor
{
	Default
	{
		Obituary "$O_STINGER";
		DamageType 'shot';
		DamageFunction Random[Stinger](15,25);
		Speed 40;
		Radius 2;
		Height 2;
		PROJECTILE;
		+SKYEXPLODE;
		+BLOODSPLATTER;
	}
	States
	{
	Spawn:
		TPRJ A -1 Bright;
		Stop;
	Death:
		TNT1 A 0
		{
			if ( !Random[Stinger](0,2) ) A_PlaySound("stinger/hit2",CHAN_BODY,0.5);
			else A_PlaySound("stinger/hit",CHAN_BODY,0.6);
		}
		TPRJ BCDEFG 2 Bright;
		Stop;
	XDeath:
		TNT1 A 1 A_PlaySound("stinger/flesh");
		Stop;
	}
}

Class Stinger : UTWeapon
{
	Default
	{
		Tag "$T_STINGER";
		Inventory.PickupMessage "$I_STINGER";
		Weapon.UpSound "stinger/select";
		Weapon.SlotNumber 3;
		Weapon.SelectionOrder 7;
		Weapon.AmmoType "StingerAmmo";
		Weapon.AmmoUse 1;
		Weapon.AmmoType2 "StingerAmmo";
		Weapon.AmmoUse2 1;
		Weapon.AmmoGive 40;
		UTWeapon.DropAmmo 20;
	}
	action void A_StingerFire()
	{
		Weapon weap = Weapon(invoker);
		if ( !weap ) return;
		if ( weap.Ammo1.Amount <= 0 ) return;
		if ( !weap.DepleteAmmo(weap.bAltFire,true,1) ) return;
		UTMainHandler.DoFlash(self,Color(16,0,64,255),1);
		UTMainHandler.DoSwing(self,(FRandom[Stinger](-0.1,-0.2),FRandom[Stinger](-0.1,0.1)),4,-1.5,2,SWING_Spring,2,2);
		A_AlertMonsters();
		A_QuakeEx(1,1,1,4,0,1,"",QF_RELATIVE|QF_SCALEDOWN,rollIntensity:0.1);
		Vector3 x, y, z;
		[x, y, z] = dt_Matrix4.GetAxes(pitch,angle,roll);
		Vector3 origin = (pos.x,pos.y,player.viewz)+10.0*x+8.0*y-9.0*z;
		Actor p = Spawn("StingerProjectile",origin);
		p.angle = angle;
		p.pitch = BulletSlope();
		p.vel = (cos(p.angle)*cos(p.pitch),sin(p.angle)*cos(p.pitch),-sin(p.pitch))*p.speed;
		p.target = self;
	}
	action void A_StingerAltFire()
	{
		Weapon weap = Weapon(invoker);
		if ( !weap ) return;
		if ( weap.Ammo1.Amount <= 0 ) return;
		UTMainHandler.DoFlash(self,Color(16,0,64,255),1);
		UTMainHandler.DoSwing(self,(FRandom[Stinger](-0.3,-0.8),FRandom[Stinger](-0.4,0.4)),4,-1,3,SWING_Spring,3,5);
		A_AlertMonsters();
		A_QuakeEx(1,1,1,4,0,1,"",QF_RELATIVE|QF_SCALEDOWN,rollIntensity:0.1);
		Vector3 x, y, z;
		[x, y, z] = dt_Matrix4.GetAxes(pitch,angle,roll);
		Vector3 origin = (pos.x,pos.y,player.viewz)+10.0*x+8.0*y-9.0*z;
		[x, y, z] = dt_Matrix4.GetAxes(BulletSlope(),angle,roll);
		A_PlaySound("stinger/altfire",CHAN_WEAPON);
		Actor p;
		double a, s;
		Vector3 dir;
		for ( int i=0; i<4; i++ )
		{
			if ( weap.Ammo1.Amount <= 0 ) return;
			if ( !weap.DepleteAmmo(weap.bAltFire,true,1) ) return;
			a = FRandom[Stinger](0,360);
			s = FRandom[Stinger](0,0.08);
			dir = (x+y*cos(a)*s+z*sin(a)*s).unit();
			p = Spawn("StingerProjectile",origin);
			p.angle = atan2(dir.y,dir.x);
			p.pitch = asin(-dir.z);
			p.vel = (cos(p.angle)*cos(p.pitch),sin(p.angle)*cos(p.pitch),-sin(p.pitch))*p.speed;
			p.target = self;
		}
	}
	States
	{
	Spawn:
		STNP A -1;
		Stop;
		STNP B -1;
		Stop;
	Select:
		STNS A 1 A_Raise(int.max);
		Wait;
	Ready:
		STNS ABCDEFGHIJKLMNOPQRSTU 1 A_WeaponReady(WRF_NOFIRE);
	Idle:
		STNI A 1
		{
			A_CheckReload();
			A_WeaponReady();
		}
		Wait;
	Fire:
		STNF A 2
		{
			A_PlaySound("stinger/fire",CHAN_WEAPON);
			A_StingerFire();
			A_Overlay(PSP_FLASH,"MFlash");
			A_OverlayFlags(PSP_FLASH,PSPF_RenderStyle,true);
			A_OverlayRenderstyle(PSP_FLASH,STYLE_Add);
		}
		STNF BC 2;
		STNI A 3;
		STNI A 0 A_Refire(1);
		Goto Idle;
		STNI A 0 A_PlaySound("stinger/hold",CHAN_WEAPON,1.0,true);
	Hold:
		STNH A 1
		{
			A_StingerFire();
			A_Overlay(PSP_FLASH,"MFlashHold");
			A_OverlayFlags(PSP_FLASH,PSPF_RenderStyle,true);
			A_OverlayRenderstyle(PSP_FLASH,STYLE_Add);
		}
		STNH BCDEFG 1;
		STNH A 0 A_Refire();
		STNH A 2 A_PlaySound("stinger/release",CHAN_WEAPON);
		Goto Idle;
	AltFire:
		STNF A 2
		{
			A_StingerAltFire();
			A_Overlay(PSP_FLASH,"MFlash");
			A_OverlayFlags(PSP_FLASH,PSPF_RenderStyle,true);
			A_OverlayRenderstyle(PSP_FLASH,STYLE_Add);
		}
		STNF BC 2;
		STNI A 35;	// yes, 1 second cooldown
		Goto Idle;
	Deselect:
		STND ABCDEFGHIJK 1;
		STND K 1 A_Lower(int.max);
		Wait;
	MFlash:
		STFF ABC 2 Bright;
		Stop;
	MFlashHold:
		STFH ABCDEFG 1 Bright;
		Stop;
	}
}
