-- xyzspain

Landoll7130 = {};

function Landoll7130.prerequisitesPresent(specializations)
  return true;
end;

function Landoll7130:load(xmlFile)
---ls2011
self.safeMode = true
  ---ls2011 
			---emision de particulas ruedas

self.wheelParticleSystems = {};
    local psFile = getXMLString(xmlFile, "vehicle.wheelParticleSystems#file");
    if psFile ~= nil then
        local i=0;
        while true do
            local baseName = string.format("vehicle.wheelParticleSystems.wheelParticleSystem(%d)", i);
            local node = getXMLString(xmlFile, baseName.. "#node");
            if node == nil then
                break;
            end;
            node = Utils.indexToObject(self.components, node);
			self.wheelParticleSystems.node = node;
            if node ~= nil then
                local wheelParticleSystem = {};
                wheelParticleSystem.particleSystems = {};
                Utils.loadParticleSystem(xmlFile, wheelParticleSystem.particleSystems, "vehicle.wheelParticleSystems", node, false, nil, self.baseDirectory);
                table.insert(self.wheelParticleSystems, wheelParticleSystem);
            end;
            i = i+1;
        end;
    end;
	
	-------------	
		-- desplazamiento de conjuntos
	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 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;
		local bx, by, bz = getWorldTranslation(self.hydraulics[i].translationPunch);		
		self.hydraulics[i].punchDistance = Utils.vector3Length(ax-bx, ay-by, az-bz);
		self.hydraulics[i].doScale = Utils.getNoNil(getXMLBool(xmlFile, hydraulicName .. "#doScale"), false);
	end;
	-------------------
	  self.piedettach = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.piedettach#index"));
	self.pietransport = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.pietransport#index"));
	setVisibility(self.piedettach,true);
	setVisibility(self.pietransport,false);	
	--revoluciones tractor
		self.lowerRmp = SpecializationUtil.callSpecializationsFunction("lowerRmp");
	-- animacion
	self.anim = SpecializationUtil.callSpecializationsFunction("anim");
	self.Go = {}; 
	self.Done = {};
	self.charId = {};
	self.clipIndex = {};
	self.CheckDone = {};
	self.animParts = {}
	self.Activated = {};
	
	local count = getXMLInt(xmlFile, "vehicle.animParts#count");
	local part = self.animParts;
    for i=1, count do
		part[i] = {};
		local partname = string.format("vehicle.animParts.part".."%d", i);
	    local nameR = getXMLString(xmlFile, partname.."#name");
		self.charId[nameR] = Utils.indexToObject(self.components, getXMLString(xmlFile, partname.."#rootNode"));
		self.clipIndex[nameR] = getXMLString(xmlFile, partname.."#animationClip");
		self.CheckDone[nameR] = false;
		self.Activated[nameR] = false;
	end; 	
	
	self.AllActivated = SpecializationUtil.callSpecializationsFunction("AllActivated");
	--------------------------
	local translationNode1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.translation1#index"));
    if translationNode1 ~= nil then
        self.translation1 = {};
        self.translation1.node = translationNode1;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translation1#minTrans"));
        self.translation1.minTrans = {};
        self.translation1.minTrans[1] = Utils.getNoNil(x, 0);
        self.translation1.minTrans[2] = Utils.getNoNil(y, 0);
        self.translation1.minTrans[3] = Utils.getNoNil(z, 0);

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.translation1#maxTrans"));
        self.translation1.maxTrans = {};
        self.translation1.maxTrans[1] = Utils.getNoNil(x, 0);
        self.translation1.maxTrans[2] = Utils.getNoNil(y, 0);
        self.translation1.maxTrans[3] = Utils.getNoNil(z, 0);

	    self.translation1.transTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.translation1#transTime"), 2)*1000;
      
    end;
	--- ajuste altura tractor
	local count = getXMLInt(xmlFile, "vehicle.rotationParts#count");
	if count ~= nil then
		self.rotationParts = {}
		local rotationPart = self.rotationParts;
		for i=1, count do
			local varName = string.format("rotationPart" .. "%d", i);
			rotationPart[varName] = {};
			local rotationPartname = string.format("vehicle.rotationParts.rotationPart" .. "%d", i);
			rotationPart[varName].index = Utils.indexToObject(self.components, getXMLString(xmlFile, rotationPartname .. "#index"));
			local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, rotationPartname .. "#minRot"));
			rotationPart[varName].minRot = {};
			rotationPart[varName].minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
			rotationPart[varName].minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
			rotationPart[varName].minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));
			local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, rotationPartname .. "#maxRot"));
			rotationPart[varName].maxRot = {};
			rotationPart[varName].maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
			rotationPart[varName].maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
			rotationPart[varName].maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

			rotationPart[varName].rotTime = Utils.getNoNil(getXMLInt(xmlFile, rotationPartname .. "#rotTime"), 2)*1000;	
			rotationPart[varName].touchRotLimit = Utils.getNoNil(getXMLInt(xmlFile, rotationPartname .. "#touchRotLimit"), 1)/1000;	
		end;
	end;
	
	 local rotationNode2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotation2#index"));
    if rotationNode2 ~= nil then
        self.rotation2 = {};
        self.rotation2.node = rotationNode2;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation2#minRot"));
        self.rotation2.minRot = {};
        self.rotation2.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation2.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation2.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation2#maxRot"));
        self.rotation2.maxRot = {};
        self.rotation2.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation2.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation2.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

         self.rotation2.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation2#rotTime"), 2)*1000;
      
    end;
 
	 local rotationNode4 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotation4#index"));
    if rotationNode4 ~= nil then
        self.rotation4 = {};
        self.rotation4.node = rotationNode4;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation4#minRot"));
        self.rotation4.minRot = {};
        self.rotation4.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation4.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation4.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation4#maxRot"));
        self.rotation4.maxRot = {};
        self.rotation4.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation4.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation4.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

         self.rotation4.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation4#rotTime"), 2)*1000;
      
    end;
		 local rotationNode5 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotation5#index"));
    if rotationNode5 ~= nil then
        self.rotation5 = {};
        self.rotation5.node = rotationNode5;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation5#minRot"));
        self.rotation5.minRot = {};
        self.rotation5.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation5.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation5.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotation5#maxRot"));
        self.rotation5.maxRot = {};
        self.rotation5.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotation5.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotation5.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

         self.rotation5.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotation5#rotTime"), 2)*1000;
      
    end;
		---- particulas
	self.groundParticleSystems = {};
    local groundParticleSystemCount = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.groundParticleSystems#count"), 0);
    for i=1, groundParticleSystemCount do
        local namei = string.format("vehicle.groundParticleSystems.part%d", i);
		local nodei = Utils.indexToObject(self.components, getXMLString(xmlFile, namei .. "#index"));
        Utils.loadParticleSystem(xmlFile, self.groundParticleSystems, namei, nodei, false, nil, self.baseDirectory)	
		Utils.setEmittingState(self.groundParticleSystems,false)
		self.groundParticleSystemsdisableTime = 0;		
    end;
	
	
	
	----- cultivators and rollers
	self.cultivator1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cultivator1#index"));
	self.cultivator2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cultivator2#index"));
	self.cultivator3 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cultivator3#index"));
	self.cultivator4 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cultivator4#index"));
	self.roller1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roller1#index"));
	self.roller2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roller2#index"));
	self.roller3 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roller3#index"));
	self.roller4 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roller4#index"));
	self.roller5 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roller5#index"));
	self.roller6 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roller6#index"));
	----------
	 ---------flashlights
	self.FlashLightLeft = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.indexl#index"));
    self.FlashLightRight = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.indexr#index"));	 
	
    self.flashLightLeftActive = false;
    self.flashLightRightActive = false;
    self.flash = false;
    self.flashRight = false;
    self.flashLeft = false;
    self.delayOnLeft = 0;
    self.delayOffLeft = 0;
    self.delayOnRight = 0;
    self.delayOffRight = 0;
    self.delayOn = 0;
    self.delayOff = 0;
   
    turnSoundFile = Utils.getFilename("turn.wav", self.baseDirectory);
    self.turnSoundId = createSample("turnSound");
    loadSample(self.turnSoundId, turnSoundFile, false);
