//=============================================================================
// Rifle.
//=============================================================================
class Rifle expands Weapon;

#exec MESH IMPORT MESH=Rifle ANIVFILE=MODELS\Rifle_a.3D DATAFILE=MODELS\Rifle_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=Rifle X=0 Y=0 Z=0 YAW=-60 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=Rifle SEQ=All         STARTFRAME=0   NUMFRAMES=101
#exec MESH SEQUENCE MESH=Rifle SEQ=Select      STARTFRAME=0   NUMFRAMES=20 RATE=27 GROUP=Select
#exec MESH SEQUENCE MESH=Rifle SEQ=Still       STARTFRAME=20  NUMFRAMES=1
#exec MESH SEQUENCE MESH=Rifle SEQ=Fire        STARTFRAME=21  NUMFRAMES=10
#exec MESH SEQUENCE MESH=Rifle SEQ=ScopeFire   STARTFRAME=31  NUMFRAMES=10
#exec MESH SEQUENCE MESH=Rifle SEQ=ScopeUp     STARTFRAME=41  NUMFRAMES=14
#exec MESH SEQUENCE MESH=Rifle SEQ=StillScope  STARTFRAME=55  NUMFRAMES=1
#exec MESH SEQUENCE MESH=Rifle SEQ=DownWScope  STARTFRAME=56  NUMFRAMES=15
#exec MESH SEQUENCE MESH=Rifle SEQ=Down        STARTFRAME=71  NUMFRAMES=15
#exec MESH SEQUENCE MESH=Rifle SEQ=ScopeDown   STARTFRAME=86  NUMFRAMES=15
#exec TEXTURE IMPORT NAME=JRifle1 FILE=MODELS\Rifle.PCX GROUP=Skins
#exec OBJ LOAD FILE=textures\FireEffect18.utx PACKAGE=UNREALI
#exec MESHMAP SCALE MESHMAP=Rifle X=0.005 Y=0.004 Z=0.01
#exec MESHMAP SETTEXTURE MESHMAP=Rifle NUM=1 TEXTURE=JRifle1
#exec MESHMAP SETTEXTURE MESHMAP=Rifle NUM=0 TEXTURE=FireEffect18

#exec MESH IMPORT MESH=RiPick ANIVFILE=MODELS\RiPick_a.3D DATAFILE=MODELS\RiPick_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=RiPick X=0 Y=0 Z=0 YAW=-64 
#exec MESH SEQUENCE MESH=RiPick SEQ=All         STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=RiPick SEQ=Still       STARTFRAME=0   NUMFRAMES=1
#exec TEXTURE IMPORT NAME=JRifle1 FILE=MODELS\Rifle.PCX GROUP=Skins
#exec MESHMAP SCALE MESHMAP=RiPick X=0.06 Y=0.06 Z=0.12
#exec MESHMAP SETTEXTURE MESHMAP=RiPick NUM=1 TEXTURE=JRifle1

#exec MESH IMPORT MESH=Rifle3rd ANIVFILE=MODELS\Rifle3_a.3D DATAFILE=MODELS\Rifle3_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=Rifle3rd X=0 Y=-550 Z=-10 YAW=-64 ROLL=0
#exec MESH SEQUENCE MESH=Rifle3rd SEQ=All  STARTFRAME=0  NUMFRAMES=10
#exec MESH SEQUENCE MESH=Rifle3rd SEQ=Still  STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=Rifle3rd SEQ=Fire  STARTFRAME=1  NUMFRAMES=9
#exec MESHMAP SCALE MESHMAP=Rifle3rd X=0.05 Y=0.05 Z=0.1
#exec MESHMAP SETTEXTURE MESHMAP=Rifle3rd NUM=1 TEXTURE=JRifle1
#exec MESHMAP SETTEXTURE MESHMAP=Rifle3rd NUM=0 TEXTURE=FireEffect18

#exec AUDIO IMPORT FILE="Sounds\rifle\RPICKUP1.WAV" NAME="RiflePickup" GROUP="Rifle"
#exec AUDIO IMPORT FILE="Sounds\rifle\RSHOT9.WAV" NAME="RifleShot" GROUP="Rifle"
#exec AUDIO IMPORT FILE="Sounds\rifle\LIGHTON1.UFX" NAME="RifleLightOn" GROUP="Rifle"
#exec AUDIO IMPORT FILE="Sounds\rifle\LIGHTO~1.UFX" NAME="RifleLightOff"  GROUP="Rifle"

