2021-12-27 04:11:52 +01:00
local wea = worldeditadditions
2022-09-19 02:16:22 +02:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
2021-12-27 04:11:52 +01:00
2021-12-28 03:17:26 +01:00
--- Applies the given brush with the given height and size to the given position.
-- @param pos1 Vector3 The position at which to apply the brush.
-- @param brush_name string The name of the brush to apply.
-- @param height number The height of the brush application.
-- @param brush_size Vector3 The size of the brush application. Values are interpreted on the X/Y coordinates, and NOT X/Z!
-- @returns bool, string|{ added: number, removed: number } A bool indicating whether the operation was successful or not, followed by either an error message as a string (if it was not successful) or a table of statistics (if it was successful).
local function apply ( pos1 , brush_name , height , brush_size )
-- 1: Get & validate brush
local success , brush , brush_size_actual = wea.sculpt . make_brush ( brush_name , brush_size )
if not success then return success , brush end
2022-01-02 18:19:53 +01:00
-- print(wea.sculpt.make_preview(brush, brush_size_actual))
2021-12-28 18:45:20 +01:00
2021-12-28 03:17:26 +01:00
local brush_size_terrain = Vector3.new (
brush_size_actual.x ,
0 ,
brush_size_actual.y
)
local brush_size_radius = ( brush_size_terrain / 2 ) : floor ( )
2021-12-28 20:04:13 +01:00
-- To try and make sure we catch height variations
local buffer = Vector3.new ( 0 , math.min ( height * 2 , 100 ) , 0 )
local pos1_compute = pos1 - brush_size_radius - buffer
local pos2_compute = pos1 + brush_size_radius + Vector3.new ( 0 , height , 0 ) + buffer
2021-12-28 03:17:26 +01:00
2021-12-28 19:38:10 +01:00
pos1_compute , pos2_compute = Vector3.sort (
pos1_compute ,
pos2_compute
)
2021-12-28 03:17:26 +01:00
-- 2: Fetch the nodes in the specified area, extract heightmap
local manip , area = worldedit.manip_helpers . init ( pos1_compute , pos2_compute )
local data = manip : get_data ( )
2022-09-19 02:16:22 +02:00
local heightmap , heightmap_size = wea_c.terrain . make_heightmap (
2021-12-28 03:17:26 +01:00
pos1_compute , pos2_compute ,
manip , area ,
data
)
2022-09-19 02:16:22 +02:00
local heightmap_orig = wea_c.table . shallowcopy ( heightmap )
2021-12-28 03:17:26 +01:00
2021-12-28 03:20:47 +01:00
local success2 , added , removed = wea.sculpt . apply_heightmap (
brush , brush_size_actual ,
height ,
( heightmap_size / 2 ) : floor ( ) ,
heightmap , heightmap_size
)
if not success2 then return success2 , added end
2021-12-27 04:11:52 +01:00
2021-12-28 03:17:26 +01:00
-- 3: Save back to disk & return
2022-09-19 02:16:22 +02:00
local success3 , stats = wea_c.terrain . apply_heightmap_changes (
2021-12-28 03:17:26 +01:00
pos1_compute , pos2_compute ,
area , data ,
heightmap_orig , heightmap ,
heightmap_size
)
2021-12-28 03:20:47 +01:00
if not success3 then return success2 , stats end
2021-12-28 03:17:26 +01:00
worldedit.manip_helpers . finish ( manip , data )
return true , stats
2021-12-27 04:11:52 +01:00
end
return apply