Skip to content

Commit

Permalink
fixed horn & seat issue
Browse files Browse the repository at this point in the history
- Cleanup/Refactor
- Fixed horn issue after jumping/upside down
- Fixed seat not properly updating in thread
  • Loading branch information
QuantumMalice committed Aug 20, 2024
1 parent cfd8562 commit e068965
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
54 changes: 37 additions & 17 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local function startThread(vehicle)
local speedBuffer, healthBuffer, bodyBuffer, roll, airborne = {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, 0.0, false

CreateThread(function()
while (cache.vehicle == vehicle) and (cache.seat == -1) do
while (cache.vehicle == vehicle) and (Handler:getSeat() == -1) do

-- Retrieve latest vehicle data
bodyBuffer[1] = GetVehicleBodyHealth(vehicle)
Expand All @@ -43,7 +43,7 @@ local function startThread(vehicle)
Handler:setLimited(true)

CreateThread(function()
while (cache.vehicle == vehicle) and (healthBuffer[1] < 500) do
while (cache.vehicle == vehicle) and (Handler:getSeat() == -1) and (healthBuffer[1] < 500) do
local newtorque = (healthBuffer[1] + 500) / 1100
SetVehicleCheatPowerIncrease(vehicle, newtorque)
Wait(1)
Expand All @@ -64,7 +64,23 @@ local function startThread(vehicle)
end

if (roll > 75.0 or roll < -75.0) or airborne then
SetVehicleOutOfControl(vehicle, false, false)
if Handler:canControl() then
Handler:setControl(false)

CreateThread(function()
while not Handler:canControl() and Handler:getSeat() == -1 do
DisableControlAction(2, 59, true) -- Disable left/right
DisableControlAction(2, 60, true) -- Disable up/down
Wait(1)
end

if not Handler:canControl() then Handler:setControl(true) end
end)
end
else
if not Handler:canControl() then
Handler:setControl(true)
end
end
end

Expand Down Expand Up @@ -132,37 +148,41 @@ local function startThread(vehicle)
end

lib.onCache('seat', function(seat)
Handler:setSeat(seat)

if seat == -1 then
startThread(cache.vehicle)
end
end)

lib.callback.register('vehiclehandler:adminfuel', function(newlevel)
if not Handler or not Handler:isActive() then return end
return Handler:adminfuel(newlevel)
lib.callback.register('vehiclehandler:basicfix', function(fixtype)
if not Handler then return end
return Handler:basicfix(fixtype)
end)

lib.callback.register('vehiclehandler:adminwash', function()
if not Handler or not Handler:isActive() then return end
return Handler:adminwash()
lib.callback.register('vehiclehandler:basicwash', function()
if not Handler then return end
return Handler:basicwash()
end)

lib.callback.register('vehiclehandler:adminfix', function()
if not Handler or not Handler:isActive() then return end
return Handler:adminfix()
end)

lib.callback.register('vehiclehandler:basicwash', function()
if not Handler then return end
return Handler:basicwash()
lib.callback.register('vehiclehandler:adminwash', function()
if not Handler or not Handler:isActive() then return end
return Handler:adminwash()
end)

lib.callback.register('vehiclehandler:basicfix', function(fixtype)
if not Handler then return end
return Handler:basicfix(fixtype)
lib.callback.register('vehiclehandler:adminfuel', function(newlevel)
if not Handler or not Handler:isActive() then return end
return Handler:adminfuel(newlevel)
end)

CreateThread(function()
Handler = Handler:new()
startThread(cache.vehicle)
Handler = Handler:new(cache.seat)
if cache.seat == -1 then
startThread(cache.vehicle)
end
end)
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lua54 'yes'
name 'vehiclehandler'
description 'Collision/damage handling for FiveM.'
author 'QuantumMalice'
version '1.2.1'
version '1.2.2'

files {
'data/progress.lua',
Expand Down
49 changes: 35 additions & 14 deletions modules/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ local BONES <const> = {
}

---@class Handler : OxClass
---@field private private { active: boolean, limited: boolean, oxfuel: boolean, units: number }
---@field private private { active: boolean, limited: boolean, control: boolean, oxfuel: boolean, units: number, seat: number|false }
---@diagnostic disable-next-line: assign-type-mismatch
local Handler = lib.class('vehiclehandler')

function Handler:constructor()
function Handler:constructor(seat)
self:setActive(false)
self:setLimited(false)
self:setControl(true)
self:setSeat(seat)
self.private.oxfuel = GetResourceState('ox_fuel') == 'started' and true or false
self.private.units = Settings.units == 'mph' and 2.23694 or 3.6
end
Expand All @@ -25,10 +28,14 @@ function Handler:isActive() return self.private.active end

function Handler:isLimited() return self.private.limited end

function Handler:canControl() return self.private.control end

function Handler:isFuelOx() return self.private.oxfuel end

function Handler:getUnits() return self.private.units end

function Handler:getSeat() return self.private.seat end

function Handler:isValid()
if not cache.ped then return false end
if cache.vehicle or IsPedInAnyPlane(cache.ped) then return true end
Expand Down Expand Up @@ -56,18 +63,6 @@ function Handler:isTireBroken(vehicle, coords)
return false
end

function Handler:getEngineData(vehicle)
if not vehicle or vehicle == 0 then return end

local backengine = Settings.backengine[GetEntityModel(vehicle)]
local distance = backengine and -2.5 or 2.5
local offset = GetOffsetFromEntityInWorldCoords(vehicle, 0, distance, 0)
local index = backengine and 5 or 4
local health = GetVehicleEngineHealth(vehicle)

return backengine, offset, index, health
end

function Handler:setActive(state)
if state ~= nil and type(state) == 'boolean' then
self.private.active = state
Expand All @@ -80,6 +75,32 @@ function Handler:setLimited(state)
end
end

function Handler:setControl(state)
if state ~= nil and type(state) == 'boolean' then
self.private.control = state
end
end

function Handler:setSeat(seat)
if seat ~= nil then
if type(seat) == 'number' or seat == false then
self.private.seat = seat
end
end
end

function Handler:getEngineData(vehicle)
if not vehicle or vehicle == 0 then return end

local backengine = Settings.backengine[GetEntityModel(vehicle)]
local distance = backengine and -2.5 or 2.5
local offset = GetOffsetFromEntityInWorldCoords(vehicle, 0, distance, 0)
local index = backengine and 5 or 4
local health = GetVehicleEngineHealth(vehicle)

return backengine, offset, index, health
end

function Handler:breakTire(vehicle, index)
if vehicle == nil or type(vehicle) ~= 'number' then return end
if index == nil or type(index) ~= 'number' then return end
Expand Down

0 comments on commit e068965

Please sign in to comment.