---------------
	self.lightsbrake = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.brakelights#index"));
	

	self.braking = false;
	
		-------------------------
    local workSound = getXMLString(xmlFile, "vehicle.workSound#file");
    if workSound ~= nil and workSound ~= "" then
        workSound = Utils.getFilename(workSound, self.baseDirectory); 
        self.workSound = createSample("workSound");
        self.workSoundEnabled = false;
        loadSample(self.workSound, workSound, false);
        self.workSoundPitchOffset = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.workSound#pitchOffset"), 1);
        self.workSoundVolume = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.workSound#volume"), 1);
    end;


	--------------
	local sound1 = getXMLString(xmlFile, "vehicle.sound1#file");
    if sound1 ~= nil and sound1 ~= "" then
        sound1 = Utils.getFilename(sound1, self.baseDirectory); 
        self.sound1 = createSample("sound1");
        self.sound1Enabled = false;
        loadSample(self.sound1, sound1, false);
        self.sound1PitchOffset = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.sound1#pitchOffset"), 1);
        self.sound1Volume = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.sound1#volume"), 1);
    end;
		----------
	local sound2 = getXMLString(xmlFile, "vehicle.sound2#file");
    if sound2 ~= nil and sound2 ~= "" then
        sound2 = Utils.getFilename(sound2, self.baseDirectory); 
        self.sound2 = createSample("sound2");
        self.sound2Enabled = false;
        loadSample(self.sound2, sound2, false);
        self.sound2PitchOffset = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.sound2#pitchOffset"), 1);
        self.sound2Volume = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.sound2#volume"), 1);
    end;
	
	
	----------
	 self.keys = {};
    local i=0;
    while true do
        local baseName = string.format("vehicle.keys.input(%d)", i);
        local inputName = getXMLString(xmlFile, baseName.. "#name");
        if inputName == nil then
            break;
        end;
        local inputKey = getXMLString(xmlFile, baseName.. "#key");
        if Input[inputKey] == nil then
            print("Error: invalid key '" .. inputKey .. "'  for input event '" .. inputName .. "'");
            break;
        end;
        self.keys[inputName] = Input[inputKey];
        i = i+1;
    end;
		---libertad bloques
	self.setJointRotLimit = SpecializationUtil.callSpecializationsFunction("setJointRotLimit");
	self.firstDo = {};
	self.arm = {};
	------cutting
	local numCuttingAreas = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.cuttingAreas#count"), 0);
    for i=1, numCuttingAreas do
        local areanamei = string.format("vehicle.cuttingAreas.cuttingArea%d", i);
        self.cuttingAreas[i].groundthreshold = Utils.getNoNil(getXMLFloat(xmlFile, areanamei .. "#groundthreshold"), 0.2);
        self.cuttingAreas[i].groundindex = Utils.indexToObject(self.components, getXMLString(xmlFile, areanamei .. "#groundindex"));
    end;
	self.vehicleType = getXMLString(xmlFile, "vehicle#type");
	
	self.AIon=false;
	----	
	self.go=false;
	self.freeCultivatorAndRoller=false;
	
	-----------------
	
