class CyBotYDown expands CyBotGuns;

#exec MESH IMPORT MESH=CyBotYDown ANIVFILE=MODELS\CyBotYDown_a.3d DATAFILE=MODELS\CyBotYDown_d.3d X=0 Y=0 Z=0
#exec MESH LODPARAMS MESH=CyBotYDown STRENGTH=0
#exec MESH ORIGIN MESH=CyBotYDown X=0 Y=0 Z=0 ROLL=128

#exec MESH SEQUENCE MESH=CyBotYDown SEQ=All STARTFRAME=0 NUMFRAMES=10
#exec MESH SEQUENCE MESH=CyBotYDown SEQ=Closed STARTFRAME=0 NUMFRAMES=1
#exec MESH SEQUENCE MESH=CyBotYDown SEQ=Openned STARTFRAME=7 NUMFRAMES=1
#exec MESH SEQUENCE MESH=CyBotYDown SEQ=Openning STARTFRAME=0 NUMFRAMES=8 RATE=5.0
#exec MESH SEQUENCE MESH=CyBotYDown SEQ=Fire STARTFRAME=7 NUMFRAMES=3 RATE=6.0

#exec MESHMAP NEW MESHMAP=CyBotYDown MESH=CyBotYDown
#exec MESHMAP SCALE MESHMAP=CyBotYDown X=0.0375 Y=0.0375 Z=0.075

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

#exec TEXTURE IMPORT NAME=Sk_CybotY FILE=SKINS\Sk_CybotY.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=CyBotYDown NUM=2 TEXTURE=Sk_CybotY

#exec TEXTURE IMPORT NAME=CyBase FILE=SKINS\CyBase.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=CyBotYDown NUM=3 TEXTURE=CyBase

#exec TEXTURE IMPORT NAME=TranslucentInvisible FILE=SKINS\TranslucentInvisible.bmp GROUP=Skins FLAGS=2 LODSET=2
#exec MESHMAP SETTEXTURE MESHMAP=CyBotYDown NUM=4 TEXTURE=TranslucentInvisible

var CyBotYDownOver CyOvY;
var CyBotYUp Heli;

var() int Health;
var() bool bEMPWeak;
var() float DodgeSpeed, ProjDetectionTime, ProjDetectRadius, MaxDodgeDist;

var float DetectionCount;

var vector DodgeLocation;

replication
{
	reliable if (Role == ROLE_Authority)
		DodgeLocation;
}

