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

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