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