--mtz valtó
--powered by hz888


powershift = {};

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

function powershift:load(xmlFile)

    self.gearfile = Utils.getFilename("gearhud.png", self.baseDirectory);
    self.hudGearWidth = 0.205;
    self.hudGearHeight = 0.255;
    self.hudGearPoxX = 0.796;
    self.hudGearPoxY = 0.666;
    self.hudGearOverlay = Overlay:new("hudGearpanel", self.gearfile, self.hudGearPoxX, self.hudGearPoxY, self.hudGearWidth, self.hudGearHeight);

    self.brakeLight = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.brakeLight#index"));
    if self.brakeLight ~= nil then
	setVisibility(self.brakeLight, false);
    end;
    self.kardan1Node = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.kardan1#index"));
    self.kardan2Node = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.kardan2#index"));
    self.brakeLightActive = false;
    self.motorMaxRpmLimit = self.motor.maxRpm;
    self.motormaxi = math.max(self.motor.maxRpm[1], self.motor.maxRpm[2], self.motor.maxRpm[3], self.motor.maxRpm[4]);
    self.motormax = {self.motor.maxRpm[1], self.motor.maxRpm[2], self.motor.maxRpm[3], self.motor.maxRpm[4]};
    self.motormaxr = {self.motor.maxRpm[1]*2, self.motor.maxRpm[2]*1.5, self.motor.maxRpm[3], self.motor.maxRpm[4]};
    self.fGearRatios = self.motor.forwardGearRatios;
    self.bGearRatio = self.motor.backwardGearRatio;
    self.bForce = self.motor.brakeForce;
    self.level = 0;
    self.ok = true;
    self.reverse = false;
    self.gang0 = true;
    self.gang2 = false;
    self.off = false;
    self.isSlow = false;
    self.isFast = true;

    self.forwardTorqueCurve1 = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve1s = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve2 = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve2s = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve3 = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve3s = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve4 = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve4s = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve5 = AnimCurve:new(linearInterpolator1);
    self.forwardTorqueCurve0 = AnimCurve:new(linearInterpolator1);

    self.backwardTorqueCurve1 = AnimCurve:new(linearInterpolator1);    
    self.backwardTorqueCurve2 = AnimCurve:new(linearInterpolator1);
    self.backwardTorqueCurve0 = AnimCurve:new(linearInterpolator1);
    local torqueI = 0;
    while true do
        local key = string.format("vehicle.motor.backwardTorque(%d)", torqueI);
        local rpm = getXMLFloat(xmlFile, key.."#rpm");
        local torque = getXMLFloat(xmlFile, key.."#torque");
        if torque == nil or rpm == nil then
            break;
        end;
        self.backwardTorqueCurve1:addKeyframe({v=(torque*2)*3, time = rpm});
        self.backwardTorqueCurve2:addKeyframe({v=(torque*4)*3, time = rpm});
        self.backwardTorqueCurve0:addKeyframe({v=(torque*16)*3, time = rpm});
        torqueI = torqueI +1;
    end;
    local torqueI = 0;
    while true do
        local key = string.format("vehicle.motor.torque(%d)", torqueI);
        local rpm = getXMLFloat(xmlFile, key.."#rpm");
        local torque = getXMLFloat(xmlFile, key.."#torque");
        if torque == nil or rpm == nil then
            break;
        end;
	if torqueI < 2 then
        	self.forwardTorqueCurve1:addKeyframe({v=(torque/2)*3, time = rpm});
        	self.forwardTorqueCurve1s:addKeyframe({v=(torque/5)*3, time = rpm});
        	self.forwardTorqueCurve0:addKeyframe({v=(torque*4.5)*3, time = rpm});
	else
        	self.forwardTorqueCurve1:addKeyframe({v=(torque/5)*3, time = rpm});
        	self.forwardTorqueCurve1s:addKeyframe({v=(torque/10)*3, time = rpm});
        	self.forwardTorqueCurve0:addKeyframe({v=(torque*3.5)*3, time = rpm});
	end;
	if torqueI < 1 then
        	self.forwardTorqueCurve5:addKeyframe({v=(torque*3)*3, time = rpm});
	else
        	self.forwardTorqueCurve5:addKeyframe({v=(torque*3.5)*3, time = rpm});
	end;
        self.forwardTorqueCurve2:addKeyframe({v=(torque/2)*3, time = rpm});
        self.forwardTorqueCurve2s:addKeyframe({v=(torque/3)*3, time = rpm});
        self.forwardTorqueCurve3:addKeyframe({v=torque*3, time = rpm});
        self.forwardTorqueCurve3s:addKeyframe({v=(torque/2)*3, time = rpm});
        self.forwardTorqueCurve4:addKeyframe({v=(torque*2)*3, time = rpm});
        self.forwardTorqueCurve4s:addKeyframe({v=(torque)*3, time = rpm});
        torqueI = torqueI +1;
    end;


