----------------------------------------------------------------------------- 
--  File     : /units/prototype/prototype_script.lua 
-- 
--  Author(s): Resin Smoker
-- 
--  Summary  : Seraphim "Finger of Death" point defense
-- 
--  Copyright © 2011 4DC_V0.8  All rights reserved.
-----------------------------------------------------------------------------

local SStructureUnit = import('/lua/seraphimunits.lua').SStructureUnit
local SDFUltraChromaticBeamGenerator = import('/lua/seraphimweapons.lua').SDFUltraChromaticBeamGenerator02
local EffectTemplate = import('/lua/EffectTemplates.lua')
local WeaponFile = import('/lua/sim/DefaultWeapons.lua')
local DefaultBeamWeapon = WeaponFile.DefaultBeamWeapon

prototype = Class(SStructureUnit) {

    Weapons = {
        FingerOfDeath = Class(SDFUltraChromaticBeamGenerator) {
            FxBeamStartPoint = nil,
            
            PlayFxBeamStart = function(self, muzzle)
                if not self.unit:IsDead() then 
                    self.TargetSync(self.unit.MyWep:GetCurrentTarget())             
                    self.unit.BeamActive = true
                    self.unit:SetMaintenanceConsumptionActive()
                    self.unit:AddBeamFX() 
                    SDFUltraChromaticBeamGenerator.PlayFxBeamStart(self, muzzle)                                       
                end                                                                
            end,                           
                                                
            PlayFxWeaponUnpackSequence = function(self)          
                if not self.unit:IsDead() then  
                    self.TargetSync(self.unit.MyWep:GetCurrentTarget())                        
                    DefaultBeamWeapon.PlayFxWeaponUnpackSequence(self)              
                end 
            end, 
            
            TargetSync = function(self, target)
                if target then
                    for i = 1, self.unit:GetWeaponCount() do
                        WARN(i)                            
                        local wep = self.unit:GetWeapon(i)
                        wep:SetTargetEntity(target)                          
                    end 
                end 
            end,                
                                               
            PlayFxWeaponPackSequence = function(self)
                if not self.unit:IsDead() then                  
                    DefaultBeamWeapon.PlayFxWeaponPackSequence(self)
                    for i = 1, self.unit:GetWeaponCount() do                       
                        local wep = self.unit:GetWeapon(i)
                        wep:SetTurretYawSpeed(wep:GetBlueprint().TurretYawSpeed)
                        wep:SetTurretPitchSpeed(wep:GetBlueprint().TurretPitchSpeed) 
                    end                       
                end              
            end,                                                                      
                                                                                    
            PlayFxBeamEnd = function(self, beam)   
                self.unit:SetMaintenanceConsumptionInactive()
                self.unit:RemoveBeamFX()                       
                if not self:GetCurrentTarget() then
                    for i = 1, self.unit:GetWeaponCount() do                       
                        local wep = self.unit:GetWeapon(i)
                        wep:SetTurretYawSpeed(wep:GetBlueprint().TurretYawSpeed * 3)
                        wep:SetTurretPitchSpeed(wep:GetBlueprint().TurretPitchSpeed * 3) 
                    end
                else
                    self.TargetSync(self.unit.MyWep:GetCurrentTarget()) 
                end
                SDFUltraChromaticBeamGenerator.PlayFxBeamEnd(self, beam)                                   
            end,                 
        }        
    },
    
    OnStopBeingBuilt = function(self,builder,layer) 
        SStructureUnit.OnStopBeingBuilt(self,builder,layer) 
         -- Global Variables 
        self.MyWep = self:GetWeaponByLabel('FingerOfDeath') 
         -- Global Booleans     
        self.FxStatus = false
        self.BeamActive = false
         -- Kill the beam if its on without a target 
        self:ForkThread(self.BeamChecker)                                                      
    end,  
    
    BeamChecker = function(self)                
        while not self:IsDead() do 
            if not self.MyWep:GetCurrentTarget() then
                if self.BeamActive then
                    self.BeamActive = false
                    self:ForkThread(self.BeamStop) 
                    self:ForkThread(self.RemoveBeamFX)
                end
            end 
            WaitSeconds(1)            
        end
    end,
    
    AddBeamFX = function(self)
        if not self.FxStatus then
         -- Add small Beam FX
            self.FxStatus = true
            self.BeamFxBag = {} 
            local army = self:GetArmy()
            for k, v in EffectTemplate.SUltraChromaticBeamGeneratorBeam do                                                                           
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'base', self, 'seg01', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg01', self, 'seg02', army, v) )                   
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg02', self, 'seg03', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg03', self, 'seg04', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg04', self, 'seg05', army, v) )                  
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg05', self, 'seg06', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg06', self, 'seg07', army, v) )            
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg07', self, 'seg08', army, v) )                   
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg08', self, 'seg09', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg09', self, 'seg10', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg10', self, 'seg11', army, v) )                  
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg11', self, 'seg12', army, v) )
                table.insert(self.BeamFxBag, AttachBeamEntityToEntity(self, 'seg12', self, 'weapon', army, v) )                                                        
            end                                              
         -- Clean up 
            for k, v in self.BeamFxBag do  
                self.Trash:Add(v)
            end                      
        end
    end,     
    
    RemoveBeamFX = function(self)
        if self.FxStatus then
            self.FxStatus = false
         -- Remove large beam FX
            if self.BeamFxBag then            
                for k, v in self.BeamFxBag do
                    v:Destroy()
                end
                self.BeamEffectsBag = {}    
            end                         
        end
    end,
    
    DestructionPartsHighToss = {'weapon'},       
    DestructionPartsLowToss = {'seg01','seg02','seg03','seg04','seg05','seg06','seg07','seg08','seg09','seg10','seg11','seg12'},    
    DeathThreadDestructionWaitTime = 10,     
        
    OnKilled = function(self, instigator, type, overkillRatio)
        self:ForkThread(self.BeamStop)
        self:ForkThread(self.RemoveBeamFX)
        for k, v in self.DestructionPartsHighToss do
            if self:IsValidBone(v) then
                self:HideBone(v, true)
            end
        end
        for k, v in self.DestructionPartsLowToss do
            if self:IsValidBone(v) then
                self:HideBone(v, true)
            end
        end        
        SStructureUnit.OnKilled(self, instigator, type, overkillRatio)           
    end,   
        
    BeamStop = function(self)
        self:SetMaintenanceConsumptionInactive()
        for k, v in self.MyWep.Beams do
            v.Beam:Disable() 
        end            
        if self and not self:BeenDestroyed() and self.MyWep then
            local bp = self.MyWep:GetBlueprint()
            if bp.Audio.BeamStop then
                self.MyWep:PlaySound(bp.Audio.BeamStop)
            end
            if bp.Audio.BeamLoop and self.MyWep.Beams[1].Beam then
                self.MyWep.Beams[1].Beam:SetAmbientSound(nil, nil)
            end
        end     
    end,    
    
}
TypeClass = prototype