2022-01-26 11:25:17 +01:00
|
|
|
-- Input and output functions for grid maps
|
|
|
|
|
2021-06-03 23:30:04 +02:00
|
|
|
local worldpath = mapgen_rivers.world_data_path
|
2020-04-14 21:11:54 +02:00
|
|
|
|
2022-01-03 16:20:51 +01:00
|
|
|
local floor = math.floor
|
|
|
|
local sbyte, schar = string.byte, string.char
|
|
|
|
local unpk = unpack
|
|
|
|
|
2022-01-25 19:09:24 +01:00
|
|
|
-- Loading files
|
|
|
|
local function load_full_map(filename, bytes, signed, size, converter)
|
2020-11-14 17:31:36 +01:00
|
|
|
local file = io.open(worldpath .. filename, 'rb')
|
2020-04-09 21:13:38 +02:00
|
|
|
local data = file:read('*all')
|
2020-04-13 15:59:34 +02:00
|
|
|
if #data < bytes*size then
|
|
|
|
data = minetest.decompress(data)
|
|
|
|
end
|
2020-04-09 21:13:38 +02:00
|
|
|
|
|
|
|
local map = {}
|
|
|
|
|
|
|
|
for i=1, size do
|
2021-06-26 11:49:26 +02:00
|
|
|
local i0 = (i-1)*bytes+1
|
2020-04-09 21:13:38 +02:00
|
|
|
local elements = {data:byte(i0, i1)}
|
2021-06-26 11:49:26 +02:00
|
|
|
local n = sbyte(data, i0)
|
2020-04-09 21:13:38 +02:00
|
|
|
if signed and n >= 128 then
|
|
|
|
n = n - 256
|
|
|
|
end
|
|
|
|
|
2021-06-26 11:49:26 +02:00
|
|
|
for j=1, bytes-1 do
|
|
|
|
n = n*256 + sbyte(data, i0+j)
|
2020-04-09 21:13:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
map[i] = n
|
|
|
|
end
|
|
|
|
file:close()
|
|
|
|
|
2021-06-26 11:50:52 +02:00
|
|
|
if converter then
|
|
|
|
for i=1, size do
|
|
|
|
map[i] = converter(map[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-09 21:13:38 +02:00
|
|
|
return map
|
|
|
|
end
|
|
|
|
|
2021-06-26 11:50:52 +02:00
|
|
|
local loader_mt = {
|
|
|
|
__index = function(loader, i)
|
|
|
|
local file = loader.file
|
|
|
|
local bytes = loader.bytes
|
|
|
|
file:seek('set', (i-1)*bytes)
|
|
|
|
local strnum = file:read(bytes)
|
|
|
|
|
|
|
|
local n = sbyte(strnum, 1)
|
|
|
|
if loader.signed and n >= 128 then
|
|
|
|
n = n - 256
|
|
|
|
end
|
|
|
|
|
|
|
|
for j=2, bytes do
|
|
|
|
n = n*256 + sbyte(strnum, j)
|
|
|
|
end
|
|
|
|
|
|
|
|
if loader.conv then
|
|
|
|
n = loader.conv(n)
|
|
|
|
end
|
|
|
|
loader[i] = n
|
|
|
|
return n
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2022-01-25 19:09:24 +01:00
|
|
|
local function interactive_loader(filename, bytes, signed, size, converter)
|
2021-06-26 11:50:52 +02:00
|
|
|
local file = io.open(worldpath .. filename, 'rb')
|
|
|
|
if file then
|
|
|
|
converter = converter or false
|
|
|
|
return setmetatable({file=file, bytes=bytes, signed=signed, size=size, conv=converter}, loader_mt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-25 19:09:24 +01:00
|
|
|
local load_methods = {
|
|
|
|
full = load_full_map,
|
|
|
|
interactive = interactive_loader,
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapgen_rivers.load_file(...)
|
|
|
|
local load_method = mapgen_rivers.settings.load_method
|
|
|
|
local load_func = load_methods[load_method]
|
|
|
|
if load_func then
|
|
|
|
return load_func(...)
|
|
|
|
else
|
|
|
|
minetest.log("error", ("[mapgen_rivers] Unknown load method %s"):format(load_method))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Writing files
|
|
|
|
function mapgen_rivers.write_file(filename, data, bytes)
|
2021-07-24 18:55:13 +02:00
|
|
|
local size = #data
|
|
|
|
local file = io.open(worldpath .. filename, 'wb')
|
|
|
|
|
|
|
|
local bytelist = {}
|
|
|
|
for j=1, bytes do
|
|
|
|
bytelist[j] = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1, size do
|
2022-01-03 16:20:51 +01:00
|
|
|
local n = floor(data[i])
|
2021-07-24 18:55:13 +02:00
|
|
|
data[i] = n
|
|
|
|
for j=bytes, 2, -1 do
|
|
|
|
bytelist[j] = n % 256
|
2022-01-03 16:20:51 +01:00
|
|
|
n = floor(n / 256)
|
2021-07-24 18:55:13 +02:00
|
|
|
end
|
|
|
|
bytelist[1] = n % 256
|
|
|
|
|
2022-01-03 16:20:51 +01:00
|
|
|
file:write(schar(unpk(bytelist)))
|
2021-07-24 18:55:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
file:close()
|
2021-06-03 20:08:57 +02:00
|
|
|
end
|