-- Specjalizacja dla Ursus C330 --
-- Autor: Rockstar94 --
-- 1.0: 29.12.2015 --


C330 = {};

function C330.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function C330:load(xmlFile)
	self.isSelected = true;
	self.isSelectable = true;
	
	self.Belka = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Belka#index"));
	setVisibility((self.Belka), true);
	
	self.Sruba = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Sruba#index"));
	setVisibility((self.Sruba), false);
	
	self.Swiatlo = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Swiatlo#index"));
	self.Swiatlo_1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Swiatlo_1#index"));
	swiatlo_c330 = false;
		
	self.PitchArm = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TractorAnimations.PitchArm#Index"));
	self.PitchArmRefNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TractorAnimations.PitchArm#refNode"));	
	
	local Maska_C330 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Maska#index"));
	if Maska_C330 ~= nil then
		self.Maska_C330 = Maska_C330;
	end;
	
	maska_SoundFile = Utils.getFilename("Dzwieki/Elementy/maska_otwieranie.wav", self.baseDirectory);
    self.maska_SoundId = createSample("maska");
    loadSample(self.maska_SoundId, maska_SoundFile, false);
	self.maskaPlaying = false;
	
	maska_close_SoundFile = Utils.getFilename("Dzwieki/Elementy/maska_zamykanie.wav", self.baseDirectory);
    self.maska_close_SoundId = createSample("maska_close");
    loadSample(self.maska_close_SoundId, maska_close_SoundFile, false);
	self.maska_closePlaying = false;
	
	self.ScrollParts = Utils.loadScrollers(self.components, xmlFile, "vehicle.Paski.Pasek", {}, false);
	
	self.Halogen = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Halogen#index"));
	halogen_c330 = false;
	
	local Drzwi_prawe_C330 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Drzwi_prawe#index"));
	if Drzwi_prawe_C330 ~= nil then
		self.Drzwi_prawe_C330 = Drzwi_prawe_C330;
	end;
	
	local Drzwi_lewe_C330 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Drzwi_lewe#index"));
	if Drzwi_lewe_C330 ~= nil then
		self.Drzwi_lewe_C330 = Drzwi_lewe_C330;
	end;
	
	local Pompka = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Pompka#index"));
	if Pompka ~= nil then
		self.Pompka = Pompka;
	end;
	
	local przyMaska_1 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.przyMaska_1#index"));
	if przyMaska_1 ~= nil then
		self.przyMaska_1 = przyMaska_1;
	end;
	
	local przyBelka = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.przyBelka#index"));
	if przyBelka ~= nil then
		self.przyBelka = przyBelka;
	end;
	
	drzwi_SoundFile = Utils.getFilename("Dzwieki/Elementy/otworz.wav", self.baseDirectory);
    self.drzwi_SoundId = createSample("drzwi");
    loadSample(self.drzwi_SoundId, drzwi_SoundFile, false);
	self.drzwiPlaying = false;
	
	drzwi_close_SoundFile = Utils.getFilename("Dzwieki/Elementy/zamknij.wav", self.baseDirectory);
    self.drzwi_close_SoundId = createSample("drzwi_close");
    loadSample(self.drzwi_close_SoundId, drzwi_close_SoundFile, false);
	self.drzwi_closePlaying = false;
	
	drzwi1_SoundFile = Utils.getFilename("Dzwieki/Elementy/otworz.wav", self.baseDirectory);
    self.drzwi1_SoundId = createSample("drzwi1");
    loadSample(self.drzwi1_SoundId, drzwi1_SoundFile, false);
	self.drzwi1Playing = false;
	
	drzwi1_close_SoundFile = Utils.getFilename("Dzwieki/Elementy/zamknij.wav", self.baseDirectory);
    self.drzwi1_close_SoundId = createSample("drzwi1_close");
    loadSample(self.drzwi1_close_SoundId, drzwi1_close_SoundFile, false);
	self.drzwi1_closePlaying = false;

	openSoundFile = Utils.getFilename("Dzwieki/Elementy/maska_down.wav", self.baseDirectory);
	self.openSoundId = createSample("Sample");
	loadSample(self.openSoundId, openSoundFile, false);
	self.openPlaying = false;	
	
	clouseSoundFile = Utils.getFilename("Dzwieki/Elementy/maska_up.wav", self.baseDirectory);
	self.clouseSoundId = createSample("Sample");
	loadSample(self.clouseSoundId, clouseSoundFile, false);
	self.clousePlaying = false;
	
	wycieraczkaSoundFile = Utils.getFilename("Dzwieki/Elementy/wycieraczka.wav", self.baseDirectory);
    self.wycieraczkaSoundId = createSample("LizardHor12nSound");
    loadSample(self.wycieraczkaSoundId, wycieraczkaSoundFile, false);
    self.wycieraczkaPlaying = false;
	
	self.setHydraulicTime = 30;
	self.setHydraulicDirection = SpecializationUtil.callSpecializationsFunction("setHydraulicDirection");
	
	local hydraulicsCount = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.hydraulics#count"), 0);	
	self.hydraulics = {};	
	for i=1, hydraulicsCount do
		local hydraulicName = string.format("vehicle.hydraulics.hydraulic%d", i);		
		self.hydraulics[i] = {};		
		self.hydraulics[i].node = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#index"));
		self.hydraulics[i].punch = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#punch"));
		self.hydraulics[i].translationPunch = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#punchFixpoint"));
		self.hydraulics[i].fixPoint = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#fixpoint"));	
		local xUp, yUp, zUp = Utils.getVectorFromString(Utils.getNoNil(getXMLString(xmlFile, hydraulicName .. "#upVectors"),"0 1 0"));
		self.hydraulics[i].upVectors = {xUp, yUp, zUp};
		
		local ax, ay, az;		
		if self.hydraulics[i].punch ~= nil then
			ax, ay, az = getWorldTranslation(self.hydraulics[i].punch);
		else
			ax, ay, az = getWorldTranslation(self.hydraulics[i].node);
		end;
		if self.hydraulics[i].translationPunch ~= nil then
			local bx, by, bz = getWorldTranslation(self.hydraulics[i].translationPunch);		
			self.hydraulics[i].punchDistance = Utils.vector3Length(ax-bx, ay-by, az-bz);
		end;
	end;
	
	self.reka = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.reka#index"));
	setVisibility((self.reka), true);
	
	self.Bolec = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Bolec#index"));
	setVisibility((self.Bolec), false);
	
	self.reka_act = true;
	
	self.drzwi_zamkniete = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.drzwi_zamkniete#index"));
	setVisibility((self.drzwi_zamkniete), true);
	
	self.drzwi_otwarte = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.drzwi_otwarte#index"));
	setVisibility((self.drzwi_otwarte), false);
	
	self.drzwi_zamkniete_1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.drzwi_zamkniete_1#index"));
	setVisibility((self.drzwi_zamkniete_1), true);
	
	self.drzwi_otwarte_1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.drzwi_otwarte_1#index"));
	setVisibility((self.drzwi_otwarte_1), false);
	
	self.Maska_1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Maska_1#index"));
	setVisibility((self.Maska_1), true);
	
	self.Belka_1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Belka_1#index"));
	setVisibility((self.Belka_1), true);

	self.Tur = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Tur#index"));
	setVisibility((self.Tur), false);
	
	self.maska_act = true;
	self.wycieraczka_1 = false;
	self.belka_act = true;
	self.tur_act = false;
	
	self.rotacja_drzwi = getXMLString(xmlFile, "vehicle.rotacja_drzwi#animationName");
	self.rotacja_drzwi_1 = getXMLString(xmlFile, "vehicle.rotacja_drzwi_1#animationName");
	
	self.Bolec_przedni = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Bolec_przedni#index"));
	setVisibility((self.Bolec_przedni), false);
	
	self.Tur_1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Tur_1#index"));
	setVisibility((self.Tur_1), false);
	self.tur_act1 = false;
	
	self.rotacja_osi = getXMLString(xmlFile, "vehicle.rotacja_osi#animationName");
