mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(grid): use an array to store nearby entries
This commit is contained in:
@@ -32,7 +32,7 @@ local xDelta = (mapMaxX - mapMinX) / 34
|
||||
local yDelta = (mapMaxY - mapMinY) / 50
|
||||
local grid = {}
|
||||
local lastCell = {}
|
||||
local lastNearbyEntries = {}
|
||||
local gridCache = {}
|
||||
local entrySet = {}
|
||||
|
||||
lib.grid = {}
|
||||
@@ -82,18 +82,18 @@ end
|
||||
|
||||
---@param point vector
|
||||
---@param filter? fun(entry: GridEntry): boolean
|
||||
---@return GridEntry[]
|
||||
---@return Array<GridEntry>
|
||||
function lib.grid.getNearbyEntries(point, filter)
|
||||
local minX, maxX, minY, maxY = getGridDimensions(point, xDelta, yDelta)
|
||||
|
||||
if lastNearbyEntries.minX == minX and
|
||||
lastNearbyEntries.maxX == maxX and
|
||||
lastNearbyEntries.minY == minY and
|
||||
lastNearbyEntries.maxY == maxY then
|
||||
return lastNearbyEntries.entries
|
||||
if gridCache.minX == minX and
|
||||
gridCache.maxX == maxX and
|
||||
gridCache.minY == minY and
|
||||
gridCache.maxY == maxY then
|
||||
return gridCache.entries
|
||||
end
|
||||
|
||||
local entries = {}
|
||||
local entries = lib.array:new()
|
||||
local n = 0
|
||||
|
||||
table.wipe(entrySet)
|
||||
@@ -118,11 +118,11 @@ function lib.grid.getNearbyEntries(point, filter)
|
||||
end
|
||||
end
|
||||
|
||||
lastNearbyEntries.minX = minX
|
||||
lastNearbyEntries.maxX = maxX
|
||||
lastNearbyEntries.minY = minY
|
||||
lastNearbyEntries.maxY = maxY
|
||||
lastNearbyEntries.entries = entries
|
||||
gridCache.minX = minX
|
||||
gridCache.maxX = maxX
|
||||
gridCache.minY = minY
|
||||
gridCache.maxY = maxY
|
||||
gridCache.entries = entries
|
||||
|
||||
return entries
|
||||
end
|
||||
@@ -145,7 +145,7 @@ function lib.grid.addEntry(entry)
|
||||
|
||||
grid[y] = row
|
||||
|
||||
table.wipe(lastNearbyEntries)
|
||||
table.wipe(gridCache)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -184,7 +184,7 @@ function lib.grid.removeEntry(entry)
|
||||
::continue::
|
||||
end
|
||||
|
||||
table.wipe(lastNearbyEntries)
|
||||
table.wipe(gridCache)
|
||||
|
||||
return success
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user