class NaliWeapons expands TournamentWeapon config(User);

#exec OBJ LOAD FILE=Textures\NaliHUD.utx PACKAGE=NaliWeaponsII.HUD

var class<Overlay> Overlayer;
var Overlay OV, UDamOv;
var() class<Overlay> StockOverlayers[7];

var() float FireRateChange;
var() bool bInfinity;
var() int KickBack;
var() float MoreDamage;
var() bool HealthGiver;
var() float Splasher;
var() bool bTheOne;

var color FColor;
var byte CurrentHand;

var() texture CustomCross;
var() int CrossHairSize;
var() float CrossHairScale;

var() bool bSpecialOverlayer;
var() mesh SpecialOvMeshR, SpecialOvMeshL;

var texture SpecialHUD[3];
var string SpecialInfo[3];

var() bool bMegaWeapon;
var() bool bForceTranslucentCrossHair;

var texture FXWhite, FXYellow, FXRed, FXBlue, FXGreen, FXPurple, FXMisc;
var texture CurrentPickFX, CurrentModFX;
var bool bInvisible, bModified;
var bool bAddedRandomlly;

var(Crosshair) config bool bCustomCrosshair;
var(Crosshair) config bool bCustomColor;
var(Crosshair) config bool bOpaqueCrosshair;


replication
{
	// Things the server should send to the client.
	reliable if( bNetOwner && (Role==ROLE_Authority) )
		UDamOV, OV, Overlayer, FireRateChange, bInfinity, KickBack, MoreDamage, 
	HealthGiver, Splasher, bTheOne, SpecialHUD, SpecialInfo, CurrentHand;
	reliable if(Role==ROLE_Authority)
		bInvisible, bModified, CurrentPickFX, CurrentModFX, bAddedRandomlly;
}

simulated function BeginPlay()
{
	Super.BeginPlay();

	if (Role == ROLE_Authority)
	{
		if (!Class'NWMutator'.default.bWeaponsDynLight)
			LightType = LT_None;
	}

	LODBias = Class'NWMutator'.default.WeaponDistDetail * 8/255;

	bOwnsCrosshair = class'NaliWeapons'.default.bCustomCrosshair;
}

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

function SetWeaponStay()
{
	if (bMegaWeapon)
		bWeaponStay = False; 	//Mega Weapons never stay
	else
		Super.SetWeaponStay();
}

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

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

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

function Destroyed()
{
	if (OV != None)
		OV.Destroy();

	Super.Destroyed();
}

simulated function TweenDown()
{
	Super.TweenDown();
	LightType = LT_None;
}


simulated function PlaySelect()
{
	Super.PlaySelect();

	if (Role == ROLE_Authority)
	{
		if (Class'NWMutator'.default.bWeaponsDynLight)
			LightType = Default.LightType;
	}
}

function DropFrom(vector StartLocation)
{
	CurrentPickFX = None;
	Super.DropFrom(StartLocation);
}



function inventory SpawnCopy( pawn Other )
{
    local NaliWeapons Copy;
	local int SpecialType;
	local float N, M;

    if( Level.Game.ShouldRespawn(self) )
    {
        Copy = spawn(Class,Other,,,rot(0,0,0));
        Copy.Tag           = Tag;
        Copy.Event         = Event;
        if ( !bWeaponStay )
            GotoState('Sleeping');
    }
    else
        Copy = self;

    Copy.RespawnTime = 0.0;
	if (bAddedRandomlly && !bMegaWeapon)
	Copy.PickupAmmoCount = Copy.Default.PickupAmmoCount * Class'CTFNWMutator'.default.AddedWeaponsAmmoMulti;
    Copy.bHeldItem = true;
    Copy.bTossedOut = false;
    Copy.GiveTo( Other );
	GiveModifier(Copy);
    Copy.Instigator = Other;
    Copy.GiveAmmo(Other);
    Copy.SetSwitchPriority(Other);
    if ( !Other.bNeverSwitchOnPickup )
        Copy.WeaponSet(Other);
    Copy.AmbientGlow = 0;
    return Copy;
}


