Originally Posted by Fyrakin
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:
local function IsCoordinateInRange(x, y)
if not FyrMM.currentMap.TrueMapSize or not FyrMM.currentMap.PlayerNX or not FyrMM.SV.CustomPinViewRange or not FyrMM.SV.ViewRangeFiltering then return true end
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
end
Lua Code:
-- this is used so heavily that I'd localize it at the file level
local g_currentMap = FyrMM.currentMap
local function IsCoordinateInRange(x, y)
local mapSize = g_currentMap.TrueMapSize
local nx = g_currentMap.PlayerNX
local pinViewRange = FyrMM.SV.CustomPinViewRange
if not (mapSize and nx and pinViewRange) then return true end
local dx = x - nx
local dy = y - g_currentMap.PlayerNY
return (dx * dx + dy * dy) * mapSize * mapSize <= pinViewRange * pinViewRange
end