class IRPR expands NaliWeapons;

//1st person
#exec MESH IMPORT MESH=IRPR ANIVFILE=MODELS\IRPR_a.3d DATAFILE=MODELS\IRPR_d.3d X=0 Y=0 Z=0
#exec MESH LODPARAMS MESH=IRPR STRENGTH=0
#exec MESH ORIGIN MESH=IRPR X=0 Y=0 Z=0 YAW=128

//1st person right handed
#exec MESH IMPORT MESH=IRPRR ANIVFILE=MODELS\IRPR_a.3d DATAFILE=MODELS\IRPR_d.3d X=0 Y=0 Z=0 UNMIRROR=1
#exec MESH LODPARAMS MESH=IRPRR STRENGTH=0
#exec MESH ORIGIN MESH=IRPRR X=0 Y=0 Z=0

//Pickup
#exec MESH IMPORT MESH=IRPRPick ANIVFILE=MODELS\IRPR_a.3d DATAFILE=MODELS\IRPR_d.3d X=0 Y=0 Z=0
//#exec MESH LODPARAMS MESH=IRPRPick STRENGTH=0
#exec MESH ORIGIN MESH=IRPRPick X=0 Y=0 Z=0

//3rd person
#exec MESH IMPORT MESH=IRPRM ANIVFILE=MODELS\IRPR_a.3d DATAFILE=MODELS\IRPR_d.3d X=0 Y=0 Z=0 UNMIRROR=1
#exec MESH LODPARAMS MESH=IRPRM STRENGTH=0
#exec MESH ORIGIN MESH=IRPRM X=-160 Y=0 Z=-40

//-----------------------------------------------------------------------------------

//Animations
#exec MESH SEQUENCE MESH=IRPR SEQ=All STARTFRAME=0 NUMFRAMES=16
#exec MESH SEQUENCE MESH=IRPR SEQ=Still STARTFRAME=0 NUMFRAMES=1
#exec MESH SEQUENCE MESH=IRPR SEQ=Fire STARTFRAME=0 NUMFRAMES=7 RATE=7.0
#exec MESH SEQUENCE MESH=IRPR SEQ=Select STARTFRAME=7 NUMFRAMES=5 RATE=8.0 GROUP=Select
#exec MESH SEQUENCE MESH=IRPR SEQ=Down STARTFRAME=11 NUMFRAMES=5 RATE=16.0

#exec MESH SEQUENCE MESH=IRPRR SEQ=All STARTFRAME=0 NUMFRAMES=16
#exec MESH SEQUENCE MESH=IRPRR SEQ=Still STARTFRAME=0 NUMFRAMES=1
#exec MESH SEQUENCE MESH=IRPRR SEQ=Fire STARTFRAME=0 NUMFRAMES=7 RATE=7.0
#exec MESH SEQUENCE MESH=IRPRR SEQ=Select STARTFRAME=7 NUMFRAMES=5 RATE=8.0 GROUP=Select
#exec MESH SEQUENCE MESH=IRPRR SEQ=Down STARTFRAME=11 NUMFRAMES=5 RATE=16.0

#exec MESH SEQUENCE MESH=IRPRPick SEQ=Still STARTFRAME=0 NUMFRAMES=1

#exec MESH SEQUENCE MESH=IRPRM SEQ=Still STARTFRAME=0 NUMFRAMES=1
#exec MESH SEQUENCE MESH=IRPRM SEQ=Fire STARTFRAME=0 NUMFRAMES=7 RATE=7.0

//-----------------------------------------------------------------------------------

//Scale
#exec MESHMAP NEW MESHMAP=IRPR MESH=IRPR
#exec MESHMAP SCALE MESHMAP=IRPR X=0.0625 Y=0.0625 Z=0.125

#exec MESHMAP NEW MESHMAP=IRPRR MESH=IRPRR
#exec MESHMAP SCALE MESHMAP=IRPRR X=0.0625 Y=0.0625 Z=0.125

#exec MESHMAP NEW MESHMAP=IRPRPick MESH=IRPRPick
#exec MESHMAP SCALE MESHMAP=IRPRPick X=0.0625 Y=0.0625 Z=0.125

