Skip to content

Commit

Permalink
fix(client): improve flag-switching and check endCoords distance
Browse files Browse the repository at this point in the history
This should resolve the entity raycast taking priority over the world
raycast, i.e. targeting LS Customs workbenches would frequently target
an entity beneath instead. An issue for entities intersecting zones has
also been resolved which could hide all options.
  • Loading branch information
thelindat committed Nov 9, 2022
1 parent 92a0c7b commit ea4b6b7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion client/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function GetEntityOptions(entity, _type, model)
global = Peds
elseif _type == 2 then
global = Vehicles
elseif _type == 3 then
else
global = Objects
end

Expand Down
56 changes: 32 additions & 24 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local GetEntityOptions = GetEntityOptions
local IsDisabledControlJustPressed = IsDisabledControlJustPressed
local DisableControlAction = DisableControlAction
local DisablePlayerFiring = DisablePlayerFiring
local options
local options = {}
local currentTarget = {}

-- Toggle ox_target, instead of holding the hotkey
Expand All @@ -47,27 +47,45 @@ local function enableTargeting()
SendNuiMessage('{"event": "visible", "state": true}')

isActive = true
local flag, hit, entityHit, endCoords, distance, currentZone, nearbyZones, lastEntity, entityType, entityModel, hasTick
local flag, hit, entityHit, endCoords, distance, currentZone, nearbyZones, lastEntity, entityType, entityModel, hasTick = 1
local getNearbyZones, drawSprites = DrawSprites()

while isActive do
local playerCoords = GetEntityCoords(cache.ped)
hit, entityHit, endCoords = RaycastFromCamera(26)
hit, entityHit, endCoords = RaycastFromCamera(flag)
entityType = entityHit ~= 0 and GetEntityType(entityHit) or 0
distance = #(playerCoords - endCoords)

if entityType == 0 then
hit, entityHit, endCoords = RaycastFromCamera(1)
flag, entityType = 1, entityHit ~= 0 and GetEntityType(entityHit) or 0
else
flag = 26
local _flag = flag == 1 and 26 or 1
local _hit, _entityHit, _endCoords = RaycastFromCamera(_flag)
local _distance = #(playerCoords - _endCoords)

if _distance < distance then
flag, hit, entityHit, endCoords, distance = _flag, _hit, _entityHit, _endCoords, _distance
entityType = entityHit ~= 0 and GetEntityType(entityHit) or 0
end
end

distance = #(playerCoords - endCoords)

if hit and distance < 7 then
local newOptions
local lastZone = currentZone

if getNearbyZones then
---@type CZone[]?, CZone?
nearbyZones, currentZone = getNearbyZones(endCoords)
else
---@type CZone?
currentZone = GetCurrentZone(endCoords)
end

if lastZone ~= currentZone or entityHit ~= lastEntity then
if next(options) then
table.wipe(options)
SendNuiMessage('{"event": "leftTarget"}')
end

if lastEntity ~= entityHit then
if flag ~= 1 then
entityHit = HasEntityClearLosToEntity(entityHit, cache.ped, 7) and entityHit or 0
end
Expand All @@ -76,14 +94,8 @@ local function enableTargeting()
local success, result = pcall(GetEntityModel, entityHit)
entityModel = success and result

if entityType == 0 and entityModel then
entityType = 3
else SendNuiMessage('{"event": "leftTarget"}') end

if entityModel then
newOptions = GetEntityOptions(entityHit, entityType, entityModel)
elseif options then
table.wipe(options)
end

if Debug then
Expand All @@ -98,14 +110,6 @@ local function enableTargeting()
end
end

if getNearbyZones then
---@type CZone[]?, CZone?
nearbyZones, currentZone = getNearbyZones(endCoords, currentZone)
else
---@type CZone?
currentZone = GetCurrentZone(endCoords, currentZone)
end

options = newOptions or options or {}

if currentZone then
Expand Down Expand Up @@ -254,6 +258,10 @@ local function enableTargeting()
end)
end

if not next(options) then
flag = flag == 1 and 26 or 1
end

Wait(60)
end

Expand All @@ -264,7 +272,7 @@ local function enableTargeting()
setNuiFocus(false)
SendNuiMessage('{"event": "visible", "state": false}')
table.wipe(currentTarget)
options = nil
table.wipe(options)
end

local function disableTargeting()
Expand Down
7 changes: 1 addition & 6 deletions client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ if GetConvarInt('ox_target:drawSprite', 1) == 1 then
lib.requestStreamedTextureDict(dict)

---@param coords vector3
---@param currentZone CZone
---@return CZone[] | false | nil, CZone?
return function(coords, currentZone)
return function(coords)
if Zones then
inRange = {}
local n = 0
Expand All @@ -74,10 +73,6 @@ if GetConvarInt('ox_target:drawSprite', 1) == 1 then
end
end

if not newZone and currentZone then
SendNuiMessage('{"event": "leftTarget"}')
end

return n > 0 and inRange, newZone
end
end, function()
Expand Down

0 comments on commit ea4b6b7

Please sign in to comment.