class VulcanRock expands Projectile;

#exec TEXTURE IMPORT NAME=VRock FILE=SKINS\VRock.bmp GROUP=Skins FLAGS=2 LODSET=2

var bool bCanHitOwner, bHitWater;

var int Kickback;
var float MoreDamage;
var bool HealthGiver;
var LavaOv lo;


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

simulated function PostBeginPlay()
{
local vector X,Y,Z;
local rotator RandRot;

	Super.PostBeginPlay();

	if (!Region.Zone.bWaterZone)
		lo = Spawn(Class'LavaOv', Self);

	SetTimer(2.0, false);

	if ( Role == ROLE_Authority )
	{
		GetAxes(Instigator.ViewRotation,X,Y,Z);	
		Velocity = X * (Instigator.Velocity Dot X)*0.4 + Vector(Rotation) * (Speed +
			FRand() * 100);
		Velocity.z += 210;
		MaxSpeed = 1000;
		RandSpin(50000);
		if (Instigator.HeadRegion.Zone.bWaterZone)
		{
			bHitWater = True;
			Disable('Tick');
			Velocity=0.6*Velocity;			
		}
	}
}

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

	Super.Destroyed();
}

simulated function ZoneChange( Zoneinfo NewZone )
{
	local waterring w;
	
	if (!NewZone.bWaterZone || bHitWater) Return;

	bHitWater = True;
		w = Spawn(class'WaterRing',,,,rot(16384,0,0));
		w.DrawScale = 0.2;
		w.RemoteRole = ROLE_None;
	Velocity=0.6*Velocity;

	if (lo != None)
		lo.Destroy();
}


simulated function Timer()
{
	bCanHitOwner = True;
}


simulated function Landed( vector HitNormal )
{
	HitWall( HitNormal, None );
}

simulated function ProcessTouch( actor Other, vector HitLocation )
{
	if ( (Other!=instigator) || bCanHitOwner )
		RockExplosion(HitLocation, -Normal(Velocity), Other);
}

simulated function HitWall( vector HitNormal, actor Wall )
{
	if ( Level.NetMode != NM_DedicatedServer )
		Spawn(ExplosionDecal,,,,rotator(HitNormal));

	RockExplosion(Location, HitNormal, Wall);
}

///////////////////////////////////////////////////////
simulated function RockExplosion(vector HitLocation, vector HitNormal, actor Other)
{
	SpawnRocks(HitLocation, HitNormal, Other);
 	Destroy();
}

function SpawnRocks( vector HitLocation, vector HitNormal, actor Other)
{
local LittleVulcanRock RW;
local int i;
local rotator R;
local vector Dir;
local vector Vel2D, Norm2D;


	if ((HealthGiver && Other != Instigator) || !HealthGiver)
	Other.TakeDamage(Damage * FMax(1.0,MoreDamage), Instigator, HitLocation, MomentumTransfer * vector(Rotation) * FMin(KickBack * 1000, 1.0), MyDamageType);
		
	if (Other.IsA('Pawn') && HealthGiver)
	{
		If (((Pawn(Other).PlayerReplicationInfo.Team == Instigator.PlayerReplicationInfo.Team && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt')) ||
		(Level.Game.IsA('MonsterHunt') && !Pawn(Other).IsA('ScriptedPawn')) || Pawn(Other) == Instigator)  && Pawn(Other).Health < Class'NWMutator'.default.MaxHealth)
		{
			Pawn(Other).Health += Damage * FMax(1.0, MoreDamage) * DrawScale;
			if (Pawn(Other).Health > Class'NWMutator'.default.MaxHealth)
					Pawn(Other).Health = Class'NWMutator'.default.MaxHealth;
		}
	}


	Vel2D = Velocity;
	Vel2D.Z = 0;
	Norm2D = HitNormal;
	Norm2D.Z = 0;
	Norm2D = Normal(Norm2D);
	Vel2D = Normal(Vel2D);
	if ( (Vel2D Dot Norm2D) < -0.999 )
	{
		HitNormal = Normal(HitNormal + 0.6 * Vel2D);
		Norm2D = HitNormal;
		Norm2D.Z = 0;
		Norm2D = Normal(Norm2D);
		if ( (Vel2D Dot Norm2D) < -0.999 )
		{
			if ( Rand(1) == 0 )
				HitNormal = HitNormal + vect(0.05,0,0);
			else
				HitNormal = HitNormal - vect(0.05,0,0);
			if ( Rand(1) == 0 )
				HitNormal = HitNormal + vect(0,0.05,0);
			else
				HitNormal = HitNormal - vect(0,0.05,0);
			HitNormal = Normal(HitNormal);
		}
	}

	For (i=0; i<6; i++)
	{
		RW = Spawn(Class'LittleVulcanRock',,, HitLocation, rotator(HitNormal) + (Rand(16000) - 8000) * rot(1,1,1));

		RW.Velocity = 0.6*(( Velocity dot vector(RW.Rotation) ) * vector(RW.Rotation) * 2.0);
		RW.RandSpin(100000);
		RW.speed = VSize(RW.Velocity);
		RW.Kickback = Kickback;
		RW.MoreDamage = MoreDamage;
		RW.HealthGiver = HealthGiver;
	}

	Spawn(Class'RockHit');

	MakeNoise(1.0);
}


defaultproperties
{
	DrawType=DT_Mesh
	Mesh=LodMesh'UnrealI.TBoulder'
	bBounce=True
	bFixedRotationDir=True
	Physics=PHYS_Falling
	RemoteRole=ROLE_SimulatedProxy
	Damage=60.000000
	ExplosionDecal=Class'VulcanRockDecal'
	ImpactSound=Sound'UnrealI.Titan.Rockhit'
	MaxSpeed=1000.000000
	MomentumTransfer=200000
	MyDamageType=Rock
	speed=800.000000
	DrawScale=3.000000
	CollisionHeight=6.000000
	CollisionRadius=6.000000
	MultiSkins(1)=VRock
}