end;	
function Landoll7130:delete()
	if self.workSound ~= nil then
        delete(self.workSound);
    end;
  
 if self.sound1 ~= nil then
        delete(self.sound1);
    end;

self:lowerRmp();
	
end;
function Landoll7130:mouseEvent(posX, posY, isDown, isUp, button)
end;

function Landoll7130:keyEvent(unicode, sym, modifier, isDown)
 
---flashlights
	
	if isDown and sym == Input.KEY_KP_1 then
		self.flashLeft = not self.flashLeft;
		self.delayOnLeft = 10;
		self.flashRight = false;
		self.flash = false;
		self.flashLightRightActive = false;
	end;

	if isDown and sym == Input.KEY_KP_3 then
		self.flashRight = not self.flashRight;
		self.delayOnRight = 10;
		self.flashLeft = false;
		self.flash = false;
		self.flashLightLeftActive = false;
	end;
	if isDown and sym == Input.KEY_KP_2 then
		self.flash = not self.flash;
		self.flashRight = false;
		self.flashLeft = false;
		self.delayOn = 20;
		self.flashLightRightActive = false;
		self.flashLightLeftActive = false;
	end;
	-----------
	
	if sym == Input.KEY_KP_8 then
		self.rotation2max = isDown;
		self.rotation4max = isDown;
		self.rotation5max = isDown;
	end;
	if sym == Input.KEY_KP_5 then
		self.rotation2min = isDown;
		self.rotation4min = isDown;
		self.rotation5min = isDown;
	end;
	if isDown and sym == Input.KEY_KP_7 then
		self.down=true;
	else
		self.down=false;
	end;
	if isDown and sym == Input.KEY_KP_4 then
		self.up=true;
	else
		self.up=false;
	end;
	 
end;
function Landoll7130:update(dt)


---ls2011
if self: getIsActive() then 
if InputBinding.hasEvent (InputBinding.IMPLEMENT_EXTRA) then 
self.safeMode = not self.safeMode 
end;
end;
---ls2011
if self.attacherVehicle then


