Stop reusing holes in areas IDs

This eliminates the need of iterating the whole list for every protection operations. Note that the highest index isn't cached, i.e. the first or few (if there are many holes) operations would still suffer from the lag.
This commit is contained in:
1F616EMO 2024-10-25 17:56:51 +08:00 committed by SmallJoker
parent d2b227eca6
commit ec77a57f42

View File

@ -83,19 +83,19 @@ function areas:populateStore()
self.store_ids = store_ids
end
-- Finds the first usable index in a table
-- Eg: {[1]=false,[4]=true} -> 2
local function findFirstUnusedIndex(t)
local i = 0
repeat i = i + 1
until t[i] == nil
return i
-- Guarentees returning an unused index in areas.areas
local index_cache = 0
local function findFirstUnusedIndex()
local t = areas.areas
repeat index_cache = index_cache + 1
until t[index_cache] == nil
return index_cache
end
--- Add an area.
-- @return The new area's ID.
function areas:add(owner, name, pos1, pos2, parent)
local id = findFirstUnusedIndex(self.areas)
local id = findFirstUnusedIndex()
self.areas[id] = {
name = name,
pos1 = pos1,