2021-12-31 02:42:32 +00:00
local wea = worldeditadditions
wea.erode = { }
2020-08-18 01:11:37 +00:00
2021-12-31 02:42:32 +00:00
dofile ( wea.modpath .. " /lib/erode/snowballs.lua " )
dofile ( wea.modpath .. " /lib/erode/river.lua " )
2020-08-21 12:27:40 +00:00
2021-12-31 02:42:32 +00:00
function wea . erode . run ( pos1 , pos2 , algorithm , params )
2020-08-21 12:27:40 +00:00
pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
local manip , area = worldedit.manip_helpers . init ( pos1 , pos2 )
local data = manip : get_data ( )
2021-02-26 02:20:53 +00:00
local heightmap_size = {
z = ( pos2.z - pos1.z ) + 1 ,
x = ( pos2.x - pos1.x ) + 1
}
2020-08-21 12:27:40 +00:00
2020-08-21 19:59:50 +00:00
local region_height = ( pos2.y - pos1.y ) + 1
2021-12-31 02:42:32 +00:00
local heightmap = wea.terrain . make_heightmap ( pos1 , pos2 , manip , area , data )
local heightmap_eroded = wea.table . shallowcopy ( heightmap )
2020-08-21 12:27:40 +00:00
2020-08-21 19:59:50 +00:00
-- print("[erode.run] algorithm: "..algorithm..", params:");
2021-12-31 02:42:32 +00:00
-- print(wea.format.map(params))
-- wea.format.array_2d(heightmap, heightmap_size.x)
2020-08-21 21:01:24 +00:00
local success , msg , stats
2020-08-21 12:27:40 +00:00
if algorithm == " snowballs " then
2021-12-31 02:42:32 +00:00
success , msg = wea.erode . snowballs (
2021-05-29 23:02:21 +00:00
heightmap , heightmap_eroded ,
heightmap_size ,
region_height ,
params
)
if not success then return success , msg end
elseif algorithm == " river " then
2021-12-31 02:42:32 +00:00
success , msg = wea.erode . river (
2021-05-29 23:02:21 +00:00
heightmap , heightmap_eroded ,
heightmap_size ,
region_height ,
params
)
2020-08-21 12:27:40 +00:00
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.
2021-05-29 23:02:21 +00:00
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! "
2020-08-21 12:27:40 +00:00
end
2021-12-31 02:42:32 +00:00
success , stats = wea.terrain . apply_heightmap_changes (
2020-08-21 12:27:40 +00:00
pos1 , pos2 , area , data ,
heightmap , heightmap_eroded , heightmap_size
)
2020-08-21 14:21:10 +00:00
if not success then return success , stats end
2020-08-21 12:27:40 +00:00
worldedit.manip_helpers . finish ( manip , data )
2020-08-21 21:01:24 +00:00
return true , msg , stats
2020-08-21 12:27:40 +00:00
end