#****************************************************************************
#**
#**  File     :  /cdimage/units/XSL0001/XSL0001_script.lua
#**  Author(s):  Drew Staltman, Jessica St. Croix, Gordon Duclos
#**
#**  Summary  :  Seraphim Commander Script
#**
#**  Copyright © 2007 Gas Powered Games, Inc.  All rights reserved.
#****************************************************************************

local SWalkingLandUnit = import('/lua/seraphimunits.lua').SWalkingLandUnit
local Buff = import('/lua/sim/Buff.lua')

local SWeapons = import('/lua/seraphimweapons.lua')
local SDFChronotronCannonWeapon = SWeapons.SDFChronotronCannonWeapon
local SDFChronotronOverChargeCannonWeapon = SWeapons.SDFChronotronCannonOverChargeWeapon
local SIFCommanderDeathWeapon = SWeapons.SIFCommanderDeathWeapon
local EffectTemplate = import('/lua/EffectTemplates.lua')
local EffectUtil = import('/lua/EffectUtilities.lua')
local SIFLaanseTacticalMissileLauncher = SWeapons.SIFLaanseTacticalMissileLauncher
local AIUtils = import('/lua/ai/aiutilities.lua')

local SDFUnstablePhasonBeam = import('/lua/seraphimweapons.lua').SDFUnstablePhasonBeam
local SAALosaareAutoCannonWeapon = import('/lua/seraphimweapons.lua').SAALosaareAutoCannonWeapon
local SDFAireauWeapon = import('/lua/seraphimweapons.lua').SDFAireauWeapon


local TVISACTIVE = import('/Mods/Commanders Revenges FA UEF,Cybran,Aeon,Seraphim/modlocator.lua').TVisactive()
        
# Setup as RemoteViewing child unit rather than SWalkingLandUnit
local RemoteViewing = import('/lua/RemoteViewing.lua').RemoteViewing
SWalkingLandUnit = RemoteViewing( SWalkingLandUnit ) 

