#****************************************************************************
#**
#**  File     :  /cdimage/units/UEL0001/UEL0001_script.lua
#**  Author(s):  John Comes, David Tomandl, Jessica St. Croix
#**
#**  Summary  :  UEF Commander Script
#**
#**  Copyright © 2005 Gas Powered Games, Inc.  All rights reserved.
#****************************************************************************
local Shield = import('/lua/shield.lua').Shield

local TWalkingLandUnit = import('/lua/terranunits.lua').TWalkingLandUnit
local TerranWeaponFile = import('/lua/terranweapons.lua')
local TDFZephyrCannonWeapon = TerranWeaponFile.TDFZephyrCannonWeapon
local TDFHeavyPlasmaCannonWeapon = TerranWeaponFile.TDFHeavyPlasmaCannonWeapon
local TIFCommanderDeathWeapon = TerranWeaponFile.TIFCommanderDeathWeapon
local EffectTemplate = import('/lua/EffectTemplates.lua')
local TIFCruiseMissileLauncher = TerranWeaponFile.TIFCruiseMissileLauncher
local TSAMLauncher = import('/lua/terranweapons.lua').TSAMLauncher
local TDFOverchargeWeapon = TerranWeaponFile.TDFOverchargeWeapon
local EffectUtil = import('/lua/EffectUtilities.lua')
local Buff = import('/lua/sim/Buff.lua')
local TVISACTIVE = import('/Mods/Commanders Revenges FA UEF,Cybran,Aeon,Seraphim/modlocator.lua').TVisactive()

local podteleport,podlocation

