

TurboBlowOff = {};


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


function TurboBlowOff:load(savegame)
    if self.isClient then
        self.TurboBlowOff = {};
        self.TurboBlowOff.sample = Utils.loadSample(xmlFile, {}, "vehicle.soundTurboBlowOff", nil, self.baseDirectory);
        self.TurboBlowOff.accelerate = false;
        self.TurboBlowOff.decelerate = false;
        self.TurboBlowOff.braking = false;
    end;
end;


function TurboBlowOff:delete()
    if self.isClient then
        Utils.deleteSample(self.TurboBlowOff.sample);
    end;
end;


function TurboBlowOff:readStream(streamId, connection)
end;


function TurboBlowOff:writeStream(streamId, connection)
end;


function TurboBlowOff:readUpdateStream(streamId, timestamp, connection)
end;


function TurboBlowOff:writeUpdateStream(streamId, connection, dirtyMask)
end;


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


function TurboBlowOff:keyEvent(unicode, sym, modifier, isDown)
    if isDown then
        if sym == Input.KEY_w then
            self.TurboBlowOff.accelerate = true;
            self.TurboBlowOff.decelerate = false;
        end;
        if sym == Input.KEY_s then
            self.TurboBlowOff.braking = true;
        end;
    else
        if sym == Input.KEY_w then
            self.TurboBlowOff.accelerate = false;
            self.TurboBlowOff.decelerate = true;
        end;
    end;
end;