#exec MESHMAP NEW MESHMAP=IRPRM MESH=IRPRM
#exec MESHMAP SCALE MESHMAP=IRPRM X=0.0625 Y=0.0625 Z=0.125

//-----------------------------------------------------------------------------------

//Skins
#exec TEXTURE IMPORT NAME=Sk_IRPR_01 FILE=SKINS\Sk_IRPR_01.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=IRPR NUM=1 TEXTURE=Sk_IRPR_01
#exec MESHMAP SETTEXTURE MESHMAP=IRPRR NUM=1 TEXTURE=Sk_IRPR_01
#exec MESHMAP SETTEXTURE MESHMAP=IRPRPick NUM=1 TEXTURE=Sk_IRPR_01
#exec MESHMAP SETTEXTURE MESHMAP=IRPRM NUM=1 TEXTURE=Sk_IRPR_01

#exec TEXTURE IMPORT NAME=Sk_IRPR_02 FILE=SKINS\Sk_IRPR_02.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=IRPR NUM=2 TEXTURE=Sk_IRPR_02
#exec MESHMAP SETTEXTURE MESHMAP=IRPRR NUM=2 TEXTURE=Sk_IRPR_02
#exec MESHMAP SETTEXTURE MESHMAP=IRPRPick NUM=2 TEXTURE=Sk_IRPR_02
#exec MESHMAP SETTEXTURE MESHMAP=IRPRM NUM=2 TEXTURE=Sk_IRPR_02

#exec TEXTURE IMPORT NAME=Sk_IRPR_03 FILE=SKINS\Sk_IRPR_03.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=IRPR NUM=3 TEXTURE=Sk_IRPR_03
#exec MESHMAP SETTEXTURE MESHMAP=IRPRR NUM=3 TEXTURE=Sk_IRPR_03
#exec MESHMAP SETTEXTURE MESHMAP=IRPRPick NUM=3 TEXTURE=Sk_IRPR_03
#exec MESHMAP SETTEXTURE MESHMAP=IRPRM NUM=3 TEXTURE=Sk_IRPR_03

#exec MESHMAP SETTEXTURE MESHMAP=IRPR NUM=4 TEXTURE=IRPR_FX
#exec MESHMAP SETTEXTURE MESHMAP=IRPRR NUM=4 TEXTURE=IRPR_FX
#exec MESHMAP SETTEXTURE MESHMAP=IRPRPick NUM=4 TEXTURE=IRPR_FX
#exec MESHMAP SETTEXTURE MESHMAP=IRPRM NUM=4 TEXTURE=IRPR_FX

//--------------------------------------------------------------

//Icons
#exec TEXTURE IMPORT NAME=IRPRIcon FILE=ICONS\IRPRIcon.bmp GROUP=Icons MIPS=OFF
#exec TEXTURE IMPORT NAME=IRPRScopeZoom FILE=ICONS\IRPRScopeZoom.bmp GROUP=Icons LODSET=0

#exec OBJ LOAD FILE=Textures\NaliIcons.utx PACKAGE=NaliWeaponsII.Icons

//--------------------------------------------------------------

//Fonts
#exec NEW TRUETYPEFONTFACTORY NAME=AlienNeoBig FONTNAME="Alien Encounters Bold" HEIGHT=24

//--------------------------------------------------------------

//Sounds
#exec AUDIO IMPORT NAME="IRPR_Fire" FILE=SOUNDS\IRPR_Fire.wav GROUP="IRPR"
#exec AUDIO IMPORT NAME="IRPR_Pickup" FILE=SOUNDS\IRPR_Pickup.wav GROUP="IRPR"
#exec AUDIO IMPORT NAME="IRPR_ScopeOff" FILE=SOUNDS\IRPR_ScopeOff.wav GROUP="IRPR"
#exec AUDIO IMPORT NAME="IRPR_ScopeOn" FILE=SOUNDS\IRPR_ScopeOn.wav GROUP="IRPR"
#exec AUDIO IMPORT NAME="IRPR_Select" FILE=SOUNDS\IRPR_Select.wav GROUP="IRPR"

