Skip to content
This repository was archived by the owner on Sep 24, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 15 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,26 @@ Icons: https://fontawesome.com/

Here a simple target tracking script that tracks where your player is looking at. Coords and models can be used. You can add multiple payphone models for example and when your player looks at it. It activates the UI to trigger an event. Polyzones can be used also. Its uses 0.00 ms (0.16% CPU Time) when idle. This can be used in multiple scripts to help with optimisation. Press ALT to activate. Using RegisterKeyMapping removes the need of checking if key has been pressed in a thread and players can customise the keybind in the ESCAPE menu. You can also create multiple options per target. Read the example to learn how to use it.

Example:
**PhilsBadMan**

```lua
Citizen.CreateThread(function()
local peds = {
`a_f_m_bevhills_02`,
}
AddTargetModel(peds, {
options = {
{
event = "Random 1event",
icon = "fas fa-dumpster",
label = "Random 1",
},
{
event = "Random 2event",
icon = "fas fa-dumpster",
label = "Random 2",
},
{
event = "Random 3event",
icon = "fas fa-dumpster",
label = "Random 3",
},
{
event = "Random 4event",
icon = "fas fa-dumpster",
label = "Random 4",
},
},
job = {"garbage"}
distance = 2.5
})
Edit to allow passing of arguments in events.
In this example I pass the model and object with the event

local coffee = {
690372739,
}
AddTargetModel(coffee, {
options = {
{
event = "coffeeevent",
icon = "fas fa-coffee",
label = "Coffee",
},
},
job = {"all"}
distance = 2.5
})

AddBoxZone("PoliceDuty", vector3(441.79, -982.07, 30.69), 0.4, 0.6, {
name="PoliceDuty",
heading=91,
debugPoly=false,
minZ=30.79,
maxZ=30.99
}, {
options = {
To Trigger:

```lua
options = {
{
event = "signon",
icon = "far fa-clipboard",
label = "Sign On",
event = "banking:atm",
icon = "fas fa-piggy-bank",
label = "Use ATM",
args = true,
},
{
event = "signoff",
icon = "far fa-clipboard",
label = "Sign Off",
event = "banking:hack",
icon = "fas fa-piggy-bank",
label = "Hack ATM",
args = false,
},
},
job = {"police", "ambulance", "mechanic"},
distance = 1.5
})
end)
```
140 changes: 107 additions & 33 deletions bt-target/client/main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Models = {}
local Bones = {}
local Zones = {}

Citizen.CreateThread(function()
Expand All @@ -19,83 +20,129 @@ if Config.ESX then
PlayerJob = ESX.GetPlayerData().job

RegisterNetEvent('esx:setJob')
AddEventHandler('esx:setJob', function(job)
PlayerJob = job
end)
AddEventHandler('esx:setJob', function(job)
PlayerJob = job
end)
end)
else
PlayerJob = Config.NonEsxJob()
end

function playerTargetEnable()
if success then return end
if IsPedArmed(PlayerPedId(), 6) then return end
--if success then return end
local playerPed = PlayerPedId() -- Defining player ped id before running loop
if IsPedArmed(playerPed, 6) then return end

targetActive = true

SendNUIMessage({response = "openTarget"})

while targetActive do
local plyCoords = GetEntityCoords(GetPlayerPed(-1))
local plyCoords = GetEntityCoords(playerPed)
local hit, coords, entity = RayCastGamePlayCamera(20.0)
local nearestVehicle = GetNearestVehicle()

if hit == 1 then
if GetEntityType(entity) ~= 0 then
for _, model in pairs(Models) do
if _ == GetEntityModel(entity) then
if Models[_]["job"] == nil then Models[_]["job"] = { "all" } end -- Nil check for Job default to ALL if undefined
for k , v in ipairs(Models[_]["job"]) do
if v == "all" or v == PlayerJob.name then
if _ == GetEntityModel(entity) then
if #(plyCoords - coords) <= Models[_]["distance"] then

success = true
if ((v == "all") or (PlayerJob and v == PlayerJob.name)) then -- Added Nil Check for PlayerJob
if #(plyCoords - coords) <= Models[_]["distance"] then

SendNUIMessage({response = "validTarget", data = Models[_]["options"]})
success = true

while success and targetActive do
local plyCoords = GetEntityCoords(GetPlayerPed(-1))
local hit, coords, entity = RayCastGamePlayCamera(20.0)
SendNUIMessage({response = "validTarget", data = Models[_]["options"], object = entity, model = _})

DisablePlayerFiring(PlayerPedId(), true)
while success and targetActive do
local plyCoords = GetEntityCoords(playerPed)
local hit, coords, entity = RayCastGamePlayCamera(20.0)

if (IsControlJustReleased(0, 24) or IsDisabledControlJustReleased(0, 24)) then
SetNuiFocus(true, true)
SetCursorLocation(0.5, 0.5)
end
DisablePlayerFiring(playerPed, true)

if GetEntityType(entity) == 0 or #(plyCoords - coords) > Models[_]["distance"] then
success = false
end
if (IsControlJustReleased(0, 24) or IsDisabledControlJustReleased(0, 24)) then
SetNuiFocus(true, true)
SetCursorLocation(0.5, 0.5)
end

Citizen.Wait(1)
if GetEntityType(entity) == 0 or #(plyCoords - coords) > Models[_]["distance"] then
success = false
end
SendNUIMessage({response = "leftTarget"})

Citizen.Wait(1)
end
SendNUIMessage({response = "leftTarget"})
end
end

end
end
end
--Vehicle Bones
-- if nearestVehicle then
-- for _, bone in pairs(Bones) do
-- local boneIndex = GetEntityBoneIndexByName(nearestVehicle, _)
-- local bonePos = GetWorldPositionOfEntityBone(nearestVehicle, boneIndex)
-- local distanceToBone = GetDistanceBetweenCoords(bonePos, plyCoords, 1)
-- if #(bonePos - coords) <= Bones[_]["distance"] then
-- for k , v in ipairs(Bones[_]["job"]) do
-- if v == "all" or v == PlayerJob.name then
-- if #(plyCoords - coords) <= Bones[_]["distance"] then
-- success = true
-- newOptions = {}
-- for i, op in ipairs(Bones[_]["options"]) do
-- table.insert(newOptions,Bones[_]["options"][i])
-- end
-- SendNUIMessage({response = "validTarget", data = newOptions})

-- while success and targetActive do
-- local plyCoords = GetEntityCoords(GetPlayerPed(-1))
-- local hit, coords, entity = RayCastGamePlayCamera(7.0)
-- local boneI = GetEntityBoneIndexByName(nearestVehicle, _)

-- DisablePlayerFiring(PlayerPedId(), true)

-- if (IsControlJustReleased(0, 24) or IsDisabledControlJustReleased(0, 24)) then
-- SetNuiFocus(true, true)
-- SetCursorLocation(0.5, 0.5)
-- end

-- if #(plyCoords - coords) > Bones[_]["distance"] then
-- success = false
-- end

-- Citizen.Wait(1)
-- end
-- SendNUIMessage({response = "leftTarget"})
-- end
-- end
-- end
-- end
-- end
-- end
end

for _, zone in pairs(Zones) do
if Zones[_]:isPointInside(coords) then
if Zones[_]["targetoptions"]["job"] == nil then Zones[_]["targetoptions"]["job"] = { "all" } end -- Nil check for Job default to ALL if undefined
for k , v in ipairs(Zones[_]["targetoptions"]["job"]) do
if v == "all" or v == PlayerJob.name then
if ((v == "all") or (PlayerJob and v == PlayerJob.name)) then -- Added Nil Check for PlayerJob
if #(plyCoords - Zones[_].center) <= zone["targetoptions"]["distance"] then

success = true

SendNUIMessage({response = "validTarget", data = Zones[_]["targetoptions"]["options"]})
while success and targetActive do
local plyCoords = GetEntityCoords(GetPlayerPed(-1))
local plyCoords = GetEntityCoords(playerPed)
local hit, coords, entity = RayCastGamePlayCamera(20.0)

DisablePlayerFiring(PlayerPedId(), true)
DisablePlayerFiring(playerPed, true)

if (IsControlJustReleased(0, 24) or IsDisabledControlJustReleased(0, 24)) then
SetNuiFocus(true, true)
SetCursorLocation(0.5, 0.5)
SetNuiFocus(true, true)
SetCursorLocation(0.5, 0.5)

elseif not Zones[_]:isPointInside(coords) or #(vector3(Zones[_].center.x, Zones[_].center.y, Zones[_].center.z) - plyCoords) > zone.targetoptions.distance then
end

Expand All @@ -108,6 +155,7 @@ function playerTargetEnable()
end
SendNUIMessage({response = "leftTarget"})
end

end
end
end
Expand All @@ -129,22 +177,40 @@ end

RegisterNUICallback('selectTarget', function(data, cb)
SetNuiFocus(false, false)

success = false

targetActive = false

TriggerEvent(data.event)
if data.event.args then
TriggerEvent(data.event.event, data)
else
TriggerEvent(data.event.event)
end
end)

RegisterNUICallback('closeTarget', function(data, cb)
SetNuiFocus(false, false)

success = false

targetActive = false
end)

--Added functions
function GetNearestVehicle()
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
if not (playerCoords and playerPed) then
return
end

local pointB = GetEntityForwardVector(playerPed) * 0.001 + playerCoords

local shapeTest = StartShapeTestCapsule(playerCoords.x, playerCoords.y, playerCoords.z, pointB.x, pointB.y, pointB.z, 1.0, 10, playerPed, 7)
local _, hit, _, _, entity = GetShapeTestResult(shapeTest)

return (hit == 1 and IsEntityAVehicle(entity)) and entity or false
end

--Functions from https://forum.cfx.re/t/get-camera-coordinates/183555/14

function RotationToDirection(rotation)
Expand Down Expand Up @@ -200,6 +266,12 @@ function AddTargetModel(models, parameteres)
end
end

function AddTargetBone(bones, parameteres)
for _, bone in pairs(bones) do
Bones[bone] = parameteres
end
end

function RemoveZone(name)
if not Zones[name] then return end
if Zones[name].destroy then
Expand All @@ -217,4 +289,6 @@ exports("AddPolyzone", AddPolyzone)

exports("AddTargetModel", AddTargetModel)

exports("AddTargetBone", AddTargetBone)

exports("RemoveZone", RemoveZone)
8 changes: 5 additions & 3 deletions bt-target/html/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ window.addEventListener('message', function(event) {

$("#target-"+index+"").css("padding-top", "7px");

$("#target-"+index).data('TargetData', item.event);
item.object = event.data.object
item.model = event.data.model
$("#target-"+index).data('TargetData', item);
});

$(".target-eye").css("color", "rgb(30,144,255)");
Expand All @@ -41,7 +43,7 @@ $(document).on('mousedown', (event) => {
if (element.id.split("-")[0] === 'target') {
let TargetData = $("#"+element.id).data('TargetData');

$.post('http://bt-target/selectTarget', JSON.stringify({
$.post('https://' + GetParentResourceName() + '/selectTarget', JSON.stringify({
event: TargetData,
}));

Expand All @@ -55,7 +57,7 @@ $(document).on('keydown', function() {
case 27: // ESC
$(".target-label").html("");
$('.target-wrapper').hide();
$.post('http://bt-target/closeTarget');
$.post('https://' + GetParentResourceName() + '/closeTarget');
break;
}
});