--
-- Specialization Don
--
-- @author  	Aluha74rus
-- @version 	v1.0
-- @date  		15/02/20

Don = {};
local Don_directory = g_currentModDirectory;


function Don.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(FillUnit, specializations) and SpecializationUtil.hasSpecialization(Dischargeable, specializations)
end;

function Don.registerEventListeners(vehicleType)	
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", Don);
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", Don)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", Don)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", Don)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", Don)
end

function Don:onRegisterActionEvents(isSelected, isActiveForInput)
	if isActiveForInput then
		if self.Don == nil then 
			self.Don = {}
		else	
			self:clearActionEventsTable( self.Don )
		end 
		for _,actionName in pairs({ "AXIS_LEFT_DOOR", "AXIS_RIGHT_DOOR"} ) do
			local _, eventName = self:addActionEvent(self.Don, InputAction[actionName], self, Don.actionCallback, true, true, false, true, nil);
			if isSelected then
				g_inputBinding.events[eventName].displayPriority = 1
			elseif  isActiveForInput then
				g_inputBinding.events[eventName].displayPriority = 3
			end
		end
	end
end

function Don.registerFunctions(vehicleType)
    SpecializationUtil.registerFunction(vehicleType, "playCabAnim",         Don.playCabAnim)
    SpecializationUtil.registerFunction(vehicleType, "updateGearboxHandle", Don.updateGearboxHandle)
end

