Add forceloading
This commit is contained in:
parent
0d4128330e
commit
67d99a67be
|
@ -34,3 +34,4 @@ dofile(modpath.."/falling.lua")
|
||||||
dofile(modpath.."/features.lua")
|
dofile(modpath.."/features.lua")
|
||||||
dofile(modpath.."/voxelarea.lua")
|
dofile(modpath.."/voxelarea.lua")
|
||||||
dofile(modpath.."/vector.lua")
|
dofile(modpath.."/vector.lua")
|
||||||
|
dofile(modpath.."/forceloading.lua")
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
-- Prevent anyone else accessing those functions
|
||||||
|
local forceload_block = minetest.forceload_block
|
||||||
|
local forceload_free_block = minetest.forceload_free_block
|
||||||
|
minetest.forceload_block = nil
|
||||||
|
minetest.forceload_free_block = nil
|
||||||
|
|
||||||
|
local blocks_forceloaded
|
||||||
|
local total_forceloaded = 0
|
||||||
|
|
||||||
|
local BLOCKSIZE = 16
|
||||||
|
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
|
||||||
|
|
||||||
|
function minetest.forceload_block(pos)
|
||||||
|
local blockpos = get_blockpos(pos)
|
||||||
|
local hash = minetest.hash_node_position(blockpos)
|
||||||
|
if blocks_forceloaded[hash] ~= nil then
|
||||||
|
blocks_forceloaded[hash] = blocks_forceloaded[hash] + 1
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
if total_forceloaded >= (minetest.setting_get("max_forceloaded_blocks") or 16) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
total_forceloaded = total_forceloaded+1
|
||||||
|
blocks_forceloaded[hash] = 1
|
||||||
|
forceload_block(blockpos)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function minetest.forceload_free_block(pos)
|
||||||
|
local blockpos = get_blockpos(pos)
|
||||||
|
local hash = minetest.hash_node_position(blockpos)
|
||||||
|
if blocks_forceloaded[hash] == nil then return end
|
||||||
|
if blocks_forceloaded[hash] > 1 then
|
||||||
|
blocks_forceloaded[hash] = blocks_forceloaded[hash] - 1
|
||||||
|
else
|
||||||
|
total_forceloaded = total_forceloaded-1
|
||||||
|
blocks_forceloaded[hash] = nil
|
||||||
|
forceload_free_block(blockpos)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Keep the forceloaded areas after restart
|
||||||
|
local wpath = minetest.get_worldpath()
|
||||||
|
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
|
||||||
|
return minetest.deserialize(t)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function write_file(filename, table)
|
||||||
|
local f = io.open(filename, "w")
|
||||||
|
f:write(minetest.serialize(table))
|
||||||
|
f:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
|
||||||
|
for _, __ in pairs(blocks_forceloaded) do
|
||||||
|
total_forceloaded = total_forceloaded + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.after(5, function()
|
||||||
|
for hash, _ in pairs(blocks_forceloaded) do
|
||||||
|
local blockpos = minetest.get_position_from_hash(hash)
|
||||||
|
forceload_block(blockpos)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
minetest.register_on_shutdown(function()
|
||||||
|
write_file(wpath.."/force_loaded.txt", blocks_forceloaded)
|
||||||
|
end)
|
|
@ -64,6 +64,16 @@ function minetest.hash_node_position(pos)
|
||||||
return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
|
return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function minetest.get_position_from_hash(hash)
|
||||||
|
local pos = {}
|
||||||
|
pos.x = (hash%65536) - 32768
|
||||||
|
hash = math.floor(hash/65536)
|
||||||
|
pos.y = (hash%65536) - 32768
|
||||||
|
hash = math.floor(hash/65536)
|
||||||
|
pos.z = (hash%65536) - 32768
|
||||||
|
return pos
|
||||||
|
end
|
||||||
|
|
||||||
function minetest.get_item_group(name, group)
|
function minetest.get_item_group(name, group)
|
||||||
if not minetest.registered_items[name] or not
|
if not minetest.registered_items[name] or not
|
||||||
minetest.registered_items[name].groups[group] then
|
minetest.registered_items[name].groups[group] then
|
||||||
|
|
Loading…
Reference in New Issue