UEL0001 = Class(TWalkingLandUnit) {    
    DeathThreadDestructionWaitTime = 2,

    Weapons = {
		###### Heavy Bolter #### Left And Right #### Debut ####
		HeavyPlasma01 = Class(TDFHeavyPlasmaCannonWeapon) {},
		HeavyPlasma02 = Class(TDFHeavyPlasmaCannonWeapon) {},
		###### Heavy Bolter #### Left And Right #### Fin ####
        DeathWeapon = Class(TIFCommanderDeathWeapon) {},
		###### Heavy MEGA OVER CANON #### Left And Right #### debut ####
		Megacannon01 = Class(TDFZephyrCannonWeapon) {},
		Megacannon02 = Class(TDFZephyrCannonWeapon) {},	
		Megacannon03 = Class(TDFZephyrCannonWeapon) {},
		Megacannon04 = Class(TDFZephyrCannonWeapon) {},	
		###### Heavy MEGA OVER CANON #### Left And Right #### FIN ####
		###### Heavy Anti Air #### Left And Right #### debut ####
		MissileRack01 = Class(TSAMLauncher) {},		
		MissileRack02 = Class(TSAMLauncher) {},	
		###### Heavy Anti Air #### Left And Right #### Fin ####
    Minigun01 = Class(TDFHeavyPlasmaCannonWeapon) 
    {       
          OnCreate = function(self)
            	TDFHeavyPlasmaCannonWeapon.OnCreate(self)
            	if not self.SpinManip then 
                   self.SpinManip = CreateRotator(self.unit, 'Minigun01_Barrel', 'z', nil, 350, 350, 350)
                   self.unit.Trash:Add(self.SpinManip)
            	end
           end,
    },
    Minigun02 = Class(TDFHeavyPlasmaCannonWeapon) 
    {       
          OnCreate = function(self)
            	TDFHeavyPlasmaCannonWeapon.OnCreate(self)
            	if not self.SpinManip then 
                   self.SpinManip = CreateRotator(self.unit, 'Minigun02_Barrel', 'z', nil, 350, 350, 350)
                   self.unit.Trash:Add(self.SpinManip)
            	end
           end,
    },  
        RightZephyr = Class(TDFZephyrCannonWeapon) {},
        LeftZephyr = Class(TDFZephyrCannonWeapon) {},
        OverCharge = Class(TDFOverchargeWeapon) {
            OnCreate = function(self)
                TDFOverchargeWeapon.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
                self:SetWeaponEnabled(true)
                self.unit:SetWeaponEnabledByLabel('RightZephyr', false)
                self.unit:ResetWeaponByLabel('RightZephyr')
                self.unit:BuildManipulatorSetEnabled(false)
                self.AimControl:SetEnabled(true)
                self.AimControl:SetPrecedence(20)
                self.unit.BuildArmManipulator:SetPrecedence(0)
                self.AimControl:SetHeadingPitch( self.unit:GetWeaponManipulatorByLabel('RightZephyr'):GetHeadingPitch() )
            end,

            OnWeaponFired = function(self)
                TDFOverchargeWeapon.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('RightZephyr', true)
                self.unit:BuildManipulatorSetEnabled(false)
                self.AimControl:SetEnabled(false)
                self.AimControl:SetPrecedence(0)
                self.unit.BuildArmManipulator:SetPrecedence(0)
                self.unit:GetWeaponManipulatorByLabel('RightZephyr'):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
                    TDFOverchargeWeapon.OnFire(self)
                end
            end,
            IdleState = State(TDFOverchargeWeapon.IdleState) {
                OnGotTarget = function(self)
                    if not self.unit:IsOverchargePaused() then
                        TDFOverchargeWeapon.IdleState.OnGotTarget(self)
                    end
                end,            
                OnFire = function(self)
                    if not self.unit:IsOverchargePaused() then
                        ChangeState(self, self.RackSalvoFiringState)
                    end
                end,
            },
            RackSalvoFireReadyState = State(TDFOverchargeWeapon.RackSalvoFireReadyState) {
                OnFire = function(self)
                    if not self.unit:IsOverchargePaused() then
                        TDFOverchargeWeapon.RackSalvoFireReadyState.OnFire(self)
                    end
                end,
            },            
            
        },
    },

    OnCreate = function(self)
        TWalkingLandUnit.OnCreate(self)
        self:SetCapturable(false)
        self:HideBone('Back_Upgrade_B01', true) --Right_Arm_Barrel
		self:HideBone('Megacannon01', true)
		self:HideBone('Megacannon02', true)
		self:HideBone('Megacannon03', true)
		self:HideBone('Megacannon04', true)
		self:HideBone('Rack_Over_Mega_Heavy_Canon01', true)
		self:HideBone('Rack_Over_Mega_Heavy_Canon02', true)
		self:HideBone('Minigun01', true)
		self:HideBone('Minigun02', true)
		self:HideBone('Right_Upgrade02', true)
		self:HideBone('Right_Upgrade03', true)
		self:HideBone('Cape', true)
		self:HideBone('Heavy_Bolter01', true)
		self:HideBone('Heavy_Bolter02', true)
		    self:SetupBuildBones()
        self.HasLeftPod = false
        self.HasRightPod = false
        # Restrict what enhancements will enable later
        self:AddBuildRestriction( categories.UEF * (categories.BUILTBYTIER2COMMANDER + categories.BUILTBYTIER3COMMANDER) )
        self:AddBuildRestriction( categories.UEF * (categories.BUILTBYTIER4COMMANDER) )
    end,

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

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

    OnFailedCapture = function(self, target)
        TWalkingLandUnit.OnFailedCapture(self, target)
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        --self:SetWeaponEnabledByLabel('RightZephyr', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('RightZephyr'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
    end,

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

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

    OnStopBeingBuilt = function(self,builder,layer)
        TWalkingLandUnit.OnStopBeingBuilt(self,builder,layer)
        if self:BeenDestroyed() then return end
        self.Animator = CreateAnimator(self)
        self.Animator:SetPrecedence(0)
        if self.IdleAnim then
            self.Animator:PlayAnim(self:GetBlueprint().Display.AnimationIdle, true)
            for k, v in self.DisabledBones do
                self.Animator:SetBoneEnabled(v, false)
            end
        end
        self:BuildManipulatorSetEnabled(false)
        --self:SetWeaponEnabledByLabel('RightZephyr', true)
		self:SetWeaponEnabledByLabel('HeavyPlasma01', false)
		self:SetWeaponEnabledByLabel('HeavyPlasma02', false)
		self:SetWeaponEnabledByLabel('Megacannon01', false)
		self:SetWeaponEnabledByLabel('Megacannon02', false)
		self:SetWeaponEnabledByLabel('Megacannon03', false)
		self:SetWeaponEnabledByLabel('Megacannon04', false)
		self:SetWeaponEnabledByLabel('MissileRack01', false)
		self:SetWeaponEnabledByLabel('MissileRack02', false)
    self:SetWeaponEnabledByLabel('Minigun01', false)
    self:SetWeaponEnabledByLabel('Minigun02', false)
        self:ForkThread(self.GiveInitialResources)
    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_B01', true) --Right_Arm_Barrel

#NEW
		self:HideBone('Megacannon01', true)
		self:HideBone('Megacannon02', true)
		self:HideBone('Megacannon03', true)
		self:HideBone('Megacannon04', true)
		self:HideBone('Rack_Over_Mega_Heavy_Canon01', true)
		self:HideBone('Rack_Over_Mega_Heavy_Canon02', true)
		self:HideBone('Minigun01', true)
		self:HideBone('Minigun02', true)
		self:HideBone('Right_Upgrade02', true)
		self:HideBone('Right_Upgrade03', true)
		self:HideBone('Heavy_Bolter01', true)
		self:HideBone('Heavy_Bolter02', true)
		self:HideBone('Cape', 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,

    OnStartBuild = function(self, unitBeingBuilt, order)
        TWalkingLandUnit.OnStartBuild(self, unitBeingBuilt, order)
        if self.Animator then
            self.Animator:SetRate(0)
        end
        self.UnitBeingBuilt = unitBeingBuilt
        self.UnitBuildOrder = order
        self.BuildingUnit = true        
    end,

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

    CreateBuildEffects = function( self, unitBeingBuilt, order )
        local UpgradesFrom = unitBeingBuilt:GetBlueprint().General.UpgradesFrom
        # If we are assisting an upgrading unit, or repairing a unit, play seperate effects
        if (order == 'Repair' and not unitBeingBuilt:IsBeingBuilt()) or (UpgradesFrom and UpgradesFrom != 'none' and self:IsUnitState('Guarding'))then
            EffectUtil.CreateDefaultBuildBeams( self, unitBeingBuilt, self:GetBlueprint().General.BuildBones.BuildEffectBones, self.BuildEffectsBag )
        else
            EffectUtil.CreateUEFCommanderBuildSliceBeams( self, unitBeingBuilt, self:GetBlueprint().General.BuildBones.BuildEffectBones, self.BuildEffectsBag )        
        end           
    end,

    OnStopBuild = function(self, unitBeingBuilt)
        TWalkingLandUnit.OnStopBuild(self, unitBeingBuilt)
        if self:BeenDestroyed() then return end
        if (self.IdleAnim and not self:IsDead()) then
            self.Animator:PlayAnim(self.IdleAnim, true)
        end
        self:BuildManipulatorSetEnabled(false)
        self.BuildArmManipulator:SetPrecedence(0)
        --self:SetWeaponEnabledByLabel('RightZephyr', true)
        self:SetWeaponEnabledByLabel('OverCharge', false)
        self:GetWeaponManipulatorByLabel('RightZephyr'):SetHeadingPitch( self.BuildArmManipulator:GetHeadingPitch() )
        self.UnitBeingBuilt = nil
        self.UnitBuildOrder = nil
        self.BuildingUnit = false          
    end,

		OnTeleportUnit = function(self, teleporter, location, orientation)
    			#LOG('OnTeleportUnit')
    			TWalkingLandUnit.OnTeleportUnit(self, teleporter, location, orientation)
    			
    			podteleporter = teleporter
    			podlocation = location
    			#LOG(podteleporter)
    			#LOG(podlocation)
    end,
    
    PlayTeleportInEffects = function(self)
        local army = self:GetArmy()
        local bp = self:GetBlueprint()
        for k, v in EffectTemplate.GenericTeleportIn01 do
            emit = CreateEmitterAtEntity(self,army,v):OffsetEmitter(0, (bp.Physics.MeshExtentsY or 1) / 2, 0)
        end
        
        if self.RightPod 
    		 and not self.RightPod:IsDead()
    		 and not self.RightPod:IsUnitState( 'Attached' ) then
    					self.RightPod.OnTeleportUnit(self.RightPod, podteleporter, podlocation)
    					IssueClearCommands({self.RightPod})
    					IssueGuard( {self.RightPod}, self)
    			end
    			
				if self.LeftPod 
				 and not self.LeftPod:IsDead()
				 and not self.LeftPod:IsUnitState( 'Attached' ) then
							self.LeftPod.OnTeleportUnit(self.LeftPod, podteleporter, podlocation)
							IssueClearCommands({self.LeftPod})
							IssueGuard( {self.LeftPod}, self)
				end
    end,
    
    		
    NotifyOfPodDeath = function(self, pod, Unit)
        if pod == 'RightPod' then
            local location = self:GetPosition('AttachSpecial02')
            local pod = CreateUnitHPR('UEP0001', self:GetArmy(), location[1], location[2], location[3], 0, 0, 0)
            pod:SetParent(self, 'RightPod')
            pod:SetCreator(self)
            self.Trash:Add(pod)
            self.HasRightPod = true
            self.RightPod = pod
    				
    				if TVISACTIVE then
	    				pod.xp = Unit.xp
	          	if Unit.VeteranLevel > 0 then
		        		local i = 1
		            while i <= Unit.VeteranLevel do
		            		pod:SetVeteranLevel(i)
		            		i = i + 1
		            end
		          end
	          	pod:CheckVeteranLevel()
	         	end
	         	pod:PlayTeleportOutEffects()
	         	self:RequestRefreshUI()
        elseif pod == 'LeftPod' then
            local location = self:GetPosition('AttachSpecial01')
            local pod = CreateUnitHPR('UEP0001', self:GetArmy(), location[1], location[2], location[3], 0, 0, 0)
            pod:SetParent(self, 'LeftPod')
            pod:SetCreator(self)
            self.Trash:Add(pod)
            self.HasLeftPod = true
            self.LeftPod = pod
            if TVISACTIVE then
		    			pod.xp = Unit.xp
	          	if Unit.VeteranLevel > 0 then
		        		local i = 1
		            while i <= Unit.VeteranLevel do
		            		pod:SetVeteranLevel(i)
		            		i = i + 1
		            end
		          end
	          	pod:CheckVeteranLevel()
	         	end
	         	pod:PlayTeleportOutEffects()
          	self:RequestRefreshUI()
        end
        
    end,

    CreateEnhancement = function(self, enh)
        TWalkingLandUnit.CreateEnhancement(self, enh)
        local bp = self:GetBlueprint().Enhancements[enh]
        if not bp then return end
        if enh == 'LeftPod' then
            local location = self:GetPosition('AttachSpecial01')
            local pod = CreateUnitHPR('UEP0001', self:GetArmy(), location[1], location[2], location[3], 0, 0, 0)
            pod:SetParent(self, 'LeftPod')
            pod:SetCreator(self)
            self.Trash:Add(pod)
            self.HasLeftPod = true
            self.LeftPod = pod
            pod:PlayTeleportOutEffects()
        		
        elseif enh == 'RightPod' then
            local location = self:GetPosition('AttachSpecial02')
            local pod = CreateUnitHPR('UEP0001', self:GetArmy(), location[1], location[2], location[3], 0, 0, 0)
            pod:SetParent(self, 'RightPod')
            pod:SetCreator(self)
            self.Trash:Add(pod)
            self.HasRightPod = true
            self.RightPod = pod
            pod:PlayTeleportOutEffects()
        elseif enh == 'Teleporter' then
            self:AddCommandCap('RULEUCC_Teleport')
        elseif enh == 'Shield' then
            self:AddToggleCap('RULEUTC_ShieldToggle')
            self:CreatePersonalShield(bp)
            self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0)
            self:SetMaintenanceConsumptionActive()
        elseif enh == 'ShieldGeneratorField' then
            self:DestroyShield()
            ForkThread(function()
                WaitTicks(1)
                self:CreateShield(bp)
                self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0)
                self:SetMaintenanceConsumptionActive()
            end)
        elseif enh =='AdvancedEngineering' then
            local cat = ParseEntityCategory(bp.BuildableCategoryAdds)
            self:RemoveBuildRestriction(cat)
            if TVISACTIVE then
		          if not Buffs['UEFACUT2BuildRate'] then
	                BuffBlueprint {
	                    Name = 'UEFACUT2BuildRate',
	                    DisplayName = 'UEFACUT2BuildRate',
	                    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
              if not Buffs['UEFACUT2BuildRate'] then
	                BuffBlueprint {
	                    Name = 'UEFACUT2BuildRate',
	                    DisplayName = 'UEFACUT2BuildRate',
	                    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, 'UEFACUT2BuildRate')
        elseif enh =='T3Engineering' then
            if Buff.HasBuff( self, 'UEFACUT2BuildRate' ) then
                Buff.RemoveBuff( self, 'UEFACUT2BuildRate' )
            end
            local cat = ParseEntityCategory(bp.BuildableCategoryAdds)
            self:RemoveBuildRestriction(cat)
            if TVISACTIVE then
	            if not Buffs['UEFACUT3BuildRate'] then
	                BuffBlueprint {
	                    Name = 'UEFACUT3BuildRate',
	                    DisplayName = 'UEFCUT3BuildRate',
	                    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
	          else
	          	if not Buffs['UEFACUT3BuildRate'] then
                BuffBlueprint {
                    Name = 'UEFACUT3BuildRate',
                    DisplayName = 'UEFCUT3BuildRate',
                    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, 'UEFACUT3BuildRate')
        elseif enh =='DamageStablization' then
        		if TVISACTIVE then
	        		if not Buffs['UEFACUDamageStablization'] then
	                BuffBlueprint {
	                    Name = 'UEFACUDamageStablization',
	                    DisplayName = 'UEFACUDamageStablization',
	                    BuffType = 'ACUREGEN',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        Regen = {
	                            Add = bp.NewRegenRate,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'UEFACUDamageStablization')
	          else
	          	self:SetRegenRate(bp.NewRegenRate)
	         	end
        #ResourceAllocation              
        elseif enh == 'ResourceAllocation' then
        	if TVISACTIVE then
	        	if not Buffs['UefACUResourceAllocation'] then
	                BuffBlueprint {
	                    Name = 'UefACUResourceAllocation',
	                    DisplayName = 'UefACUResourceAllocation',
	                    BuffType = 'UefACUResourceAllocation',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        MassProductionBuf = {
	                            Add =  bp.ProductionPerSecondMass,
	                            Mult = 1,
	                        },
	                        EnergyProductionBuf = {
	                            Add = bp.ProductionPerSecondEnergy,
	                            Mult = 1,
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'UefACUResourceAllocation')
	        else
	          local bpEcon = self:GetBlueprint().Economy
            self:SetProductionPerSecondEnergy(bp.ProductionPerSecondEnergy + bpEcon.ProductionPerSecondEnergy or 0)
            self:SetProductionPerSecondMass(bp.ProductionPerSecondMass + bpEcon.ProductionPerSecondMass or 0)
          end
		########################### Heavy Bolter L  ##################### LCH #########       
        elseif enh =='HeavyBolterLUpgrade' then
					self:SetWeaponEnabledByLabel('HeavyPlasma01', true)
					self:SetWeaponEnabledByLabel('LeftZephyr', false)
		########################### Heavy Bolter R  ##################### RCH #########
        elseif enh =='HeavyBolterRUpgrade' then
					self:SetWeaponEnabledByLabel('HeavyPlasma02', true)
					self:SetWeaponEnabledByLabel('RightZephyr', false) 
					self:SetWeaponEnabledByLabel('OverCharge', false)
       
        #ResourceAllocation              
        elseif enh == 'ResourceAllocationAdvance' then
        	if TVISACTIVE then
	        	if Buff.HasBuff( self, 'UefACUResourceAllocation' ) then
	                Buff.RemoveBuff( self, 'UefACUResourceAllocation' )
	          end
	          if not Buffs['ResourceAllocationAdvance'] then
	                BuffBlueprint {
	                    Name = 'ResourceAllocationAdvance',
	                    DisplayName = 'ResourceAllocationAdvance',
	                    BuffType = 'UefACUResourceAllocation',
	                    Stacks = 'ALWAYS',
	                    Duration = -1,
	                    Affects = {
	                        MassProductionBuf = {
	                            Add =  bp.ProductionPerSecondMass,
	                            Mult = 1,
	                        },
	                        EnergyProductionBuf = {
	                            Add = bp.ProductionPerSecondEnergy,
	                            Mult = 1.0,
	                        },
	                    },
	                }
	            end
	            Buff.ApplyBuff(self, 'ResourceAllocationAdvance')
	         else
	          local bpEcon = self:GetBlueprint().Economy
            self:SetProductionPerSecondEnergy(bp.ProductionPerSecondEnergy + bpEcon.ProductionPerSecondEnergy or 0)
            self:SetProductionPerSecondMass(bp.ProductionPerSecondMass + bpEcon.ProductionPerSecondMass or 0)
           end
		########################### Heavy Over Mega Canon R  ##################### RCH #########
        elseif enh =='HeavyOverMegaCanonRUpgrade' then
						self:SetWeaponEnabledByLabel('Megacannon02', true)
		########################### Heavy Over Mega Canon L  ##################### RCH #########
        elseif enh =='HeavyOverMegaCanonLUpgrade' then
						self:SetWeaponEnabledByLabel('Megacannon01', true)
						
				elseif enh =='HeavyOverMegaCanonRUpgrade2' then
						self:SetWeaponEnabledByLabel('Megacannon04', true)
		    elseif enh =='HeavyOverMegaCanonLUpgrade2' then
						self:SetWeaponEnabledByLabel('Megacannon03', true)
    ########################### Heavy Over Mega Canon L  ##################### RCH #########
        elseif enh =='HeavyAntiAirRUpgrade' then
			self:SetWeaponEnabledByLabel('MissileRack01', true)	
        elseif enh =='HeavyAntiAirLUpgrade' then
			self:SetWeaponEnabledByLabel('MissileRack02', true)	
		########################### Health  ##################### BCK #########
		elseif enh == 'Health' then
			if not Buffs['VeterancyMegaHealth6'] then
				BuffBlueprint {
					Name = 'VeterancyMegaHealth6',
					DisplayName = 'VeterancyMegaHealth6',
					BuffType = 'VETERANCYHEALTH',
					Stacks = 'ALWAYS',
					Duration = -1,
					Affects = {
	                MaxHealth = {
		                Add = bp.NewHealth,
		                Mult = 1.0,
	                        },
					},
				}
			end
			Buff.ApplyBuff(self, 'VeterancyMegaHealth6')
##MINIGUNS BEGIN
		elseif enh =='LeftMinigun' then
			self:SetWeaponEnabledByLabel('Minigun01', true)
    elseif enh =='RightMinigun' then
			self:SetWeaponEnabledByLabel('Minigun02', true)
##MINIGUNS END			
###BUILTBYTIER4COMMANDER
				elseif enh =='T4Engineering' then
         local cat = ParseEntityCategory(bp.BuildableCategoryAdds)
         self:RemoveBuildRestriction(cat)
         if TVISACTIVE then
	          if Buff.HasBuff( self, 'UEFACUT3BuildRate' ) then
                Buff.RemoveBuff( self, 'UEFACUT3BuildRate' )
            end
            if not Buffs['UEFACUT4BuildRate'] then
	                BuffBlueprint {
	                    Name = 'UEFACUT4BuildRate',
	                    DisplayName = 'UEFACUT4BuildRate',
	                    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
	          else
	          	if not Buffs['UEFACUT4BuildRate'] then
                BuffBlueprint {
                    Name = 'UEFACUT4BuildRate',
                    DisplayName = 'UEFACUT4BuildRate',
                    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, 'UEFACUT4BuildRate')
	##BUILTBYTIER4COMMANDER END
##END ENH
        end
    end,
    
    OnPaused = function(self)
        TWalkingLandUnit.OnPaused(self)
        if self.BuildingUnit then
            TWalkingLandUnit.StopBuildingEffects(self, self:GetUnitBeingBuilt())
        end    
    end,
    
    OnUnpaused = function(self)
        if self.BuildingUnit then
            TWalkingLandUnit.StartBuildingEffects(self, self:GetUnitBeingBuilt(), self.UnitBuildOrder)
        end
        TWalkingLandUnit.OnUnpaused(self)
    end,      

}

TypeClass = UEL0001