-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
470 lines (411 loc) · 12 KB
/
client.js
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* FiveM admin-cam
* Client code
*/
// TODO: create external config file
const Config = {
'mouse-look-sens': {
uxName: 'Mouse Sensitivity',
type: 'float',
value: 0.1,
},
'gamepad-look-sens': {
uxName: 'Gamepad Look Sensitivity',
type: 'float',
value: 0.03,
},
'movement-factor': { uxName: 'Movement Factor', type: 'float', value: 0.01 },
}
let activePlayers = []
emitNet('getAllPlayers')
onNet('updateAllPlayers', (playerList) => {
activePlayers = playerList
})
let spawnedPed = null
let spawnedPedVeh = null
let cam = null
let isSpectate = false
const camCoordRanges = {
x: { max: 10, min: -10 },
y: { max: 15, min: -15 },
z: { min: 2, max: 15 },
fov: { min: 10, max: 140 },
}
RegisterCommand('acam-getconfig', (src, args, raw) => {
sendInfo('Current configuration:', { color: [255, 180, 0] })
sendInfo(
'Sensitivity Factor Mouse: ' +
(
GetResourceKvpFloat('MOUSELOOKSENS') || Config['mouse-look-sens'].value
).toFixed(3),
{ color: [255, 180, 0] }
)
sendInfo(
'Sensitivity Factor Gamepad: ' +
(
GetResourceKvpFloat('GAMEPADLOOKSENS') ||
Config['gamepad-look-sens'].value
).toFixed(3),
{ color: [255, 180, 0] }
)
sendInfo(
'Sensitivity Factor Movement: ' +
(
GetResourceKvpFloat('MOVEMENTFACTOR') || Config['movement-factor'].value
).toFixed(3),
{ color: [255, 180, 0] }
)
})
RegisterCommand('acam-setconfig', (src, args, raw) => {
if (args.length == 0) {
return sendInfo(
'Format: acam-setconfig <' +
Object.keys(Config).join(' | ') +
'> <new value>'
)
}
if (!(args[0] in Config))
return sendError(
'Unkown config, please use one of the following: ' +
Object.keys(Config).join(', ')
)
const confObj = Config[args[0]]
if (args.length < 2)
return sendError(
'Please provide a value to be set as config (value type: ' +
confObj.type +
')'
)
let value = args[1]
// not sure if I'll need integer config, I'll just add it just in case
if (confObj.type == 'float' || confObj.type == 'int') {
value = +args[1]
// I don't think there will ever be a negative value required
// in the config so I'll just go ahead and hardcode the < 0 rule
// instead of adding another property into the config file
if (value < 0 || Number.isNaN(value))
return sendError('You have to provide a numerical sensitivity above 0')
if (confObj.type == 'float')
SetResourceKvpFloat(args[0].split('-').join('').toUpperCase(), value)
else SetResourceKvpInt(args[0].split('-').join('').toUpperCase(), value)
sendInfo(
'Setted config ' +
args[0] +
' to ' +
value.toFixed(3) +
'.' +
(cam != null && IsCamActive(cam)
? " Changes will take effect after you've stopped watching the current player."
: '')
)
return
}
SetResourceKvp(args[0].split('-').join('').toUpperCase(), args[1])
sendInfo(
'Setted config ' +
args[0] +
' to ' +
args[1] +
'.' +
(cam != null && IsCamActive(cam)
? " Changes will take effect after you've stopped watching the current player."
: '')
)
})
RegisterCommand(
'fixCamToPed',
(src, args, raw) => {
let ped = null
if (args.length < 1) {
if (spawnedPed == null)
return sendError('Please specify an ID or spawn a ped (/spawnped)')
ped = spawnedPed
} else {
let tmpPed = PlayerPedId(args[0])
if (!IsEntityAPed(tmpPed))
return sendError('Specified ID is not a player')
ped = tmpPed
}
let eCoords = GetEntityCoords(ped),
eRot = GetGameplayCamRot(2)
if (!DoesCamExist(cam)) {
cam = CreateCameraWithParams(
'DEFAULT_SCRIPTED_CAMERA',
eCoords,
eRot,
100,
true,
2
)
}
playerInvisible()
AttachCamToEntity(cam, ped, 0, 0, 12, false)
SetCamActive(cam, true)
RenderScriptCams(true, 1, 500, true, false, false)
// important to call after player invisible so the last location gets cached
setTickTimer()
},
false
)
RegisterCommand(
'setCamFov',
(src, args, raw) => {
if (args.length < 1) SetCamFov(cam, 100)
else SetCamFov(cam, Number(args[0]))
},
false
)
RegisterCommand(
'stopWatch',
(src, args, raw) => {
clearTickTimer()
Wait(1)
if (DoesCamExist(cam)) {
// destroy cam
SetCamActive(cam, false)
DestroyCam(cam, false)
cam = null
}
playerVisible()
RenderScriptCams(false, 1, 500, true, false, false)
},
false
)
RegisterCommand(
'spawnRandomPed',
(src, args, raw) => {
if (DoesEntityExist(spawnedPed)) {
SetEntityAsNoLongerNeeded(spawnedPed)
spawnedPed = null
}
if (DoesEntityExist(spawnedPedVeh)) {
SetEntityAsNoLongerNeeded(spawnedPed)
spawnedPedVeh = null
}
const pedHash = GetHashKey('a_m_m_socenlat_01')
const carHash = GetHashKey('police3')
RequestModel(pedHash)
RequestModel(carHash)
let loadCnt = 0
let cancel = false
while (!HasModelLoaded(pedHash) && !HasModelLoaded(carHash) && !cancel) {
loadCnt++
if (loadCnt > 10) {
cancel = true
return
}
Wait(500)
}
if (cancel) {
sendInfo('Could not load model within 5s')
return
}
const [pX, pY, pZ] = GetGameplayCamCoord()
spawnedPedVeh = CreateVehicle(carHash, pX, pY, pZ, 0, true, false)
SetVehicleOnGroundProperly(spawnedPedVeh)
SetVehicleEngineOn(spawnedPedVeh, true, true, false)
spawnedPed = CreatePedInsideVehicle(
spawnedPedVeh,
4,
pedHash,
-1,
true,
false
)
SetBlockingOfNonTemporaryEvents(spawnedPed)
TaskVehicleDriveWander(spawnedPed, spawnedPedVeh, 65, 2883636)
},
false
)
function sendInfo(txt, options = {}) {
emit('chat:addMessage', {
...options,
multiline: true,
args: ['[i] Admin Watch', txt],
})
}
function sendError(txt, options = {}) {
emit('chat:addMessage', {
...options,
args: ['[!] Admin Watch', txt],
})
}
function log(txt) {
console.log(txt)
/**
emit('chat:addMessage', {
args: [txt],
})
/**/
}
let playerLastLocation = [0, 0, 0]
function playerInvisible() {
let playerPed = GetPlayerPed(-1)
let oldpLoc = playerLastLocation
playerLastLocation = GetEntityCoords(playerPed)
Wait(1000)
log(
'Logged player last location from ' +
JSON.stringify(oldpLoc) +
' to ' +
JSON.stringify(playerLastLocation)
)
FreezeEntityPosition(playerPed, true)
NetworkConcealEntity(playerPed, true)
SetEntityHeading(playerPed, 0)
}
function playerVisible() {
let playerPed = GetPlayerPed(-1)
log('Recovering player with location ' + JSON.stringify(playerLastLocation))
Wait(1000)
let safeCoord = GetSafeCoordForPed(
playerLastLocation[0],
playerLastLocation[1],
playerLastLocation[2],
true,
0
)
SetEntityCoords(
playerPed,
safeCoord[0],
safeCoord[1],
safeCoord[2],
false,
false,
false,
false
)
NetworkConcealEntity(playerPed, false)
FreezeEntityPosition(playerPed, false)
playerLastLocation = [0, 0, 0]
}
let camControlTick = null
let mouseSensFactor = Config['mouse-look-sens'].value
let gamepadSensFactor = Config['gamepad-look-sens'].value
let movementFactor = Config['movement-factor'].value
function setTickTimer() {
if (camControlTick != null) clearTickTimer()
mouseSensFactor =
GetResourceKvpFloat('MOUSELOOKSENS') || Config['mouse-look-sens'].value
gamepadSensFactor =
GetResourceKvpFloat('GAMEPADLOOKSENS') || Config['gamepad-look-sens'].value
movementFactor =
GetResourceKvpFloat('MOVEMENTFACTOR') || Config['movement-factor'].value
camControlTick = setTick(adminTickFunc)
}
function clearTickTimer() {
clearTick(camControlTick)
camControlTick = null
}
let camOffset = {
coords: [0, 0, camCoordRanges.z.min],
rot: [0, 0, 0],
fov: 100,
}
function adminTickFunc() {
let playerPed = GetPlayerPed(-1)
if (!DoesCamExist(cam)) {
return
}
//let newCamLoc = { ...camOffset }
const fovRelativeSensRatio = 90 // smaller = quicker look-around
let drawData = []
// + special key 45 used for fov/zoom
if (IsControlPressed(0, 45)) {
if (IsControlPressed(0, 71)) {
// Zoom In
camOffset.fov -= GetControlValue(0, 71) / 60
if (camOffset.fov < camCoordRanges.fov.min)
camOffset.fov = camCoordRanges.fov.min
} else if (IsControlPressed(0, 72)) {
// Zoom Out
camOffset.fov += GetControlValue(0, 72) / 60
if (camOffset.fov > camCoordRanges.fov.max)
camOffset.fov = camCoordRanges.fov.max
}
} else {
// 180 > rotation > -180
const cameraRot = GetCamRot(cam, 2)[2]
const cameraRotNormalize = cameraRot + 90 // normalize (only positive) and turn 90 deg (make rotation start on positive X axis)
// Move Left/Right
camOffset.coords[0] -=
Cos(cameraRotNormalize + 90) *
(GetControlValue(0, 30) - 127) *
movementFactor
camOffset.coords[1] -=
Sin(cameraRotNormalize + 90) *
(GetControlValue(0, 30) - 127) *
movementFactor
// Move Up
camOffset.coords[0] -=
Cos(cameraRotNormalize) * (GetControlValue(0, 31) - 127) * movementFactor
camOffset.coords[1] -=
Sin(cameraRotNormalize) * (GetControlValue(0, 31) - 127) * movementFactor
if (camOffset.coords[0] < camCoordRanges.x.min)
camOffset.coords[0] = camCoordRanges.x.min
else if (camOffset.coords[0] > camCoordRanges.x.max)
camOffset.coords[0] = camCoordRanges.x.max
if (camOffset.coords[1] < camCoordRanges.y.min)
camOffset.coords[1] = camCoordRanges.y.min
else if (camOffset.coords[1] > camCoordRanges.y.max)
camOffset.coords[1] = camCoordRanges.y.max
// Move Up
if (IsControlPressed(0, 36)) {
camOffset.coords[2] += 70 * movementFactor
if (camOffset.coords[2] > camCoordRanges.z.max)
camOffset.coords[2] = camCoordRanges.z.max
}
// Move Down
else if (IsControlPressed(0, 26)) {
camOffset.coords[2] -= 100 * movementFactor
if (camOffset.coords[2] < camCoordRanges.z.min)
camOffset.coords[2] = camCoordRanges.z.min
}
}
// higher sens for mouse
lookaroundSens = IsInputDisabled(2) ? mouseSensFactor : gamepadSensFactor
// Look Left/Right
camOffset.rot[2] -=
(((GetControlValue(0, 1) - 127) * camOffset.fov) / 180) * lookaroundSens
if (camOffset.rot[2] < -360 || camOffset.rot[2] > 360) camOffset.rot[2] = 0
// Look Up/Down
camOffset.rot[0] -=
(((GetControlValue(0, 2) - 127) * camOffset.fov) / 180) * lookaroundSens
if (camOffset.rot[0] < -89) camOffset.rot[0] = -89
else if (camOffset.rot[0] > 89) camOffset.rot[0] = 89
// update coords
AttachCamToEntity(
cam,
spawnedPed,
camOffset.coords[0],
camOffset.coords[1],
camOffset.coords[2],
false
)
// didn't work with the selected ped coord smh
let playerPedCoords = GetCamCoord(cam)
SetEntityCoords(
playerPed,
playerPedCoords[0],
playerPedCoords[1],
playerPedCoords[2],
false,
false,
false,
false
)
// update rot
SetCamRot(cam, camOffset.rot[0], camOffset.rot[1], camOffset.rot[2], 2)
// update fov
SetCamFov(cam, camOffset.fov)
}
function quickArrCompare(a1, a2) {
if (!Array.isArray(a1) || !Array.isArray(a2))
throw new Error('Plesae give 2 arrays in the parameters')
// https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript/7837725#7837725
var i = a1.length
while (i--) {
if (a1[i] !== a2[i]) return false
}
return true
}