Minetest-WorldEditAdditions/worldeditadditions/lib/erode/erode.lua

62 lines
1.9 KiB
Lua
Raw Normal View History

local wea = worldeditadditions
2022-09-19 00:16:22 +00:00
local wea_c = worldeditadditions_core
wea.erode = {}
dofile(wea.modpath.."/lib/erode/snowballs.lua")
dofile(wea.modpath.."/lib/erode/river.lua")
function wea.erode.run(pos1, pos2, algorithm, params)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
local data = manip:get_data()
local heightmap_size = {
z = (pos2.z - pos1.z) + 1,
x = (pos2.x - pos1.x) + 1
}
local region_height = (pos2.y - pos1.y) + 1
2022-09-19 00:16:22 +00:00
local heightmap = wea_c.terrain.make_heightmap(pos1, pos2, manip, area, data)
local heightmap_eroded = wea_c.table.shallowcopy(heightmap)
-- print("[erode.run] algorithm: "..algorithm..", params:");
2022-09-19 00:16:22 +00:00
-- print(wea_c.format.map(params))
-- wea_c.format.array_2d(heightmap, heightmap_size.x)
local success, msg, stats
if algorithm == "snowballs" then
success, msg = wea.erode.snowballs(
heightmap, heightmap_eroded,
heightmap_size,
region_height,
params
)
if not success then return success, msg end
elseif algorithm == "river" then
success, msg = wea.erode.river(
heightmap, heightmap_eroded,
heightmap_size,
region_height,
params
)
if not success then return success, msg end
else
2021-05-22 01:36:56 +00:00
-- FUTURE: Add a new "river" algorithm here that:
2021-05-29 22:20:34 +00:00
-- * Fills in blocks that are surrounded on more than 3 horizontal sides
2021-05-22 01:36:56 +00:00
-- * Destroys blocks that have no horizontal neighbours
-- A bit like cellular automata actually.
return false, "Error: Unknown algorithm '"..algorithm.."'. Currently implemented algorithms: snowballs (2d; hydraulic-like), river (2d; cellular automata-like; fills potholes and lowers towers). Ideas for algorithms to implement are welcome!"
end
2022-09-19 00:16:22 +00:00
success, stats = wea_c.terrain.apply_heightmap_changes(
pos1, pos2, area, data,
heightmap, heightmap_eroded, heightmap_size
)
if not success then return success, stats end
worldedit.manip_helpers.finish(manip, data)
return true, msg, stats
end