//=============================================================================
// BattleSuit. Almost invicibility.
//=============================================================================
class BattleSuit expands UBers_ShieldBelt;

//Model import
#exec mesh import mesh=BattleSuitv3 anivfile=Models\BattleSuitv3_a.3d datafile=Models\BattleSuitv3_d.3d x=0 y=0 z=0 MLOD=1 LODSTYLE=0
#exec MESH LODPARAMS MESH=BattleSuitv3 STRENGTH=0.25
#exec mesh origin mesh=BattleSuitv3 x=0 y=0 z=0
#exec mesh sequence mesh=BattleSuitv3 seq=All startframe=0 numframes=1

#exec meshmap new meshmap=BattleSuitv3 mesh=BattleSuitv3
#exec meshmap scale meshmap=BattleSuitv3 x=0.081 y=0.081 z=0.162

//Audio import
#exec AUDIO IMPORT NAME="InvulnerablePick" FILE=SOUNDS\InvulnerablePick.wav GROUP="BattleSuit"
#exec AUDIO IMPORT NAME="InvulnerableWarn" FILE=SOUNDS\InvulnerableWarn.wav GROUP="BattleSuit"
#exec AUDIO IMPORT NAME="InvulnerableEnd" FILE=SOUNDS\InvulnerableEnd.wav GROUP="BattleSuit"

//FX import
#exec TEXTURE IMPORT NAME=BattleSuitNew FILE=SKINS\BattleSuitNew.pcx GROUP=Skins FLAGS=2
#exec TEXTURE IMPORT NAME=BattleSuitNew FILE=SKINS\BattleSuitNew.pcx GROUP=Skins PALETTE=BattleSuitNew

#exec OBJ LOAD FILE=Textures\XPickupsFX.utx PACKAGE=XPickups.FX
#exec OBJ LOAD FILE=Sounds\Loopsounds.uax PACKAGE=XPickups.Misc
#exec obj load file=..\Textures\SpaceFX.utx package=SpaceFX


var int FinalCount;

var() travel int TimeActive;

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

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

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

var bool bDroppedNWaiting;

var BattleCorona Effect1;

var() byte UseSoundRadius, UseSoundVol;

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

simulated function PostBeginPlay()
{
	Effect1 = Spawn(Class'BattleCorona', Self);

	Super(ut_ShieldBelt).PostBeginPlay();
}

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

	if (WearedOffEvent != '' && (TimeActive <= 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 != '' && TimeActive > 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 (Effect1 != None)
		Effect1.Destroy();

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

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

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

	Super(ut_ShieldBelt).Destroyed();
}


function DropACopy()
{
local BattleSuit 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'BattleSuit', None,, PossibleLoc, Rand(32768) * rot(0,1,0));
	else
		BS = Spawn(Class'BattleSuit', None,, Pawn(Owner).Location, Rand(32768) * rot(0,1,0));

	BS.bDroppedNWaiting = True;
	BS.TimeActive = TimeActive;
	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];
	}
}


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;
	BattleSuit(Copy).WearedOffEvent = WearedOffEvent;
	BattleSuit(Copy).DroppedEvent = DroppedEvent;
	BattleSuit(Copy).ReActivateEvent = ReActivateEvent;
	BattleSuit(Copy).TimeActive = TimeActive;
    Copy.GiveTo( Other );
    return Copy;
}




function PickupFunction(Pawn Other)
{
    local Inventory I;

    MyEffect = Spawn(class'BattleEffect', Other,,Other.Location, Other.Rotation); 
    MyEffect.Mesh = Owner.Mesh;
    MyEffect.DrawScale = Owner.Drawscale;

    SetEffectTexture();

    I = Pawn(Owner).FindInventoryType(class'UBersInvisibility');
    if ( I != None )
        MyEffect.bHidden = true;
}

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

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

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

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

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

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

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

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


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

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

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

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


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

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

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

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

		if (Event == '')
			Event = BattleSuit(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);
}


singular function UsedUp()
{

    if ( Owner != None )
    {
        if (Level.Game.LocalLog != None)
            Level.Game.LocalLog.LogItemDeactivate(Self, Pawn(Owner));
        if (Level.Game.WorldLog != None)
            Level.Game.WorldLog.LogItemDeactivate(Self, Pawn(Owner));
    }

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

    Destroy();
}


function ArmorImpactEffect(vector HitLocation)
{ 
    if ( Owner.IsA('PlayerPawn') )
    {
        PlayerPawn(Owner).ClientFlash(-0.05,vect(400,400,400));
    }
}


