do
local oldUnit = Unit
Unit = Class(oldUnit) {

------------------------------------------------------------------------------------------
--
--  File     :  /lua/unit.lua
--
--  Author(s):  EbolaSoup, Resin Smoker, Optimus Prime, Vissroid, Domino
--
--  Summary  :  Detects the layer trasition and adjusts the units speed accordingly
--    
--  Copyright © 2011 4DC_V0.8  All rights reserved.
------------------------------------------------------------------------------------------   

    OnLayerChange = function(self, new, old)
        if self:GetBlueprint().Physics.MotionType == 'RULEUMT_Hover' then         
            if new == 'Water' then
                -- Adds 50% speed boost
                self:SetSpeedMult(1.5)
            else
                -- Unit speed returns to normal
                self:SetSpeedMult(1.0)
            end                                   
        end
        oldUnit.OnLayerChange(self, new, old) 
    end,
    
------------------------------------------------------------------------------------------
--
--  File     :  /lua/unit.lua
--
--  Author(s):  Resin_Smoker
--
--  Summary  :  Scripts added for rail gun weapons. 
--    
--  Copyright © 2010 4DC_V0.8  All rights reserved.
------------------------------------------------------------------------------------------  
      
    OnCollisionCheck = function(self, other, firingWeapon)
        if other.LastImpact and other.LastImpact == self:GetEntityId() then
            -- LOG('Unit.lua, Hit the same unit twice')
            return false
        else
            -- return oldUnit.OnCollisionCheck(self, other, firingWeapon) Commented out for testing
            return true             
        end
        oldUnit.OnCollisionCheck(self, other, firingWeapon)     
    end,       
    
------------------------------------------------------------------------------------------
--
--  File     :  /lua/unit.lua
--
--  Author(s):  Resin_Smoker
--
--  Summary  :  SHIELD Scripts required for drone spawned bubble shields.
--
--  Note     :  Not currently used but left in for later possible use 
--    
--  Copyright © 2010 4DC_V0.8  All rights reserved.
------------------------------------------------------------------------------------------  

    SpawnDomeShield = function(self) 
--[[
        if not self:IsDead() then    
            -- Get units bp 
            local bp = self:GetBlueprint() 
            
            -- Initialize variables 
            local sldArea, offSet, sldHealth            
            local x, y, z = self:GetUnitSizes()   
            
            self.XzySize = {}      
            table.insert(self.XzySize, x)
            table.insert(self.XzySize, y)   
            table.insert(self.XzySize, z)                                       
            table.sort(self.XzySize)
            LOG('Table: ', self.XzySize[3])
            -- Get the surface area of a Rectangle 2(L*W+L*H+W*H)       
           # sldArea = ((bp.SizeX * bp.SizeZ) + (bp.SizeX * bp.SizeY) + (bp.SizeZ * bp.SizeY )) * 1.8     
           # sldArea = ((bp.SizeX * bp.SizeZ) + (bp.SizeX * bp.SizeY) + (bp.SizeZ * bp.SizeY )) * 1.412                 
           
            sldArea = 1.25 * (self.XzySize[3] * 1.4142) #diagonal of a square
            offSet = bp.SizeY * 0.5 #need to figure the distance the units main bone is above the ground and apply this as the offset, plus 1/2 the Y axis.        
            
            LOG('BP Sizes X Y Z: ', bp.SizeX, bp.SizeY, bp.SizeZ)
            LOG('Unit Sizes X Y Z: ', x, y, z)

            LOG('SLD AREA1: ', sldArea)           
            -- Rescale the shield if sizes get ridiculous
            #if sldArea <= 5 then
            #    offSet = bp.SizeY or 1            
            #elseif sldArea > 5 and sldArea < 10 then
            #    sldArea = sldArea * 0.75
            #    offSet = bp.SizeY or 0.75 
            #elseif sldArea > 10 and sldArea < 20 then
            #    sldArea = sldArea * 0.5
            #    offSet = bp.SizeY or 0.5                 
            #elseif sldArea > 20 then
            #    sldArea = sldArea * 0.25
            #    offSet = bp.SizeY or 0.25                               
            #end   
            
            # LOG('SLD AREA2: ', sldArea)
                                                                                 
            -- Sets the bubble shields hitpoints 
            if table.find(bp.Categories ,'EXPERIMENTAL') then                 
                if self:GetMaxHealth() * 0.1 >= 1000 then 
                    sldHealth = self:GetMaxHealth() * 0.1
                else
                    sldHealth = 5000
                end                              
            else
                sldHealth = 2000    
            end                    
                    
            -- Adjust the energy cost to match the shield strength 
            local eCost = 10 * (sldHealth / 1000)
           -- self:SetMaintenanceConsumptionInactive() 
           -- self:SetEnergyMaintenanceConsumptionOverride(eCost) 
           -- self:SetConsumptionPerSecondEnergy(eCost)             

            -- Adds the shield toggle to the shielded unit 
          --  self:AddToggleCap('RULEUTC_ShieldToggle') 
            
			--[[
            -- Remove the old shield if any 
            if self.MyShield then
                self:DestroyShield()                                            
            end

            -- Create the shield on the unit 
            self.MyShield = Shield { 
                Owner = self,       
                Mesh = '/effects/entities/AeonShield01/AeonShield01_mesh',                                        
                MeshZ = '/effects/entities/Shield01/Shield01z_mesh',                            
                ImpactMesh = '/effects/entities/ShieldSection01/ShieldSection01_mesh',                        
                ImpactEffects = 'AeonShieldHit01',                                 
                Size = sldArea,                                
                ShieldMaxHealth = sldHealth,
                ShieldRechargeTime = 10,
                ShieldEnergyDrainRechargeTime = 10,
                ShieldVerticalOffset = -offSet * 0.706,                 
                ShieldRegenRate = sldHealth * 0.025,
                ShieldRegenStartTime =  1,                                               
                PassOverkillDamage = false, 
				ShieldRadius = 16, 
				Resizable = true,
            } 
            
            -- Activate the bubble shield
            self:SetFocusEntity(self.MyShield) 
            self:EnableShield() 
            self.Trash:Add(self.MyShield) 
--]]
			local spec = {
				Owner = self,       
                Mesh = '/effects/entities/AeonShield01/AeonShield01_mesh',                                        
                MeshZ = '/effects/entities/Shield01/Shield01z_mesh',                            
                ImpactMesh = '/effects/entities/ShieldSection01/ShieldSection01_mesh',                        
                ImpactEffects = 'AeonShieldHit01',                                 
                Size = sldArea,                                
                ShieldMaxHealth = sldHealth,
                ShieldRechargeTime = 10,
                ShieldEnergyDrainRechargeTime = 10,
                ShieldVerticalOffset = -offSet * 0.706,                 
                ShieldRegenRate = sldHealth * 0.025,
                ShieldRegenStartTime =  1,                                               
                PassOverkillDamage = false, 
				
				--------------------------------------------------
				MaintenanceConsumptionPerSecondEnergy = eCost,
				--ShieldRadius = sldArea, 
				--Resizable = true,
			}

			self:CreateShield(spec)
        end   
		--]]
		
			local spec = {
				Owner = self,       
                Mesh = '/effects/entities/AeonShield01/AeonShield01_mesh',                                        
                MeshZ = '/effects/entities/Shield01/Shield01z_mesh',                            
                ImpactMesh = '/effects/entities/ShieldSection01/ShieldSection01_mesh',                        
                ImpactEffects = 'AeonShieldHit01',                                 
                Size = sldArea,                                
                ShieldMaxHealth = sldHealth,
                ShieldRechargeTime = 10,
                ShieldEnergyDrainRechargeTime = 10,
                ShieldVerticalOffset = -offSet * 0.706,                 
                ShieldRegenRate = sldHealth * 0.025,
                ShieldRegenStartTime =  1,                                               
                PassOverkillDamage = false, 
				
				--------------------------------------------------
				MaintenanceConsumptionPerSecondEnergy = eCost,
				--ShieldRadius = sldArea, 
				--Resizable = true,
			}

			self:CreateShield(spec)
		
    end,   

------------------------------------------------------------------------------------------
--
--  File     :  /lua/unit.lua
--
--  Author(s):  Resin_Smoker, Domino
--
--  Summary  :  SHIELD Scripts required for drone spawned personal shields.
--
--  Note     :  This script enables a drone to enhance MLU's (Moble Land Units) 
--              and Structures with shields. The type of shield and enhancement
--              strength given a unit is determined by that units type and tech.
--    
--  Copyright © 2011 4DC_V0.8  All rights reserved.
------------------------------------------------------------------------------------------   
 
    SpawnPersonalShield  = function(self) 

        if not self:IsDead() then 
                               
            -- Get units bp and Initialize variables
            local bp = self:GetBlueprint() 
            local bpDisplay = bp.Display            
            local sldHealth, rChgTime, eCost, impactFx                         
            
            -- Sets the shield hitpoints by tech level 
            sldHealth = self:GetMaxHealth()
            if table.find(bp.Categories ,'EXPERIMENTAL') then 
                if sldHealth * 0.250 >= 10000 then 
                    sldHealth = sldHealth * 0.250                   
                else
                    sldHealth = 10000
                end               
                rChgTime = 120 
                eCost = (sldHealth * 0.001)                                   
            elseif table.find(bp.Categories ,'TECH3') then 
                sldHealth = sldHealth * 0.333 
                rChgTime = 90
                eCost = 30                                                                                    
            elseif table.find(bp.Categories ,'TECH2') then 
                sldHealth = sldHealth * 0.500
                rChgTime = 60 
                eCost = 10                                                          
            else
                rChgTime = 30
                eCost = 5           
            end  
            
            -- Adjust the energy cost to match the shield strength             
            if bp.Economy.MaintenanceConsumptionPerSecondEnergy > 0 then
                eCost = eCost + bp.Economy.MaintenanceConsumptionPerSecondEnergy
            end
            			
            -- Adds the shield toggle to the shielded unit 
            self:AddToggleCap('RULEUTC_ShieldToggle')               
            
            -- Remove the old shield if any 
            if self.MyShield then
                self:DestroyShield()                                            
            end 
            
            -- Set the Impact FX based upon faction type
            if table.find(bp.Categories, 'AEON') then 
                impactFx = 'AeonShieldHit01'               
            elseif table.find(bp.Categories, 'CYBRAN') then 
                impactFx = 'CybranShieldHit01'                                          
            elseif table.find(bp.Categories, 'UEF') then 
                impactFx = 'UEFShieldHit01'    
            else 
                impactFx = 'SeraphimShieldHit01'                                                                                                                                             
            end            
			
            -- Define the shield spec
			local spec = {
				Owner = self,
                ImpactEffects = impactFx,                     
                CollisionSizeX = bp.SizeX * 25.0 or 1,
                CollisionSizeY = bp.SizeY * 25.0 or 1,
                CollisionSizeZ = bp.SizeZ * 25.0 or 1,
                CollisionCenterX = bp.CollisionOffsetX or 0,
                CollisionCenterY = bp.CollisionOffsetY or 0,
                CollisionCenterZ = bp.CollisionOffsetZ or 0,
                OwnerShieldMesh = bpDisplay.PShieldMeshBlueprint,
                RegenAssistMult = 60,
                ShieldMaxHealth = sldHealth,
                ShieldRechargeTime = rChgTime,
                ShieldEnergyDrainRechargeTime = 5,
                ShieldRegenRate = sldHealth * 0.01,
                ShieldRegenStartTime =  1,
                PassOverkillDamage = false, 
				
				--------------------------------------------------
				MaintenanceConsumptionPerSecondEnergy = eCost,
			}
			
			-- Create the personal shield
			self:CreatePersonalShield(spec)	
        end   
    end,    

------------------------------------------------------------------------------------------
--
--  File     :  /lua/unit.lua
--
--  Author(s):  Resin_Smoker, Domino
--
--  Summary  :  This script snippet is required to be within the /lua/sim/Units.lua. 
--              This is borrowed from the CBFP. It adds a unit event that's fired when 
--              the unit is picked up by a transport or when it's dropped from one.
--
--  Note     :  TRANSPORTING Scripts required for the Advanced Jamming mod, copied
--              copied from the community bug fix patch v3, slightly edited.
--    
--  Copyright © 2011 4DC_V0.8  All rights reserved.
------------------------------------------------------------------------------------------

    OnTransportAttach = function(self, attachBone, unit)
        oldUnit.OnTransportAttach(self, attachBone, unit)
        unit:OnAttachedToTransport(self)
    end,
    
    OnAttachedToTransport = function(self, transport)
    end,   

    OnTransportDetach = function(self, attachBone, unit)
        oldUnit.OnTransportDetach(self, attachBone, unit)
        unit:OnDetachedToTransport(self)
    end,
    
    OnDetachedToTransport = function(self, transport)
    end,     

}
end     