//=============================================================================
// UBersEnforcer.
//=============================================================================
class UBersEnforcer expands enforcer;

#exec AUDIO IMPORT NAME="2_EnforcerPickup" FILE=SOUNDS\2_EnforcerPickup.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 event RenderOverlays(canvas Canvas)
{
    local PlayerPawn PlayerOwner;
    local int realhand;
	local rotator NewRot;
    local bool bPlayerOwner;
    local int Hand;

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

    if ( (bMuzzleFlash > 0) && !Level.bDropDetail )
        MFTexture = MuzzleFlashVariations[Rand(5)];
    PlayerOwner = PlayerPawn(Owner);
    if ( PlayerOwner != None )
    {
        if ( PlayerOwner.DesiredFOV != PlayerOwner.DefaultFOV )
            return;
        realhand = PlayerOwner.Handedness;
        if (  (Level.NetMode == NM_Client) && (realHand == 2) )
        {
            bHideWeapon = true;
            return;
        }
        if ( !bHideWeapon )
        {
            if ( Mesh == mesh'AutoML' )
                PlayerOwner.Handedness = 1;
            else if ( bIsSlave || (SlaveEnforcer != None) )
                PlayerOwner.Handedness = -1;
        }
    }
    if ( (PlayerOwner == None) || (PlayerOwner.Handedness == 0) )
    {
        if ( AnimSequence == 'Shot2' )
        {
            FlashO = -2 * Default.FlashO;
            FlashY = Default.FlashY * 2.5;
        }
        else
        {
            FlashO = 1.9 * Default.FlashO;
            FlashY = Default.FlashY;
        }
    }
    else if ( AnimSequence == 'Shot2' )
    {
        FlashO = Default.FlashO * 0.3;
        FlashY = Default.FlashY * 2.5;
    }
    else
    {
        FlashO = Default.FlashO;
        FlashY = Default.FlashY;
    }
    if ( !bHideWeapon && ( (SlaveEnforcer != None) || bIsSlave ) )
    {
        if ( PlayerOwner == None )
            bMuzzleFlash = 0;

        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);


	//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 != realhand)
		{
			CurrentHand = realhand;
			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;
	}


        if ( SlaveEnforcer != None )
        {
            if ( SlaveEnforcer.bBringingUp )
            {
                SlaveEnforcer.bBringingUp = false;
                SlaveEnforcer.PlaySelect();
            }
            SlaveEnforcer.RenderOverlays(Canvas);
        }
    }
    else
	{
       	 	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);


		//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 != realhand)
		{
			CurrentHand = realhand;
			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;
	}

	}

    if ( PlayerOwner != None )
        PlayerOwner.Handedness = realhand;

	SetTranslucentWeapon();
}


function SetSwitchPriority(pawn Other)
{
    local int i;
    local name temp, carried;

    if ( PlayerPawn(Other) != None )
    {
        Super(TournamentWeapon).SetSwitchPriority(Other);

        // also set double switch priority

        for ( i=0; i<20; i++)
            if ( PlayerPawn(Other).WeaponPriority[i] == 'UBersDoubleEnforcer' )
            {
                DoubleSwitchPriority = i;
                return;
            }
    }       
}


simulated function PlayIdleAnim()
{
    if ( Mesh == PickupViewMesh )
        return;
    if ( (FRand()>0.96) && (AnimSequence != 'Twiddle') ) 
        PlayAnim('Twiddle',0.6,0.3);
    else 
        LoopAnim('Sway',0.2, 0.3);
}



simulated function PlayFiring()
{	
	if (UBersEnforcer(SlaveEnforcer) != None)
		UBersEnforcer(SlaveEnforcer).UBFireRate = UBFireRate;

	PlayOwnedSound(FireSound, SLOT_None,2.0*Pawn(Owner).SoundDampening);
	bMuzzleFlash++;
	PlayAnim('Shoot',(0.5 + 0.31 * FireAdjust) * UBFireRate, 0.02 / UBFireRate);
}

simulated function PlayAltFiring()
{
	PlayAnim('T1', 1.3 * UBFireRate, 0.05 / UBFireRate);
	bFirstFire = true;
	
	if (UBersEnforcer(SlaveEnforcer) != None)
		UBersEnforcer(SlaveEnforcer).UBFireRate = UBFireRate;
}


