2022-09-18 18:59:57 +02:00
local wea = worldeditadditions
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
2021-02-18 03:18:34 +01:00
local function parse_scale_component ( val )
local result = tonumber ( val )
if result then return true , result end
2022-09-18 18:59:57 +02:00
if string.find ( val , " / " ) then
local parts = wea_c.split ( val , " / " , true )
2021-02-18 03:18:34 +01:00
local a = tonumber ( parts [ 1 ] )
local b = tonumber ( parts [ 2 ] )
if not b then return false , " Invalid number after the forward slash in scale component. " end
if not a then return false , " Invalid number before the forward slash in scale component. " end
return true , a / b
end
if string.find ( val , " %% " ) then
local part = tonumber ( string.sub ( val , 1 , - 2 ) )
if not part then return false , " We thought a scale component was a percentage, but failed to parse percentage as number. " end
return true , part / 100
end
return false , " Failed to parse scale component (unrecognised format - we support things like '3', '0.5', '1/10', or '33%' without quotes). "
end
2021-02-08 00:39:09 +01:00
-- ███████ ██████ █████ ██ ███████
-- ██ ██ ██ ██ ██ ██
-- ███████ ██ ███████ ██ █████
-- ██ ██ ██ ██ ██ ██
-- ███████ ██████ ██ ██ ███████ ███████
2022-09-18 18:59:57 +02:00
wea_c.register_command ( " scale " , {
2021-02-08 00:39:09 +01:00
params = " <axis> <scale_factor> | <factor_x> [<factor_y> <factor_z> [<anchor_x> <anchor_y> <anchor_z>]] " ,
description = " Combined scale up / down. Takes either an axis name + a scale factor (e.g. y 3 or -z 2; negative values swap the anchor point for the scale operation), or 3 scale factor values for x, y, and z respectively. In the latter mode, a set of anchors can also be specified, which indicate which size the scale operation should be anchored to. " ,
privs = { worldedit = true } ,
require_pos = 2 ,
parse = function ( params_text )
if not params_text then params_text = " " end
2022-09-18 18:59:57 +02:00
local parts = wea_c.split_shell ( params_text )
2021-02-08 00:39:09 +01:00
2022-09-18 18:59:57 +02:00
local scale = Vector3.new ( 1 , 1 , 1 )
local anchor = Vector3.new ( 1 , 1 , 1 )
2021-02-08 00:39:09 +01:00
if # parts == 2 then
2021-02-18 02:24:53 +01:00
if not ( parts [ 1 ] == " x " or parts [ 1 ] == " y " or parts [ 1 ] == " z "
2021-07-30 19:05:45 +02:00
or parts [ 1 ] == " -x " or parts [ 1 ] == " -y " or parts [ 1 ] == " -z " ) then
2021-02-08 00:39:09 +01:00
return false , " Error: Got 2 arguments, but the first doesn't look like the name of an axis. "
end
local axis = parts [ 1 ]
2021-02-18 03:18:34 +01:00
local success , factor = parse_scale_component ( parts [ 2 ] )
if not success then return success , " Error: Invalid scale factor. Details: " .. factor end
2021-02-08 00:39:09 +01:00
if axis : sub ( 1 , 1 ) == " - " then
axis = axis : sub ( 2 , 2 )
2021-02-18 01:30:24 +01:00
anchor [ axis ] = - 1
2021-02-08 00:39:09 +01:00
end
2022-09-18 18:59:57 +02:00
-- BUG: POTENTIAL BUG HERE
2021-02-08 00:39:09 +01:00
scale [ axis ] = factor
elseif # parts >= 3 then
2021-02-18 03:18:34 +01:00
local success , val = parse_scale_component ( parts [ 1 ] )
if not success then return false , " Error: x axis scale factor wasn't a number. Details: " .. val end
2021-02-08 00:39:09 +01:00
scale.x = val
2021-02-18 03:18:34 +01:00
success , val = parse_scale_component ( parts [ 2 ] )
if not success then return false , " Error: y axis scale factor wasn't a number. Details: " .. val end
2021-02-08 00:39:09 +01:00
scale.y = val
2021-02-18 03:18:34 +01:00
success , val = parse_scale_component ( parts [ 3 ] )
if not success then return false , " Error: z axis scale factor wasn't a number. Details: " .. val end
2021-02-08 00:39:09 +01:00
scale.z = val
else
2021-02-18 03:18:34 +01:00
local success , val = parse_scale_component ( parts [ 1 ] )
if not success then
return false , " Error: scale factor wasn't a number. Details: " .. val
2021-02-08 00:39:09 +01:00
end
scale.x = val
scale.y = val
scale.z = val
end
if # parts == 6 then
local val = tonumber ( parts [ 4 ] )
if not val then return false , " Error: x axis anchor wasn't a number. " end
2021-02-18 01:30:24 +01:00
anchor.x = val
2021-02-08 00:39:09 +01:00
val = tonumber ( parts [ 5 ] )
if not val then return false , " Error: y axis anchor wasn't a number. " end
2021-02-18 01:30:24 +01:00
anchor.y = val
2021-02-08 00:39:09 +01:00
val = tonumber ( parts [ 6 ] )
if not val then return false , " Error: z axis anchor wasn't a number. " end
2021-02-18 01:30:24 +01:00
anchor.z = val
2021-02-08 00:39:09 +01:00
end
if scale.x == 0 then return false , " Error: x scale factor was 0 " end
if scale.y == 0 then return false , " Error: y scale factor was 0 " end
if scale.z == 0 then return false , " Error: z scale factor was 0 " end
2021-02-18 01:30:24 +01:00
if anchor.x == 0 then return false , " Error: x axis anchor was 0 " end
if anchor.y == 0 then return false , " Error: y axis anchor was 0 " end
if anchor.z == 0 then return false , " Error: z axis anchor was 0 " end
2021-02-08 00:39:09 +01:00
2021-02-18 01:30:24 +01:00
return true , scale , anchor
2021-02-08 00:39:09 +01:00
end ,
2021-02-18 01:30:24 +01:00
nodes_needed = function ( name , scale , anchor )
2021-02-08 00:39:09 +01:00
local volume = worldedit.volume ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
local factor = math.ceil ( math.abs ( scale.x ) )
* math.ceil ( math.abs ( scale.y ) )
* math.ceil ( math.abs ( scale.z ) )
return volume * factor
end ,
2021-02-18 01:30:24 +01:00
func = function ( name , scale , anchor )
2021-02-08 00:39:09 +01:00
2022-09-18 18:59:57 +02:00
local start_time = wea_c.get_ms_time ( )
2021-02-08 00:39:09 +01:00
2022-09-18 18:59:57 +02:00
local pos1 , pos2 = Vector3.sort ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2021-02-08 00:39:09 +01:00
2022-09-18 18:59:57 +02:00
local success , stats = wea.scale (
pos1 , pos2 ,
2021-02-18 01:30:24 +01:00
scale , anchor
)
if not success then return success , stats end
2021-02-08 00:39:09 +01:00
2021-02-18 03:18:34 +01:00
worldedit.pos1 [ name ] = stats.pos1
worldedit.pos2 [ name ] = stats.pos2
worldedit.marker_update ( name )
2022-09-18 18:59:57 +02:00
local time_taken = wea_c.get_ms_time ( ) - start_time
2021-02-08 00:39:09 +01:00
2022-09-18 18:59:57 +02:00
minetest.log ( " action " , name .. " used //scale at " .. pos1 .. " - " .. pos2 .. " , updating " .. stats.updated .. " nodes in " .. time_taken .. " s " )
return true , stats.updated .. " nodes updated in " .. wea_c.format . human_time ( time_taken )
2021-02-08 00:39:09 +01:00
end
} )