end;

function powershift:delete()

    if self.hudGearOverlay~= nil then
        self.hudGearOverlay:delete();
    end;

end;

function powershift:mouseEvent(posX, posY, isDown, isUp, button)

    self.lastMouseXPos = posX;
    self.lastMouseYPos = posY;
    if not self.off then
	if not self.kuplung then
	    if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_LEFT) and not self.isAITractorActivated then

		if self.reverse then
			self.gang0 = true;
			self.reverse = false;
		elseif self.gang0 then
			if self.isFast then
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve1;
			else
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve1s;
			end;
			self.gang2 = true;
			self.gang0 = false;
		elseif self.gang2 then
			if self.isFast then
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve3;
			else
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve3s;
			end;
			
			self.gang3 = true;
			self.gang2 = false;
		elseif self.gang3 then
			if self.isFast then
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve4;
			else
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve4s;
			end;
			self.gang4 = true;
			self.gang3 = false;
		
		end;
	    end;

	    if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_RIGHT) and not self.isAITractorActivated then
		if self.gang4 then
			if self.isFast then
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve3;
			else
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve3s;
			end;
			
			self.gang3 = true;
			self.gang4 = false;
		elseif self.gang3 then
			if self.isFast then
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve2;
			else
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve2s;
			end;
			self.gang2 = true;
			self.gang3 = false;
		elseif self.gang2 then
			if self.isFast then
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve1;
			else
				self.motor.forwardTorqueCurve = self.forwardTorqueCurve1s;
			end;
			self.gang0 = true;
			self.gang2 = false;
		elseif self.gang2 then
			self.gang0 = true;
			self.gang2 = false;
		elseif self.gang0 then
			if self.isFast then
				self.motor.backwardTorqueCurve = self.backwardTorqueCurve2;
			else
				self.motor.backwardTorqueCurve = self.backwardTorqueCurve1;
			end;
			self.reverse = true;
			self.gang0 = false;
		end;
	    end;
	else
		if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_RIGHT) and not self.isAITractorActivated then
		    if not self.reverse then
			if self.gang4 then
				self.gang4k = true;
				self.gang4 = false;
			else
				self.gang4k = false;
			end;
			if self.gang3  then
				self.gang3k = true;
				self.gang3 = false;
			else
				self.gang3k = false;
			end;
			if self.gang2 then
				self.gang2k = true;
				self.gang2 = false;
			else
				self.gang2k = false;
			end;
			
		    end;
			if self.isFast then
				self.motor.backwardTorqueCurve = self.backwardTorqueCurve2;
			else
				self.motor.backwardTorqueCurve = self.backwardTorqueCurve1;
			end;
			
			self.reverse = true;
		end;
		if Input.isMouseButtonPressed(Input.MOUSE_BUTTON_LEFT) and not self.isAITractorActivated then
			self.reverse = false;
			self.gang2 = self.gang2k;
			self.gang3 = self.gang3k;
			self.gang4 = self.gang4k;
			
			if self.gang2 then
				if self.isFast then
					self.motor.forwardTorqueCurve = self.forwardTorqueCurve2;
				else
					self.motor.forwardTorqueCurve = self.forwardTorqueCurve2s;
				end;
			
			elseif self.gang3 then
				if self.isFast then
					self.motor.forwardTorqueCurve = self.forwardTorqueCurve3;
				else
					self.motor.forwardTorqueCurve = self.forwardTorqueCurve3s;
				end;
			elseif self.gang4 then
				if self.isFast then
					self.motor.forwardTorqueCurve = self.forwardTorqueCurve4;
				else
					self.motor.forwardTorqueCurve = self.forwardTorqueCurve4s;
				end;
			
		
		end;
	end;
    end;
	if self.isMouseActive then
		self.mouseButton = MouseControlsVehicle.BUTTON_NONE;
	end;