--- flashLights
if self:getIsActive() then --aislarlo de otros objetos
	if self.FlashLightLeft ~= nil then
            if self.flashLightLeftActive then
            	setVisibility(self.FlashLightLeft, self.flashLightLeftActive);
				
	    else
            	setVisibility(self.FlashLightLeft, self.flashLightLeftActive);
	    end;
        end;

	if self.FlashLightRight ~= nil then
            if self.FlashLightRightActive then
            	setVisibility(self.FlashLightRight, self.flashLightRightActive);
	    else
            	setVisibility(self.FlashLightRight, self.flashLightRightActive);
	    end;
        end;

	if self.flashLeft then
		if self.delayOnLeft >= -1 then
			self.delayOnLeft = self.delayOnLeft -dt;
		end;
		if not self.flashLightLeftActive then
			if self.delayOnLeft < 0 then
				self.delayOffLeft = 500;
				self.flashLightLeftActive = true;
				playSample(self.turnSoundId, 1, 0.7, 0);
			end;
		end;
		if self.delayOffLeft >= 0 then
			self.delayOffLeft = self.delayOffLeft -dt;
		end;
		if self.flashLightLeftActive then
			if self.delayOffLeft < 0 then
				self.delayOnLeft = 500;
				self.flashLightLeftActive = false;
			end;
		end;
	end;

	if self.flashRight then
		if self.delayOnRight >= 0 then
			self.delayOnRight = self.delayOnRight -dt;
		end;
		if not self.flashLightRightActive then
			if self.delayOnRight < 0 then
				self.delayOffRight = 500;
				self.flashLightRightActive = true;
				playSample(self.turnSoundId, 1, 0.7, 0);
			end;
		end;
		if self.delayOffRight >= 0 then
			self.delayOffRight = self.delayOffRight -dt;
		end;
		if self.flashLightRightActive then
			if self.delayOffRight < 0 then
				self.delayOnRight = 500;
				self.flashLightRightActive = false;
			end;
		end;
	end;

	if self.flash then
	
		if self.delayOn >= 0 then
			self.delayOn = self.delayOn -dt;
		end;
		if not self.flashLightRightActive then
			if self.delayOn < 0 then
				self.delayOff = 500;
				
				self.flashLightRightActive = true;
				self.flashLightLeftActive = true;
				playSample(self.turnSoundId, 1, 0.7, 0);
				
			end;
		end;
		if self.delayOff >= 0 then
			self.delayOff = self.delayOff -dt;
		end;
		if self.flashLightRightActive then
			if self.delayOff < 0 then
				self.delayOn = 500;
			
				self.flashLightLeftActive = false;
				self.flashLightRightActive = false;
			end;
		end;
		else
		
	end;

	if not self.flashLeft and not self.flashRight then
		if not self.flash then
			self.flashLightRightActive = false;
			self.flashLightLeftActive = false;
		end;
	end;
		
	if not self.flash and not self.flashLeft then
		if not self.flashRight then
			self.flashLightRightActive = false;
		end;
	end;

	if not self.flash and not self.flashRight then
		if not self.flashLeft then
			self.flashLightLeftActive = false;
		end;
	end;
end;

---particulas ruedas

local dens = 0; local dens1; local dens2; local dens3;
	 local x,y,z = getWorldTranslation(self.wheels[1].repr);
	 local x1,y1,z1 = getWorldTranslation(self.wheels[2].repr);
	 local x2,y2,z2 = getWorldTranslation(self.wheels[2].repr);
	local id = g_currentMission.terrainDetailId;
	local xx, zz, widthX, widthZ, heightX, heightZ = Utils.getXZWidthAndHeight(id, x, z, x1, z1, x2, z2);
	dens1 = getDensityParallelogram(id, xx, zz, widthX, widthZ, heightX, heightZ, g_currentMission.cultivatorChannel, 1);
	dens2 = getDensityParallelogram(id, xx, zz, widthX, widthZ, heightX, heightZ, g_currentMission.sowingChannel, 1);
	dens3 = getDensityParallelogram(id, xx, zz, widthX, widthZ, heightX, heightZ, g_currentMission.ploughChannel, 1);
	if g_currentMission.environment.lastRainScale <= 0.1 and g_currentMission.environment.timeSinceLastRain > 30 then
		dens = dens1+dens2+dens3;
	end;
	if dens > 0 and self.lastSpeed*self.speedDisplayScale*3600 > 4 then
		
		
		for k=1, 2 do
			Utils.setEmittingState(self.wheelParticleSystems[k].particleSystems, self.wheels[k].hasGroundContact);
		end;
	else
		for i=1, table.getn(self.wheelParticleSystems) do
			Utils.setEmittingState(self.wheelParticleSystems[i].particleSystems, false);
		end;
	
	end;
	
	--------------------brake------------------------
   inputAxis_2 = InputBinding.getAnalogInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);

        if InputBinding.isAxisZero(inputAxis_2) then
        inputAxis_2 = InputBinding.getDigitalInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);
        end;
	
		if (self.movingDirection*self.lastSpeed*(-inputAxis_2)) < -0.001 then
			setVisibility(self.lightsbrake, true);
		
			self.braking = true;
			
		else
		     setVisibility(self.lightsbrake, false);
		
			self.braking = false;
			
		end;
	
	--------------
	if self.time > self.groundParticleSystemsdisableTime then
           Utils.setEmittingState(self.groundParticleSystems, false);
    end;
			
	self.wasToFast = false;
	
	
	
	 if self:getIsActive()  then
        if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				 self.Go.Lift = not self.Go.Lift;
					self.Done.Lift= true;
				
		
		end;
	end;
	

		if self.Go.Lift ~= nil and self.Done.Lift ~= false then
			self:anim("Lift", false); 
		end;
		
	if  self.Go.Lift then 
			if not self.backupRmp then
				self.backupRmp = self.attacherVehicle.motor.minRpm;
			end; 			
			if self.attacherVehicle.motor.minRpm >= -500 then
				self.attacherVehicle.motor.minRpm = self.attacherVehicle.motor.minRpm + 500; 
			end; 
			if not self.sound1Enabled  and not self.AIon and self:getIsActiveForSound() then
			setSamplePitch(self.sound1, self.sound1PitchOffset);
			playSample(self.sound1, 0, self.sound1Volume, 0);
			self.sound1Enabled = true;
			end;
			self.freeCultivatorAndRoller=true;
		
			
	end;		

	if (self.CheckDone["Lift"] == true) and self.Activated["Lift"] == false then
