//=============================================================================
// UbersImpactHammer.
//=============================================================================
class UbersImpactHammer expands ImpactHammer;

var() float UBFireRate;
var BattleOv OV;
var UDamOvFx OVa;
var UBersOvFx OVb;
var byte CurrentHand;
var() texture UDamFX, UBersFX, DamBersFX;

replication
{
	// Things the server should send to the client.
	reliable if( bNetOwner && (Role==ROLE_Authority) )
		UBFireRate, OV;
}

simulated function SetOwnerDisplay()
{
	Super.SetOwnerDisplay();
	SetTranslucentWeapon();
}

simulated function setHand(float Hand)
{
	Super.SetHand(Hand);
	SetTranslucentWeapon();
}

simulated function BecomeItem()
{
	Super.BecomeItem();
	SetTranslucentWeapon();
}

simulated function BecomePickup()
{
	Super.BecomePickup();
	SetTranslucentWeapon();
}

simulated function PlayFiring()
{
	if (Owner != None)
	{
		if ( Affector != None )
			Affector.FireEffect();
		Owner.PlayOwnedSound(FireSound, SLOT_Misc, 1.7*Pawn(Owner).SoundDampening,,,);
		if ( PlayerPawn(Owner) != None )
			PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
		PlayAnim( 'Fire', 0.65 * UBFireRate );
	}
}

simulated function PlayAltFiring()
{
	if (Owner != None)
	{
		if ( Affector != None )
			Affector.FireEffect();
		PlayOwnedSound(AltFireSound, SLOT_Misc, 1.7*Pawn(Owner).SoundDampening,,,);
		LoopAnim( 'Fire', 0.65 * UBFireRate);
	}
}

state Firing
{
	function AltFire(float F) 
	{
	}

	function Tick( float DeltaTime )
	{
		local Pawn P;
		local Rotator EnemyRot;
		local vector HitLocation, HitNormal, StartTrace, EndTrace, X, Y, Z;
		local actor HitActor;

		if ( bChangeWeapon )
			GotoState('DownWeapon');

		if (  Bot(Owner) != None )
		{
			if ( Bot(Owner).Enemy == None )
				Bot(Owner).bFire = 0;
			else
				Bot(Owner).bFire = 1;
		}
		P = Pawn(Owner);
		if ( P == None ) 
		{
			AmbientSound = None;
			GotoState('');
			return;
		}
		else if( P.bFire==0 ) 
		{
			TraceFire(0);
			PlayFiring();
			GoToState('FireBlast');
			return;
		}

		ChargeSize += 0.75 * DeltaTime * UBFireRate;

		Count += DeltaTime;
		if ( Count > 0.2 / UBFireRate )
		{
			Count = 0;
			Owner.MakeNoise(1.0);
		}
		if (ChargeSize > 1 * UBFireRate) 
		{
			if ( !P.IsA('PlayerPawn') && (P.Enemy != None) )
			{
				EnemyRot = Rotator(P.Enemy.Location - P.Location);
				EnemyRot.Yaw = EnemyRot.Yaw & 65535;
				if ( (abs(EnemyRot.Yaw - (P.Rotation.Yaw & 65535)) > 8000)
					&& (abs(EnemyRot.Yaw - (P.Rotation.Yaw & 65535)) < 57535) )
					return;
				GetAxes(EnemyRot,X,Y,Z);
			}
			else
				GetAxes(P.ViewRotation, X, Y, Z);
			StartTrace = P.Location + CalcDrawOffset() + FireOffset.X * X + FireOffset.Y * Y + FireOffset.Z * Z; 
			if ( (Level.NetMode == NM_Standalone) && P.IsA('PlayerPawn') )
				EndTrace = StartTrace + 25 * X; 
			else
				EndTrace = StartTrace + 60 * X; 
			HitActor = Trace(HitLocation, HitNormal, EndTrace, StartTrace, true);
			if ( (HitActor != None) && (HitActor.DrawType == DT_Mesh) )
			{
				ProcessTraceHit(HitActor, HitLocation, HitNormal, vector(AdjustedAim), Y, Z);
				PlayFiring();
				GoToState('FireBlast');
			}
		}
	}

	function BeginState()
	{
		ChargeSize = 0.0;
		Count = 0.0;
	}

	function EndState()
	{
		Super.EndState();
		AmbientSound = None;
	}

Begin:
	FinishAnim();
	AmbientSound = TensionSound;
	SoundVolume = 255*Pawn(Owner).SoundDampening;		
	LoopAnim('Shake', 0.9 * UBFireRate);
}