function SetEffectTexture()
{
    MyEffect.ScaleGlow = 1.0;
    if ( TeamFireTextures[TeamNum] == None )
        TeamFireTextures[TeamNum] =FireTexture(DynamicLoadObject(TeamFireTextureStrings[TeamNum], class'Texture'));
    MyEffect.Texture = TeamFireTextures[TeamNum];
    if ( TeamTextures[TeamNum] == None )
        TeamTextures[TeamNum] = Texture(DynamicLoadObject(TeamTextureStrings[TeamNum], class'Texture'));
    MyEffect.LowDetailTexture = TeamTextures[TeamNum];
}

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

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

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

state Activated
{

	function Timer()
	{

		TimeActive--;

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

	function EndState()
	{
		UsedUp();
	}

	simulated function Tick( float DeltaTime)
	{
		if ((Pawn(Owner).FindInventoryType(Class'DoubleSpeed') != None || Pawn(Owner).FindInventoryType(Class'UBerseker') != None || Pawn(Owner).FindInventoryType(Class'UbersUDamage') != None || Pawn(Owner).FindInventoryType(Class'UbersInvisibility') != None) && ast!=None)
		{
			ast.Destroy();
			ast = None;
		}
		else if (ast==None && (Pawn(Owner).FindInventoryType(Class'DoubleSpeed') == None && Pawn(Owner).FindInventoryType(Class'UbersUDamage') == None && Pawn(Owner).FindInventoryType(Class'UbersInvisibility') == 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;

    		FinalCount = Min(FinalCount, TimeActive - 1);
		SetTimer(1.0,false);
   		Owner.PlaySound(PickupSound);
		LifeSpan = 0.0;
		AmbientSound = None;

		SpawnAS();

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


function int ArmorAbsorbDamage(int Damage, name DamageType, vector HitLocation)
{
    local int ArmorDamage;

    if ( DamageType == 'Drowned' || DamageType == 'Burned' || DamageType == 'Frozen' || DamageType == 'Corroded' || DamageType == 'Fell')
	return 0;

    if( (DamageType!='None') && ((ProtectionType1==DamageType) || (ProtectionType2==DamageType)) )
        return 0;
    
    If (Damage < 40)
	return 0;
    else
	return (Damage/2);
}


defaultproperties
{
	RespawnSound=InvulnerablePick
	LODBias=0.850000
	bAutoActivate=True
     bActivatable=True
	AbsorptionPriority=12
	Mesh=BattleSuitv3
	PickupViewMesh=BattleSuitv3
     DeActivateSound=InvulnerableWarn
     Charge=0
     TimeActive=30
     FinalCount=4
     PickupSound=InvulnerablePick
     RespawnTime=136.000000
     Itemname="Battle Suit"
     TeamFireTextureStrings(0)="XPickups.FX.FX.BattleSuitFX"
     TeamFireTextureStrings(1)="XPickups.FX.FX.BattleSuitFX"
     TeamFireTextureStrings(2)="XPickups.FX.FX.BattleSuitFX"
     TeamFireTextureStrings(3)="XPickups.FX.FX.BattleSuitFX"
     TeamFireTextures(0)=BattleSuitFX
     TeamFireTextures(1)=BattleSuitFX
     TeamFireTextures(2)=BattleSuitFX
     TeamFireTextures(3)=BattleSuitFX
     TeamTextureStrings(0)="XPickups.Skins.ShieldBelt.BattleSuitNew"
     TeamTextureStrings(1)="XPickups.Skins.ShieldBelt.BattleSuitNew"
     TeamTextureStrings(2)="XPickups.Skins.ShieldBelt.BattleSuitNew"
     TeamTextureStrings(3)="XPickups.Skins.ShieldBelt.BattleSuitNew"
     TeamTextures(0)=BattleSuitNew
     TeamTextures(1)=BattleSuitNew
     TeamTextures(2)=BattleSuitNew
     TeamTextures(3)=BattleSuitNew
     bDisplayableInv=False
     PickupMessage="BATTLE SUIT"
     CollisionRadius=30.000000
  CollisionHeight=20.000000
  MultiSkins(0)=Texture'SpaceFX.worm3c'
  MultiSkins(1)=Texture'XPickups.Skins.InvSkin05'
  MultiSkins(2)=Texture'XPickups.Skins.InvSkin01'
  MultiSkins(3)=Texture'BotPack.Ammocount.AmmoCountJunk'
  MultiSkins(4)=Texture'XPickups.Skins.InvSkin00'
  MultiSkins(5)=Texture'XPickups.Skins.InvSkin04'
  MultiSkins(6)=Texture'XPickups.Skins.UBersSkin00'
	SoundVolume=135
	SoundRadius=55
	GroundSound=Invul_GroundLoop
	UseSound=Invul_PowerLoop
	MaxDesireability=4.000000
	UseSoundRadius=20
	UseSoundVol=255
}