---llamada a especializacion
	self:lowerRmp();
	self.go=true; --- necesaria  para revoluciones tractor
	if self.sound1Enabled then
		stopSample(self.sound1);
		self.sound1Enabled = false;
	end;
	self.openFinished=true;
	self.free=true;
	elseif (self.CheckDone["Lift"] == false) and self.Activated["Lift"] == true then
	self.openFinished=false;
	end;	
			
		-- if self.attacherVehicle.lastSpeed*3600 > 2  then
		for k, cuttingArea in pairs(self.cuttingAreas) do
			local x,y,z = getWorldTranslation(cuttingArea.groundindex);
	        local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z);
			local x1,y1,z1 = getRotation(self.wheels[5].repr);	
			if k==1 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.cultivator1, x1/1.5, y1, z1);
			end;
			if k==2 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.roller1, y1, x1*-1,z1);
			end;
			if k==3 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.roller2,y1, x1*-1, z1);
			end;
			if k==4 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.cultivator2, z1, y1, x1/1.5);
			end;
			if k==5 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.roller3, y1,(x1*-1),z1);
			end;
			if k==6 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.roller4, y1,(x1*-1),z1);
			end;
			if k==7 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.cultivator3, z1, y1, (x1*-1)/1.5);
			end;
			if k==8 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.roller5, y1, x1*-1,z1);
			end;
			if k==9 and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
			setRotation(self.roller6, y1, x1*-1, z1);
			end;
		end;
		
			------cutting, worksound ,ps	
				
		if	self.free==true  then  
		  local toFast = self:doCheckSpeedLimit() and self.lastSpeed*3600 > 20;
            if not toFast then
			
			
			local area = 0;
			for k, cuttingArea in pairs(self.cuttingAreas) do
			    local x,y,z = getWorldTranslation(cuttingArea.groundindex);
	            local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z);
				if (k == 2 or  k == 3 or k == 5 or k == 6 or  k == 8 or k == 9)and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
					local x,y,z = getWorldTranslation(cuttingArea.start);
					local x1,y1,z1 = getWorldTranslation(cuttingArea.width);
					local x2,y2,z2 = getWorldTranslation(cuttingArea.height);
					area = Utils.updateCultivatorArea(x, z, x1, z1, x2, z2);
					---ls2011
					if self.safeMode then
						Landoll7130.updateSafeArea (x, z, x1, z1, x2, z2) 
					else 
					Utils.updateCultivatorArea (x, z, x1, z1, x2, z2); 
					end;
					---ls2011
				end;
					
				if (k == 1 or  k == 4 or k == 7)and cuttingArea.groundindex ~= nil and terrainHeight+cuttingArea.groundthreshold >= y then
					local x,y,z = getWorldTranslation(cuttingArea.start);
					local x1,y1,z1 = getWorldTranslation(cuttingArea.width);
					local x2,y2,z2 = getWorldTranslation(cuttingArea.height);
					area = Utils.updatePloughArea(x, z, x1, z1, x2, z2);
                    local pixelToQm = 2048 / 8192 * 2048 / 8192; -- 8192px are mapped to 2048m
                    local qm = area*pixelToQm;
                    local ha = qm/10000;
					
					---ls2011
					if self.safeMode then
					Landoll7130.updateSafeArea (x, z, x1, z1, x2, z2) 
					else 
					Utils.updatePloughArea (x, z, x1, z1, x2, z2); 
					end;
					---ls2011
					
				if self.attacherVehicle.lastSpeed*3600 > 3  then
					if g_currentMission.environment.lastRainScale <= 0.1 and g_currentMission.environment.timeSinceLastRain > 30 then
						self.groundParticleSystemsdisableTime = self.time + 40;
						Utils.setEmittingState(self.groundParticleSystems, true)
													
					end;
						if not self.workSoundEnabled and not self.AIon and self:getIsActiveForSound() then
							setSamplePitch(self.workSound, self.workSoundPitchOffset);
							playSample(self.workSound, 0, self.workSoundVolume, 0);
							self.workSoundEnabled = true;
						end;
				elseif self.attacherVehicle.lastSpeed*3600 < 3 then
						Utils.setEmittingState(self.groundParticleSystems, false)
						 if self.workSoundEnabled then
							stopSample(self.workSound);
							self.workSoundEnabled = false;
							end;
				
				
				end;	
				end;
		end;
	else
			Utils.setEmittingState(self.groundParticleSystems, false)
						 if self.workSoundEnabled then
							stopSample(self.workSound);
							self.workSoundEnabled = false;
							end;
	end;
		self.wasToFast = toFast;
		else
		Utils.setEmittingState(self.groundParticleSystems, false)
						 if self.workSoundEnabled then
							stopSample(self.workSound);
							self.workSoundEnabled = false;
							end;
	end;		
	