end;
end;
function powershift:keyEvent(unicode, sym, modifier, isDown)

    if isDown and sym == Input.KEY_space then
	self.off = not self.off;
    end;

    if self.gang0 then
	if isDown and sym == Input.KEY_home then
			self.isSlow = false;
			self.isFast = true;
	end;
	if isDown and sym == Input.KEY_end then
			self.isFast = false;
			self.isSlow = true;
	end;
	
	 elseif self.gang2 then
	if isDown and sym == Input.KEY_home then
			self.isSlow = false;
			self.isFast = true;
			self.motor.forwardTorqueCurve = self.forwardTorqueCurve2;
	end;
	if isDown and sym == Input.KEY_end then
			self.isFast = false;
			self.isSlow = true;
			self.motor.forwardTorqueCurve = self.forwardTorqueCurve2s;
	end;
	 elseif self.gang3 then
	if isDown and sym == Input.KEY_home then
			self.isSlow = false;
			self.isFast = true;
			self.motor.forwardTorqueCurve = self.forwardTorqueCurve3;
	end;
	if isDown and sym == Input.KEY_end then
			self.isFast = false;
			self.isSlow = true;
			self.motor.forwardTorqueCurve = self.forwardTorqueCurve3s;
	end;
	 elseif self.gang4 then
	if isDown and sym == Input.KEY_home then
			self.isSlow = false;
			self.isFast = true;
			self.motor.forwardTorqueCurve = self.forwardTorqueCurve4;
	end;
	if isDown and sym == Input.KEY_end then
			self.isFast = false;
			self.isSlow = true;
			self.motor.forwardTorqueCurve = self.forwardTorqueCurve4s;
	end;
	elseif self.reverse then
	if isDown and sym == Input.KEY_home then
			self.isSlow = false;
			self.isFast = true;
	end;
	if isDown and sym == Input.KEY_end then
			self.isFast = false;
			self.isSlow = true;
	end;
    end;


end;


