//=============================================================================
// UBersUDamage.
//=============================================================================
class UBersUDamage expands UDamage;

//Model Import
#exec mesh import mesh=ProofUdamTrans anivfile=Models\ProofUdamTrans_a.3d datafile=Models\ProofUdamTrans_d.3d x=0 y=0 z=0 mlod=0
#exec mesh origin mesh=ProofUdamTrans x=0 y=0 z=0
#exec mesh sequence mesh=ProofUdamTrans seq=All startframe=0 numframes=1

#exec meshmap new meshmap=ProofUdamTrans mesh=ProofUdamTrans
#exec meshmap scale meshmap=ProofUdamTrans x=0.10408 y=0.10408 z=0.20816

//Audio import
#exec AUDIO IMPORT NAME="UT3UDamEnd" FILE=SOUNDS\UT3UDamEnd.wav GROUP="UDamage"
#exec AUDIO IMPORT NAME="UT3UDamFire" FILE=SOUNDS\UT3UDamFire.wav GROUP="UDamage"
#exec AUDIO IMPORT NAME="UT3UDamPick" FILE=SOUNDS\UT3UDamPick.wav GROUP="UDamage"
#exec AUDIO IMPORT NAME="UT3UDamWarn" FILE=SOUNDS\UT3UDamWarn.wav GROUP="UDamage"
#exec AUDIO IMPORT NAME="UdamageRespawn" FILE=SOUNDS\UdamageRespawn.wav GROUP="UDamage"

//Skins import
#exec TEXTURE IMPORT NAME=InvSkin04 FILE=SKINS\PBbas18AL.pcx GROUP=Skins FLAGS=2
#exec TEXTURE IMPORT NAME=InvSkin04 FILE=SKINS\PBbas18AL.pcx GROUP=Skins PALETTE=InvSkin04
#exec MESHMAP SETTEXTURE MESHMAP=ProofUdamTrans NUM=0 TEXTURE=InvSkin04

#exec TEXTURE IMPORT NAME=InvSkin01 FILE=SKINS\CBotT3.pcx GROUP=Skins FLAGS=2
#exec TEXTURE IMPORT NAME=InvSkin01 FILE=SKINS\CBotT3.pcx GROUP=Skins PALETTE=InvSkin01
#exec MESHMAP SETTEXTURE MESHMAP=ProofUdamTrans NUM=1 TEXTURE=InvSkin01

#exec TEXTURE IMPORT NAME=UDamSkin02 FILE=SKINS\UT2Udamage.pcx GROUP=Skins FLAGS=2
#exec TEXTURE IMPORT NAME=UDamSkin02 FILE=SKINS\UT2Udamage.pcx GROUP=Skins PALETTE=UDamSkin02
#exec MESHMAP SETTEXTURE MESHMAP=ProofUdamTrans NUM=2 TEXTURE=UDamSkin02

#exec TEXTURE IMPORT NAME=UBersSkin00 FILE=SKINS\udamExtraT.pcx GROUP=Skins FLAGS=2
#exec TEXTURE IMPORT NAME=UBersSkin00 FILE=SKINS\udamExtraT.pcx GROUP=Skins PALETTE=UBersSkin00
#exec MESHMAP SETTEXTURE MESHMAP=ProofUdamTrans NUM=3 TEXTURE=UBersSkin00

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


var(UDamage) texture UDamageGFX;
var(UDamage) sound UDamEndSound;

var() name WearedOffEvent;
var() name DroppedEvent;
var() name ReActivateEvent;

var bool bDroppedNWaiting;

var() sound GroundSound, UseSound;
var AmbientSTrail ast;

var UDamageEffect Effect1A, Effect1B, Effect1C;
var UDamageCoronaV Effect2;
var UDamageCoronaH Effect3A, Effect3B;
var UDamageSOver OverFX;

var float EffectTimeCountA, EffectTimeCountB;
var bool bEffectSpawnedA, bEffectSpawnedB;

