Skip to content

Commit

Permalink
Update 0.7.1 - Improved manageBrakeTemperature function, now it has d…
Browse files Browse the repository at this point in the history
…ifferent values for all the vehicle classes.

- Added a new Config, "Config.ApplyDamageAll". Now you can select if the damage of the crash applies to the engine only or to engine/petroltank/body.
  • Loading branch information
Muhaddil committed Oct 20, 2024
1 parent 466daa4 commit e4cbe23
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 57 deletions.
152 changes: 104 additions & 48 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local originalTractionLossMult = nil
local originalTractionCurveMin = nil
local originalLowSpeedTractionLossMult = nil
local lastVehicle = nil
local lastVehicle2 = nil
local damageMultiplier = Config.damageMultiplier
local checkInterval2 = Config.CheckIntervalEngineDamage
local previousSpeed = 0
Expand All @@ -25,7 +26,6 @@ local brakeTemperature = 0
local maxBrakeTemperature = Config.MaxBrakeTemp
local isBrakeOverheated = false
local coolingRate = Config.CoolingRate
local checkInterval3 = 1500
local originalBrakeForce = nil
local originalHandbrakeForce = nil

Expand Down Expand Up @@ -382,12 +382,24 @@ function ApplyEngineDamage(vehicle, damageAmount)
newPetrolTankHealth = -4000
end

SetDisableVehicleEngineFires(vehicle, true)
SetDisableVehiclePetrolTankFires(vehicle, true)
SetVehicleCanEngineOperateOnFire(vehicle, true)
SetVehicleEngineHealth(vehicle, newEngineHealth)
SetVehicleBodyHealth(vehicle, newHealthBody)
SetVehiclePetrolTankHealth(vehicle, newPetrolTankHealth)
if Config.ApplyDamageAll == true then
SetDisableVehiclePetrolTankFires(vehicle, true)
SetVehicleCanLeakPetrol(vehicle, true)
SetVehiclePetrolTankHealth(vehicle, newPetrolTankHealth)
SetDisableVehicleEngineFires(vehicle, true)
SetVehicleCanEngineOperateOnFire(vehicle, true)
SetVehicleEngineHealth(vehicle, newEngineHealth)
SetVehicleBodyHealth(vehicle, newHealthBody)
elseif Config.ApplyDamageAll == false then
SetDisableVehiclePetrolTankFires(vehicle, true)
SetVehicleCanLeakPetrol(vehicle, false)
SetVehiclePetrolTankHealth(vehicle, 1000)
SetDisableVehicleEngineFires(vehicle, true)
SetVehicleCanEngineOperateOnFire(vehicle, true)
SetVehicleEngineHealth(vehicle, newEngineHealth)
SetVehicleBodyHealth(vehicle, 1000)
end


if newEngineHealth <= -4000 then
SetVehicleEngineOn(vehicle, false, true)
Expand Down Expand Up @@ -857,9 +869,9 @@ if Config.EnableCarPhysics then
local maxSpeedMs = maxSpeedKmH / 3.6
local currentSpeedMs = GetEntitySpeed(vehicle)

-- DebugPrint("Current Speed: " .. currentSpeedMs .. " m/s")
-- DebugPrint("Max Speed: " .. maxSpeedMs .. " m/s")
-- DebugPrint("Speed Limit Active: " .. tostring(speedLimitActive))
DebugPrint("Current Speed: " .. currentSpeedMs .. " m/s")
DebugPrint("Max Speed: " .. maxSpeedMs .. " m/s")
DebugPrint("Speed Limit Active: " .. tostring(speedLimitActive))

if terrain == "sand" or terrain == "mountain" then
if currentSpeedMs > maxSpeedMs then
Expand Down Expand Up @@ -917,39 +929,94 @@ if Config.EnableCarPhysics then
return normalVehicleClasses[vehicleClass] ~= nil
end

local function manageBrakeTemperature(vehicle)
if vehicle ~= lastVehicle then
local defaultConfig = { BrakeTemperaturaGain = 20, MaxBrakeTemp = 600, CoolingRate = 1.5 }

local function manageBrakeTemperature()
local playerPed = PlayerPedId()
local vehicle = GetVehiclePedIsIn(playerPed, false)

if vehicle ~= lastVehicle2 then
originalBrakeForce = GetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce")
originalHandbrakeForce = GetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce")
print(originalBrakeForce, originalHandbrakeForce)
lastVehicle = vehicle
DebugPrint(originalBrakeForce, originalHandbrakeForce)

local vehicleClass = GetVehicleClass(vehicle)
DebugPrint(vehicleClass)
local config = Config.ClassConfigs[vehicleClass] or defaultConfig

brakeTemperaturaGain = config.BrakeTemperaturaGain
maxBrakeTemperature = config.MaxBrakeTemp
coolingRate = config.CoolingRate

