#
# Terran Sub-Launched Cruise Missile
#
local TMissileCruiseSubProjectile = import('/lua/terranprojectiles.lua').TMissileCruiseSubProjectile

TIFMissileCruise02 = Class(TMissileCruiseSubProjectile) {

	FxAirUnitHitScale = 1,
    FxLandHitScale = 1,
    FxNoneHitScale = 1,
    FxPropHitScale = 1,
    FxProjectileHitScale = 1,
    FxProjectileUnderWaterHitScale = 1,
    FxShieldHitScale = 1,
    FxUnderWaterHitScale = 1,
    FxUnitHitScale = 1,
    FxWaterHitScale = 1,
    FxOnKilledScale = 1,

    OnCreate = function(self)
        TMissileCruiseSubProjectile.OnCreate(self)
        self:SetCollisionShape('Sphere', 0, 0, 0, 2.0)
        self.MovementTurnLevel = 1
        self:ForkThread( self.MovementThread )
    end,

    MovementThread = function(self)        
        self.WaitTime = 0.1
        self:SetTurnRate(8)
        WaitSeconds(0.2)        
        while not self:BeenDestroyed() do
            self:SetTurnRateByDist()
            WaitSeconds(self.WaitTime)
        end
    end,

    SetTurnRateByDist = function(self)
        local dist = self:GetDistanceToTarget()
        #Get the nuke as close to 90 deg as possible
        if dist > 50 then        
            #Freeze the turn rate as to prevent steep angles at long distance targets
            WaitSeconds(0.2)
            self:SetTurnRate(80)
        elseif dist > 64 and dist <= 107 then
						# Increase check intervals
						self:SetTurnRate(80)
						WaitSeconds(1.5)
            self:SetTurnRate(90)
        elseif dist > 21 and dist <= 53 then
						# Further increase check intervals
            WaitSeconds(0.2)
            self:SetTurnRate(95)
				elseif dist > 0 and dist <= 21 then
						# Further increase check intervals            
            self:SetTurnRate(100)   
            KillThread(self.MoveThread)         
        end
    end,        

    GetDistanceToTarget = function(self)
        local tpos = self:GetCurrentTargetPosition()
        local mpos = self:GetPosition()
        local dist = VDist2(mpos[1], mpos[3], tpos[1], tpos[3])
        return dist
    end,
    
    OnExitWater = function(self)
        TMissileCruiseSubProjectile.OnExitWater(self)
        self:SetDestroyOnWater(true)
    end,
}

TypeClass = TIFMissileCruise02