end;

function C330:delete()
	if self.maskaPlaying ~= nil then
		delete(self.maska_SoundId)
	end;	
	if self.maska_closePlaying ~= nil then
		delete(self.maska_close_SoundId)
	end;	
	if self.drzwiPlaying ~= nil then
		delete(self.drzwi_SoundId)
	end;	
	if self.drzwi_closePlaying ~= nil then
		delete(self.drzwi_close_SoundId)
	end;	
	if self.drzwi1Playing ~= nil then
		delete(self.drzwi1_SoundId)
	end;	
	if self.drzwi1_closePlaying ~= nil then
		delete(self.drzwi1_close_SoundId)
	end;	
	
	if self.openPlaying ~= nil then
		delete(self.openSoundId)
	end;	
	
	if self.clousePlaying ~= nil then
		delete(self.clouseSoundId)
	end;	
	if self.wycieraczkaPlaying ~= nil then
		delete(self.wycieraczkaSoundId)
	end;	
end;

function C330:attachImplement(implement)
	local jointType = implement.object.attacherJoint.jointType;
	local jointIndex = implement.jointDescIndex;
	
	if jointType == Vehicle.JOINTTYPE_IMPLEMENT then
		if jointIndex == 1 then
			setVisibility((self.Belka), false);
			setVisibility((self.Sruba), true);
			setVisibility((self.Bolec), false);
			setVisibility((self.Bolec_przedni), false);
		end;
	end;
	if jointType == Vehicle.JOINTTYPE_TRAILER then
		if jointIndex == 2 then
			setVisibility((self.Belka), true);
			setVisibility((self.Sruba), false);
			setVisibility((self.Bolec), false);
			setVisibility((self.Bolec_przedni), false);
		end;
		if jointIndex == 3 then
			setVisibility((self.Belka), true);
			setVisibility((self.Sruba), false);
			setVisibility((self.Bolec), false);
			setVisibility((self.Bolec_przedni), true);
		end;
	elseif jointType == Vehicle.JOINTTYPE_TRAILERLOW then
		if jointIndex == 4 then	
			setVisibility((self.Belka), true);
			setVisibility((self.Sruba), false);
			setVisibility((self.Bolec), true);
			setVisibility((self.Bolec_przedni), false);
		end;
	end;
	if jointType == Vehicle.JOINTTYPE_FRONTLOADER then
		if jointIndex == 5 then
			self:playAnimation(self.rotacja_osi, -1, nil, true);
		end;
	end;
