This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
forked from Spazd/qb-pursuitmode
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclient.lua
237 lines (183 loc) · 8.07 KB
/
client.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
local QBCore = exports["qb-core"]:GetCoreObject()
local gear = 1
local currentVehicle = nil
local currentVehicleMode = Config.VehicleModes[1]
local playerJob = {['name'] = 'unemployed'}
local hashModelMap = {}
function GetVehicleMode()
return currentVehicleMode
end
local function fixVehicleHandling(veh)
-- Necessary to apply the HandlingFloat
-- Source: https://forum.cfx.re/t/cant-change-setvehiclehandlingfloat-transforming-vehicle-to-awd-fivem-bug/3393188/2
SetVehicleModKit(veh,0)
SetVehicleMod(veh,0,GetVehicleMod(veh,0),false)
SetVehicleMod(veh,1,GetVehicleMod(veh,1),false)
SetVehicleMod(veh,2,GetVehicleMod(veh,2),false)
SetVehicleMod(veh,3,GetVehicleMod(veh,3),false)
SetVehicleMod(veh,4,GetVehicleMod(veh,4),false)
SetVehicleMod(veh,5,GetVehicleMod(veh,5),false)
SetVehicleMod(veh,6,GetVehicleMod(veh,6),false)
SetVehicleMod(veh,7,GetVehicleMod(veh,7),false)
SetVehicleMod(veh,8,GetVehicleMod(veh,8),false)
SetVehicleMod(veh,9,GetVehicleMod(veh,9),false)
SetVehicleMod(veh,10,GetVehicleMod(veh,10),false)
SetVehicleMod(veh,11,GetVehicleMod(veh,11),false)
SetVehicleMod(veh,12,GetVehicleMod(veh,12),false)
SetVehicleMod(veh,13,GetVehicleMod(veh,13),false)
SetVehicleMod(veh,15,GetVehicleMod(veh,15),false)
SetVehicleMod(veh,16,GetVehicleMod(veh,16),false)
SetVehicleMod(veh,25,GetVehicleMod(veh,25),false)
SetVehicleMod(veh,27,GetVehicleMod(veh,27),false)
SetVehicleMod(veh,28,GetVehicleMod(veh,28),false)
SetVehicleMod(veh,30,GetVehicleMod(veh,30),false)
SetVehicleMod(veh,33,GetVehicleMod(veh,33),false)
SetVehicleMod(veh,34,GetVehicleMod(veh,34),false)
SetVehicleMod(veh,35,GetVehicleMod(veh,35),false)
SetVehicleWheelIsPowered(veh,0,true)
SetVehicleWheelIsPowered(veh,1,true)
SetVehicleWheelIsPowered(veh,2,true)
SetVehicleWheelIsPowered(veh,3,true)
end
-- this maps the configured model names to their respective hashes
-- this is required because you cannot get the model from a car reliably
local function setupModelHashMap()
for k,v in pairs(Config.VehiclesConfig) do
hashModelMap[GetHashKey(k)] = k
end
end
local function getModelFromHash(hash)
return hashModelMap[hash]
end
local function isValidVehicle(vehicle)
if Config.UseGeneralVehicleConfig then
if Config.EmergencyVehiclesOnly then
return GetVehicleClass(vehicle) == 18
end
return true
end
local vehicleHash = GetEntityModel(vehicle)
local vehicleModel = getModelFromHash(vehicleHash)
return Config.VehiclesConfig[vehicleModel]
end
local function getHandlingConfig(vehicleHash)
local vehicleModel = getModelFromHash(vehicleHash)
if Config.VehiclesConfig[vehicleModel] then
return Config.VehiclesConfig[vehicleModel][currentVehicleMode]
end
end
local function getGeneralHandlingConfig()
if Config.GeneralVehicleConfig then
return Config.GeneralVehicleConfig[currentVehicleMode]
end
end
local function updateHandling(vehicle)
local handlingConfig = getHandlingConfig(GetEntityModel(vehicle))
if Config.UseGeneralVehicleConfig and not handlingConfig then
handlingConfig = getGeneralHandlingConfig()
end
for k,v in pairs(handlingConfig) do
if math.type(v) == 'float' then
SetVehicleHandlingFloat(vehicle, "CHandlingData", k, v)
elseif math.type(v) == 'integer' then
SetVehicleHandlingInt(vehicle, "CHandlingData", k, v)
elseif type(v) == 'vector3' then
SetVehicleHandlingVector(vehicle, "CHandlingData", k, v)
end
end
fixVehicleHandling(vehicle)
end
local function applyVehicleMods(vehicle)
local vehicleMode = Config.VehicleModes[gear]
ToggleVehicleMod(vehicle, 18, Config["VehicleModifications"][vehicleMode]["Turbo"] or GetVehicleMod(vehicle, 18)) -- Turbo
ToggleVehicleMod(vehicle, 22, Config["VehicleModifications"][vehicleMode]["XenonHeadlights"] or GetVehicleMod(vehicle, 22)) -- Xenon Headlights
SetVehicleMod(vehicle, 11, Config["VehicleModifications"][vehicleMode]["Engine"] or GetVehicleMod(vehicle, 18), false) -- Engine
SetVehicleMod(vehicle, 12, Config["VehicleModifications"][vehicleMode]["Brakes"] or GetVehicleMod(vehicle, 18), false) -- Brakes
SetVehicleMod(vehicle, 13, Config["VehicleModifications"][vehicleMode]["Transmission"] or GetVehicleMod(vehicle, 18), false) -- Transmission
SetVehicleXenonLightsColour(vehicle, Config["VehicleModifications"][vehicleMode]["XenonHeadlightsColor"] or GetVehicleXenonLightsColour(vehicle)) -- Xenon Headlights Color
if Config.SlowdownOnSwitch then
local speed = GetEntitySpeed(vehicle)
SetVehicleForwardSpeed(vehicle, speed * (1.0 - Config.SlowdownPercentage))
end
end
local function changeVehicleMode(vehicle)
local vehicleModel = getModelFromHash(GetEntityModel(vehicle))
if UseGeneralVehicleConfig then
repeat
gear = gear % #Config.VehicleModes + 1
currentVehicleMode = Config.VehicleModes[gear]
until Config.GeneralVehicleConfig[currentVehicleMode] ~= nil
else
repeat
gear = gear % #Config.VehicleModes + 1
currentVehicleMode = Config.VehicleModes[gear]
until Config.VehiclesConfig[vehicleModel][currentVehicleMode] ~= nil
end
if currentVehicle ~= nil and vehicle ~= currentVehicle then
gear = 1
end
currentVehicle = vehicle
TriggerEvent('qb-pursuitmode:vehicleModeChanged', currentVehicleMode)
end
local function updatePlayerInfo()
local playerData = QBCore.Functions.GetPlayerData()
playerJob = playerData.job
end
local function isAuthorizedToSwitchMode()
if next(Config.AuthorizedJobs) == nil then -- No jobs defined
return true
end
for i, v in ipairs(Config.AuthorizedJobs) do
if playerJob.name == v then
return true
end
end
return false
end
local function playSound(vehicle)
if Config.PlayServerSyncedSound then
local maxSoundDistance = Config.MaxSoundDistance
local speed = GetEntitySpeed(vehicle)
if speed > 30.0 then
maxSoundDistance = Config.MaxSoundDistanceAtMediumSpeeds
elseif speed > 70.0 then
maxSoundDistance = Config.MaxSoundDistanceAtHighSpeeds
end
TriggerServerEvent('InteractSound_SV:PlayWithinDistance', maxSoundDistance, Config.SoundFile, Config.SoundVolume)
else
TriggerEvent('InteractSound_CL:PlayOnOne', Config.SoundFile, Config.SoundVolume)
end
end
RegisterNetEvent('qb-pursuitmode:client:changeVehicleMode')
AddEventHandler('qb-pursuitmode:client:changeVehicleMode', function()
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped, false) then
local vehicle = GetVehiclePedIsIn(ped)
if DoesEntityExist(vehicle) and isValidVehicle(vehicle) and isAuthorizedToSwitchMode() then
changeVehicleMode(vehicle)
updateHandling(vehicle)
applyVehicleMods(vehicle)
if Config.PlaySoundOnSwitch then playSound(vehicle) end
QBCore.Functions.Notify((Config.SwitchNotification):format(currentVehicleMode))
end
end
end)
-- used for when restarting the script, will fetch the players job again and update the modelHashMap
AddEventHandler('onClientResourceStart', function (resourceName)
if(GetCurrentResourceName() ~= resourceName) then
return
end
updatePlayerInfo()
setupModelHashMap()
end)
RegisterNetEvent('QBCore:Client:OnJobUpdate', function(JobInfo)
playerJob = JobInfo
end)
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
updatePlayerInfo()
setupModelHashMap()
end)
RegisterCommand("pursuitmode", function(source, args, rawCommand)
TriggerEvent('qb-pursuitmode:client:changeVehicleMode')
end, false)
RegisterKeyMapping('pursuitmode', 'Change pursuitmode (POLICE ONLY)', 'keyboard', Config.DefaultKey)