function powershift:update(dt)

    self.input = InputBinding.getAnalogInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);
    if InputBinding.isAxisZero(self.input) then
        self.input = InputBinding.getDigitalInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);
    end;
      if self.isAITractorActivated then
	self.motor.backwardGearRatio = self.bGearRatio/2;
	self.motor.forwardGearRatios = self.fGearRatios;
	self.motor.forwardTorqueCurve = self.forwardTorqueCurve0;
	self.motor.backwardTorqueCurve = self.backwardTorqueCurve0;
	self.motor.maxRpm[self.motor.speedLevel] = self.motormax[self.motor.speedLevel];
	self.maxAccelerationSpeed = self.motormaxi/100000;
	if self.turnStage == 3 or self.turnStage == 4 then
		if self.turnStage == 3 then
			self.motor.brakeForce = self.bForce/3;
		elseif self.turnStage == 4 and self.movingDirection < 0 then
			self.motor.brakeForce = self.bForce;
		else
			self.motor.brakeForce = 0;
		end;	
	else
		self.motor.brakeForce = 0;
	end;	
        setVisibility(self.brakeLight, false);
	self.reverse = false;
	self.gang0 = true;
	
	self.gang2 = false;
	self.gang3 = false;
	self.gang4 = false;
	
	if self.kardan1Node ~= nil and self.kardan2Node ~= nil then
         	rotate(self.kardan1Node, self.wheelRpm*0.1*self.movingDirection, 0, 0);
        	rotate(self.kardan2Node, self.wheelRpm*-0.1*self.movingDirection, 0, 0);
	end;
      else
	if not self.off then
	    if Input.isKeyPressed(Input.KEY_space) then
			if self.motor.speedLevel ~= 0 then
				self.level = self.motor.speedLevel;
				self.motor.speedLevel = 0;
				self.motor.brakeForce = self.bForce;
			elseif -self.input < -0.001 then
				self.motor.brakeForce = -self.input*-self.bForce;
				self.movingDirection = 0;
				self.brakeLightActive = true;
			elseif -self.input > 0.001 then
				self.movingDirection = 0;
			elseif self.level ~= 0 then
				if -self.input > 0.001 or -self.input < -0.001 then
					self.level = 0;
				end;
				self.motor.brakeForce = self.bForce;
			else
				self.brakeLightActive = false;
				self.motor.brakeForce = self.bForce/10;
			end;
			self.maxAccelerationSpeed = 0;
			self.motor.backwardGearRatio = 0;
			self.motor.forwardGearRatios = {0, 0, 0};
		self.kuplung = true;
	    else
		self.kuplung = false;
	    end;
    	    if self.isMotorStarted and self:getIsActiveForInput() and self.isEntered  then
		if self.reverse and self.ok and not self.kuplung then
			if self.motor.speedLevel ~= 0 then
		        	self.motor.maxRpm[self.motor.speedLevel] = self.motormaxr[self.motor.speedLevel];
				self.maxAccelerationSpeed = -self.motor.maxRpm[self.motor.speedLevel]/100000;
				if self.isFast then
					self.motor.backwardGearRatio = self.bGearRatio;
				else
					self.motor.backwardGearRatio = self.bGearRatio+6;
				end;		
				self.movingDirection = 0;
			elseif -self.input < -0.001 then
				self.maxAccelerationSpeed = 0;
				self.motor.backwardGearRatio = 0;
			else
				self.maxAccelerationSpeed = -self.motormaxi/100000;
				if self.isFast then
					self.motor.backwardGearRatio = self.bGearRatio;
				else
					self.motor.backwardGearRatio = self.bGearRatio+6;
				end;
				self.movingDirection = 0;
			end;
			if self.level ~= 0 then
				self.motor.speedLevel = self.level;
				self.level = 0;
			end;
			if self.motor.speedLevel ~= 0 and self.movingDirection > 0 then
				self.motor.brakeForce = self.bForce;
				self.movingDirection = 1;	
			elseif -self.input < -0.001 then
				self.motor.brakeForce = -self.input*-self.bForce*2;
				self.movingDirection = 1;
				self.brakeLightActive = true;
			elseif -self.input > 0.001 and self.movingDirection > 0 then
				self.motor.brakeForce = -self.input*self.bForce;
				self.movingDirection = 1;
			else
				self.motor.brakeForce = self.bForce/10;
				self.brakeLightActive = false;
			end;
			self.motor.forwardGearRatios = {0, 0, 0};
		elseif self.gang0 then
			if self.motor.speedLevel ~= 0 then
				self.level = self.motor.speedLevel;
				self.motor.speedLevel = 0;
				self.motor.brakeForce = self.bForce;
			elseif -self.input < -0.001 then
				self.motor.brakeForce = -self.input*-self.bForce;
				self.movingDirection = 0;
				self.brakeLightActive = true;
			elseif -self.input > 0.001 then
				self.movingDirection = 0;
			elseif self.level ~= 0 then
				if -self.input > 0.001 or -self.input < -0.001 then
					self.level = 0;
				end;
				self.motor.brakeForce = self.bForce;
			else
				self.brakeLightActive = false;
				self.motor.brakeForce = self.bForce/10;
			end;
			self.maxAccelerationSpeed = 0;
			self.motor.backwardGearRatio = 0;
			self.motor.forwardGearRatios = {0, 0, 0};
		elseif self.gang2 and not self.kuplung then
			
			if self.level ~= 0 then
				self.motor.speedLevel = self.level;
				self.level = 0;
			end;
			if self.motor.speedLevel ~= 0 then
				if self.motor.speedLevel == 4 then
					self.ok = false;
				end;
		        	self.motor.maxRpm[self.motor.speedLevel] = self.motormax[self.motor.speedLevel];
				self.maxAccelerationSpeed = self.motor.maxRpm[self.motor.speedLevel]/100000;
				if self.isFast then
					self.motor.forwardGearRatios = {self.fGearRatios[1]*4, self.fGearRatios[2]*4, self.fGearRatios[3]*4};
				else
					self.motor.forwardGearRatios = {self.fGearRatios[1]*7, self.fGearRatios[2]*7, self.fGearRatios[3]*7};
				end;
			elseif -self.input < -0.001 then
				self.maxAccelerationSpeed = 0;
				self.motor.forwardGearRatios = {0, 0, 0};
			else
				self.maxAccelerationSpeed = self.motormaxi/100000;
				if self.isFast then
					self.motor.forwardGearRatios = {self.fGearRatios[1]*4, self.fGearRatios[2]*4, self.fGearRatios[3]*4};
				else
					self.motor.forwardGearRatios = {self.fGearRatios[1]*7, self.fGearRatios[2]*7, self.fGearRatios[3]*7};
				end;
			end;
				
			if self.motor.speedLevel ~= 0 and self.movingDirection < 0 then
				self.motor.brakeForce = self.bForce;
				self.movingDirection = 1;			
			elseif -self.input < -0.001 then
				self.motor.brakeForce = -self.input*-self.bForce*2;
				self.brakeLightActive = true;
				self.movingDirection = 1;
			elseif -self.input > 0.001 and self.movingDirection < 0 then
				self.motor.brakeForce = -self.input*self.bForce;
				self.movingDirection = 1;
			else
				self.motor.brakeForce = self.bForce/10;
				self.brakeLightActive = false;
			end;
			self.motor.backwardGearRatio = 0;
		elseif self.gang3 and not self.kuplung then
			if self.level ~= 0 then
				self.motor.speedLevel = self.level;
				self.level = 0;
			end;
			if self.motor.speedLevel ~= 0 then
				if self.motor.speedLevel == 4 then
					self.ok = false;
				end;
		        	self.motor.maxRpm[self.motor.speedLevel] = self.motormax[self.motor.speedLevel];
				self.maxAccelerationSpeed = self.motor.maxRpm[self.motor.speedLevel]/100000;
				if self.isFast then
					self.motor.forwardGearRatios = {self.fGearRatios[1]*2.8, self.fGearRatios[2]*2.8, self.fGearRatios[3]*2.8};
				else
					self.motor.forwardGearRatios = {self.fGearRatios[1]*4.5, self.fGearRatios[2]*4.5, self.fGearRatios[3]*4.5};
				end;
			elseif -self.input < -0.001 then
				self.maxAccelerationSpeed = 0;
				self.motor.forwardGearRatios = {0, 0, 0};
			else
				self.maxAccelerationSpeed = self.motormaxi/100000;
				if self.isFast then
					self.motor.forwardGearRatios = {self.fGearRatios[1]*2.8, self.fGearRatios[2]*2.8, self.fGearRatios[3]*2.8};
				else
					self.motor.forwardGearRatios = {self.fGearRatios[1]*4.5, self.fGearRatios[2]*4.5, self.fGearRatios[3]*4.5};
				end;
			end;
				
			if self.motor.speedLevel ~= 0 and self.movingDirection < 0 then
				self.motor.brakeForce = self.bForce;
				self.movingDirection = 1;			
			elseif -self.input < -0.001 then
				self.motor.brakeForce = -self.input*-self.bForce*2;
				self.brakeLightActive = true;
				self.movingDirection = 1;
			elseif -self.input > 0.001 and self.movingDirection < 0 then
				self.motor.brakeForce = -self.input*self.bForce;
				self.movingDirection = 1;
			else
				self.motor.brakeForce = self.bForce/10;
				self.brakeLightActive = false;
			end;
			self.motor.backwardGearRatio = 0;
		elseif self.gang4 and not self.kuplung then
			if self.level ~= 0 then
				self.motor.speedLevel = self.level;
				self.level = 0;
			end;
			if self.motor.speedLevel ~= 0 then
				if self.motor.speedLevel == 4 then
					self.ok = false;
				end;
		        	self.motor.maxRpm[self.motor.speedLevel] = self.motormax[self.motor.speedLevel];
				self.maxAccelerationSpeed = self.motor.maxRpm[self.motor.speedLevel]/100000;
				if self.isFast then
					self.motor.forwardGearRatios = {self.fGearRatios[1]*1.5, self.fGearRatios[2]*1.5, self.fGearRatios[3]*1.5};
				else
					self.motor.forwardGearRatios = {self.fGearRatios[1]*2.5, self.fGearRatios[2]*2.5, self.fGearRatios[3]*2.5};
				end;
			elseif -self.input < -0.001 then
				self.maxAccelerationSpeed = 0;
				self.motor.forwardGearRatios = {0, 0, 0};
			else
				self.maxAccelerationSpeed = self.motormaxi/100000;
				if self.isFast then
					self.motor.forwardGearRatios = {self.fGearRatios[1]*1.5, self.fGearRatios[2]*1.5, self.fGearRatios[3]*1.5};
				else
					self.motor.forwardGearRatios = {self.fGearRatios[1]*2.5, self.fGearRatios[2]*2.5, self.fGearRatios[3]*2.5};
				end;
			end;
				
			if self.motor.speedLevel ~= 0 and self.movingDirection < 0 then
				self.motor.brakeForce = self.bForce;
				self.movingDirection = 1;			
			elseif -self.input < -0.001 then
				self.motor.brakeForce = -self.input*-self.bForce*2;
				self.brakeLightActive = true;
				self.movingDirection = 1;
			elseif -self.input > 0.001 and self.movingDirection < 0 then
				self.motor.brakeForce = -self.input*self.bForce;
				self.movingDirection = 1;
			else
				self.motor.brakeForce = self.bForce/10;
				self.brakeLightActive = false;
			end;
			self.motor.backwardGearRatio = 0;

		end;
		if self.reverse then
			if self.kardan1Node ~= nil and self.kardan2Node ~= nil then
				if self.wheelRpm ~= 0 then
         				rotate(self.kardan1Node, self.wheelRpm*-0.1, 0, 0);
        				rotate(self.kardan2Node, self.wheelRpm*0.1, 0, 0);
				end;
			end;
		else
			if self.kardan1Node ~= nil and self.kardan2Node ~= nil then
         			rotate(self.kardan1Node, self.wheelRpm*0.1, 0, 0);
        			rotate(self.kardan2Node, self.wheelRpm*-0.1, 0, 0);
			end;
		end;

		if not self.ok then
			if self.movingDirection <= 0 then
				self.ok = true;
			end;
		end;
	    end;
			if InputBinding.hasEvent(InputBinding.CONTROL_SWITCH) then
				self.isMouseActive = not self.isMouseActive;
				
				if not self.isMouseActive then
					InputBinding.wrapMousePositionEnabled = true;
					self.cameras[self.camIndex].isActivated = true;
				end;
			end;
			if self.isMouseActive then
				self.cameras[self.camIndex].isActivated = false;			
				InputBinding.wrapMousePositionEnabled = false;
				self.hudGearPoxX = self.lastMouseXPos-0.1;
				self.hudGearPoxY = self.lastMouseYPos-0.1;
    				self.hudGearOverlay = Overlay:new("hudGearpanel", self.gearfile, self.hudGearPoxX, self.hudGearPoxY, self.hudGearWidth, self.hudGearHeight);
			end;
    	else
		self.motor.backwardGearRatio = self.bGearRatio;
		self.motor.forwardGearRatios = self.fGearRatios;
		self.motor.forwardTorqueCurve = self.forwardTorqueCurve0;
		self.motor.backwardTorqueCurve = self.backwardTorqueCurve2;
		self.motor.maxRpm[self.motor.speedLevel] = self.motormax[self.motor.speedLevel];
		self.maxAccelerationSpeed = self.motormaxi/100000;
		self.reverse = false;
		self.gang0 = true;
		self.gang2 = false;
		self.gang3 = false;
		self.gang4 = false;
		

		if -self.input < -0.001 and self.movingDirection > 0 then
			self.brakeLightActive = true;
			self.motor.brakeForce = -self.input*-self.bForce*2;
		elseif -self.input > 0.001 and self.movingDirection < 0 then
    			self.brakeLightActive = true;
			self.motor.brakeForce = -self.input*self.bForce;
		else
			self.brakeLightActive = false;
			self.motor.brakeForce = self.bForce/10;
		end;
		if self.motor.speedLevel ~= 0 and self.movingDirection < 0 then
			self.motor.brakeForce = self.bForce;
		end;
		if self.kardan1Node ~= nil and self.kardan2Node ~= nil then
         		rotate(self.kardan1Node, self.wheelRpm*0.1*self.movingDirection, 0, 0);
        		rotate(self.kardan2Node, self.wheelRpm*-0.1*self.movingDirection, 0, 0);
		end;
    	end;		
      end;
    if self.brakeLight ~= nil then
    	if self.brakeLightActive then
            	setVisibility(self.brakeLight, self.brakeLightActive);
	else
            	setVisibility(self.brakeLight, self.brakeLightActive);
	end;
    end;

    if Input.isKeyPressed(Input.KEY_pageup) or Input.isKeyPressed(Input.KEY_pagedown) or self.camIndex == 2 then
	self.cameras[self.camIndex].allowTranslation = false;
    else
	self.cameras[self.camIndex].allowTranslation = true;
    end;

    if self.motor.speedLevel ~= 0 or self.isAITractorActivated then
	if Input.isKeyPressed(Input.KEY_pageup) and self.motor.maxRpm[self.motor.speedLevel] < self.motormaxi then
		if self.reverse then
			self.motormaxr[self.motor.speedLevel] = self.motormaxr[self.motor.speedLevel] + 10;
		else
			self.motormax[self.motor.speedLevel] = self.motormax[self.motor.speedLevel] + 10;
		end;
	elseif Input.isKeyPressed(Input.KEY_pagedown) and self.motor.maxRpm[self.motor.speedLevel] > self.motor.minRpm then
		if self.reverse then
			self.motormaxr[self.motor.speedLevel] = self.motormaxr[self.motor.speedLevel] - 10;
		else
			self.motormax[self.motor.speedLevel] = self.motormax[self.motor.speedLevel] - 10;
		end;
	end;
			
    end;

