
local data2 = ac.getSim()
if car.isAIControlled and data2.isInMainMenu then
  return nil
end

local rpm_limit = 0
local time1 = 0
local time2 = 0
local limiter_hz = 20
local data = ac.getCar()
local old_rpm_limit = data.rpmLimiter

local set_boost_by_gear = 0 

local dyno_preload = 1
local old_dyno_preload = 1
local data = ac.getCarPhysics()
local gear_ratio_stock = data.finalRatio 

function script.update(dt)
  local data = ac.getCar()
  local data2 = ac.accessCarPhysics()  

  ac.store("turbo_count", data.turboCount)

  for i = 0, data.turboCount do
    if ac.load("set_boost_by_gear") ~= nil then
      set_boost_by_gear = ac.load("set_boost_by_gear") * 0.0689476 --psi to bar
      ac.setTurboMaxBoost(i, set_boost_by_gear / data.turboCount) 
	end
  end 


  if ac.load("dyno_preload") ~= nil then
    dyno_preload = ac.load("dyno_preload")
  end
  gear_ratio = gear_ratio_stock - (gear_ratio_stock * dyno_preload / 100)  
  if dyno_preload ~= old_dyno_preload then
    old_dyno_preload = dyno_preload
    ac.setGearsFinalRatio(gear_ratio)
  end

  
  if data.extraE == true then 
    ac.store("extraE", 1)
  else
    ac.store("extraE", 0)
  end

  ac.store("rpm_limiter1", old_rpm_limit)  
  if data.extraE == false then
    rpm_limit = old_rpm_limit
    ac.setEngineRPMLimit(old_rpm_limit, 'true')
  end

  if ac.load("rpm_limiter2") ~= nil and data.extraE == true then
    rpm_limit = ac.load("rpm_limiter2")
    ac.setEngineRPMLimit(rpm_limit, 'true')
  end

  if ac.load("hz1") ~= nil and data.extraE == false then
    limiter_hz = ac.load("hz1")
  end

  if ac.load("hz2") ~= nil and data.extraE == true then
    limiter_hz = ac.load("hz2")
  end
  
  time1 = os.clock() * 1000
  if (time2 + (1000 / limiter_hz)) < time1 then
    if (data2.rpm + 500) > rpm_limit then
      data2.gas = 0
      ac.log("here")
    end
  end
  if (time2 + (2000 / limiter_hz)) < time1 then

    time2 = os.clock() * 1000
  end
  


end