end;

function C330:detachImplement(implementIndex)
	local implement = self.attachedImplements[implementIndex];
	local jointIndex = implement.jointDescIndex;
	
	if jointIndex == 1 then
		setVisibility((self.Belka), true);
		setVisibility((self.Sruba), false);
		setVisibility((self.Bolec), false);
		setVisibility((self.Bolec_przedni), false);
	end;
	if jointIndex == 2 then
		setVisibility((self.Belka), true);
		setVisibility((self.Sruba), false);
		setVisibility((self.Bolec), false);
		setVisibility((self.Bolec_przedni), false);
	end
	if jointIndex == 3 then
		setVisibility((self.Belka), true);
		setVisibility((self.Sruba), false);
		setVisibility((self.Bolec), false);
		setVisibility((self.Bolec_przedni), false);
	end
	if jointIndex == 4 then
		setVisibility((self.Belka), true);
		setVisibility((self.Sruba), false);
		setVisibility((self.Bolec), false);
		setVisibility((self.Bolec_przedni), false);
	end;
	if jointIndex == 5 then
		self:playAnimation(self.rotacja_osi, 1, nil, true);
	end;
end;

function C330:keyEvent(unicode, sym, modifier, isDown)
 end;
 
function C330:update(dt)	
	if self.przy_Maska_C330 then
		if not self.tur_act1 then
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA4) then
				self.tur_act = not self.tur_act;
			end;
			if self.tur_act then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("tur_c330_1")), InputBinding.IMPLEMENT_EXTRA4);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("tur_c330")), InputBinding.IMPLEMENT_EXTRA4);
			end;
		end;
	end;

	if self.tur_act then
		setVisibility(self.Tur,true);
	else
		setVisibility(self.Tur,false);
	end;
	
	if self.przy_Maska_C330 then
		if self.tur_act then
			if InputBinding.hasEvent(InputBinding.TOGGLE_COVER) then
				self.tur_act1 = not self.tur_act1;
			end;
			if self.tur_act1 then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("tur_c330_1_2")), InputBinding.TOGGLE_COVER);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("tur_c330_1_1")), InputBinding.TOGGLE_COVER);
			end;
		end;
	end;

	if self.tur_act1 then
		setVisibility(self.Tur_1,true);
	else
		setVisibility(self.Tur_1,false);
	end;
	
	local nearestDistance = 1;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.przyBelka);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
	if distance < nearestDistance then
		self.przy_przyBelka = true;
	else
		self.przy_przyBelka = false;
	end;
	
	if self.przy_przyBelka then 
		if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA) then
			self.belka_act = not self.belka_act;
		end;
		if self.belka_act then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("belka_c330")), InputBinding.IMPLEMENT_EXTRA);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("belka_c330_1")), InputBinding.IMPLEMENT_EXTRA);
		end;
		if Input.isKeyPressed(Input.KEY_n) then
			self:setAnimationTime(19, self.animationParts[19].currentPosition+(self.animationParts[19].offSet+dt), false);
		elseif Input.isKeyPressed(Input.KEY_j) then
			self:setAnimationTime(19, self.animationParts[19].currentPosition-(self.animationParts[19].offSet+dt), false);
		end;
		g_currentMission:addExtraPrintText(g_i18n:getText("zaczep_c330"));
	end;
	
	if self.belka_act then
		setVisibility(self.Belka_1,true);
	else
		setVisibility(self.Belka_1,false);
	end;
	
	if self.isEntered then	
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.wycieraczka_c330) then
				self.wycieraczka_1 = not self.wycieraczka_1;
			end;
		end;
	end;

	if self.wycieraczka_1 then
		self:setAnimationTime(18, 1);
		self.sound3 = true;
	else 
		self:setAnimationTime(18, 0);
		self.sound3 = false;
	end;

	local nearestDistance = 2;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.przyMaska_1);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
	if distance < nearestDistance then
		self.przy_przyMaska_1 = true;
	else
		self.przy_przyMaska_1 = false;
	end;
	
	if self.przy_przyMaska_1 then
		if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA) then
			self.maska_act = not self.maska_act;
		end;
		if self.maska_act then
			g_currentMission:addHelpButtonText(g_i18n:getText("maska_1_c330"), InputBinding.IMPLEMENT_EXTRA);
		else
			g_currentMission:addHelpButtonText(g_i18n:getText("maska_1_c330_1"), InputBinding.IMPLEMENT_EXTRA);
		end;
	end;
	
	if self.maska_act then
		setVisibility(self.Maska_1,true);
	else
		setVisibility(self.Maska_1,false);
	end;

	if self.przy_przyMaska_1 then
		if self.maska_act then
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
				if self.animationParts[16].clipEndTime then
					self:setAnimationTime(16, self.animationParts[16].offSet);
					self.sound1 = false;
					self.sound2 = true;
				elseif self.animationParts[16].clipStartTime  then
					self:setAnimationTime(16, self.animationParts[16].animDuration);	
					self.sound1 = true;
					self.sound2 = false;
				end;
			end;
			if self.animationParts[16].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("maska_c330_open")), InputBinding.IMPLEMENT_EXTRA3);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("maska_c330_open_1")), InputBinding.IMPLEMENT_EXTRA3);
			end;
		end;
	end;
	
	if self.setHydraulicTime > 0 then
		for k,v in pairs(self.hydraulics) do 
			self:setHydraulicDirection(k);
		end;
		self.setHydraulicTime = self.setHydraulicTime - 1;
	end;
	
	if self:getIsActive() then
		self.setHydraulicTime = 30;
    end;

	if self.isEntered then	
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.lampa_c330) then
				self.swiatlo_c330 = not self.swiatlo_c330;
			end;
		end;
	end;
	
	if self.swiatlo_c330 then
		setVisibility(self.Swiatlo, true);
		setVisibility(self.Swiatlo_1, true);
	else
		setVisibility(self.Swiatlo, false);
		setVisibility(self.Swiatlo_1, false);
	end;
	
	if self.PitchArm ~= nil then
		local x, y, z = getRotation(self.PitchArmRefNode);
		setRotation(self.PitchArm, x * (-1.5), y, z);	
	end;

	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.Maska_C330);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
	if distance < nearestDistance then
		self.przy_Maska_C330 = true;
	else
		self.przy_Maska_C330 = false;
	end;	
	
	if self.przy_Maska_C330 then
		if self.maska_act then
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				if self.animationParts[1].clipEndTime then
					self:setAnimationTime(1, self.animationParts[1].offSet);
					self.maska_otwieranie = false;
					self.maska_zamykanie = true;
				elseif self.animationParts[1].clipStartTime  then
					self:setAnimationTime(1, self.animationParts[1].animDuration);	
					self.maska_otwieranie = true;
					self.maska_zamykanie = false;
				end;
			end;
			if self.animationParts[1].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("maska_c330")), InputBinding.IMPLEMENT_EXTRA2);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("maska_c330_1")), InputBinding.IMPLEMENT_EXTRA2);
			end;
		end;
    end;
	
	if self.maska_otwieranie then
		if not self.maskaPlaying then
			playSample(self.maska_SoundId, 1, 3, 3);
			self.maskaPlaying = true;
		end;      
    else
		if self.maskaPlaying then
		   stopSample(self.maska_SoundId);
		   self.maskaPlaying = false;
		end;        
    end;	
	
	if self.maska_zamykanie then
		if not self.maska_closePlaying then
			playSample(self.maska_close_SoundId, 1, 3, 3);
			self.maska_closePlaying = true;
		end;      
    else
		if self.maska_closePlaying then
		   stopSample(self.maska_close_SoundId);
		   self.maska_closePlaying = false;
		end;        
    end;

	if self.isEntered then
	    if self.sound3 then
		   if not self.wycieraczkaPlaying then
				playSample(self.wycieraczkaSoundId, 0, 1, 0);
				self.wycieraczkaPlaying = true;
			end;
		else
			if self.wycieraczkaPlaying then
				stopSample(self.wycieraczkaSoundId);
				self.wycieraczkaPlaying = false;
			end;
		end;
	else
	    if self.sound3 then
			if self.wycieraczkaPlaying then
				stopSample(self.wycieraczkaSoundId);
				self.wycieraczkaPlaying = false;
			end;
		end;
	end;
	
	if self.isMotorStarted then	
		self:setAnimationTime(2, 1);
		self:setAnimationTime(9, 1);
		self:setAnimationTime(17, 1);
	else 
		self:setAnimationTime(2, 0);
		self:setAnimationTime(9, 0);
		self:setAnimationTime(17, 0);
	end;
	
	if self.realLightsActive then
		self:setAnimationTime(3,self.animationParts[3].animDuration);
	else 
		self:setAnimationTime(3,self.animationParts[3].offSet);
	end;
	
	if self.isClient then
        Utils.updateScrollers(self.ScrollParts, dt, self.isMotorStarted);
		if self.isEntered then
			if not self.brakeLightsVisibility then
				if self.animationParts[4].clipEndTime then
					self:setAnimationTime(4, self.animationParts[4].offSet);
				end;
			end;
			if self.brakeLightsVisibility then
				if self.animationParts[4].clipStartTime then
					self:setAnimationTime(4, self.animationParts[4].animDuration);	
				end;
			end;
		end;	
    end;

	if self.handbrake then
		self:setAnimationTime(5,self.animationParts[5].animDuration);
	else 
		self:setAnimationTime(5,self.animationParts[5].offSet);
	end;	
	
	if self.isEntered then
		if self.lastSpeed*3600 > 12 then
			self:setAnimationTime(20,self.animationParts[20].animDuration);
		else 
			self:setAnimationTime(20,self.animationParts[20].offSet);
		end;
		if not self.brakeLightsVisibility and self.lastSpeed*3600 < 1 then
			if Input.isKeyPressed(Input.KEY_w) or Input.isKeyPressed(Input.KEY_s) then
				self:setAnimationTime(6,self.animationParts[6].animDuration);
			else 
				self:setAnimationTime(6,self.animationParts[6].offSet);
			end;
		end;
		if not self.brakeLightsVisibility then
			if not self.isHired then
				if self.movingDirection < 0 then
					if Input.isKeyPressed(Input.KEY_s) and self.lastSpeed*3600 > 1 then
						self:setAnimationTime(7,self.animationParts[7].animDuration);
					else 
						self:setAnimationTime(7,self.animationParts[7].offSet);
					end;
				end;
			end;
		end;
		if self.movingDirection > 0 then
			if not self.isHired then
				if Input.isKeyPressed(Input.KEY_w) or self.lastSpeed*3600 > 1 then
					self:setAnimationTime(8,self.animationParts[8].animDuration);
				else 
					self:setAnimationTime(8,self.animationParts[8].offSet);
				end;		
			end;
		end;
	end;
	
	if self.isEntered then	
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.halogen_c330) then
				self.halogen_c330 = not self.halogen_c330;
			end;
		end;
	end;
	
	if self.halogen_c330 then
		setVisibility(self.Halogen, true);
	else
		setVisibility(self.Halogen, false);
	end;
	
	if self.isEntered then
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.dach_c330) then
				if self.animationParts[10].clipEndTime then
					self:setAnimationTime(10, self.animationParts[10].offSet);
				elseif self.animationParts[10].clipStartTime  then
					self:setAnimationTime(10, self.animationParts[10].animDuration);	
				end;
			end;
		end;
	end;
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.Drzwi_prawe_C330);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
	if distance < nearestDistance then
		self.przy_Drzwi_prawe_C330 = true;
	else
		self.przy_Drzwi_prawe_C330 = false;
	end;

	if self.przy_Drzwi_prawe_C330 or self.isEntered then
		if InputBinding.hasEvent(InputBinding.drzwi_prawe_c330) then
			if self.animationParts[11].clipEndTime then
				self.drzwi1_otwieranie = false;
				self.drzwi1_zamykanie = true;
				self:setAnimationTime(11, self.animationParts[11].offSet);
			elseif self.animationParts[11].clipStartTime  then
				self:setAnimationTime(11, self.animationParts[11].animDuration);	
				self.drzwi1_otwieranie = true;
				self.drzwi1_zamykanie = false;
			end;
		end;
	end;
	
	if self.przy_Drzwi_prawe_C330 then
		if self.animationParts[11].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_c330")), InputBinding.drzwi_prawe_c330);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_c330_1")), InputBinding.drzwi_prawe_c330);
		end;
    end;	
	
	if self.isEntered then
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.szyba_c330) then
				if self.animationParts[12].clipEndTime then
					self:setAnimationTime(12, self.animationParts[12].offSet);
				elseif self.animationParts[12].clipStartTime  then
					self:setAnimationTime(12, self.animationParts[12].animDuration);	
				end;
			end;
		end;
	end;
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.Drzwi_lewe_C330);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
	if distance < nearestDistance then
		self.przy_Drzwi_lewe_C330 = true;
	else
		self.przy_Drzwi_lewe_C330 = false;
	end;

	if self.przy_Drzwi_lewe_C330 or self.isEntered then
		if InputBinding.hasEvent(InputBinding.drzwi_lewe_c330) then
			if self.animationParts[13].clipEndTime then
				self.drzwi_otwieranie = false;
				self.drzwi_zamykanie = true;
				self:setAnimationTime(13, self.animationParts[13].offSet);
			elseif self.animationParts[13].clipStartTime  then
				self:setAnimationTime(13, self.animationParts[13].animDuration);	
				self.drzwi_otwieranie = true;
				self.drzwi_zamykanie = false;
			end;
		end;
	end;
	
	if self.przy_Drzwi_lewe_C330 then
		if self.animationParts[13].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_c330")), InputBinding.drzwi_lewe_c330);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_c330_1")), InputBinding.drzwi_lewe_c330);
		end;
    end;	
	
	if self.drzwi_otwieranie then
		if not self.drzwiPlaying then
			playSample(self.drzwi_SoundId, 1, 3, 3);
			self.drzwiPlaying = true;
		end;      
    else
		if self.drzwiPlaying then
		   stopSample(self.drzwi_SoundId);
		   self.drzwiPlaying = false;
		end;        
    end;	
	
	if self.drzwi_zamykanie then
		if not self.drzwi_closePlaying then
			playSample(self.drzwi_close_SoundId, 1, 3, 3);
			self.drzwi_closePlaying = true;
		end;      
    else
		if self.drzwi_closePlaying then
		   stopSample(self.drzwi_close_SoundId);
		   self.drzwi_closePlaying = false;
		end;        
    end;
	
	if self.drzwi1_otwieranie then
		if not self.drzwi1Playing then
			playSample(self.drzwi1_SoundId, 1, 3, 3);
			self.drzwi1Playing = true;
		end;      
    else
		if self.drzwi1Playing then
		   stopSample(self.drzwi1_SoundId);
		   self.drzwi1Playing = false;
		end;        
    end;	
	
	if self.drzwi1_zamykanie then
		if not self.drzwi1_closePlaying then
			playSample(self.drzwi1_close_SoundId, 1, 3, 3);
			self.drzwi1_closePlaying = true;
		end;      
    else
		if self.drzwi1_closePlaying then
		   stopSample(self.drzwi1_close_SoundId);
		   self.drzwi1_closePlaying = false;
		end;        
    end;

	if self.sound1 then
		if not self.openPlaying then
			playSample(self.openSoundId, 1, 3, 3);
			self.openPlaying = true;
		end;      
    else
		if self.openPlaying then
		   stopSample(self.openSoundId);
		   self.openPlaying = false;
		end;        
    end;	
	
	if self.sound2 then
		if not self.clousePlaying then
			playSample(self.clouseSoundId, 1, 3, 3);
			self.clousePlaying = true;
		end;       
	else
		if self.clousePlaying then
			stopSample(self.clouseSoundId);
			self.clousePlaying = false;
		end;        
    end;
	
	if self.isEntered then
		if self.movingDirection > 0 then
			if self.lastSpeed*3600 > 1 then
				if Input.isKeyPressed(Input.KEY_w) then
					self:setAnimationTime(14,self.animationParts[14].animDuration);
				else 
					self:setAnimationTime(14,self.animationParts[14].offSet);
				end;
			end;
		end;
		if self.movingDirection < 0 then
			if self.lastSpeed*3600 > 1 then
				if Input.isKeyPressed(Input.KEY_s) then
					self:setAnimationTime(14,self.animationParts[14].animDuration);
				else 
					self:setAnimationTime(14,self.animationParts[14].offSet);
				end;
			end;
		end;
	end;
	
	if self.reka_act then
		setVisibility(self.reka,true);
	else
		setVisibility(self.reka,false);
	end;
	
	if self.isEntered then	
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if InputBinding.hasEvent(InputBinding.reka_1) then
				self.reka_act = not self.reka_act;
			end;
		end;
	end;
	
	if self.animationParts[13].clipEndTime then
		setVisibility(self.drzwi_otwarte,true);
		setVisibility(self.drzwi_zamkniete,false);
		self:playAnimation(self.rotacja_drzwi, 1, nil, true);
	else
		setVisibility(self.drzwi_otwarte,false);
		setVisibility(self.drzwi_zamkniete,true);
		self:playAnimation(self.rotacja_drzwi, -1, nil, true);
	end;

	if self.animationParts[11].clipEndTime then
		setVisibility(self.drzwi_otwarte_1,true);
		setVisibility(self.drzwi_zamkniete_1,false);
		self:playAnimation(self.rotacja_drzwi_1, 1, nil, true);
	else
		setVisibility(self.drzwi_otwarte_1,false);
		setVisibility(self.drzwi_zamkniete_1,true);
		self:playAnimation(self.rotacja_drzwi_1, -1, nil, true);
	end;
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.Pompka);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
	if distance < nearestDistance then
		self.przy_Pompka = true;
	else
		self.przy_Pompka = false;
	end;
	
    if self.przy_Pompka then
		if Input.isKeyPressed(Input.KEY_n) then
			self:setAnimationTime(15,self.animationParts[15].animDuration);
		else 
			self:setAnimationTime(15,self.animationParts[15].offSet);
		end;
		g_currentMission:addHelpButtonText(string.format(g_i18n:getText("pompka_paliwa_c330")), InputBinding.pompka_c360);
    end;
