mirror of
https://github.com/sys4-fr/server-nalc.git
synced 2025-12-22 01:25:31 +01:00
Optimize areas with AreaStore (by est31)
This commit is contained in:
@@ -20,6 +20,23 @@ function areas:save()
|
||||
file:close()
|
||||
end
|
||||
|
||||
local function populateStore(self)
|
||||
if not rawget(_G, "AreaStore") then
|
||||
return
|
||||
end
|
||||
local store = AreaStore()
|
||||
local store_ids = {}
|
||||
store:reserve(#self.areas)
|
||||
for id, area in pairs(areas.areas) do
|
||||
local sid = store:insert_area(area.pos1,
|
||||
area.pos2, tostring(id))
|
||||
assert(sid ~= nil) -- if its nil, no id could be found
|
||||
store_ids[id] = sid
|
||||
end
|
||||
self.store = store
|
||||
self.store_ids = store_ids
|
||||
end
|
||||
|
||||
-- Load the areas table from the save file
|
||||
function areas:load()
|
||||
local file, err = io.open(self.config.filename, "r")
|
||||
@@ -31,6 +48,7 @@ function areas:load()
|
||||
if type(self.areas) ~= "table" then
|
||||
self.areas = {}
|
||||
end
|
||||
populateStore(self)
|
||||
file:close()
|
||||
end
|
||||
|
||||
@@ -48,6 +66,12 @@ function areas:add(owner, name, pos1, pos2, parent)
|
||||
local id = findFirstUnusedIndex(self.areas)
|
||||
self.areas[id] = {name=name, pos1=pos1, pos2=pos2, owner=owner,
|
||||
parent=parent}
|
||||
-- add to AreaStore
|
||||
if self.store then
|
||||
local sid = self.store:insert_area(pos1, pos2, dump(id))
|
||||
assert(sid ~= nil) -- if its nil, no id could be found
|
||||
self.store_ids[id] = sid
|
||||
end
|
||||
return id
|
||||
end
|
||||
|
||||
@@ -75,6 +99,13 @@ function areas:remove(id, recurse)
|
||||
|
||||
-- Remove main entry
|
||||
self.areas[id] = nil
|
||||
|
||||
-- remove from AreaStore
|
||||
if self.store then
|
||||
local sid = self.store_ids[id]
|
||||
self.store_ids[id] = nil
|
||||
self.store:remove_area(sid)
|
||||
end
|
||||
end
|
||||
|
||||
-- Checks if a area between two points is entirely contained by another area
|
||||
|
||||
Reference in New Issue
Block a user