class MegaIce expands Decoration;

#exec MESH IMPORT MESH=MegaIce ANIVFILE=MODELS\MegaIce_a.3d DATAFILE=MODELS\MegaIce_d.3d X=0 Y=0 Z=0
#exec MESH LODPARAMS MESH=MegaIce STRENGTH=0
#exec MESH ORIGIN MESH=MegaIce X=0 Y=0 Z=0

#exec MESH SEQUENCE MESH=MegaIce SEQ=All STARTFRAME=0 NUMFRAMES=17
#exec MESH SEQUENCE MESH=MegaIce SEQ=Still STARTFRAME=0 NUMFRAMES=1
#exec MESH SEQUENCE MESH=MegaIce SEQ=Grow STARTFRAME=1 NUMFRAMES=16 RATE=8.0

#exec MESHMAP NEW MESHMAP=MegaIce MESH=MegaIce
#exec MESHMAP SCALE MESHMAP=MegaIce X=0.1 Y=0.08 Z=0.16

#exec AUDIO IMPORT NAME="WaterFreeze" FILE=SOUNDS\WaterFreeze.wav GROUP="Freezer"

var bool bFirstPlayed, bColSet;

var float MoreDamage;
var bool HealthGiver;
var() int IceHealth;
var() bool bDestructable;

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


function Bump( actor Other )
{
    local float speed, oldZ;
    if( bPushable && (Pawn(Other)!=None) && (Other.Mass > 40) && Level.NetMode == NM_StandAlone)
    {
        bBobbing = false;
        oldZ = Velocity.Z;
        speed = VSize(Other.Velocity);
        Velocity = Other.Velocity * FMin(120.0, 20 + speed)/speed;
        if ( Physics == PHYS_None ) {
            Velocity.Z = 25;
            if (!bPushSoundPlaying) PlaySound(PushSound, SLOT_Misc,0.25);
            bPushSoundPlaying = True;          
        }
        else
            Velocity.Z = oldZ;
        SetPhysics(PHYS_Falling);
        Instigator = Pawn(Other);
    }
}

simulated function PostBeginPlay()
{
	PlayAnim('Grow', 6.0);
	DesiredRotation = Rand(65535) * rot(0,0,1) + rot(-16384,0,0);
	PlaySound(Sound'WaterFreeze',, 30.0);
	SetTimer(0.1, True);
}

simulated function Timer()
{
local Actor A;
local vector X, Y, Z, HitLocation, HitNormal;
local bool bSomething;

	if (!bFirstPlayed)
	{
		bFirstPlayed = True;
		
		//Trace and damage functions
		GetAxes(Rotation, X, Y, Z);

		if (Role == ROLE_Authority)
		{
		ForEach TraceActors(Class'Actor', A, HitLocation, HitNormal, Location + 80*DrawScale*X, Location - 64*DrawScale*X)
		{
			if (A != Instigator)
				A.TakeDamage( 80 * FMax(1.0, MoreDamage), Instigator, HitLocation, vect(0,0,0), 'Spiked');

			if (A.IsA('Pawn') && HealthGiver)
			{
				If (((Pawn(A).PlayerReplicationInfo.Team == Instigator.PlayerReplicationInfo.Team && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt')) ||
				(Level.Game.IsA('MonsterHunt') && !Pawn(A).IsA('ScriptedPawn')) || Pawn(A) == Instigator)  && Pawn(A).Health < Class'NWMutator'.default.MaxHealth)
				{
					Pawn(A).Health += 80 * FMax(1.0, MoreDamage);
					if (Pawn(A).Health > Class'NWMutator'.default.MaxHealth)
						Pawn(A).Health = Class'NWMutator'.default.MaxHealth;
				}
			}
		}
		}
	}
	else if( Normalize(Rotation).Pitch > -17000 && Normalize(Rotation).Pitch < -15000 && !bColSet)
	{
		bColSet = True;
		SetCollision( True, True, True);
		bCollideWorld = True;
		bProjTarget = True;
		SetTimer(0.3, True);
	}
	
	if (bColSet && Level.NetMode == NM_StandAlone)
	{
		ForEach VisibleCollidingActors(Class'Actor', A, CollisionRadius, Location + (CollisionHeight + CollisionRadius) * vect(0,0,1))
		{
			If (A != Self)
			{
				bSomething = True;
				Mass = Default.Mass + A.Mass;
			}
		}

		if (!bSomething)
			Mass = Default.Mass;
	}
	else if(Level.NetMode != NM_StandAlone)
	{
		SetPhysics(PHYS_None);
		RemoteRole = ROLE_DumbProxy;
	}

	if (Region.Zone.DamageType == 'Burned')
		Destroy();
}

simulated function Tick( float DeltaTime)
{
	if (bColSet && LifeSpan <= 1)
	{
		RemoteRole = Role_SimulatedProxy;
		DrawScale = LifeSpan * Default.DrawScale;
		ScaleGlow = LifeSpan * Default.ScaleGlow;
	}
}


function TakeDamage( int NDamage, Pawn instigatedBy, Vector hitlocation, vector momentum, name damageType )
{
	if (DamageType != 'Freezed' && DamageType != 'Spiked')
		IceHealth -= NDamage;

	If ((IceHealth <= 0 || DamageType == 'Burned') && bDestructable)
	{
		ReleaseFragEffect(class'FragmentIce', Texture'Ice', Momentum, DrawScale * 2.0, 12);
		PlaySound(Sound'IceHit',, 30.0);

		Instigator = InstigatedBy;

		if (Instigator != None)
			MakeNoise(1.0);
	}
}

simulated function ReleaseFragEffect(class<fragment> FragType, texture FragSkin, vector Momentum, float DSize, int NumFrags) 
{
	local int i;
	local actor A, Toucher;
	local Fragment s;

	for (i=0 ; i<NumFrags ; i++) 
	{
		s = Spawn( FragType, Owner);
		s.CalcVelocity(Momentum/100,0);
		s.Skin = FragSkin;
		s.Texture = FragSkin;
		s.DrawScale = DSize*0.5+0.7*DSize*FRand();
		s.Style = STY_Translucent;
		s.bMeshEnviroMap = True;
	}

	Destroy();
}


defaultproperties
{
	DrawType=DT_Mesh
	Mesh=MegaIce
	RemoteRole=ROLE_SimulatedProxy
	Style=STY_Translucent
	bMeshEnviroMap=True
	Texture=Ice
	ScaleGlow=2.000000
	CollisionHeight=190.000000
	CollisionRadius=45.000000
	bCollideActors=True
	bProjTarget=True
	DrawScale=3.000000
	bNetTemporary=False
	LifeSpan=20.000000
	bRotateToDesired=True
	Mass=150.000000
	Buoyancy=250.000000
	bStatic=False
	bStasis=False
	Physics=PHYS_Falling
	IceHealth=80
	RotationRate=(Pitch=2000,Yaw=2000,Roll=2000)
	bPushable=True
	bDestructable=True
}
