//=============================================================================
// UBersPulseGun.
//=============================================================================
class UBersPulseGun expands PulseGun;

#exec AUDIO IMPORT NAME="5_PulseGunPickup" FILE=SOUNDS\5_PulseGunPickup.wav GROUP="WeaponPick"

var() float UBFireRate;
var BattleOv OV;
var byte CurrentHand;
var UDamOvFx OVa;
var UBersOvFx OVb;
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()
{
	FlashCount++;
	AmbientSound = FireSound;
	SoundVolume = Pawn(Owner).SoundDampening*255;
	LoopAnim( 'shootLOOP', (1 + 0.5 * FireAdjust) * UBFireRate, 0.0);
	bWarnTarget = (FRand() < 0.2);
}

simulated function PlayAltFiring()
{
	
	AmbientSound = AltFireSound;
	if ( (AnimSequence == 'BoltLoop') || (AnimSequence == 'BoltStart') )		
		PlayAnim( 'boltloop', UBFireRate);
	else
		PlayAnim( 'boltstart' , UBFireRate);
}


state NormalFire
{
	ignores AnimEnd;

	function Projectile ProjectileFire(class<projectile> ProjClass, float ProjSpeed, bool bWarn)
	{
		local Vector Start, X,Y,Z;

		Owner.MakeNoise(Pawn(Owner).SoundDampening);
		GetAxes(Pawn(owner).ViewRotation,X,Y,Z);
		Start = Owner.Location + CalcDrawOffset() + FireOffset.X * X + FireOffset.Y * Y + FireOffset.Z * Z; 
		AdjustedAim = pawn(owner).AdjustAim(ProjSpeed, Start, AimError, True, bWarn);	
		Start = Start - Sin(Angle)*Y*4 + (Cos(Angle)*4 - 10.78)*Z;
		Angle += 1.8;
		return Spawn(ProjClass,,, Start,AdjustedAim);	
	}

	function Tick( float DeltaTime )
	{
		if (Owner==None) 
			GotoState('Pickup');
	}

	function BeginState()
	{
		Super.BeginState();
		Angle = 0;
		AmbientGlow = 200;
	}

	function EndState()
	{
		PlaySpinDown();
		AmbientSound = None;
		AmbientGlow = 0;	
		OldFlashCount = FlashCount;	
		Super.EndState();
	}

Begin:
	Sleep(0.18 / UBFireRate);
	Finish();
}

state AltFiring
{
	ignores AnimEnd;

	function Tick(float DeltaTime)
	{
		local Pawn P;

		P = Pawn(Owner);
		if ( P == None )
		{
			GotoState('Pickup');
			return;
		}
		if ( (P.bAltFire == 0) || (P.IsA('Bot')
					&& ((P.Enemy == None) || (Level.TimeSeconds - Bot(P).LastSeenTime > 5))) )
		{
			P.bAltFire = 0;
			Finish();
			return;
		}

		Count += Deltatime;
		if ( Count > (0.24 / UBFireRate) )
		{
			if ( Owner.IsA('PlayerPawn') )
				PlayerPawn(Owner).ClientInstantFlash( InstFlash,InstFog);
			if ( Affector != None )
				Affector.FireEffect();
			Count -= (0.24 / UBFireRate);
			if ( !AmmoType.UseAmmo(1) )
				Finish();
		}
	}
	
	function EndState()
	{
		AmbientGlow = 0;
		AmbientSound = None;
		if ( PlasmaBeam != None )
		{
			PlasmaBeam.Destroy();
			PlasmaBeam = None;
		}
		Super.EndState();
	}

Begin:
	AmbientGlow = 200;
	FinishAnim();	
	LoopAnim( 'boltloop', 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();

    Texture'Ammoled'.NotifyActor = Self;
    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;
	}


    Texture'Ammoled'.NotifyActor = 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)
	UDamFX=3rdPulseGunUdam_fx
	UBersFX=3rdPulseGunBers_fx
	DamBersFX=3rdPulseGunBaU_fx
	PickupSound=5_PulseGunPickup
}