if not self.Go.Lift  and self.go then
			self.free=false;
			if not self.backupRmp then
				self.backupRmp = self.attacherVehicle.motor.minRpm;
			end; 			
			if self.attacherVehicle.motor.minRpm >= -500 then
				self.attacherVehicle.motor.minRpm = self.attacherVehicle.motor.minRpm + 500; 
			end; 
			if not self.sound1Enabled  and not self.AIon and self:getIsActiveForSound() then
			setSamplePitch(self.sound1, self.sound1PitchOffset);
			playSample(self.sound1, 0, self.sound1Volume, 0);
			self.sound1Enabled = true;
			end;
	end;
	if (self.CheckDone["Lift"] == false) and self.Activated["Lift"] == true and self.go then
---llamada a especializacion
	self:lowerRmp();
		if self.sound1Enabled then
		stopSample(self.sound1);
		self.sound1Enabled = false;
		end;
	self.freeCultivatorAndRoller=false;
	self.go=false; --- necesaria  para revoluciones tractor
	end;	
	
		


end;---attacher	
 -------------
	if self.translation1Max ~= nil then
					local x, y, z = getTranslation(self.translation1.node);
					local trans = {x,y,z};
					local newTrans = Utils.getMovedLimitedValues(trans, self.translation1.maxTrans, self.translation1.minTrans, 3, self.translation1.transTime, dt, not self.translation1Max);
					setTranslation(self.translation1.node, unpack(newTrans));
				end;

	if self.down then
			local rotationPart = self.rotationParts.rotationPart1;
			local rot = {getRotation(rotationPart.index)};
			local newRot = Utils.getMovedLimitedValues(rot, rotationPart.maxRot, rotationPart.minRot, 3, rotationPart.rotTime, dt, true);
			setRotation(rotationPart.index, unpack(newRot));
	    end;		
		if self.up then
			local rotationPart = self.rotationParts.rotationPart1;
			local rot = {getRotation(rotationPart.index)};
			local newRot = Utils.getMovedLimitedValues(rot, rotationPart.maxRot, rotationPart.minRot, 3, rotationPart.rotTime, dt, false);
			setRotation(rotationPart.index, unpack(newRot));
	    end;  
local doRotate = self.rotation2max or self.rotation2min 
	if self.rotation2 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation2.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation2.maxRot, self.rotation2.minRot, 3, self.rotation2.rotTime, dt, not self.rotation2max);
        setRotation(self.rotation2.node, unpack(newRot));
    end;
local doRotate = self.rotation4max or self.rotation4min 
	if self.rotation4 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation4.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation4.maxRot, self.rotation4.minRot, 3, self.rotation4.rotTime, dt, not self.rotation4max);
        setRotation(self.rotation4.node, unpack(newRot));
    end;