var name WOEvent[32];
var name DEvent[32];
var name RAEvent[32];

var() byte UseSoundRadius, UseSoundVol;

replication
{
    reliable if( Role==ROLE_Authority)
	bDroppedNWaiting;
}

simulated function PostBeginPlay()
{
	Effect1A = Spawn(Class'UDamageEffect', Self);
	Effect2 = Spawn(Class'UDamageCoronaV', Self);
	Effect3A = Spawn(Class'UDamageCoronaH', Self);
	Effect3A.PrePivotDelta = vect(0,5,25);
	Effect3B = Spawn(Class'UDamageCoronaH', Self);
	Effect3B.PrePivotDelta = vect(0,-5,25);
	OverFX = Spawn(Class'UDamageSOver', Self);

	Super.PostBeginPlay();
}

simulated function Destroyed()
{
local Actor A;
local byte bj;

	if (WearedOffEvent != '' && (Charge <= 0 || bDroppedNWaiting))
	{
	ForEach AllActors( class 'Actor', A, WearedOffEvent)
                		A.Trigger( Self, Instigator );

		For (bj=0; bj<32; bj++)
		{
			if (WOEvent[bj] != '')
			{
				ForEach AllActors( class 'Actor', A, WOEvent[bj])
                			A.Trigger( Self, Instigator );
			}
		}
	}

	if (DroppedEvent != '' && Charge > 0 && Pawn(Owner)!=None)
	{
	ForEach AllActors( class 'Actor', A, DroppedEvent)
                		A.Trigger( Self, Instigator );

		For (bj=0; bj<32; bj++)
		{
			if (DEvent[bj] != '')
			{
				ForEach AllActors( class 'Actor', A, DEvent[bj])
                			A.Trigger( Self, Instigator );
			}
		}
	}


	if (Effect1A != None)
		Effect1A.Destroy();
	if (Effect1B != None)
		Effect1B.Destroy();
	if (Effect1C != None)
		Effect1C.Destroy();
	if (Effect2 != None)
		Effect2.Destroy();
	if (Effect3A != None)
		Effect3A.Destroy();
	if (Effect3B != None)
		Effect3B.Destroy();
	if (OverFX != None)
		OverFX.Destroy();

	if (ast != None)
		ast.Destroy();

	if (Charge > 0 && !bDroppedNWaiting)
		DropACopy();

	if (Pawn(Owner) == None && !bHidden)
		Level.Game.PlaySpawnEffect(Self);

	Super.Destroyed();
}

function DropACopy()
{
local UBersUDamage BS;
local vector PossibleLoc, HitLocation, HitNormal;
local actor AT;
local byte bj;

	PossibleLoc = Pawn(Owner).Location + (Rand(128) - 64)*vect(1,0,0) + (Rand(128) - 64)*vect(0,1,0);
	AT = Trace (HitLocation, HitNormal, PossibleLoc, Pawn(Owner).Location, False);

	if (AT == None)
		BS = Spawn(Class'UBersUDamage', None,, PossibleLoc, Rand(32768) * rot(0,1,0));
	else
		BS = Spawn(Class'UBersUDamage', None,, Pawn(Owner).Location, Rand(32768) * rot(0,1,0));

	BS.bDroppedNWaiting = True;
	BS.Charge = Charge;
	BS.Event = Event;
	BS.WearedOffEvent = WearedOffEvent;
	BS.SetPhysics(PHYS_Falling);
	BS.bRotatingPickup = False;
	BS.RotationRate = rot(0,0,0);
	BS.Tag = Tag;
	BS.bFixedRotationDir = False;
	BS.ReActivateEvent = ReActivateEvent;
	BS.DroppedEvent = DroppedEvent;
	BS.LifeSpan = 20.0;

	For (bj=0; bj<32; bj++)
	{
		BS.WOEvent[bj] = WOEvent[bj];
		BS.DEvent[bj] = DEvent[bj];
		BS.RAEvent[bj] = RAEvent[bj];
	}
}

