Skip to content

Commit

Permalink
Update 0.6.3 - Improved limitSpeed function for better realism
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhaddil committed Oct 13, 2024
1 parent af30a07 commit 5569548
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
94 changes: 74 additions & 20 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ function SendNotification(msgtitle, msg, time, type)
lib.notify({
title = msgtitle,
description = msg,
type = type,
position = 'center',
showDuration = true,
type = type2,
style = {
backgroundColor = 'rgba(0, 0, 0, 0.75)',
color = 'rgba(255, 255, 255, 1)',
['.description'] = {
color = '#909296',
backgroundColor = 'transparent'
}
}
})
else
if Config.FrameWork == 'qb' then
Expand Down Expand Up @@ -653,7 +661,7 @@ if Config.EnableCarPhysics then
local mountainHashes = {
815500405, 509508168, 951832588, 1913209870, 1333033863,
1288448767, 1336319281, -1286696947, -461750719,
-1289542914, -730990693, -840216541, 2128369009
-1289542914, -730990693, -840216541, 2128369009, -1942898710
}

if contains(sandHashes, groundHash) then
Expand Down Expand Up @@ -767,6 +775,9 @@ if Config.EnableCarPhysics then
local hasOffroadTyres = hasOffroadTires(vehicle)
local isEmergency = isEmergencyVehicle(vehicle)
local tractionBonus = isEmergency and Config.TractionBonus or 0
DebugPrint(isEmergency)
DebugPrint(hasOffroadTyres)
DebugPrint(driveType)

if vehicle ~= lastVehicle then
originalTractionCurveMin = nil
Expand Down Expand Up @@ -839,31 +850,48 @@ 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
local speedDifference = currentSpeedMs - maxSpeedMs
local reductionFactor = Config.reductionFactor

if not speedLimitActive then
local newSpeedMs = currentSpeedMs - (speedDifference * reductionFactor)
SetVehicleForwardSpeed(vehicle, newSpeedMs)
speedLimitActive = true

DebugPrint("New Speed: " .. newSpeedMs .. " m/s")
Citizen.CreateThread(function()
while true do
currentSpeedMs = GetEntitySpeed(vehicle)
local newSpeedMs = currentSpeedMs - (speedDifference * reductionFactor)

if currentSpeedMs - newSpeedMs < 1 then
SetEntityMaxSpeed(vehicle, maxSpeedMs)
speedLimitActive = true
end
if currentSpeedMs > maxSpeedMs and speedLimitActive then
SetVehicleCheatPowerIncrease(vehicle, -100.0)
SetVehicleBrake(vehicle, true)
SetVehicleCurrentRpm(vehicle, 0.0)
else
if currentSpeedMs - newSpeedMs < 1 then
SetEntityMaxSpeed(vehicle, maxSpeedMs)
speedLimitActive = true
break
end
end
Wait(0)
end
end)
end
end
else
local maxSpeedOriginal = GetVehicleHandlingFloat(vehicle, "CHandlingData", "fInitialDriveMaxFlatVel")
SetEntityMaxSpeed(vehicle, maxSpeedOriginal)
speedLimitActive = false
if speedLimitActive then
speedLimitActive = false
SetVehicleCheatPowerIncrease(vehicle, 1.0)
SetVehicleBrake(vehicle, false)
SetVehicleCurrentRpm(vehicle, 1.0)
end
end
end

Expand All @@ -882,6 +910,32 @@ if Config.EnableCarPhysics then
return normalVehicleClasses[vehicleClass] ~= nil
end

Citizen.CreateThread(function()
while true do
local timeout = Config.CarPhysicsTimeout
local playerPed = PlayerPedId()
local veh = GetVehiclePedIsIn(playerPed, false)
if veh ~= 0 then
timeout = 500
local terrain = isOnSandOrMountain()

local vehicleClass = GetVehicleClass(veh)
local hasOffroadTyres = hasOffroadTires(veh)

-- if isNormalCar(vehicleClass) and not hasOffroadTyres or isEmergencyVehicle and not hasOffroadTyres then
-- limitSpeed(veh, terrain)
-- end
if isNormalCar(vehicleClass) or isEmergencyVehicle and not hasOffroadTyres then
limitSpeed(veh, terrain)
end

end

Citizen.Wait(timeout)
end
end)


Citizen.CreateThread(function()
while true do
local timeout = Config.CarPhysicsTimeout
Expand All @@ -901,12 +955,12 @@ if Config.EnableCarPhysics then
limitSpeed(veh, terrain)
end

if terrain == "sand" or terrain == "mountain" then
local multiplier = getTerrainEffectMultiplier(vehicleClass, terrain, hasOffroadTyres)
SetVehicleEngineTorqueMultiplier(veh, multiplier)
else
SetVehicleEngineTorqueMultiplier(veh, 1.0)
end
-- if terrain == "sand" or terrain == "mountain" then
-- local multiplier = getTerrainEffectMultiplier(vehicleClass, terrain, hasOffroadTyres)
-- SetVehicleEngineTorqueMultiplier(veh, multiplier)
-- else
-- SetVehicleEngineTorqueMultiplier(veh, 1.0)
-- end
end

Citizen.Wait(timeout)
Expand Down
6 changes: 3 additions & 3 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Config.MaxBreakdownChance = 0.5 -- Maximum probability of failure
Config.BreakdownCooldown = 10800000 -- Cooldown in milliseconds (e.g. 10800000 ms = 3 hours)
Config.SpeedToDamageRatio = 1.0 -- Does nothing | Useless
Config.preventVehicleFlip = true -- Disable flipping overturned cars
Config.damageMultiplier = 3 -- Damage multiplier applied to the engine in each crash
Config.damageMultiplier = 5 -- Damage multiplier applied to the engine in each crash
Config.CheckIntervalEngineDamage = 2000 -- Cooldown in milliseconds
Config.AutoRunSQL = true
Config.AutoVersionChecker = true
Expand All @@ -28,8 +28,8 @@ Config.UseExternalMileageSystem = false

-- Config for the vehicle physics in harsh terrains
Config.EnableCarPhysics = true
Config.MaxSpeed = 25 -- In KM/hours
Config.CarPhysicsTimeout = 1500 -- In milliseconds
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
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.6.21-beta'
version 'v0.6.3-beta'

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

0 comments on commit 5569548

Please sign in to comment.