forked from overextended/qtarget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
502 lines (439 loc) · 14.1 KB
/
client.lua
File metadata and controls
502 lines (439 loc) · 14.1 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
local screen = {}
---------------------------------------
---Source: https://github.com/citizenfx/lua/blob/luaglm-dev/cfx/libs/scripts/examples/scripting_gta.lua
---Credits to gottfriedleibniz
local glm = require 'glm'
-- Cache common functions
local glm_rad = glm.rad
local glm_quatEuler = glm.quatEulerAngleZYX
local glm_rayPicking = glm.rayPicking
-- Cache direction vectors
local glm_up = glm.up()
local glm_forward = glm.forward()
local function ScreenPositionToCameraRay()
local pos = GetFinalRenderedCamCoord()
local rot = glm_rad(GetFinalRenderedCamRot(2))
local q = glm_quatEuler(rot.z, rot.y, rot.x)
return pos, glm_rayPicking(
q * glm_forward,
q * glm_up,
glm_rad(screen.fov),
screen.ratio,
0.10000, -- GetFinalRenderedCamNearClip(),
10000.0, -- GetFinalRenderedCamFarClip(),
0, 0
)
end
---------------------------------------
local playerPed
---@param flag number
---@param playerCoords vector
---@return number flag
---@return vector coords
---@return number distance
---@return number entity
---@return number entity_type
local function RaycastCamera(flag, playerCoords)
if not playerPed then playerPed = PlayerPedId() end
local rayPos, rayDir = ScreenPositionToCameraRay()
local destination = rayPos + 10000 * rayDir
local rayHandle = StartShapeTestLosProbe(rayPos.x, rayPos.y, rayPos.z, destination.x, destination.y, destination.z, flag or -1, playerPed, 0)
while true do
Wait(0)
local result, _, endCoords, _, entityHit = GetShapeTestResult(rayHandle)
-- todo: add support for materialHash
if result ~= 1 then
local distance = playerCoords and #(playerCoords - endCoords)
return flag, endCoords, distance, entityHit, entityHit and GetEntityType(entityHit) or 0
end
end
end
exports('raycast', RaycastCamera)
local hasFocus = false
local function DisableNUI()
SetNuiFocus(false, false)
SetNuiFocusKeepInput(false)
hasFocus = false
end
local targetActive = false
local function EnableNUI()
if targetActive and not hasFocus then
SetCursorLocation(0.5, 0.5)
SetNuiFocus(true, true)
SetNuiFocusKeepInput(true)
hasFocus = true
end
end
local success = false
local sendData = {}
local sendDistance = {}
local nuiData = {}
local table_wipe = table.wipe
local pairs = pairs
local CheckOptions
local function LeaveTarget()
table_wipe(sendData)
success = false
SendNUIMessage({response = 'leftTarget'})
end
---@param hit number
---@param data table
---@param entity number
---@param distance number
local function CheckEntity(hit, data, entity, distance)
if next(data) then
table_wipe(sendDistance)
table_wipe(nuiData)
local slot = 0
for _, data in pairs(data) do
if CheckOptions(data, entity, distance) then
slot += 1
sendData[slot] = data
sendData[slot].entity = entity
nuiData[slot] = {
icon = data.icon,
label = data.label
}
sendDistance[data.distance] = true
else sendDistance[data.distance] = false end
end
if nuiData[1] then
success = true
SendNUIMessage({response = 'validTarget', data = nuiData})
while targetActive do
local _, _, distance, entity2, _ = RaycastCamera(hit, GetEntityCoords(playerPed))
if entity ~= entity2 then
if hasFocus then DisableNUI() end
break
elseif not hasFocus and IsDisabledControlPressed(0, 24) then
EnableNUI()
else
for k, v in pairs(sendDistance) do
if (v == false and distance < k) or (v == true and distance > k) then
return CheckEntity(hit, data, entity, distance)
end
end
end
Wait(20)
end
end
LeaveTarget()
end
end
local Bones = Load('bones')
---@param coords vector
---@param entity number
---@param bonelist table
---@return boolean | number
---@return number?
---@return string?
local function CheckBones(coords, entity, bonelist)
local closestBone = -1
local closestDistance = 20
local closestPos, closestBoneName
for _, v in pairs(bonelist) do
if Bones[v] then
local boneId = GetEntityBoneIndexByName(entity, v)
local bonePos = GetWorldPositionOfEntityBone(entity, boneId)
local distance = #(coords - bonePos)
if closestBone == -1 or distance < closestDistance then
closestBone, closestDistance, closestPos, closestBoneName = boneId, distance, bonePos, v
end
end
end
if closestBone ~= -1 then return closestBone, closestPos, closestBoneName
else return false end
end
local Types = {{}, {}, {}}
local Players = {}
local Entities = {}
local Models = {}
local Zones = {}
local function EnableTarget()
if success or not IsControlEnabled(0, 24) or IsNuiFocused() then return end
if not CheckOptions then CheckOptions = _ENV.CheckOptions end
if not targetActive and CheckOptions then
targetActive = true
SendNUIMessage({response = 'openTarget'})
CreateThread(function()
local playerId = PlayerId()
repeat
if hasFocus then
DisableControlAction(0, 1, true)
DisableControlAction(0, 2, true)
end
DisablePlayerFiring(playerId, true)
DisableControlAction(0, 25, true)
DisableControlAction(0, 37, true)
DisableControlAction(0, 142, true)
Wait(0)
until targetActive == false
end)
playerPed = PlayerPedId()
screen.ratio = GetAspectRatio(true)
screen.fov = GetFinalRenderedCamFov()
local curFlag = 30
while targetActive do
local sleep = 0
local hit, coords, distance, entity, entityType = RaycastCamera(curFlag, GetEntityCoords(playerPed))
if curFlag == 30 then curFlag = -1 else curFlag = 30 end
if distance <= Config.MaxDistance then
if entityType > 0 then
-- Owned entity targets
if NetworkGetEntityIsNetworked(entity) then
local data = Entities[NetworkGetNetworkIdFromEntity(entity)]
if data then
CheckEntity(hit, data, entity, distance)
end
end
-- Player targets
if entityType == 1 and IsPedAPlayer(entity) then
CheckEntity(hit, Players, entity, distance)
-- Vehicle bones
elseif entityType == 2 and distance <= 1.1 then
local closestBone, closestPos, closestBoneName = CheckBones(coords, entity, Bones.Vehicle)
local data = Bones[closestBoneName]
if next(data) then
if closestBone and #(coords - closestPos) <= data.distance then
table_wipe(nuiData)
local slot = 0
for _, data in pairs(data.options) do
if CheckOptions(data, entity) then
slot += 1
sendData[slot] = data
sendData[slot].entity = entity
nuiData[slot] = {
icon = data.icon,
label = data.label
}
end
end
if nuiData[1] then
success = true
SendNUIMessage({response = 'validTarget', data = nuiData})
while targetActive do
local _, coords, distance, entity2 = RaycastCamera(hit, GetEntityCoords(playerPed))
if hit and entity == entity2 then
local closestBone2, closestPos2 = CheckBones(coords, entity, Bones.Vehicle)
if closestBone ~= closestBone2 or #(coords - closestPos2) > data.distance or distance > 1.1 then
if hasFocus then DisableNUI() end
break
elseif not hasFocus and IsDisabledControlPressed(0, 24) then EnableNUI() end
else
if hasFocus then DisableNUI() end
break
end
Wait(20)
end
end
end
end
-- Entity targets
else
local data = Models[GetEntityModel(entity)]
if data then CheckEntity(hit, data, entity, distance) end
end
-- Generic targets
if not success then
local data = Types[entityType]
if data then CheckEntity(hit, data, entity, distance) end
end
else sleep += 20 end
if not success then
local closestDis, closestZone
for _, zone in pairs(Zones) do
if distance < (closestDis or Config.MaxDistance) and distance <= zone.targetoptions.distance and zone:isPointInside(coords) then
closestDis = distance
closestZone = zone
end
end
if closestZone then
table_wipe(nuiData)
local slot = 0
for _, data in pairs(closestZone.targetoptions.options) do
if CheckOptions(data, entity, distance) then
slot += 1
sendData[slot] = data
sendData[slot].entity = entity
nuiData[slot] = {
icon = data.icon,
label = data.label
}
end
end
if nuiData[1] then
success = true
SendNUIMessage({response = 'validTarget', data = nuiData})
while targetActive do
local _, coords, distance, _, _ = RaycastCamera(hit, GetEntityCoords(playerPed))
if not closestZone:isPointInside(coords) or distance > closestZone.targetoptions.distance then
if hasFocus then DisableNUI() end
break
elseif not hasFocus and IsDisabledControlPressed(0, 24) then
EnableNUI()
end
Wait(20)
end
LeaveTarget()
else
repeat
Wait(20)
local _, coords, _, entity2 = RaycastCamera(hit)
until not targetActive or entity ~= entity2 or not closestZone:isPointInside(coords)
end
else sleep += 20 end
else LeaveTarget() end
else sleep += 20 end
Wait(sleep)
end
hasFocus = false
SendNUIMessage({response = 'closeTarget'})
end
end
local function DisableTarget()
if targetActive then
SetNuiFocus(false, false)
SetNuiFocusKeepInput(false)
targetActive = false
end
end
RegisterNUICallback('selectTarget', function(option)
hasFocus = false
local data = sendData[option]
CreateThread(function()
Wait(0)
if data.action ~= nil then
data.action(data.entity)
else
TriggerEvent(data.event, data)
end
end)
end)
RegisterNUICallback('closeTarget', function()
hasFocus = false
end)
RegisterKeyMapping('+playerTarget', 'Enable targeting~', 'keyboard', 'LMENU')
RegisterCommand('+playerTarget', EnableTarget, false)
RegisterCommand('-playerTarget', DisableTarget, false)
TriggerEvent('chat:removeSuggestion', '/+playerTarget')
TriggerEvent('chat:removeSuggestion', '/-playerTarget')
-------------------------------------------------------------------------------
-- Exports
-------------------------------------------------------------------------------
local function AddCircleZone(name, center, radius, options, targetoptions)
Zones[name] = CircleZone:Create(center, radius, options)
Zones[name].targetoptions = targetoptions
end
exports('AddCircleZone', AddCircleZone)
local function AddBoxZone(name, center, length, width, options, targetoptions)
Zones[name] = BoxZone:Create(center, length, width, options)
Zones[name].targetoptions = targetoptions
end
exports('AddBoxZone', AddBoxZone)
local function AddPolyzone(name, points, options, targetoptions)
Zones[name] = PolyZone:Create(points, options)
Zones[name].targetoptions = targetoptions
end
exports('AddPolyzone', AddPolyzone)
local function AddTargetBone(bones, parameters)
for _, bone in pairs(bones) do
Bones[bone] = parameters
end
end
exports('AddTargetBone', AddTargetBone)
local function SetOptions(table, distance, options)
for _, v in pairs(options) do
if v.required_item then
v.item = v.required_item
v.required_item = nil
end
if not v.distance or v.distance > distance then v.distance = distance end
table[v.label] = v
end
end
local function AddTargetEntity(entity, parameters)
entity = NetworkGetEntityIsNetworked(entity) and NetworkGetNetworkIdFromEntity(entity) or false
if entity then
local distance, options = parameters.distance or Config.MaxDistance, parameters.options
if not Entities[entity] then Entities[entity] = {} end
SetOptions(Entities[entity], distance, options)
end
end
exports('AddTargetEntity', AddTargetEntity)
local function AddEntityZone(name, entity, options, targetoptions)
Zones[name] = EntityZone:Create(entity, options)
Zones[name].targetoptions = targetoptions
end
exports('AddEntityZone', AddEntityZone)
local function AddTargetModel(models, parameters)
local distance, options = parameters.distance or Config.MaxDistance, parameters.options
for _, model in pairs(models) do
if type(model) == 'string' then model = joaat(model) end
if not Models[model] then Models[model] = {} end
SetOptions(Models[model], distance, options)
end
end
exports('AddTargetModel', AddTargetModel)
local function RemoveZone(name)
if not Zones[name] then return end
if Zones[name].destroy then
Zones[name]:destroy()
end
Zones[name] = nil
end
exports('RemoveZone', RemoveZone)
local function RemoveTargetModel(models, labels)
for _, model in pairs(models) do
if type(model) == 'string' then model = joaat(model) end
for _, v in pairs(labels) do
if Models[model] then
Models[model][v] = nil
end
end
end
end
exports('RemoveTargetModel', RemoveTargetModel)
local function RemoveTargetEntity(entity, labels)
entity = NetworkGetEntityIsNetworked(entity) and NetworkGetNetworkIdFromEntity(entity) or false
if entity then
for _, v in pairs(labels) do
if Entities[entity] then
Entities[entity][v] = nil
end
end
end
end
exports('RemoveTargetEntity', RemoveTargetEntity)
local function AddType(type, parameters)
local distance, options = parameters.distance or Config.MaxDistance, parameters.options
SetOptions(Types[type], distance, options)
end
local function AddPed(parameters) AddType(1, parameters) end
exports('Ped', AddPed)
local function AddVehicle(parameters) AddType(2, parameters) end
exports('Vehicle', AddVehicle)
local function AddObject(parameters) AddType(3, parameters) end
exports('Object', AddObject)
local function AddPlayer(parameters)
local distance, options = parameters.distance or Config.MaxDistance, parameters.options
SetOptions(Players, distance, options)
end
exports('Player', AddPlayer)
local function RemoveType(type, labels)
for _, v in pairs(labels) do
Types[type][v] = nil
end
end
local function RemovePed(labels) RemoveType(1, labels) end
exports('RemovePed', RemovePed)
local function RemoveVehicle(labels) RemoveType(2, labels) end
exports('RemoveVehicle', RemoveVehicle)
local function RemoveObject(labels) RemoveType(3, labels) end
exports('RemoveObject', RemoveObject)
local function RemovePlayer(labels)
for _, v in pairs(labels) do
Players[v] = nil
end
end
exports('RemovePlayer', RemovePlayer)
if Config.Debug then Load('debug') end