From 9a17b9bac5300ce38507dd87a4224eb8258ac4ba Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 14 Sep 2022 07:43:17 +1000 Subject: [PATCH] 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. --- imports/points/client.lua | 46 +++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/imports/points/client.lua b/imports/points/client.lua index 3463e60..4c546e7 100644 --- a/imports/points/client.lua +++ b/imports/points/client.lua @@ -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 }