2021-06-03 23:30:04 +02:00
|
|
|
local worldpath = mapgen_rivers.world_data_path
|
2020-04-14 21:11:54 +02:00
|
|
|
|
2021-06-03 20:08:57 +02:00
|
|
|
function mapgen_rivers.load_map(filename, bytes, signed, size)
|
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
|
2021-06-26 11:49:26 +02:00
|
|
|
local sbyte = string.byte
|
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()
|
|
|
|
|
|
|
|
return map
|
|
|
|
end
|
|
|
|
|
2021-06-03 20:08:57 +02:00
|
|
|
function mapgen_rivers.write_map(filename, data, bytes)
|
|
|
|
local size = #data
|
|
|
|
local file = io.open(worldpath .. filename, 'wb')
|
|
|
|
local mfloor = math.floor
|
|
|
|
local schar = string.char
|
|
|
|
local upack = unpack
|
|
|
|
|
|
|
|
local bytelist = {}
|
|
|
|
for j=1, bytes do
|
|
|
|
bytelist[j] = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1, size do
|
|
|
|
local n = mfloor(data[i])
|
2021-06-05 11:24:28 +02:00
|
|
|
data[i] = n
|
2021-06-03 20:08:57 +02:00
|
|
|
for j=bytes, 2, -1 do
|
|
|
|
bytelist[j] = n % 256
|
|
|
|
n = mfloor(n / 256)
|
|
|
|
end
|
|
|
|
bytelist[1] = n % 256
|
|
|
|
|
|
|
|
file:write(schar(upack(bytelist)))
|
|
|
|
end
|
|
|
|
|
|
|
|
file:close()
|
|
|
|
end
|