From 57187c8c302a8a296cc16baee868cbdf900011b7 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 11 Sep 2022 12:06:30 +1000 Subject: [PATCH] perf(client/zones): reduce overhead In a naive test with 10k sphere zones vs points, zones ran 0.02~0.04ms higher. Overhead is roughly halved with these tweaks. --- imports/zones/client.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/imports/zones/client.lua b/imports/zones/client.lua index 4abde3e..a6c87f5 100644 --- a/imports/zones/client.lua +++ b/imports/zones/client.lua @@ -218,22 +218,26 @@ local function removeZone(self) end local inside = {} -local insideCount +local insideCount = 0 local tick +local glm_polygon_contains = glm.polygon.contains + CreateThread(function() while true do - table.wipe(inside) + if insideCount ~= 0 then + table.wipe(inside) + insideCount = 0 + end local coords = GetEntityCoords(cache.ped) cache.coords = coords - insideCount = 0 for _, zone in pairs(Zones) do - local contains = zone:contains(coords) zone.distance = #(zone.coords - coords) + local radius = zone.radius - if contains then + if not radius and glm_polygon_contains(zone.polygon, coords, zone.thickness / 4) or zone.distance < radius then if not zone.insideZone then zone.insideZone = true @@ -263,7 +267,7 @@ CreateThread(function() end if not tick then - if insideCount > 0 then + if insideCount ~= 0 then tick = SetInterval(function() for i = 1, insideCount do local zone = inside[i] @@ -317,8 +321,6 @@ local function debugSphere(self) self.radius, 255, 42, 24, 100, false, false, false, true, false, false, false) end -local glm_polygon_contains = glm.polygon.contains - local function contains(self, coords) return glm_polygon_contains(self.polygon, coords, self.thickness / 4) end