simulated event RenderOverlays( canvas Canvas )
{
    local rotator NewRot;
    local bool bPlayerOwner;
    local int Hand;
    local PlayerPawn PlayerOwner;

	if ( PlayerPawn(Owner) != None && PlayerPawn(Owner).ViewTarget == None && !PlayerPawn(Owner).bBehindView )
		SetNormalViewWeapon();
	else
		SetTranslucentWeapon();

    if ( bHideWeapon || (Owner == None) )
        return;

    PlayerOwner = PlayerPawn(Owner);

    if ( PlayerOwner != None )
    {
        if ( PlayerOwner.DesiredFOV != PlayerOwner.DefaultFOV )
            return;
        bPlayerOwner = true;
        Hand = PlayerOwner.Handedness;

        if (  (Level.NetMode == NM_Client) && (Hand == 2) )
        {
            bHideWeapon = true;
            return;
        }
    }

    if ( !bPlayerOwner || (PlayerOwner.Player == None) )
        Pawn(Owner).WalkBob = vect(0,0,0);

    if ( (bMuzzleFlash > 0) && bDrawMuzzleFlash && Level.bHighDetailMode && (MFTexture != None) )
    {
        MuzzleScale = Default.MuzzleScale * Canvas.ClipX/640.0;
        if ( !bSetFlashTime )
        {
            bSetFlashTime = true;
            FlashTime = Level.TimeSeconds + FlashLength;
        }
        else if ( FlashTime < Level.TimeSeconds )
            bMuzzleFlash = 0;
        if ( bMuzzleFlash > 0 )
        {
            if ( Hand == 0 )
                Canvas.SetPos(Canvas.ClipX/2 - 0.5 * MuzzleScale * FlashS + Canvas.ClipX * (-0.2 * Default.FireOffset.Y * FlashO), Canvas.ClipY/2 - 0.5 * MuzzleScale * FlashS + Canvas.ClipY * (FlashY + FlashC));
            else
                Canvas.SetPos(Canvas.ClipX/2 - 0.5 * MuzzleScale * FlashS + Canvas.ClipX * (Hand * Default.FireOffset.Y * FlashO), Canvas.ClipY/2 - 0.5 * MuzzleScale * FlashS + Canvas.ClipY * FlashY);

            Canvas.Style = 3;
            Canvas.DrawIcon(MFTexture, MuzzleScale);
            Canvas.Style = 1;
        }
    }
    else
        bSetFlashTime = false;

    SetLocation( Owner.Location + CalcDrawOffset() );
    NewRot = Pawn(Owner).ViewRotation;

    if ( Hand == 0 )
        newRot.Roll = -2 * Default.Rotation.Roll;
    else
        newRot.Roll = Default.Rotation.Roll * Hand;

    setRotation(newRot);
    Canvas.DrawActor(self, False, False);

	
	//Overlayer
	if (OV == None && PlayerOwner != None && PlayerOwner.FindInventoryType(Class'BattleSuit') != None)
	{
		OV = Spawn(Class'BattleOv', PlayerOwner,,  Location , Rotation);			
		OV.DrawScale = PlayerViewScale;
		OV.bOnlyOwnerSee = True;
		OV.Mesh = Mesh;
		CurrentHand = 20;
	}

	if (OV != None)
	{
		
		if (CurrentHand != Hand)
		{
			CurrentHand = Hand;
			OV.Mesh = Mesh;
		}

		OV.SetLocation(Owner.Location + CalcDrawOffset());
		OV.SetRotation(newRot);
		OV.AnimFrame = AnimFrame;
		OV.AnimSequence = AnimSequence;
		OV.Fatness = Fatness;
		Canvas.DrawActor(OV, False, False);
	}

	if (OV != None && (PlayerOwner == None || PlayerOwner.FindInventoryType(Class'BattleSuit') == None))
	{
		OV.Destroy();
		OV = None;
	}

	//Udamage overlayer
	if (OVa == None && PlayerOwner != None && PlayerOwner.FindInventoryType(Class'UBersUDamage') != None)
	{
		OVa = Spawn(Class'UDamOvFx', PlayerOwner,,  Location , Rotation);			
		OVa.DrawScale = PlayerViewScale;
		OVa.bOnlyOwnerSee = True;
		OVa.Mesh = Mesh;
		CurrentHand = 20;
	}

	if (OVa != None)
	{
		
		if (CurrentHand != Hand)
		{
			CurrentHand = Hand;
			OVa.Mesh = Mesh;
		}

		OVa.SetLocation(Owner.Location + CalcDrawOffset());
		OVa.SetRotation(newRot);
		OVa.AnimFrame = AnimFrame;
		OVa.AnimSequence = AnimSequence;
		OVa.Fatness = Fatness;
		Canvas.DrawActor(OVa, False, False);
	}

	if (OVa != None && (PlayerOwner == None || PlayerOwner.FindInventoryType(Class'UBersUDamage') == None))
	{
		OVa.Destroy();
		OVa = None;
	}


	//Berserk overlayer
	if (OVb == None && PlayerOwner != None && PlayerOwner.FindInventoryType(Class'UBerseker') != None)
	{
		OVb = Spawn(Class'UBersOvFx', PlayerOwner,,  Location , Rotation);			
		OVb.DrawScale = PlayerViewScale;
		OVb.bOnlyOwnerSee = True;
		OVb.Mesh = Mesh;
		CurrentHand = 20;
	}

	if (OVb != None)
	{
		
		if (CurrentHand != Hand)
		{
			CurrentHand = Hand;
			OVb.Mesh = Mesh;
		}

		OVb.SetLocation(Owner.Location + CalcDrawOffset());
		OVb.SetRotation(newRot);
		OVb.AnimFrame = AnimFrame;
		OVb.AnimSequence = AnimSequence;
		OVb.Fatness = Fatness;
		Canvas.DrawActor(OVb, False, False);
	}

	if (OVb != None && (PlayerOwner == None || PlayerOwner.FindInventoryType(Class'UBerseker') == None))
	{
		OVb.Destroy();
		OVb = None;
	}

	SetTranslucentWeapon();

}

