Tidy up of helpers, and use joaat() instead

This commit is contained in:
Jim Shield
2025-08-13 18:13:58 +01:00
parent 748e5e2201
commit 06ef5cb53b
3 changed files with 115 additions and 120 deletions

View File

@@ -212,7 +212,7 @@ elseif checkExists(Exports.OXCoreExport) then
cache.Vehicles = {} cache.Vehicles = {}
for k, v in pairs(Ox.GetVehicleData()) do for k, v in pairs(Ox.GetVehicleData()) do
cache.Vehicles[k] = { cache.Vehicles[k] = {
model = k, hash = GetHashKey(k), model = k, hash = joaat(k),
price = v.price, price = v.price,
name = v.name, name = v.name,
brand = v.make brand = v.make
@@ -225,7 +225,7 @@ elseif checkExists(Exports.ESXExport) then
for _, v in pairs(MySQL.query.await('SELECT model, price, name FROM vehicles')) do for _, v in pairs(MySQL.query.await('SELECT model, price, name FROM vehicles')) do
cache.Vehicles[v.model] = { cache.Vehicles[v.model] = {
model = v.model, model = v.model,
hash = GetHashKey(v.model), hash = joaat(v.model),
price = v.price, price = v.price,
name = v.name, name = v.name,
} }

View File

@@ -226,9 +226,9 @@ end
function cv(amount) function cv(amount)
local formatted = tostring(amount or "0") local formatted = tostring(amount or "0")
while true do while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') local newFormatted, count = formatted:gsub("^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then break end if count == 0 then break end
Wait(0) formatted = newFormatted
end end
return formatted return formatted
end end
@@ -243,13 +243,14 @@ end
--- ``` --- ```
function formatCoord(coord) function formatCoord(coord)
local vecType = type(coord):gsub("tor", "") local vecType = type(coord):gsub("tor", "")
local components = { local parts = {}
[1] = coord.x and ("^6"..string.format("%.1f", coord.x)) or "",
[2] = coord.y and ("^7, ^6"..string.format("%.1f", coord.y)) or "", if coord.x then parts[#parts + 1] = string.format("^6%.1f", coord.x) end
[3] = coord.z and ("^7, ^6"..string.format("%.1f", coord.z)) or "", if coord.y then parts[#parts + 1] = string.format("^6%.1f", coord.y) end
[4] = coord.w and ("^7, ^6"..string.format("%.1f", coord.w)) or "", if coord.z then parts[#parts + 1] = string.format("^6%.1f", coord.z) end
} if coord.w then parts[#parts + 1] = string.format("^6%.1f", coord.w) end
return "^5"..vecType.."^7("..components[1]..components[2]..components[3]..components[4].."^7)"
return string.format("^5%s^7(%s^7)", vecType, table.concat(parts, "^7, "))
end end
--- Calculates the center point of a list of coordinates. --- Calculates the center point of a list of coordinates.
@@ -261,13 +262,17 @@ end
--- print("Center of Zones:", center) --- print("Center of Zones:", center)
--- ``` --- ```
function getCenterOfZones(tbl) function getCenterOfZones(tbl)
local count = #tbl
if count == 0 then return vector3(0, 0, 0) end
local totalX, totalY, totalZ = 0, 0, 0 local totalX, totalY, totalZ = 0, 0, 0
for _, coord in ipairs(tbl) do for i = 1, count do
local coord = tbl[i]
totalX = totalX + coord.x totalX = totalX + coord.x
totalY = totalY + coord.y totalY = totalY + coord.y
totalZ = totalZ + coord.z totalZ = totalZ + coord.z
end end
local count = #tbl
return vector3(totalX / count, totalY / count, totalZ / count) return vector3(totalX / count, totalY / count, totalZ / count)
end end
@@ -280,9 +285,9 @@ end
--- print("Number of keys:", count) --- print("Number of keys:", count)
--- ``` --- ```
function countTable(tbl) function countTable(tbl)
local i = 0 local count = 0
for _ in pairs(tbl) do i += 1 end for _ in pairs(tbl) do count = count + 1 end
return i return count
end end
--- Returns an iterator over a table's keys in sorted order. --- Returns an iterator over a table's keys in sorted order.
@@ -299,7 +304,19 @@ function pairsByKeys(t)
print("^1Error^7: ^3Nil ^2table recieved for ^3pairsByKeys^7(), ^2setting to ^7{} ^2to prevent break^7") print("^1Error^7: ^3Nil ^2table recieved for ^3pairsByKeys^7(), ^2setting to ^7{} ^2to prevent break^7")
t = {} t = {}
end end
local a = {} for n in pairs(t) do a[#a+1] = n end table.sort(a) local i = 0 local iter = function() i += 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter
local keys = {}
for key in pairs(t) do
keys[#keys + 1] = key
end
table.sort(keys)
local index = 0
return function()
index = index + 1
local key = keys[index]
return key, t[key]
end
end end
--- Creates a new table with consecutive numerical indices sorted by the 'id' field. --- Creates a new table with consecutive numerical indices sorted by the 'id' field.
@@ -313,15 +330,18 @@ end
--- end --- end
--- ``` --- ```
function createConsecutiveTable(originalTable) function createConsecutiveTable(originalTable)
local sortedEntries = {} local entries = {}
for _, entry in pairs(originalTable) do table.insert(sortedEntries, entry) end for _, entry in pairs(originalTable) do
table.sort(sortedEntries, function(a, b) return a.id < b.id end) entries[#entries + 1] = entry
local newTable = {}
for newIndex, entry in ipairs(sortedEntries) do
entry.id = newIndex
newTable[newIndex] = entry
end end
return newTable
table.sort(entries, function(a, b) return a.id < b.id end)
for index, entry in ipairs(entries) do
entry.id = index
end
return entries
end end
--- Concatenates a table of strings into a single string separated by newlines. --- Concatenates a table of strings into a single string separated by newlines.
@@ -333,11 +353,7 @@ end
--- print(combinedText) --- print(combinedText)
--- ``` --- ```
function concatenateText(tbl) function concatenateText(tbl)
local result = "" return table.concat(tbl, "\n")
for i = 1, #tbl do
result = result..tbl[i]..(i < #tbl and "\n" or "")
end
return result
end end
--- Converts a rotation (degrees) to a direction vector. --- Converts a rotation (degrees) to a direction vector.
@@ -349,11 +365,14 @@ end
--- print(direction) --- print(direction)
--- ``` --- ```
function RotationToDirection(rot) function RotationToDirection(rot)
local adjust = math.pi / 180 local radianConvert = math.pi / 180
local rotX, rotZ = radianConvert * rot.x, radianConvert * rot.z
local cosX = math.abs(math.cos(rotX))
return vec3( return vec3(
-math.sin(adjust * rot.z) * math.abs(math.cos(adjust * rot.x)), -math.sin(rotZ) * cosX,
math.cos(adjust * rot.z) * math.abs(math.cos(adjust * rot.x)), math.cos(rotZ) * cosX,
math.sin(adjust * rot.x) math.sin(rotX)
) )
end end
@@ -366,11 +385,9 @@ end
--- print(bar) --- print(bar)
--- ``` --- ```
function basicBar(percentage) function basicBar(percentage)
local perc = math.ceil(percentage)
local total = 10 local total = 10
local filled = math.floor((perc / 100) * total) local filled = math.floor(math.min(math.max(percentage, 0), 100) / 100 * total)
local empty = total - filled return string.rep("", filled) .. string.rep("", total - filled)
return string.rep("", filled)..string.rep("", empty)
end end
--- Normalizes a 3D vector. --- Normalizes a 3D vector.
@@ -382,12 +399,8 @@ end
--- print(normalizedVec) --- print(normalizedVec)
--- ``` --- ```
function normalizeVector(vec) function normalizeVector(vec)
local len = math.sqrt(vec.x^2 + vec.y^2 + vec.z^2) local length = math.sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z)
if len ~= 0 then return length > 0 and vec3(vec.x / length, vec.y / length, vec.z / length) or vec3(0, 0, 0)
return vec3(vec.x / len, vec.y / len, vec.z / len)
else
return vec3(0, 0, 0)
end
end end
------------------------------------------------------------- -------------------------------------------------------------
@@ -403,16 +416,14 @@ end
--- drawLine(vector3(100, 200, 300), vector3(150, 250, 350), vector4(255, 0, 0, 255)) --- drawLine(vector3(100, 200, 300), vector3(150, 250, 350), vector4(255, 0, 0, 255))
--- ``` --- ```
function drawLine(startCoords, endCoords, col) function drawLine(startCoords, endCoords, col)
if debugMode then if not debugMode then return end
CreateThread(function()
local count = 1000 CreateThread(function()
while count >= 0 do for i = 100, 0, -1 do
DrawLine(startCoords.x, startCoords.y, startCoords.z, endCoords.x, endCoords.y, endCoords.z, col.x, col.y, col.z, col.w) DrawLine(startCoords.x, startCoords.y, startCoords.z, endCoords.x, endCoords.y, endCoords.z, col.x, col.y, col.z, col.w)
count -= 10 Wait(0)
Wait(0) end
end end)
end)
end
end end
--- Draws a sphere at the specified coordinates (for debugging). --- Draws a sphere at the specified coordinates (for debugging).
@@ -423,16 +434,14 @@ end
--- drawSphere(vector3(100, 200, 300), vector4(0, 255, 0, 255)) --- drawSphere(vector3(100, 200, 300), vector4(0, 255, 0, 255))
--- ``` --- ```
function drawSphere(coords, col) function drawSphere(coords, col)
if debugMode then if not debugMode then return end
CreateThread(function()
local count = 1000 CreateThread(function()
while count >= 0 do for i = 100, 0, -1 do
DrawSphere(coords.x, coords.y, coords.z, 0.5, col.x, col.y, col.z, col.w) DrawSphere(coords.x, coords.y, coords.z, 0.5, col.x, col.y, col.z, col.w)
count -= 1 Wait(10)
Wait(10) end
end end)
end)
end
end end
--- Performs a raycast between two coordinates and returns the results. --- Performs a raycast between two coordinates and returns the results.
@@ -450,15 +459,15 @@ end
--- end --- end
--- ``` --- ```
function PerformRaycast(startCoords, endCoords, entity, flags) function PerformRaycast(startCoords, endCoords, entity, flags)
drawLine(startCoords, endCoords, vec4(0,0,255,255)) drawLine(startCoords, endCoords, vec4(0, 0, 255, 255))
local val1, val2, val3, val4, val5, val6 = GetShapeTestResult(
StartExpensiveSynchronousShapeTestLosProbe( local shapeTest = StartExpensiveSynchronousShapeTestLosProbe(
startCoords.x, startCoords.y, startCoords.z, startCoords.x, startCoords.y, startCoords.z,
endCoords.x, endCoords.y, endCoords.z, endCoords.x, endCoords.y, endCoords.z,
flags or 4294967295, entity, 0 flags or 4294967295, entity, 0
)
) )
return val1, val2, val3, val4, val5, val6
return GetShapeTestResult(shapeTest)
end end
--- Adjusts the Z-coordinate of a position to the ground level. --- Adjusts the Z-coordinate of a position to the ground level.
@@ -470,16 +479,34 @@ end
--- print("Ground Position:", groundCoords) --- print("Ground Position:", groundCoords)
--- ``` --- ```
function adjustForGround(coords) function adjustForGround(coords)
local foundGround, zPos = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z + 1.0) local foundGround, groundZ = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z + 1.0)
if foundGround then if foundGround then
if coords.w then return coords.w and vec4(coords.x, coords.y, groundZ, coords.w) or vec3(coords.x, coords.y, groundZ)
return vec4(coords.x, coords.y, zPos, coords.w)
else
return vec3(coords.x, coords.y, zPos)
end
else
return coords
end end
return coords
end
local function waitForNetworkEntity(netId, converter, timeout)
timeout = timeout or 100
while not NetworkDoesNetworkIdExist(netId) and timeout > 0 do
timeout = timeout - 1
Wait(10)
end
if not NetworkDoesNetworkIdExist(netId) then return 0 end
local entity = converter(netId)
timeout = 100
while not DoesEntityExist(entity) and entity ~= 0 and timeout > 0 do
timeout = timeout - 1
Wait(10)
end
return DoesEntityExist(entity) and entity or 0
end end
--- Ensures a network vehicle exists from its network ID. --- Ensures a network vehicle exists from its network ID.
@@ -493,21 +520,7 @@ end
--- end --- end
--- ``` --- ```
function ensureNetToVeh(vehNetID) function ensureNetToVeh(vehNetID)
--debugPrint("^6Bridge^7: ^3ensureNetToVeh^7: ^2Requesting NetworkDoesNetworkIdExist^7(^6"..vehNetID.."^7)") return waitForNetworkEntity(vehNetID, NetToVeh)
local timeout = 100
while not NetworkDoesNetworkIdExist(vehNetID) and timeout > 0 do
timeout -= 1
Wait(10)
end
if not NetworkDoesNetworkIdExist(vehNetID) then return 0 end
timeout = 100
local vehicle = NetToVeh(vehNetID)
while not DoesEntityExist(vehicle) and vehicle ~= 0 and timeout > 0 do
timeout -= 1
Wait(10)
end
if not DoesEntityExist(vehicle) then return 0 end
return vehicle
end end
--- Ensures a network entity exists from its network ID. --- Ensures a network entity exists from its network ID.
@@ -521,21 +534,7 @@ end
--- end --- end
--- ``` --- ```
function ensureNetToEnt(entNetID) function ensureNetToEnt(entNetID)
--debugPrint("^6Bridge^7: ^3ensureNetToEnt^7: ^2Requesting NetworkDoesNetworkIdExist^7(^6"..entNetID.."^7)") return waitForNetworkEntity(entNetID, NetworkGetEntityFromNetworkId)
local timeout = 100
while not NetworkDoesNetworkIdExist(entNetID) and timeout > 0 do
timeout -= 1
Wait(10)
end
if not NetworkDoesNetworkIdExist(entNetID) then return 0 end
timeout = 100
local entity = NetworkGetEntityFromNetworkId(entNetID)
while not DoesEntityExist(entity) and entity ~= 0 and timeout > 0 do
timeout -= 1
Wait(10)
end
if not DoesEntityExist(entity) then return 0 end
return entity
end end
function sendLog(text) function sendLog(text)
@@ -580,11 +579,7 @@ RegisterNetEvent(getScript()..":server:sendlog", sendServerLog)
-- local hungerAmount = GetRandomTiming(hunger) -- local hungerAmount = GetRandomTiming(hunger)
-- print(hungerAmount) -- number between 10, 20 -- print(hungerAmount) -- number between 10, 20
function GetRandomTiming(tbl) function GetRandomTiming(tbl)
if type(tbl) == "table" then return type(tbl) == "table" and math.random(tbl[1], tbl[2]) or tbl
return math.random(tbl[1], tbl[2])
else
return tbl
end
end end
------------------------------------------------------------- -------------------------------------------------------------
@@ -820,9 +815,9 @@ local materials = {
--- local matHash, matName = GetGroundMaterialAtPosition(vector3(100,200,300)) --- local matHash, matName = GetGroundMaterialAtPosition(vector3(100,200,300))
--- print("Material:", matName) --- print("Material:", matName)
--- ``` --- ```
function GetGroundMaterialAtPosition(coords) function GetGroundMaterialAtPosition(coords, ped)
local endX, endY, endZ = coords.x, coords.y, coords.z - 1.0 local endCoords = vec3(coords.x, coords.y, coords.z - 1.1)
local rayHandle = StartShapeTestCapsule(coords.x, coords.y, coords.z, endX, endY, endZ, 1.0, 1, playerPed, 7) local rayHandle = StartShapeTestCapsule(coords.x, coords.y, coords.z, endX, endY, endZ, 1.0, 1, Ped or PlayerPedId(), 7)
local _, hit, _, _, materialHash, _ = GetShapeTestResultIncludingMaterial(rayHandle) local _, hit, _, _, materialHash, _ = GetShapeTestResultIncludingMaterial(rayHandle)
local materialName = "Unknown" local materialName = "Unknown"
for k, v in pairs(materials) do for k, v in pairs(materials) do

View File

@@ -57,7 +57,7 @@ function searchCar(vehicle)
} }
if Vehicles then if Vehicles then
for k, v in pairs(Vehicles) do for k, v in pairs(Vehicles) do
if tonumber(v.hash) == model or GetHashKey(v.hash) == model or GetHashKey(v.model) == model then if tonumber(v.hash) == model or joaat(v.hash) == model or joaat(v.model) == model then
debugPrint("^6Bridge^7: ^2Vehicle info found in^7 ^4Vehicles^7 ^2table^7: ^6"..(v.hash and v.hash or v.model).. " ^7(^6"..Vehicles[k].name.."^7)") debugPrint("^6Bridge^7: ^2Vehicle info found in^7 ^4Vehicles^7 ^2table^7: ^6"..(v.hash and v.hash or v.model).. " ^7(^6"..Vehicles[k].name.."^7)")
carInfo.name = Vehicles[k].name.." "..Vehicles[k].brand carInfo.name = Vehicles[k].name.." "..Vehicles[k].brand
carInfo.price = Vehicles[k].price carInfo.price = Vehicles[k].price