lastVehicle2 = vehicle
brakeTemperature = 0
end

if brakeTemperature < 0 then
brakeTemperature = 0
end

local speed = GetEntitySpeed(vehicle) * 3.6
DebugPrint("Temperatura del freno: " .. brakeTemperature)

local wheelNumber = GetVehicleNumberOfWheels(vehicle)
local allWheelsInAir = true

if speed > 5 then
local brakePressure = GetVehicleWheelBrakePressure(vehicle, 0)
for i = 0, wheelNumber - 1 do
local wheelSpeed = GetVehicleWheelSpeed(vehicle, i)
local wheelSpeedInKM = wheelSpeed * 3.6

if brakePressure > 0.1 then
brakeTemperature = brakeTemperature + Config.BrakeTemperaturaGain
if wheelSpeedInKM > 1 or wheelSpeedInKM < -1 then
allWheelsInAir = false
break
end
print(brakeTemperature)

if brakeTemperature >= maxBrakeTemperature then
isBrakeOverheated = true
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", 0.0)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", 0.0)
SetVehicleBrakeLights(vehicle, false)
brakeTemperature = brakeTemperature - coolingRate * 5
else
if isBrakeOverheated then
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", originalBrakeForce)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", originalHandbrakeForce)
brakeTemperature = brakeTemperature - coolingRate * 5
isBrakeOverheated = false
end

if not allWheelsInAir then
if speed > 5 then
local totalBrakePressure = 0
local validWheels = 0

for i = 0, wheelNumber - 1 do
local brakePressure = GetVehicleWheelBrakePressure(vehicle, i)

if brakePressure > 0.1 then
totalBrakePressure = totalBrakePressure + brakePressure
validWheels = validWheels + 1
end
end

if validWheels > 0 then
local averageBrakePressure = totalBrakePressure / validWheels
DebugPrint("Presión promedio de frenos: " .. averageBrakePressure)

if averageBrakePressure > 0.1 then
brakeTemperature = brakeTemperature + brakeTemperaturaGain
end
end

if brakeTemperature >= maxBrakeTemperature then
isBrakeOverheated = true
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", 0.0)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", 0.0)
SetVehicleBrakeLights(vehicle, false)
brakeTemperature = brakeTemperature - coolingRate * 5
else
if isBrakeOverheated then
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", originalBrakeForce)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", originalHandbrakeForce)
brakeTemperature = brakeTemperature - coolingRate * 5
isBrakeOverheated = false
end

if brakeTemperature > 0 and brakeTemperature < maxBrakeTemperature then
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", originalBrakeForce)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", originalHandbrakeForce)
isBrakeOverheated = false
brakeTemperature = brakeTemperature - coolingRate
end
end
else
if brakeTemperature > 0 and brakeTemperature < maxBrakeTemperature then
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", originalBrakeForce)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", originalHandbrakeForce)
Expand All @@ -959,7 +1026,9 @@ if Config.EnableCarPhysics then
end
else
if brakeTemperature > 0 then
brakeTemperature = math.max(0, brakeTemperature - coolingRate)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fBrakeForce", originalBrakeForce)
SetVehicleHandlingFloat(vehicle, "CHandlingData", "fHandBrakeForce", originalHandbrakeForce)
brakeTemperature = brakeTemperature - coolingRate
end
end
end
Expand All @@ -982,27 +1051,14 @@ if Config.EnableCarPhysics then
if isNormalCar(vehicleClass) or isEmergencyVehicle and not hasOffroadTyres then
limitSpeed(veh, terrain)
end

manageBrakeTemperature()
end

Citizen.Wait(timeout)
end
end)

Citizen.CreateThread(function()
while true do
checkInterval3 = 5000

local playerPed = GetPlayerPed(-1)
local vehicle = GetVehiclePedIsIn(playerPed, false)

if vehicle and IsVehicleOnAllWheels(vehicle) then
manageBrakeTemperature(vehicle)
checkInterval3 = 500
end
Citizen.Wait(checkInterval3)
end
end)

