//=============================================================================
// Minigun.
//=============================================================================
class Minigun expands Weapon;

#exec AUDIO IMPORT FILE="Sounds\General\Bulletr2.WAV" NAME="Bulletr2"  GROUP="General"

#exec MESH IMPORT MESH=minigun ANIVFILE=MODELS\smini_a.3D DATAFILE=MODELS\smini_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=minigun X=0 Y=0 Z=0 YAW=64 PITCH=-7 ROLL=-62 
#exec MESH SEQUENCE MESH=minigun SEQ=All     STARTFRAME=0  NUMFRAMES=119
#exec MESH SEQUENCE MESH=minigun SEQ=Select  STARTFRAME=0  NUMFRAMES=23 RATE=34 GROUP=Select
#exec MESH SEQUENCE MESH=minigun SEQ=Still   STARTFRAME=23  NUMFRAMES=1
#exec MESH SEQUENCE MESH=minigun SEQ=Fire    STARTFRAME=24  NUMFRAMES=30
#exec MESH SEQUENCE MESH=minigun SEQ=Wind    STARTFRAME=24  NUMFRAMES=15
#exec MESH SEQUENCE MESH=minigun SEQ=Shoot1  STARTFRAME=39  NUMFRAMES=15
#exec MESH SEQUENCE MESH=minigun SEQ=Shoot2  STARTFRAME=54  NUMFRAMES=20
#exec MESH SEQUENCE MESH=minigun SEQ=Unwind  STARTFRAME=74  NUMFRAMES=15
#exec MESH SEQUENCE MESH=minigun SEQ=Cock    STARTFRAME=89  NUMFRAMES=20
#exec MESH SEQUENCE MESH=minigun SEQ=Down    STARTFRAME=109 NUMFRAMES=10
#exec TEXTURE IMPORT NAME=minigun1 FILE=MODELS\smini.PCX GROUP="Skins" 
#exec OBJ LOAD FILE=Textures\fireeffect13.utx PACKAGE=UNREALI
#exec MESHMAP SCALE MESHMAP=minigun X=0.0055 Y=0.0055 Z=0.011
#exec MESHMAP SETTEXTURE MESHMAP=minigun NUM=1 TEXTURE=minigun1
#exec MESHMAP SETTEXTURE MESHMAP=minigun NUM=0 TEXTURE=FireEffect13

#exec MESH IMPORT MESH=minipick ANIVFILE=MODELS\minipi_a.3D DATAFILE=MODELS\minipi_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=minipick X=0 Y=-100 Z=-40 YAW=64 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=minipick SEQ=All     STARTFRAME=0  NUMFRAMES=1
#exec TEXTURE IMPORT NAME=minigun1 FILE=MODELS\smini.PCX GROUP="Skins" 
#exec MESHMAP SCALE MESHMAP=minipick X=0.04 Y=0.04 Z=0.08
#exec MESHMAP SETTEXTURE MESHMAP=minipick NUM=1 TEXTURE=minigun1

#exec MESH IMPORT MESH=SMini3 ANIVFILE=MODELS\Smini3_a.3D DATAFILE=MODELS\smini3_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=SMini3 X=0 Y=60 Z=-10 YAW=64 ROLL=0
#exec MESH SEQUENCE MESH=SMini3 SEQ=All STARTFRAME=0  NUMFRAMES=11
#exec MESH SEQUENCE MESH=SMini3 SEQ=Fire STARTFRAME=0  NUMFRAMES=10 RATE=20.0
#exec MESH SEQUENCE MESH=SMini3 SEQ=Shoot1  STARTFRAME=0  NUMFRAMES=10 RATE=20.0
#exec MESH SEQUENCE MESH=SMini3 SEQ=Shoot2  STARTFRAME=0  NUMFRAMES=10 RATE=20.0
#exec MESH SEQUENCE MESH=SMini3 SEQ=Still STARTFRAME=10  NUMFRAMES=1
#exec MESH SEQUENCE MESH=SMini3 SEQ=Select STARTFRAME=10  NUMFRAMES=1
#exec MESH SEQUENCE MESH=SMini3 SEQ=Wind STARTFRAME=10  NUMFRAMES=1
#exec MESH SEQUENCE MESH=SMini3 SEQ=UnWind STARTFRAME=10  NUMFRAMES=1
#exec MESH SEQUENCE MESH=SMini3 SEQ=Cock STARTFRAME=10  NUMFRAMES=1
#exec MESH SEQUENCE MESH=SMini3 SEQ=Down STARTFRAME=10  NUMFRAMES=1
#exec TEXTURE IMPORT NAME=minigun1 FILE=MODELS\smini.PCX GROUP="Skins"
#exec OBJ LOAD FILE=textures\FireEffect18.utx PACKAGE=UNREALI
#exec MESHMAP SCALE MESHMAP=SMini3 X=0.25 Y=0.25 Z=0.5
#exec MESHMAP SETTEXTURE MESHMAP=SMini3 NUM=1 TEXTURE=minigun1
#exec MESHMAP SETTEXTURE MESHMAP=SMini3 NUM=0 TEXTURE=FireEffect18

