diff options
author | est31 <MTest31@outlook.com> | 2016-12-22 23:16:00 +0100 |
---|---|---|
committer | est31 <MTest31@outlook.com> | 2016-12-22 23:16:00 +0100 |
commit | 81d56b94919dceb7b2e51d70b21a7ca22f852bd5 (patch) | |
tree | 1e9ef1be1b3295a8673d6e4f0bdeb4c2d3a6015f /builtin/game/forceloading.lua | |
parent | 8077612dcb48221281e726a60eb97bf73fde462b (diff) | |
parent | 231ac33d34dfaaddf292c5f31b1eae43eeefba2d (diff) | |
download | minetest-81d56b94919dceb7b2e51d70b21a7ca22f852bd5.tar.gz minetest-81d56b94919dceb7b2e51d70b21a7ca22f852bd5.tar.bz2 minetest-81d56b94919dceb7b2e51d70b21a7ca22f852bd5.zip |
Merge 0.4.15 changes into stable-0.4
0.4.15 release!
Diffstat (limited to 'builtin/game/forceloading.lua')
-rw-r--r-- | builtin/game/forceloading.lua | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/builtin/game/forceloading.lua b/builtin/game/forceloading.lua index 8c9fbf512..8a05de36c 100644 --- a/builtin/game/forceloading.lua +++ b/builtin/game/forceloading.lua @@ -5,9 +5,10 @@ core.forceload_block = nil core.forceload_free_block = nil local blocks_forceloaded +local blocks_temploaded = {} local total_forceloaded = 0 -local BLOCKSIZE = 16 +local BLOCKSIZE = core.MAP_BLOCKSIZE local function get_blockpos(pos) return { x = math.floor(pos.x/BLOCKSIZE), @@ -15,32 +16,52 @@ local function get_blockpos(pos) z = math.floor(pos.z/BLOCKSIZE)} 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 hash = core.hash_node_position(blockpos) - if blocks_forceloaded[hash] ~= nil then - blocks_forceloaded[hash] = blocks_forceloaded[hash] + 1 + local relevant_table, other_table = get_relevant_tables(transient) + if relevant_table[hash] ~= nil then + relevant_table[hash] = relevant_table[hash] + 1 return true + elseif other_table[hash] ~= nil then + relevant_table[hash] = 1 else if total_forceloaded >= (tonumber(core.setting_get("max_forceloaded_blocks")) or 16) then return false end total_forceloaded = total_forceloaded+1 - blocks_forceloaded[hash] = 1 + relevant_table[hash] = 1 forceload_block(blockpos) return true end end -function core.forceload_free_block(pos) +function core.forceload_free_block(pos, transient) local blockpos = get_blockpos(pos) local hash = core.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 + 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 else total_forceloaded = total_forceloaded-1 - blocks_forceloaded[hash] = nil + relevant_table[hash] = nil forceload_free_block(blockpos) end end |