-- motorsoundConfiguration.lua
-- sets an individual engine sound for engine configurations
--
-- Author Rival with help from bassaddict
-- 26.10.2016

motorsoundConfiguration = {};

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

function motorsoundConfiguration:load(savegame)
end;
function motorsoundConfiguration:loadSounds(xmlFile, motorId)
    if self.isClient then
		local xmlFile = self.xmlFile;
		local key, motorId = Vehicle.getXMLConfigurationKey(xmlFile, self.configurations["motor"], "vehicle.motorConfigurations.motorConfiguration", "vehicle", "motorsound");
		
        self.sampleRefuel = SoundUtil.loadSample(xmlFile, {}, "vehicle.refuelSound", "$data/maps/sounds/refuel.wav", self.baseDirectory, self.components[1].node);
		if hasXMLProperty(xmlFile, key..".motorStartSound") then
			self.sampleMotorStart = SoundUtil.loadSample(xmlFile, {}, key..".motorStartSound", nil, self.baseDirectory);
			self.sampleMotorStop = SoundUtil.loadSample(xmlFile, {}, key..".motorStopSound", nil, self.baseDirectory);
			self.sampleMotor = SoundUtil.loadSample(xmlFile, {}, key..".motorSound", nil, self.baseDirectory, self.components[1].node);
			self.sampleMotorRun = SoundUtil.loadSample(xmlFile, {}, key..".motorSoundRun", nil, self.baseDirectory, self.components[1].node);
			self.sampleMotorLoad = SoundUtil.loadSample(xmlFile, {}, key..".motorSoundLoad", nil, self.baseDirectory, self.components[1].node);
			self.sampleGearbox = SoundUtil.loadSample(xmlFile, {}, key..".gearboxSound", nil, self.baseDirectory, self.components[1].node);
			self.sampleRetarder = SoundUtil.loadSample(xmlFile, {}, key..".retarderSound", nil, self.baseDirectory, self.components[1].node);
			self.sampleBrakeCompressorStart = SoundUtil.loadSample(xmlFile, {}, key..".brakeCompressorStartSound", nil, self.baseDirectory);
			self.sampleBrakeCompressorRun = SoundUtil.loadSample(xmlFile, {}, key..".brakeCompressorRunSound", nil, self.baseDirectory);
			self.sampleBrakeCompressorStop = SoundUtil.loadSample(xmlFile, {}, key..".brakeCompressorStopSound", nil, self.baseDirectory);
			self.sampleCompressedAir = SoundUtil.loadSample(xmlFile, {}, key..".compressedAirSound", nil, self.baseDirectory);
			self.sampleAirReleaseValve = SoundUtil.loadSample(xmlFile, {}, key..".airReleaseValveSound", nil, self.baseDirectory);
		else
			self.sampleMotorStart = SoundUtil.loadSample(xmlFile, {}, "vehicle.motorStartSound", nil, self.baseDirectory);
			self.sampleMotorStop = SoundUtil.loadSample(xmlFile, {}, "vehicle.motorStopSound", nil, self.baseDirectory);
			self.sampleMotor = SoundUtil.loadSample(xmlFile, {}, "vehicle.motorSound", nil, self.baseDirectory, self.components[1].node);
			self.sampleMotorRun = SoundUtil.loadSample(xmlFile, {}, "vehicle.motorSoundRun", nil, self.baseDirectory, self.components[1].node);
			self.sampleMotorLoad = SoundUtil.loadSample(xmlFile, {}, "vehicle.motorSoundLoad", nil, self.baseDirectory, self.components[1].node);
			self.sampleGearbox = SoundUtil.loadSample(xmlFile, {}, "vehicle.gearboxSound", nil, self.baseDirectory, self.components[1].node);
			self.sampleRetarder = SoundUtil.loadSample(xmlFile, {}, "vehicle.retarderSound", nil, self.baseDirectory, self.components[1].node);
			self.sampleBrakeCompressorStart = SoundUtil.loadSample(xmlFile, {}, "vehicle.brakeCompressorStartSound", nil, self.baseDirectory);
			self.sampleBrakeCompressorRun = SoundUtil.loadSample(xmlFile, {}, "vehicle.brakeCompressorRunSound", nil, self.baseDirectory);
			self.sampleBrakeCompressorStop = SoundUtil.loadSample(xmlFile, {}, "vehicle.brakeCompressorStopSound", nil, self.baseDirectory);
			self.sampleCompressedAir = SoundUtil.loadSample(xmlFile, {}, "vehicle.compressedAirSound", nil, self.baseDirectory);
			self.sampleAirReleaseValve = SoundUtil.loadSample(xmlFile, {}, "vehicle.airReleaseValveSound", nil, self.baseDirectory);
		end;
        self.sampleReverseDrive = SoundUtil.loadSample(xmlFile, {}, "vehicle.reverseDriveSound", nil, self.baseDirectory);
        local maxRpmDelta = (self.motor:getMaxRpm() - self.motor:getMinRpm());
        local maxRpsDelta = maxRpmDelta / 60;
		if hasXMLProperty(xmlFile, key..".motorSound") then
			if self.sampleMotor.sample ~= nil then
				self.motorSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSound#pitchMax"), 2.0);
				self.motorSoundPitchScale = getXMLFloat(xmlFile, key..".motorSound#pitchScale");
				if self.motorSoundPitchScale == nil then
					self.motorSoundPitchScale = (self.motorSoundPitchMax - self.sampleMotor.pitchOffset) / maxRpsDelta;
				end
				self.motorSoundVolumeMin = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSound#volumeMin"), self.sampleMotor.volume);
				self.motorSoundVolumeMinSpeed = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSound#volumeMinSpeed"), math.huge);
			end
			if self.sampleMotorRun.sample ~= nil then
				self.motorSoundRunPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSoundRun#pitchMax"), 2.0);
				self.motorSoundRunPitchScale = getXMLFloat(xmlFile, key..".motorSoundRun#pitchScale");
				if self.motorSoundRunPitchScale == nil then
					self.motorSoundRunPitchScale = (self.motorSoundRunPitchMax - self.sampleMotorRun.pitchOffset) / maxRpsDelta;
				end
				self.motorSoundRunMinimalVolumeFactor = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSoundRun#minimalVolumeFactor"), 0.0);
			end
			if self.sampleMotorLoad.sample ~= nil then
				self.motorSoundLoadPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSoundLoad#pitchMax"), 2.0);
				self.motorSoundLoadPitchScale = getXMLFloat(xmlFile, key..".motorSoundLoad#pitchScale");
				if self.motorSoundLoadPitchScale == nil then
					self.motorSoundLoadPitchScale = (self.motorSoundLoadPitchMax - self.sampleMotorLoad.pitchOffset) / maxRpsDelta;
				end
				self.motorSoundLoadMinimalVolumeFactor = Utils.getNoNil(getXMLFloat(xmlFile, key..".motorSoundLoad#minimalVolumeFactor"), 0.0);
				self.motorSoundLoadFactor = 0;
			end
			if self.sampleGearbox.sample ~= nil then
				self.gearboxSoundVolumeMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#volumeMax"), 2.0);
				self.gearboxSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#pitchMax"), 2.0);
				self.gearboxSoundReverseVolumeMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#reverseVolumeMax"), self.gearboxSoundVolumeMax);
				self.gearboxSoundReversePitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#reversePitchMax"), self.gearboxSoundPitchMax);
				self.gearboxSoundPitchExponent = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#pitchExponent"), 1.0);
			end
			if self.sampleRetarder.sample ~= nil then
				self.retarderSoundVolumeMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".retarderSound#volumeMax"), 2.0);
				self.retarderSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".retarderSound#pitchMax"), 2.0);
				self.retarderSoundMinSpeed = Utils.getNoNil(getXMLFloat(xmlFile, key..".retarderSound#minSpeed"), 5.0);
				self.retarderSoundActualVolume = 0.0;
			end
			self.pitchInfluenceFromWheels = 0;
			self.volumeInfluenceFromWheels = 0;
			self.wheelInfluenceOnGearboxSoundVolume = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#wheelInfluenceOnVolume"), 0.1);
			self.wheelInfluenceOnGearboxSoundPitch = Utils.getNoNil(getXMLFloat(xmlFile, key..".gearboxSound#wheelInfluenceOnPitch"), 0.2);
			self.wheelInfluenceOnRetarderSoundVolume = Utils.getNoNil(getXMLFloat(xmlFile, key..".retarderSound#wheelInfluenceOnVolume"), 0.1);
			self.wheelInfluenceOnRetarderSoundPitch = Utils.getNoNil(getXMLFloat(xmlFile, key..".retarderSound#wheelInfluenceOnPitch"), 0.2);
			if self.sampleBrakeCompressorRun.sample ~= nil then
				self.brakeCompressorRunSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, key..".brakeCompressorRunSound#pitchMax"), 2.0);
				self.brakeCompressorRunSoundPitchScale = getXMLFloat(xmlFile, key..".brakeCompressorRunSound#pitchScale");
				if self.brakeCompressorRunSoundPitchScale == nil then
					self.brakeCompressorRunSoundPitchScale = (self.brakeCompressorRunSoundPitchMax - self.sampleBrakeCompressorRun.pitchOffset) / maxRpsDelta;
				end
			end
		else
			if self.sampleMotor.sample ~= nil then
				self.motorSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSound#pitchMax"), 2.0);
				self.motorSoundPitchScale = getXMLFloat(xmlFile, "vehicle.motorSound#pitchScale");
				if self.motorSoundPitchScale == nil then
					self.motorSoundPitchScale = (self.motorSoundPitchMax - self.sampleMotor.pitchOffset) / maxRpsDelta;
				end
				self.motorSoundVolumeMin = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSound#volumeMin"), self.sampleMotor.volume);
				self.motorSoundVolumeMinSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSound#volumeMinSpeed"), math.huge);
			end
			if self.sampleMotorRun.sample ~= nil then
				self.motorSoundRunPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSoundRun#pitchMax"), 2.0);
				self.motorSoundRunPitchScale = getXMLFloat(xmlFile, "vehicle.motorSoundRun#pitchScale");
				if self.motorSoundRunPitchScale == nil then
					self.motorSoundRunPitchScale = (self.motorSoundRunPitchMax - self.sampleMotorRun.pitchOffset) / maxRpsDelta;
				end
				self.motorSoundRunMinimalVolumeFactor = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSoundRun#minimalVolumeFactor"), 0.0);
			end
			if self.sampleMotorLoad.sample ~= nil then
				self.motorSoundLoadPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSoundLoad#pitchMax"), 2.0);
				self.motorSoundLoadPitchScale = getXMLFloat(xmlFile, "vehicle.motorSoundLoad#pitchScale");
				if self.motorSoundLoadPitchScale == nil then
					self.motorSoundLoadPitchScale = (self.motorSoundLoadPitchMax - self.sampleMotorLoad.pitchOffset) / maxRpsDelta;
				end
				self.motorSoundLoadMinimalVolumeFactor = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.motorSoundLoad#minimalVolumeFactor"), 0.0);
				self.motorSoundLoadFactor = 0;
			end
			if self.sampleGearbox.sample ~= nil then
				self.gearboxSoundVolumeMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#volumeMax"), 2.0);
				self.gearboxSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#pitchMax"), 2.0);
				self.gearboxSoundReverseVolumeMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#reverseVolumeMax"), self.gearboxSoundVolumeMax);
				self.gearboxSoundReversePitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#reversePitchMax"), self.gearboxSoundPitchMax);
				self.gearboxSoundPitchExponent = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#pitchExponent"), 1.0);
			end
			if self.sampleRetarder.sample ~= nil then
				self.retarderSoundVolumeMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.retarderSound#volumeMax"), 2.0);
				self.retarderSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.retarderSound#pitchMax"), 2.0);
				self.retarderSoundMinSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.retarderSound#minSpeed"), 5.0);
				self.retarderSoundActualVolume = 0.0;
			end
			self.pitchInfluenceFromWheels = 0;
			self.volumeInfluenceFromWheels = 0;
			self.wheelInfluenceOnGearboxSoundVolume = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#wheelInfluenceOnVolume"), 0.1);
			self.wheelInfluenceOnGearboxSoundPitch = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.gearboxSound#wheelInfluenceOnPitch"), 0.2);
			self.wheelInfluenceOnRetarderSoundVolume = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.retarderSound#wheelInfluenceOnVolume"), 0.1);
			self.wheelInfluenceOnRetarderSoundPitch = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.retarderSound#wheelInfluenceOnPitch"), 0.2);
			if self.sampleBrakeCompressorRun.sample ~= nil then
				self.brakeCompressorRunSoundPitchMax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.brakeCompressorRunSound#pitchMax"), 2.0);
				self.brakeCompressorRunSoundPitchScale = getXMLFloat(xmlFile, "vehicle.brakeCompressorRunSound#pitchScale");
				if self.brakeCompressorRunSoundPitchScale == nil then
					self.brakeCompressorRunSoundPitchScale = (self.brakeCompressorRunSoundPitchMax - self.sampleBrakeCompressorRun.pitchOffset) / maxRpsDelta;
				end
			end
        end
        self.brakeCompressor = {};
        self.brakeCompressor.capacity = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.brakeCompressor#capacity"), 6);
        self.brakeCompressor.refillFilllevel = math.min(self.brakeCompressor.capacity, Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.brakeCompressor#refillFillLevel"), self.brakeCompressor.capacity/2));
        self.brakeCompressor.fillSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.brakeCompressor#fillSpeed"), 0.6) / 1000;
        self.brakeCompressor.fillLevel = 0;
        self.brakeCompressor.doFill = true;
        self.soundsAdjustedToIndoorCamera = false;
        self.compressedAirSoundEnabled = false;
        self.compressionSoundTime = 0;
	end;
end;
Motorized.loadSounds = Utils.overwrittenFunction(Motorized.loadSounds, motorsoundConfiguration.loadSounds)

function motorsoundConfiguration:delete()
end;
function motorsoundConfiguration:mouseEvent(posX, posY, isDown, isUp, button)
end;
function motorsoundConfiguration:keyEvent(unicode, sym, modifier, isDown)
end;
function motorsoundConfiguration:update(dt)
end;
function motorsoundConfiguration:updateTick(dt)
end;
function motorsoundConfiguration:draw()    
end;