#exec MESH NOTIFY MESH=minigun SEQ=Shoot1 TIME=0.05 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot1 TIME=0.25 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot1 TIME=0.45 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot1 TIME=0.65 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot1 TIME=0.85 FUNCTION=GenerateBullet

#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.1 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.2 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.3 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.4 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.5 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.6 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.7 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.8 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Fire TIME=0.9 FUNCTION=GenerateBullet

#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.05 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.15 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.25 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.35 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.45 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.55 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.65 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.75 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.85 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=minigun SEQ=Shoot2 TIME=0.95 FUNCTION=GenerateBullet

#exec AUDIO IMPORT FILE="Sounds\minigun\shot.WAV" NAME="MiniFire" GROUP="Minigun"
#exec AUDIO IMPORT FILE="Sounds\minigun\MAlt3.WAV" NAME="MiniAltFire" GROUP="Minigun"
#exec AUDIO IMPORT FILE="Sounds\minigun\MPickup2.WAV" NAME="MiniSelect" GROUP="Minigun"
#exec AUDIO IMPORT FILE="Sounds\minigun\Wind4.WAV" NAME="MiniWind" GROUP="Minigun"
#exec AUDIO IMPORT FILE="Sounds\minigun\UnWind4.WAV" NAME="MiniUnWind" GROUP="Minigun"

var float ShotAccuracy, Count;
var bool bOutOfAmmo;
var OverHeatLight s;

function GenerateBullet()
{
	if ( AmmoType.UseAmmo(1) ) 
		TraceFire(ShotAccuracy);
	else
		GotoState('FinishFire');
}

function ProcessTraceHit(Actor Other, Vector HitLocation, Vector HitNormal, Vector X, Vector Y, Vector Z)
{
	local BulletHit Bullet;
	local vector FireDir;
	Owner.PlaySound(FireSound, SLOT_None, 1.0*Pawn(Owner).SoundDampening);	  //Power up
	if ( PlayerPawn(Owner) != None )
		PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
	
	if (Other==Level) SpawnEffect(HitLocation, HitNormal);
	else if (Other!=self && Other!=Owner ) 
	{
		spawn(class'SpriteSmokePuff',,,HitLocation+HitNormal*9);
		FireDir = vector(AdjustedAim);		
		if ( Other.IsA('ScriptedPawn') && (FRand() < 0.2) )
			Pawn(Other).WarnTarget(Pawn(Owner), 500, FireDir);	
		Other.Damage(10, Pawn(Owner), HitLocation, 3000.0*FireDir, 'shot');
	}
}

function Fire( float Value )
{
	bFireMem = false;
	bAltFireMem = false;
	Enable('Tick');
	if ( (Count<1) && AmmoType.UseAmmo(1) )
	{
		CheckVisibility();
		if ( PlayerPawn(Owner) != None )
			PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
		Owner.PlaySound(Misc1Sound, SLOT_None, 0.6*Pawn(Owner).SoundDampening);	
		bPointing=True;
		ShotAccuracy = 0.1;
		PlayFiring();
		GotoState('NormalFire');
	}	
	else GoToState('Idle');
}

function AltFire( float Value )
{
	bFireMem = false;
	bAltFireMem = false;
	Enable('Tick');
	if ( (Count<1) && AmmoType.UseAmmo(1) )
	{
		CheckVisibility();
		Owner.PlaySound(Misc1Sound, SLOT_None, 0.6*Pawn(Owner).SoundDampening);	
		bPointing=True;
		ShotAccuracy = 0.8;
		PlayAltFiring();	
		MakeLight();
		GoToState('AltFiring');		
	}
	else GoToState('Idle');
}

function MakeLight()
{
	local Vector X,Y,Z;

	if (s!=None) s.Destroy();
	GetAxes(Pawn(Owner).ViewRotation,X,Y,Z);
	s = Spawn(Class'OverHeatLight',,,Owner.Location+CalcDrawOffset()+X*25+Z*25);
	s.SetBase(Pawn(Owner));
	if (Count>3) s.LightBrightness = int((Count-3)*50);
}

