Re-organize grid management code for less dependance between files

Remove gridio.lua and move its function to appropriate files
This commit is contained in:
Gaël C
2024-02-01 19:30:07 +01:00
parent 2cf3b19167
commit 146f009684
4 changed files with 98 additions and 127 deletions

View File

@ -1,6 +1,8 @@
-- Generate the grid using terrainlib_lua
-- Only called on first mapgen, if there is no grid yet
-- Constants
local EvolutionModel = dofile(mapgen_rivers.modpath .. '/terrainlib_lua/erosion.lua')
local twist = dofile(mapgen_rivers.modpath .. '/terrainlib_lua/twist.lua')
@ -52,12 +54,10 @@ local function margin(dem, width, elev)
end
end
-- Generate the grid
-- Generate grid
minetest.log("action", '[mapgen_rivers] Generating grid, this may take a while...')
local grid = {size={x=X, y=Y}}
if X*Y > 4e6 then
minetest.log("warning", "[mapgen_rivers] You are going to generate a very large grid (>4M nodes). If you experience problems, you should increase blocksize or reduce map size.")
end
@ -103,19 +103,48 @@ model:flow()
local mfloor = math.floor
local mmin, mmax = math.min, math.max
local unpk, schar = unpack, string.char
local offset_x, offset_y = twist(model.dirs, model.rivers, 5)
for i=1, X*Y do
offset_x[i] = mmin(mmax(offset_x[i]*256, -128), 127)
offset_y[i] = mmin(mmax(offset_y[i]*256, -128), 127)
end
grid.dem = model.dem
grid.lakes = model.lakes
grid.dirs = model.dirs
grid.rivers = model.rivers
grid.offset_x = offset_x
grid.offset_y = offset_y
-- Write data
collectgarbage()
local datapath = mapgen_rivers.world_data_path
minetest.mkdir(datapath)
return grid
local sfile = io.open(datapath .. 'size', "w")
sfile:write(X..'\n'..Y)
sfile:close()
local function write_file(filename, data, bytes)
local file = io.open(datapath .. filename, 'wb')
local bytelist = {}
for j=1, bytes do
bytelist[j] = 0
end
for i=1, #data do
local n = mfloor(data[i])
data[i] = n
for j=bytes, 2, -1 do
bytelist[j] = n % 256
n = mfloor(n / 256)
end
bytelist[1] = n % 256
file:write(schar(unpk(bytelist)))
end
file:close()
end
write_file('dem', model.dem, 2)
write_file('lakes', model.lakes, 2)
write_file('dirs', model.dirs, 1)
write_file('rivers', model.rivers, 4)
write_file('offset_x', offset_x, 1)
write_file('offset_y', offset_y, 1)