mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2024-11-14 23:00:22 +01:00
108 lines
2.4 KiB
Lua
108 lines
2.4 KiB
Lua
local datapath = minetest.get_worldpath() .. "/river_data/"
|
|
|
|
local floor = math.floor
|
|
local sbyte = string.byte
|
|
local unpk = unpack
|
|
|
|
local load_map
|
|
local use_interactive_loader
|
|
if minetest.settings:has("mapgen_rivers_use_interactive_loader") then
|
|
use_interactive_loader = minetest.settings:get_bool("mapgen_rivers_use_interactive_loader")
|
|
else
|
|
use_interactive_loader = not minetest.settings:get_bool("mapgen_rivers_load_all")
|
|
end
|
|
|
|
if use_interactive_loader then
|
|
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,
|
|
}
|
|
|
|
load_map = function(filename, bytes, signed, size, converter)
|
|
local file = io.open(datapath .. filename, 'rb')
|
|
if file then
|
|
minetest.register_on_shutdown(function()
|
|
file:close()
|
|
end)
|
|
converter = converter or false
|
|
return setmetatable({file=file, bytes=bytes, signed=signed, size=size, conv=converter}, loader_mt)
|
|
end
|
|
end
|
|
|
|
else
|
|
|
|
load_map = function(filename, bytes, signed, size, converter)
|
|
local file = io.open(datapath .. filename, 'rb')
|
|
local data = file:read('*all')
|
|
if #data < bytes*size then
|
|
data = minetest.decompress(data)
|
|
end
|
|
|
|
local map = {}
|
|
|
|
for i=1, size do
|
|
local i0 = (i-1)*bytes+1
|
|
local i1 = i*bytes
|
|
local elements = {data:byte(i0, i1)}
|
|
local n = sbyte(data, i0)
|
|
if signed and n >= 128 then
|
|
n = n - 256
|
|
end
|
|
|
|
for j=1, bytes-1 do
|
|
n = n*256 + sbyte(data, i0+j)
|
|
end
|
|
|
|
map[i] = n
|
|
end
|
|
file:close()
|
|
|
|
if converter then
|
|
for i=1, size do
|
|
map[i] = converter(map[i])
|
|
end
|
|
end
|
|
|
|
return map
|
|
end
|
|
end
|
|
|
|
local sfile = io.open(datapath..'size', 'r')
|
|
assert(sfile)
|
|
local X, Z = tonumber(sfile:read('*l')), tonumber(sfile:read('*l'))
|
|
sfile:close()
|
|
|
|
local function offset_converter(o)
|
|
return (o + 0.5) * (1/256)
|
|
end
|
|
|
|
mapgen_rivers.grid = {
|
|
size = {x=X, y=Z},
|
|
dem = load_map('dem', 2, true, X*Z),
|
|
lakes = load_map('lakes', 2, true, X*Z),
|
|
dirs = load_map('dirs', 1, false, X*Z),
|
|
rivers = load_map('rivers', 4, false, X*Z),
|
|
|
|
offset_x = load_map('offset_x', 1, true, X*Z, offset_converter),
|
|
offset_y = load_map('offset_y', 1, true, X*Z, offset_converter);
|
|
}
|