From 564cee346afcf94339472e75995394f0363a5543 Mon Sep 17 00:00:00 2001 From: Christopher Head Date: Sat, 20 Aug 2016 21:48:18 -0700 Subject: Use VoxelManipulators for get_node_force. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A VoxelManipulator, when asked to read a mapblock, in addition to making that mapblock available to the caller, also pulls it into the server’s map cache, thus making get_node calls in the immediate future succeed. This has the dual advantages that not every mapblock containing a Mesecons circuit need remain loaded at all times (rather mapblocks can be loaded on demand as signals are sent), and that the server need not bother running ABMs and ticking entities within those mapblocks that are loaded due to Mesecons signalling. --- mesecons/util.lua | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/mesecons/util.lua b/mesecons/util.lua index d95f216..502b269 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -236,43 +236,25 @@ local function unhash_blockpos(hash) return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE) end -mesecon.forceloaded_blocks = {} - -- get node and force-load area function mesecon.get_node_force(pos) - local hash = hash_blockpos(pos) - - if mesecon.forceloaded_blocks[hash] == nil then - -- if no more forceload spaces are available, try again next time - if minetest.forceload_block(pos) then - mesecon.forceloaded_blocks[hash] = 0 - end - else - mesecon.forceloaded_blocks[hash] = 0 + local node = minetest.get_node_or_nil(pos) + if node == nil then + -- Node is not currently loaded; use a VoxelManipulator to prime + -- the mapblock cache and try again. + minetest.get_voxel_manip(pos, pos) + node = minetest.get_node_or_nil(pos) end - - return minetest.get_node_or_nil(pos) + return node end -minetest.register_globalstep(function (dtime) - for hash, time in pairs(mesecon.forceloaded_blocks) do - -- unload forceloaded blocks after 10 minutes without usage - if (time > mesecon.setting("forceload_timeout", 600)) then - minetest.forceload_free_block(unhash_blockpos(hash)) - mesecon.forceloaded_blocks[hash] = nil - else - mesecon.forceloaded_blocks[hash] = time + dtime - end - end -end) - --- Store and read the forceloaded blocks to / from a file --- so that those blocks are remembered when the game --- is restarted -mesecon.forceloaded_blocks = mesecon.file2table("mesecon_forceloaded") -minetest.register_on_shutdown(function() - mesecon.table2file("mesecon_forceloaded", mesecon.forceloaded_blocks) -end) +-- Un-forceload any forceloaded mapblocks from older versions of Mesecons which +-- used forceloading instead of VoxelManipulators. +local old_forceloaded_blocks = mesecon.file2table("mesecon_forceloaded") +for hash, _ in pairs(old_forceloaded_blocks) do + minetest.forceload_free_block(unhash_blockpos(hash)) +end +os.remove(wpath..DIR_DELIM.."mesecon_forceloaded") -- Autoconnect Hooks -- Nodes like conductors may change their appearance and their connection rules -- cgit v1.2.3