function TurboBlowOff:update(dt)

    if self.isMotorStarted then
        if self.isClient then
            if self:getIsActiveForSound() then
                if self.TurboBlowOff.sample ~= nil then
                    if self.TurboBlowOff.decelerate then
                        --[[
                        pcall(function () print("=") end);
                        pcall(function () print("self.axisForward".."="..tostring(self.axisForward)) end);
                        pcall(function () print("self:getLastSpeed()".."="..tostring(self:getLastSpeed())) end);
                        pcall(function () print("self.movingDirection".."="..tostring(self.movingDirection)) end);
                        pcall(function () print("self.reverserDirection".."="..tostring(self.reverserDirection)) end);
                        pcall(function () print("self.motor.minRpm".."="..tostring(self.motor.minRpm)) end);
                        pcall(function () print("self.motor.minRpm".."="..tostring(self.motor.minRpm)) end);
                        pcall(function () print("self.motor.maxRpm".."="..tostring(self.motor.maxRpm)) end);
                        pcall(function () print("self.motor.maxForwardSpeed".."="..tostring(self.motor.maxForwardSpeed)) end);
                        pcall(function () print("self.motor.lastMotorRpm".."="..tostring(self.motor.lastMotorRpm)) end);
                        pcall(function () print("self.lastSpeed".."="..tostring(self.lastSpeed)) end);
                        pcall(function () print("self.lastRoundPerMinute".."="..tostring(self.lastRoundPerMinute)) end);
                        pcall(function () print("=") end);
                        pcall(print("".."="..tostring()));
                        --]]

                        Utils.playSample(self.TurboBlowOff.sample, 1, 0, nil);
                        self.TurboBlowOff.decelerate = false;
                    end;
                end;
            end;
        end;
    end;

    --[[

    if self.isMotorStarted then
        local accInput = 0;
        if self.axisForward ~= nil then
            accInput = -self.axisForward;
        end;
        if self.cruiseControl ~= nil and self.cruiseControl.state ~= Drivable.CRUISECONTROL_STATE_OFF then
            accInput = 1;
        end
        if self.isClient then
            if self:getIsActiveForSound() then
                if self.compressionSoundTime <= g_currentMission.time then
                    Utils.playSample(self.sampleCompression, 1, 0, nil);
                    self.compressionSoundTime = g_currentMission.time + 180000;
                end
            end;
            if self.sampleReverseDrive.sample ~= nil then
                if (accInput < 0 or accInput == 0) and (self:getLastSpeed() > 3 and self.movingDirection ~= self.reverserDirection) then
                    if self:getIsActiveForSound() then
                        Utils.playSample(self.sampleReverseDrive, 0, 0, nil);
                    end;
                else
                    Utils.stopSample(self.sampleReverseDrive);
                end;
            end;
        end;
        if table.getn(self.wheels) > 0 then
            local minRpm = self.motor.minRpm;
            local maxRpm = self.motor.maxRpm;
            local maxSpeed;
            if self.movingDirection >= 0 then
                maxSpeed = self.motor.maxForwardSpeed*0.001;
            else
                maxSpeed = self.motor.maxBackwardSpeed*0.001;
            end
            -- The actual rpm offset is 80% from the motor and 20% from the speed
            local targetRpmOffset = (self.motor.lastMotorRpm-minRpm)*0.8 + math.min(self.lastSpeed/maxSpeed, 1)*(maxRpm-minRpm)*0.2;
            local alpha = math.pow(0.01, dt*0.001);
            local roundPerMinute = targetRpmOffset + alpha*(self.lastRoundPerMinute-targetRpmOffset);
            self.lastRoundPerMinute = roundPerMinute;
            local roundPerSecond = roundPerMinute / 60;
            if self.isClient then
                local motorSoundPitch = math.min(self.sampleMotor.pitchOffset + self.motorSoundPitchScale*math.abs(roundPerSecond), self.motorSoundPitchMax)
                Utils.setSamplePitch(self.sampleMotor, motorSoundPitch);
                local motorSoundRunPitch = math.min(self.sampleMotorRun.pitchOffset + self.motorSoundRunPitchScale*math.abs(roundPerSecond), self.motorSoundRunPitchMax)
                Utils.setSamplePitch(self.sampleMotorRun, motorSoundRunPitch);
                local rpmRunVolume = math.abs(roundPerMinute)/(maxRpm - minRpm);
                if math.abs(accInput) < 0.01 then
                    rpmRunVolume = rpmRunVolume * 0.666;
                end;
                rpmRunVolume = Utils.clamp(rpmRunVolume, 0.0, 1.0);
                Utils.setSampleVolume(self.sampleMotorRun, rpmRunVolume*self.sampleMotorRun.volume);
                local speedFactor = Utils.clamp(self:getLastSpeed() / math.ceil(self.motor:getMaximumForwardSpeed()*3.6), 0, 1);
                local pitchRun2 = Utils.lerp(self.sampleMotorRun2.pitchOffset, self.motorRun2PitchMax, speedFactor);
                local volumeRun2 = Utils.lerp(self.sampleMotorRun2.volume, self.motorRun2VolumeMax, speedFactor);
                Utils.setSamplePitch(self.sampleMotorRun2, pitchRun2)
                Utils.setSampleVolume(self.sampleMotorRun2, volumeRun2);
                if self.sampleCompressedAir.sample ~= nil then
                    if self.movingDirection > 0 and self.lastSpeed > self.motor.maxForwardSpeed*0.0005 then -- faster than 50% of max speed
                        if accInput > 0.5 then
                            -- if we drive fast enough, we need to run the compressor the next time we brake
                            self.compressedAirSoundEnabled = false;
                        elseif accInput < -0.7 then
                            -- play the compressor sound if we drive fast enough and brake
                            if not self.compressedAirSoundEnabled then
                                if self:getIsActiveForSound() then
                                    Utils.playSample(self.sampleCompressedAir, 1, 0, nil);
                                end;
                                self.compressedAirSoundEnabled = true;
                            end
                        end;
                    end
                end
            end;
        end;
    end;
    ]]
end;


function TurboBlowOff:updateTick(dt)
end;


function TurboBlowOff:draw()
end;


function TurboBlowOff:stopSounds()
    if self.isClient then
        Utils.stopSample(self.TurboBlowOff.sample, true);
    end;
end;


function TurboBlowOff:onLeave()
    TurboBlowOff.stopSounds(self);
end