var() float WallMaxTickness;
var bool bRightH;
var Font NeoF;
var bool bZoomIn;
var vector OwnerLocation;
var float StillTime, StillStart;

simulated function PreBeginPlay()
{
local IRPR WSniper;

	Super.PreBeginPlay();
	AmmoMulti();

	ForEach AllActors(Class'IRPR', WSniper)
	{
		if (WSniper == Self)
			Spawn(Class'HeatDetector');

		break;
	}
}

function AmmoMulti()
{
	if ((Level.Game.IsA('MonsterHunt') && Class'NWMutator'.Default.EnableMonsterHuntMode && Class'NWMutator'.Default.MonsterHuntOnly) || (Class'NWMutator'.Default.EnableMonsterHuntMode && !Class'NWMutator'.Default.MonsterHuntOnly))
	{
		PickupAmmoCount *= Class'NWMutator'.default.ammo_IRPR;
	}
}

function setHand(float Hand)
{
	Super.SetHand(Hand);
	if ( Hand == 1 )
	{
		bRightH = True;
		Mesh = mesh'IRPR';
	}
	else
	{
		bRightH = False;
		Mesh = mesh'IRPRR';
	}
}

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

    bestAim = 0.95;
    P = Pawn(Owner);
    if ( P == None )
    {
        GotoState('');
        return;
    }
    if ( VSize(P.Location - OwnerLocation) < 6 )
        StillTime += FMin(2.0, Level.TimeSeconds - StillStart);

    else
        StillTime = 0;
    StillStart = Level.TimeSeconds;
    OwnerLocation = P.Location;
    FireDir = vector(P.ViewRotation);
    targ = P.PickTarget(bestAim, bestDist, FireDir, Owner.Location);
    if ( Pawn(targ) != None )
    {
        SetTimer(1 + 4 * FRand(), false);
        bPointing = true;
        Pawn(targ).WarnTarget(P, 200, FireDir);
    }
    else 
    {
        SetTimer(0.4 + 1.6 * FRand(), false);
        if ( (P.bFire == 0) && (P.bAltFire == 0) )
            bPointing = false;
    }
}   


function Finish()
{
	if ( (Pawn(Owner).bFire!=0) && (FRand() < 0.6) )
		Timer();
	Super.Finish();
}


	simulated function Tick(float DeltaTime)
	{
		if (Pawn(Owner) != None)
		{
			if ( Pawn(Owner).bAltFire == 0 )
			{
				if ( (PlayerPawn(Owner) != None) && PlayerPawn(Owner).Player.IsA('ViewPort') )
					PlayerPawn(Owner).StopZoom();

				SetTimer(0.0,False);
			}
		}
	}



function Fire( float Value )
{
    if ( (AmmoType == None) && (AmmoName != None) )
    {
        // ammocheck
        GiveAmmo(Pawn(Owner));
    }

    if (bInfinity)
    {
	GotoState('NormalFire');
        bPointing=True;
        bCanClientFire = true;
        ClientFire(Value);

		TraceFire(0.0);
    }
    else if ( AmmoType.UseAmmo(1) )
    {
        GotoState('NormalFire');
        bPointing=True;
        bCanClientFire = true;
        ClientFire(Value);

		TraceFire(0.0);
    }
}


simulated function PlayFiring()
{
	PlayOwnedSound(FireSound, SLOT_None, Pawn(Owner).SoundDampening*5.0);
	PlayAnim('Fire', 1.2 * FireRateChange, 0.0);
}


