Skip to content

Commit

Permalink
refactor: casings
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm authored Dec 22, 2024
1 parent bdb5f33 commit 4a03806
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 126 deletions.
136 changes: 61 additions & 75 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
local config = require 'config.client'
local sharedConfig = require 'config.shared'
local casings = {}
local currentCasing = nil
local currentCasing = 0
local bloodDrops = {}
local currentBloodDrop = nil
local fingerprints = {}
local currentFingerprint = 0
local shotsFired = 0
local recentlyGSR = false

exports.ox_inventory:displayMetadata({
collector = locale('collector'),
location = locale('location'),
caliber = locale('casing.caliber'),
})

local function dropBulletCasing()
local randX = math.random() + math.random(-1, 1)
local randY = math.random() + math.random(-1, 1)
local coords = GetOffsetFromEntityInWorldCoords(cache.ped, randX, randY, 0)
local serial = exports.ox_inventory:getCurrentWeapon().metadata.serial
TriggerServerEvent('evidence:server:CreateCasing', cache.weapon, serial, coords)
Wait(300)

TriggerServerEvent('qbx_evidence:server:createCasing', coords)
end

---@param casingId integer
local function drawCasing(casingId)
local coords = GetEntityCoords(cache.ped)

if #(coords - casings[casingId].coords) >= 1.5 then return end

qbx.drawText3d({
text = ('[~g~G~s~] %s'):format(locale('casing.label')),
coords = casings[casingId].coords
})

local streets = qbx.getStreetName(casings[casingId].coords)
local zone = qbx.getZoneName(casings[casingId].coords)
local location = {
main = streets.main,
zone = zone
}

if IsControlJustReleased(0, 47) then
TriggerServerEvent('qbx_evidence:server:collectCasing', casingId, location)
end
end

local function dnaHash(s)
Expand Down Expand Up @@ -60,14 +88,6 @@ local function drawEvidenceIfInRange(args)
end
end

local function canDiscoverEvidence()
return LocalPlayer.state.isLoggedIn
and QBX.PlayerData.job.type == 'leo'
and QBX.PlayerData.job.onduty
and IsPlayerFreeAiming(cache.playerId)
and cache.weapon == `WEAPON_FLASHLIGHT`
end

---@param evidence table<number, {coords: vector3}>
---@return number? evidenceId
local function getCloseEvidence(evidence)
Expand Down Expand Up @@ -136,55 +156,36 @@ RegisterNetEvent('qbx_evidence:client:clearBloodDropsInArea', function()
end
end)

