-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathclient.lua
More file actions
587 lines (496 loc) · 18.4 KB
/
Copy pathclient.lua
File metadata and controls
587 lines (496 loc) · 18.4 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
if not LoadResourceFile(GetCurrentResourceName(), 'web/build/index.html') then
print('^1[sd-zonecreator] ERROR: web/build folder not found!^0')
print('^1[sd-zonecreator] You likely downloaded the source code instead of the release.^0')
print('^1[sd-zonecreator] Please download the latest release from: https://github.com/Samuels-Development/sd-zonecreator/releases/latest^0')
return
end
local isZoneCreatorOpen = false -- Whether the zone creator UI is currently open
local isViewingZone = false -- Whether the zone viewer camera is active
local viewerCamera = nil -- Handle for the viewer camera
local originalPlayerCoords = nil -- Player's original coordinates before entering viewer
local originalPlayerHeading = nil -- Player's original heading before entering viewer
local currentDebugZone = nil -- Reference to the current debug zone polygon
local viewerCamCoords = vector3(0, 0, 0) -- Current position of the viewer camera
local viewerCamRot = vector3(0, 0, 0) -- Current rotation of the viewer camera
local currentZoneData = { points = {}, thickness = 150, zoneName = 'preview', groundZ = 0 } -- Current zone data for thickness adjustment
local StopZoneViewer -- Forward declaration of StopZoneViewer function
local zLookupQueue = {} -- Queue of pending Z coordinate lookup requests
local zLookupThreadRunning = false -- Whether the Z lookup background thread is running
local MAX_QUEUE_SIZE = 20 -- Maximum number of queued Z lookups
local Z_CACHE_GRID_SIZE = 10.0 -- Grid size for Z coordinate caching in game units
local zCoordCache = {} -- Cache storing Z coordinates by grid position
local Z_CACHE_TTL = 120000 -- Time-to-live for cached Z values in milliseconds
local Z_LOOKUP_COOLDOWN = 200 -- Minimum time between Z lookups in milliseconds
--- Opens the Zone Creator UI
local function OpenZoneCreator()
if isZoneCreatorOpen then return end
isZoneCreatorOpen = true
SetNuiFocus(true, true)
SendNUIMessage({
action = 'showZoneCreator'
})
end
--- Closes the Zone Creator UI
local function CloseZoneCreator()
isZoneCreatorOpen = false
SetNuiFocus(false, false)
SendNUIMessage({
action = 'hideZoneCreator'
})
end
--- NUI Callback for closing the zone creator
RegisterNUICallback('closeZoneCreator', function(_, cb)
CloseZoneCreator()
cb('ok')
end)
--- NUI Callback for copying text to clipboard from zone creator
RegisterNUICallback('copyToClipboard', function(data, cb)
if data.text then
SendNUIMessage({
action = 'copyToClipboard',
data = { text = data.text }
})
lib.notify({
title = 'Zone Creator',
description = 'Copied to clipboard!',
type = 'success'
})
end
cb('ok')
end)
--- Calculates the center point of a polygon
---@param points table Array of {x, y} points
---@return number, number Center X and Y coordinates
local function CalculatePolygonCenter(points)
local sumX, sumY = 0, 0
for _, point in ipairs(points) do
sumX = sumX + point.x
sumY = sumY + point.y
end
return sumX / #points, sumY / #points
end
--- Gets a cache key for coordinates by rounding to grid
---@param x number X coordinate
---@param y number Y coordinate
---@return string Cache key
local function GetZCacheKey(x, y)
local gridX = math.floor(x / Z_CACHE_GRID_SIZE)
local gridY = math.floor(y / Z_CACHE_GRID_SIZE)
return gridX .. "_" .. gridY
end
--- Checks the Z cache for a coordinate
---@param x number X coordinate
---@param y number Y coordinate
---@return number|nil Cached Z value or nil if not found or expired
local function GetCachedZ(x, y)
local key = GetZCacheKey(x, y)
local cached = zCoordCache[key]
if cached then
local now = GetGameTimer()
if now - cached.timestamp < Z_CACHE_TTL then
return cached.z
else
zCoordCache[key] = nil
end
end
return nil
end
--- Stores a Z value in the cache
---@param x number X coordinate
---@param y number Y coordinate
---@param z number Z coordinate to cache
local function CacheZ(x, y, z)
if z then
local key = GetZCacheKey(x, y)
zCoordCache[key] = { z = z, timestamp = GetGameTimer() }
end
end
--- Performs a comprehensive ground Z lookup by teleporting the player
---@param x number X coordinate
---@param y number Y coordinate
---@return number|nil Ground Z coordinate or nil if not found
local function GetGroundZComprehensive(x, y)
local ped = PlayerPedId()
local wasVisible = IsEntityVisible(ped)
local originalCoords = GetEntityCoords(ped)
local originalHeading = GetEntityHeading(ped)
local foundZ = nil
SetEntityVisible(ped, false, false)
FreezeEntityPosition(ped, true)
SetEntityCollision(ped, false, false)
Wait(50)
SetEntityCoords(ped, x, y, 1000.0, false, false, false, false)
Wait(100)
RequestCollisionAtCoord(x, y, 0.0)
RequestCollisionAtCoord(x, y, 100.0)
RequestCollisionAtCoord(x, y, 500.0)
local timeout = 0
while not HasCollisionLoadedAroundEntity(ped) and timeout < 50 do
Wait(100)
timeout = timeout + 1
end
Wait(500)
local searchHeights = { 1000.0, 800.0, 600.0, 400.0, 300.0, 200.0, 150.0, 100.0, 75.0, 50.0, 25.0, 0.0, -50.0 }
for _, height in ipairs(searchHeights) do
local found, groundZ = GetGroundZFor_3dCoord(x, y, height, false)
if found and groundZ ~= 0.0 then
foundZ = groundZ
break
end
Wait(50)
end
if not foundZ then
local teleportHeights = { 500.0, 200.0, 50.0 }
for _, tpHeight in ipairs(teleportHeights) do
SetEntityCoords(ped, x, y, tpHeight, false, false, false, false)
RequestCollisionAtCoord(x, y, tpHeight)
Wait(300)
for _, searchHeight in ipairs(searchHeights) do
local found, groundZ = GetGroundZFor_3dCoord(x, y, searchHeight, false)
if found and groundZ ~= 0.0 then
foundZ = groundZ
break
end
Wait(25)
end
if foundZ then break end
end
end
if not foundZ then
SetEntityCoords(ped, x, y, 1000.0, false, false, false, false)
Wait(200)
local rayHandle = StartShapeTestRay(x, y, 1000.0, x, y, -100.0, 1, ped, 7)
local retval, hit, endCoords, surfaceNormal, entityHit
local rayTimeout = 0
repeat
Wait(0)
retval, hit, endCoords, surfaceNormal, entityHit = GetShapeTestResult(rayHandle)
rayTimeout = rayTimeout + 1
until retval ~= 1 or rayTimeout > 100
if hit == 1 and endCoords.z ~= 0.0 then
foundZ = endCoords.z
end
end
Wait(100)
SetEntityCoords(ped, originalCoords.x, originalCoords.y, originalCoords.z, false, false, false, false)
SetEntityHeading(ped, originalHeading)
SetEntityCollision(ped, true, true)
FreezeEntityPosition(ped, false)
SetEntityVisible(ped, wasVisible, false)
return foundZ
end
--- Background thread that processes the Z lookup queue
local function StartZLookupThread()
if zLookupThreadRunning then return end
zLookupThreadRunning = true
CreateThread(function()
Wait(100)
while #zLookupQueue > 0 do
local request = table.remove(zLookupQueue, 1)
local x, y, callback = request.x, request.y, request.callback
local cachedZ = GetCachedZ(x, y)
if cachedZ then
callback(cachedZ, x, y)
Wait(50)
else
local foundZ = GetGroundZComprehensive(x, y)
if foundZ then
CacheZ(x, y, foundZ)
end
callback(foundZ, x, y)
Wait(100)
end
end
zLookupThreadRunning = false
end)
end
--- Queues an asynchronous Z coordinate lookup
---@param x number X coordinate
---@param y number Y coordinate
---@param callback function Callback function receiving (groundZ, actualX, actualY)
local function GetGroundZAtPositionAsync(x, y, callback)
local cachedZ = GetCachedZ(x, y)
if cachedZ then
SetTimeout(0, function()
callback(cachedZ, x, y)
end)
return
end
if #zLookupQueue >= MAX_QUEUE_SIZE then
SetTimeout(0, function()
callback(nil, x, y)
end)
return
end
local cacheKey = GetZCacheKey(x, y)
for _, request in ipairs(zLookupQueue) do
if GetZCacheKey(request.x, request.y) == cacheKey then
SetTimeout(0, function()
callback(nil, x, y)
end)
return
end
end
table.insert(zLookupQueue, { x = x, y = y, callback = callback })
StartZLookupThread()
end
--- Gets ground Z at position synchronously for use in zone viewer
---@param x number X coordinate
---@param y number Y coordinate
---@return number Z coordinate or default fallback value
local function GetGroundZAtPosition(x, y)
local searchHeights = { 1000.0, 500.0, 200.0, 100.0, 50.0, 0.0 }
for _, height in ipairs(searchHeights) do
local found, groundZ = GetGroundZFor_3dCoord(x, y, height, false)
if found and groundZ ~= 0.0 then
return groundZ
end
end
return 50.0
end
--- Creates a debug zone polygon using ox_lib
---@param points table Array of {x, y} points
---@param thickness number Zone thickness
---@param name string Zone name
---@param groundZ number The ground Z coordinate for the zone base
local function CreateDebugZone(points, thickness, name, groundZ)
if currentDebugZone then
currentDebugZone:remove()
currentDebugZone = nil
end
local zonePoints = {}
for _, point in ipairs(points) do
table.insert(zonePoints, vec3(point.x, point.y, groundZ))
end
currentDebugZone = lib.zones.poly({
name = 'zonehelper_preview_' .. name,
points = zonePoints,
thickness = thickness,
debug = true,
debugColour = { 34, 197, 94, 100 }
})
end
--- Removes the current debug zone if it exists
local function RemoveDebugZone()
if currentDebugZone then
currentDebugZone:remove()
currentDebugZone = nil
end
end
--- Updates the debug zone with the current zone data thickness
local function UpdateDebugZoneThickness()
CreateDebugZone(currentZoneData.points, currentZoneData.thickness, currentZoneData.zoneName, currentZoneData.groundZ)
end
--- Stops the zone viewer and returns player to original state
StopZoneViewer = function()
if not isViewingZone then return end
isViewingZone = false
RemoveDebugZone()
if viewerCamera then
RenderScriptCams(false, true, 500, true, false)
DestroyCam(viewerCamera, false)
viewerCamera = nil
end
local ped = PlayerPedId()
SetEntityVisible(ped, true, false)
SetEntityCollision(ped, true, true)
FreezeEntityPosition(ped, false)
if originalPlayerCoords then
SetEntityCoords(ped, originalPlayerCoords.x, originalPlayerCoords.y, originalPlayerCoords.z, false, false, false, false)
SetEntityHeading(ped, originalPlayerHeading or 0.0)
end
if isZoneCreatorOpen then
SetNuiFocus(true, true)
SendNUIMessage({
action = 'zoneViewerStopped',
data = {
thickness = currentZoneData.thickness
}
})
end
end
--- Starts the zone viewer camera at the specified location
---@param centerX number Center X coordinate of the zone
---@param centerY number Center Y coordinate of the zone
---@param points table Array of zone points
---@param groundZ number Ground Z coordinate
---@param thickness number Zone thickness
---@param zoneName string Name of the zone
local function StartZoneViewer(centerX, centerY, points, groundZ, thickness, zoneName)
if isViewingZone then return end
local ped = PlayerPedId()
local actualGroundZ = groundZ or GetGroundZAtPosition(centerX, centerY)
currentZoneData.points = points
currentZoneData.thickness = thickness
currentZoneData.zoneName = zoneName
currentZoneData.groundZ = actualGroundZ
originalPlayerCoords = GetEntityCoords(ped)
originalPlayerHeading = GetEntityHeading(ped)
local cameraZ = actualGroundZ + 50.0
CreateDebugZone(points, thickness, zoneName, actualGroundZ)
viewerCamera = CreateCam('DEFAULT_SCRIPTED_CAMERA', true)
viewerCamCoords = vector3(centerX, centerY, cameraZ)
viewerCamRot = vector3(-45.0, 0.0, 0.0)
SetCamCoord(viewerCamera, viewerCamCoords.x, viewerCamCoords.y, viewerCamCoords.z)
SetCamRot(viewerCamera, viewerCamRot.x, viewerCamRot.y, viewerCamRot.z, 2)
SetCamFov(viewerCamera, 60.0)
RenderScriptCams(true, true, 500, true, false)
SetEntityVisible(ped, false, false)
SetEntityCollision(ped, false, false)
FreezeEntityPosition(ped, true)
SetEntityCoords(ped, centerX, centerY, actualGroundZ, false, false, false, false)
isViewingZone = true
SetNuiFocus(false, false)
SendNUIMessage({
action = 'zoneViewerStarted',
data = {
groundZ = actualGroundZ,
thickness = thickness
}
})
local moveSpeed = 1.0
local rotSpeed = 3.0
local heightAdjustCooldown = 0
CreateThread(function()
while isViewingZone do
Wait(0)
DisableAllControlActions(0)
local mouseX = GetDisabledControlNormal(0, 1) * rotSpeed
local mouseY = GetDisabledControlNormal(0, 2) * rotSpeed
viewerCamRot = vector3(
math.max(-89.0, math.min(89.0, viewerCamRot.x - mouseY)),
viewerCamRot.y,
viewerCamRot.z - mouseX
)
local radX = math.rad(viewerCamRot.x)
local radZ = math.rad(viewerCamRot.z)
local forward = vector3(
-math.sin(radZ) * math.cos(radX),
math.cos(radZ) * math.cos(radX),
math.sin(radX)
)
local right = vector3(
math.cos(radZ),
math.sin(radZ),
0.0
)
if IsDisabledControlPressed(0, 15) then
moveSpeed = math.min(moveSpeed * 1.1, 10.0)
elseif IsDisabledControlPressed(0, 14) then
moveSpeed = math.max(moveSpeed * 0.9, 0.1)
end
local movement = vector3(0, 0, 0)
if IsDisabledControlPressed(0, 32) then
movement = movement + forward
end
if IsDisabledControlPressed(0, 33) then
movement = movement - forward
end
if IsDisabledControlPressed(0, 34) then
movement = movement - right
end
if IsDisabledControlPressed(0, 35) then
movement = movement + right
end
if IsDisabledControlPressed(0, 44) then
movement = movement - vector3(0, 0, 1)
end
if IsDisabledControlPressed(0, 38) then
movement = movement + vector3(0, 0, 1)
end
if IsDisabledControlPressed(0, 21) then
movement = movement * 3.0
end
viewerCamCoords = viewerCamCoords + (movement * moveSpeed)
SetCamCoord(viewerCamera, viewerCamCoords.x, viewerCamCoords.y, viewerCamCoords.z)
SetCamRot(viewerCamera, viewerCamRot.x, viewerCamRot.y, viewerCamRot.z, 2)
if heightAdjustCooldown <= 0 then
local thicknessChanged = false
if IsDisabledControlPressed(0, 172) then
currentZoneData.thickness = currentZoneData.thickness + 1.0
UpdateDebugZoneThickness()
heightAdjustCooldown = 100
thicknessChanged = true
elseif IsDisabledControlPressed(0, 173) then
if currentZoneData.thickness > 1.0 then
currentZoneData.thickness = currentZoneData.thickness - 1.0
UpdateDebugZoneThickness()
thicknessChanged = true
end
heightAdjustCooldown = 100
end
if thicknessChanged then
SendNUIMessage({
action = 'zoneViewerUpdate',
data = {
groundZ = currentZoneData.groundZ,
thickness = currentZoneData.thickness
}
})
end
else
heightAdjustCooldown = heightAdjustCooldown - GetFrameTime() * 1000
end
if IsDisabledControlJustPressed(0, 177) then
StopZoneViewer()
end
end
end)
end
--- NUI Callback for getting ground Z at a position
RegisterNUICallback('getPointZ', function(data, cb)
local x = tonumber(data.x)
local y = tonumber(data.y)
if not x or not y then
cb({ z = nil, x = nil, y = nil })
return
end
GetGroundZAtPositionAsync(x, y, function(groundZ, actualX, actualY)
cb({
z = groundZ,
x = actualX or x,
y = actualY or y
})
end)
end)
--- NUI Callback for viewing a zone in 3D
RegisterNUICallback('viewZone', function(data, cb)
local points = data.points
local groundZ = data.groundZ or 0
local thickness = data.thickness or 150
local zoneName = data.zoneName or 'preview'
if not points or #points < 3 then
lib.notify({
title = 'Zone Viewer',
description = 'Need at least 3 points to view zone',
type = 'error'
})
cb('error')
return
end
local centerX, centerY = CalculatePolygonCenter(points)
StartZoneViewer(centerX, centerY, points, groundZ, thickness, zoneName)
cb('ok')
end)
--- Event handler for opening the zone creator from server command
RegisterNetEvent('sd-zonecreator:openZoneCreator', function()
OpenZoneCreator()
end)
--- Background thread that sends player position updates to the NUI
CreateThread(function()
while true do
Wait(500)
if isZoneCreatorOpen then
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
SendNUIMessage({
action = 'updatePlayerPosition',
data = {
x = coords.x,
y = coords.y,
z = coords.z
}
})
end
end
end)