class HMissile expands Projectile;

#exec MESH IMPORT MESH=HMissile ANIVFILE=MODELS\XMissile_a.3d DATAFILE=MODELS\XMissile_d.3d X=0 Y=0 Z=0
#exec MESH LODPARAMS MESH=HMissile STRENGTH=0
#exec MESH ORIGIN MESH=HMissile X=0 Y=0 Z=0 YAW=128

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

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

#exec TEXTURE IMPORT NAME=Sk_Missile FILE=SKINS\Sk_Missile.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=HMissile NUM=1 TEXTURE=Sk_Missile

#exec MESHMAP SETTEXTURE MESHMAP=HMissile NUM=2 TEXTURE=Green_Over


var bool bRing,bHitWater,bWaterStart;
var HM_Trail trail;


var int Kickback;
var float Splasher, MoreDamage;
var bool HealthGiver;
var actor LTarget;
var bool bStartedMission;

var MBurner XBurnerTrail;


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


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

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

    Super.Destroyed();
}

simulated function PostBeginPlay()
{
	Trail = Spawn(Class'HM_Trail', Self);
	XBurnerTrail = Spawn(class'GreenBurner',self);

	SetTimer(0.1, False);
}

simulated function Timer()
{
local ut_SpriteSmokePuff b;
local OtherBubble d;
local vector X, Y, Z;

	if (!bStartedMission)
	{
		bStartedMission = True;
		
		if (LTarget != None)
			SetTimer(1.5, False);
		else
			SetTimer(0.2, True);

		return;
	}

	if (LTarget != None)
		ReleaseRockets();
	else
	{
		if (Region.Zone.bWaterZone)
		{
			GetAxes(Rotation, X, Y, Z);
			Y *= Rand(48) - 24;
			Z *= Rand(48) - 24;
			d = Spawn(class'OtherBubble',,, Location + Y + Z);
        		d.RemoteRole = ROLE_None;
		}
		else
		{
       			b = Spawn(class'ut_SpriteSmokePuff');
        		b.RemoteRole = ROLE_None;
		}
	}
}


function ReleaseRockets()
{
local int i;
local MiniHMissile MM;
local vector X, Y, Z, RefPoint;

	GetAxes(Rotation, X, Y, Z);

	Spawn(Class'GreenFlash');
	
	For (i=0; i<4; i++)
	{
		if (i == 0)
			RefPoint = 16*X + (16*Y) + (16*Z);
		else if (i == 1)
			RefPoint = 16*X + (-16*Y) + (16*Z);
		else if (i == 2)
			RefPoint = 16*X + (16*Y) + (-16*Z);
		else
			RefPoint = 16*X + (-16*Y) + (-16*Z);

		MM = SpawnRocket(rotator(RefPoint));
		MM.LTarget = LTarget;
		MM.Kickback = Kickback;
		MM.MoreDamage = MoreDamage;
		MM.HealthGiver = HealthGiver;
		MM.Splasher = Splasher;
	}

	Destroy();
}


function MiniHMissile SpawnRocket(rotator RocketRotation)
{
	return Spawn(Class'MiniHMissile',,,, RocketRotation);
}


auto state Flying
{

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

        bHitWater = True;
        if ( Level.NetMode != NM_DedicatedServer )
        {
            w = Spawn(class'WaterRing',,,,rot(16384,0,0));
            w.DrawScale = 0.2;
            w.RemoteRole = ROLE_None;
        }       
        Velocity=0.6*Velocity;
    }

    simulated function ProcessTouch (Actor Other, Vector HitLocation)
    {
        if ( (Other != instigator) && (!Other.IsA('Projectile') || Other.bProjTarget)) 
            Explode(HitLocation,Normal(HitLocation-Other.Location));
    }

function BlowUp(vector HitLocation)
{
	Spawn(Class'BigGreenFlare');
        
	MakeNoise(1.0);

	HurtRadiusX(Damage * FMax(1.0,MoreDamage), 220 * FMax(1.0, Splasher), MyDamageType, MomentumTransfer * (KickBack + 1), HitLocation);
}


    simulated function Explode(vector HitLocation, vector HitNormal)
    {
	local X_Green_CorpseOv Xover;

        BlowUp(HitLocation);

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

        Destroy();
    }

    function BeginState()
    {
        local vector Dir;

        Dir = vector(Rotation);
        Velocity = speed * Dir;
        Acceleration = Dir * 50;
        if (Region.Zone.bWaterZone)
        {
            bHitWater = True;
            Velocity=0.6*Velocity;
        }
    }
}


//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=HMissile
	ExplosionDecal=Class'HEnergyDecal'
	MyDamageType=MissileBlast
	speed=500.000000
	MaxSpeed=1200.000000
	Damage=120.000000
	MomentumTransfer=80000
	SpawnSound=Sound'UnrealShare.Eightball.Ignite'
	RemoteRole=ROLE_SimulatedProxy
	DrawScale=1.000000
	AmbientGlow=96
	bUnlit=True
	SoundRadius=128
	SoundVolume=255
	AmbientSound=AM_HM_Loop
	LightType=LT_Steady
	LightEffect=LE_NonIncidence
	LightBrightness=100
	LightHue=85
	LightSaturation=64
	LightRadius=6
	bBounce=True
	bNetTemporary=False
}
