class XMissile expands Projectile;

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

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

#exec MESHMAP NEW MESHMAP=XMissile MESH=XMissile
#exec MESHMAP SCALE MESHMAP=XMissile 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=XMissile NUM=1 TEXTURE=Sk_Missile

#exec MESHMAP SETTEXTURE MESHMAP=XMissile NUM=2 TEXTURE=XM_Special

#exec AUDIO IMPORT NAME="XM_Loop" FILE=SOUNDS\XM_Loop.wav GROUP="MultiML"


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


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

var MBurner XBurnerTrail;


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


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

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

    Super.Destroyed();
}

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

	SetTimer(0.05, True);
}

simulated function Timer()
{
local TM_Smoke ts;

	if (!bStartedMission)
	{
		bStartedMission = True;
		
		if (LTarget != None)
		{
			bRotateToDesired = True;
			bHaveTarget = True;
		}
		return;
	}

	if (LTarget != None)
	{
		DesiredRotation = rotator(LTarget.Location - Location);
	}

	ts = Spawn(Class'TM_Smoke');
	ts.RemoteRole = ROLE_None;
}


simulated function Tick( float DeltaTime)
{
local vector Dir;

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

		if (Pawn(LTarget) != None)
		{
			if (Pawn(LTarget).Health < 0 || Pawn(LTarget).bHidden)
				Explode(Location,vect(0,0,1));
		}
		else if (LTarget == None && bHaveTarget)
		{
			SpawnProjCopy();
			Destroy();
		}
	}
}

function SpawnProjCopy()
{
	Spawn(Class);
}

simulated function ShakePeople(float shaketime, float RollMag, float vertmag, float MaxRange)
{
local Pawn P;

	for ( P=Level.PawnList; P!=None; P=P.NextPawn )
	{
			if ( P.IsA('PlayerPawn') && (VSize(P.Location - Location) < MaxRange) )
				PlayerPawn(P).ShakeView(shaketime, 
		(FMax(0.1, MaxRange - VSize(Location - P.Location))) * RollMag / MaxRange, 
		(FMax(0.1, MaxRange - VSize(Location - P.Location))) * vertmag / MaxRange);
	}
}


function SpawnShock(vector HitNormal)
{
local vector X, Y, Z;
local BigShockwave b;

	GetAxes(rotator(HitNormal), X, Y, Z);
	b = Spawn(Class'BigShockwave',,,, rotator(HitNormal));
	GiveSpecials(b);
	Spawn(Class'RingTypeA',,, Location + 32*X, rotator(HitNormal));
	Spawn(Class'RingTypeB',,, Location + 600*X, rotator(HitNormal));
}

simulated function GiveSpecials(BigShockwave b)
{
	b.Kickback = Kickback;
	b.Splasher = Splasher;
	b.MoreDamage = MoreDamage;
}

singular function TakeDamage(int NDamage, Pawn instigatedBy, Vector hitlocation, vector momentum, name damageType )
{
	if (NDamage > 0)
		Explode(HitLocation,vect(0,0,1));
}


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,vect(0,0,1));
    }

function BlowUp(vector HitLocation)
{
local Pawn P;
	
	MakeNoise(3.0);
	HurtRadius(Damage * FMax(1.0,MoreDamage), 250 * FMax(1.0, Splasher), MyDamageType, MomentumTransfer * (KickBack + 1), HitLocation);
}


    simulated function Explode(vector HitLocation, vector HitNormal)
    {
	local vector X, Y, Z;
	GetAxes(rotator(HitNormal), X, Y, Z);

        BlowUp(HitLocation);
	Spawn(Class'X_NukeCoronaA',,, Location + 100*X);
	ShakePeople(0.5, 40, 200, 4000);

	SpawnShock(HitNormal);

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


defaultproperties
{
	DrawType=DT_Mesh
	Mesh=XMissile
	ExplosionDecal=Class'XMissileDecal'
	MyDamageType=NuclearExplosion
	speed=900.000000
	MaxSpeed=2000.000000
	Damage=1000.000000
	MomentumTransfer=200000
	SpawnSound=Sound'UnrealShare.Eightball.Ignite'
	RemoteRole=ROLE_SimulatedProxy
	DrawScale=1.000000
	AmbientGlow=96
	bUnlit=True
	SoundRadius=128
	SoundVolume=255
	AmbientSound=XM_Loop
	LightType=LT_Steady
	LightEffect=LE_NonIncidence
	LightBrightness=100
	LightHue=191
	LightSaturation=13
	LightRadius=6
	bBounce=False
	RotationRate=(Yaw=28000,Pitch=28000,Roll=28000)
	bProjTarget=True
	bNetTemporary=False
}