function TraceFire( float Accuracy )
{
    local vector HitLocation, HitNormal, StartTrace, EndTrace, X,Y,Z;
    local actor Other;
    local Pawn PawnOwner;
    local bool bSecWall;
    local vector StartW,XW,YW,ZW;

    PawnOwner = Pawn(Owner);
	FireOffset = vect(0,0,0);
    Owner.MakeNoise(PawnOwner.SoundDampening);
    GetAxes(PawnOwner.ViewRotation,X,Y,Z);
    StartTrace = Owner.Location + PawnOwner.Eyeheight * Z; 
    AdjustedAim = PawnOwner.AdjustAim(1000000, StartTrace, 2*AimError, False, False);   
    X = vector(AdjustedAim);
    EndTrace = StartTrace + 30000 * X;

    GetAxes(PawnOwner.ViewRotation,XW,YW,ZW);
	FireOffset = vect(60,-5,0);
	
	if (!bRightH)
    StartW = Owner.Location + CalcDrawOffset() + FireOffset.X * XW + (-FireOffset.Y) * YW + FireOffset.Z * ZW;
	else
    StartW = Owner.Location + CalcDrawOffset() + FireOffset.X * XW + FireOffset.Y * YW + FireOffset.Z * ZW;

	Spawn(Class'IRPREffectMuzzle',,, StartW );

	ForEach TraceActors(Class'Actor', Other, HitLocation, HitNormal, EndTrace, StartTrace)
	{
		ProcessTraceHit(Other, HitLocation, HitNormal, X,Y,Z);

		if (Other.Tag == 'HitWall' || Other == Level)
			break;
	}

	GetAxes(rotator(EndTrace - HitLocation), X, Y, Z);//Backwards

	if (Other.Tag != 'HitWall')
	{
		StartW = HitLocation;
		Other = Trace (HitLocation, HitNormal, StartW, StartW  + WallMaxTickness * X, False);

		ProcessTraceHit(Other, HitLocation, HitNormal, X,Y,Z);
		
		StartW = HitLocation + 2*HitNormal;

		ForEach TraceActors(Class'Actor', Other, HitLocation, HitNormal, EndTrace, StartW)
		{
			ProcessTraceHit(Other, HitLocation, HitNormal, X,Y,Z);

			if (Other.Tag == 'HitWall')
				break;
		}
	}
}