XSL0001 = Class( SWalkingLandUnit ) {
    DeathThreadDestructionWaitTime = 2,

    Weapons = {
        DeathWeapon = Class(SIFCommanderDeathWeapon) {},
        ChronotronCannon = Class(SDFChronotronCannonWeapon) {},
        MultiGun01 = Class(SDFAireauWeapon) {
        	OnCreate = function(self)
              SDFAireauWeapon.OnCreate(self)
            	if not self.SpinManip then 
              	self.SpinManip = CreateRotator(self.unit, 'SpinMultiGun', 'z', nil, 200, 200, 200)
                self.unit.Trash:Add(self.SpinManip)
              end
            end,
        },
        Missile = Class(SIFLaanseTacticalMissileLauncher) {
            OnCreate = function(self)
                SIFLaanseTacticalMissileLauncher.OnCreate(self)
                self:SetWeaponEnabled(false)
            end,
        },
        Electrobolter01 = Class(SDFUnstablePhasonBeam) 
    		{       
            OnCreate = function(self)
              SDFUnstablePhasonBeam.OnCreate(self)
            	if not self.SpinManip then 
                self.SpinManip = CreateRotator(self.unit, 'Electrobolter', 'z', nil, 250, 250, 250)
                self.unit.Trash:Add(self.SpinManip)
            	end
            end, 
    		},
    		AntiAirMissiles01 = Class(SAALosaareAutoCannonWeapon)
    		{
    				OnCreate = function(self)
              SAALosaareAutoCannonWeapon.OnCreate(self)
            	if not self.SpinManip then 
              	self.SpinManip = CreateRotator(self.unit, 'TurnPoint', 'y', nil, 40, 40, 40)
                self.unit.Trash:Add(self.SpinManip)
              end
            end,
        },
        AntiAirMissiles02 = Class(SAALosaareAutoCannonWeapon){},
        AntiAirMissiles03 = Class(SAALosaareAutoCannonWeapon){},
        AntiAirMissiles04 = Class(SAALosaareAutoCannonWeapon){},
        AntiAirMissiles05 = Class(SAALosaareAutoCannonWeapon){},
        AntiAirMissiles06 = Class(SAALosaareAutoCannonWeapon){},
        AntiAirMissiles07 = Class(SAALosaareAutoCannonWeapon){},
        AntiAirMissiles08 = Class(SAALosaareAutoCannonWeapon){},
        
        
        OverCharge = Class(SDFChronotronOverChargeCannonWeapon) {

            OnCreate = function(self)
                SDFChronotronOverChargeCannonWeapon.OnCreate(self)
                self:SetWeaponEnabled(false)
                self.AimControl:SetEnabled(false)
                self.AimControl:SetPrecedence(0)
				self.unit:SetOverchargePaused(false)
            end,

            OnEnableWeapon = function(self)
                if self:BeenDestroyed() then return end
                SDFChronotronOverChargeCannonWeapon.OnEnableWeapon(self)
                self:SetWeaponEnabled(true)
                self.unit:SetWeaponEnabledByLabel('ChronotronCannon', false)
                self.unit:BuildManipulatorSetEnabled(false)
                self.AimControl:SetEnabled(true)
                self.AimControl:SetPrecedence(20)
                self.unit.BuildArmManipulator:SetPrecedence(0)
                self.AimControl:SetHeadingPitch( self.unit:GetWeaponManipulatorByLabel('ChronotronCannon'):GetHeadingPitch() )
            end,
            
            OnWeaponFired = function(self)
                SDFChronotronOverChargeCannonWeapon.OnWeaponFired(self)
                self:OnDisableWeapon()
                self:ForkThread(self.PauseOvercharge)
            end,
            
            OnDisableWeapon = function(self)
                if self.unit:BeenDestroyed() then return end
                self:SetWeaponEnabled(false)
                self.unit:SetWeaponEnabledByLabel('ChronotronCannon', true)
                self.unit:BuildManipulatorSetEnabled(false)
                self.AimControl:SetEnabled(false)
                self.AimControl:SetPrecedence(0)
                self.unit.BuildArmManipulator:SetPrecedence(0)
                self.unit:GetWeaponManipulatorByLabel('ChronotronCannon'):SetHeadingPitch( self.AimControl:GetHeadingPitch() )
            end,
            
            PauseOvercharge = function(self)
                if not self.unit:IsOverchargePaused() then
                    self.unit:SetOverchargePaused(true)
                    WaitSeconds(1/self:GetBlueprint().RateOfFire)
                    self.unit:SetOverchargePaused(false)
                end
            end,
            
            OnFire = function(self)
                if not self.unit:IsOverchargePaused() then
                    SDFChronotronOverChargeCannonWeapon.OnFire(self)
                end
            end,
            IdleState = State(SDFChronotronOverChargeCannonWeapon.IdleState) {
                OnGotTarget = function(self)
                    if not self.unit:IsOverchargePaused() then
                        SDFChronotronOverChargeCannonWeapon.IdleState.OnGotTarget(self)
                    end
                end,            
                OnFire = function(self)
                    if not self.unit:IsOverchargePaused() then
                        ChangeState(self, self.RackSalvoFiringState)
                    end
                end,
            },
            RackSalvoFireReadyState = State(SDFChronotronOverChargeCannonWeapon.RackSalvoFireReadyState) {
                OnFire = function(self)
                    if not self.unit:IsOverchargePaused() then
                        SDFChronotronOverChargeCannonWeapon.RackSalvoFireReadyState.OnFire(self)
                    end
                end,
            },  
        },
    },


    OnCreate = function(self)
        SWalkingLandUnit.OnCreate(self)
        self:SetCapturable(false)
        self:SetupBuildBones()
        self:HideBone('Back_Upgrade', true)
        self:HideBone('Right_Upgrade', true)
        self:HideBone('Left_Upgrade', true)
#NEW
		self:HideBone('Electrobolter', true)
		self:HideBone('TurnPoint', true)
		self:HideBone('SpinMultiGun', true)
		     
        # Restrict what enhancements will enable later
        self:AddBuildRestriction( categories.SERAPHIM * (categories.BUILTBYTIER2COMMANDER + categories.BUILTBYTIER3COMMANDER) )
        self:AddBuildRestriction( categories.SERAPHIM * ( categories.BUILTBYTIER4COMMANDER) )
    end,

    OnPrepareArmToBuild = function(self)
        SWalkingLandUnit.OnPrepareArmToBuild(self)
        if self:BeenDestroyed() then return end
        self:BuildManipulatorSetEnabled(true)
        self.BuildArmManipulator:SetPrecedence(20)
        self:SetWeaponEnabledByLabel('ChronotronCannon', false)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self.BuildArmManipulator:SetHeadingPitch( self:GetWeaponManipulatorByLabel('ChronotronCannon'):GetHeadingPitch() )
    end,

    OnStopCapture = function(self, target)
        SWalkingLandUnit.OnStopCapture(self, target)
        if self:BeenDestroyed() then return end
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        self:SetWeaponEnabledByLabel('ChronotronCannon', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('ChronotronCannon'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
    end,

    OnFailedCapture = function(self, target)
        SWalkingLandUnit.OnFailedCapture(self, target)
        if self:BeenDestroyed() then return end
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        self:SetWeaponEnabledByLabel('ChronotronCannon', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('ChronotronCannon'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
    end,

    OnStopReclaim = function(self, target)
        SWalkingLandUnit.OnStopReclaim(self, target)
        if self:BeenDestroyed() then return end
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        self:SetWeaponEnabledByLabel('ChronotronCannon', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('ChronotronCannon'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
    end,

    OnStopBeingBuilt = function(self,builder,layer)
        SWalkingLandUnit.OnStopBeingBuilt(self,builder,layer)
        self:DisableRemoteViewingButtons()
        self:SetWeaponEnabledByLabel('ChronotronCannon', true)	
#NEW		
		self:SetWeaponEnabledByLabel('Electrobolter01', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles01', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles02', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles03', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles04', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles05', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles06', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles07', false)
		self:SetWeaponEnabledByLabel('AntiAirMissiles08', false)
		self:SetWeaponEnabledByLabel('MultiGun01', false)
		
        self:ForkThread(self.GiveInitialResources)
        self.ShieldEffectsBag = {}
    end,

    OnFailedToBuild = function(self)
        SWalkingLandUnit.OnFailedToBuild(self)
        if self:BeenDestroyed() then return end
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        self:SetWeaponEnabledByLabel('ChronotronCannon', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('ChronotronCannon'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
    end,

    OnStartBuild = function(self, unitBeingBuilt, order)
        local bp = self:GetBlueprint()
        if order != 'Upgrade' or bp.Display.ShowBuildEffectsDuringUpgrade then
            self:StartBuildingEffects(unitBeingBuilt, order)
        end
        self:DoOnStartBuildCallbacks(unitBeingBuilt)
        self:SetActiveConsumptionActive()
        self:PlayUnitSound('Construct')
        self:PlayUnitAmbientSound('ConstructLoop')
        if bp.General.UpgradesTo and unitBeingBuilt:GetUnitId() == bp.General.UpgradesTo and order == 'Upgrade' then
            self.Upgrading = true
            self.BuildingUnit = false        
            unitBeingBuilt.DisallowCollisions = true
        end
        self.UnitBeingBuilt = unitBeingBuilt
        self.UnitBuildOrder = order
        self.BuildingUnit = true
    end,  

    OnStopBuild = function(self, unitBeingBuilt)
        SWalkingLandUnit.OnStopBuild(self, unitBeingBuilt)
        if self:BeenDestroyed() then return end
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        self:SetWeaponEnabledByLabel('ChronotronCannon', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('ChronotronCannon'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
        self.UnitBeingBuilt = nil
        self.UnitBuildOrder = nil
        self.BuildingUnit = false
    end,
    
    PlayCommanderWarpInEffect = function(self)
        self:HideBone(0, true)
        self:SetUnSelectable(true)
        self:SetBusy(true)
        self:SetBlockCommandQueue(true)
        self:ForkThread(self.WarpInEffectThread)
    end, 
    
    WarpInEffectThread = function(self)
        self:PlayUnitSound('CommanderArrival')
        self:CreateProjectile( '/effects/entities/UnitTeleport01/UnitTeleport01_proj.bp', 0, 1.35, 0, nil, nil, nil):SetCollision(false)
        WaitSeconds(2.1)
        self:ShowBone(0, true)
        self:HideBone('Back_Upgrade', true)
        self:HideBone('Right_Upgrade', true)
        self:HideBone('Left_Upgrade', true)
#NEW
		self:HideBone('Electrobolter', true)
		self:HideBone('TurnPoint', true)
		self:HideBone('SpinMultiGun', true)
		
        self:SetUnSelectable(false)
        self:SetBusy(false)
        self:SetBlockCommandQueue(false)

        local totalBones = self:GetBoneCount() - 1
        local army = self:GetArmy()
        for k, v in EffectTemplate.UnitTeleportSteam01 do
            for bone = 1, totalBones do
                CreateAttachedEmitter(self,bone,army, v)
            end
        end

        WaitSeconds(6)
    end,

    GiveInitialResources = function(self)
        WaitTicks(2)
        self:GetAIBrain():GiveResource('Energy', self:GetBlueprint().Economy.StorageEnergy)
        self:GetAIBrain():GiveResource('Mass', self:GetBlueprint().Economy.StorageMass)
    end,

    CreateBuildEffects = function( self, unitBeingBuilt, order )
        EffectUtil.CreateSeraphimUnitEngineerBuildingEffects( self, unitBeingBuilt, self:GetBlueprint().General.BuildBones.BuildEffectBones, self.BuildEffectsBag )
    end,

    RegenBuffThread = function(self)
        while not self:IsDead() do
            #Get friendly units in the area (including self)
            local units = AIUtils.GetOwnUnitsAroundPoint(self:GetAIBrain(), categories.ALLUNITS, self:GetPosition(), self:GetBlueprint().Enhancements.RegenAura.Radius)
            
            #Give them a 5 second regen buff
            for _,unit in units do
                Buff.ApplyBuff(unit, 'SeraphimACURegenAura')
            end
            
            #Wait 5 seconds
            WaitSeconds(5)
        end
    end,
       
    AdvancedRegenBuffThread = function(self)
        while not self:IsDead() do
            #Get friendly units in the area (including self)
            local units = AIUtils.GetOwnUnitsAroundPoint(self:GetAIBrain(), categories.ALLUNITS, self:GetPosition(), self:GetBlueprint().Enhancements.AdvancedRegenAura.Radius)
            
            #Give them a 5 second regen buff
            for _,unit in units do
                Buff.ApplyBuff(unit, 'SeraphimAdvancedACURegenAura')
            end
            
            #Wait 5 seconds
            WaitSeconds(5)
        end
    end,

    CreateEnhancement = function(self, enh)
        SWalkingLandUnit.CreateEnhancement(self, enh)
        local bp = self:GetBlueprint().Enhancements[enh]
        if not bp then return end
        # Regenerative Aura
        if enh == 'RegenAura' then
            if TVISACTIVE then
	            if not Buffs['SeraphimACURegenAura'] then
	                BuffBlueprint {
	                    Name = 'SeraphimACURegenAura',
	                    DisplayName = 'SeraphimACURegenAura',
	                    BuffType = 'COMMANDERAURA',
	                    Stacks = 'ALWAYS',
	                    Duration = 5,
	                    Affects = {
	                        RegenPercent = {
	                            Add = 0,
	                            Mult = bp.RegenPerSecond or 0.1,
	                            Ceil = bp.RegenCeiling,
	                            Floor = bp.RegenFloor,
	                        },
	                    },
	                }
	                
	            end
            else 
	            if not Buffs['SeraphimACURegenAura'] then
	                BuffBlueprint {
	                    Name = 'SeraphimACURegenAura',
	                    DisplayName = 'SeraphimACURegenAura',
	                    BuffType = 'COMMANDERAURA',
	                    Stacks = 'REPLACE',
	                    Duration = 5,
	                    Affects = {
	                        RegenPercent = {
	                            Add = 0,
	                            Mult = bp.RegenPerSecond or 0.1,
	                            Ceil = bp.RegenCeiling,
	                            Floor = bp.RegenFloor,
	                        },
	                    },
	                }
	                
	            end
            end
            
            table.insert( self.ShieldEffectsBag, CreateAttachedEmitter( self, 'XSL0001', self:GetArmy(), '/effects/emitters/seraphim_regenerative_aura_01_emit.bp' ) )
            self.RegenThreadHandle = self:ForkThread(self.RegenBuffThread)
                        
        elseif enh == 'AdvancedRegenAura' then
            if self.RegenThreadHandle then
                if self.ShieldEffectsBag then
                    for k, v in self.ShieldEffectsBag do
                        v:Destroy()
                    end
		            self.ShieldEffectsBag = {}
		        end
                KillThread(self.RegenThreadHandle)
                
            end
            
            if TVISACTIVE then 
	            if not Buffs['SeraphimAdvancedACURegenAura'] then
	                BuffBlueprint {
	                    Name = 'SeraphimAdvancedACURegenAura',
	                    DisplayName = 'SeraphimAdvancedACURegenAura',
	                    BuffType = 'COMMANDERAURA',
	                    Stacks = 'ALWAYS',
	                    Duration = 5,
	                    Affects = {
	                        RegenPercent = {
	                            #Add = bp.RegenPerSecond or 0.1,
	                            #Mult = 0,
	                            Add = 0,
	                            Mult = bp.RegenPerSecond or 0.1,
	                            
	                            Ceil = bp.RegenCeiling,
	                            Floor = bp.RegenFloor,
	                        },
	                        MaxHealth = {
	                            Add = 0,
	                            Mult = bp.MaxHealthFactor or 1.0,
	                        },                        
	                    },
	                }
	            end
            else
	            if not Buffs['SeraphimAdvancedACURegenAura'] then
	                BuffBlueprint {
	                    Name = 'SeraphimAdvancedACURegenAura',
	                    DisplayName = 'SeraphimAdvancedACURegenAura',
	                    BuffType = 'COMMANDERAURA',
	                    Stacks = 'REPLACE',
	                    Duration = 5,
	                    Affects = {
	                        RegenPercent = {
	                            Add = 0,
	                            Mult = bp.RegenPerSecond or 0.1,
	                            Ceil = bp.RegenCeiling,
	                            Floor = bp.RegenFloor,
	                        },
	                        MaxHealth = {
	                            Add = 0,
	                            Mult = bp.MaxHealthFactor or 1.0,
	                            DoNoFill = true,
	                        },                        
	                    },
	                }
	            end
            end
            
            table.insert( self.ShieldEffectsBag, CreateAttachedEmitter( self, 'XSL0001', self:GetArmy(), '/effects/emitters/seraphim_regenerative_aura_01_emit.bp' ) )
            self.AdvancedRegenThreadHandle = self:ForkThread(self.AdvancedRegenBuffThread)
            
        #Resource Allocation
        elseif enh == 'ResourceAllocation' then
        	if TVISACTIVE then
	        	if not Buffs['SeraACUResourceAllocation'] then
	                BuffBlueprint {
	                    Name = 'SeraACUResourceAllocation',
	                    DisplayName = 'SeraACUResourceAllocation',
	                    BuffType = 'SeraACUResourceAllocation',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        MassProductionBuf = {
	                            Add =  bp.ProductionPerSecondMass,
	                            Mult = 1,
	                        },
	                        EnergyProductionBuf = {
	                            Add = bp.ProductionPerSecondEnergy,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'SeraACUResourceAllocation')
	        else
	        	local bpEcon = self:GetBlueprint().Economy
            self:SetProductionPerSecondEnergy(bp.ProductionPerSecondEnergy + bpEcon.ProductionPerSecondEnergy or 0)
            self:SetProductionPerSecondMass(bp.ProductionPerSecondMass + bpEcon.ProductionPerSecondMass or 0)
          end  
        elseif enh == 'ResourceAllocationAdvanced' then
            if TVISACTIVE then
	            if not Buffs['SeraACUResourceAllocationAdvanced'] then
	                BuffBlueprint {
	                    Name = 'SeraACUResourceAllocationAdvanced',
	                    DisplayName = 'SeraACUResourceAllocationAdvanced',
	                    BuffType = 'SeraACUResourceAllocationAdvanced',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        MassProductionBuf = {
	                            Add =  bp.ProductionPerSecondMass,
	                            Mult = 1,
	                        },
	                        EnergyProductionBuf = {
	                            Add = bp.ProductionPerSecondEnergy,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'SeraACUResourceAllocationAdvanced')
	           	if Buff.HasBuff( self, 'SeraACUResourceAllocation' ) then
                Buff.RemoveBuff(self,'SeraACUResourceAllocation',false)
            	end 
	          else
		          local bpEcon = self:GetBlueprint().Economy
	            self:SetProductionPerSecondEnergy(bp.ProductionPerSecondEnergy + bpEcon.ProductionPerSecondEnergy or 0)
	            self:SetProductionPerSecondMass(bp.ProductionPerSecondMass + bpEcon.ProductionPerSecondMass or 0)
	          end
        #Damage Stabilization
        elseif enh == 'DamageStabilization' then
            if not Buffs['SeraphimACUDamageStabilization'] then
               BuffBlueprint {
                    Name = 'SeraphimACUDamageStabilization',
                    DisplayName = 'SeraphimACUDamageStabilization',
                    BuffType = 'ACUUPGRADEDMG',
                    Stacks = 'ALWAYS',
                    Duration = -1,
                    Affects = {
                        MaxHealth = {
                            Add = bp.NewHealth,
                            Mult = 1.0,
                        },
                        Regen = {
                            Add = bp.NewRegenRate,
                            Mult = 1.0,
                        },
                    },
                } 
            end
            if Buff.HasBuff( self, 'SeraphimACUDamageStabilization' ) then
                Buff.RemoveBuff( self, 'SeraphimACUDamageStabilization' )
            end  
            Buff.ApplyBuff(self, 'SeraphimACUDamageStabilization')    
      	elseif enh == 'DamageStabilizationAdvanced' then
            if not Buffs['SeraphimACUDamageStabilizationAdv'] then
               BuffBlueprint {
                    Name = 'SeraphimACUDamageStabilizationAdv',
                    DisplayName = 'SeraphimACUDamageStabilizationAdv',
                    BuffType = 'ACUUPGRADEDMG',
                    Stacks = 'ALWAYS',
                    Duration = -1,
                    Affects = {
                        MaxHealth = {
                            Add = bp.NewHealth,
                            Mult = 1.0,
                        },
                        Regen = {
                            Add = bp.NewRegenRate,
                            Mult = 1.0,
                        },
                    },
                } 
            end
            if Buff.HasBuff( self, 'SeraphimACUDamageStabilizationAdv' ) then
                Buff.RemoveBuff( self, 'SeraphimACUDamageStabilizationAdv' )
            end  
            Buff.ApplyBuff(self, 'SeraphimACUDamageStabilizationAdv')     	    
        #Teleporter
        elseif enh == 'Teleporter' then
            self:AddCommandCap('RULEUCC_Teleport')
        # Tactical Missile
        elseif enh == 'Missile' then
            self:AddCommandCap('RULEUCC_Tactical')
            self:AddCommandCap('RULEUCC_SiloBuildTactical')        
            self:SetWeaponEnabledByLabel('Missile', true)
        #T2 Engineering
        elseif enh =='AdvancedEngineering' then
           local cat = ParseEntityCategory(bp.BuildableCategoryAdds)
           self:RemoveBuildRestriction(cat)
           if TVISACTIVE then
	            if not Buffs['SeraphimACUT2BuildRate'] then
                BuffBlueprint {
                    Name = 'SeraphimACUT2BuildRate',
                    DisplayName = 'SeraphimACUT2BuildRate',
                    BuffType = 'ACUBUILDRATE',
                    Stacks = 'ALWAYS',
                    Duration = -1,
                    Affects = {
                        BuildRate = {
                            Add =  bp.NewBuildRate,
                            Mult = 1,
                        },
                        MaxHealth = {
                            Add = bp.NewHealth,
                            Mult = 1.0,
                        },
                        Regen = {
                            Add = bp.NewRegenRate,
                            Mult = 1.0,
                        },
                    },
                }
            end
           else
	          self:RemoveBuildRestriction(cat)
	            if not Buffs['SeraphimACUT2BuildRate'] then
	                BuffBlueprint {
	                    Name = 'SeraphimACUT2BuildRate',
	                    DisplayName = 'SeraphimACUT2BuildRate',
	                    BuffType = 'ACUBUILDRATE',
	                    Stacks = 'REPLACE',
	                    Duration = -1,
	                    Affects = {
	                        BuildRate = {
	                            Add =  bp.NewBuildRate - self:GetBlueprint().Economy.BuildRate,
	                            Mult = 1,
	                        },
	                        MaxHealth = {
	                            Add = bp.NewHealth,
	                            Mult = 1.0,
	                        },
	                        Regen = {
	                            Add = bp.NewRegenRate,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	          end
            Buff.ApplyBuff(self, 'SeraphimACUT2BuildRate')
            
        #T3 Engineering
        elseif enh =='T3Engineering' then
            local bp = self:GetBlueprint().Enhancements[enh]
            if not bp then return end
            local cat = ParseEntityCategory(bp.BuildableCategoryAdds)
            self:RemoveBuildRestriction(cat)
            if TVISACTIVE then
	            if not Buffs['SeraphimACUT3BuildRate'] then
	                BuffBlueprint {
	                    Name = 'SeraphimACUT3BuildRate',
	                    DisplayName = 'SeraphimCUT3BuildRate',
	                    BuffType = 'ACUBUILDRATE',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        BuildRate = {
	                            Add =  bp.NewBuildRate,
	                            Mult = 1,
	                        },
	                        MaxHealth = {
	                            Add = bp.NewHealth,
	                            Mult = 1.0,
	                        },
	                        Regen = {
	                            Add = bp.NewRegenRate,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	            if Buff.HasBuff( self, 'SeraphimACUT2BuildRate' ) then
                Buff.RemoveBuff( self, 'SeraphimACUT2BuildRate' )
            	end
            else 
	            if not Buffs['SeraphimACUT3BuildRate'] then
	                BuffBlueprint {
	                    Name = 'SeraphimACUT3BuildRate',
	                    DisplayName = 'SeraphimCUT3BuildRate',
	                    BuffType = 'ACUBUILDRATE',
	                    Stacks = 'REPLACE',
	                    Duration = -1,
	                    Affects = {
	                        BuildRate = {
	                            Add =  bp.NewBuildRate - self:GetBlueprint().Economy.BuildRate,
	                            Mult = 1,
	                        },
	                        MaxHealth = {
	                            Add = bp.NewHealth,
	                            Mult = 1.0,
	                        },
	                        Regen = {
	                            Add = bp.NewRegenRate,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	          end
            Buff.ApplyBuff(self, 'SeraphimACUT3BuildRate')
        #Blast Attack
        elseif enh == 'BlastAttack' then
        	if TVISACTIVE then
	        	if not Buffs['SeraACUBlastAttack'] then
	                BuffBlueprint {
	                    Name = 'SeraACUBlastAttack',
	                    DisplayName = 'SeraACUBlastAttack',
	                    BuffType = 'SeraACUBlastAttack',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        Damage = {
	                            Add =  bp.AdditionalDamage,
	                            Mult = 1,
	                            ByName = {
	                            	ChronotronCannon = true,
	                            },
	                        },
	                        MaxRadius = {
	                            Add =  bp.NewMaxRadius,
	                            Mult = 1,
	                            ByName = {
	                            	ChronotronCannon = true,
	                            },
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'SeraACUBlastAttack')
	          else
		          local wep = self:GetWeaponByLabel('ChronotronCannon')
	            wep:AddDamageRadiusMod(bp.NewDamageRadius or 5)
	            wep:AddDamageMod(bp.AdditionalDamage)
	          end
        elseif enh == 'RateOfFire' then
        	if TVISACTIVE then
	        	if not Buffs['SeraACURateOfFire'] then
	                BuffBlueprint {
	                    Name = 'SeraACURateOfFire',
	                    DisplayName = 'SeraACURateOfFire',
	                    BuffType = 'SeraACURateOfFire',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        RateOfFireBuf = {
	                            Add =  bp.NewRateOfFire,
	                            Mult = 1,
	                            ByName = {
	                            	ChronotronCannon = true,
	                            },
	                        },
	                        MaxRadius = {
	                            Add =  bp.NewMaxRadius,
	                            Mult = 1,
	                            ByName = {
	                            	ChronotronCannon = true,
	                            	OverCharge = true,
	                            },
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'SeraACURateOfFire')
        else
        	local wep = self:GetWeaponByLabel('ChronotronCannon')
          wep:ChangeRateOfFire(bp.NewRateOfFire or 2)
          wep:ChangeMaxRadius(bp.NewMaxRadius or 44)
          local oc = self:GetWeaponByLabel('OverCharge')
          oc:ChangeMaxRadius(bp.NewMaxRadius or 44)     
        end       
#######NEW Enhancements
			#T4Engineering
        elseif enh =='T4Engineering' then
            local cat = ParseEntityCategory(bp.BuildableCategoryAdds)
            self:RemoveBuildRestriction(cat)
            if TVISACTIVE then
	            if not Buffs['SeraphimACUT4BuildRate'] then
	                BuffBlueprint {
	                    Name = 'SeraphimACUT4BuildRate',
	                    DisplayName = 'SeraphimCUT4BuildRate',
	                    BuffType = 'ACUBUILDRATE',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        BuildRate = {
	                            Add =  bp.NewBuildRate - self:GetBlueprint().Economy.BuildRate,
	                            Mult = 1,
	                        },
	                        MaxHealth = {
	                            Add = bp.NewHealth,
	                            Mult = 1.0,
	                        },
	                        Regen = {
	                            Add = bp.NewRegenRate,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	            if Buff.HasBuff( self, 'SeraphimACUT3BuildRate' ) then
                Buff.RemoveBuff( self, 'SeraphimACUT3BuildRate' )
            	end
	          else 
		          if not Buffs['SeraphimACUT4BuildRate'] then
                BuffBlueprint {
                    Name = 'SeraphimACUT4BuildRate',
                    DisplayName = 'SeraphimCUT4BuildRate',
                    BuffType = 'ACUBUILDRATE',
                    Stacks = 'REPLACE',
                    Duration = -1,
                    Affects = {
                        BuildRate = {
                            Add =  bp.NewBuildRate - self:GetBlueprint().Economy.BuildRate,
                            Mult = 1,
                        },
                        MaxHealth = {
                            Add = bp.NewHealth,
                            Mult = 1.0,
                        },
                        Regen = {
                            Add = bp.NewRegenRate,
                            Mult = 1.0,
                        },
                    },
                }
            	end
	          end
            Buff.ApplyBuff(self, 'SeraphimACUT4BuildRate')
        #Electrobolter
        elseif enh == 'Electrobolter' then
						self:SetWeaponEnabledByLabel('Electrobolter01', true)	
      #Antiair
        elseif enh == 'Antiair' then
						self:SetWeaponEnabledByLabel('AntiAirMissiles01', true)	
						self:SetWeaponEnabledByLabel('AntiAirMissiles02', true)	
						self:SetWeaponEnabledByLabel('AntiAirMissiles03', true)	
						self:SetWeaponEnabledByLabel('AntiAirMissiles04', true)	
      #AdvanceAntiair
        elseif enh == 'AdvanceAntiair' then
						self:SetWeaponEnabledByLabel('AntiAirMissiles05', true)	
						self:SetWeaponEnabledByLabel('AntiAirMissiles06', true)	
						self:SetWeaponEnabledByLabel('AntiAirMissiles07', true)	
						self:SetWeaponEnabledByLabel('AntiAirMissiles08', true)
      #MultiGun
        elseif enh == 'MultiGun' then
						self:SetWeaponEnabledByLabel('MultiGun01', true)
            
          
############END Enhancements        
        end
    end,

    OnPaused = function(self)
        SWalkingLandUnit.OnPaused(self)
        if self.BuildingUnit then
            SWalkingLandUnit.StopBuildingEffects(self, self:GetUnitBeingBuilt())
        end
    end,

    OnUnpaused = function(self)
        if self.BuildingUnit then
            SWalkingLandUnit.StartBuildingEffects(self, self:GetUnitBeingBuilt(), self.UnitBuildOrder)
        end
        SWalkingLandUnit.OnUnpaused(self)
    end,

}

TypeClass = XSL0001