Skip to content

Commit

Permalink
feat: customisable vehicle and seat seatbelt exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGriefs committed Nov 21, 2021
1 parent cc15466 commit 9fb24a6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
22 changes: 14 additions & 8 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ local lastVelocity = vector3(0, 0, 0)
local newbieBeep = true
local showHelp = false
local activated
local hasSeatbelt

RegisterKeyMapping('seatbelt', 'Seatbelt', 'keyboard', 'k')
RegisterFrameworkCommand('seatbelt', function()
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped) and DoesPedVehicleHaveSeatbelt(ped) then
if activated then
DeactivateSeatbelt()
else
ActivateSeatbelt()
if IsPedInAnyVehicle(ped) then
local vehcileHasSeatbelt, strong = DoesPedVehicleHaveSeatbelt(ped)
if vehcileHasSeatbelt and not strong then
if activated then
DeactivateSeatbelt()
else
ActivateSeatbelt()
end
end
end
end)
Expand Down Expand Up @@ -67,7 +71,7 @@ function DeactivateSeatbelt()
Citizen.CreateThread(function()
while not activated do
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped) and DoesPedVehicleHaveSeatbelt(ped) and not IsHudHidden() then
if IsPedInAnyVehicle(ped) and hasSeatbelt and not IsHudHidden() then
local vehicle = GetVehiclePedIsIn(ped)
local speed = GetEntitySpeed(vehicle) * 3.6

Expand Down Expand Up @@ -102,7 +106,9 @@ function DeactivateSeatbelt()
while not activated do
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped) then
if DoesPedVehicleHaveSeatbelt(ped) then
local _hasSeatbelt, strong = DoesPedVehicleHaveSeatbelt(ped)
hasSeatbelt = _hasSeatbelt
if hasSeatbelt and not strong then

local vehicle = GetVehiclePedIsIn(ped)
local speed = GetEntitySpeed(vehicle)
Expand All @@ -128,7 +134,7 @@ function DeactivateSeatbelt()
while not activated do
local ped = PlayerPedId()
local vehicle = GetVehiclePedIsIn(ped)
if IsPedInAnyVehicle(ped) and DoesPedVehicleHaveSeatbelt(ped) and GetEntitySpeed(vehicle) * 3.6 > 10 then
if IsPedInAnyVehicle(ped) and hasSeatbelt and GetEntitySpeed(vehicle) * 3.6 > 10 then
TriggerServerEvent('seatbelt:ServerNotify')
end
Citizen.Wait(3e3)
Expand Down
17 changes: 17 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,21 @@ Config = {
--- Distance which LEOs can detect seatbelt-less occupants within.
--- @type number
Distance = 20,

--- Custom vehicles/seats that don't have seatbelts.
--- Bicycles, motorbikes and submersibles are excluded automatically
--- - `1` or `true` = No seatbelt, windscreen ejection still occurs
--- - `2` = Seatbelt always on, ejection is never calculated
--- @type table<hash, boolean|table<number, boolean>>
Excluded = {
[GetHashKey('MINITANK')] = 2, -- Seatbelt always on.
[GetHashKey('HALFTRACK')] = { [1] = true }, -- Seat #3 has no seatbelt.
[GetHashKey('KHANJALI')] = 2,
[GetHashKey('APC')] = 2,
[GetHashKey('THRUSTER')] = 2,
[GetHashKey('RHINO')] = 2,
},
}

--- A model hash key
--- @alias hash number
29 changes: 22 additions & 7 deletions util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end
-- config setup
Constants = {
Distance = Config.Distance + 0.0,
Excluded = Config.Excluded,
}

local GetPlayerIdentifierMethods = {
Expand Down Expand Up @@ -63,13 +64,27 @@ GetPlayerIdentifierMethods = nil
Config = nil

function DoesPedVehicleHaveSeatbelt(ped)
return not (
not IsPedInAnyVehicle(ped)
or IsPedOnAnyBike(ped)
or IsPedInAnyBoat(ped)
or IsPedInAnyPlane(ped)
or IsPedInAnyHeli(ped)
)
if not IsPedInAnyVehicle(ped)
or IsPedOnAnyBike(ped)
or IsPedInAnyBoat(ped)
or IsPedInAnyPlane(ped)
or IsPedInAnyHeli(ped)
then return false, false end

local vehicle = GetVehiclePedIsIn(ped)
local model = GetEntityModel(vehicle)
if Constants.Excluded[model] then
if Constants.Excluded[model] == 2 then
return true, true
elseif type(Constants.Excluded[model]) == 'table' then
for seat, type in pairs(Constants.Excluded[model]) do
if GetPedInVehicleSeat(vehicle, seat - 2) == ped then
return false, type == 2
end
end
end
return true, false
end
end

function Fwv(entity)
Expand Down

0 comments on commit 9fb24a6

Please sign in to comment.