simulated function Tick(float DeltaTime)
{
	if (!bHidden && Pawn(Owner) == None && AmbientSound != GroundSound)
		AmbientSound = GroundSound;
	else if (bHidden && AmbientSound == GroundSound)
		AmbientSound = None;

	if (!bEffectSpawnedA)
		EffectTimeCountA += DeltaTime;

	if (!bEffectSpawnedA && EffectTimeCountA > 0.2)
	{
		bEffectSpawnedA = True;
		Effect1B = Spawn(Class'UDamageEffect', Self);
		Effect1B.Mesh = Mesh'UDamageEffectB';
	}

	if (!bEffectSpawnedB)
		EffectTimeCountB += DeltaTime;

	if (!bEffectSpawnedB && EffectTimeCountB > 0.4)
	{
		bEffectSpawnedB = True;
		Effect1C = Spawn(Class'UDamageEffect', Self);
		Effect1C.Mesh = Mesh'UDamageEffectB';
	}

	if ((Region.Zone.bDestructive || Region.Zone.bKillZone || Region.Zone.DamagePerSec > 0) && Pawn(Owner) == None)
	{
		Charge = 0;
		Destroy();
	}
		
}

simulated function SpawnAS()
{
	ast = Spawn(Class'AmbientSTrail', Pawn(Owner));
	ast.AmbientSound = UseSound;
	ast.SoundRadius = UseSoundRadius;
	ast.SoundVolume = UseSoundVol;
}

function inventory SpawnCopy( pawn Other )
{
    local inventory Copy;

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

    Copy.RespawnTime = 0.0;
    Copy.bHeldItem = true;
	UBersUDamage(Copy).WearedOffEvent = WearedOffEvent;
	UBersUDamage(Copy).DroppedEvent = DroppedEvent;
	UBersUDamage(Copy).ReActivateEvent = ReActivateEvent;
    Copy.Charge = Charge;
    Copy.GiveTo( Other );
    return Copy;
}