function GiveModifier( NaliWeapons Copy)
{
local int SpecialType;
local float N, M;

	//Monster Hunt Mode
	if (!bMegaWeapon && !bModified && class'NWMutator'.default.EnableMonsterHuntMode && ((class'NWMutator'.default.MonsterHuntOnly && Level.Game.IsA('MonsterHunt')) || !class'NWMutator'.default.MonsterHuntOnly))
	{
		SpecialType = Rand(9);

		Copy.bModified = True;

		if (SpecialType > 0 && SpecialType < 8)
		{
			Copy.Overlayer = StockOverlayers[SpecialType - 1];

			switch (SpecialType)
			{
				case 1:
					Copy.bInfinity = True;
					Copy.SpecialHUD[0] = Texture'Infinity_HUD';
					Copy.SpecialInfo[0] = "Infinity";
					Copy.CurrentModFX = FXWhite;
					break;
				case 2:
					N = Rand(100) + 10;
					Copy.KickBack = N;
					Copy.SpecialHUD[0] = Texture'Kick_HUD';
					Copy.SpecialInfo[0] = "Kickback +" $ Int(N) $ ".000";
					Copy.CurrentModFX = FXGreen;
					break;
				case 3:
					N = Rand(4) + 2;
					M = Rand(10);
					Copy.MoreDamage = N + (M/10);
					Copy.SpecialHUD[0] = Texture'Damage_HUD';
					Copy.SpecialInfo[0] = "Damage " $ Int(N) $ "." $ Int(M) $ "X";
					Copy.CurrentModFX = FXPurple;
					break;
				case 4:
					Copy.HealthGiver = True;
					Copy.SpecialHUD[0] = Texture'Health_HUD';
					Copy.SpecialInfo[0] = "Health";
					Copy.CurrentModFX = FXBlue;
					break;
				case 5:
					N = Rand(1) + 2;
					M = Rand(10);
					Copy.Splasher = N + (M/10);
					Copy.SpecialHUD[0] = Texture'Splasher_HUD';
					Copy.SpecialInfo[0] = "Splasher " $ Int(N) $ "." $ Int(M) $ "X";
					Copy.CurrentModFX = FXYellow;
					break;
				case 6:
					N = Rand(2) + 2;
					M = Rand(10);
					Copy.FireRateChange = N + (M/10);
					Copy.SpecialHUD[0] = Texture'Fast_HUD';
					Copy.SpecialInfo[0] = "Fast " $ Int(N) $ "." $ Int(M) $ "X";
					Copy.CurrentModFX = FXRed;
					break;
				case 7:
					Copy.bTheOne = True;
					Copy.CurrentModFX = FXMisc;
					break;
			}

		}
		
	}
}


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

	Super.PostRender(Canvas);

	P = PlayerPawn(Owner);
	Scale = Canvas.ClipX/640;
	Canvas.bNoSmooth = False;

	if (bOwnsCrossHair && P != None && CustomCross != None)
	{
		Canvas.SetPos(0.5 * Canvas.ClipX - (CrossHairSize/2) * Scale * CrossHairScale, 0.5 * Canvas.ClipY - (CrossHairSize/2) * Scale * CrossHairScale);

		if (!class'NaliWeapons'.default.bOpaqueCrosshair || bForceTranslucentCrossHair)
			Canvas.Style = ERenderStyle.STY_Translucent;

		if (P.myHUD.IsA('ChallengeHUD'))
		{	
			if (class'NaliWeapons'.default.bCustomColor)
				Canvas.DrawColor = NameColor * 15;
			else
				Canvas.DrawColor = ChallengeHUD(P.myHUD).CrosshairColor * 15;

			Canvas.DrawIcon(CustomCross, Scale * CrossHairScale);

		}
		else
		{
			if (class'NaliWeapons'.default.bCustomColor)
				Canvas.DrawColor = NameColor * 15;
			else
			{
				Canvas.DrawColor.R = 0;
				Canvas.DrawColor.B = 0;
				Canvas.DrawColor.G = 255;
			}

			Canvas.DrawIcon(CustomCross, Scale * CrossHairScale);
		}
	}
	
	Canvas.Style = ERenderStyle.STY_Normal;

	if (bTheOne)
	{
		For (i=0; i<3; i++)
		{
			Canvas.DrawColor = Canvas.Default.DrawColor;

			if (SpecialHUD[i] != None)
			{
				Canvas.SetPos(0, 0.3 * Canvas.ClipY + (16 + 35*(2-i)) * Scale);
				Canvas.DrawIcon(SpecialHUD[i], Scale/8);
				
					if (SpecialInfo[i] != "")
					{
						Canvas.SetPos(40 * Scale, 0.3 * Canvas.ClipY + (32 + 32*(2-i)) * Scale);
						Canvas.DrawColor = FColor;
						Canvas.DrawText(SpecialInfo[i]);
					}
			}
		}
	}
	else if (SpecialHUD[0] != None)
	{
		Canvas.DrawColor = Canvas.Default.DrawColor;
		Canvas.SetPos(0.15 * Canvas.ClipX + 16 * Scale, 0.85 * Canvas.ClipY);
		Canvas.DrawIcon(SpecialHUD[0], Scale/8);

		Canvas.SetPos(0.15 * Canvas.ClipX + 68 * Scale, 0.85 * Canvas.ClipY + 16 * Scale);
		Canvas.DrawColor = FColor;
		Canvas.DrawText(SpecialInfo[0]);
	}
}


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

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

	
	//Overlayer
	if (OV != None)
	{
	if (OV.Class != Overlayer)
	{
		OV.Destroy();
		OV = None;
	}
	}

	if (Overlayer != None && OV == None && PlayerOwner != None)
	{
		OV = Spawn(Overlayer, PlayerOwner,,  Location , Rotation);
		
		if (!bSpecialOverlayer)
			OV.Mesh = Mesh;
		else
		{
			if (Hand == 1)
				Ov.Mesh = SpecialOvMeshL;
			else
				Ov.Mesh = SpecialOvMeshR;
		}			

		OV.DrawScale = PlayerViewScale;
		OV.bOnlyOwnerSee = True;
	}

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

		if (CurrentHand != Hand)
		{	
			CurrentHand = Hand;

			if (!bSpecialOverlayer)
				OV.Mesh = Mesh;
			else
			{
				if (Hand == 1)
					Ov.Mesh = SpecialOvMeshL;
				else
					Ov.Mesh = SpecialOvMeshR;
			}
		}
	}

	if (OV != None && PlayerOwner == None)
		OV.Destroy();


	//UDamage Overlayer
	if (UDamOV == None && PlayerOwner != None && PlayerOwner.FindInventoryType(Class'NeoXUDamage') != None)
	{
		UDamOV = Spawn(Class'N_Over_UDamage', PlayerOwner,,  Location , Rotation);			
		UDamOV.DrawScale = PlayerViewScale;
		UDamOV.bOnlyOwnerSee = True;
		UDamOV.Mesh = Mesh;
	}

	if (UDamOV != None)
	{

		if (!bSpecialOverlayer)
			UDamOV.Mesh = Mesh;
		else
		{
			if (Hand == 1)
				UDamOv.Mesh = SpecialOvMeshL;
			else
				UDamOv.Mesh = SpecialOvMeshR;
		}

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

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


	SetTranslucentWeapon();

}