simulated function PostBeginPlay()
{
	Super.PostBeginPlay();

	PlaySound(Sound'Cybot_Openning',,12.0,,2400);

	DodgeLocation = Location;

	if (Class'NWMutator'.default.bCybotID_InTeamGames && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt'))
		MultiSkins[1] = TeamNano[Instigator.PlayerReplicationInfo.Team];

	SpawnHeliTrailer();
}

singular function TakeDamage( int NDamage, Pawn instigatedBy, Vector hitlocation, vector momentum, name damageType )
{
	if ((instigatedBy.PlayerReplicationInfo.Team == Instigator.PlayerReplicationInfo.Team && instigatedBy!=Instigator) && 
	Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt'))
		return;

	if (bEMPWeak && damageType == 'Electrified')
		NDamage = 3 * NDamage;

		Health -= NDamage;

	If (Health <= 0)
	{
		SpawnWreckedPiece(momentum);
		
		if (Heli != None)
			Heli.SpawnWreckedPiece(momentum);

		Spawn(Class'UT_SuperRing2',,,, rot(16384,0,0));
		Spawn(Class'CybotWreckExplosion');
	}
}


function SpawnHeliTrailer()
{
	Heli = Spawn(Class'CyBotYUp', Self);
	Heli.DrawScale = DrawScale;
}


simulated function AnimEnd()
{
	if (!bReady)
	{
		SpawnOv();
		bFixedRotationDir = True;
		bRotateToDesired = True;
		bReady = True;
	}
}

function SpawnOv()
{
	CyOvY = Spawn(Class'CyBotYDownOver', Self);

	if (Class'NWMutator'.default.bCybotID_InTeamGames && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt'))
		CyOvY.MultiSkins[1] = TeamFX[Instigator.PlayerReplicationInfo.Team];

	if (Class'NWMutator'.default.bCybotID_InTeamGames && Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('MonsterHunt'))
		MultiSkins[4] = TeamFX[Instigator.PlayerReplicationInfo.Team];
	else
		MultiSkins[4] = Texture'N_CybotFX';
}


function SpawnWreckedPiece( vector SpawnDir)
{
local WreckedPiece wp;

	wp = Spawn(Class'WreckedPiece',,, Location, rotator(vect(0,0,-20) + SpawnDir.X * vect(1,0,0) + SpawnDir.Y * vect(0,1,0)));
	GiveProperties(wp);
}

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

	Super.Destroyed();
}

simulated function Tick(float DeltaTime)
{
local Projectile Proj, CloserProj;

	Super.Tick(DeltaTime);

	if (bReady)
	{
		DetectionCount += DeltaTime;
		
		if (DetectionCount >= ProjDetectionTime)
		{
			DetectionCount = 0;

			ForEach VisibleCollidingActors(Class'Projectile', Proj, FMax(ProjDetectRadius, 10))
			{
				if (!proj.IsA('ZY_Proj') && CloserProj == None)
					CloserProj = Proj;
				else if (!proj.IsA('ZY_Proj') && CloserProj != None)
				{
					if (Vsize(Location - Proj.Location) < VSize(Location - CloserProj.Location))
						CloserProj = Proj;
				}
			}

			if (CloserProj != None)
				DodgeProjectile(CloserProj);
		}

		if (VSize(DodgeLocation - Location) > 12.0)
			Velocity = DodgeSpeed * Normal(DodgeLocation - Location);
		else
			Velocity = vect(0,0,0);
	}
}


function DodgeProjectile( projectile ProjToAvoid)
{
local vector QuadV, DodgeDir;
local byte RndDir, i;
local vector HitLocation, HitNormal, EndTrace;
local actor A;

	QuadV = ProjToAvoid.Velocity;

	If (Abs(QuadV.X) >= Abs(QuadV.Y) && Abs(QuadV.X) >= Abs(QuadV.Z))
		RndDir = 1;
	else if (Abs(QuadV.Y) >= Abs(QuadV.X) && Abs(QuadV.Y) >= Abs(QuadV.Z))
		RndDir = 2;
	else if (Abs(QuadV.Z) >= Abs(QuadV.X) && Abs(QuadV.Z) >= Abs(QuadV.Y))
		RndDir = 3;

	For (i=0; i<3; i++)
	{

		if (RndDir == 1)
		{
			DodgeDir.Y = FRand()*2 - 1;
			DodgeDir.Z = FRand()*2 - 1;
		}
		else if (RndDir == 2)
		{
			DodgeDir.X = FRand()*2 - 1;
			DodgeDir.Z = FRand()*2 - 1;
		}
		else if (RndDir == 3)
		{
			DodgeDir.X = FRand()*2 - 1;
			DodgeDir.Y = FRand()*2 - 1;
		}

		EndTrace = Location + DodgeDir*MaxDodgeDist - (CollisionHeight + 1)*DodgeDir;
		A = Trace( HitLocation, HitNormal, EndTrace, Location, True);

		if (A == None)
		{
			DodgeLocation = EndTrace;
			break;
		}
	}
}


defaultproperties
{
	DrawType=DT_Mesh
	Mesh=CyBotYDown
	FireRate=2.000000
	SensorRadius=2048.000000
	CybotProj=Class'ZY_ProjB'
	ZPositive=False
	FireSound=Cybot_ZYFire
	Health=200
	bEMPWeak=True
	CollisionHeight=10.000000
	CollisionRadius=10.000000
	bCollideWorld=True
	bBlockActors=True
	bBlockPlayers=True
	bCollideActors=True
	bProjTarget=True
	DodgeSpeed=400.000000
	ProjDetectionTime=0.150000
	ProjDetectRadius=190.000000
	MaxDodgeDist=150.000000
}
