Files

230 lines
7.4 KiB
Lua
Raw Permalink Normal View History

---@diagnostic disable: duplicate-set-field
2025-03-04 22:28:34 -05:00
local resourceName = "qb-target"
2025-05-18 23:45:37 -04:00
if GetResourceState(resourceName) == 'missing' then return end
if GetResourceState("ox_target") ~= 'missing' then return end
2025-01-08 06:33:31 -05:00
2025-02-24 11:07:52 -05:00
Target = Target or {}
local targetDebug = BridgeSharedConfig and BridgeSharedConfig.DebugLevel == 2 or false
local targetZones = {}
2025-04-08 19:27:34 -04:00
local qb_target = exports['qb-target']
2025-01-08 06:33:31 -05:00
function Target.GetResourceName()
return "qb-target"
end
local function getLargestDistance(data)
2025-08-21 02:27:38 -05:00
local largestDistance = -1
for _, v in pairs(data) do
if v.distance and v.distance > largestDistance then
largestDistance = v.distance
end
end
return largestDistance ~= -1 and largestDistance or 2.0
end
2025-04-08 19:27:34 -04:00
---This is an internal function that is used to fix the options passed to fit alternative target systems, for example qb-ox or ox-qb etc.
---@param options table
---@return table
function Target.FixOptions(options)
2025-01-08 06:33:31 -05:00
for k, v in pairs(options) do
2025-02-05 17:42:50 -06:00
local action = v.onSelect or v.action
2025-07-29 05:22:55 -05:00
local select = action and function(entityOrData)
2025-02-05 17:42:50 -06:00
if type(entityOrData) == 'table' then
return action(entityOrData.entity)
end
2025-04-08 19:27:34 -04:00
return action(entityOrData)
2025-02-05 17:42:50 -06:00
end
2025-07-30 13:28:20 -04:00
if v.serverEvent then
2025-07-29 05:22:55 -05:00
v.type = "server"
v.event = v.serverEvent
elseif v.event then
v.type = "client"
v.event = v.event
end
2025-02-07 22:38:15 -06:00
options[k].action = select
2025-02-26 12:55:16 -06:00
options[k].job = v.job or v.groups
2025-07-29 05:22:55 -05:00
options[k].jobType = v.jobType
local optionsCanInteract = v.canInteract
if optionsCanInteract then
2025-08-20 22:23:07 -05:00
local id = Target.CreateCanInteract(optionsCanInteract)
v.canInteract = function(...)
return Target.CanInteract(id, ...)
end
end
2025-01-08 06:33:31 -05:00
end
2025-02-05 17:42:50 -06:00
return options
end
2025-04-08 19:27:34 -04:00
---This will toggle the targeting system on or off. This is useful for when you want to disable the targeting system for a specific player entirely.
---@param bool boolean
function Target.DisableTargeting(bool)
2025-04-08 19:27:34 -04:00
qb_target:AllowTargeting(not bool)
end
2025-04-08 19:27:34 -04:00
---This will add target options to players.
---@param options table
function Target.AddGlobalPlayer(options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-04-08 19:27:34 -04:00
qb_target:AddGlobalPlayer({
2025-01-25 17:59:19 -05:00
options = options,
distance = getLargestDistance(options)
2025-01-25 17:59:19 -05:00
})
end
2025-04-08 19:27:34 -04:00
---This will remove target options from all players.
function Target.RemoveGlobalPlayer()
2025-04-08 19:27:34 -04:00
qb_target:RemoveGlobalPlayer()
end
2025-01-25 17:59:19 -05:00
---This will add target options to all specified models. This is useful for when you want to add target options to all models of a specific type.
---@param options table
function Target.AddGlobalPed(options)
options = Target.FixOptions(options)
2025-07-15 22:28:30 -04:00
qb_target:AddGlobalPed({
options = options,
distance = getLargestDistance(options)
2025-07-15 22:28:30 -04:00
})
end
---This will remove target options from all peds. This is useful for when you want to remove target options from all peds.
---@param options any
function Target.RemoveGlobalPed(options)
qb_target:RemoveGlobalPed(options)
end
2025-04-08 19:27:34 -04:00
---This will add taget options to all vehicles.
---@param options table
function Target.AddGlobalVehicle(options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-04-08 19:27:34 -04:00
qb_target:AddGlobalVehicle({
2025-01-08 06:33:31 -05:00
options = options,
distance = getLargestDistance(options)
2025-01-08 06:33:31 -05:00
})
end
---This will add a networked entity to the target system.
---@param netids table | number
---@param options table
function Target.AddNetworkedEntity(netids, options)
options = Target.FixOptions(options)
qb_target:AddTargetEntity(netids, options)
end
---This will remove a networked entity from the target system.
---@param netids table | number
---@param optionNames string
function Target.RemoveNetworkedEntity(netids, optionNames)
qb_target:RemoveTargetEntity(netids, optionNames)
end
2025-06-02 22:19:42 -05:00
---This will remove target options from all vehicles.
2025-04-08 19:27:34 -04:00
---@param options table
function Target.RemoveGlobalVehicle(options)
2025-02-20 14:50:27 -05:00
local assembledLables = {}
for k, v in pairs(options) do
table.insert(assembledLables, v.label)
end
2025-04-08 19:27:34 -04:00
qb_target:RemoveGlobalVehicle(assembledLables)
2025-02-20 14:50:27 -05:00
end
2025-04-08 19:27:34 -04:00
---This will generate targets on non networked entites with the passed options.
---@param entities number | table
---@param options table
function Target.AddLocalEntity(entities, options)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-04-08 19:27:34 -04:00
qb_target:AddTargetEntity(entities, {
2025-01-08 06:33:31 -05:00
options = options,
distance = getLargestDistance(options)
2025-01-08 06:33:31 -05:00
})
end
2025-04-08 19:27:34 -04:00
---This will remove the target options from a local entity. This is useful for when you want to remove target options from a specific entity.
---@param entity number | table
---@param labels string | table | nil
function Target.RemoveLocalEntity(entity, labels)
qb_target:RemoveTargetEntity(entity, labels)
2025-04-08 19:27:34 -04:00
end
---This will add target options to all specified models. This is useful for when you want to add target options to all models of a specific type.
---@param models number | table
---@param options table
function Target.AddModel(models, options, distance)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-04-08 19:27:34 -04:00
qb_target:AddTargetModel(models, {
2025-01-08 06:33:31 -05:00
options = options,
distance = getLargestDistance(options),
2025-01-08 06:33:31 -05:00
})
end
2025-04-08 19:27:34 -04:00
---This will remove target options from all specified models.
---@param model number
function Target.RemoveModel(model)
2025-04-08 19:27:34 -04:00
qb_target:RemoveTargetModel(model)
end
---This will add a box zone to the target system. This is useful for when you want to add target options to a specific area.
---@param name string
---@param coords table
---@param size table
---@param heading number
---@param options table
function Target.AddBoxZone(name, coords, size, heading, options, debug)
2025-02-05 17:42:50 -06:00
options = Target.FixOptions(options)
2025-06-04 15:29:21 -05:00
if not next(options) then return end
2025-04-08 19:27:34 -04:00
qb_target:AddBoxZone(name, coords, size.x, size.y, {
2025-01-08 06:33:31 -05:00
name = name,
2025-06-02 22:19:42 -05:00
debugPoly = debug or targetDebug,
2025-01-08 06:33:31 -05:00
heading = heading,
2025-06-02 22:19:42 -05:00
minZ = coords.z - (size.z * 0.5),
maxZ = coords.z + (size.z * 0.5),
2025-01-08 06:33:31 -05:00
}, {
options = options,
distance = getLargestDistance(options),
2025-01-08 06:33:31 -05:00
})
2026-08-14 17:38:09 -05:00
table.insert(targetZones, { name = name, creator = GetInvokingResource() or GetCurrentResourceName() })
return name
2025-01-08 06:33:31 -05:00
end
2025-04-08 19:27:34 -04:00
---This will add a circle zone to the target system. This is useful for when you want to add target options to a specific area.
---@param name string
---@param coords table
---@param radius number
---@param options table
function Target.AddSphereZone(name, coords, radius, options, debug)
2025-04-05 20:58:13 +13:00
options = Target.FixOptions(options)
2025-04-08 19:27:34 -04:00
qb_target:AddCircleZone(name, coords, radius, {
2025-04-05 20:58:13 +13:00
name = name,
2025-07-28 00:10:51 -05:00
debugPoly = targetDebug or debug,
2025-04-05 20:58:13 +13:00
}, {
options = options,
distance = getLargestDistance(options),
2025-04-05 20:58:13 +13:00
})
2026-08-14 17:38:09 -05:00
table.insert(targetZones, { name = name, creator = GetInvokingResource() or GetCurrentResourceName() })
return name
2025-04-05 20:58:13 +13:00
end
2025-04-08 19:27:34 -04:00
---This will remove target options from a specific zone.
---@param name string
function Target.RemoveZone(name)
2025-04-08 19:27:34 -04:00
if not name then return end
2025-01-08 06:33:31 -05:00
for _, data in pairs(targetZones) do
if data.name == name then
2025-04-08 19:27:34 -04:00
qb_target:RemoveZone(name)
2025-01-08 06:33:31 -05:00
table.remove(targetZones, _)
break
end
end
end
AddEventHandler('onResourceStop', function(resource)
2025-04-08 19:27:34 -04:00
if resource ~= GetCurrentResourceName() then return end
for _, target in pairs(targetZones) do
if target.creator == resource then
qb_target:RemoveZone(target.name)
2025-01-08 06:33:31 -05:00
end
end
2025-04-08 19:27:34 -04:00
targetZones = {}
end)
return Target