  -- anti_lag pops and bang

local pops_s = ui.MediaPlayer()
pops_s:setSource('./AL_Bang.wav')
pops_s:setLooping(false)

local pops_m = ui.MediaPlayer()
pops_m:setSource('./AL_PopsLong.wav')
pops_m:setLooping(false)

local pops_l = ui.MediaPlayer()
pops_l:setSource('./AL_PopsLong.wav')
pops_l:setLooping(false)

local bang = ui.MediaPlayer()
bang:setSource('./AL_Bang.wav')
bang:setLooping(false)

local flag = 2
local bangLag = 0

function update(dt)
-- anti-lag pops and bangs

  if ac.getCar().gas > 0.7 and car.extraF then
      pops_s:setVolume(0)
      pops_m:setVolume(0)
      pops_l:setVolume(0)
  else
      pops_s:setVolume(0.05) -- Set the volume to 70%
      pops_m:setVolume(0.22) -- Set the volume to 70%
      pops_l:setVolume(0.22) -- Set the volume to 70%
  end

  if flag == 2 and car.extraF and ac.getCar().rpm < 6000 then
      if ac.getCar().gas < 0.60 and ac.getCar().rpm > 3650 then
          pops_s:play()
      end
  end

  if flag == 2 and car.extraF and ac.getCar().rpm < 7000 then
      if ac.getCar().gas < 0.70 and ac.getCar().rpm > 6900 then
          pops_m:play()
      end
  end

  if flag == 2 and car.extraF and ac.getCar().rpm < 8500 then
      if ac.getCar().gas < 0.8 and ac.getCar().rpm > 6900 then
          pops_l:play()
          bangLag = 0
      end
  end

  -- bang note: 8500-9000 RPM is range when bang can be relased if throttle input will be less than 70% (< 0.7)

  if flag == 2 and car.extraF and ac.getCar().rpm < 9000 and not bangLoop then
      bangLag = bangLag + dt
      if ac.getCar().rpm > 8500 and ac.getCar().gas < 0.7 then
          if bangLag >= 1.5 then
              bang:setVolume(1.0) -- Set the volume to 100%
              bang:play()
              bangLag = 0
              bangLoop = true
          elseif bangLag < 1.5 and bangLag >= 0 then
              bang:setVolume(0.5) -- Set the volume to 50%
              bang:play()
              bangLag = 0
              bangLoop = true
          end
      end
  end

  if ac.getCar().rpm < 4000 then -- 4500 rpm activate bang sound again
      bangLoop = false
  end

  if ac.getCar().gas == 1 then -- 100% throttle press activate bang sound again
      bangLoop = false
  end

  -- reset



end


