Thread: PTS issue, or?
View Single Post
08/30/15, 04:38 AM   #20
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by Fyrakin View Post
I found a way around without a need to scrap the root, but if I run into another performance hindrance I'll have to resort to that. My problem with distance checks is that when player moves all distances change and I have to recalculate all of them to make sure I selected the ones I need.
I tried to explain that there's no reason to compute the root if you only need it for comparison against some distance threshold. Additionally, each sub-expression should only be computed once -- it's both more efficient and readable.

Like this function, it does way too many avoidable table lookups and an unnecessary sqrt:
Lua Code:
  1. local function IsCoordinateInRange(x, y)
  2.     if not FyrMM.currentMap.TrueMapSize or not FyrMM.currentMap.PlayerNX or not FyrMM.SV.CustomPinViewRange or not FyrMM.SV.ViewRangeFiltering then return true end
  3.     return FyrMM.currentMap.TrueMapSize * math.sqrt((x-FyrMM.currentMap.PlayerNX)*(x-FyrMM.currentMap.PlayerNX)+(y-FyrMM.currentMap.PlayerNY)*(y-FyrMM.currentMap.PlayerNY)) <= FyrMM.SV.CustomPinViewRange
  4. end

Lua Code:
  1. -- this is used so heavily that I'd localize it at the file level
  2. local g_currentMap = FyrMM.currentMap
  3.  
  4. local function IsCoordinateInRange(x, y)
  5.     local mapSize = g_currentMap.TrueMapSize
  6.     local nx = g_currentMap.PlayerNX
  7.     local pinViewRange = FyrMM.SV.CustomPinViewRange
  8.     if not (mapSize and nx and pinViewRange) then return true end
  9.  
  10.     local dx = x - nx
  11.     local dy = y - g_currentMap.PlayerNY
  12.     return (dx * dx + dy * dy) * mapSize * mapSize <= pinViewRange * pinViewRange
  13. end
  Reply With Quote