forked from DaniSalam4eto/SAFS_FREECAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
313 lines (263 loc) · 9.81 KB
/
client.lua
File metadata and controls
313 lines (263 loc) · 9.81 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
local inFreecam = false
local lockMode = false
local cam = nil
local pos = vector3(0.0, 0.0, 0.0)
local yaw = 0.0
local pitch = 0.0
local roll = 0.0
local moveSpeed = Config.DefaultMoveSpeed
local fov = Config.StartFov
local activePreset = nil
local activeSlot = nil
local lastVeh = nil
local wasInVeh = false
local function clamp(v, mn, mx)
if v < mn then return mn elseif v > mx then return mx else return v end
end
local function wrapDeg(d)
d = d % 360.0
if d > 180.0 then d = d - 360.0 end
return d
end
local function rightFromYaw(y)
local r = math.rad(y)
return vector3(math.cos(r), math.sin(r), 0.0)
end
local function fwdFlat(y)
local r = math.rad(y)
return vector3(-math.sin(r), math.cos(r), 0.0)
end
local function pressed(code)
return IsControlJustPressed(0, code) or IsDisabledControlJustPressed(0, code)
end
local function held(code)
return IsControlPressed(0, code) or IsDisabledControlPressed(0, code)
end
local function ped()
return PlayerPedId()
end
local function isInVehicle()
return IsPedInAnyVehicle(ped(), false)
end
local function veh()
if not isInVehicle() then return nil end
local v = GetVehiclePedIsIn(ped(), false)
return (v ~= 0) and v or nil
end
local function vehDisplayName(v)
local model = GetEntityModel(v)
local disp = GetDisplayNameFromVehicleModel(model) or tostring(model)
local label = GetLabelText(disp)
local name = (label and label ~= "NULL" and label ~= "") and label or disp
return string.lower(name)
end
local function ensureCamFirstPerson()
local p = ped()
local v = veh()
if not v then return false end
pos = GetPedBoneCoords(p, Config.BONE_HEAD, 0.0, 0.0, 0.0)
local rot = GetEntityRotation(v, 2)
yaw = rot.z
pitch = Config.AXIS_NONE
roll = Config.AXIS_NONE
fov = Config.StartFov
if not cam or not DoesCamExist(cam) then
cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", true)
end
cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", true)
SetCamCoord(cam, pos.x, pos.y, pos.z)
SetCamRot(cam, pitch, roll, yaw, 2)
SetCamFov(cam, fov)
return true
end
local function setPlayerHidden(hidden)
local p = ped()
SetEntityVisible(p, not hidden, false)
SetEntityCollision(p, not hidden, not hidden)
FreezeEntityPosition(p, hidden)
SetPlayerInvincible(PlayerId(), hidden)
DisplayRadar(not hidden)
end
local function renderCam(enable)
RenderScriptCams(enable, false, 0, true, true)
end
local function makePresetFromSnap(v, slotName, snapPos, snapYaw, snapPitch, snapFov)
local off = GetOffsetFromEntityGivenWorldCoords(v, snapPos.x, snapPos.y, snapPos.z)
local rot = GetEntityRotation(v, 2)
local yawRel = wrapDeg((snapYaw or 0.0) - (rot.z or 0.0))
local pitchRel = wrapDeg((snapPitch or 0.0) - (rot.x or 0.0))
return {
name = tostring(slotName),
offset = { x = off.x, y = off.y, z = off.z },
yawRel = yawRel,
pitchRel = pitchRel,
fov = snapFov or Config.StartFov
}
end
local function captureForSave()
local v = veh()
if not v then return nil end
if inFreecam and cam then
return { pos = pos, yaw = yaw, pitch = pitch, fov = fov }
else
local head = GetPedBoneCoords(ped(), Config.BONE_HEAD, 0.0, 0.0, 0.0)
local h = vector3(head.x, head.y, head.z)
local rot = GetEntityRotation(v, 2)
return { pos = h, yaw = rot.z, pitch = rot.x, fov = Config.StartFov }
end
end
local function saveSlot(slot)
local v = veh()
if not v then return end
local snap = captureForSave()
if not snap then return end
local preset = makePresetFromSnap(v, slot, snap.pos, snap.yaw, snap.pitch, snap.fov)
TriggerServerEvent("fc:savePreset", vehDisplayName(v), preset)
end
local function requestSlot(slot)
local v = veh()
if not v then return end
TriggerServerEvent("fc:requestPreset", vehDisplayName(v), tostring(slot))
end
local function chosenAutosaveSlot()
if lockMode and activeSlot and string.match(activeSlot, "^%w+$") then
return activeSlot
end
return Config.AUTO_SLOT
end
local function autoSaveForVehicle(v)
if not v or not DoesEntityExist(v) then return end
local preset = makePresetFromSnap(v, chosenAutosaveSlot(), pos, yaw, pitch, fov)
TriggerServerEvent("fc:savePreset", vehDisplayName(v), preset)
end
local function enterMode(isLock, preset, vehName)
if not isInVehicle() then return end
inFreecam = true
lockMode = isLock or false
ensureCamFirstPerson()
renderCam(true)
setPlayerHidden(true)
if isLock then
activePreset = preset
activeSlot = preset and preset.name or nil
else
activePreset = nil
activeSlot = nil
end
end
local function exitCam(doAutosave)
if doAutosave then
local v = lastVeh or veh()
if v then autoSaveForVehicle(v) end
end
inFreecam = false
lockMode = false
if cam and DoesCamExist(cam) then
DestroyCam(cam, false)
cam = nil
end
renderCam(false)
setPlayerHidden(false)
activePreset = nil
activeSlot = nil
end
RegisterNetEvent("fc:applyPreset", function(preset, vehName)
if type(preset) ~= "table" then return end
enterMode(true, preset, vehName)
end)
AddEventHandler("onResourceStop", function(name)
if name ~= GetCurrentResourceName() then return end
if inFreecam then
local v = lastVeh or veh()
if v then autoSaveForVehicle(v) end
end
end)
CreateThread(function()
while true do
local vnow = veh()
if vnow ~= nil then lastVeh = vnow end
local nowIn = isInVehicle()
if wasInVeh and not nowIn then
if lastVeh and inFreecam then autoSaveForVehicle(lastVeh) end
end
wasInVeh = nowIn
if inFreecam and cam and DoesCamExist(cam) then
if not nowIn then
exitCam(true)
else
if lockMode and activePreset then
pos = GetOffsetFromEntityInWorldCoords(vnow, activePreset.offset.x, activePreset.offset.y, activePreset.offset.z)
local rot = GetEntityRotation(vnow, 2)
yaw = wrapDeg((rot.z or 0.0) + (activePreset.yawRel or 0.0))
pitch = wrapDeg((rot.x or 0.0) + (activePreset.pitchRel or 0.0))
roll = rot.y or Config.AXIS_NONE
SetCamCoord(cam, pos.x, pos.y, pos.z)
SetCamRot(cam, pitch, roll, yaw, 2)
SetCamFov(cam, activePreset.fov or fov)
else
DisableAllControlActions(0)
EnableControlAction(0, 245, true)
EnableControlAction(0, 200, true)
local lx = GetDisabledControlNormal(0, Config.CTRL_MOUSE_X)
yaw = yaw - (lx * Config.MouseSensitivity)
local speed = moveSpeed
if IsDisabledControlPressed(0, Config.CTRL_FAST) then
speed = speed * Config.FastMultiplier
elseif IsDisabledControlPressed(0, Config.CTRL_SLOW) then
speed = speed * Config.SlowMultiplier
end
local dt = GetFrameTime()
local f = fwdFlat(yaw)
local r = rightFromYaw(yaw)
if IsDisabledControlPressed(0, Config.CTRL_FWD) then pos = pos + (f * speed * dt) end
if IsDisabledControlPressed(0, Config.CTRL_BACK) then pos = pos - (f * speed * dt) end
if IsDisabledControlPressed(0, Config.CTRL_LEFT) then pos = pos - (r * speed * dt) end
if IsDisabledControlPressed(0, Config.CTRL_RIGHT) then pos = pos + (r * speed * dt) end
if IsDisabledControlPressed(0, Config.CTRL_DOWN) then pos = pos - (vector3(0.0, 0.0, 1.0) * speed * dt) end
if IsDisabledControlPressed(0, Config.CTRL_UP) then pos = pos + (vector3(0.0, 0.0, 1.0) * speed * dt) end
if IsDisabledControlPressed(0, Config.CTRL_ZOOM_OUT) then
fov = clamp(fov + (Config.FOV_RATE_PER_SEC * dt), Config.FOV_MIN, Config.FOV_MAX)
end
if IsDisabledControlPressed(0, Config.CTRL_ZOOM_IN) then
fov = clamp(fov - (Config.FOV_RATE_PER_SEC * dt), Config.FOV_MIN, Config.FOV_MAX)
end
pos = pos + GetEntityVelocity(vnow) * dt
SetCamCoord(cam, pos.x, pos.y, pos.z)
SetCamRot(cam, Config.AXIS_NONE, Config.AXIS_NONE, yaw, 2)
SetCamFov(cam, fov)
end
end
end
local altHeld = held(Config.CTRL_ALT)
local ctrlHeld = held(Config.CTRL_SLOW)
for n, code in pairs(Config.KEY_NUMS) do
if (IsDisabledControlJustPressed(0, code) or IsControlJustPressed(0, code)) then
if altHeld and nowIn then
saveSlot(n)
elseif ctrlHeld and nowIn then
if inFreecam and lockMode and activeSlot == n then
exitCam(true)
else
requestSlot(n)
end
end
end
end
if pressed(Config.CTRL_TOGGLE) then
if inFreecam then
exitCam(true)
else
if nowIn then enterMode(false) end
end
end
if held(Config.CTRL_SLOW) then
if (IsDisabledControlJustPressed(0, 172) or IsControlJustPressed(0, 172)) then
moveSpeed = clamp(moveSpeed + 0.5, 0.1, 200.0)
end
if (IsDisabledControlJustPressed(0, 173) or IsControlJustPressed(0, 173)) then
moveSpeed = clamp(moveSpeed - 0.5, 0.1, 200.0)
end
end
Wait(0)
end
end)