var int NumFire;
var bool bScopeUp;

function AltFire( float Value )
{
	bFireMem = false;
	bAltFireMem = false;
	if (AmmoType.UseAmmo(1))
	{
		CheckVisibility();
		bPointing=True;
		TraceFire(3.0);
		PlayAltFiring();
		GotoState('AltFiring');
	}
}

function Timer()
{
	local actor targ;
	local float bestAim, bestDist;
	local vector FireDir;

	bestAim = 0.95;
	if ( Pawn(Owner) == None )
	{
		GotoState('');
		return;
	}
	FireDir = vector(Pawn(Owner).ViewRotation);
	targ = Pawn(Owner).PickTarget(bestAim, bestDist, FireDir, Owner.Location);
	if ( Pawn(targ) != None )
	{
		bPointing = true;
		Pawn(targ).WarnTarget(Pawn(Owner), 200, FireDir);
	}
	else 
		bPointing = false;
}	

function PlayAltFiring()
{
	if ( PlayerPawn(Owner) != None )
		PlayerPawn(Owner).shakeview(ShakeTime, ShakeMag, ShakeVert);
	Owner.PlaySound(FireSound, SLOT_None,Pawn(Owner).SoundDampening*4.0);
	PlayAnim('Fire', 2.0,0.05);
}

function PlayFiring()
{
	Owner.PlaySound(FireSound, SLOT_None, Pawn(Owner).SoundDampening*4.0);		
	PlayAnim('Fire', 0.7,0.05);
}

function ProcessTraceHit(Actor Other, Vector HitLocation, Vector HitNormal, Vector X, Vector Y, Vector Z)
{
	local shellcase s;

	s = Spawn(class'ShellCase',Pawn(Owner), '', Owner.Location + CalcDrawOffset() + 30 * X + 2.8 * FireOffset.Y * Y - Z * 1);
	if ( s != None ) 
	{
		s.DrawScale = 2.0;
		s.Eject(((FRand()*0.3+0.4)*X + (FRand()*0.2+0.2)*Y + (FRand()*0.3+1.0) * Z)*160);              
	}
	spawn(class'WeaponLight', self, '', Owner.Location + CalcDrawOffset() + 30.0 * X);
	if (Other == Level) SpawnEffect(HitLocation, HitNormal);	
	else if ((Other != self) && (Other != Owner)) 
	{
		if ( Other.IsA('Pawn') && (HitLocation.Z - Other.Location.Z > 0.6 * Other.CollisionHeight) )
			Other.Damage(60, Pawn(Owner), HitLocation, 17000 * X, 'shot');
		else
			Other.Damage(40,  Pawn(Owner), HitLocation, 17000.0*X, 'shot');	
		spawn(class'SpriteSmokePuff',,,HitLocation+HitNormal*9);	
	}
}

function Finish()
{
	if ( bFireMem || bAltFireMem || ((Pawn(Owner).bFire!=0) || (Pawn(Owner).bAltFire!=0)) && (FRand() < 0.6) )
		Timer();
	CallParent.Finish();
}

state Idle
{

	function BeginState()
	{
		bPointing = false;
		SetTimer(0.3 + 1.6 * FRand(), true);
		CallParent.BeginState();
	}

	function EndState()
	{
		SetTimer(0.0, false);
		CallParent.EndState();
	}
}
///////////////////////////////////////////////////////
state AltFiring
{
function BeginState()
{
	NumFire = 0;
}
Begin:
	FinishAnim();
	NumFire++;
	if (NumFire<3)
		CallGlobal.AltFire(0);
	if (NumFire==3) {
		Sleep(0.8);
		NumFire=0;
	}
	Finish();
}

///////////////////////////////////////////////////////////
function PlayIdleAnim()
{
	PlayAnim('Still',1.0, 0.05);
}

function SpawnEffect(vector HitLocation, vector HitNormal)
{
	if (FRand()< 0.5) 
		spawn(class'Chip',,,HitLocation+HitNormal*5,Rotation(HitNormal));
	if (FRand()< 0.5)
		spawn(class'Chip',,,HitLocation+HitNormal*5,Rotation(HitNormal));
	if (FRand()< 0.4)
		spawn(class'Chip',,,HitLocation+HitNormal*5,Rotation(HitNormal));	
	spawn(class'SmallSpark',,,HitLocation+HitNormal*5,rotation(HitNormal));
	spawn(class'SpriteSmokePuff',,,HitLocation+HitNormal*9);		
}

