2014-01-11 17:54:00 +01:00
|
|
|
-- Prevent anyone else accessing those functions
|
2014-04-28 03:02:48 +02:00
|
|
|
local forceload_block = core.forceload_block
|
|
|
|
local forceload_free_block = core.forceload_free_block
|
|
|
|
core.forceload_block = nil
|
|
|
|
core.forceload_free_block = nil
|
2014-01-11 17:54:00 +01:00
|
|
|
|
|
|
|
local blocks_forceloaded
|
2016-09-03 06:37:51 +02:00
|
|
|
local blocks_temploaded = {}
|
2014-01-11 17:54:00 +01:00
|
|
|
local total_forceloaded = 0
|
|
|
|
|
2019-06-10 02:07:33 +02:00
|
|
|
-- true, if the forceloaded blocks got changed (flag for persistence on-disk)
|
|
|
|
local forceload_blocks_changed = false
|
|
|
|
|
2016-07-23 13:29:53 +02:00
|
|
|
local BLOCKSIZE = core.MAP_BLOCKSIZE
|
2014-01-11 17:54:00 +01:00
|
|
|
local function get_blockpos(pos)
|
|
|
|
return {
|
|
|
|
x = math.floor(pos.x/BLOCKSIZE),
|
|
|
|
y = math.floor(pos.y/BLOCKSIZE),
|
|
|
|
z = math.floor(pos.z/BLOCKSIZE)}
|
|
|
|
end
|
|
|
|
|
2016-09-03 06:37:51 +02:00
|
|
|
-- When we create/free a forceload, it's either transient or persistent. We want
|
|
|
|
-- to add to/remove from the table that corresponds to the type of forceload, but
|
|
|
|
-- we also need the other table because whether we forceload a block depends on
|
|
|
|
-- both tables.
|
|
|
|
-- This function returns the "primary" table we are adding to/removing from, and
|
|
|
|
-- the other table.
|
|
|
|
local function get_relevant_tables(transient)
|
|
|
|
if transient then
|
|
|
|
return blocks_temploaded, blocks_forceloaded
|
|
|
|
else
|
|
|
|
return blocks_forceloaded, blocks_temploaded
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function core.forceload_block(pos, transient)
|
2019-06-10 02:07:33 +02:00
|
|
|
-- set changed flag
|
|
|
|
forceload_blocks_changed = true
|
|
|
|
|
2014-01-11 17:54:00 +01:00
|
|
|
local blockpos = get_blockpos(pos)
|
2014-04-28 03:02:48 +02:00
|
|
|
local hash = core.hash_node_position(blockpos)
|
2016-09-03 06:37:51 +02:00
|
|
|
local relevant_table, other_table = get_relevant_tables(transient)
|
|
|
|
if relevant_table[hash] ~= nil then
|
|
|
|
relevant_table[hash] = relevant_table[hash] + 1
|
2014-01-11 17:54:00 +01:00
|
|
|
return true
|
2016-09-03 06:37:51 +02:00
|
|
|
elseif other_table[hash] ~= nil then
|
|
|
|
relevant_table[hash] = 1
|
2014-01-11 17:54:00 +01:00
|
|
|
else
|
2014-12-12 20:49:19 +01:00
|
|
|
if total_forceloaded >= (tonumber(core.settings:get("max_forceloaded_blocks")) or 16) then
|
2014-01-11 17:54:00 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
total_forceloaded = total_forceloaded+1
|
2016-09-03 06:37:51 +02:00
|
|
|
relevant_table[hash] = 1
|
2014-01-11 17:54:00 +01:00
|
|
|
forceload_block(blockpos)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-03 06:37:51 +02:00
|
|
|
function core.forceload_free_block(pos, transient)
|
2019-06-10 02:07:33 +02:00
|
|
|
-- set changed flag
|
|
|
|
forceload_blocks_changed = true
|
|
|
|
|
2014-01-11 17:54:00 +01:00
|
|
|
local blockpos = get_blockpos(pos)
|
2014-04-28 03:02:48 +02:00
|
|
|
local hash = core.hash_node_position(blockpos)
|
2016-09-03 06:37:51 +02:00
|
|
|
local relevant_table, other_table = get_relevant_tables(transient)
|
|
|
|
if relevant_table[hash] == nil then return end
|
|
|
|
if relevant_table[hash] > 1 then
|
|
|
|
relevant_table[hash] = relevant_table[hash] - 1
|
|
|
|
elseif other_table[hash] ~= nil then
|
|
|
|
relevant_table[hash] = nil
|
2014-01-11 17:54:00 +01:00
|
|
|
else
|
|
|
|
total_forceloaded = total_forceloaded-1
|
2016-09-03 06:37:51 +02:00
|
|
|
relevant_table[hash] = nil
|
2014-01-11 17:54:00 +01:00
|
|
|
forceload_free_block(blockpos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Keep the forceloaded areas after restart
|
2014-04-28 03:02:48 +02:00
|
|
|
local wpath = core.get_worldpath()
|
2014-01-11 17:54:00 +01:00
|
|
|
local function read_file(filename)
|
|
|
|
local f = io.open(filename, "r")
|
|
|
|
if f==nil then return {} end
|
|
|
|
local t = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
if t=="" or t==nil then return {} end
|
2014-07-12 10:51:09 +02:00
|
|
|
return core.deserialize(t) or {}
|
2014-01-11 17:54:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
|
|
|
|
for _, __ in pairs(blocks_forceloaded) do
|
|
|
|
total_forceloaded = total_forceloaded + 1
|
|
|
|
end
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
core.after(5, function()
|
2014-01-11 17:54:00 +01:00
|
|
|
for hash, _ in pairs(blocks_forceloaded) do
|
2014-04-28 03:02:48 +02:00
|
|
|
local blockpos = core.get_position_from_hash(hash)
|
2014-01-11 17:54:00 +01:00
|
|
|
forceload_block(blockpos)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-06-10 02:07:33 +02:00
|
|
|
-- persists the currently forceloaded blocks to disk
|
|
|
|
local function persist_forceloaded_blocks()
|
2021-05-30 11:15:02 +02:00
|
|
|
local data = core.serialize(blocks_forceloaded)
|
|
|
|
core.safe_file_write(wpath.."/force_loaded.txt", data)
|
2019-06-10 02:07:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- periodical forceload persistence
|
|
|
|
local function periodically_persist_forceloaded_blocks()
|
2019-08-06 20:30:18 +02:00
|
|
|
|
2019-06-10 02:07:33 +02:00
|
|
|
-- only persist if the blocks actually changed
|
|
|
|
if forceload_blocks_changed then
|
|
|
|
persist_forceloaded_blocks()
|
|
|
|
|
|
|
|
-- reset changed flag
|
|
|
|
forceload_blocks_changed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- recheck after some time
|
|
|
|
core.after(10, periodically_persist_forceloaded_blocks)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- persist periodically
|
|
|
|
core.after(5, periodically_persist_forceloaded_blocks)
|
|
|
|
|
|
|
|
-- persist on shutdown
|
|
|
|
core.register_on_shutdown(persist_forceloaded_blocks)
|