perf(client/points): apply optimisations used in zones

"SphereZones" are really just points with some extra overhead, but the module
had some overall improvements to improve performance.
Includes type definitions for sumneko.
This commit is contained in:
Linden
2022-09-14 07:43:17 +10:00
parent 32e2da2d8f
commit 9a17b9bac5

View File

@@ -1,21 +1,34 @@
---@class CPoint
---@field id number
---@field coords vector3
---@field distance number
---@field currentDistance number
---@field remove fun()
---@field onEnter fun(self: CPoint)?
---@field onExit fun(self: CPoint)?
---@field nearby fun(self: CPoint)?
local points = {}
local function removePoint(self)
points[self.id] = nil
end
local nearby = {}
local closest
local nearbyPoints = {}
local nearbyCount = 0
local closestPoint
local tick
CreateThread(function()
while true do
if nearbyCount ~= 0 then
table.wipe(nearbyPoints)
nearbyCount = 0
end
local coords = GetEntityCoords(cache.ped)
cache.coords = coords
Wait(300)
closest = nil
table.wipe(nearby)
closestPoint = nil
for _, point in pairs(points) do
local distance = #(coords - point.coords)
@@ -24,12 +37,13 @@ CreateThread(function()
point.currentDistance = distance
---@diagnostic disable-next-line: need-check-nil
if distance < (closest?.currentDistance or point.distance) then
closest = point
if distance < (closestPoint?.currentDistance or point.distance) then
closestPoint = point
end
if point.nearby then
nearby[#nearby + 1] = point
nearbyCount += 1
nearbyPoints[nearbyCount] = point
end
if point.onEnter and not point.inside then
@@ -44,20 +58,23 @@ CreateThread(function()
end
if not tick then
if #nearby > 0 then
if nearbyCount ~= 0 then
tick = SetInterval(function()
for i = 1, #nearby do
nearby[i]:nearby()
for i = 1, nearbyCount do
nearbyPoints[i]:nearby()
end
end)
end
elseif #nearby == 0 then
elseif nearbyCount == 0 then
tick = ClearInterval(tick)
end
Wait(300)
end
end)
lib.points = {
---@return CPoint
new = function(...)
local args = {...}
local id = #points + 1
@@ -91,8 +108,9 @@ lib.points = {
return self
end,
---@return CPoint
closest = function()
return closest
return closestPoint
end
}