-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.lua
145 lines (123 loc) · 3.95 KB
/
cache.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- Implementation of a reinforcement cache
local chunk_size = 16
local function coord_chunk(n)
return math.floor(n / chunk_size) * chunk_size
end
local function get_pos_chunk(pos)
return vector.new(
coord_chunk(pos.x),
coord_chunk(pos.y),
coord_chunk(pos.z)
)
end
--[[
cache:
chunk coords --> block coords --> reinforcement
]]--
local chunk_reinf_cache = {}
local cache_chunk_expiry = 10 -- debug, can be set to 60+
local function flush_reinf(reinf)
local reinf_value = reinf.value
local x, y, z = reinf.x, reinf.y, reinf.z
if reinf.new then
-- minetest.chat_send_all(" Registered: (" .. ptos(x, y, z) .. ")")
ctdb.remove_reinforcement(vector.new(x, y, z))
-- TODO: cleanup this remove-then-register hack
ctdb.register_reinforcement(
vector.new(x, y, z),
reinf.ctgroup_id,
reinf.material
)
reinf.new = false
end
if reinf_value < 1 then
-- minetest.chat_send_all(" Removed: (" .. ptos(x, y, z) .. ")")
ctdb.remove_reinforcement(vector.new(x, y, z))
else
-- minetest.chat_send_all(" Updated: (" .. ptos(x, y, z) .. ")")
ctdb.update_reinforcement(
vector.new(x, y, z),
reinf_value
)
end
end
function ct.try_flush_cache()
local current_time = os.time(os.date("!*t"))
for key, chunk in pairs(chunk_reinf_cache) do
if (chunk.time_added + cache_chunk_expiry) < current_time then
-- minetest.chat_send_all("Flushing chunk (" .. key .. ") to db:")
for _, reinf in pairs(chunk.reinforcements) do
flush_reinf(reinf)
end
chunk_reinf_cache[key] = nil
end
end
end
function ct.force_flush_cache()
for key, chunk in pairs(chunk_reinf_cache) do
-- minetest.chat_send_all("Flushing chunk (" .. key .. ") to db:")
for _, reinf in pairs(chunk.reinforcements) do
flush_reinf(reinf)
end
chunk_reinf_cache[key] = nil
end
end
function ct.chunk_ensure_cached(pos)
local vchunk_start = get_pos_chunk(pos)
local vchunk_end = vector.add(vchunk_start, chunk_size)
local chunk_reinf = chunk_reinf_cache[vtos(vchunk_start)]
if not chunk_reinf then
ctdb.get_reinforcements_for_cache(
chunk_reinf_cache,
vchunk_start,
vchunk_end
)
end
end
function ct.get_reinforcement(pos)
ct.try_flush_cache()
ct.chunk_ensure_cached(pos)
local vchunk_start = get_pos_chunk(pos)
local chunk_reinf = chunk_reinf_cache[vtos(vchunk_start)]
return chunk_reinf.reinforcements[vtos(pos)]
end
function ct.modify_reinforcement(pos, value)
ct.try_flush_cache()
ct.chunk_ensure_cached(pos)
local vchunk_start = get_pos_chunk(pos)
local chunk_reinf = chunk_reinf_cache[vtos(vchunk_start)]
if value < 1 then
-- We have to force a flush to get the DB in sync with the removal of the
-- node's entry in the cache.
--
-- If this isn't done, Citadella would reload the value from the DB, which
-- is almost certainly not coherent with the current state of the cache.
chunk_reinf.reinforcements[vtos(pos)].value = 0
ct.force_flush_cache()
-- Once the cache has been flushed, this reinforcement entry is removed.
chunk_reinf.reinforcements[vtos(pos)] = nil
else
chunk_reinf.reinforcements[vtos(pos)].value = value
end
return value
end
function ct.register_reinforcement(pos, ctgroup_id, item_name, resource_limit)
local reinf = ct.get_reinforcement(pos)
if not reinf then
local vchunk = get_pos_chunk(pos)
chunk_reinf_cache[vtos(vchunk)].reinforcements[vtos(pos)] = {
x = pos.x, y = pos.y, z = pos.z,
value = resource_limit,
material = item_name,
ctgroup_id = ctgroup_id,
new = true
}
end
end
local civmisc = minetest.get_modpath("civmisc")
if civmisc then
cleanup.register_cleanup_action("CITADEL CACHE FLUSH", function()
ct.force_flush_cache()
return true
end)
end