-----------------------------------------------------------------------------
--  File     : /units/URL0101/URL0101_Script.lia
--
--  Author(s): EbolaSoup, Resin Smoker, Optimus Prime, Vissroid, Domino
--
--  Summary  : Cybran Land Scout Script
--
--  Copyright © 2011 4DC_V0.8  All rights reserved.
-----------------------------------------------------------------------------

local CWalkingLandUnit = import('/lua/cybranunits.lua').CWalkingLandUnit
local Entity = import('/lua/sim/Entity.lua').Entity
URL0101 = Class(CWalkingLandUnit) {

    OnStopBeingBuilt = function(self,builder,layer)
        CWalkingLandUnit.OnStopBeingBuilt(self,builder,layer)
        -- Preset unit cloaking
        self:DisableUnitIntel('Cloak')          
        -- Entity used for radar
        self.BP = self:GetBlueprint()
        self.RadarEnt = Entity {}
        self.Trash:Add(self.RadarEnt)
        self.RadarEnt:InitIntel(self:GetArmy(), 'Radar', self.BP.Intel.RadarRadius)
        self.RadarEnt:EnableIntel('Radar')
        self.RadarEnt:AttachBoneTo(-1, self, 0)
        
        -- Antena spinner
        CreateRotator(self, 'Spinner', 'y', nil, 90, 5, 90)       
        
        -- Global Booleans
        self.StealthEnabled = false
                
        -- Setup stealth and economy starting conditions
        self:SetMaintenanceConsumptionInactive()
        if self.BP.Intel.StealthStartsOn == true then
            self.ToggleStatus = true
        else
            self.ToggleStatus = false
        end  
        self:ForkThread(self.EconomyHeartBeat)  
		                
        self:RequestRefreshUI()
    end,
	
	
    
    EconomyHeartBeat = function(self)       
        local eCost = self.BP.Economy.MaintenanceConsumptionPerSecondEnergy           
        while not self:IsDead() do                                         
            if not self:IsDead() then                      
              
                -- Stealth control logic
                if self:GetAIBrain():GetEconomyStored('Energy') < eCost or self.ToggleStatus == false then
                    if self.StealthEnabled == true then        
                        self:ForkThread(self.StealthDisable)
                    end
                elseif self:GetAIBrain():GetEconomyStored('Energy') > eCost or self.ToggleStatus == true then  
                    if self.StealthEnabled == false then            
                        self:ForkThread(self.StealthEnable)                         
                    end
                end                                                                                                                                                 
            end 
            -- Short delay between checks
            WaitSeconds(1)                                     
        end
    end,    
    
    OnScriptBitSet = function(self, bit)
        -- UI Toggle that turns stealth mesh off
        if not self:IsDead() and bit == 6 then
           self.ToggleStatus = false 
           CWalkingLandUnit.OnScriptBitSet(self, bit)                             
        end
    end,     
     
    OnScriptBitClear = function(self, bit)
        -- UI Toggle that turns stealth mesh on    
        if not self:IsDead() and bit == 6 then
            self.ToggleStatus = true
            CWalkingLandUnit.OnScriptBitClear(self, bit)            
        end
    end,
    
    StealthEnable = function(self)
        if not self:IsDead() and self.StealthEnabled == false then 
            self.StealthEnabled = true 
            self:EnableUnitIntel('Cloak') 
            self:SetMesh('/mods/4DC_V0.8/hook/units/url0101/url0101_stealth_mesh', true)
            self:SetMaintenanceConsumptionActive()            
        end
    end,    
    
    StealthDisable = function(self)
        if not self:IsDead() and self.StealthEnabled == true then          
            self.StealthEnabled = false     
            self:DisableUnitIntel('Cloak') 
            self:SetMesh(self.BP.Display.MeshBlueprint, true)
            self:SetMaintenanceConsumptionInactive()                       
        end
    end,           
    
}	
TypeClass = URL0101