-- Decompiled with Visenya | https://targaryentech.com
local _WHEELTUNE = {
  TireWearOn = true,
  FWearSpeed = 0.4,
  FTargetFriction = 0.7,
  FMinFriction = 0.1,
  RWearSpeed = 0.4,
  RTargetFriction = 0.7,
  RMinFriction = 0.1,
  TCSOffRatio = 0.3333333333333333,
  WheelLockRatio = 0.5,
  WheelspinRatio = 0.9090909090909091,
  FFrictionWeight = 1,
  RFrictionWeight = 1,
  FLgcyFrWeight = 10,
  RLgcyFrWeight = 10,
  FElasticity = 0.5,
  RElasticity = 0.5,
  FLgcyElasticity = 0,
  RLgcyElasticity = 0,
  FElastWeight = 1,
  RElastWeight = 1,
  FLgcyElWeight = 10,
  RLgcyElWeight = 10,
  RegenSpeed = 3.6
}
local car = script.Parent.Parent.Car.Value
local _Tune = require(car["A-Chassis Tune"])
local cValues = script.Parent.Parent:WaitForChild("Values")
local fDensity = _Tune.FWheelDensity
local rDensity = _Tune.RWheelDensity
local fFwght = _WHEELTUNE.FFrictionWeight
local rFwght = _WHEELTUNE.RFrictionWeight
local fElast = _WHEELTUNE.FElasticity
local rElast = _WHEELTUNE.RElasticity
local fEwght = _WHEELTUNE.FElastWeight
local rEwght = _WHEELTUNE.RElastWeight
if not workspace:PGSIsEnabled() then
  fDensity = _Tune.FWLgcyDensity
  rDensity = _Tune.RWLgcyDensity
  fFwght = _WHEELTUNE.FLgcyFrWeight
  rFwght = _WHEELTUNE.FLgcyFrWeight
  fElast = _WHEELTUNE.FLgcyElasticity
  rElast = _WHEELTUNE.RLgcyElasticity
  fEwght = _WHEELTUNE.FLgcyElWeight
  rEwght = _WHEELTUNE.RLgcyElWeight
end
local Wheels = {}
for i, v in pairs(car.Wheels:GetChildren()) do
  local w = {}
  w.wheel = v
  local ca
  w.x = 0
  if v.Name == "FL" or v.Name == "FR" or v.Name == "F" then
    ca = (12 - math.abs(_Tune.FCamber)) / 15
    w.x = fDensity
    w.BaseHeat = _WHEELTUNE.FTargetFriction - _WHEELTUNE.FMinFriction
    w.WearSpeed = _WHEELTUNE.FWearSpeed
    w.fWeight = fFwght
    w.elast = fElast
    w.eWeight = fEwght
  else
    ca = (12 - math.abs(_Tune.RCamber)) / 15
    w.x = rDensity
    w.BaseHeat = _WHEELTUNE.RTargetFriction - _WHEELTUNE.RMinFriction
    w.WearSpeed = _WHEELTUNE.RWearSpeed
    w.fWeight = rFwght
    w.elast = rElast
    w.eWeight = rEwght
  end
  w.Heat = w.BaseHeat
  w.stress = 0
  table.insert(Wheels, w)
end
car.DriveSeat.ChildRemoved:connect(function(child)
  if child.Name == "SeatWeld" then
    local wD = car:FindFirstChild("WearData")
    if car:FindFirstChild("WearData") == nil then
      wD = script.Parent.WearData:Clone()
      wD.Parent = car
    end
    for i, v in pairs(Wheels) do
      wD[v.wheel.Name].Value = v.Heat
    end
    wD.STime.Value = tick()
  end
end)
while wait() do
  for i, v in pairs(Wheels) do
    local speed = car.DriveSeat.Velocity.Magnitude
    local wheel = v.wheel.RotVelocity.Magnitude
    local z = 0
    local deg = 1.26E-4
    local cspeed = speed / 1.298 * (2.6 / v.wheel.Size.Y)
    local wdif = math.abs(wheel - cspeed)
    if _WHEELTUNE.TireWearOn then
      if speed < 4 then
        v.Heat = math.min(v.Heat + _WHEELTUNE.RegenSpeed / 10000, v.BaseHeat)
      elseif wdif > 1 then
        v.Heat = v.Heat - wdif * deg * v.WearSpeed / 28
      elseif v.Heat >= v.BaseHeat then
        v.Heat = v.BaseHeat
      end
    end
    if v.wheel.Name == "FL" or v.wheel.Name == "FR" or v.wheel.Name == "F" then
      z = _WHEELTUNE.FMinFriction + v.Heat
      deg = (deg - 1.188E-4 * cValues.Brake.Value) * (1 - math.abs(cValues.SteerC.Value)) + 1.26E-6 * math.abs(cValues.SteerC.Value)
    else
      z = _WHEELTUNE.RMinFriction + v.Heat
    end
    if math.ceil(wheel / 0.774 / speed * 100) < 8 then
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z * _WHEELTUNE.WheelLockRatio, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.max(v.Heat, 0)
    elseif _Tune.TCSEnabled and cValues.TCS.Value == false and math.ceil(wheel / 0.774 / speed * 100) > 80 then
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z * _WHEELTUNE.TCSOffRatio, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.max(v.Heat, 0)
    elseif math.ceil(wheel / 0.774 / speed * 100) > 130 then
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z * _WHEELTUNE.WheelspinRatio, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.max(v.Heat, 0)
    else
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.min(v.Heat, v.BaseHeat)
    end
    local vstress = math.abs(((wdif + cspeed) / 0.774 * 0.774 - cspeed) / 15)
    if vstress > 0.05 and vstress > v.stress then
      v.stress = math.min(v.stress + 0.03, 1)
    else
      v.stress = math.max(v.stress - 0.03, vstress)
    end
    local UI = script.Parent.Tires[v.wheel.Name]
    UI.First.Second.Image.ImageColor3 = Color3.new(math.min(v.stress * 2, 1), 1 - v.stress, 0)
    UI.First.Position = UDim2.new(0, 0, 1 - v.Heat / v.BaseHeat, 0)
    UI.First.Second.Position = UDim2.new(0, 0, v.Heat / v.BaseHeat, 0)
  end
end