function bool HandlePickupQuery( inventory Item )
{
local byte bj, bi;
local bool bHaveThisEventReg;

    if (item.class == class) 
    {
        if ( Charge < 999 )  
	{
                Charge += Item.Charge;
		if (Charge > 999)
			Charge = 999;

		//Events management
		///////////////////////////////////////////////////////////

		if (WearedOffEvent == '')
			WearedOffEvent = UbersUDamage(Item).WearedOffEvent;
		if (DroppedEvent == '')
			DroppedEvent = UbersUDamage(Item).DroppedEvent;

		if (WearedOffEvent != UbersUDamage(Item).WearedOffEvent)
		{
			bHaveThisEventReg = False;
			For (bj=0; bj<32; bj++)
			{
				if (WOEvent[bj] == UbersUDamage(Item).WearedOffEvent)
				{
					bHaveThisEventReg = True;
					break;
				}
			}

			if (!bHaveThisEventReg)
			{
				For (bj=0; bj<32; bj++)
				{
					if (WOEvent[bj] == '')
					{
						WOEvent[bj] = UbersUDamage(Item).WearedOffEvent;
						break;
					}
				}
			}
		}

		if (DroppedEvent != UbersUDamage(Item).DroppedEvent)
		{
			bHaveThisEventReg = False;
			For (bj=0; bj<32; bj++)
			{
				if (DEvent[bj] == UbersUDamage(Item).DroppedEvent)
				{
					bHaveThisEventReg = True;
					break;
				}
			}

			if (!bHaveThisEventReg)
			{
				For (bj=0; bj<32; bj++)
				{
					if (DEvent[bj] == '')
					{
						DEvent[bj] = UbersUDamage(Item).DroppedEvent;
						break;
					}
				}
			}
		}

		if (ReActivateEvent != UbersUDamage(Item).ReActivateEvent)
		{
			bHaveThisEventReg = False;
			For (bj=0; bj<32; bj++)
			{
				if (RAEvent[bj] == UbersUDamage(Item).ReActivateEvent)
				{
					bHaveThisEventReg = True;
					break;
				}
			}

			if (!bHaveThisEventReg)
			{
				For (bj=0; bj<32; bj++)
				{
					if (RAEvent[bj] == '')
					{
						RAEvent[bj] = UbersUDamage(Item).ReActivateEvent;
						break;
					}
				}
			}
		}

		For (bj=0; bj<32; bj++)
		{
			if (UbersUDamage(Item).WOEvent[bj] != '')
			{
				bHaveThisEventReg = False;
				For (bi=0; bi<32; bi++)
				{
					if (UbersUDamage(Item).WOEvent[bj] == WOEvent[bi])
					{
						bHaveThisEventReg = True;
						break;
					}
				}

				
				if (!bHaveThisEventReg)
				{
					For (bi=0; bi<32; bi++)
					{
						if (WOEvent[bi] == '')
						{
							WOEvent[bi] = UbersUDamage(Item).WOEvent[bj];
							break;
						}
					}
				}
				
			}
			else
				break;
		}


		For (bj=0; bj<32; bj++)
		{
			if (UbersUDamage(Item).DEvent[bj] != '')
			{
				bHaveThisEventReg = False;
				For (bi=0; bi<32; bi++)
				{
					if (UbersUDamage(Item).DEvent[bj] == DEvent[bi])
					{
						bHaveThisEventReg = True;
						break;
					}
				}

				
				if (!bHaveThisEventReg)
				{
					For (bi=0; bi<32; bi++)
					{
						if (DEvent[bi] == '')
						{
							DEvent[bi] = UbersUDamage(Item).DEvent[bj];
							break;
						}
					}
				}
				
			}
			else
				break;
		}

		For (bj=0; bj<32; bj++)
		{
			if (UbersUDamage(Item).RAEvent[bj] != '')
			{
				bHaveThisEventReg = False;
				For (bi=0; bi<32; bi++)
				{
					if (UbersUDamage(Item).RAEvent[bj] == RAEvent[bi])
					{
						bHaveThisEventReg = True;
						break;
					}
				}

				
				if (!bHaveThisEventReg)
				{
					For (bi=0; bi<32; bi++)
					{
						if (RAEvent[bi] == '')
						{
							RAEvent[bi] = UbersUDamage(Item).RAEvent[bj];
							break;
						}
					}
				}
				
			}
			else
				break;
		}

			

		if (Event == '')
			Event = UbersUDamage(Item).Event;
		////////////////////////////////////////////////////////////////////////////////////
	}
            if (Level.Game.LocalLog != None)
                Level.Game.LocalLog.LogPickup(Item, Pawn(Owner));
            if (Level.Game.WorldLog != None)
                Level.Game.WorldLog.LogPickup(Item, Pawn(Owner));
            if ( Item.PickupMessageClass == None )
                Pawn(Owner).ClientMessage(item.PickupMessage, 'Pickup');
            else
                Pawn(Owner).ReceiveLocalizedMessage( item.PickupMessageClass, 0, None, None, item.Class );
            Item.PlaySound (item.PickupSound,,2.0);
            Item.SetReSpawn();

        return true;        
    }

    if ( Inventory == None )
        return false;

    return Inventory.HandlePickupQuery(Item);
}


simulated function FireEffect()
{
        Pawn(Owner).Weapon.PlayOwnedSound(ExtraFireSound, SLOT_Interact, 8);
}


