class IceBeam expands Projectile;

#exec OBJ LOAD FILE=..\Textures\GenFX.utx
#exec AUDIO IMPORT NAME="Ice_loop" FILE=SOUNDS\Ice_loop.wav GROUP="Freezer"

#exec TEXTURE IMPORT NAME=IceBall FILE=CORONAS\IceBall.bmp GROUP=Coronas FLAGS=2 LODSET=2


var int Kickback;
var float Splasher, MoreDamage;
var bool HealthGiver;
var float Count;

var IceTrail it;


replication
{
	// Things the server should send to the client.
	reliable if(Role==ROLE_Authority)
		Kickback, MoreDamage, HealthGiver, Splasher;
}

simulated function PostBeginPlay()
{
	Super.PostBeginPlay();
	it = Spawn(Class'IceTrail', Self);
}

simulated function Tick(float DeltaTime)
{
	Count += DeltaTime;
	
	if (Count >= 0.03)
	{
		Count = 0;
		Spawn(Class'IceBeamFX',,,, Rotation);
	}
}


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

	Super.Destroyed();
}

function SpawnSpikes(vector HitNormal)
{
	local IceWall iw;

	iw = Spawn(Class'IceWall',,, Location - 4*HitNormal, rotator(HitNormal) + Rand(65535) * rot(0,0,1));
	iw.MoreDamage = MoreDamage;
	iw.HealthGiver = HealthGiver;
}

function SpawnIceBase()
{
local IceBase ib;

	ib = Spawn(Class'IceBase',,, Location, Rand(65535) * rot(0,1,0));

	if (Level.NetMode == NM_StandAlone)
		ib.RotationRate = (Rand(12000) - 6000) * rot(0,1,0);
	else
		ib.bFixedRotationDir = False;
}

function SpawnMegaIce()
{
local MegaIce MI;
local vector X, Y, Z;
	
	GetAxes(Rotation, X, Y, Z);

	MI = Spawn(Class'MegaIce',,, Location + 64*class'MegaIce'.Default.DrawScale*X);
	MI.HealthGiver = HealthGiver;
	MI.MoreDamage = MoreDamage;
}