end;

function C330:setHydraulicDirection(index)
	local hydraulic = self.hydraulics[index];

	if hydraulic.fixPoint ~= nil then
		local ax, ay, az = getWorldTranslation(hydraulic.node);
		local bx, by, bz = getWorldTranslation(hydraulic.fixPoint);
		local x, y, z = worldDirectionToLocal(getParent(hydraulic.node), bx-ax, by-ay, bz-az);
		local xUp, yUp, zUp = unpack(hydraulic.upVectors);
		setDirection(hydraulic.node, x, y, z, xUp, yUp, zUp);
		if hydraulic.punch ~= nil then
			local distance = Utils.vector3Length(ax-bx, ay-by, az-bz);
			setTranslation(hydraulic.punch, 0, 0, distance-hydraulic.punchDistance);
		end;
	end;
end;

function C330:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	if not resetVehicles then		
		local reka_act = getXMLBool(xmlFile, key.."#reka_act");
		if reka_act ~= nil then
			self.reka_act = reka_act;
		end;
		
		local tur_act = getXMLBool(xmlFile, key.."#tur_act");
		if tur_act ~= nil then
			self.tur_act = tur_act;
		end;
		
		local tur_act1 = getXMLBool(xmlFile, key.."#tur_act1");
		if tur_act1 ~= nil then
			self.tur_act1 = tur_act1;
		end;
	end;
    return BaseMission.VEHICLE_LOAD_OK;
