-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclient.lua
More file actions
286 lines (230 loc) · 9.29 KB
/
client.lua
File metadata and controls
286 lines (230 loc) · 9.29 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
-- The activations sent by the server
Activations = {}
-- Whether the next set of collision changes should be printed on the console
PrintCollisionChanges = false
-- The last known player vehicle
LastVehicle = 0
-- The last known trailer/hooked vehicle
LastHooked = 0
function GetHookedVehicle(vehicle)
if vehicle == 0 or not IsEntityAVehicle(vehicle) then
return
end
local _, trailer = GetVehicleTrailerVehicle(vehicle)
if trailer ~= 0 then
return trailer
end
local towed = GetEntityAttachedToTowTruck(vehicle)
if towed ~= 0 then
return towed
end
local hooked = GetVehicleAttachedToCargobob(vehicle)
if hooked ~= 0 then
return hooked
end
return 0
end
function DrawDebugMarker(entity)
if entity == 0 or not IsEntityOnScreen(entity) then
return
end
local pos = GetEntityCoords(entity, false)
local _, x, y = GetScreenCoordFromWorldCoord(pos.x, pos.y, pos.z)
BeginTextCommandDisplayText("CELL_EMAIL_BCON")
AddTextComponentSubstringPlayerName(tostring(entity))
SetTextColour(255, 66, 198, 255)
SetTextJustification(0)
EndTextCommandDisplayText(x, y)
end
function DisableCollisionsThisFrame(one, two)
if one == 0 or two == 0 then
return
end
SetEntityNoCollisionEntity(one, two, true)
SetEntityNoCollisionEntity(two, one, true)
if PrintCollisionChanges then
print("Disabled collisions between " .. one .. " and " .. two .. " .");
end
end
function SetAlpha(entity, alpha)
if entity == 0 then
return
end
if alpha >= 255 then
ResetEntityAlpha(entity)
else
SetEntityAlpha(entity, alpha, 0)
end
end
function GetLocalPlayerActivation()
local playerId = PlayerId()
local player = GetPlayerServerId(playerId)
return GetPlayerActivation(player)
end
function GetPlayerActivation(player)
local activation = Activations[player]
if activation ~= nil then
return activation
else
return GetDefaultActivation()
end
end
function SetLocalPlayerActivation(activation)
-- TODO: Add proper boolean checks
Debug("Requesting server to change the activation to " .. activation)
TriggerServerEvent("simplepassive:setPassive", activation)
end
function Initialize()
TriggerServerEvent("simplepassive:initialized")
end
function HandleCollisions()
while true do
local localPlayer = PlayerId()
local localPed = PlayerPedId()
local localVehicle = GetVehiclePedIsIn(localPed, false)
local localHooked = GetHookedVehicle(localVehicle)
local localActivation = GetLocalPlayerActivation()
local debug = GetConvarInt("simplepassive_debug", 0) ~= 0
local shadowAlpha = GetConvarInt("simplepassive_alpha", 200)
local setInvincible = GetConvarInt("simplepassive_makeinvincible", 0) ~= 0
local disableCombat = GetConvarInt("simplepassive_disablecombat", 0) ~= 0
SetEntityInvincible(localPed, setInvincible and localActivation)
if localVehicle then
SetEntityInvincible(localVehicle, setInvincible and localActivation)
end
if localHooked then
SetEntityInvincible(localHooked, setInvincible and localActivation)
end
-- this ones will be overridden by the checks above from the other player's client
if setInvincible then
if LastVehicle ~= localVehicle then
SetEntityInvincible(LastVehicle, false)
LastVehicle = localVehicle
end
if LastHooked ~= localHooked then
SetEntityInvincible(LastHooked, false)
LastHooked = localHooked
end
end
if localActivation and disableCombat then
DisablePlayerFiring(localPlayer, true)
DisableControlAction(0, 45, true) -- INPUT_RELOAD
DisableControlAction(0, 263, true) -- INPUT_MELEE_ATTACK1
DisableControlAction(0, 264, true) -- INPUT_MELEE_ATTACK2
DisableControlAction(0, 140, true) -- INPUT_MELEE_ATTACK_LIGHT
DisableControlAction(0, 141, true) -- INPUT_MELEE_ATTACK_HEAVY
DisableControlAction(0, 142, true) -- INPUT_MELEE_ATTACK_ALTERNATE
DisableControlAction(0, 143, true) -- INPUT_MELEE_BLOCK
DisableControlAction(0, 24, true) -- INPUT_ATTACK
DisableControlAction(0, 257, true) -- INPUT_ATTACK2
DisableControlAction(0, 69, true) -- INPUT_VEH_ATTACK
DisableControlAction(0, 70, true) -- INPUT_VEH_ATTACK2
DisableControlAction(0, 91, true) -- INPUT_VEH_PASSENGER_AIM
DisableControlAction(0, 92, true) -- INPUT_VEH_PASSENGER_ATTACK
DisableControlAction(0, 114, true) -- INPUT_VEH_FLY_ATTACK
DisableControlAction(0, 331, true) -- INPUT_VEH_FLY_ATTACK2
end
if debug then
DrawDebugMarker(localPed)
DrawDebugMarker(localVehicle)
DrawDebugMarker(localHooked)
end
for _, otherPlayer in ipairs(GetActivePlayers()) do
if otherPlayer == localPlayer then
goto continue
end
local otherPed = GetPlayerPed(otherPlayer)
local otherVehicle = GetVehiclePedIsIn(otherPed, false)
local otherHooked = GetHookedVehicle(otherVehicle)
local otherActivation = GetPlayerActivation(GetPlayerServerId(otherPlayer))
local shouldDisableCollisions = otherActivation or localActivation
local alpha = 255
if shouldDisableCollisions and not GetIsTaskActive(otherPed, 2) and
(otherVehicle == 0 or otherVehicle ~= localVehicle) then
alpha = shadowAlpha
end
SetAlpha(otherPed, alpha)
SetAlpha(otherVehicle, alpha)
SetAlpha(otherHooked, alpha)
if debug then
DrawDebugMarker(otherPed)
DrawDebugMarker(otherVehicle)
DrawDebugMarker(otherHooked)
end
if shouldDisableCollisions then
if otherVehicle and IsPedInVehicle(otherVehicle, localPed, false) and
GetPedInVehicleSeat(otherVehicle, -1) ~= localPed then
goto continue
end
DisableCollisionsThisFrame(localPed, otherPed)
DisableCollisionsThisFrame(localPed, otherVehicle)
DisableCollisionsThisFrame(localPed, otherHooked)
DisableCollisionsThisFrame(localVehicle, otherPed)
DisableCollisionsThisFrame(localVehicle, otherVehicle)
DisableCollisionsThisFrame(localVehicle, otherHooked)
DisableCollisionsThisFrame(localHooked, otherPed)
DisableCollisionsThisFrame(localHooked, otherVehicle)
DisableCollisionsThisFrame(localHooked, otherHooked)
-- luacheck: ignore 113
DisableCamCollisionForEntity(otherPed)
if otherVehicle then
-- luacheck: ignore 113
DisableCamCollisionForEntity(otherVehicle)
end
if otherHooked then
-- luacheck: ignore 113
DisableCamCollisionForEntity(otherHooked)
end
end
::continue::
end
if debug then
local debugText = "Passive Players: "
for _, playerId in ipairs(GetActivePlayers()) do
local player = GetPlayerServerId(playerId)
local activation = GetPlayerActivation(player)
-- fallback for race conditions
if activation == nil then
activation = GetDefaultActivation()
end
debugText = debugText .. " " .. player .. " " .. tostring(activation)
end
BeginTextCommandDisplayText("CELL_EMAIL_BCON")
AddTextComponentSubstringPlayerName(debugText)
SetTextScale(1, 0.5)
SetTextColour(255, 255, 255, 255)
EndTextCommandDisplayText(0, 0)
end
PrintCollisionChanges = false
Citizen.Wait(0)
end
end
function OnActivationChanged(playerId, activation)
-- TODO: Add proper boolean checks
local player = tonumber(playerId)
if player == nil then
return
end
Activations[player] = activation
Debug("Received Passive Activation of " .. player .. " (" .. tostring(activation) .. ")")
local localPlayer = PlayerId()
if player == GetPlayerServerId(localPlayer) then
local shouldDisableCombat = GetConvarInt("simplepassive_disablecombat", 0) ~= 0
SetPlayerCanDoDriveBy(localPlayer, (not activation and shouldDisableCombat) or not shouldDisableCombat)
end
end
function OnDoCleanup(player)
Activations[player] = nil
end
function OnPrintTickCommand(_, _, _)
if GetConvarInt("simplepassive_debug", 0) ~= 0 then
PrintCollisionChanges = true
end
end
exports("getActivation", GetLocalPlayerActivation)
exports("setActivation", SetLocalPlayerActivation)
Citizen.CreateThread(Initialize)
Citizen.CreateThread(HandleCollisions)
RegisterNetEvent("simplepassive:activationChanged", OnActivationChanged)
RegisterNetEvent("simplepassive:doCleanup", OnDoCleanup)
RegisterCommand("passiveprinttick", OnPrintTickCommand, true)