--simple r34 MFD for kai's R34 GT-R
--has some dodgy parts, but it works eh

local screen = "int_mfd"
--maximum values for items (used for calculating bar lengths)
local maxBoost = 1.2
local maxWater = 130
local maxOil = 150
local maxExhaust = 1000
local maxAmbient = 80
--thresholds for the bars (larger values show up as red)
local boostThreshold = 1.2
local injectorThresold = 99
local oilThreshold = 130
local waterThreshold = 119
local egtThreshold = 970

--peak values for memoryWarning
local peakBoost = 0
local peakThrottle = 0
local peakInjector = 0
local peakOil = 0
local peakWater = 0
local peakEGT = 0
local peakIntake = 0
local currentRPM = 0
local currentIDC = 0
local currentEGT = 0
local currentWtemp = 0
local currentOiltemp = 0

--misc

local barMax = 276
local reset = "int_plastic_1"
local resetCoord = vec2(1207,1852)
local resetSize = vec2(40,45)


local displayMesh = display.interactiveMesh{ mesh = reset, resolution = vec2(2048, 2048)}
local resetBtn = displayMesh.clicked(vec2(1207,1852),vec2(40,45))

local lowestFluidTemp = 48
local lowestEGT = 113


function resetCheck()
	if resetBtn() then
		resetPeak()
	end
end	

function resetPeak()
	peakBoost = 0
	peakThrottle = 0
	peakInjector = 0
	peakOil = 0
	peakWater = 0
	peakEGT = 0
	peakIntake = 0
end


function padPercent(input)
	if input > 99 then
		return string.format("%d",input)
	elseif input > 10 then
		return string.format(" %d",input)	
	elseif input < 0 then
		return string.format(" %d",input)
	else
		return string.format("  %d",input)
	end
end


function padEGT(input)
	if input > 999 then
		return string.format("%d",input)
	else
		return string.format(" %d",input)
	end
end

function stopbars(value)
	if value >= barMax then
		return barMax
	else
		return value
	end
end

function peakUpdate()
	if(car.turboBoost > peakBoost) then
		peakBoost = car.turboBoost
	end
	if(car.gas > peakThrottle) then
		peakThrottle = car.gas
	end
	if(currentIDC > peakInjector) then
		peakInjector = currentIDC
	end
	if(currentOiltemp> peakOil) then
		peakOil = currentOiltemp
	end
	if(currentWtemp > peakWater) then
		peakWater = currentWtemp
	end
	if(currentEGT >peakEGT) then
		peakEGT = currentEGT
	end
	if(sim.ambientTemperature+6 >peakIntake) then
		peakIntake = sim.ambientTemperature+6
	end