function Don:onLoad(savegame)
        self.massaLights = {}
        self.massaLights.lightShaderNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.massaLights.light#shaderNode"))
        self.massaLights.intensity = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.lights.massaLights.light#intensity")) or 1000
        self.massaLights.lightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.massaLights.light#node"))
        self.massaLights.realLightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.massaLights.realLight#node"))
        if self.massaLights.realLightNode ~= nil then
            light.defaultColor = {getLightColor(self.massaLights.realLightNode)}
            setVisibility(realLightNode, false)
        end
        if self.massaLights.lightNode ~= nil then
            setVisibility(self.massaLights.lightNode, false)
        end
		self.massaLightsAktive = false
		
        self.bunkerUnloadLights = {}
        self.bunkerUnloadLights.lightShaderNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.bunkerUnloadLights.light#shaderNode"))
        self.bunkerUnloadLights.intensity = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.lights.bunkerUnloadLights.light#intensity")) or 1000
        self.bunkerUnloadLights.lightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.bunkerUnloadLights.light#node"))
        self.bunkerUnloadLights.realLightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.bunkerUnloadLights.realLight#node"))
        if self.bunkerUnloadLights.realLightNode ~= nil then
            light.defaultColor = {getLightColor(self.bunkerUnloadLights.realLightNode)}
            setVisibility(realLightNode, false)
        end
        if self.bunkerUnloadLights.lightNode ~= nil then
            setVisibility(self.bunkerUnloadLights.lightNode, false)
        end
		self.bunkerUnloadLightsAktive = false
		
        self.threshingStartLights = {}
        self.threshingStartLights.lightShaderNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.threshingStartLights.light#shaderNode"))
        self.threshingStartLights.intensity = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.lights.threshingStartLights.light#intensity")) or 1000
        self.threshingStartLights.lightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.threshingStartLights.light#node"))
        self.threshingStartLights.realLightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.lights.threshingStartLights.realLight#node"))
        if self.threshingStartLights.realLightNode ~= nil then
            light.defaultColor = {getLightColor(self.threshingStartLights.realLightNode)}
            setVisibility(realLightNode, false)
        end
        if self.threshingStartLights.lightNode ~= nil then
            setVisibility(self.threshingStartLights.lightNode, false)
        end
		self.threshingStartLightsAktive = false	
		
	if self.isClient then
	    self.sampleRucnik = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "Rucnik", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetStart = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetStart", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetEnd = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetEnd", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetWorkStart = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetWorkStart", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetWorkEnd = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetWorkEnd", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetWorkBackStart = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetWorkBackStart", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetWorkBackEnd = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetWorkBackEnd", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetAwStart = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetAwStart", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleKLSwetAwEnd = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "KLSwetAwEnd", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleWarningShnek = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "WarningShnek", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleWarningBunker = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "WarningBunker", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleWarningBunkerBip = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.don.sounds", "WarningBunkerBip", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
        self.wsCleaningTimer = 20
        self.wsotCleaningTime = 140
        self.isWarningBunkerSamplePlaying = false
        self.isWarningBunkerBipSamplePlaying = false
	end
    
    local baseString = string.format("vehicle.don.");
	self.pust = I3DUtil.indexToObject(self.components, "0>0|4|0|0|4");	
	self.pust1 = I3DUtil.indexToObject(self.components, "0>0|4|0|0|1");	
	self.pust2 = I3DUtil.indexToObject(self.components, "0>0|4|0|0|5");	
	self.pust3 = I3DUtil.indexToObject(self.components, "0>0|4|0|0|6");
	
    self.startCutterControlMaxTimeout = 2000
    self.startCutterControlMaxTime = 0
    self.startCutterControlMinTimeout = 2000
    self.startCutterControlMinTime = 0
    self.KlawishaBunker = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.don.KlawishaBunker#index"));
    self.KlawishaMaxNaklonka = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.don.KlawishaMaxNaklonka#index"));
    self.KlawishaMinNaklonka = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.don.KlawishaMinNaklonka#index"));
    self.KlawishaGazStart =  Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.don.KlawishaGazStart#name"), "KlawishaGazStart")
    self.KlawishaStart =  Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.don.KlawishaStart#name"), "KlawishaStart")
	
    self.cabAnims = {};
	
    if hasXMLProperty(self.xmlFile, "vehicle.don.cabAnims") then
        self.cabAnims.handbrake = getXMLString(self.xmlFile,baseString.."cabAnims.mainAnimations#handbrake");
        self.cabAnims.handbrakeTimer = 0;
        self.cabAnims.klawishaUnload = getXMLString(self.xmlFile,baseString.."cabAnims.mainAnimations#klawishaUnload");
        self.cabAnims.knopkaMassa = getXMLString(self.xmlFile,baseString.."cabAnims.mainAnimations#knopkaMassa");
        
        self.cabAnims.gearbox = {};
        i=0;
        while true do
            self.cabAnims.gearbox[i] = getXMLString(self.xmlFile,baseString..string.format("cabAnims.mainAnimations.gearbox#animName%d", i));
            if self.cabAnims.gearbox[i] == nil then
                break;
            end;
            i=i+1;
        end;
		
        self.cabAnims.door = {};
        i=0;
        while true do
            self.cabAnims.door[i] = getXMLString(self.xmlFile,baseString..string.format("cabAnims.mainAnimations.door#animName%d", i));
            if self.cabAnims.door[i] == nil then
                break;
            end;
            i=i+1;
        end;
        
        self.cabAnims.lights = {};
        i=0;
        while true do
            self.cabAnims.lights[i] = getXMLString(self.xmlFile,baseString..string.format("cabAnims.mainAnimations.lights#animName%d", i));
            if self.cabAnims.lights[i] == nil then
                break;
            end;
            i=i+1;
        end;
        
        self.cabAnims.beaconLights = {};
        i=0;
        while true do
            self.cabAnims.beaconLights[i] = getXMLString(self.xmlFile,baseString..string.format("cabAnims.mainAnimations.beaconLights#animName%d", i));
            if self.cabAnims.beaconLights[i] == nil then
                break;
            end;
            i=i+1;
        end;
    end;
	
    local klawishaPoworotNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.don.klawishaPoworot#node"));
    if klawishaPoworotNode ~= nil then
        self.klawishaPoworot = {};
        self.klawishaPoworot.node = klawishaPoworotNode;
        local x, y, z = StringUtil.getVectorFromString(getXMLString(self.xmlFile, "vehicle.don.klawishaPoworot#minRot"));
        self.klawishaPoworot.minRot = {};
        self.klawishaPoworot.minRot[1] = MathUtil.degToRad(Utils.getNoNil(x, 0));
        self.klawishaPoworot.minRot[2] = MathUtil.degToRad(Utils.getNoNil(y, 0));
        self.klawishaPoworot.minRot[3] = MathUtil.degToRad(Utils.getNoNil(z, 0));

        local x, y, z = StringUtil.getVectorFromString(getXMLString(self.xmlFile, "vehicle.don.klawishaPoworot#rotLeft"));
        self.klawishaPoworot.leftRot = {};
        self.klawishaPoworot.leftRot[1] = MathUtil.degToRad(Utils.getNoNil(x, 0));
        self.klawishaPoworot.leftRot[2] = MathUtil.degToRad(Utils.getNoNil(y, 0));
        self.klawishaPoworot.leftRot[3] = MathUtil.degToRad(Utils.getNoNil(z, 0));

        local x, y, z = StringUtil.getVectorFromString(getXMLString(self.xmlFile, "vehicle.don.klawishaPoworot#rotRight"));
        self.klawishaPoworot.rightRot = {};
        self.klawishaPoworot.rightRot[1] = MathUtil.degToRad(Utils.getNoNil(x, 0));
        self.klawishaPoworot.rightRot[2] = MathUtil.degToRad(Utils.getNoNil(y, 0));
        self.klawishaPoworot.rightRot[3] = MathUtil.degToRad(Utils.getNoNil(z, 0));

        self.klawishaPoworot.rotTime = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.don.klawishaPoworot#rotTime"), 1)*1000;
		self.klawishaPoworot.lastDir = 1;
    end;
	