auto state Flying
{
	simulated function HitWall( vector HitNormal, actor Wall )
	{
		SpawnSpikes(HitNormal);
		Super.HitWall(HitNormal, Wall);
	}

    simulated function ZoneChange( Zoneinfo NewZone )
    {
	local IceBase ib;

        if (NewZone.bWaterZone) //Spawn the ice
	{
		SpawnIceBase();
		Destroy();
	}
    }


    simulated function ProcessTouch (Actor Other, Vector HitLocation)
    {
	local ObjectsFlames of;
	local DumbDoll dd;
	local int i;
	local Actor A;

		ForEach RadiusActors(Class'ObjectsFlames', of, 8.0, Other.Location)
		of.Destroy();

	if (Other.IsA('Pawn') && Other!=Instigator && Role == ROLE_Authority)
	{
	
	If (((Pawn(Other).PlayerReplicationInfo.Team != Instigator.PlayerReplicationInfo.Team && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt')) ||
	Pawn(Other).IsA('ScriptedPawn') || !Level.Game.IsA('TeamGamePlus')) && Other.Tag != 'NoFreeze')
	{

		if (((Pawn(Other).Health < Damage * FMax(1.0,MoreDamage)) || Rand(100) <= 10) && Role == ROLE_Authority)
		{
			
			dd = Spawn(Class'DumbDoll',,, Other.Location, Other.Rotation);

			dd.Mesh = Other.Mesh;
			dd.Fatness = Other.Fatness;
			dd.AnimSequence = Other.AnimSequence;
			dd.AnimFrame = Other.AnimFrame;
			dd.SetCollisionSize(Other.CollisionRadius, Other.CollisionHeight);
			dd.DrawScale = Other.DrawScale;
			dd.bProjTarget = True;
			dd.bCollideWorld = True;
			dd.SetCollision(True, True, True);
			dd.SetPhysics(PHYS_Falling);
			
			For (i=0; i<8; i++)
				dd.Multiskins[i] = Other.Multiskins[i];


			if (Other.Event != '' )
			{
				foreach AllActors( class 'Actor', A, Other.Event )
				{
					A.Trigger( Self, Instigator );
				}
			}


			if ((!Pawn(Other).IsA('Bot') && !Pawn(Other).IsA('Bots') && !Pawn(Other).IsA('PlayerPawn')) || !Level.Game.IsA('TournamentGameInfo'))
				Other.Destroy();
			else
			{

				Pawn(Other).Level.Game.Killed(Instigator, Pawn(Other), 'Freezed');
				Pawn(Other).HidePlayer();
				Pawn(Other).Level.Game.DiscardInventory(Pawn(Other));
				Pawn(Other).Health = -1;
				Pawn(Other).GotoState('Dying');

				if (Other.IsA('PlayerPawn'))
					PlayerPawn(Other).ClientFlash( 4, 10000 * vect(1,1,1));
			}

			Spawn(Class'IceShock',,, HitLocation, rotator(Velocity));
			Destroy();
			return;

		}
	}

	}
	else if ( Other.IsA('Carcass') && Other.Tag != 'NoFreeze' && Other.IsAnimating() && Role == ROLE_Authority)
	{
		dd = Spawn(Class'DumbDoll',,, HitLocation, Other.Rotation);

		dd.Mesh = Other.Mesh;
		dd.Fatness = Other.Fatness;
		dd.AnimSequence = Other.AnimSequence;
		dd.AnimFrame = Other.AnimFrame;
		dd.SetCollisionSize(Other.CollisionRadius, Other.CollisionHeight);
		dd.DrawScale = Other.DrawScale;
		dd.bProjTarget = True;
		dd.bCollideWorld = True;
		dd.SetCollision(True, True, True);
		dd.SetPhysics(PHYS_Falling);
			
		For (i=0; i<8; i++)
			dd.Multiskins[i] = Other.Multiskins[i];

		Spawn(Class'IceShock',,, HitLocation, rotator(Velocity));

		Other.Destroy();
		Destroy();
		return;
	}
	else if (Other.Tag != 'NoFreeze' && Other.IsA('Decoration') && !Other.IsA('DumbDoll') 
	&& !Other.IsA('IceBase') && !Other.IsA('MegaIce'))
	{
		Spawn(Class'IceOv', Other,, Other.Location, Other.Rotation);
		Spawn(Class'IceShock',,, HitLocation, rotator(Velocity));
		Destroy();
		return;
	}

        if ( !Other.IsA('Projectile') && Other!=Level && Other!=Instigator)
	{
            Explode(HitLocation, Normal(Velocity));
	}
    }



    function BlowUp(vector HitLocation)
    {
        HurtRadiusX(Damage * FMax(1.0,MoreDamage), 100 * FMax(1.0, Splasher), MyDamageType, MomentumTransfer * (KickBack + 1), HitLocation);
        MakeNoise(1.0);
    }


    simulated function Explode(vector HitLocation, vector HitNormal)
    {
	local X_Ice_CorpseOv XOv;
	local IceShock s;

	s = Spawn(Class'IceShock',,, HitLocation, rotator(HitNormal));
	s.RemoteRole = ROLE_None;

	BlowUp(HitLocation);

	XOv = Spawn(Class'X_Ice_CorpseOv',,, HitLocation);
	XOv.EffectRadius = 100 * FMax(1.0, Splasher);

        Destroy();
    }



    function BeginState()
    {
        local vector Dir;

        Dir = vector(Rotation);
        Velocity = speed * Dir;
        Acceleration = Dir * 50;

        if (Region.Zone.bWaterZone) //Spawn a mega ice spike
        {
		SpawnMegaIce();
		Destroy();
        }
    }
}


//Special HurtRadius - With Health giving implementation
//******************************************************************************************************************
function HurtRadiusX( float DamageAmount, float DamageRadius, name DamageName, float Momentum, vector HitLocation )
{
    local actor Victims;
    local float damageScale, dist;
    local vector dir;
    local Pawn P;
    
    if( bHurtEntry )
        return;

    bHurtEntry = true;
    foreach VisibleCollidingActors( class 'Actor', Victims, DamageRadius, HitLocation )
    {
	if (HealthGiver && Victims.IsA('Pawn'))
	{
		dir = Victims.Location - HitLocation;
		dist = FMax(1,VSize(dir));
		dir = dir/dist; 
		damageScale = 1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius);

		P = Pawn(Victims);
		If (((P.PlayerReplicationInfo.Team == Instigator.PlayerReplicationInfo.Team && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt')) ||
		(Level.Game.IsA('MonsterHunt') && !P.IsA('ScriptedPawn')) || P==Instigator) && 
		P.Health < Class'NWMutator'.default.MaxHealth)
		{
			P.Health += damageScale * DamageAmount * FMax(1.0,MoreDamage);
			if (P.Health > Class'NWMutator'.default.MaxHealth)
				P.Health = Class'NWMutator'.default.MaxHealth;
		}
		else
		{
        	if( Victims != self )
        	{
         	   Victims.TakeDamage(
                	damageScale * DamageAmount,
                	Instigator, 
			Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dir,
			(damageScale * Momentum * dir),
			DamageName
			);
        	}
	}
		
	}
	else
	{
        if( Victims != self )
        {
            dir = Victims.Location - HitLocation;
            dist = FMax(1,VSize(dir));
            dir = dir/dist; 
            damageScale = 1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius);
            Victims.TakeDamage
            (
                damageScale * DamageAmount,
                Instigator, 
                Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dir,
                (damageScale * Momentum * dir),
                DamageName
            );
        }
	}
    }

    bHurtEntry = false;
}
//******************************************************************************************************************


defaultproperties
{
	DrawType=DT_Sprite
	Style=STY_Translucent
	Texture=IceBall
	LightBrightness=100
	LightHue=0
	LightSaturation=255
	LightRadius=6
	LightEffect=LE_NonIncidence
	LightType=LT_Steady
	bBounce=False
	RemoteRole=ROLE_SimulatedProxy
	Damage=40
	MaxSpeed=1400.000000
	MomentumTransfer=15000
	MyDamageType="Freezed"
	speed=500.000000
	DrawScale=0.800000
	bUnlit=True
	ScaleGlow=2.000000
	AmbientSound=Ice_loop
	SoundRadius=255
	SoundVolume=255
	bFixedRotationDir=True
	RotationRate=(Roll=30000)
}