end;



function powershift:draw()

    if self.isMotorStarted and self.motor.speedLevel ~= 0 then	
    	g_currentMission:addExtraPrintText("KEY Pageup/KEY Pagedown:   "..string.format("%d RPM",self.motor.maxRpm[self.motor.speedLevel]).."");
    end;
    if not self.off then
    	if not self.isAITractorActivated then
		if self.isMouseActive then
		 	g_currentMission:addExtraPrintText("PERIOD: position panel");
		end;
		renderText(0.78, 0.22, 0.019, "Espaco: CAMBIO DESATIVADO");
		if self.hudGearOverlay ~= nil then
    			self.hudGearOverlay:render();
		end;
		if self.isSlow then
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0245, self.hudGearPoxY+0.087, 0.030, "l");
		end;
		if self.isFast then
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0245, self.hudGearPoxY+0.11, 0.030, "l");
		end;
		if self.gang0 then
			setTextColor(0,0.5,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0535, self.hudGearPoxY+0.099, 0.030, "N");
		else
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0535, self.hudGearPoxY+0.099, 0.030, "N");
		end;
		
		if self.gang2 then
			if self.isFast then
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.128, self.hudGearPoxY+0.063, 0.028, "2");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.113, self.hudGearPoxY+0.063, 0.028, "1");
			else
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.113, self.hudGearPoxY+0.063, 0.028, "1");
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.128, self.hudGearPoxY+0.063, 0.028, "2");
			end;
		else
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.128, self.hudGearPoxY+0.063, 0.028, "2");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.113, self.hudGearPoxY+0.063, 0.028, "1");
		end;
		if self.gang3 then
			if self.isFast then
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.091, self.hudGearPoxY+0.137, 0.028, "4");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0755, self.hudGearPoxY+0.137, 0.028, "3");
			else
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0755, self.hudGearPoxY+0.137, 0.028, "3");
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.091, self.hudGearPoxY+0.137, 0.028, "4");
			end;
		else
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.091, self.hudGearPoxY+0.137, 0.028, "4");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.0755, self.hudGearPoxY+0.137, 0.028, "3");
		end;
		if self.gang4 then
			if self.isFast then
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.091, self.hudGearPoxY+0.063, 0.028, "6");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.076, self.hudGearPoxY+0.063, 0.028, "5");
			else
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.076, self.hudGearPoxY+0.063, 0.028, "5");
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.091, self.hudGearPoxY+0.063, 0.028, "6");
			end;
		else
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.091, self.hudGearPoxY+0.063, 0.028, "6");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.076, self.hudGearPoxY+0.063, 0.028, "5");
		end;
		

		if self.reverse then
		    if self.isFast then
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.159, self.hudGearPoxY+0.094, 0.017, "R");
			renderText(self.hudGearPoxX+0.163, self.hudGearPoxY+0.063, 0.028, "2");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.149, self.hudGearPoxY+0.063, 0.028, "1");
			else
			setTextColor(1.0,0.3,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.159, self.hudGearPoxY+0.094, 0.017, "R");
			renderText(self.hudGearPoxX+0.149, self.hudGearPoxY+0.063, 0.028, "1");
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.163, self.hudGearPoxY+0.063, 0.028, "2");
			end;
		else
			setTextColor(0,0,0,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.159, self.hudGearPoxY+0.094, 0.017, "R");
			renderText(self.hudGearPoxX+0.163, self.hudGearPoxY+0.063, 0.028, "2");
			setTextColor(1,1,1,1);
			setTextBold(true);
			renderText(self.hudGearPoxX+0.149, self.hudGearPoxY+0.063, 0.028, "1");
		end;
    	end;
    else
	renderText(0.78, 0.22, 0.019, "Espaco: Cambio Ativado");
    end;

end;

function powershift:onLeave()

    self.brakeLightActive = false;
    if self.brakeLight ~= nil then
    	setVisibility(self.brakeLight, self.brakeLightActive);
    end;

end;