function ProcessTraceHit(Actor Other, Vector HitLocation, Vector HitNormal, Vector X, Vector Y, Vector Z)
{

	if (Other == Level) 
	{
		Spawn(class'IRPRHitEffect',,, HitLocation+HitNormal, Rotator(HitNormal));
		Spawn(class'IRPREffect',,, HitLocation+HitNormal);
	}

	else if ( (Other != self) && (Other != Owner) && (Other != None) ) 
	{
		if ( Other.bIsPawn )
		{
			Other.PlaySound(Sound 'ChunkHit',, 4.0,,100);
		}

		
		if ( Other.bIsPawn && (HitLocation.Z - Other.Location.Z > 0.62 * Other.CollisionHeight) 
			&& (instigator.IsA('PlayerPawn') || (instigator.IsA('Bot') && !Bot(Instigator).bNovice)) )
		{
			
			if (HealthGiver && (Pawn(Other).PlayerReplicationInfo.Team == Instigator.PlayerReplicationInfo.Team && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt')) ||
		(Level.Game.IsA('MonsterHunt') && !Pawn(Other).IsA('ScriptedPawn')) && Pawn(Other).Health < Class'NWMutator'.default.MaxHealth)
			{
				Pawn(Other).Health += 500 * FMax(1.0,MoreDamage);
					if (Pawn(Other).Health > Class'NWMutator'.default.MaxHealth)
						Pawn(Other).Health = Class'NWMutator'.default.MaxHealth;
			}

			Other.TakeDamage(500 * FMax(1.0,MoreDamage), Pawn(Owner), HitLocation, 35000 * X + (MirrorVectorByNormal(HitNormal, HitNormal) * (KickBack * 1000 + 1)), 'Decapitated');
		}
		else
		{
		
			if (HealthGiver &&  Other.bIsPawn)
			{
				if ((Pawn(Other).PlayerReplicationInfo.Team == Instigator.PlayerReplicationInfo.Team && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt')) ||
		(Level.Game.IsA('MonsterHunt') && !Pawn(Other).IsA('ScriptedPawn')) && Pawn(Other).Health < Class'NWMutator'.default.MaxHealth)
				{
					Pawn(Other).Health += 55 * FMax(1.0,MoreDamage);
						if (Pawn(Other).Health > Class'NWMutator'.default.MaxHealth)
							Pawn(Other).Health = Class'NWMutator'.default.MaxHealth;
				}
			}

			Other.TakeDamage(55 * FMax(1.0,MoreDamage),  Pawn(Owner), HitLocation, 30000.0 * X + (MirrorVectorByNormal(HitNormal, HitNormal) * (KickBack * 1000 + 1)), MyDamageType);	
		}
		if ( !Other.bIsPawn && !Other.IsA('Carcass') )
			spawn(class'IRPREffect',,,HitLocation+HitNormal*9);	
	}	
}


simulated function bool ClientAltFire( float Value )
{
	if (!bZoomIn)
		PlayOwnedSound(Sound'IRPR_ScopeOn', SLOT_None, Pawn(Owner).SoundDampening*5.0);
	else
		PlayOwnedSound(Sound'IRPR_ScopeOff', SLOT_None, Pawn(Owner).SoundDampening*5.0);

	GotoState('Zooming');
	return true;
}

function AltFire( float Value )
{
	ClientAltFire(Value);
}


simulated function PostRender( canvas Canvas )
{
	local PlayerPawn P;
	local float Scale, ZoomS;
	local int i;
	local HeatOv HO;

	P = PlayerPawn(Owner);

	if ( (P != None) && (P.DesiredFOV != P.DefaultFOV) ) 
	{
		bOwnsCrossHair = True;
		Scale = Canvas.ClipX/640;

		Canvas.SetPos(0, 0);
		Canvas.Style = ERenderStyle.STY_Modulated;
		Canvas.DrawRect(Texture'IRPRScopeZoom', Canvas.ClipX, Canvas.ClipY);

		ForEach RadiusActors(Class'HeatOv', HO, 10000.0, Pawn(Owner).Location)
		{
			if (!HO.bStandByM && HO.Owner != Pawn(Owner))
				Canvas.DrawActor( HO, False, True);
		}

		Canvas.Style = ERenderStyle.STY_Translucent;
		Canvas.SetPos(0.5 * Canvas.ClipX - 128 * Scale, 0.5 * Canvas.ClipY - 128 * Scale );
		Canvas.DrawIcon(Texture'ZoomCrossN', Scale/2);
		
		Canvas.Style = ERenderStyle.STY_Normal;
		Canvas.SetPos(0.5 * Canvas.ClipX - 80 * Scale, 0.5 * Canvas.ClipY + 128 * Scale);
		Canvas.DrawColor.R = 0;
		Canvas.DrawColor.G = 255;
		Canvas.DrawColor.B = 0;
		Canvas.Font = NeoF;
		ZoomS = P.DefaultFOV/P.DesiredFOV;
		Canvas.DrawText(int(ZoomS)$" . "$int(10 * ZoomS - 10 * int(ZoomS))$"  X");

		bZoomIn = True;

		Super(TournamentWeapon).PostRender(Canvas);
	}
	else
	{
		bZoomIn = False;
		bOwnsCrosshair = class'NaliWeapons'.default.bCustomCrosshair;
		Super.PostRender(Canvas);
	}
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////
state Zooming
{
	simulated function Tick(float DeltaTime)
	{
		if ( Pawn(Owner).bAltFire == 0 )
		{
			if ( (PlayerPawn(Owner) != None) && PlayerPawn(Owner).Player.IsA('ViewPort') )
				PlayerPawn(Owner).StopZoom();
			SetTimer(0.0,False);
			GoToState('Idle');
		}
	}

	simulated function BeginState()
	{
		if ( Owner.IsA('PlayerPawn') )
		{
			if ( PlayerPawn(Owner).Player.IsA('ViewPort') )
				PlayerPawn(Owner).ToggleZoom();
			SetTimer(0.2,True);
		}
		else
		{
			Pawn(Owner).bFire = 1;
			Pawn(Owner).bAltFire = 0;
			Global.Fire(0);
		}
	}
}


state Idle
{
    function Fire( float Value )
    {
        if ( AmmoType == None )
        {
            // ammocheck
            GiveAmmo(Pawn(Owner));
        }
	if (bInfinity)
   	{
	GotoState('NormalFire');
        bPointing=True;
        bCanClientFire = true;
        if ( Owner.IsA('Bot') )
            {
                // simulate bot using zoom
                if ( Bot(Owner).bSniping && (FRand() < 0.65) )
                    AimError = AimError/FClamp(StillTime, 1.0, 8.0);
                else if ( VSize(Owner.Location - OwnerLocation) < 6 )
                    AimError = AimError/FClamp(0.5 * StillTime, 1.0, 3.0);
                else
                    StillTime = 0;
            }

	Pawn(Owner).PlayRecoil(FiringSpeed);
        TraceFire(0.0);
        AimError = Default.AimError;
	ClientFire(Value);
    	}
        else if (AmmoType.UseAmmo(1))
        {
            GotoState('NormalFire');
            bCanClientFire = true;
            bPointing=True;
            if ( Owner.IsA('Bot') )
            {
                // simulate bot using zoom
                if ( Bot(Owner).bSniping && (FRand() < 0.65) )
                    AimError = AimError/FClamp(StillTime, 1.0, 8.0);
                else if ( VSize(Owner.Location - OwnerLocation) < 6 )
                    AimError = AimError/FClamp(0.5 * StillTime, 1.0, 3.0);
                else
                    StillTime = 0;
            }
            Pawn(Owner).PlayRecoil(FiringSpeed);
            TraceFire(0.0);
            AimError = Default.AimError;
            ClientFire(Value);
        }
    }


    function BeginState()
    {
        bPointing = false;
        SetTimer(0.4 + 1.6 * FRand(), false);
        Super.BeginState();
    }

    function EndState()
    {   
        SetTimer(0.0, false);
        Super.EndState();
    }
    
Begin:
    bPointing=False;
    if ( AmmoType.AmmoAmount<=0 ) 
        Pawn(Owner).SwitchToBestWeapon();  //Goto Weapon that has Ammo
    if ( Pawn(Owner).bFire!=0 ) Fire(0.0);
    Disable('AnimEnd');
    PlayIdleAnim();
}





defaultproperties
{
	NeoF=Font'AlienNeoBig'
	WallMaxTickness=192
	DrawType=DT_Mesh
	Mesh=IRPRPick
	CollisionHeight=10.000000
	CollisionRadius=15.000000
	AutoSwitchPriority=7
	Icon=IRPRIcon
	InventoryGroup=10
	ItemArticle="an"
	ItemName="InfraRed Precision Rifle (I.R.P.R.)"
	MaxDesireability=0.600000
	PickupMessage="You got the InfraRed Precision Rifle (I.R.P.R.)"
	PickupSound=IRPR_Pickup
	PickupViewMesh=IRPRPick
	PickupViewScale=1.000000
	PlayerViewMesh=IRPRR
	PlayerViewOffset=(X=22.000000,Y=-10.000000,Z=-9.000000)
	PlayerViewScale=1.000000
	RespawnTime=30.000000
	StatusIcon=IRPRIcon
	ThirdPersonMesh=IRPRM
	ThirdPersonScale=0.750000
	AltDamageType=None
	AltFireSound=None
	AltProjectileClass=None
	AmmoName=IRPRAmmo
	bInstantHit=True
	bOwnsCrosshair=False
	bRapidFire=False
	DeathMessage="%o was terminated by %k's %w."
	FireOffset=(X=0.000000,Y=0.000000,Z=0.000000)
	FireSound=IRPR_Fire
	FiringSpeed=1.000000
	MaxTargetRange=4096.000000
	MyDamageType=Laser
	NameColor=(R=0,G=64,B=0)
	PickupAmmoCount=6
	ProjectileClass=None
	SelectSound=IRPR_Select
	shakemag=900.000000
	shaketime=0.150000
	shakevert=15.000000
	AIRating=0.800000
	AltRefireRate=1.000000
	bAltWarnTarget=False
	bRecommendAltSplashDamage=False
	bRecommendSplashDamage=False
	bSplashDamage=False
	bWarnTarget=False
	RefireRate=0.600000
	Mass=50.000000
	bNoSmooth=False
	bOwnsCrossHair=True
	CustomCross=IRPRCross
	CrossHairSize=64
	Texture=None
	CrossHairScale=1.000000
}
