-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathmain.lua
456 lines (361 loc) · 14 KB
/
main.lua
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
if not lib.checkDependency('ox_lib', '3.21.0', true) then return end
lib.locale()
local utils = require 'client.utils'
local state = require 'client.state'
local options = require 'client.api'.getTargetOptions()
require 'client.debug'
require 'client.defaults'
require 'client.compat.qtarget'
local SendNuiMessage = SendNuiMessage
local GetEntityCoords = GetEntityCoords
local GetEntityType = GetEntityType
local HasEntityClearLosToEntity = HasEntityClearLosToEntity
local GetEntityBoneIndexByName = GetEntityBoneIndexByName
local GetEntityBonePosition_2 = GetEntityBonePosition_2
local GetEntityModel = GetEntityModel
local IsDisabledControlJustPressed = IsDisabledControlJustPressed
local DisableControlAction = DisableControlAction
local DisablePlayerFiring = DisablePlayerFiring
local GetModelDimensions = GetModelDimensions
local GetOffsetFromEntityInWorldCoords = GetOffsetFromEntityInWorldCoords
local currentTarget = {}
local currentMenu
local menuChanged
local menuHistory = {}
local nearbyZones
-- Toggle ox_target, instead of holding the hotkey
local toggleHotkey = GetConvarInt('ox_target:toggleHotkey', 0) == 1
local mouseButton = GetConvarInt('ox_target:leftClick', 1) == 1 and 24 or 25
local debug = GetConvarInt('ox_target:debug', 0) == 1
local vec0 = vec3(0, 0, 0)
---@param option OxTargetOption
---@param distance number
---@param endCoords vector3
---@param entityHit? number
---@param entityType? number
---@param entityModel? number | false
local function shouldHide(option, distance, endCoords, entityHit, entityType, entityModel)
if option.menuName ~= currentMenu then
return true
end
if distance > (option.distance or 7) then
return true
end
if option.groups and not utils.hasPlayerGotGroup(option.groups) then
return true
end
if option.items and not utils.hasPlayerGotItems(option.items, option.anyItem) then
return true
end
local bone = entityModel and option.bones or nil
if bone then
---@cast entityHit number
---@cast entityType number
---@cast entityModel number
local _type = type(bone)
if _type == 'string' then
local boneId = GetEntityBoneIndexByName(entityHit, bone)
if boneId ~= -1 and #(endCoords - GetEntityBonePosition_2(entityHit, boneId)) <= 2 then
bone = boneId
else
return true
end
elseif _type == 'table' then
local closestBone, boneDistance
for j = 1, #bone do
local boneId = GetEntityBoneIndexByName(entityHit, bone[j])
if boneId ~= -1 then
local dist = #(endCoords - GetEntityBonePosition_2(entityHit, boneId))
if dist <= (boneDistance or 1) then
closestBone = boneId
boneDistance = dist
end
end
end
if closestBone then
bone = closestBone
else
return true
end
end
end
local offset = entityModel and option.offset or nil
if offset then
---@cast entityHit number
---@cast entityType number
---@cast entityModel number
if not option.absoluteOffset then
local min, max = GetModelDimensions(entityModel)
offset = (max - min) * offset + min
end
offset = GetOffsetFromEntityInWorldCoords(entityHit, offset.x, offset.y, offset.z)
if #(endCoords - offset) > (option.offsetSize or 1) then
return true
end
end
if option.canInteract then
local success, resp = pcall(option.canInteract, entityHit, distance, endCoords, option.name, bone)
return not success or not resp
end
end
local function startTargeting()
if state.isDisabled() or state.isActive() or IsNuiFocused() or IsPauseMenuActive() then return end
state.setActive(true)
local flag = 511
local hit, entityHit, endCoords, distance, lastEntity, entityType, entityModel, hasTarget, zonesChanged
local zones = {}
CreateThread(function()
local dict, texture = utils.getTexture()
local lastCoords
while state.isActive() do
lastCoords = endCoords == vec0 and lastCoords or endCoords or vec0
if debug then
DrawMarker(28, lastCoords.x, lastCoords.y, lastCoords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.2,
0.2,
---@diagnostic disable-next-line: param-type-mismatch
255, 42, 24, 100, false, false, 0, true, false, false, false)
end
utils.drawZoneSprites(dict, texture)
DisablePlayerFiring(cache.playerId, true)
DisableControlAction(0, 25, true)
DisableControlAction(0, 140, true)
DisableControlAction(0, 141, true)
DisableControlAction(0, 142, true)
if state.isNuiFocused() then
DisableControlAction(0, 1, true)
DisableControlAction(0, 2, true)
if not hasTarget or options and IsDisabledControlJustPressed(0, 25) then
state.setNuiFocus(false, false)
end
elseif hasTarget and IsDisabledControlJustPressed(0, mouseButton) then
state.setNuiFocus(true, true)
end
Wait(0)
end
SetStreamedTextureDictAsNoLongerNeeded(dict)
end)
while state.isActive() do
if not state.isNuiFocused() and lib.progressActive() then
state.setActive(false)
break
end
local playerCoords = GetEntityCoords(cache.ped)
hit, entityHit, endCoords = lib.raycast.fromCamera(flag, 4, 20)
distance = #(playerCoords - endCoords)
if entityHit ~= 0 and entityHit ~= lastEntity then
local success, result = pcall(GetEntityType, entityHit)
entityType = success and result or 0
end
if entityType == 0 then
local _flag = flag == 511 and 26 or 511
local _hit, _entityHit, _endCoords = lib.raycast.fromCamera(_flag, 4, 20)
local _distance = #(playerCoords - _endCoords)
if _distance < distance then
flag, hit, entityHit, endCoords, distance = _flag, _hit, _entityHit, _endCoords, _distance
if entityHit ~= 0 then
local success, result = pcall(GetEntityType, entityHit)
entityType = success and result or 0
end
end
end
nearbyZones, zonesChanged = utils.getNearbyZones(endCoords)
local entityChanged = entityHit ~= lastEntity
local newOptions = (zonesChanged or entityChanged or menuChanged) and true
if entityHit > 0 and entityChanged then
currentMenu = nil
if flag ~= 511 then
entityHit = HasEntityClearLosToEntity(entityHit, cache.ped, 7) and entityHit or 0
end
if lastEntity ~= entityHit and debug then
if lastEntity then
SetEntityDrawOutline(lastEntity, false)
end
if entityType ~= 1 then
SetEntityDrawOutline(entityHit, true)
end
end
if entityHit > 0 then
local success, result = pcall(GetEntityModel, entityHit)
entityModel = success and result
end
end
if hasTarget and (zonesChanged or entityChanged and hasTarget > 1) then
SendNuiMessage('{"event": "leftTarget"}')
if entityChanged then options:wipe() end
if debug and lastEntity > 0 then SetEntityDrawOutline(lastEntity, false) end
hasTarget = false
end
if newOptions and entityModel and entityHit > 0 then
options:set(entityHit, entityType, entityModel)
end
lastEntity = entityHit
currentTarget.entity = entityHit
currentTarget.coords = endCoords
currentTarget.distance = distance
local hidden = 0
local totalOptions = 0
for k, v in pairs(options) do
local optionCount = #v
local dist = k == '__global' and 0 or distance
totalOptions += optionCount
for i = 1, optionCount do
local option = v[i]
local hide = shouldHide(option, dist, endCoords, entityHit, entityType, entityModel)
if option.hide ~= hide then
option.hide = hide
newOptions = true
end
if hide then hidden += 1 end
end
end
if zonesChanged then table.wipe(zones) end
for i = 1, #nearbyZones do
local zoneOptions = nearbyZones[i].options
local optionCount = #zoneOptions
totalOptions += optionCount
zones[i] = zoneOptions
for j = 1, optionCount do
local option = zoneOptions[j]
local hide = shouldHide(option, distance, endCoords, entityHit)
if option.hide ~= hide then
option.hide = hide
newOptions = true
end
if hide then hidden += 1 end
end
end
if newOptions then
if hasTarget == 1 and options.size > 1 then
hasTarget = true
end
if hasTarget and hidden == totalOptions then
if hasTarget and hasTarget ~= 1 then
hasTarget = false
SendNuiMessage('{"event": "leftTarget"}')
end
elseif menuChanged or hasTarget ~= 1 and hidden ~= totalOptions then
hasTarget = options.size
if currentMenu and options.__global[1]?.name ~= 'builtin:goback' then
table.insert(options.__global, 1,
{
icon = 'fa-solid fa-circle-chevron-left',
label = locale('go_back'),
name = 'builtin:goback',
menuName = currentMenu,
openMenu = 'home'
})
end
SendNuiMessage(json.encode({
event = 'setTarget',
options = options,
zones = zones,
}, { sort_keys = true }))
end
menuChanged = false
end
if toggleHotkey and IsPauseMenuActive() then
state.setActive(false)
end
if not hasTarget or hasTarget == 1 then
flag = flag == 511 and 26 or 511
end
Wait(hit and 50 or 100)
end
if lastEntity and debug then
SetEntityDrawOutline(lastEntity, false)
end
state.setNuiFocus(false)
SendNuiMessage('{"event": "visible", "state": false}')
table.wipe(currentTarget)
options:wipe()
if nearbyZones then table.wipe(nearbyZones) end
end
do
---@type KeybindProps
local keybind = {
name = 'ox_target',
defaultKey = GetConvar('ox_target:defaultHotkey', 'LMENU'),
defaultMapper = 'keyboard',
description = locale('toggle_targeting'),
}
if toggleHotkey then
function keybind:onPressed()
if state.isActive() then
return state.setActive(false)
end
return startTargeting()
end
else
keybind.onPressed = startTargeting
function keybind:onReleased()
state.setActive(false)
end
end
lib.addKeybind(keybind)
end
---@generic T
---@param option T
---@param server? boolean
---@return T
local function getResponse(option, server)
local response = table.clone(option)
response.entity = currentTarget.entity
response.zone = currentTarget.zone
response.coords = currentTarget.coords
response.distance = currentTarget.distance
if server then
response.entity = response.entity ~= 0 and NetworkGetEntityIsNetworked(response.entity) and
NetworkGetNetworkIdFromEntity(response.entity) or 0
end
response.icon = nil
response.groups = nil
response.items = nil
response.canInteract = nil
response.onSelect = nil
response.export = nil
response.event = nil
response.serverEvent = nil
response.command = nil
return response
end
RegisterNUICallback('select', function(data, cb)
cb(1)
local zone = data[3] and nearbyZones[data[3]]
---@type OxTargetOption?
local option = zone and zone.options[data[2]] or options[data[1]][data[2]]
if option then
if option.openMenu then
local menuDepth = #menuHistory
if option.name == 'builtin:goback' then
option.menuName = option.openMenu
option.openMenu = menuHistory[menuDepth]
if menuDepth > 0 then
menuHistory[menuDepth] = nil
end
else
menuHistory[menuDepth + 1] = currentMenu
end
menuChanged = true
currentMenu = option.openMenu ~= 'home' and option.openMenu or nil
options:wipe()
else
state.setNuiFocus(false)
end
currentTarget.zone = zone?.id
if option.onSelect then
option.onSelect(option.qtarget and currentTarget.entity or getResponse(option))
elseif option.export then
exports[option.resource or zone.resource][option.export](nil, getResponse(option))
elseif option.event then
TriggerEvent(option.event, getResponse(option))
elseif option.serverEvent then
TriggerServerEvent(option.serverEvent, getResponse(option, true))
elseif option.command then
ExecuteCommand(option.command)
end
if option.menuName == 'home' then return end
end
if not option?.openMenu and IsNuiFocused() then
state.setActive(false)
end
end)