end;

function Don:onDelete()
    if self.isClient then
		g_soundManager.deleteSample(self.sampleRucnik);
		g_soundManager.deleteSample(self.sampleKLSwetStart);
		g_soundManager.deleteSample(self.sampleKLSwetEnd);
		g_soundManager.deleteSample(self.sampleKLSwetWorkStart);
		g_soundManager.deleteSample(self.sampleKLSwetWorkEnd);
		g_soundManager.deleteSample(self.sampleKLSwetWorkBackStart);
		g_soundManager.deleteSample(self.sampleKLSwetWorkBackEnd);
		g_soundManager.deleteSample(self.sampleKLSwetAwStart);
		g_soundManager.deleteSample(self.sampleKLSwetAwEnd);
		g_soundManager.deleteSample(self.sampleWarningShnek);
		g_soundManager.deleteSample(self.sampleWarningBunker);
		g_soundManager.deleteSample(self.sampleWarningBunkerBip);
    end
end
function Don:onUpdate(dt, isActiveForInput, isSelected)
    if self.massaLightsAktive then
        local value = 1
        local _,y,z,w = getShaderParameter(self.massaLights.lightShaderNode, "lightControl")
        setShaderParameter(self.massaLights.lightShaderNode, "lightControl", math.max(value*self.massaLights.intensity, 1), y, z, w, false)
    elseif not self.massaLightsAktive then
        value = 0
        local _,y,z,w = getShaderParameter(self.massaLights.lightShaderNode, "lightControl")
        setShaderParameter(self.massaLights.lightShaderNode, "lightControl", math.max(value*self.massaLights.intensity, 0), y, z, w, false)
    end;
    if self.bunkerUnloadLightsAktive then
        local value = 1
        local _,y,z,w = getShaderParameter(self.bunkerUnloadLights.lightShaderNode, "lightControl")
        setShaderParameter(self.bunkerUnloadLights.lightShaderNode, "lightControl", math.max(value*self.bunkerUnloadLights.intensity, 1), y, z, w, false)
    elseif not self.massaLightsAktive then
        value = 0
        local _,y,z,w = getShaderParameter(self.bunkerUnloadLights.lightShaderNode, "lightControl")
        setShaderParameter(self.bunkerUnloadLights.lightShaderNode, "lightControl", math.max(value*self.bunkerUnloadLights.intensity, 0), y, z, w, false)
    end;
    if self.threshingStartLightsAktive then
        local value = 1
        local _,y,z,w = getShaderParameter(self.threshingStartLights.lightShaderNode, "lightControl")
        setShaderParameter(self.threshingStartLights.lightShaderNode, "lightControl", math.max(value*self.threshingStartLights.intensity, 1), y, z, w, false)
    elseif not self.massaLightsAktive then
        value = 0
        local _,y,z,w = getShaderParameter(self.threshingStartLights.lightShaderNode, "lightControl")
        setShaderParameter(self.threshingStartLights.lightShaderNode, "lightControl", math.max(value*self.threshingStartLights.intensity, 0), y, z, w, false)
    end;
	
    local KlawishaMassaEnableTimeStartDelay = 500;
	local KlawishaMassaEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + KlawishaMassaEnableTimeStartDelay;
	
    local KlawishaStartEnableTimeStartDelay = 1570;
	local KlawishaStartEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + KlawishaStartEnableTimeStartDelay;
	local Duration = 1000;
	local DisableTime = KlawishaStartEnableTime + Duration;
	
    local massaLightsEnableTimeStartDelay = 3380;
	local massaLightsEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + massaLightsEnableTimeStartDelay;
	
    if self.isClient then
	    if self:getIsAnimationPlaying("handbrakeAnim") then
		    if self.spec_animatedVehicle.animations["handbrakeAnim"].currentSpeed < 0 and self.spec_animatedVehicle.animations["handbrakeAnim"].currentTime > 130 then
			    if not g_soundManager:playSample(self.sampleRucnik,1.5*dt) then
				    g_soundManager:playSample(self.sampleRucnik,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("lightsAnim0") then
		    if self.spec_animatedVehicle.animations["lightsAnim0"].currentSpeed > 0 and self.spec_animatedVehicle.animations["lightsAnim0"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleKLSwetStart,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetStart,1.5*dt);
			    end;
		    elseif self.spec_animatedVehicle.animations["lightsAnim0"].currentSpeed < 0 and self.spec_animatedVehicle.animations["lightsAnim0"].currentTime > 30 then
			    if not g_soundManager:playSample(self.sampleKLSwetEnd,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetEnd,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("lightsAnim2") then
		    if self.spec_animatedVehicle.animations["lightsAnim2"].currentSpeed > 0 and self.spec_animatedVehicle.animations["lightsAnim2"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkStart,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkStart,1.5*dt);
			    end;
		    elseif self.spec_animatedVehicle.animations["lightsAnim2"].currentSpeed < 0 and self.spec_animatedVehicle.animations["lightsAnim2"].currentTime > 30 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkEnd,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkEnd,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("lightsAnim4") then
		    if self.spec_animatedVehicle.animations["lightsAnim4"].currentSpeed > 0 and self.spec_animatedVehicle.animations["lightsAnim4"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkBackStart,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkBackStart,1.5*dt);
			    end;
		    elseif self.spec_animatedVehicle.animations["lightsAnim4"].currentSpeed < 0 and self.spec_animatedVehicle.animations["lightsAnim4"].currentTime > 30 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkBackEnd,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkBackEnd,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("beaconLights") then
		    if self.spec_animatedVehicle.animations["beaconLights"].currentSpeed > 0 and self.spec_animatedVehicle.animations["beaconLights"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkStart,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkStart,1.5*dt);
			    end;
		    elseif self.spec_animatedVehicle.animations["beaconLights"].currentSpeed < 0 and self.spec_animatedVehicle.animations["beaconLights"].currentTime > 30 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkEnd,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkEnd,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("klawishaUnload") then
		    if self.spec_animatedVehicle.animations["klawishaUnload"].currentSpeed > 0 and self.spec_animatedVehicle.animations["klawishaUnload"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkBackStart,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkBackStart,1.5*dt);
			    end;
		    elseif self.spec_animatedVehicle.animations["klawishaUnload"].currentSpeed < 0 and self.spec_animatedVehicle.animations["klawishaUnload"].currentTime > 30 then
			    if not g_soundManager:playSample(self.sampleKLSwetWorkBackEnd,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetWorkBackEnd,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("combineStartAnim") then
		    if self.spec_animatedVehicle.animations["combineStartAnim"].currentSpeed > 0 and self.spec_animatedVehicle.animations["combineStartAnim"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleKLSwetAwStart,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetAwStart,1.5*dt);
			    end;
		    elseif self.spec_animatedVehicle.animations["combineStartAnim"].currentSpeed < 0 and self.spec_animatedVehicle.animations["combineStartAnim"].currentTime > 130 then
			    if not g_soundManager:playSample(self.sampleKLSwetAwEnd,1.5*dt) then
				    g_soundManager:playSample(self.sampleKLSwetAwEnd,1.5*dt);
			    end;
		    end;
		end;
		
		if self.KlawishaStart ~= nil and self.playAnimation ~= nil then
		    if (g_currentMission.time <= DisableTime and g_currentMission.time >= KlawishaStartEnableTime  and self.spec_motorized.isMotorStarted) then
                self:playAnimation(self.KlawishaStart, 1, nil, true);
		    else
				self.massaLightsAktive = false
                self:playAnimation(self.KlawishaStart, -1, nil, true);
		    end;
		end;
		if (g_currentMission.time <= massaLightsEnableTime  and self.spec_motorized.isMotorStarted) then
			self.massaLightsAktive = true
		else
			self.massaLightsAktive = false
		end;
		if self:getIsTurnedOn() then
			self.threshingStartLightsAktive = true
		else
		    self.threshingStartLightsAktive = false	
		end;
		if self.cabAnims.knopkaMassa ~= nil then
		    if (g_currentMission.time <= KlawishaMassaEnableTime  and self.spec_motorized.isMotorStarted) then
                self:playCabAnim(self.cabAnims.knopkaMassa, 1);
		    else 
                self:playCabAnim(self.cabAnims.knopkaMassa, -1);
		    end;
		end;	
        
        if self.cabAnims.handbrake ~= nil then
            if (self.movingDirection == 0 or self:getLastSpeed()<0.3) or not self.spec_motorized.isMotorStarted then
                if self.cabAnims.handbrakeTimer == 0 then
                    self.cabAnims.handbrakeTimer = g_currentMission.time;
                end;
                if self.cabAnims.handbrakeTimer + 4000 < g_currentMission.time then    
                    self:playCabAnim(self.cabAnims.handbrake, -1);
                    self.cabAnims.handbrakeTimer = 0;
                end;
            else
                self.cabAnims.handbrakeTimer = 0;
                self:playCabAnim(self.cabAnims.handbrake, 1);
            end;
        end;
        
        if self.spec_combine.numAttachedCutters > 0 then
            local cutterObject = next(self.spec_combine.attachedCutters);
            if not cutterObject:getIsLowered(false) then
                setRotation(self.KlawishaMaxNaklonka, MathUtil.degToRad(5), 0, 0);
                if self.startCutterControlMaxTime > g_currentMission.time then
                    setRotation(self.KlawishaMaxNaklonka, 0, 0, 0);
                end;
            else
                self.startCutterControlMaxTime = g_currentMission.time + self.startCutterControlMaxTimeout
            end;
            if cutterObject:getIsLowered(true) then
                setRotation(self.KlawishaMinNaklonka, MathUtil.degToRad(-5), 0, 0);
                if self.startCutterControlMinTime > g_currentMission.time then
                    setRotation(self.KlawishaMinNaklonka, 0, 0, 0);
                end;
            else
                self.startCutterControlMinTime = g_currentMission.time + self.startCutterControlMinTimeout
            end;
        end;
        if self.cabAnims.gearbox ~= nil then
            if self:getIsMotorStarted() then
                if self.movingDirection > 0.5 then            
                    if self:getAnimationTime(self.cabAnims.gearbox[1]) < 1 then
                        if self:getLastSpeed() > 1 and self:getLastSpeed() < 4 then
                            self:updateGearboxHandle(self.cabAnims.gearbox[1], 1);
                        end;
                    else
                        if self:getLastSpeed() < 1 then
                            self:updateGearboxHandle(self.cabAnims.gearbox[1], -1);
                        end;
                    end;
                    if self:getAnimationTime(self.cabAnims.gearbox[2]) < 1 then
                        if self:getLastSpeed() > 5 and self:getLastSpeed() < 14 then
                            self:updateGearboxHandle(self.cabAnims.gearbox[2], 1);
                        end;
                    else
                        if self:getLastSpeed() < 5 then
                            self:updateGearboxHandle(self.cabAnims.gearbox[2], -1);
                        end;
                    end;
                    if self:getAnimationTime(self.cabAnims.gearbox[3]) < 1 then
                        if self:getLastSpeed() > 15 then
                            self:updateGearboxHandle(self.cabAnims.gearbox[3], 1);
                        end;
                    else
                        if self:getLastSpeed() < 15 then
                            self:updateGearboxHandle(self.cabAnims.gearbox[3], -1);
                        end;
                    end;
                elseif self.movingDirection < 0.5 then
                    if self:getLastSpeed() > 1 then
                        self:updateGearboxHandle(self.cabAnims.gearbox[1], -1);
                    else
                        self:updateGearboxHandle(self.cabAnims.gearbox[1], 1);
                    end;
                end;
            else
                i=0;
                while true do
                    if self.cabAnims.gearbox[i] == nil then
                        break;
                    end;
                    if self:getIsAnimationPlaying(self.cabAnims.gearbox[i]) then
                        self:stopAnimation(self.cabAnims.gearbox[i]);
                    end;
                    i=i+1;
                end;
                if self:getAnimationTime(self.cabAnims.gearbox[1]) ~= 0 then
                    self:setAnimationTime(self.cabAnims.gearbox[1], 0);
                end;
            end;
        end;
        
        if self.cabAnims.lights ~= nil then
            for i=0, self.spec_lights.numLightTypes do 
                if self.spec_lights.currentLightState > i then
					self:playCabAnim(self.cabAnims.lights[i], 1);
                else
                    self:playCabAnim(self.cabAnims.lights[i], -1);
                end;
            end;
        end;
        
        if self.cabAnims.threshingStart ~= nil then
            if self:getIsTurnedOn() then
                self:playCabAnim(self.cabAnims.threshingStart, 1);
            else
                self:playCabAnim(self.cabAnims.threshingStart, -1);
            end;
        end;
	    if self.inputRotLDwer then
            self:playCabAnim(self.cabAnims.door[0], 1);
        else
            self:playCabAnim(self.cabAnims.door[0], -1);
        end;
	    if self.inputRotPDver then
            self:playCabAnim(self.cabAnims.door[1], 1);
        else
            self:playCabAnim(self.cabAnims.door[1], -1);
        end;
    end
	if self:getIsAnimationPlaying("foldPipe") then
	    if self.spec_animatedVehicle.animations["foldPipe"].currentSpeed > 0 and self.spec_animatedVehicle.animations["foldPipe"].currentTime < 70 then
            setRotation(self.KlawishaBunker, MathUtil.degToRad(-17), 0, 0);
	    elseif self.spec_animatedVehicle.animations["foldPipe"].currentSpeed < 0 and self.spec_animatedVehicle.animations["foldPipe"].currentTime > 130 then
            setRotation(self.KlawishaBunker, MathUtil.degToRad(17), 0, 0);
	    end;
    else
        setRotation(self.KlawishaBunker, 0, 0, 0);
	end;
        
    if self.cabAnims.klawishaUnload ~= nil then
        if self.spec_dischargeable.currentDischargeState == Dischargeable.DISCHARGE_STATE_OFF then   
            self:playCabAnim(self.cabAnims.klawishaUnload, -1);
			self.bunkerUnloadLightsAktive = false
        else 
            self:playCabAnim(self.cabAnims.threshingStart, 0.7);
            self:playCabAnim(self.cabAnims.klawishaUnload, 1);
			self.bunkerUnloadLightsAktive = true
	    end;
	end;
	if self.spec_motorized.isMotorStarted  then
            self:playAnimation(self.KlawishaGazStart, 1, nil, true);
		else
            self:playAnimation(self.KlawishaGazStart, -1, nil, true);
    end
end;
function Don:onUpdateTick(dt)
    if self.isClient then
        if self.wsCleaningTimer <= g_currentMission.time and self.spec_pipe.targetState == 2 then
            g_soundManager:playSample(self.sampleWarningShnek,1.5*dt);
            self.wsCleaningTimer = g_currentMission.time + self.wsotCleaningTime
        end
	    if self:getFillUnitFillLevel(self.spec_combine.fillUnitIndex) > (self:getFillUnitCapacity(self.spec_combine.fillUnitIndex) * 0.95) then
            if not self.isWarningBunkerSamplePlaying then
                g_soundManager:playSample(self.sampleWarningBunker,1.5*dt);
                self.isWarningBunkerSamplePlaying = true
            end
        else
            if self.isWarningBunkerSamplePlaying then
                g_soundManager:stopSample(self.sampleWarningBunker)
                self.isWarningBunkerSamplePlaying = false
            end
	    end;
	    if self:getFillUnitFillLevel(self.spec_combine.fillUnitIndex) > (self:getFillUnitCapacity(self.spec_combine.fillUnitIndex) * 0.50) then
            if not self.isWarningBunkerBipSamplePlaying then
                g_soundManager:playSample(self.sampleWarningBunkerBip,1.5*dt);
                self.isWarningBunkerBipSamplePlaying = true
            end
        else
            if self.isWarningBunkerBipSamplePlaying then
                g_soundManager:stopSample(self.sampleWarningBunkerBip)
                self.isWarningBunkerBipSamplePlaying = false
            end
	    end;
	end;
    if self:getIsActive() then
        if self.spec_lights.turnLightState ~= nil then
			if self.klawishaPoworot ~= nil then
				local dir = 0;
				local hasRotChanged = false;
				local destRot = self.klawishaPoworot.minRot;
				
				if self.spec_lights.turnLightState == 1 then
					destRot = self.klawishaPoworot.leftRot;
					dir = 1;
				elseif self.spec_lights.turnLightState == 2 then
					destRot = self.klawishaPoworot.rightRot;
					dir = -1;
				end
				if self.klawishaPoworot.lastDir ~= dir then
					self.klawishaPoworot.lastDir = dir;
					self.klawishaPoworot.rotatingTime = 0;
				end
				
				if self.klawishaPoworot.rotatingTime ~= nil then
					self.klawishaPoworot.rotatingTime = self.klawishaPoworot.rotatingTime + dt;
					local t = math.min(1.0, self.klawishaPoworot.rotatingTime / self.klawishaPoworot.rotTime);
					if t == 1 then
						self.klawishaPoworot.rotatingTime = nil;
					end
					local rot = {getRotation(self.klawishaPoworot.node)};
					
					local rx1,ry1,rz1,rw1 = mathEulerToQuaternion(unpack(rot));
					local rx2,ry2,rz2,rw2 = mathEulerToQuaternion(unpack(destRot));
					
					local qx,qy,qz,qw = MathUtil.nlerpQuaternionShortestPath(rx1,ry1,rz1,rw1, rx2,ry2,rz2,rw2, t);
					setQuaternion(self.klawishaPoworot.node, qx,qy,qz,qw);
				end
			end;
            for i=0, self.spec_lights.numLightTypes do 
                if self.spec_lights.turnLightState == 3 then
                    setVisibility(self.pust, true);
                    setVisibility(self.pust1, false);
                    setVisibility(self.pust3, true);
                    setVisibility(self.pust2, false);
                else
                    setVisibility(self.pust1, true);
                    setVisibility(self.pust, false);
                    setVisibility(self.pust2, true);
                    setVisibility(self.pust3, false);
                end;
            end;
		end;
        if self.spec_lights.beaconLightsActive then
            self:playCabAnim(self.cabAnims.beaconLights[0], 1);
        else
            self:playCabAnim(self.cabAnims.beaconLights[0], -1);
		end;
	end;
end;

function Don:playCabAnim(animName, animSpeed)    
    local animTime = self:getAnimationTime(animName);
    if not self:getIsAnimationPlaying(animName) then    
        self:playAnimation(animName, animSpeed, animTime, true);
    end;
end;

function Don:updateGearboxHandle(animName, animSpeed)    
    self:playCabAnim(animName, animSpeed);
end;

function Don:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
    if self.isClient then
		if keyStatus > 0 then
			if actionName == 'AXIS_LEFT_DOOR' then
			    self.inputRotLDwer = not self.inputRotLDwer
			elseif actionName == 'AXIS_RIGHT_DOOR' then
			    self.inputRotPDver = not self.inputRotPDver
			end
		end
    end
end