-- LUA für realistisches Partikelsystem am Auspuff
-- basiert im Grundsatz dynamicExhaustSystem.lua von Face bzw. dessen Shader ausm dem Kirovets K 700A
--
-- by modelleicher
-- www.schwabenmodding.bplaced.net
-- V3 konvertiert zum LS13, Performanceoptimierung und Code aufgeräumt
-- Kommentare in Englisch
-- LUA for realistic exhaust particles. Based on the DynamicExhaustingSystemShader.xml from Face
-- V3 converted to FS13, performance optimized and cleaned the code, also added english comments


realExhaustParticleSystemV3 = {};

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

function realExhaustParticleSystemV3:load(xmlFile)
	self.reps = {}; -- real exhaust particle system table.
	self.reps.minAlpha = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.realExhaustParticleSystem#minAlpha"), 0.1);
	self.reps.maxAlpha = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.realExhaustParticleSystem#maxAlpha"), 1);
	self.reps.parameter = Utils.getNoNil(getXMLString(xmlFile, "vehicle.realExhaustParticleSystem#parameter"), "exhaustingSystem");
	self.reps.realAlpha = self.reps.minAlpha; -- current alpha value
	self.reps.isAcc = false; -- bool is accelerating
	self.reps.accBackup = self.motor.minRpm + 150; -- value needet for acceleration true/false check
	-- new in V3, optional second particleSystem for better looking, but lower performance
	self.reps.hasSecondPS = getXMLBool(xmlFile, "vehicle.realExhaustParticleSystem#hasSecondPS");
	if self.reps.hasSecondPS == true then
		self.reps.secondPS = {};
		Utils.loadParticleSystem(xmlFile, self.reps.secondPS, "vehicle.realExhaustParticleSystem.SecondParticleSystem", self.components, false, nil, self.baseDirectory)
		self.reps.secondPSOn = false;
	end;	
end;

function realExhaustParticleSystemV3:delete()
	if self.reps.secondPS ~= nil then
		Utils.deleteParticleSystem(self.reps.secondPS);
	end;
end;
function realExhaustParticleSystemV3:mouseEvent(posX, posY, isDown, isUp, button)
end;
function realExhaustParticleSystemV3:keyEvent(unicode, sym, modifier, isDown)
end;
function realExhaustParticleSystemV3:update(dt)
	if self:getIsActiveForInput() and self.isMotorStarted or self.isServer and self:getIsActive() and self.isMotorStarted then
		-- check if tractor is accelerating
		if self.reps.accBackup < self.motor.lastMotorRpm then
			self.reps.isAcc = true;
		elseif self.reps.accBackup > self.motor.lastMotorRpm then
			self.reps.isAcc = false;
		end;
		-- acc 0 , 1 means if key for acceleration is pressed, or 0 to 1 float for pedal axis
		local acc = 0;
		acc = -InputBinding.getAnalogInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);
		if InputBinding.isAxisZero(acc) then
			acc = -InputBinding.getDigitalInputAxis(InputBinding.AXIS_MOVE_FORWARD_VEHICLE);
		end;		
		-- speedLevels 1,2,3,4:
		local increaseAlpha = nil;
		if self.motor.speedLevel == 1 then
			if self.motor.lastMotorRpm < (self.motor.maxRpm[1] - 100) then
				increaseAlpha = true;
			else
				increaseAlpha = false;
			end;
		elseif self.motor.speedLevel == 2 then
			if self.motor.lastMotorRpm < (self.motor.maxRpm[2] - 200) then
				increaseAlpha = true;
			else
				increateAlpha = false;
			end;
		elseif self.motor.speedLevel == 3 then
			if self.motor.lastMotorRpm < (self.motor.maxRpm[3] - 250) then
				increaseAlpha = true;
			else
				increaseAlpha = false;
			end;
		elseif self.motor.speedLevel == 4 then
			if self.motor.lastMotorRpm < (self.motor.maxRpm[4] - 150) then		
				increaseAlpha = true;
			else
				increaseAlpha = false;
			end;
		end;
		
		-- if no speedlevel active
		if self.motor.speedLevel == 0 then
			if self.reps.isAcc == true then -- only more alpha if accelerating
				self.reps.realAlpha = math.min(self.reps.maxAlpha, self.reps.realAlpha + 0.01);
				self.reps.secondPSOn = true;
			elseif self.reps.isAcc == false then
				self.reps.realAlpha = math.max(self.reps.minAlpha, self.reps.realAlpha - 0.1);	
				self.reps.secondPSOn = false;
			end;
			-- if acc == 1 means full power but not full speed, also more alpha
			if acc == 1 then
				if self.motor.lastMotorRpm < (self.motor.maxRpm[3] - 250) then 
					increaseAlpha = true;
				else
					self.reps.secontPSOn = false;
				end;
			end;
		end;
		
		-- calculate current alpha value
		if increaseAlpha == true then
			self.reps.realAlpha = math.min(self.reps.maxAlpha, self.reps.realAlpha + 0.01);
			--self.reps.secondPSOn = true;		
		elseif increaseAlpha == false then
			self.reps.realAlpha = math.max(self.reps.minAlpha, self.reps.realAlpha - 0.01);	
			--self.reps.secondPSOn = false;			
		end;
		-- if secondPS is active
		if self.reps.hasSecondPS then
			--if self.reps.secondPSOn then
			if self.reps.realAlpha > (self.reps.maxAlpha * 0.8) then
				Utils.setEmittingState(self.reps.secondPS, true);
			else
				Utils.setEmittingState(self.reps.secondPS, false)
			end;
		end;
		-- set new alpha value to the shader
		setShaderParameter(self.exhaustParticleSystems[1].shape, self.reps.parameter, 0, 0, 0, self.reps.realAlpha, false);		
		self.reps.accBackup = self.motor.lastMotorRpm;			
	end;
end;
function realExhaustParticleSystemV3:onLeave()
	if self.reps.hasSecondPS then
		Utils.setEmittingState(self.reps.secondPS, false);
	end;
end;
function realExhaustParticleSystemV3:draw()
end;