simulated function SetTranslucentWeapon()
{
	
	if (Pawn(Owner) != None)
	{
		if (Pawn(Owner).FindInventoryType(class'NeoXInvisibility')!=None || Pawn(Owner).FindInventoryType(class'NeoXStealth')!=None 
		|| Pawn(Owner).FindInventoryType(class'UT_Invisibility')!=None || Pawn(Owner).FindInventoryType(class'UT_Stealth')!=None)
		{
			Texture = Texture'UnrealShare.Belt_fx.Invis.Invis';
			Style = STY_Translucent;
			bMeshEnviroMap = True;
			bUnlit = True;
			bInvisible = True;
		}
		else if (Pawn(Owner).FindInventoryType(class'UDamage')!=None && Pawn(Owner).FindInventoryType(class'NeoXUDamage')==None)
		{
			Texture = FXPurple;
			Style = STY_Translucent;
			bMeshEnviroMap = True;
			bUnlit = True;
			bInvisible = False;
		}
		else if (CurrentPickFX != None && !bInvisible)
		{
			Texture = CurrentPickFX;
			Style = STY_Translucent;
			bMeshEnviroMap = True;
			bUnlit = True;
		}
		else if (CurrentModFX != None && !bInvisible)
		{
			Texture = CurrentModFX;
			Style = STY_Modulated;
			bMeshEnviroMap = True;
			bUnlit = True;
		}
		else
		{
			Texture = Default.Texture;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = False;
			bInvisible = False;
		}
	}
	else if (CurrentModFX != None)
	{
		Texture = CurrentModFX;
		Style = STY_Modulated;
		bMeshEnviroMap = True;
		bUnlit = True;
	}
	else
	{
		Texture = Default.Texture;
		Style = STY_Normal;
		bMeshEnviroMap = False;
		bUnlit = False;
		bInvisible = False;
	}
}