function SetOwnerLighting()
{
	if ((Pawn(Owner).FindInventoryType(Class'UBerseker')==None
		|| Pawn(Owner).FindInventoryType(Class'UBerseker').Charge <= 0)
		&&
		(Pawn(Owner).FindInventoryType(Class'UBersInvisibility')==None
		|| Pawn(Owner).FindInventoryType(Class'UBersInvisibility').Charge <= 0))
		{

	if ( Owner.bIsPawn && Pawn(Owner).bIsPlayer
		&& (Pawn(Owner).PlayerReplicationInfo.HasFlag != None) ) 
		return;
	Owner.AmbientGlow = 254; 
	Owner.LightEffect=LE_NonIncidence;
	Owner.LightBrightness=255;
	Owner.LightHue=210;
	Owner.LightRadius=10;
	Owner.LightSaturation=0;
	Owner.LightType=LT_Steady;
	
	
	}
}

function SetUDamageWeapon()
{
	if ( !bActive )
		return;

	SetOwnerLighting();

	// Make old weapon normal again.
	if ( UDamageWeapon != None )
	{
		UDamageWeapon.SetDefaultDisplayProperties();
		if ( UDamageWeapon.IsA('TournamentWeapon') )
			TournamentWeapon(UDamageWeapon).Affector = None;
	}
		
	UDamageWeapon = Pawn(Owner).Weapon;
	// Make new weapon cool.

	if ( UDamageWeapon != None )
	{
		if ( UDamageWeapon.IsA('TournamentWeapon') )
			TournamentWeapon(UDamageWeapon).Affector = self;

		if (!UDamageWeapon.IsA('BersChainSaw') && !UDamageWeapon.IsA('UbersImpactHammer') && !UDamageWeapon.IsA('UBersEnforcer') && !UDamageWeapon.IsA('UbersUT_Biorifle')
 && !UDamageWeapon.IsA('UBersShockRifle') && !UDamageWeapon.IsA('UBersSuperShockRifle') && !UDamageWeapon.IsA('UBersPulseGun') && !UDamageWeapon.IsA('UBersRipper')
 && !UDamageWeapon.IsA('UBersMinigun2') && !UDamageWeapon.IsA('UBersUT_FlakCannon') && !UDamageWeapon.IsA('UBersUT_Eightball') && !UDamageWeapon.IsA('UBersSniperRifle')
 && !UDamageWeapon.IsA('UBersWarheadLauncher') && !UDamageWeapon.IsA('UBersTranslocator'))
		{
		if ((Pawn(Owner).FindInventoryType(Class'UBerseker')==None 
		|| Pawn(Owner).FindInventoryType(Class'UBerseker').Charge <= 0)
		&&
		(Pawn(Owner).FindInventoryType(Class'UBersInvisibility')==None
		|| Pawn(Owner).FindInventoryType(Class'UBersInvisibility').Charge <= 0))
		{

		if ( Level.bHighDetailMode )
			UDamageWeapon.SetDisplayProperties(ERenderStyle.STY_Translucent, 
									 UDamageGFX,
									 true,
									 true);
		else
			UDamageWeapon.SetDisplayProperties(ERenderStyle.STY_Normal, 
							 UDamageGFX,
							 true,
							 true);

		}
		}

		if (UDamageWeapon.IsA('BersChainSaw'))
			BersChainSaw(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UbersImpactHammer'))
			UbersImpactHammer(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersEnforcer'))
			UBersEnforcer(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UbersUT_Biorifle'))
			UbersUT_Biorifle(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersShockRifle'))
			UBersShockRifle(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersSuperShockRifle'))
			UBersSuperShockRifle(UDamageWeapon).SetPickupSkins();
		else if (UDamageWeapon.IsA('UBersPulseGun'))
			UBersPulseGun(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersRipper'))
			UBersRipper(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersMinigun2'))
			UBersMinigun2(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersUT_FlakCannon'))
			UBersUT_FlakCannon(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersUT_Eightball'))
			UBersUT_Eightball(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersSniperRifle'))
			UBersSniperRifle(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersWarheadLauncher'))
			UBersWarheadLauncher(UDamageWeapon).SetTranslucentWeapon();
		else if (UDamageWeapon.IsA('UBersTranslocator'))
			UBersTranslocator(UDamageWeapon).SetTranslucentWeapon();

	}
}


state Activated
{

	function Timer()
	{

		Charge--;

		if (Charge <= FinalCount && Charge>0)
		{
			SetTimer(0.7, False);
			Owner.PlaySound(DeActivateSound,, 8);
		}
		else if (Charge <= 0)
		{
			Owner.PlaySound(UDamEndSound,, 8);
			UsedUp();
		}
		else
			SetTimer(1.0, False);
	}

	simulated function Tick( float DeltaTime)
	{
		if (Pawn(Owner).FindInventoryType(Class'UBerseker') != None && ast!=None)
		{
			ast.Destroy();
			ast = None;
		}
		else if (ast==None && Pawn(Owner).FindInventoryType(Class'UBerseker') == None)
		{
			ast = Spawn(Class'AmbientSTrail', Pawn(Owner));
			ast.AmbientSound = UseSound;
			ast.SoundRadius = UseSoundRadius;
			ast.SoundVolume = UseSoundVol;
		}
	}

	function BeginState()
	{
	local Actor A;
	local byte bj;
		
		SpawnAS();
		
		bActive = true;
        	FinalCount = Min(FinalCount, Charge - 1);
		SetTimer(1.0,false);
        	Owner.PlaySound(ActivateSound); 
        	SetOwnerLighting();
        	Pawn(Owner).DamageScaling = 3.0;
        	SetUDamageWeapon();
		LifeSpan = 0.0;
		AmbientSound = None;

		if (ReActivateEvent != '' && bDroppedNWaiting)
		{
			ForEach AllActors( class 'Actor', A, ReActivateEvent)
                		A.Trigger( Self, Instigator );

		For (bj=0; bj<32; bj++)
		{
			if (RAEvent[bj] != '')
			{
				ForEach AllActors( class 'Actor', A, RAEvent[bj])
                			A.Trigger( Self, Instigator );
			}
		}

		}

		bDroppedNWaiting = False;
	}
}


State Sleeping
{
    ignores Touch;

    function BeginState()
    {
        BecomePickup();
        bHidden = true;
    }
    function EndState()
    {
        local int i;

        bSleepTouch = false;
        for ( i=0; i<4; i++ )
            if ( (Touching[i] != None) && Touching[i].IsA('Pawn') )
                bSleepTouch = true;
    }           
Begin:
    Sleep( ReSpawnTime );
    Sleep( XPlaySpawnEffect(self) );
    GoToState( 'Pickup' );
}


function float XPlaySpawnEffect(inventory Inv)
{
    spawn(class 'EnhancedReSpawnX',Inv,, Inv.Location );
    return 0.3;
}


defaultproperties
{
	RespawnSound=UdamageRespawn
	Charge=30
	FinalCount=4
	UDamageGFX=FireTexture'UnrealShare.Belt_fx.UDamageFX'
	ExtraFireSound=Sound'XPickups.UDamage.UT3UDamFire'
	PickupSound=Sound'XPickups.UDamage.UT3UDamPick'
	Mesh=Mesh'XPickups.ProofUdamTrans'
	PickupViewMesh=Mesh'XPickups.ProofUdamTrans'
	CollisionHeight=30.000000
	CollisionRadius=23.000000
	RespawnTime=82.000000
	AmbientGlow=128
	bMeshEnviroMap=False
	ScaleGlow=2.000000
	RotationRate=(Yaw=30000)
	UDamEndSound=Sound'XPickups.UDamage.UT3UDamEnd'
	DeActivateSound=Sound'XPickups.UDamage.UT3UDamWarn'
	SoundVolume=135
	SoundRadius=55
	GroundSound=Udamage_GroundLoop
	UseSound=Udamage_PowerLoop
	Texture=UnrealShare.Belt_fx.DamageWet
	MaxDesireability=4.000000
	PickupMessage="DAMAGE AMP"
	UseSoundRadius=20
	UseSoundVol=255
}