]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/forceloading.lua
Put torch/signlike node on floor if no paramtype2 (#11074)
[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 -- true, if the forceloaded blocks got changed (flag for persistence on-disk)
12 local forceload_blocks_changed = false
13
14 local BLOCKSIZE = core.MAP_BLOCKSIZE
15 local function get_blockpos(pos)
16         return {
17                 x = math.floor(pos.x/BLOCKSIZE),
18                 y = math.floor(pos.y/BLOCKSIZE),
19                 z = math.floor(pos.z/BLOCKSIZE)}
20 end
21
22 -- When we create/free a forceload, it's either transient or persistent. We want
23 -- to add to/remove from the table that corresponds to the type of forceload, but
24 -- we also need the other table because whether we forceload a block depends on
25 -- both tables.
26 -- This function returns the "primary" table we are adding to/removing from, and
27 -- the other table.
28 local function get_relevant_tables(transient)
29         if transient then
30                 return blocks_temploaded, blocks_forceloaded
31         else
32                 return blocks_forceloaded, blocks_temploaded
33         end
34 end
35
36 function core.forceload_block(pos, transient)
37         -- set changed flag
38         forceload_blocks_changed = true
39
40         local blockpos = get_blockpos(pos)
41         local hash = core.hash_node_position(blockpos)
42         local relevant_table, other_table = get_relevant_tables(transient)
43         if relevant_table[hash] ~= nil then
44                 relevant_table[hash] = relevant_table[hash] + 1
45                 return true
46         elseif other_table[hash] ~= nil then
47                 relevant_table[hash] = 1
48         else
49                 if total_forceloaded >= (tonumber(core.settings:get("max_forceloaded_blocks")) or 16) then
50                         return false
51                 end
52                 total_forceloaded = total_forceloaded+1
53                 relevant_table[hash] = 1
54                 forceload_block(blockpos)
55                 return true
56         end
57 end
58
59 function core.forceload_free_block(pos, transient)
60         -- set changed flag
61         forceload_blocks_changed = true
62
63         local blockpos = get_blockpos(pos)
64         local hash = core.hash_node_position(blockpos)
65         local relevant_table, other_table = get_relevant_tables(transient)
66         if relevant_table[hash] == nil then return end
67         if relevant_table[hash] > 1 then
68                 relevant_table[hash] = relevant_table[hash] - 1
69         elseif other_table[hash] ~= nil then
70                 relevant_table[hash] = nil
71         else
72                 total_forceloaded = total_forceloaded-1
73                 relevant_table[hash] = nil
74                 forceload_free_block(blockpos)
75         end
76 end
77
78 -- Keep the forceloaded areas after restart
79 local wpath = core.get_worldpath()
80 local function read_file(filename)
81         local f = io.open(filename, "r")
82         if f==nil then return {} end
83         local t = f:read("*all")
84         f:close()
85         if t=="" or t==nil then return {} end
86         return core.deserialize(t) or {}
87 end
88
89 local function write_file(filename, table)
90         local f = io.open(filename, "w")
91         f:write(core.serialize(table))
92         f:close()
93 end
94
95 blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
96 for _, __ in pairs(blocks_forceloaded) do
97         total_forceloaded = total_forceloaded + 1
98 end
99
100 core.after(5, function()
101         for hash, _ in pairs(blocks_forceloaded) do
102                 local blockpos = core.get_position_from_hash(hash)
103                 forceload_block(blockpos)
104         end
105 end)
106
107 -- persists the currently forceloaded blocks to disk
108 local function persist_forceloaded_blocks()
109         write_file(wpath.."/force_loaded.txt", blocks_forceloaded)
110 end
111
112 -- periodical forceload persistence
113 local function periodically_persist_forceloaded_blocks()
114
115         -- only persist if the blocks actually changed
116         if forceload_blocks_changed then
117                 persist_forceloaded_blocks()
118
119                 -- reset changed flag
120                 forceload_blocks_changed = false
121         end
122
123         -- recheck after some time
124         core.after(10, periodically_persist_forceloaded_blocks)
125 end
126
127 -- persist periodically
128 core.after(5, periodically_persist_forceloaded_blocks)
129
130 -- persist on shutdown
131 core.register_on_shutdown(persist_forceloaded_blocks)