--Made by WarDaddyUSA

local M = {}

local engtime = 0
local engtimer = 0
local particleAmount = 0
local ignkeySmoother = newExponentialSmoothing(6)
local oilPressureSmoother = newExponentialSmoothing(80)
local voltsSmoother = newExponentialSmoothing(7)
local fuelBaseSmoother = newExponentialSmoothing(120)
local fuelSmoother = newExponentialSmoothing(10)
local watertempSmoother = newExponentialSmoothing(10)

local function init()
   electrics.values.shiftlight = 0
end

local function reset()
   init()
end

local function updateGFX(dt)
   
  if powertrain.getDevice("mainEngine").ignitionCoef ~= 0 and electrics.values.rpm > 100 then electrics.values.ignkey = 2
  else electrics.values.ignkey = 0 end
  
	if electrics.values.rpm < 100 and powertrain.getDevice("mainEngine").ignitionCoef ~= 0 then electrics.values.ignkey = 1 end
 
 
  
    electrics.values.ignkey = ignkeySmoother:get(electrics.values['ignkey'])

   
	
   electrics.values.fuelBase = electrics.values.fuel

	--Oil Press Math
	
    if electrics.values.rpm > 500 and powertrain.getDevice("mainEngine").oilVolume > 2.5 then electrics.values.oilPressure = electrics.values.rpm  *0.0001000 * powertrain.getDevice("mainEngine").oilVolume / 0.05 + 55
	else electrics.values.oilPressure = electrics.values.rpm  *0.2 * powertrain.getDevice("mainEngine").oilVolume / 12.5 end
	

	 electrics.values.oilPressure = oilPressureSmoother:get(electrics.values['oilPressure'])
 
	 
    if electrics.values.ignkey < 1 then electrics.values.fuelBase = 0 end 
	electrics.values.fuelBase = fuelBaseSmoother:get(electrics.values['fuelBase'])
	
	--Voltage
	
    if electrics.values.ignkey > 1.5 then electrics.values.volts = 100
	else electrics.values.volts = 90 end
	if electrics.values.ignkey < 0.5 then electrics.values.volts = 85 end
	if powertrain.getDevice("mainEngine").starterEngagedCoef > 0 then electrics.values.volts = 80 end
	
	--Gauges and lights 
	
	electrics.values.volts = voltsSmoother:get(electrics.values['volts'])
	
	if electrics.values.volts < 85 and electrics.values.ignkey > 0.5  then electrics.values.battery = 1 else electrics.values.battery = 0 end
	
	if electrics.values.oilPressure < 70 and electrics.values.ignkey > 0.5  then   electrics.values.oil = 1 else   electrics.values.oil = 0 end
	
	if electrics.values.ignkey < 0.5 then electrics.values.fuel = 0 end
		
		electrics.values.fuel = fuelSmoother:get(electrics.values['fuel'])

	if electrics.values.ignkey < 0.5 then electrics.values.watertemp = 0 end
		
		electrics.values.watertemp = watertempSmoother:get(electrics.values['watertemp'])
	
	
end


M.onInit = init
M.onReset = init
M.updateGFX = updateGFX

return M