class WREGrenade expands Projectile;

#exec MESH IMPORT MESH=WREGrenade ANIVFILE=MODELS\WREGrenade_a.3d DATAFILE=MODELS\WREGrenade_d.3d X=0 Y=0 Z=0
#exec MESH LODPARAMS MESH=WREGrenade STRENGTH=0
#exec MESH ORIGIN MESH=WREGrenade X=0 Y=0 Z=0

#exec MESH SEQUENCE MESH=WREGrenade SEQ=All STARTFRAME=0 NUMFRAMES=1
#exec MESH SEQUENCE MESH=WREGrenade SEQ=Still STARTFRAME=0 NUMFRAMES=1

#exec MESHMAP NEW MESHMAP=WREGrenade MESH=WREGrenade
#exec MESHMAP SCALE MESHMAP=WREGrenade X=0.05 Y=0.05 Z=0.1


#exec TEXTURE IMPORT NAME=Sk_WREGrenade FILE=Skins\Sk_WREGrenade.bmp GROUP=Skins FLAGS=2
#exec MESHMAP SETTEXTURE MESHMAP=WREGrenade NUM=1 TEXTURE=Sk_WREGrenade

#exec OBJ LOAD FILE=Textures\NaliEffects.utx PACKAGE=NaliWeaponsII.GFX
#exec MESHMAP SETTEXTURE MESHMAP=WREGrenade NUM=2 TEXTURE=BlueLiquix

#exec AUDIO IMPORT NAME="MetalHit" FILE=SOUNDS\MetalHit.wav GROUP="Hits"


var bool bCanHitOwner, bHitWater;

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


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

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

	Super.PostBeginPlay();

	SetTimer(2.5+FRand()*0.5,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);	
		bCanHitOwner = False;
		if (Instigator.HeadRegion.Zone.bWaterZone)
		{
			bHitWater = True;
			Disable('Tick');
			Velocity=0.6*Velocity;			
		}
	}	
}

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

simulated function Timer()
{
	Explosion(Location+Vect(0,0,1)*16);
}

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

simulated function ProcessTouch( actor Other, vector HitLocation )
{
	if ( (Other!=instigator) || bCanHitOwner )
		Explosion(HitLocation);
}

simulated function HitWall( vector HitNormal, actor Wall )
{
	bCanHitOwner = True;
	Velocity = 0.75*(( Velocity dot HitNormal ) * HitNormal * (-2.0) + Velocity);   // Reflect off Wall w/damping
	RandSpin(100000);
	speed = VSize(Velocity);
	if ( Level.NetMode != NM_DedicatedServer )
		PlaySound(ImpactSound, SLOT_Misc, 8.0);
	if ( Velocity.Z > 400 )
		Velocity.Z = 0.5 * (400 + Velocity.Z);	
	else if ( speed < 20 ) 
	{
		bBounce = False;
		SetPhysics(PHYS_None);
	}
}

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

	MakeNoise(1.0);
}

simulated function Explosion(vector HitLocation)
{
	local WREGrenadeExplosion s;
	local WRECorona r;
	local X_Ion_CorpseOv Xover;

	BlowUp(HitLocation);
	if ( Level.NetMode != NM_DedicatedServer )
	{
		spawn(ExplosionDecal,,,,rot(16384,0,0));
	}
  		s = spawn(class'WREGrenadeExplosion',,,HitLocation);
		s.RemoteRole = ROLE_None;
		r = spawn(class'WRECorona',,,HitLocation);
		r.RemoteRole = ROLE_None;

	Xover = Spawn(class'X_Ion_CorpseOv',,, HitLocation);
	Xover.EffectRadius = 200 * FMax(1.0, Splasher);

 	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_Mesh
	Mesh=WREGrenade
	LightBrightness=100
	LightHue=191
	LightSaturation=13
	LightRadius=6
	LightEffect=LE_NonIncidence
	LightType=LT_Steady
	bBounce=True
	bFixedRotationDir=True
	Physics=PHYS_Falling
	RemoteRole=ROLE_SimulatedProxy
	Damage=45.000000
	ExplosionDecal=IonDecal
	ImpactSound=MetalHit
	MaxSpeed=1000.000000
	MomentumTransfer=50000
	MyDamageType="Ionized"
	speed=600.000000
}
