Skip to content

Commit

Permalink
Only load barrier removal data for the current map
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed Mar 12, 2024
1 parent 206a59b commit cac8fb1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 41 deletions.
2 changes: 1 addition & 1 deletion mods/ctf/ctf_core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ctf_core = {
settings = {
-- server_mode = minetest.settings:get("ctf_server_mode") or "play",
server_mode = minetest.settings:get_bool("creative_mode", false) and "mapedit" or "play",
low_ram_mode = minetest.settings:get("ctf_low_ram_mode") or false,
low_ram_mode = minetest.settings:get("ctf_low_ram_mode") == "true" or false,
}
}

Expand Down
32 changes: 26 additions & 6 deletions mods/ctf/ctf_map/map_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ function ctf_map.place_map(mapmeta, callback)
local dirname = mapmeta.dirname
local schempath = ctf_map.maps_dir .. dirname .. "/map.mts"

local barrier_data = mapmeta.barriers and mapmeta.barriers()

ctf_map.emerge_with_callbacks(nil, mapmeta.pos1, mapmeta.pos2, function(ctx)
local rotation = (mapmeta.rotation and mapmeta.rotation ~= "z") and "90" or "0"
local res = minetest.place_schematic(mapmeta.pos1, schempath, rotation, {["ctf_map:chest"] = "air"})

minetest.log("action", string.format(
"Placed map %s in %.2fs", dirname, (minetest.get_us_time() - ctx.start_time) / 1000000
"Placed map %s in %.2fs", dirname, (minetest.get_us_time() - ctx.start_time) / 1e6
))

for name, def in pairs(mapmeta.teams) do
Expand Down Expand Up @@ -46,6 +48,10 @@ function ctf_map.place_map(mapmeta, callback)

ctf_map.current_map = mapmeta

if barrier_data then
ctf_map.current_map.barrier_data = barrier_data
end

callback()
end)
end
Expand All @@ -61,7 +67,7 @@ local ID_WATER = minetest.get_content_id("default:water_source")
---@param mapmeta table Map meta table
---@param callback function
function ctf_map.remove_barrier(mapmeta, callback)
if not mapmeta.barriers then
if not mapmeta.barrier_data then
minetest.log("action", "Clearing barriers using mapmeta.barrier_area")

local pos1, pos2 = mapmeta.barrier_area.pos1, mapmeta.barrier_area.pos2
Expand Down Expand Up @@ -94,14 +100,26 @@ function ctf_map.remove_barrier(mapmeta, callback)
vm2:update_liquids()
end)
else
minetest.log("action", "Clearing barriers using barriers.data")

local i = 0
for _, barrier_area in pairs(mapmeta.barriers) do
for _, barrier_area in pairs(mapmeta.barrier_data) do
minetest.after(i, function()
local vm = VoxelManip()
vm:read_from_map(barrier_area.pos1, barrier_area.pos2)

local data = vm:get_data()
assert(#data == barrier_area.max)

if #data ~= barrier_area.max then
-- minetest.log(dump(mapmeta.barrier_data)) -- Used for debugging issues
minetest.log("error", "Potential issue with barriers.data. Aborting... | " ..
"Debug: "..dump(#data)..", "..dump(barrier_area.max))

mapmeta.barrier_data = nil
ctf_map.remove_barrier(mapmeta, callback)
return
end

for idx in pairs(data) do
data[idx] = barrier_area.reps[idx] or ID_IGNORE
end
Expand All @@ -110,13 +128,15 @@ function ctf_map.remove_barrier(mapmeta, callback)
vm:write_to_map(false)
end)

i = i + 0.04
i = i + 0.05
end

minetest.after(i - 0.04, function()
minetest.after(i - 0.05, function()
local vm = VoxelManip(mapmeta.pos1, mapmeta.pos2)
vm:update_liquids()

mapmeta.barrier_data = nil -- Contains a large amount of data, free it up now that it's not needed

callback()
end)

Expand Down
74 changes: 40 additions & 34 deletions mods/ctf/ctf_map/map_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ local function calc_flag_center(map)
return flag_center
end

local function connect_barriers_file(map_name, offset, barriers_filepath)
return function()
local f, err = io.open(barriers_filepath, "rb")

if (ctf_core.settings.server_mode ~= "mapedit" and assert(f, err)) or f then
local barriers = f:read("*all")

f:close()

assert(barriers and barriers ~= "")

barriers = minetest.deserialize(minetest.decompress(barriers, "deflate"))

if barriers then
for _, barrier_area in pairs(barriers) do
barrier_area.pos1 = vector.add(barrier_area.pos1, offset)
barrier_area.pos2 = vector.add(barrier_area.pos2, offset)

for i = 1, barrier_area.max do
if not barrier_area.reps[i] then
barrier_area.reps[i] = minetest.CONTENT_IGNORE
else
barrier_area.reps[i] = minetest.get_content_id(barrier_area.reps[i])
end
end
end

return barriers
else
minetest.log("error", "Map "..map_name.." has a corrupted barriers file. Re-save map to fix")
end
else
minetest.log("error", "Map "..map_name.." is missing its barriers file. Re-save map to fix")
end
end
end

function ctf_map.load_map_meta(idx, dirname)
local meta = Settings(ctf_map.maps_dir .. dirname .. "/map.conf")

Expand Down Expand Up @@ -134,7 +171,7 @@ function ctf_map.load_map_meta(idx, dirname)
offset.y = -size.y/2

map = {
map_version = CURRENT_MAP_VERSION,
map_version = tonumber(meta:get("map_version") or "0"),
pos1 = offset,
pos2 = vector.add(offset, size),
offset = offset,
Expand All @@ -160,39 +197,8 @@ function ctf_map.load_map_meta(idx, dirname)
game_modes = minetest.deserialize(meta:get("game_modes")),
enable_shadows = tonumber(meta:get("enable_shadows") or "0.26"),
}
if tonumber(meta:get("map_version")) > 2 and not ctf_core.settings.low_ram_mode then
local f, err = io.open(ctf_map.maps_dir .. dirname .. "/barriers.data", "rb")

if (ctf_core.settings.server_mode ~= "mapedit" and assert(f, err)) or f then
local barriers = f:read("*all")

f:close()

assert(barriers and barriers ~= "")

barriers = minetest.deserialize(minetest.decompress(barriers, "deflate"))

if barriers then
for _, barrier_area in pairs(barriers) do
barrier_area.pos1 = vector.add(barrier_area.pos1, offset)
barrier_area.pos2 = vector.add(barrier_area.pos2, offset)

for i = 1, barrier_area.max do
if not barrier_area.reps[i] then
barrier_area.reps[i] = minetest.CONTENT_IGNORE
else
barrier_area.reps[i] = minetest.get_content_id(barrier_area.reps[i])
end
end
end

map.barriers = barriers
else
minetest.log("error", "Map "..dirname.." has a corrupted barriers file. Re-save map to fix")
end
else
minetest.log("error", "Map "..dirname.." is missing its barriers file. Re-save map to fix")
end
if tonumber(meta:get("map_version")) >= 3 and not ctf_core.settings.low_ram_mode then
map.barriers = connect_barriers_file(dirname, offset, ctf_map.maps_dir .. dirname .. "/barriers.data")
end

for id, def in pairs(map.chests) do
Expand Down

0 comments on commit cac8fb1

Please sign in to comment.