local doRotate = self.rotation5max or self.rotation5min 
	if self.rotation5 ~= nil and doRotate then
        local x, y, z = getRotation(self.rotation5.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotation5.maxRot, self.rotation5.minRot, 3, self.rotation5.rotTime, dt, not self.rotation5max);
        setRotation(self.rotation5.node, unpack(newRot));
    end;



	
	
	--- asignacion de libertad ejes de rotacion segun condiciones de emsamblage  0 x, 2 y ,1 z 
		
		
	self:setJointRotLimit(self.componentJoints[3],12, 0, 1000,self.free == true, 0,dt);	
	self:setJointRotLimit(self.componentJoints[8],4, 0, 1000,self.free == true, 1,dt);	
	self:setJointRotLimit(self.componentJoints[13],4, 0, 1000,self.free == true, 1,dt);
	
	self:setJointRotLimit(self.componentJoints[4],7, 0, 1000,self.free == true, 0,dt);
	self:setJointRotLimit(self.componentJoints[5],90, 0, 1000,self.freeCultivatorAndRoller == true, 0,dt);
	self:setJointRotLimit(self.componentJoints[6],90, 0, 1000,self.freeCultivatorAndRoller == true, 0,dt);
	self:setJointRotLimit(self.componentJoints[9],7, 0, 1000,self.free == true, 2,dt);
	self:setJointRotLimit(self.componentJoints[10],90, 0, 1000,self.freeCultivatorAndRoller == true, 2,dt);
	 self:setJointRotLimit(self.componentJoints[11],90, 0, 1000,self.freeCultivatorAndRoller == true, 2,dt);
	self:setJointRotLimit(self.componentJoints[14],7, 0, 1000,self.free == true, 2,dt);
	self:setJointRotLimit(self.componentJoints[15],90, 0, 1000,self.freeCultivatorAndRoller == true, 2,dt);
	self:setJointRotLimit(self.componentJoints[16],90, 0, 1000,self.freeCultivatorAndRoller == true, 2,dt);
	-- juntas

		local joint = self.componentJoints[1];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[2];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[3];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[4];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[5];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[6];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[7];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[8];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[9];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[10];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[11];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[12];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[13];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[14];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[15];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[16];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[17];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[18];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[19];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
			local joint = self.componentJoints[20];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[21];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[22];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[23];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[24];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[25];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[26];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[27];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
				local joint = self.componentJoints[28];
		setJointFrame(joint.jointIndex, 0,joint.jointNode);
		self:setHydraulicDirection();