Citizen.CreateThread(function()
while true do
local timeout = Config.CarPhysicsTimeout
Expand Down
40 changes: 32 additions & 8 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Config.BreakdownCooldown = 10800000 -- Cooldown in milliseconds (e.g. 108000
Config.SpeedToDamageRatio = 1.0 -- Does nothing | Useless
Config.preventVehicleFlip = true -- Disable flipping overturned cars
Config.damageMultiplier = 5 -- Damage multiplier applied to the engine in each crash
Config.ApplyDamageAll = false -- If the ApplyEngineDamage function applies the damage to the engine only or to engine/petroltank/body.
Config.CheckIntervalEngineDamage = 2000 -- Cooldown in milliseconds
Config.AutoRunSQL = true
Config.AutoVersionChecker = true
Expand All @@ -28,14 +29,37 @@ Config.UseExternalMileageSystem = false

-- Config for the vehicle physics in harsh terrains
Config.EnableCarPhysics = true
Config.MaxSpeed = 40 -- In KM/hours
Config.CarPhysicsTimeout = 2500 -- In milliseconds
Config.CarSinking = false -- Works but it's as little bit buggy, not a great implementation
Config.reductionFactor = 0.1 -- How fast the vehicles brake on sand/grass
Config.TractionBonus = 0.2 -- Additional traction boost for emergency vehicles, improving grip on rough terrains like sand or grass
Config.BrakeTemperaturaGain = 35 -- How much heat is applied to the brakes every 1.5 seconds
Config.MaxBrakeTemp = 500 -- The max temperatura the brakes can handle before giving out
Config.CoolingRate = 1 -- How fast the brakes cool down
Config.MaxSpeed = 40 -- In KM/hours
Config.CarPhysicsTimeout = 2500 -- In milliseconds
Config.CarSinking = false -- Works but it's as little bit buggy, not a great implementation
Config.reductionFactor = 0.1 -- How fast the vehicles brake on sand/grass
Config.TractionBonus = 0.2 -- Additional traction boost for emergency vehicles, improving grip on rough terrains like sand or grass

Config.ClassConfigs = {
[0] = { BrakeTemperaturaGain = 15, MaxBrakeTemp = 550, CoolingRate = 1.1 }, -- Compacts
[1] = { BrakeTemperaturaGain = 20, MaxBrakeTemp = 600, CoolingRate = 1.0 }, -- Sedans
[2] = { BrakeTemperaturaGain = 25, MaxBrakeTemp = 650, CoolingRate = 1.05 }, -- SUVs
[3] = { BrakeTemperaturaGain = 30, MaxBrakeTemp = 700, CoolingRate = 1.0 }, -- Coupes
[4] = { BrakeTemperaturaGain = 35, MaxBrakeTemp = 800, CoolingRate = 0.8 }, -- Muscle
[5] = { BrakeTemperaturaGain = 40, MaxBrakeTemp = 900, CoolingRate = 0.7 }, -- Sports Classics
[6] = { BrakeTemperaturaGain = 45, MaxBrakeTemp = 950, CoolingRate = 1.3 }, -- Sports
[7] = { BrakeTemperaturaGain = 50, MaxBrakeTemp = 1000, CoolingRate = 1.6 }, -- Super
[8] = { BrakeTemperaturaGain = 10, MaxBrakeTemp = 500, CoolingRate = 1.5 }, -- Motorcycles
[9] = { BrakeTemperaturaGain = 50, MaxBrakeTemp = 1100, CoolingRate = 0.4 }, -- Off-road
[10] = { BrakeTemperaturaGain = 55, MaxBrakeTemp = 1200, CoolingRate = 0.3 }, -- Industrial
[11] = { BrakeTemperaturaGain = 35, MaxBrakeTemp = 800, CoolingRate = 1.0 }, -- Utility
[12] = { BrakeTemperaturaGain = 30, MaxBrakeTemp = 750, CoolingRate = 1.2 }, -- Vans
[13] = { BrakeTemperaturaGain = 5, MaxBrakeTemp = 450, CoolingRate = 2.5 }, -- Cycles
[14] = { BrakeTemperaturaGain = 20, MaxBrakeTemp = 550, CoolingRate = 1.8 }, -- Boats
[15] = { BrakeTemperaturaGain = 60, MaxBrakeTemp = 1300, CoolingRate = 0.4 }, -- Helicopters
[16] = { BrakeTemperaturaGain = 65, MaxBrakeTemp = 1400, CoolingRate = 0.3 }, -- Planes
[17] = { BrakeTemperaturaGain = 20, MaxBrakeTemp = 600, CoolingRate = 1.6 }, -- Service
[18] = { BrakeTemperaturaGain = 20, MaxBrakeTemp = 650, CoolingRate = 1.5 }, -- Emergency
[19] = { BrakeTemperaturaGain = 25, MaxBrakeTemp = 700, CoolingRate = 1.5 }, -- Military
[20] = { BrakeTemperaturaGain = 70, MaxBrakeTemp = 1500, CoolingRate = 0.2 }, -- Commercial
[21] = { BrakeTemperaturaGain = 80, MaxBrakeTemp = 1600, CoolingRate = 0.1 }, -- Trains
[22] = { BrakeTemperaturaGain = 50, MaxBrakeTemp = 900, CoolingRate = 0.6 }, -- Open Wheel
}

-- Types of breakdowns
Config.BreakdownTypes = {
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lua54 'yes'

author 'Muhaddil'
description 'Mileage-based vehicle breakdown system for ESX&QBCore'
version 'v0.7.01-beta'
version 'v0.7.1-beta'

shared_script 'config.lua'
client_script 'client.lua'
Expand Down

0 comments on commit e4cbe23

Please sign in to comment.