simulated function SetTranslucentWeapon()
{
local Inventory UD, UB, UI;
	
	if (Pawn(Owner) != None)
	{

		UI = Pawn(Owner).FindInventoryType(class'UbersInvisibility');
		UD = Pawn(Owner).FindInventoryType(class'UbersUdamage');
		UB = Pawn(Owner).FindInventoryType(class'UBerseker');

		if (UI != None)
		{
			if (!UI.bActive)
				UI = None;
		}
		if (UD != None)
		{
			if (!UD.bActive)
				UD = None;
		}
		if (UB != None)
		{
			if (!UB.bActive)
				UB = None;
		}

		if ( UI != None)
		{
			ScaleGlow = 1.0;
			MultiSkins[1] = None;
			Style = STY_Translucent;
			bMeshEnviroMap = True;
			bUnlit = True;
			Texture = Texture'UnrealShare.Belt_fx.Invis.Invis';
		}
		else if (UD != None && UB == None)
		{
			ScaleGlow = 3.0;
			MultiSkins[1] = UDamFX;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = True;
			Texture = Default.Texture;
		}
		else if ( UD == None && UB != None)
		{
			ScaleGlow = 3.0;
			MultiSkins[1] = UBersFX;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = True;
			Texture = Default.Texture;
		}
		else if ( UD != None && UB != None)
		{
			ScaleGlow = 3.0;
			MultiSkins[1] = DamBersFX;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = True;
			Texture = Default.Texture;
		}
		else
		{
			ScaleGlow = 1.0;
			MultiSkins[1] = None;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = False;
			Texture = Default.Texture;
		}
	}
	else
	{
		ScaleGlow = 1.0;
		MultiSkins[1] = None;
		Style = STY_Normal;
		Texture = Default.Texture;
		bMeshEnviroMap = False;
		bUnlit = False;
	}
}


simulated function SetNormalViewWeapon()
{

	if ( Owner == None )
		SetTranslucentWeapon();

	if (Pawn(Owner) != None)
	{
		if ( Pawn(Owner).FindInventoryType(class'UbersInvisibility')!=None)
		{
			MultiSkins[1] = None;
			Style = STY_Translucent;
			bMeshEnviroMap = True;
			bUnlit = True;
			Texture = Texture'UnrealShare.Belt_fx.Invis.Invis';
		}
		else
		{
			MultiSkins[1] = None;
			Texture = Default.Texture;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = False;
		}
	}
	else
	{
		MultiSkins[1] = None;
		Texture = Default.Texture;
		Style = STY_Normal;
		bMeshEnviroMap = False;
		bUnlit = False;
	}
	
}

state Pickup
{
	simulated function BeginState()
	{
		Super.BeginState();
		SetTranslucentWeapon();
	}
}

defaultproperties
{
	UBFireRate=1.000000
	RotationRate=(Yaw=30000)
	bCanThrow=False
	UDamFX=3rdImpactUdam_fx
	UBersFX=3rdImpactBers_fx
	DamBersFX=3rdImpactBaU_fx
}