local ventSwitch = 3 --0-Extra A, 1-Extra B, etc..
local fanSwitch = 4 --0-Extra A, 1-Extra B, etc..
local ventTempTreshold = 82 --i've set the treshold to 85, real value is 90, increase or decrease if you want to
local fanTempOnTreshold = 80 
local fanTempOffTreshold = 74 
local openVents = false --are the vents open?
local spinFans = false --are radiator fans spinning?
local isOverride = false --if the user has pressed extra switch

function VentsBehaviour(isOverride)
    if (isOverride) then --if the user has pressed extra switch
        openVents = true
    elseif (car.waterTemperature >= ventTempTreshold)  then --always up if temps are high
        openVents = true
    elseif (car.speedKmh > 20 and car.waterTemperature < ventTempTreshold) then --if the speed is above 20kmh and temps are fine
        openVents = false
    elseif (car.speedKmh <= 20 and car.waterTemperature < ventTempTreshold) then --if the speed is below 20kmh and temps are fine
        openVents = false
    end
end

function FansBehaviour()
	if(car.waterTemperature >= fanTempOnTreshold) then --if temps high then spin fans
		spinFans = true
	elseif (car.waterTemperature <= fanTempOffTreshold) then --if temps low stop
		spinFans = false
	end
end


function script.update(dt)
    if(car.extraD ~= openVents) then --user overriden extra switch
        isOverride = not isOverride
    end
    VentsBehaviour(isOverride)
	FansBehaviour()
    ac.setExtraSwitch(ventSwitch, openVents)
	ac.setExtraSwitch(fanSwitch, spinFans)
end