end;

function C330:getSaveAttributesAndNodes(nodeIdent)
	if self.reka_act then
		reka_act = "true";
	else
		reka_act = "false";
	end;
	
	if self.tur_act then
		tur_act = "true";
	else
		tur_act = "false";
	end;
	
	if self.tur_act1 then
		tur_act1 = "true";
	else
		tur_act1 = "false";
	end;
	
	local attributes = string.format('reka_act="%s" tur_act="%s" tur_act1="%s"',reka_act, tur_act, tur_act1);
    return attributes,nodes;
end;

function C330:mouseEvent(posX, posY, isDown, isUp, button)
end;

function C330:validateAttacherJoint(implement, jointDesc, dt)
return true;
end;

function C330:draw()
	if self.isEntered then	
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			if self.wycieraczka_1 then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("wycieraczka_c330_1")), InputBinding.wycieraczka_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("wycieraczka_c330")), InputBinding.wycieraczka_c330);
			end;
			if self.swiatlo_c330 then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("lampa_c330_1")), InputBinding.lampa_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("lampa_c330")), InputBinding.lampa_c330);
			end;
			if self.halogen_c330 then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("halogen_c330_1")), InputBinding.halogen_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("halogen_c330")), InputBinding.halogen_c330);
			end;
			if self.animationParts[10].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("dach_c330")), InputBinding.dach_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("dach_c330_1")), InputBinding.dach_c330);
			end;
			if self.animationParts[11].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_prawe_c330")), InputBinding.drzwi_prawe_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_prawe_c330_1")), InputBinding.drzwi_prawe_c330);
			end;
			if self.animationParts[12].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("szyba_c330")), InputBinding.szyba_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("szyba_c330_1")), InputBinding.szyba_c330);
			end;
			if self.animationParts[13].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_lewe_c330")), InputBinding.drzwi_lewe_c330);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("drzwi_lewe_c330_1")), InputBinding.drzwi_lewe_c330);
			end;
			g_currentMission:addExtraPrintText(g_i18n:getText("funkcje_1"));
			if Input.isKeyPressed(Input.KEY_lshift) then
				if self.reka_act then
					setTextBold(true);
					renderText(0.72, 0.785, 0.015, g_i18n:getText("reka_2"));
				else
					setTextBold(true);
					renderText(0.72, 0.785, 0.015, g_i18n:getText("reka_1"));
				end;
				if self.animacja_act then
					setTextBold(true);
					renderText(0.72, 0.77, 0.015, g_i18n:getText("animacja_2"));
				else
					setTextBold(true);
					renderText(0.72, 0.77, 0.015, g_i18n:getText("animacja_1"));
				end;
			end;
		end;
	end;		
end;