end
function update(dt)
	currentRPM = math.clamp(car.rpm,0,8100)
	currentEGT = math.clamp(car.exhaustTemperature,lowestEGT,1200)
	currentOiltemp = math.clamp(car.oilTemperature,lowestFluidTemp,200)
	currentWtemp = math.clamp(car.waterTemperature,lowestFluidTemp,200)
	currentIDC = math.clamp(13.4*currentRPM/1200,0,100)
	if(car.rpm < 100) then
		currentIDC = 0
	end
	
	
	resetCheck()
	peakUpdate()
	
	--boost
	if(car.turboBoost < boostThreshold) then
		display.rect{
		pos = vec2(405, 84),
		size = vec2((282/maxBoost)*car.turboBoost, 39),
		color = rgbm(0.157,0.451,0.136,1)
		}
	else
		display.rect{
		pos = vec2(405, 84),
		size = vec2((282/maxBoost)*car.turboBoost, 39),
		color = rgbm(1,0.16,0.136,1)
		}
	end
	display.text{ 
        text = string.format("%.2f",car.turboBoost), 
        pos = vec2(720, 85), 
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }
	  display.rect{
		pos = vec2(stopbars((282/maxBoost)*peakBoost)+405, 84),
		size = vec2(7, 39),
		color = rgbm(1,1,1,0.7)
		}
	--throttle
	display.rect{
		pos = vec2(405, 135),
		size = vec2(282*car.gas, 39),
		color = rgbm(0.157,0.451,0.136,1)
	}
	display.text{ 
        text = padPercent(car.gas*100), 
        pos = vec2(790, 138), 
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }
	  display.rect{
		pos = vec2(stopbars(282*peakThrottle)+405, 135),
		size = vec2(7, 39),
		color = rgbm(1,1,1,0.7)
		}	  
	--injector
	if((currentIDC)<injectorThresold) then
		display.rect{
			pos = vec2(405, 187),
			size = vec2((282/100)*math.clamp(currentIDC-8,0,100), 39),
			color = rgbm(0.157,0.451,0.136,1)
		}
	else
		display.rect{
		pos = vec2(405, 187),
		size = vec2((282/100)*math.clamp(currentIDC-8,0,100), 39),
		color = rgbm(1,0.16,0.136,1)
	}
	end	
	display.text{
        text = string.format(padPercent(math.clamp(currentIDC-8,0,100))),
        pos = vec2(740, 188), 
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
     
	 }
	  
	 display.text{
	    text = string.format(".0"),
        pos = vec2(815, 188), 
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }
	  if(peakInjector < 10) then
			display.rect{
			pos = vec2(405, 187),
			size = vec2(7, 39),
			color = rgbm(1,1,1,0.7)
			}
	  else
		  display.rect{
			pos = vec2(stopbars(282/100)*peakInjector+380, 187),
			size = vec2(7, 39),
			color = rgbm(1,1,1,0.7)
		}
	end
	--oil temp
	if currentOiltemp<oilThreshold then
		display.rect{
			pos = vec2(405, 238),
			size = vec2((282/maxOil)*currentOiltemp, 39),
			color = rgbm(0.157,0.451,0.136,1)
		}
	else
		display.rect{
		pos = vec2(405, 238),
		size = vec2((282/maxOil)*currentOiltemp, 39),
		color = rgbm(1,0.16,0.136,1)
	}
	end
	display.text{ 
        text = string.format("%.0f",currentOiltemp),
        pos = vec2(810, 240), 
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }
	  display.rect{
		pos = vec2(stopbars((282/maxOil)*peakOil)+400, 238),
		size = vec2(7, 39),
		color = rgbm(1,1,1,0.7)
		}
		
	--water temp
	if currentWtemp<waterThreshold then
		display.rect{
			pos = vec2(405, 290),
			size = vec2((282/maxWater)*currentWtemp, 39),
			color = rgbm(0.157,0.451,0.136,1)
		}
	else
		display.rect{
			pos = vec2(405, 290),
			size = vec2((282/maxWater)*currentWtemp, 39),
			color = rgbm(1,0.16,0.136,1)
		}
	end
		display.rect{
			pos = vec2(stopbars((282/maxWater)*peakWater)+400, 290),
			size = vec2(7, 39),
			color = rgbm(1,1,1,0.7)
		}	
	display.text{ 
        text = string.format("%.0f",math.clamp(currentWtemp,lowestFluidTemp,200)),
        pos = vec2(810, 290), 
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }		
	--exhaust temp
	if currentEGT< egtThreshold then
		display.rect{
			pos = vec2(405, 342),
			size = vec2((282/maxExhaust)*currentEGT, 39),
			color = rgbm(0.157,0.451,0.136,1)
		}
	else
		display.rect{
			pos = vec2(405, 342),
			size = vec2((282/maxExhaust)*currentEGT, 39),
			color = rgbm(1,0.16,0.136,1)
		}
	end
	
	display.text{ 
        pos = vec2(765, 343), 
        text = padEGT(currentEGT),
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }
		display.rect{
			pos = vec2(stopbars((282/maxExhaust)*peakEGT)+400, 342),
			size = vec2(7, 39),
			color = rgbm(1,1,1,0.7)
		}	
	--intake temp
	display.rect{
		pos = vec2(405, 394),
		size = vec2((282/maxAmbient)*sim.ambientTemperature+6, 39),
		color = rgbm(0.157,0.451,0.136,1)
	} 
	display.text{ 
        pos = vec2(785, 393), 
        text = padEGT(sim.ambientTemperature+6),
        letter = vec2(40, 75),
        font = "isotonic medium",
        width = 40,
        alignment = 1,
		spacing = -14
      }
	display.rect{
		pos = vec2(stopbars((282/maxAmbient)*peakIntake)+390, 394),
		size = vec2(7, 39),
		color = rgbm(1,1,1,0.7)
	}
end