Repository navigation
Expand file tree
/
Copy pathclient.lua
More file actions
558 lines (458 loc) · 18.6 KB
/
Copy pathclient.lua
File metadata and controls
558 lines (458 loc) · 18.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
local defaultCoords = Config.propSpawnPosition
local defaultHeadingOffset = Config.defaultHeadingOffset or 180.0
local studioViewPosition = Config.studioViewPosition
local studioCameraMoveStep = Config.studioCameraMoveStep or 0.5
local studioCameraVerticalStep = Config.studioCameraVerticalStep or 0.5
local studioCameraOrbitStep = Config.studioCameraOrbitStep or 2.0
local studioCameraPanSensitivity = Config.studioCameraPanSensitivity or 0.15
local studioCameraRotationStep = Config.studioCameraRotationStep or 5.0
local studioCameraZoomStep = Config.studioCameraZoomStep or 1.5
local studioCameraZoomSpeed = Config.studioCameraZoomSpeed or 8.0
local propMoveStep = Config.propMoveStep or 0.25
local propRotationStep = Config.propRotationStep or 5.0
local studioTime = tonumber(GetResourceKvpString("studioTime")) or Config.defaultStudioTime or 12
local studioLook = GetResourceKvpString("studioLook") or Config.defaultStudioLook or "studio"
-- Track props
local lastSpawnedProp = nil
local studioCamera = nil
local pendingStudioZoom = 0.0
-- Rotation settings
local rotationSpeed = 0.25
local rotationEnabled = false
local propRotationOffset = 0.0
-- UI state
local currentIndex = 1
local nuiOpen = false
local freecamEnabled = false
-----------------------------------------------------
-- PROP LIST (Developers can add/remove freely)
-----------------------------------------------------
local propList = Config.propList
-----------------------------------------------------
-- Helper: Delete a prop safely
-----------------------------------------------------
local function DeleteProp(entity)
if entity and DoesEntityExist(entity) then
DeleteEntity(entity)
end
end
local function DeleteCurrentProp()
DeleteProp(lastSpawnedProp)
lastSpawnedProp = nil
end
-----------------------------------------------------
-- Helper: Apply "no lag" settings to props
-----------------------------------------------------
local function ApplyNoLagProperties(entity)
FreezeEntityPosition(entity, true)
SetEntityCollision(entity, false, false)
SetEntityInvincible(entity, true)
SetEntityDynamic(entity, false)
SetEntityProofs(entity, true, true, true, true, true, true, true, true)
end
-----------------------------------------------------
-- Spawn a prop by model name with the configured default rotation
-----------------------------------------------------
local function SpawnSkyProp(model)
DeleteCurrentProp()
if type(model) ~= "string" or model == "" then return false end
local hash = GetHashKey(model)
if not IsModelInCdimage(hash) then return false end
RequestModel(hash)
local loadDeadline = GetGameTimer() + 5000
while not HasModelLoaded(hash) and GetGameTimer() < loadDeadline do Wait(10) end
if not HasModelLoaded(hash) then return false end
-- Create the object
local obj = CreateObject(hash, defaultCoords.x, defaultCoords.y, defaultCoords.z, false, false, false)
ApplyNoLagProperties(obj)
SetEntityHeading(obj, GetEntityHeading(obj) + defaultHeadingOffset + propRotationOffset)
lastSpawnedProp = obj
SetModelAsNoLongerNeeded(hash)
return true
end
-----------------------------------------------------
-- Spawn by index (wraps around list)
-----------------------------------------------------
local function SpawnByIndex(index)
if #propList == 0 then return false end
index = math.floor(tonumber(index) or currentIndex)
if index < 1 then index = #propList end
if index > #propList then index = 1 end
currentIndex = index
return SpawnSkyProp(propList[currentIndex])
end
-----------------------------------------------------
-- Rotation thread (smooth spinning)
-----------------------------------------------------
CreateThread(function()
while true do
Wait(0)
if rotationEnabled and lastSpawnedProp and DoesEntityExist(lastSpawnedProp) then
SetEntityHeading(lastSpawnedProp, GetEntityHeading(lastSpawnedProp) + rotationSpeed)
end
end
end)
local photoLooks = {
natural = false,
studio = "cinema",
vibrant = "rply_saturation",
film = "NG_filmic10"
}
if studioTime < 0 or studioTime > 23 then studioTime = Config.defaultStudioTime or 12 end
if photoLooks[studioLook] == nil then studioLook = Config.defaultStudioLook or "studio" end
local function ApplyStudioTime()
NetworkOverrideClockTime(studioTime, 0, 0)
end
local function ApplyStudioLook()
ClearTimecycleModifier()
SetArtificialLightsState(false)
local modifier = photoLooks[studioLook]
if modifier then
SetTimecycleModifier(modifier)
SetTimecycleModifierStrength(studioLook == "studio" and 0.5 or 1.0)
end
if studioLook == "studio" then SetArtificialLightsState(true) end
end
local function ClearStudioPhotoSettings()
ClearTimecycleModifier()
SetArtificialLightsState(false)
NetworkClearClockTimeOverride()
end
local function SetStudioTime(hour)
hour = math.floor(tonumber(hour) or -1)
if hour < 0 or hour > 23 then return false end
studioTime = hour
SetResourceKvp("studioTime", tostring(studioTime))
if nuiOpen then ApplyStudioTime() end
return true
end
local function SetStudioLook(look)
if photoLooks[look] == nil then return false end
studioLook = look
SetResourceKvp("studioLook", studioLook)
if nuiOpen then ApplyStudioLook() end
return true
end
local DestroyStudioCamera
local CreateStudioCamera
local function UpdateNuiFocus()
SetNuiFocus(nuiOpen, nuiOpen)
SetNuiFocusKeepInput(false)
end
local function SetFreecamMode(enabled, restoreStudioCamera)
if freecamEnabled == enabled then return end
freecamEnabled = enabled
if enabled then
DestroyStudioCamera()
SetNuiFocus(false, false)
else
if nuiOpen and restoreStudioCamera ~= false then CreateStudioCamera() end
UpdateNuiFocus()
end
ExecuteCommand("freecam")
end
DestroyStudioCamera = function()
pendingStudioZoom = 0.0
if studioCamera then
RenderScriptCams(false, false, 0, true, true)
DestroyCam(studioCamera, false)
studioCamera = nil
end
ClearStudioPhotoSettings()
end
CreateStudioCamera = function()
DestroyStudioCamera()
studioCamera = CreateCam("DEFAULT_SCRIPTED_CAMERA", true)
SetCamCoord(studioCamera, studioViewPosition.x, studioViewPosition.y, studioViewPosition.z)
SetCamRot(studioCamera, 0.0, 0.0, studioViewPosition.w, 2)
RenderScriptCams(true, false, 0, true, true)
ApplyStudioTime()
ApplyStudioLook()
end
local function ResetStudioCameraPosition()
if not studioCamera and nuiOpen and not freecamEnabled then CreateStudioCamera() end
if not studioCamera then return false end
pendingStudioZoom = 0.0
SetCamCoord(studioCamera, studioViewPosition.x, studioViewPosition.y, studioViewPosition.z)
SetCamRot(studioCamera, 0.0, 0.0, studioViewPosition.w, 2)
SetCamActive(studioCamera, true)
RenderScriptCams(true, false, 0, true, true)
return true
end
local function SaveStudioSetup()
if not lastSpawnedProp or not DoesEntityExist(lastSpawnedProp) or not studioCamera then return false end
local propPosition = GetEntityCoords(lastSpawnedProp)
local propHeading = GetEntityHeading(lastSpawnedProp)
local cameraPosition = GetCamCoord(studioCamera)
local cameraRotation = GetCamRot(studioCamera, 2)
local values = {
index = currentIndex,
propX = propPosition.x,
propY = propPosition.y,
propZ = propPosition.z,
propHeading = propHeading,
cameraX = cameraPosition.x,
cameraY = cameraPosition.y,
cameraZ = cameraPosition.z,
cameraPitch = cameraRotation.x,
cameraRoll = cameraRotation.y,
cameraHeading = cameraRotation.z
}
for key, value in pairs(values) do
SetResourceKvp("savedSetup_" .. key, tostring(value))
end
SetResourceKvp("savedSetup_exists", "true")
return true
end
local function LoadStudioSetup()
if GetResourceKvpString("savedSetup_exists") ~= "true" then return false end
local savedIndex = tonumber(GetResourceKvpString("savedSetup_index"))
if not savedIndex or not SpawnByIndex(savedIndex) then return false end
local propPosition = vec3(
tonumber(GetResourceKvpString("savedSetup_propX")) or defaultCoords.x,
tonumber(GetResourceKvpString("savedSetup_propY")) or defaultCoords.y,
tonumber(GetResourceKvpString("savedSetup_propZ")) or defaultCoords.z
)
local propHeading = tonumber(GetResourceKvpString("savedSetup_propHeading"))
if lastSpawnedProp and DoesEntityExist(lastSpawnedProp) then
SetEntityCoordsNoOffset(lastSpawnedProp, propPosition.x, propPosition.y, propPosition.z, false, false, false)
if propHeading then
SetEntityHeading(lastSpawnedProp, propHeading)
propRotationOffset = propHeading - defaultHeadingOffset
end
end
if studioCamera then
local cameraPosition = vec3(
tonumber(GetResourceKvpString("savedSetup_cameraX")) or studioViewPosition.x,
tonumber(GetResourceKvpString("savedSetup_cameraY")) or studioViewPosition.y,
tonumber(GetResourceKvpString("savedSetup_cameraZ")) or studioViewPosition.z
)
local cameraRotation = vec3(
tonumber(GetResourceKvpString("savedSetup_cameraPitch")) or 0.0,
tonumber(GetResourceKvpString("savedSetup_cameraRoll")) or 0.0,
tonumber(GetResourceKvpString("savedSetup_cameraHeading")) or studioViewPosition.w
)
pendingStudioZoom = 0.0
SetCamCoord(studioCamera, cameraPosition.x, cameraPosition.y, cameraPosition.z)
SetCamRot(studioCamera, cameraRotation.x, cameraRotation.y, cameraRotation.z, 2)
SetCamActive(studioCamera, true)
RenderScriptCams(true, false, 0, true, true)
end
return true
end
local function GetCameraMovementOffset(direction, distance)
local cameraRotation = studioCamera and GetCamRot(studioCamera, 2) or GetGameplayCamRot(2)
local heading = math.rad(cameraRotation.z)
local forwardX, forwardY = -math.sin(heading), math.cos(heading)
local rightX, rightY = math.cos(heading), math.sin(heading)
if direction == "forward" then return forwardX * distance, forwardY * distance end
if direction == "backward" then return -forwardX * distance, -forwardY * distance end
if direction == "left" then return -rightX * distance, -rightY * distance end
if direction == "right" then return rightX * distance, rightY * distance end
return 0.0, 0.0
end
local function MoveStudioCamera(direction)
if not studioCamera then return false end
local cameraPosition = GetCamCoord(studioCamera)
if direction == "up" then
SetCamCoord(studioCamera, cameraPosition.x, cameraPosition.y, cameraPosition.z + studioCameraVerticalStep)
return true
end
if direction == "down" then
SetCamCoord(studioCamera, cameraPosition.x, cameraPosition.y, cameraPosition.z - studioCameraVerticalStep)
return true
end
local offsetX, offsetY = GetCameraMovementOffset(direction, studioCameraMoveStep)
SetCamCoord(studioCamera, cameraPosition.x + offsetX, cameraPosition.y + offsetY, cameraPosition.z)
return true
end
local function OrbitStudioCamera(direction)
if not studioCamera or not lastSpawnedProp or not DoesEntityExist(lastSpawnedProp) then return false end
local propPosition = GetEntityCoords(lastSpawnedProp)
local cameraPosition = GetCamCoord(studioCamera)
local offsetX = cameraPosition.x - propPosition.x
local offsetY = cameraPosition.y - propPosition.y
local distance = math.sqrt((offsetX * offsetX) + (offsetY * offsetY))
if distance < 0.01 then return false end
local angle = math.atan(offsetY, offsetX) + math.rad(studioCameraOrbitStep * direction)
local nextX = propPosition.x + (math.cos(angle) * distance)
local nextY = propPosition.y + (math.sin(angle) * distance)
SetCamCoord(studioCamera, nextX, nextY, cameraPosition.z)
PointCamAtCoord(studioCamera, propPosition.x, propPosition.y, propPosition.z)
return true
end
local function PanStudioCamera(deltaX, deltaY)
if not studioCamera then return false end
local rotation = GetCamRot(studioCamera, 2)
local pitch = math.max(-85.0, math.min(85.0, rotation.x + (deltaY * studioCameraPanSensitivity)))
local heading = rotation.z + (deltaX * studioCameraPanSensitivity)
SetCamRot(studioCamera, pitch, rotation.y, heading, 2)
return true
end
local function RotateStudioCamera(direction)
if not studioCamera then return false end
local rotation = GetCamRot(studioCamera, 2)
SetCamRot(studioCamera, rotation.x, rotation.y,
rotation.z + (studioCameraRotationStep * direction), 2)
return true
end
local function GetStudioCameraForwardVector()
local camera = studioCamera
if not camera then return { x = 0.0, y = 1.0, z = 0.0 } end
local rotation = GetCamRot(camera, 2)
local pitch = math.rad(rotation.x)
local heading = math.rad(rotation.z)
return {
x = -math.sin(heading) * math.cos(pitch),
y = math.cos(heading) * math.cos(pitch),
z = math.sin(pitch)
}
end
local function MoveStudioCameraAlongView(distance)
if not studioCamera then return false end
local forward = GetStudioCameraForwardVector()
local cameraPosition = GetCamCoord(studioCamera)
SetCamCoord(studioCamera,
cameraPosition.x + (forward.x * distance),
cameraPosition.y + (forward.y * distance),
cameraPosition.z + (forward.z * distance))
return true
end
CreateThread(function()
while true do
if not studioCamera or math.abs(pendingStudioZoom) < 0.001 then
Wait(100)
else
Wait(0)
local maximumStep = studioCameraZoomSpeed * GetFrameTime()
local adjustment = math.min(math.abs(pendingStudioZoom), maximumStep)
if pendingStudioZoom < 0 then adjustment = -adjustment end
if MoveStudioCameraAlongView(adjustment) then
pendingStudioZoom = pendingStudioZoom - adjustment
else
pendingStudioZoom = 0.0
end
end
end
end)
local function MoveCurrentProp(direction)
if not lastSpawnedProp or not DoesEntityExist(lastSpawnedProp) then return false end
local offsetX, offsetY = GetCameraMovementOffset(direction, propMoveStep)
local propPosition = GetEntityCoords(lastSpawnedProp)
SetEntityCoordsNoOffset(lastSpawnedProp, propPosition.x + offsetX, propPosition.y + offsetY, propPosition.z, false,
false, false)
return true
end
local function RotateCurrentProp(direction)
if not lastSpawnedProp or not DoesEntityExist(lastSpawnedProp) then return false end
local rotationChange = propRotationStep * direction
propRotationOffset = propRotationOffset + rotationChange
SetEntityHeading(lastSpawnedProp, GetEntityHeading(lastSpawnedProp) + rotationChange)
return true
end
local function ResetPropRotation()
if not lastSpawnedProp or not DoesEntityExist(lastSpawnedProp) then return false end
SetEntityHeading(lastSpawnedProp, GetEntityHeading(lastSpawnedProp) - propRotationOffset)
propRotationOffset = 0.0
return true
end
RegisterCommand("skyprop", function() SpawnByIndex(1) end, false)
RegisterCommand("nextprop", function() SpawnByIndex(currentIndex + 1) end, false)
RegisterCommand("prevprop", function() SpawnByIndex(currentIndex - 1) end, false)
RegisterCommand("rotateprop", function() rotationEnabled = not rotationEnabled end, false)
RegisterCommand("propstudiofreecam", function() SetFreecamMode(not freecamEnabled) end, false)
RegisterCommand("clearprops", function()
DeleteCurrentProp()
end, false)
-----------------------------------------------------
-- NUI (UI communication)
-----------------------------------------------------
RegisterCommand("propstudio", function()
nuiOpen = not nuiOpen
if nuiOpen then
propRotationOffset = 0.0
if freecamEnabled then SetFreecamMode(false, false) end
CreateStudioCamera()
else
DestroyStudioCamera()
end
UpdateNuiFocus()
SendNUIMessage({
action = "toggle",
show = nuiOpen,
props = propList,
index = currentIndex,
rotate = rotationEnabled,
freecam = freecamEnabled,
studioTime = studioTime,
studioLook = studioLook
})
end, false)
RegisterNUICallback("close", function(_, cb)
nuiOpen = false
DestroyStudioCamera()
UpdateNuiFocus()
cb("ok")
end)
AddEventHandler("onResourceStop", function(resourceName)
if resourceName == GetCurrentResourceName() then
DestroyStudioCamera()
end
end)
RegisterNUICallback("spawnIndex", function(data, cb)
SpawnByIndex(tonumber(data.index))
cb("ok")
end)
RegisterNUICallback("moveStudioCamera", function(data, cb)
cb({ success = MoveStudioCamera(data.direction) })
end)
RegisterNUICallback("orbitStudioCamera", function(data, cb)
cb({ success = OrbitStudioCamera(tonumber(data.direction) or 0) })
end)
RegisterNUICallback("panStudioCamera", function(data, cb)
cb({ success = PanStudioCamera(tonumber(data.deltaX) or 0, tonumber(data.deltaY) or 0) })
end)
RegisterNUICallback("rotateStudioCamera", function(data, cb)
cb({ success = RotateStudioCamera(tonumber(data.direction) or 0) })
end)
RegisterNUICallback("resetStudioCamera", function(_, cb)
cb({ success = ResetStudioCameraPosition() })
end)
RegisterNUICallback("saveStudioSetup", function(_, cb)
cb({ success = SaveStudioSetup() })
end)
RegisterNUICallback("loadStudioSetup", function(_, cb)
cb({ success = LoadStudioSetup() })
end)
RegisterNUICallback("zoomStudioCamera", function(data, cb)
local direction = tonumber(data.direction) or 0
pendingStudioZoom = pendingStudioZoom - (direction * studioCameraZoomStep)
cb({ success = direction ~= 0 })
end)
RegisterNUICallback("moveProp", function(data, cb)
cb({ success = MoveCurrentProp(data.direction) })
end)
RegisterNUICallback("rotateProp", function(data, cb)
cb({ success = RotateCurrentProp(tonumber(data.direction) or 0) })
end)
RegisterNUICallback("resetPropRotation", function(_, cb)
cb({ success = ResetPropRotation() })
end)
RegisterNUICallback("setStudioTime", function(data, cb)
cb({ success = SetStudioTime(data.hour) })
end)
RegisterNUICallback("setStudioLook", function(data, cb)
cb({ success = SetStudioLook(data.look) })
end)
RegisterNUICallback("toggleRotate", function(_, cb)
rotationEnabled = not rotationEnabled
cb({ rotate = rotationEnabled })
end)
RegisterNUICallback("toggleFreecam", function(_, cb)
SetFreecamMode(not freecamEnabled)
cb({ freecam = freecamEnabled })
end)
RegisterNUICallback("deleteProp", function(_, cb)
DeleteCurrentProp()
cb("ok")
end)