From acd2f8c4db19fdecca5e88442afe2a7f50c8b58c Mon Sep 17 00:00:00 2001 From: sammyt291 Date: Wed, 22 Jul 2020 15:25:21 +0100 Subject: [PATCH 1/3] Add FindClosest function Adds a function to find the closest zone of a given class from a given pos --- lua/zones.lua | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lua/zones.lua b/lua/zones.lua index f95d6f2..d60fc62 100644 --- a/lua/zones.lua +++ b/lua/zones.lua @@ -150,6 +150,39 @@ function zones.GetID(zone) return table.KeyFromValue(zones.List,zone) end +--Returns Zone ID and distance from given Pos (Takes: Class, Pos Returns: Zone, Distance) +function zones.FindClosest( class, pos ) + if pos == nil then pos = Vector(0,0,0) end + + local zone = zones.FindByClass( class ) + + local target = -1 --Var for current closest Zone ID + local dist = -1 --Var for distance to closest Zone + local loc = Vector(0,0,0) --Var for expensive distance calc. + + for id,data in pairs(zone) do --For each zone + for _,bound in pairs(data.bounds) do --For each bound. + + local tempDist = pos:DistToSqr( bound ) + if tempDist < dist or target == -1 then + + target = id + dist = tempDist + loc = bound + + end + + end + end + + if not (target == -1) then --Get "expensive" Distance + dist = pos:Distance( loc ) + return target, dist + end + + return +end + @@ -608,4 +641,4 @@ function zones.PointInPoly(point,poly) //True if point is within a polygon. end return inside -end \ No newline at end of file +end From 6e3b8c40a7558fb81e3475a48a5bd4d29689e570 Mon Sep 17 00:00:00 2001 From: sammyt291 Date: Wed, 22 Jul 2020 15:36:51 +0100 Subject: [PATCH 2/3] Update zones.lua --- lua/zones.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/zones.lua b/lua/zones.lua index d60fc62..3132a17 100644 --- a/lua/zones.lua +++ b/lua/zones.lua @@ -150,7 +150,7 @@ function zones.GetID(zone) return table.KeyFromValue(zones.List,zone) end ---Returns Zone ID and distance from given Pos (Takes: Class, Pos Returns: Zone, Distance) +--Returns Zone ID and distance from given Pos (Takes: Class, Pos Returns: Zone ID, Distance) function zones.FindClosest( class, pos ) if pos == nil then pos = Vector(0,0,0) end From 6fe4c7a651e739b467c477466c732de71ec494fe Mon Sep 17 00:00:00 2001 From: sammyt291 Date: Wed, 22 Jul 2020 17:19:01 +0100 Subject: [PATCH 3/3] Tidy up return --- lua/zones.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lua/zones.lua b/lua/zones.lua index 3132a17..3a498bd 100644 --- a/lua/zones.lua +++ b/lua/zones.lua @@ -158,7 +158,6 @@ function zones.FindClosest( class, pos ) local target = -1 --Var for current closest Zone ID local dist = -1 --Var for distance to closest Zone - local loc = Vector(0,0,0) --Var for expensive distance calc. for id,data in pairs(zone) do --For each zone for _,bound in pairs(data.bounds) do --For each bound. @@ -168,16 +167,14 @@ function zones.FindClosest( class, pos ) target = id dist = tempDist - loc = bound end end end - if not (target == -1) then --Get "expensive" Distance - dist = pos:Distance( loc ) - return target, dist + if not (target == -1) then + return target, math.sqrt(dist) end return