forked from QuantumMalice/vehiclehandler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
169 lines (137 loc) · 5.87 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
local ox_lib, msg_lib = lib.checkDependency('ox_lib', '3.17.0')
if not ox_lib then print(msg_lib) return end
if GetResourceState('ox_inventory') == 'started' then
local ox_inv, msg_inv = lib.checkDependency('ox_inventory', '2.39.1')
if not ox_inv then print(msg_inv) return end
end
---@class Handler : OxClass
local Handler = require 'modules.handler'
local Settings = lib.load('data.vehicle')
local function startThreads(vehicle)
if not vehicle then return end
if not Handler or Handler:isActive() then return end
Handler:setActive(true)
local oxfuel = Handler:isFuelOx()
local units = Handler:getUnits()
local class = GetVehicleClass(vehicle) or false
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
-- Retrieve latest vehicle data
bodyBuffer[1] = GetVehicleBodyHealth(vehicle)
healthBuffer[1] = GetVehicleEngineHealth(vehicle)
speedBuffer[1] = GetEntitySpeed(vehicle) * units
-- Driveability handler (health, fuel)
local fuelLevel = oxfuel and Entity(vehicle).state.fuel or GetVehicleFuelLevel(vehicle)
if healthBuffer[1] <= 0 or fuelLevel <= 6.4 then
if IsVehicleDriveable(vehicle, true) then
SetVehicleUndriveable(vehicle, true)
end
end
-- Reduce torque after half-life
if not Handler:isLimited() then
if healthBuffer[1] < 500 then
Handler:setLimited(true)
CreateThread(function()
while (cache.vehicle == vehicle) and (healthBuffer[1] < 500) do
local newtorque = (healthBuffer[1] + 500) / 1100
SetVehicleCheatPowerIncrease(vehicle, newtorque)
Wait(1)
end
Handler:setLimited(false)
end)
end
end
-- Prevent rotation controls while flipped/airborne
if Settings.regulated[class] then
if speedBuffer[1] < 2.0 then
if airborne then airborne = false end
roll = GetEntityRoll(vehicle)
else
airborne = IsEntityInAir(vehicle)
end
if (roll > 75.0 or roll < -75.0) or airborne then
SetVehicleOutOfControl(vehicle, false, false)
end
end
-- Damage handler
local bodyDiff = bodyBuffer[2] - bodyBuffer[1]
if bodyDiff >= 1 then
-- Calculate latest damage
local bodyDamage = bodyDiff * Settings.globalmultiplier * Settings.classmultiplier[class]
local vehicleHealth = healthBuffer[1] - bodyDamage
-- Update engine health
if vehicleHealth ~= healthBuffer[1] and vehicleHealth > 0 then
SetVehicleEngineHealth(vehicle, vehicleHealth)
elseif vehicleHealth ~= 0 then
SetVehicleEngineHealth(vehicle, 0.0) -- prevent negative engine health
end
-- Prevent negative body health
if bodyBuffer[1] < 0 then
SetVehicleBodyHealth(vehicle, 0.0)
end
-- Prevent negative tank health (explosion)
if GetVehiclePetrolTankHealth(vehicle) < 0 then
SetVehiclePetrolTankHealth(vehicle, 0.0)
end
end
-- Impact handler
local speedDiff = speedBuffer[2] - speedBuffer[1]
if speedDiff >= Settings.threshold.speed then
-- Handle wheel loss
if Settings.breaktire then
if bodyDiff >= Settings.threshold.tire then
math.randomseed(GetGameTimer())
Handler:breakTire(vehicle, math.random(0, 1))
end
end
-- Handle heavy impact
if speedDiff >= Settings.threshold.heavy then
SetVehicleUndriveable(vehicle, true)
SetVehicleEngineHealth(vehicle, 0.0) -- Disable vehicle completely
end
end
-- Store data for next cycle
bodyBuffer[2] = bodyBuffer[1]
healthBuffer[2] = healthBuffer[1]
speedBuffer[2] = speedBuffer[1]
Wait(100)
end
Handler:setActive(false)
-- Retrigger thread if admin spawns a new vehicle while in one
if cache.vehicle and cache.seat == -1 then
if Handler:isLimited() then Handler:setLimited(false) end
startThreads(cache.vehicle)
end
end)
end
lib.onCache('seat', function(seat)
if seat == -1 then
startThreads(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)
end)
lib.callback.register('vehiclehandler:adminwash', function()
if not Handler or not Handler:isActive() then return end
return Handler:adminwash()
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()
end)
lib.callback.register('vehiclehandler:basicfix', function(fixtype)
if not fixtype or type(fixtype) ~= 'string' then return end
if not Handler then return end
return Handler:basicfix(fixtype)
end)
CreateThread(function()
Handler = Handler:new()
startThreads(cache.vehicle)
end)