simulated function SetNormalViewWeapon()
{

	if ( Owner == None )
		SetTranslucentWeapon();

	if (Pawn(Owner) != None)
	{
		if (Pawn(Owner).FindInventoryType(class'NeoXInvisibility')!=None || Pawn(Owner).FindInventoryType(class'NeoXStealth')!=None 
		|| Pawn(Owner).FindInventoryType(class'UT_Invisibility')!=None || Pawn(Owner).FindInventoryType(class'UT_Stealth')!=None)
		{
			Style = STY_Translucent;
			Texture = None;
			bMeshEnviroMap = False;
			bUnlit = True;
		}
		else if (Pawn(Owner).FindInventoryType(class'UDamage')!=None && Pawn(Owner).FindInventoryType(class'NeoXUDamage')==None)
		{
			Texture = FXPurple;
			Style = STY_Translucent;
			bMeshEnviroMap = True;
			bUnlit = True;
		}
		else
		{
			Texture = Default.Texture;
			Style = STY_Normal;
			bMeshEnviroMap = False;
			bUnlit = False;
		}
	}
	else
	{
		Texture = Default.Texture;
		Style = STY_Normal;
		bMeshEnviroMap = False;
		bUnlit = False;
	}
	
}

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


State Sleeping
{
    ignores Touch;

    function BeginState()
    {
        BecomePickup();
        bHidden = true;
	LightType = LT_None;
    }

    function EndState()
    {
        local int i;

        bSleepTouch = false;

	if (Class'NWMutator'.default.bWeaponsDynLight)
		LightType = Default.LightType;
        for ( i=0; i<4; i++ )
            if ( (Touching[i] != None) && Touching[i].IsA('Pawn') )
                bSleepTouch = true;
    }    
       
Begin:
    Sleep( ReSpawnTime );
    PlaySound( RespawnSound );  
    Sleep( Level.Game.PlaySpawnEffect(self) );
    GoToState( 'Pickup' );
}




defaultproperties
{
	FireRateChange=1.000000
	LODBias=8.000000
	StockOverlayers(0)=Class'N_Over_Inf'
	StockOverlayers(1)=Class'N_Over_Kick'
	StockOverlayers(2)=Class'Overlay'
	StockOverlayers(3)=Class'N_Over_Health'
	StockOverlayers(4)=Class'N_Over_Splash'
	StockOverlayers(5)=Class'N_Over_Fast'
	StockOverlayers(6)=Class'N_Over_TheOne'
	FColor=(R=0,G=255,B=255)
	CrossHairScale=1.000000
	bCustomCrosshair=True
	bCustomColor=True
	bOpaqueCrosshair=True
	FXWhite=White_Over
	FXYellow=Yeallow_Over
	FXRed=Red_Over
	FXBlue=Blue_Over
	FXGreen=Green_Over
	FXPurple=Texture'UnrealShare.DamageWet'
	FXMisc=One_Over
}
