forked from Mirrorlandia_minetest/minetest
Forceloading: Transient forceloads
Adds a flag to forceload_block which lets you turn off persistence for that forceload.
This commit is contained in:
parent
077b6cfa21
commit
2516c516bc
@ -5,6 +5,7 @@ core.forceload_block = nil
|
|||||||
core.forceload_free_block = nil
|
core.forceload_free_block = nil
|
||||||
|
|
||||||
local blocks_forceloaded
|
local blocks_forceloaded
|
||||||
|
local blocks_temploaded = {}
|
||||||
local total_forceloaded = 0
|
local total_forceloaded = 0
|
||||||
|
|
||||||
local BLOCKSIZE = core.MAP_BLOCKSIZE
|
local BLOCKSIZE = core.MAP_BLOCKSIZE
|
||||||
@ -15,32 +16,52 @@ local function get_blockpos(pos)
|
|||||||
z = math.floor(pos.z/BLOCKSIZE)}
|
z = math.floor(pos.z/BLOCKSIZE)}
|
||||||
end
|
end
|
||||||
|
|
||||||
function core.forceload_block(pos)
|
-- 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)
|
||||||
local blockpos = get_blockpos(pos)
|
local blockpos = get_blockpos(pos)
|
||||||
local hash = core.hash_node_position(blockpos)
|
local hash = core.hash_node_position(blockpos)
|
||||||
if blocks_forceloaded[hash] ~= nil then
|
local relevant_table, other_table = get_relevant_tables(transient)
|
||||||
blocks_forceloaded[hash] = blocks_forceloaded[hash] + 1
|
if relevant_table[hash] ~= nil then
|
||||||
|
relevant_table[hash] = relevant_table[hash] + 1
|
||||||
return true
|
return true
|
||||||
|
elseif other_table[hash] ~= nil then
|
||||||
|
relevant_table[hash] = 1
|
||||||
else
|
else
|
||||||
if total_forceloaded >= (tonumber(core.setting_get("max_forceloaded_blocks")) or 16) then
|
if total_forceloaded >= (tonumber(core.setting_get("max_forceloaded_blocks")) or 16) then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
total_forceloaded = total_forceloaded+1
|
total_forceloaded = total_forceloaded+1
|
||||||
blocks_forceloaded[hash] = 1
|
relevant_table[hash] = 1
|
||||||
forceload_block(blockpos)
|
forceload_block(blockpos)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function core.forceload_free_block(pos)
|
function core.forceload_free_block(pos, transient)
|
||||||
local blockpos = get_blockpos(pos)
|
local blockpos = get_blockpos(pos)
|
||||||
local hash = core.hash_node_position(blockpos)
|
local hash = core.hash_node_position(blockpos)
|
||||||
if blocks_forceloaded[hash] == nil then return end
|
local relevant_table, other_table = get_relevant_tables(transient)
|
||||||
if blocks_forceloaded[hash] > 1 then
|
if relevant_table[hash] == nil then return end
|
||||||
blocks_forceloaded[hash] = blocks_forceloaded[hash] - 1
|
if relevant_table[hash] > 1 then
|
||||||
|
relevant_table[hash] = relevant_table[hash] - 1
|
||||||
|
elseif other_table[hash] ~= nil then
|
||||||
|
relevant_table[hash] = nil
|
||||||
else
|
else
|
||||||
total_forceloaded = total_forceloaded-1
|
total_forceloaded = total_forceloaded-1
|
||||||
blocks_forceloaded[hash] = nil
|
relevant_table[hash] = nil
|
||||||
forceload_free_block(blockpos)
|
forceload_free_block(blockpos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2622,13 +2622,17 @@ These functions return the leftover itemstack.
|
|||||||
the creative mode setting, and checks for "sneak" to set the `invert_wall`
|
the creative mode setting, and checks for "sneak" to set the `invert_wall`
|
||||||
parameter.
|
parameter.
|
||||||
|
|
||||||
* `minetest.forceload_block(pos)`
|
* `minetest.forceload_block(pos[, transient])`
|
||||||
* forceloads the position `pos`.
|
* forceloads the position `pos`.
|
||||||
* returns `true` if area could be forceloaded
|
* returns `true` if area could be forceloaded
|
||||||
* Please note that forceloaded areas are saved when the server restarts.
|
* If `transient` is `false` or absent, the forceload will be persistent
|
||||||
|
(saved between server runs). If `true`, the forceload will be transient
|
||||||
|
(not saved between server runs).
|
||||||
|
|
||||||
* `minetest.forceload_free_block(pos)`
|
* `minetest.forceload_free_block(pos[, transient])`
|
||||||
* stops forceloading the position `pos`
|
* stops forceloading the position `pos`
|
||||||
|
* If `transient` is `false` or absent, frees a persistent forceload.
|
||||||
|
If `true`, frees a transient forceload.
|
||||||
|
|
||||||
* `minetest.request_insecure_environment()`: returns an environment containing
|
* `minetest.request_insecure_environment()`: returns an environment containing
|
||||||
insecure functions if the calling mod has been listed as trusted in the
|
insecure functions if the calling mod has been listed as trusted in the
|
||||||
|
Loading…
Reference in New Issue
Block a user