end;
function Landoll7130:draw()
if self.safeMode then 
g_currentMission: addHelpButtonText ("switch to normal mode", InputBinding.IMPLEMENT_EXTRA); 
else 
g_currentMission: addHelpButtonText ("switch to safe mode", InputBinding.IMPLEMENT_EXTRA); 
end; 
if self.AIon == false then
	g_currentMission:addExtraPrintText(g_i18n:getText(self.vehicleType..".2"));
	 g_currentMission:addHelpButtonText(string.format(g_i18n:getText(self.vehicleType..".1"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2); 
		
end;

end;
function Landoll7130:onAttach()
self.translation1Max = true;

setVisibility(self.pietransport,true);
setVisibility(self.piedettach,false);


 


end;
function Landoll7130:onDetach()

	self.translation1Max = false;
setVisibility(self.pietransport,false);
setVisibility(self.piedettach,true);
	
    if self.deactivateOnDetach then
        Landoll7130.onDeactivate(self);
    else
        Landoll7130.onDeactivateSounds(self)
    end;

setVisibility(self.lightsbrake, false);

self.flashLightRightActive = false;
self.flashLightLeftActive = false;
setVisibility(self.FlashLightRight, false);
setVisibility(self.FlashLightLeft, false);

 


end;
----ls2011
Landoll7130.updateSafeArea = function(l_18_0, l_18_1, l_18_2, l_18_3, l_18_4, l_18_5)
  local cultiId = g_currentMission.cultivatorChannel
  local sowingId = g_currentMission.sowingChannel
  local detailId = g_currentMission.terrainDetailId
  local ploughId = g_currentMission.ploughChannel
  local sprayId = g_currentMission.sprayChannel
  local x, z, widthX, widthZ, heightX, heightZ = Utils.getXZWidthAndHeight(detailId, l_18_0, l_18_1, l_18_2, l_18_3, l_18_4, l_18_5)
  Utils.updateDestroyCommonArea(l_18_0, l_18_1, l_18_2, l_18_3, l_18_4, l_18_5)
  setDensityMaskedParallelogram(detailId, x, z, widthX, widthZ, heightX, heightZ, cultiId, 1, detailId, sowingId, 1, 1)
  setDensityMaskedParallelogram(detailId, x, z, widthX, widthZ, heightX, heightZ, cultiId, 1, detailId, ploughId, 1, 1)
  setDensityParallelogram(detailId, x, z, widthX, widthZ, heightX, heightZ, sowingId, 1, 0)
  setDensityParallelogram(detailId, x, z, widthX, widthZ, heightX, heightZ, ploughId, 1, 0)
  setDensityParallelogram(detailId, x, z, widthX, widthZ, heightX, heightZ, sprayId, 1, 0)
 end;
 
 ---ls2011
function Landoll7130:onLeave()

    if self.deactivateOnLeave then
        Landoll7130.onDeactivate(self);
    else
        Landoll7130.onDeactivateSounds(self)
    end;

end;

function Landoll7130:onDeactivate()

    Landoll7130.onDeactivateSounds(self)
   

end;

function Landoll7130:onDeactivateSounds()
	 if self.workSoundEnabled then
        stopSample(self.workSound);
        self.workSoundEnabled = false;
    end;
   
	 if self.sound1Enabled then
        stopSample(self.sound1);
        self.sound1Enabled = false;
    end;
	
	
end;
function Landoll7130:setJointRotLimit(nodei, up, down, speed, value, axle, dt)

	if not self.firstDo[nodei] then
		self.firstDo[nodei] = true;
		self.arm[nodei] = {0}; 
	end; 
	x, y, z = getRotation(nodei);
	rot = {x,y,z};	
	newRotLimit = {}; 
	newRotLimit[nodei] = {0,0,0};
	newRotLimit[nodei] = Utils.getMovedLimitedValues(self.arm[nodei], {down}, {up}, 1, speed * 2, dt, value);
	if math.abs(newRotLimit[nodei][1] - self.arm[nodei][1]) > 0.001 then
		local joint = nodei;
		setJointRotationLimit(joint.jointIndex, axle, true, Utils.degToRad(-newRotLimit[nodei][1]), Utils.degToRad(newRotLimit[nodei][1]));
	end;
	self.arm[nodei] = newRotLimit[nodei];			
end;
function Landoll7130:setHydraulicDirection()
	for i=1, table.getn(self.hydraulics) do
		local ax, ay, az = getWorldTranslation(self.hydraulics[i].node);
		local bx, by, bz = getWorldTranslation(self.hydraulics[i].fixPoint);
		local x, y, z = worldDirectionToLocal(getParent(self.hydraulics[i].node), bx-ax, by-ay, bz-az);
		
		setDirection(self.hydraulics[i].node, x, y, z, 0, 1, 0);
		local distance = Utils.vector3Length(ax-bx, ay-by, az-bz);
		if self.hydraulics[i].doScale then
			local xScale, yScale, zScale = getScale(self.hydraulics[i].node);
			local newScale = yScale * (distance / self.hydraulics[i].punchDistance);
			setScale(self.hydraulics[i].node, 1, 1, newScale);
		else
			if self.hydraulics[i].punch ~= nil then
				setTranslation(self.hydraulics[i].punch, 0, 0, distance-self.hydraulics[i].punchDistance);
			end;
		end;
	
	end;
end;
function Landoll7130:anim(varName, loopCheck)	
	-- print("ANIMATION");
	
	self.Activated[varName] = true;

	if self.Go[varName] == true and self.Done[varName] ~= false then
		local charId = getAnimCharacterSet(self.charId[varName]);
		local clipIndex = getAnimClipIndex(charId, self.clipIndex[varName]);
		assignAnimTrackClip(charId , 0, clipIndex);
		setAnimTrackLoopState(charId, 0, loopCheck);
		setAnimTrackSpeedScale(charId, 0, 1);
		enableAnimTrack(charId, 0);
		if getAnimTrackTime(charId, 0) >= getAnimClipDuration(charId, clipIndex) and loopCheck == false then
			disableAnimTrack(charId, 0);	
			self.Done[varName] = false;	
			self.CheckDone[varName] = true;	
			self.Activated[varName] = false;
		
			---final animacion hacia delante
		end; 
	elseif self.Go[varName] == false and self.Done[varName] ~= false then
		local charId = getAnimCharacterSet(self.charId[varName]);
		local clipIndex = getAnimClipIndex(charId, self.clipIndex[varName]);
		if loopCheck == true then
			disableAnimTrack(charId, 0);
			self.Done[varName] = false;	
		end; 	
		assignAnimTrackClip(charId , 0, clipIndex);
		setAnimTrackLoopState(charId, 0, loopCheck);
		setAnimTrackSpeedScale(charId, 0, -1);
		enableAnimTrack(charId, 0);
		if getAnimTrackTime(charId, 0) <= 0 then
			disableAnimTrack(charId, 0);
			self.Done[varName] = false;	
			self.CheckDone[varName] = false;
			self.Activated[varName] = true;
			---final animacion hacia atras
		
		end;
	end;
end; 


function Landoll7130:AllActivated()
	
	if (self.CheckDone["Lift"] == true) and not self.Activated["Lift"]  then
		return true;
	else
		return false;
	end;
	
	
	
end;
function Landoll7130:lowerRmp()
	if self.backupRmp and self.attacherVehicle then
		if self.attacherVehicle.motor.minRpm <= self.backupRmp then
			self.attacherVehicle.motor.minRpm = self.attacherVehicle.motor.minRpm -100; 
		else
			self.attacherVehicle.motor.minRpm = self.backupRmp; 
			self.backupRmp = nil; 			
		end; 	
	end; 
end;

function key_convert(key)
    if key ~= nil then 
	    local keyname = "";
	    if key > 32 and key < 123 then
	    	keyname = string.upper(string.char(key));
	    elseif key > 255 and key < 266 then
	    	keyname = "NUM "..string.char(key-208);
	    else 
	    	keyname = "";
	    end;
	    return(keyname);
	  end;
end;
