forked from bepo13/FiveM-SimpleCarHUD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
226 lines (197 loc) · 13.6 KB
/
Copy pathclient.lua
File metadata and controls
226 lines (197 loc) · 13.6 KB
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
-- SCREEN POSITION PARAMETERS
local screenPosX = 0.165 -- X coordinate (top left corner of HUD)
local screenPosY = 0.882 -- Y coordinate (top left corner of HUD)
-- GENERAL PARAMETERS
local enableController = true -- Enable controller inputs
-- SPEEDOMETER PARAMETERS
local speedLimit = 100.0 -- Speed limit for changing speed color
local speedColorText = {255, 255, 255} -- Color used to display speed label text
local speedColorUnder = {255, 255, 255} -- Color used to display speed when under speedLimit
local speedColorOver = {255, 96, 96} -- Color used to display speed when over speedLimit
-- FUEL PARAMETERS
local fuelShowPercentage = true -- Show fuel as a percentage (disabled shows fuel in liters)
local fuelWarnLimit = 25.0 -- Fuel limit for triggering warning color
local fuelColorText = {255, 255, 255} -- Color used to display fuel text
local fuelColorOver = {255, 255, 255} -- Color used to display fuel when good
local fuelColorUnder = {255, 96, 96} -- Color used to display fuel warning
-- SEATBELT PARAMETERS
local seatbeltInput = 311 -- Toggle seatbelt on/off with K or DPAD down (controller)
local seatbeltPlaySound = true -- Play seatbelt sound
local seatbeltDisableExit = true -- Disable vehicle exit when seatbelt is enabled
local seatbeltEjectSpeed = 45.0 -- Speed threshold to eject player (MPH)
local seatbeltEjectAccel = 100.0 -- Acceleration threshold to eject player (G's)
local seatbeltColorOn = {160, 255, 160} -- Color used when seatbelt is on
local seatbeltColorOff = {255, 96, 96} -- Color used when seatbelt is off
-- CRUISE CONTROL PARAMETERS
local cruiseInput = 137 -- Toggle cruise on/off with CAPSLOCK or A button (controller)
local cruiseColorOn = {160, 255, 160} -- Color used when seatbelt is on
local cruiseColorOff = {255, 255, 255} -- Color used when seatbelt is off
-- LOCATION AND TIME PARAMETERS
local locationAlwaysOn = false -- Always display location and time
local locationColorText = {255, 255, 255} -- Color used to display location and time
-- Lookup tables for direction and zone
local directions = { [0] = 'N', [1] = 'NW', [2] = 'W', [3] = 'SW', [4] = 'S', [5] = 'SE', [6] = 'E', [7] = 'NE', [8] = 'N' }
local zones = { ['AIRP'] = "Los Santos International Airport", ['ALAMO'] = "Alamo Sea", ['ALTA'] = "Alta", ['ARMYB'] = "Fort Zancudo", ['BANHAMC'] = "Banham Canyon Dr", ['BANNING'] = "Banning", ['BEACH'] = "Vespucci Beach", ['BHAMCA'] = "Banham Canyon", ['BRADP'] = "Braddock Pass", ['BRADT'] = "Braddock Tunnel", ['BURTON'] = "Burton", ['CALAFB'] = "Calafia Bridge", ['CANNY'] = "Raton Canyon", ['CCREAK'] = "Cassidy Creek", ['CHAMH'] = "Chamberlain Hills", ['CHIL'] = "Vinewood Hills", ['CHU'] = "Chumash", ['CMSW'] = "Chiliad Mountain State Wilderness", ['CYPRE'] = "Cypress Flats", ['DAVIS'] = "Davis", ['DELBE'] = "Del Perro Beach", ['DELPE'] = "Del Perro", ['DELSOL'] = "La Puerta", ['DESRT'] = "Grand Senora Desert", ['DOWNT'] = "Downtown", ['DTVINE'] = "Downtown Vinewood", ['EAST_V'] = "East Vinewood", ['EBURO'] = "El Burro Heights", ['ELGORL'] = "El Gordo Lighthouse", ['ELYSIAN'] = "Elysian Island", ['GALFISH'] = "Galilee", ['GOLF'] = "GWC and Golfing Society", ['GRAPES'] = "Grapeseed", ['GREATC'] = "Great Chaparral", ['HARMO'] = "Harmony", ['HAWICK'] = "Hawick", ['HORS'] = "Vinewood Racetrack", ['HUMLAB'] = "Humane Labs and Research", ['JAIL'] = "Bolingbroke Penitentiary", ['KOREAT'] = "Little Seoul", ['LACT'] = "Land Act Reservoir", ['LAGO'] = "Lago Zancudo", ['LDAM'] = "Land Act Dam", ['LEGSQU'] = "Legion Square", ['LMESA'] = "La Mesa", ['LOSPUER'] = "La Puerta", ['MIRR'] = "Mirror Park", ['MORN'] = "Morningwood", ['MOVIE'] = "Richards Majestic", ['MTCHIL'] = "Mount Chiliad", ['MTGORDO'] = "Mount Gordo", ['MTJOSE'] = "Mount Josiah", ['MURRI'] = "Murrieta Heights", ['NCHU'] = "North Chumash", ['NOOSE'] = "N.O.O.S.E", ['OCEANA'] = "Pacific Ocean", ['PALCOV'] = "Paleto Cove", ['PALETO'] = "Paleto Bay", ['PALFOR'] = "Paleto Forest", ['PALHIGH'] = "Palomino Highlands", ['PALMPOW'] = "Palmer-Taylor Power Station", ['PBLUFF'] = "Pacific Bluffs", ['PBOX'] = "Pillbox Hill", ['PROCOB'] = "Procopio Beach", ['RANCHO'] = "Rancho", ['RGLEN'] = "Richman Glen", ['RICHM'] = "Richman", ['ROCKF'] = "Rockford Hills", ['RTRAK'] = "Redwood Lights Track", ['SANAND'] = "San Andreas", ['SANCHIA'] = "San Chianski Mountain Range", ['SANDY'] = "Sandy Shores", ['SKID'] = "Mission Row", ['SLAB'] = "Stab City", ['STAD'] = "Maze Bank Arena", ['STRAW'] = "Strawberry", ['TATAMO'] = "Tataviam Mountains", ['TERMINA'] = "Terminal", ['TEXTI'] = "Textile City", ['TONGVAH'] = "Tongva Hills", ['TONGVAV'] = "Tongva Valley", ['VCANA'] = "Vespucci Canals", ['VESP'] = "Vespucci", ['VINE'] = "Vinewood", ['WINDF'] = "Ron Alternates Wind Farm", ['WVINE'] = "West Vinewood", ['ZANCUDO'] = "Zancudo River", ['ZP_ORT'] = "Port of South Los Santos", ['ZQ_UAR'] = "Davis Quartz" }
-- Globals
local pedInVeh = false
local timeText = ""
local locationText = ""
local currentFuel = 0.0
-- Main thread
Citizen.CreateThread(function()
-- Initialize local variable
local currSpeed = 0.0
local cruiseSpeed = 999.0
local prevVelocity = {x = 0.0, y = 0.0, z = 0.0}
local cruiseIsOn = false
local seatbeltIsOn = false
while true do
-- Loop forever and update HUD every frame
Citizen.Wait(0)
-- Get player PED, position and vehicle and save to locals
local player = GetPlayerPed(-1)
local position = GetEntityCoords(player)
local vehicle = GetVehiclePedIsIn(player, false)
-- Set vehicle states
if IsPedInAnyVehicle(player, false) then
pedInVeh = true
else
-- Reset states when not in car
pedInVeh = false
cruiseIsOn = false
seatbeltIsOn = false
end
-- Display Location and time when in any vehicle or on foot (if enabled)
if pedInVeh or locationAlwaysOn then
-- Get time and display
drawTxt(timeText, 4, locationColorText, 0.4, screenPosX, screenPosY + 0.048)
-- Display heading, street name and zone when possible
drawTxt(locationText, 4, locationColorText, 0.5, screenPosX, screenPosY + 0.075)
-- Display remainder of HUD when engine is on and vehicle is not a bicycle
local vehicleClass = GetVehicleClass(vehicle)
if pedInVeh and GetIsVehicleEngineRunning(vehicle) and vehicleClass ~= 13 then
-- Save previous speed and get current speed
local prevSpeed = currSpeed
currSpeed = GetEntitySpeed(vehicle)
-- Set PED flags
SetPedConfigFlag(PlayerPedId(), 32, true)
-- Check if seatbelt button pressed, toggle state and handle seatbelt logic
if IsControlJustReleased(0, seatbeltInput) and (enableController or GetLastInputMethod(0)) and vehicleClass ~= 8 then
-- Toggle seatbelt status and play sound when enabled
seatbeltIsOn = not seatbeltIsOn
if seatbeltPlaySound then
PlaySoundFrontend(-1, "Faster_Click", "RESPAWN_ONLINE_SOUNDSET", 1)
end
end
if not seatbeltIsOn then
-- Eject PED when moving forward, vehicle was going over 45 MPH and acceleration over 100 G's
local vehIsMovingFwd = GetEntitySpeedVector(vehicle, true).y > 1.0
local vehAcc = (prevSpeed - currSpeed) / GetFrameTime()
if (vehIsMovingFwd and (prevSpeed > (seatbeltEjectSpeed/2.237)) and (vehAcc > (seatbeltEjectAccel*9.81))) then
SetEntityCoords(player, position.x, position.y, position.z - 0.47, true, true, true)
SetEntityVelocity(player, prevVelocity.x, prevVelocity.y, prevVelocity.z)
Citizen.Wait(1)
SetPedToRagdoll(player, 1000, 1000, 0, 0, 0, 0)
else
-- Update previous velocity for ejecting player
prevVelocity = GetEntityVelocity(vehicle)
end
elseif seatbeltDisableExit then
-- Disable vehicle exit when seatbelt is on
DisableControlAction(0, 75)
end
-- When player in driver seat, handle cruise control
if (GetPedInVehicleSeat(vehicle, -1) == player) then
-- Check if cruise control button pressed, toggle state and set maximum speed appropriately
if IsControlJustReleased(0, cruiseInput) and (enableController or GetLastInputMethod(0)) then
cruiseIsOn = not cruiseIsOn
cruiseSpeed = currSpeed
end
local maxSpeed = cruiseIsOn and cruiseSpeed or GetVehicleHandlingFloat(vehicle,"CHandlingData","fInitialDriveMaxFlatVel")
SetEntityMaxSpeed(vehicle, maxSpeed)
else
-- Reset cruise control
cruiseIsOn = false
end
-- Check what units should be used for speed
if ShouldUseMetricMeasurements() then
-- Get vehicle speed in KPH and draw speedometer
local speed = currSpeed*3.6
local speedColor = (speed >= speedLimit) and speedColorOver or speedColorUnder
drawTxt(("%.3d"):format(math.ceil(speed)), 2, speedColor, 0.8, screenPosX + 0.000, screenPosY + 0.000)
drawTxt("KPH", 2, speedColorText, 0.4, screenPosX + 0.030, screenPosY + 0.018)
else
-- Get vehicle speed in MPH and draw speedometer
local speed = currSpeed*2.23694
local speedColor = (speed >= speedLimit) and speedColorOver or speedColorUnder
drawTxt(("%.3d"):format(math.ceil(speed)), 2, speedColor, 0.8, screenPosX + 0.000, screenPosY + 0.000)
drawTxt("MPH", 2, speedColorText, 0.4, screenPosX + 0.030, screenPosY + 0.018)
end
-- Draw fuel gauge
local fuelColor = (currentFuel >= fuelWarnLimit) and fuelColorOver or fuelColorUnder
drawTxt(("%.3d"):format(math.ceil(currentFuel)), 2, fuelColor, 0.8, screenPosX + 0.055, screenPosY + 0.000)
drawTxt("FUEL", 2, fuelColorText, 0.4, screenPosX + 0.085, screenPosY + 0.018)
-- Draw cruise control status
local cruiseColor = cruiseIsOn and cruiseColorOn or cruiseColorOff
drawTxt("CRUISE", 2, cruiseColor, 0.4, screenPosX + 0.040, screenPosY + 0.048)
-- Draw seatbelt status if not a motorcyle
if vehicleClass ~= 8 then
local seatbeltColor = seatbeltIsOn and seatbeltColorOn or seatbeltColorOff
drawTxt("SEATBELT", 2, seatbeltColor, 0.4, screenPosX + 0.080, screenPosY + 0.048)
end
end
end
end
end)
-- Secondary thread to update strings
Citizen.CreateThread(function()
while true do
-- Update when player is in a vehicle or on foot (if enabled)
if pedInVeh or locationAlwaysOn then
-- Get player, position and vehicle
local player = GetPlayerPed(-1)
local position = GetEntityCoords(player)
-- Update time text string
local hour = GetClockHours()
local minute = GetClockMinutes()
timeText = ("%.2d"):format((hour == 0) and 12 or hour) .. ":" .. ("%.2d"):format( minute) .. ((hour < 12) and " AM" or " PM")
-- Get heading and zone from lookup tables and street name from hash
local heading = directions[math.floor((GetEntityHeading(player) + 22.5) / 45.0)]
local zoneNameFull = zones[GetNameOfZone(position.x, position.y, position.z)]
local streetName = GetStreetNameFromHashKey(GetStreetNameAtCoord(position.x, position.y, position.z))
-- Update location text string
locationText = heading
locationText = (streetName == "" or streetName == nil) and (locationText) or (locationText .. " | " .. streetName)
locationText = (zoneNameFull == "" or zoneNameFull == nil) and (locationText) or (locationText .. " | " .. zoneNameFull)
-- Update fuel when in a vehicle
if pedInVeh then
local vehicle = GetVehiclePedIsIn(player, false)
if fuelShowPercentage then
-- Display remaining fuel as a percentage
currentFuel = 100 * GetVehicleFuelLevel(vehicle) / GetVehicleHandlingFloat(vehicle,"CHandlingData","fPetrolTankVolume")
else
-- Display remainign fuel in liters
currentFuel = GetVehicleFuelLevel(vehicle)
end
end
-- Update every second
Citizen.Wait(1000)
else
-- Wait until next frame
Citizen.Wait(0)
end
end
end)
-- Helper function to draw text to screen
function drawTxt(content, font, colour, scale, x, y)
SetTextFont(font)
SetTextScale(scale, scale)
SetTextColour(colour[1],colour[2],colour[3], 255)
SetTextEntry("STRING")
SetTextDropShadow(0, 0, 0, 0,255)
SetTextDropShadow()
SetTextEdge(4, 0, 0, 0, 255)
SetTextOutline()
AddTextComponentString(content)
DrawText(x, y)
end