]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/forceloading.lua
Use a settings object for the main settings
[dragonfireclient.git] / builtin / game / forceloading.lua
1 -- Prevent anyone else accessing those functions
2 local forceload_block = core.forceload_block
3 local forceload_free_block = core.forceload_free_block
4 core.forceload_block = nil
5 core.forceload_free_block = nil
6
7 local blocks_forceloaded
8 local blocks_temploaded = {}
9 local total_forceloaded = 0
10
11 local BLOCKSIZE = core.MAP_BLOCKSIZE
12 local function get_blockpos(pos)
13         return {
14                 x = math.floor(pos.x/BLOCKSIZE),
15                 y = math.floor(pos.y/BLOCKSIZE),
16                 z = math.floor(pos.z/BLOCKSIZE)}
17 end
18
19 -- When we create/free a forceload, it's either transient or persistent. We want
20 -- to add to/remove from the table that corresponds to the type of forceload, but
21 -- we also need the other table because whether we forceload a block depends on
22 -- both tables.
23 -- This function returns the "primary" table we are adding to/removing from, and
24 -- the other table.
25 local function get_relevant_tables(transient)
26         if transient then
27                 return blocks_temploaded, blocks_forceloaded
28         else
29                 return blocks_forceloaded, blocks_temploaded
30         end
31 end
32
33 function core.forceload_block(pos, transient)
34         local blockpos = get_blockpos(pos)
35         local hash = core.hash_node_position(blockpos)
36         local relevant_table, other_table = get_relevant_tables(transient)
37         if relevant_table[hash] ~= nil then
38                 relevant_table[hash] = relevant_table[hash] + 1
39                 return true
40         elseif other_table[hash] ~= nil then
41                 relevant_table[hash] = 1
42         else
43                 if total_forceloaded >= (tonumber(core.settings:get("max_forceloaded_blocks")) or 16) then
44                         return false
45                 end
46                 total_forceloaded = total_forceloaded+1
47                 relevant_table[hash] = 1
48                 forceload_block(blockpos)
49                 return true
50         end
51 end
52
53 function core.forceload_free_block(pos, transient)
54         local blockpos = get_blockpos(pos)
55         local hash = core.hash_node_position(blockpos)
56         local relevant_table, other_table = get_relevant_tables(transient)
57         if relevant_table[hash] == nil then return end
58         if relevant_table[hash] > 1 then
59                 relevant_table[hash] = relevant_table[hash] - 1
60         elseif other_table[hash] ~= nil then
61                 relevant_table[hash] = nil
62         else
63                 total_forceloaded = total_forceloaded-1
64                 relevant_table[hash] = nil
65                 forceload_free_block(blockpos)
66         end
67 end
68
69 -- Keep the forceloaded areas after restart
70 local wpath = core.get_worldpath()
71 local function read_file(filename)
72         local f = io.open(filename, "r")
73         if f==nil then return {} end
74         local t = f:read("*all")
75         f:close()
76         if t=="" or t==nil then return {} end
77         return core.deserialize(t) or {}
78 end
79
80 local function write_file(filename, table)
81         local f = io.open(filename, "w")
82         f:write(core.serialize(table))
83         f:close()
84 end
85
86 blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
87 for _, __ in pairs(blocks_forceloaded) do
88         total_forceloaded = total_forceloaded + 1
89 end
90
91 core.after(5, function()
92         for hash, _ in pairs(blocks_forceloaded) do
93                 local blockpos = core.get_position_from_hash(hash)
94                 forceload_block(blockpos)
95         end
96 end)
97
98 core.register_on_shutdown(function()
99         write_file(wpath.."/force_loaded.txt", blocks_forceloaded)
100 end)