function PlayFiring()
{
	LoopAnim('Shoot1',0.8, 0.05);
}

function PlayAltFiring()
{
	Owner.PlaySound(AltFireSound, SLOT_None, 0.6*Pawn(Owner).SoundDampening);	
	PlayAnim('Fire',0.8, 0.05);
}

function SpawnEffect(vector HitLocation, vector HitNormal)
{
	local Vector X,Y,Z;

	GetAxes(Pawn(Owner).ViewRotation,X,Y,Z);	
	if (FRand()< 0.2) 
		spawn(class'Chip',,,HitLocation+HitNormal*5,Rotation(HitNormal));
	if (FRand()<0.3)	spawn(class'SmallSpark2',,,HitLocation+HitNormal*5,rotation(HitNormal*2+VRand()));
	spawn(class'SpriteSmokePuff',,,HitLocation+HitNormal*9);		
	spawn(class'WeaponLight',,,Location+X*25+Z*15);
	if (FRand()<0.1) PlaySound(Misc3Sound, SLOT_Misc, 0.5,,, 0.5+FRand());		
}



////////////////////////////////////////////////////////
state FinishFire
{
	function Fire(float F) {}
	function AltFire(float F) {}

Begin:
	Owner.PlaySound(Misc2Sound, SLOT_None, 0.6*Pawn(Owner).SoundDampening);  //Finish firing, power down	
	PlayAnim('UnWind',0.8, 0.05);
	FinishAnim();
	Finish();
}

///////////////////////////////////////////////////////
state NormalFire
{
Begin:
	FinishAnim();
	if ( (AmmoType.AmmoAmount<=0) || (Pawn(Owner).Weapon != self) )
		GotoState('FinishFire');
	else if ( bFireMem || Pawn(Owner).bFire!=0 )
		CallGlobal.Fire(0);
	else if ( bAltFireMem || Pawn(Owner).bAltFire!=0 )
		CallGlobal.AltFire(0);
	else 
		GotoState('FinishFire');
}

////////////////////////////////////////////////////////
state AltFiring
{
	function Tick( float DeltaTime )
	{
		if( (pawn(Owner).bAltFire==0) || bOutOfAmmo) 
			GoToState('FinishFire');
//		Count += DeltaTime;
//		if (Count>3) s.LightBrightness = Min(int((Count-3)*50),255);
//		if (Count>6) GoToState('OverHeat');
	}

Begin:
	FinishAnim();
	LoopAnim('Shoot2',0.8);
}

////////////////////////////////////////////////////////
state OverHeat
{
ignores Fire, AltFire;

	function Tick( float DeltaTime )
	{
		if( (pawn(Owner).bAltFire==0) || bOutOfAmmo) GoToState('FinishFire');
		Count += DeltaTime;
		IF (Count>3) s.LightBrightness = int((Count-3)*50);	
		if( Count > 7) GoToState('FinishFire');
	}

Begin:
	ShotAccuracy = 0.6;
	Owner.PlaySound(Misc3Sound, SLOT_None, 0.6*Pawn(Owner).SoundDampening);  //Overheating
	LoopAnim('Shoot1',0.3);
}



///////////////////////////////////////////////////////////
State DownWeapon
{
ignores Fire, AltFire;

	function Tick( float DeltaTime )
	{
		if( Count > 3) {
			Count -= DeltaTime;			
		}
		else {
			Disable('Tick');
			Count=0;
		}
	}


Begin:
	TweenDown();
	FinishAnim();
	if (s!=None) s.Destroy();
	bOnlyOwnerSee = false;
	Pawn(Owner).ChangedWeapon();
}


///////////////////////////////////////////////////////////
state Idle
{
	function Tick( float DeltaTime )
	{
		if( Count > 3) {
			Count -= DeltaTime;
			if (s!=None && Count>3) s.LightBrightness = int((Count-3)*50);			
		}
		else {
			Disable('Tick');
			if (s!=None) s.Destroy();
			Count=0;
		}
	}
Begin:
	PlayAnim('Still');
	bPointing=False;
	if ( (AmmoType != None) && (AmmoType.AmmoAmount<=0) ) 
		Pawn(Owner).SwitchToBestWeapon();  //Goto Weapon that has Ammo
	Disable('AnimEnd');
	PlayIdleAnim();
	
}