RegisterNetEvent('qbx_evidence:client:addCasing', function(casingId, weapon, coords, serie)
casings[casingId] = {
type = weapon,
serie = serie and serie or locale('serial_not_visible'),
coords = vec3(coords.x, coords.y, coords.z - 0.9)
}
RegisterNetEvent('qbx_evidence:client:addCasing', function(casingId, newCasing)
casings[casingId] = newCasing
end)

RegisterNetEvent('qbx_evidence:client:removeCasing', function(casingId)
casings[casingId] = nil
currentCasing = 0
end)

RegisterNetEvent('qbx_evidence:client:clearCasingsInArea', function()
local pos = GetEntityCoords(cache.ped)
local casingList = {}
local function flashlightLoop()
CreateThread(function()
while cache.weapon do
local sleep = 1000

if lib.progressCircle({
duration = 5000,
position = 'bottom',
label = locale('clearing_casing'),
useWhileDead = false,
canCancel = true,
disable = {
move = false,
car = false,
combat = true,
mouse = false,
}
})
then
if casings and next(casings) then
for casingId in pairs(casings) do
if #(pos - casings[casingId].coords) < 10.0 then
casingList[#casingList + 1] = casingId
end
if IsPlayerFreeAiming(cache.playerId) then
sleep = 10
currentCasing = getCloseEvidence(casings) or currentCasing
currentBloodDrop = getCloseEvidence(bloodDrops) or currentBloodDrop
currentFingerprint = getCloseEvidence(fingerprints) or currentFingerprint
end
TriggerServerEvent('qbx_evidence:server:clearCasings', casingList)
exports.qbx_core:Notify(locale('casing_cleared'), 'success')

Wait(sleep)
end
else
exports.qbx_core:Notify(locale('canceled'), 'error')
end
end)
end)
end

local function playerShootingLoop()
CreateThread(function()
while cache.weapon do
if IsPedShooting(cache.ped) and not config.whitelistedWeapons[cache.weapon] then
if IsPedShooting(cache.ped) then
shotsFired += 1

if shotsFired > sharedConfig.statuses.gsr.threshold and not recentlyGSR and math.random() <= config.statuses.gsr.chance then
Expand Down Expand Up @@ -214,29 +215,27 @@ local function playerShootingLoop()
end

lib.onCache('weapon', function(weapon)
if not weapon or weapon == `WEAPON_UNARMED` or GetWeapontypeGroup(weapon) == 3566412244 then return end
if not weapon then return end

if QBX.PlayerData.job.type == 'leo' then
if not QBX.PlayerData.job.onduty or weapon ~= `WEAPON_FLASHLIGHT` then return end

playerShootingLoop()
flashlightLoop()
else
local weaponTypeGroup = GetWeapontypeGroup(weapon)

if config.blacklistedWeaponGroups[weaponTypeGroup] then return end

playerShootingLoop()
end
end)

--- draw 3D text on the ground to show evidence, if they press pickup button, set metadata and add it to their inventory.
CreateThread(function()
while true do
Wait(0)
if currentCasing and currentCasing ~= 0 then
drawEvidenceIfInRange({
evidenceId = currentCasing,
coords = casings[currentCasing].coords,
text = locale('bullet_casing', casings[currentCasing].type),
metadata = {
type = locale('casing'),
street = getStreetLabel(casings[currentCasing].coords),
ammolabel = config.ammoLabels[exports.qbx_core:GetWeapons()[casings[currentCasing].type].ammotype],
ammotype = casings[currentCasing].type,
serie = casings[currentCasing].serie
},
serverEventOnPickup = 'qbx_evidence:server:addCasingToInventory'
})
drawCasing(currentCasing)
end

if currentBloodDrop and currentBloodDrop ~= 0 then
Expand Down Expand Up @@ -268,17 +267,4 @@ CreateThread(function()
})
end
end
end)

CreateThread(function()
while true do
local closeEvidenceSleep = 1000
if canDiscoverEvidence() then
closeEvidenceSleep = 10
currentCasing = getCloseEvidence(casings) or currentCasing
currentBloodDrop = getCloseEvidence(bloodDrops) or currentBloodDrop
currentFingerprint = getCloseEvidence(fingerprints) or currentFingerprint
end
Wait(closeEvidenceSleep)
end
end)
18 changes: 11 additions & 7 deletions config/client.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
return {
whitelistedWeapons = {
`weapon_unarmed`,
`weapon_snowball`,
`weapon_stungun`,
`weapon_petrolcan`,
`weapon_hazardcan`,
`weapon_fireextinguisher`,
blacklistedWeaponGroups = {
3539449195, -- GROUP_DIGISCANNER
4257178988, -- GROUP_FIREEXTINGUISHER
1175761940, -- GROUP_HACKINGDEVICE
3759491383, -- GROUP_METALDETECTOR
431593103, -- GROUP_PARACHUTE
1595662460, -- GROUP_PETROLCAN
75159441, -- GROUP_TRANQILIZER
2685387236, -- GROUP_UNARMED
3566412244, -- GROUP_MELEE
1548507267, -- GROUP_THROWN
},

ammoLabels = {
Expand Down
10 changes: 6 additions & 4 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"casing": {
"label": "Bullet casing",
"caliber": "Caliber"
},
"collector": "Collected by",
"location": "Found at",
"canceled": "Canceled",
"clearing_blood": "Clearing blood...",
"blood_cleared": "Cleared blood!",
"blood": "Blood",
"blood_text": "[~g~G~s~] Blood %s",
"clearing_casing": "Clearing casing...",
"casing_cleared": "Cleared casing!",
"casing": "Bullet casing",
"bullet_casing": "[~g~G~s~] Bullet Casing %s",
"clearing_fingerprint": "Clearing fingerprint...",
"fingerprint_cleared": "Cleared fingerprint!",
"fingerprint": "Fingerprint",
Expand Down
86 changes: 46 additions & 40 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@ RegisterNetEvent('qbx_evidence:server:setGSR', function()
playerGSR[source] = os.time() + sharedConfig.statuses.gsr.duration
end)

RegisterNetEvent('qbx_evidence:server:createCasing', function(coords)
local weapon = exports.ox_inventory:GetCurrentWeapon(source)
local ammo = exports.ox_inventory:Items(weapon.name).ammoname
local casingId = lib.string.random('111111')
local casingData = {
serial = weapon.metadata.serial,
caliber = ammo.label,
coords = vec3(coords.x, coords.y, coords.z - 0.9),
created = GetGameTimer(),
}

casings[casingId] = casingData

TriggerClientEvent('qbx_evidence:client:addCasing', -1, casingId, casingData)
end)

RegisterNetEvent('qbx_evidence:server:collectCasing', function(casingId, location)
if not casings[casingId] then return end

local src = source
local casing = casings[casingId]
local player = exports.qbx_core:GetPlayer(src)
local playerPed = GetPlayerPed(src)
local playerCoords = GetEntityCoords(playerPed)

if not player or #(playerCoords - casing.coords) > 5.0 then return end

local name = ('%s %s'):format(player.firstname, player.lastname)
local metadata = {
label = locale('bullet_casing'),
caliber = casing.caliber,
collector = name,
location = ('%s, %s'):format(location.main, location.zone),
}

local collected = exports.ox_inventory:AddItem(src, 'evidence', 1, metadata)

if not collected then
exports.qbx_core:Notify(src, 'Your inventory is full...', 'error')
return
end

TriggerClientEvent('qbx_evidence:client:removeCasing', -1, casingId)
casings[casingId] = nil
end)

RegisterNetEvent('evidence:server:CreateBloodDrop', function(citizenid, bloodtype, coords)
local bloodId = generateId(bloodDrops)
bloodDrops[bloodId] = {
Expand Down Expand Up @@ -94,46 +140,6 @@ RegisterNetEvent('qbx_evidence:server:addFingerprintToInventory', function(finge
end
end)

RegisterNetEvent('evidence:server:CreateCasing', function(weapon, serial, coords)
local casingId = generateId(casings)
local serieNumber = exports.ox_inventory:GetCurrentWeapon(source).metadata.serial
if not serieNumber then
serieNumber = serial
end
TriggerClientEvent('qbx_evidence:client:addCasing', -1, casingId, weapon, coords, serieNumber)
end)

RegisterNetEvent('qbx_evidence:server:clearCasings', function(casingList)
if casingList and next(casingList) then
for _, v in pairs(casingList) do
TriggerClientEvent('qbx_evidence:client:removeCasing', -1, v)
casings[v] = nil
end
end
end)

RegisterNetEvent('qbx_evidence:server:addCasingToInventory', function(casingId, casingInfo)
local src = source
local player = exports.qbx_core:GetPlayer(src)
local playerName = player.PlayerData.charinfo.firstname..' '..player.PlayerData.charinfo.lastname
local streetName = casingInfo.street
local ammoType = casingInfo.ammolabel
local serialNumber = casingInfo.serie
local metadata = {}
metadata.type = 'Casing Evidence'
metadata.description = 'Ammo Type: '..ammoType
metadata.description = metadata.description..'\n\nSerial #: '..serialNumber
metadata.description = metadata.description..'\n\nCollected By: '..playerName
metadata.description = metadata.description..'\n\nCollected At: '..streetName
if not exports.ox_inventory:RemoveItem(src, 'empty_evidence_bag', 1) then
return exports.qbx_core:Notify(src, locale('error.have_evidence_bag'), 'error')
end
if exports.ox_inventory:AddItem(src, 'filled_evidence_bag', 1, metadata) then
TriggerClientEvent('qbx_evidence:client:removeCasing', -1, casingId)
casings[casingId] = nil
end
end)

CreateThread(function()
while true do
for source, expiration in pairs(playerGSR) do
Expand Down

0 comments on commit 4a03806

Please sign in to comment.