/*function bool HandlePickupQuery( inventory Item )
{
	local Pawn P;
	local Inventory Copy;
	local int OldAmmo;

	if ( (Item.class == class) && (SlaveEnforcer == None) ) 
	{
		P = Pawn(Owner);
		// spawn a double
		Copy = Spawn(class, P);
		Copy.BecomeItem();
		ItemName = DoubleName;
		SlaveEnforcer = UBersEnforcer(Copy);
		SetTwoHands();
		AIRating = 0.4;
		SlaveEnforcer.SetUpSlave( Pawn(Owner).Weapon == self );
		SlaveEnforcer.SetDisplayProperties(Style, Texture, bUnlit, bMeshEnviromap);
		SetTwoHands();
		P.ReceiveLocalizedMessage( class'PickupMessagePlus', 0, None, None, Self.Class );
		Item.PlaySound(Item.PickupSound);
		if (Level.Game.LocalLog != None)
			Level.Game.LocalLog.LogPickup(Item, Pawn(Owner));
		if (Level.Game.WorldLog != None)
			Level.Game.WorldLog.LogPickup(Item, Pawn(Owner));
		Item.SetRespawn();
		return true;
	}

     	if (Item.IsA('UBersEnforcer'))
	{
		P = Pawn(Owner);
		if ( AmmoType != None )
		{
			OldAmmo = AmmoType.AmmoAmount;
			if ( AmmoType.AddAmmo(Weapon(Item).PickupAmmoCount) && (OldAmmo == 0) 
				&& (P.Weapon.class != item.class) && !P.bNeverSwitchOnPickup )
					WeaponSet(P);
		}
		P.ReceiveLocalizedMessage( class'PickupMessagePlus', 0, None, None, Self.Class );
		Item.PlaySound(Item.PickupSound);
		if (Level.Game.LocalLog != None)
			Level.Game.LocalLog.LogPickup(Item, Pawn(Owner));
		if (Level.Game.WorldLog != None)
			Level.Game.WorldLog.LogPickup(Item, Pawn(Owner));
		Item.SetRespawn();   
		return true;
	}
	if ( Inventory == None )
		return false;

	return Inventory.HandlePickupQuery(Item);

}*/


simulated function PlayRepeatFiring()
{
	if ( Affector != None )
		Affector.FireEffect();
	if ( PlayerPawn(Owner) != None )
	{
		PlayerPawn(Owner).ClientInstantFlash( -0.2, vect(325, 225, 95));
		PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
	}
	bMuzzleFlash++;
	PlayOwnedSound(FireSound, SLOT_None,2.0*Pawn(Owner).SoundDampening);
	PlayAnim('Shot2', (0.7 + 0.3 * FireAdjust) * UBFireRate, 0.05 / UBFireRate);
}

state AltFiring
{
ignores Fire, AltFire, AnimEnd;

	function Timer()
	{
		if ( Slaveenforcer != none )
			Slaveenforcer.AltFire(0);
	}

	function EndState()
	{
		Super.EndState();
		OldFlashCount = FlashCount;
	}

Begin:
	if ( SlaveEnforcer != none )
		SetTimer(0.20, false);
	FinishAnim();
Repeater:	
	if (AmmoType.UseAmmo(1)) 
	{
		FlashCount++;
		if ( SlaveEnforcer != None )
			Pawn(Owner).PlayRecoil(3 * FiringSpeed);
		else if ( !bIsSlave )
			Pawn(Owner).PlayRecoil(1.5 * FiringSpeed);
		TraceFire(AltAccuracy);
		PlayRepeatFiring();
		FinishAnim();
	}

	if ( AltAccuracy < 3 ) 
		AltAccuracy += 0.5;
	if ( bIsSlave )
	{
		if ( (Pawn(Owner).bAltFire!=0) 
			&& AmmoType.AmmoAmount>0 )
			Goto('Repeater');
	}
	else if ( bChangeWeapon )
		GotoState('DownWeapon');
	else if ( (Pawn(Owner).bAltFire!=0) 
		&& AmmoType.AmmoAmount>0 )
	{
		if ( PlayerPawn(Owner) == None )
			Pawn(Owner).bAltFire = int( FRand() < AltReFireRate );
		Goto('Repeater');	
	}
	PlayAnim('T2', 0.9 * UBFireRate, 0.05 / UBFireRate);	
	FinishAnim();
	Finish();
}


state NormalFire
{
ignores Fire, AltFire, AnimEnd;

	function Timer()
	{
		if ( SlaveEnforcer != none )
			SlaveEnforcer.Fire(0);
	}

	function EndState()
	{
		Super.EndState();
		OldFlashCount = FlashCount;
	}

Begin:
	FlashCount++;
	if ( SlaveEnforcer != none )
		SetTimer(0.20 / UBFireRate, false);
	FinishAnim();
	if ( bIsSlave )
		GotoState('Idle');
	else 
		Finish();
}


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=3rdEnforcerUdam_fx
	UBersFX=3rdEnforcerBers_fx
	DamBersFX=3rdEnforcerBaU_fx
	PickupSound=2_EnforcerPickup
}