#****************************************************************************
#**
#**  File     :  /lua/unit.lua
#**  Author(s):  John Comes, David Tomandl, Gordon Duclos
#**
#**  Summary  : The Unit lua module
#**
#**  Copyright © 2005 Gas Powered Games, Inc.  All rights reserved.
#****************************************************************************

local oldUnit=Unit
Unit = Class(oldUnit) {
    #Check to see if we should veteran up.
    CheckVeteranLevel = function(self)
    	if not self.XPnextLevel then return end
      	while self.xp >= self.XPnextLevel do
            local bp = self:GetBlueprint()
            if not bp then return end
            self.xp = self.xp - self.XPnextLevel
						self:SetVeteranLevel(self.VeteranLevel + 1)
			
						if EntityCategoryContains(categories.COMMAND,self) then
								self.XPnextLevel = bp.Economy.XPperLevel * (1.3*self.VeteranLevel)
			      else 
			      		self.XPnextLevel = bp.Economy.XPperLevel * (1+ 0.1*self.VeteranLevel)
			      end	
						
       end
       self.LevelProgress = self.xp / self.XPnextLevel + self.VeteranLevel
       self.Sync.LevelProgress = self.LevelProgress
    end,

		OnWorkBegin = function(self, work)
        local unitEnhancements = import('/lua/enhancementcommon.lua').GetEnhancements(self:GetEntityId())
        local bp = self:GetBlueprint()
        if not bp then return false end
        local tempEnhanceBp = bp.Enhancements[work]
        local iscommander = false
        if EntityCategoryContains(categories.COMMAND,self) then
					iscommander = true
    		end
    		
        if tempEnhanceBp.Prerequisite then
            if unitEnhancements[tempEnhanceBp.Slot] != tempEnhanceBp.Prerequisite and not iscommander then
                error('*ERROR: Ordered enhancement does not have the proper prereq!', 2)
                return false
            end
        elseif unitEnhancements[tempEnhanceBp.Slot] and not iscommander then
            error('*ERROR: Ordered enhancement does not have the proper slot available!', 2)
            return false
        end
        self.WorkItem = tempEnhanceBp
        self.WorkItemBuildCostEnergy = tempEnhanceBp.BuildCostEnergy
        self.WorkItemBuildCostMass = tempEnhanceBp.BuildCostEnergy
        self.WorkItemBuildTime = tempEnhanceBp.BuildTime
        self.WorkProgress = 0
        self:SetActiveConsumptionActive()
        self:PlayUnitSound('EnhanceStart')
        self:PlayUnitAmbientSound('EnhanceLoop')
        self:UpdateConsumptionValues()
        self:CreateEnhancementEffects(work)
        ChangeState(self,self.WorkingState)
    end,
}