//scale: fix some bugs, and rename direction → anchor internally

It's nice to have some consistency, and find&replace saves soooo much 
time :P
This commit is contained in:
Starbeamrainbowlabs 2021-02-18 00:30:24 +00:00
parent 17a67fc5f9
commit ed3adde56a
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 39 additions and 35 deletions

View file

@ -1,13 +1,13 @@
--- Scales the defined region by the given scale factor in the given directions. --- Scales the defined region by the given scale factor in the given anchors.
-- Scale factor vectors containing both scale up and scale down operations are -- Scale factor vectors containing both scale up and scale down operations are
-- split into 2 different scale operations automatically. -- split into 2 different scale operations automatically.
-- @param pos1 Vector Position 1 of the defined region, -- @param pos1 Vector Position 1 of the defined region,
-- @param pos2 Vector Position 2 of the defined region. -- @param pos2 Vector Position 2 of the defined region.
-- @param scale Vector The scale factor - as a vector - by which to scale (values between -1 and 1 are considered a scale down operation). -- @param scale Vector The scale factor - as a vector - by which to scale (values between -1 and 1 are considered a scale down operation).
-- @param direction Vector The direction to scale in - as a vector. e.g. { x = -1, y = 1, z = -1 } would mean scale in the negative x, positive y, and nevative z directions. -- @param anchor Vector The anchor to scale in - as a vector. e.g. { x = -1, y = 1, z = -1 } would mean scale in the negative x, positive y, and negative z directions.
-- @return boolean, string|table Whether the operation was successful or not. If not, then an error messagea as a string is also passed. If it was, then a statistics object is returned instead. -- @return boolean, string|table Whether the operation was successful or not. If not, then an error messagea as a string is also passed. If it was, then a statistics object is returned instead.
function worldeditadditions.scale(pos1, pos2, scale, direction) function worldeditadditions.scale(pos1, pos2, scale, anchor)
if scale.x == 0 or scale.y == 0 or scale.z == 0 then if scale.x == 0 or scale.y == 0 or scale.z == 0 then
return false, "A component of the scale factoro was 0." return false, "A component of the scale factoro was 0."
end end
@ -27,14 +27,14 @@ function worldeditadditions.scale(pos1, pos2, scale, direction)
local success, stats local success, stats
if scale_up.x ~= 1 or scale_up.y ~= 1 or scale_up.z ~= 1 then if scale_up.x ~= 1 or scale_up.y ~= 1 or scale_up.z ~= 1 then
success, stats = worldeditadditions.scale_up(pos1, pos2, scale_up, direction) success, stats = worldeditadditions.scale_up(pos1, pos2, scale_up, anchor)
if not success then return success, stats end if not success then return success, stats end
stats_total.updated = stats.updated stats_total.updated = stats.updated
stats_total.operations = stats_total.operations + 1 stats_total.operations = stats_total.operations + 1
stats_total.scale_down = stats.scale stats_total.scale_down = stats.scale
end end
if scale_down.x ~= 1 or scale_down.y ~= 1 or scale_down.z ~= 1 then if scale_down.x ~= 1 or scale_down.y ~= 1 or scale_down.z ~= 1 then
success, stats = worldeditadditions.scale_down(pos1, pos2, scale_down, direction) success, stats = worldeditadditions.scale_down(pos1, pos2, scale_down, anchor)
if not success then return success, stats end if not success then return success, stats end
stats_total.updated = stats_total.updated + stats.updated stats_total.updated = stats_total.updated + stats.updated
stats_total.operations = stats_total.operations + 1 stats_total.operations = stats_total.operations + 1

View file

@ -8,15 +8,15 @@
-- @param pos1 Vector Position 1 of the defined region, -- @param pos1 Vector Position 1 of the defined region,
-- @param pos2 Vector Position 2 of the defined region. -- @param pos2 Vector Position 2 of the defined region.
-- @param scale Vector The scale factor - as a vector - by which to scale down. -- @param scale Vector The scale factor - as a vector - by which to scale down.
-- @param direction Vector The direction to scale in - as a vector. e.g. { x = -1, y = 1, z = -1 } would mean scale in the negative x, positive y, and nevative z directions. -- @param anchor Vector The direction to scale in - as a vector. e.g. { x = -1, y = 1, z = -1 } would mean scale in the negative x, positive y, and nevative z directions.
-- @return boolean, string|table Whether the operation was successful or not. If not, then an error messagea as a string is also passed. If it was, then a statistics object is returned instead. -- @return boolean, string|table Whether the operation was successful or not. If not, then an error messagea as a string is also passed. If it was, then a statistics object is returned instead.
function worldeditadditions.scale_down(pos1, pos2, scale, direction) function worldeditadditions.scale_down(pos1, pos2, scale, anchor)
pos1, pos2 = worldedit.sort_pos(pos1, pos2) pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if scale.x > 1 or scale.y > 1 or scale.z > 1 or scale.x < -1 or scale.y < -1 or scale.z < -1 then if scale.x > 1 or scale.y > 1 or scale.z > 1 or scale.x < -1 or scale.y < -1 or scale.z < -1 then
return false, "Error: Scale factor vectors may not mix values -1 < factor < 1 and (1 < factor or factor < -1) - in other words, you can't scale both up and down at the same time (try worldeditadditions.scale, which automatically applies such scale factor vectors as 2 successive operations)" return false, "Error: Scale factor vectors may not mix values -1 < factor < 1 and (1 < factor or factor < -1) - in other words, you can't scale both up and down at the same time (try worldeditadditions.scale, which automatically applies such scale factor vectors as 2 successive operations)"
end end
if direction.x == 0 or direction.y == 0 or direction.z == 0 then if anchor.x == 0 or anchor.y == 0 or anchor.z == 0 then
return false, "Error: One of the components of the direction vector was 0 (direction components should either be greater than or less than 0 to indicate the direction to scale in.)" return false, "Error: One of the components of the anchor vector was 0 (anchor components should either be greater than or less than 0 to indicate the anchor to scale in.)"
end end
local scale_down = { local scale_down = {
@ -51,9 +51,9 @@ function worldeditadditions.scale_down(pos1, pos2, scale, direction)
local posi_copy = worldeditadditions.shallowcopy(posi_rel) local posi_copy = worldeditadditions.shallowcopy(posi_rel)
posi_copy = vector.floor(vector.divide(scale_down)) posi_copy = vector.floor(vector.divide(scale_down))
if direction.x < 0 then posi_copy.x = size.x - posi_copy.x end if anchor.x < 0 then posi_copy.x = size.x - posi_copy.x end
if direction.y < 0 then posi_copy.y = size.y - posi_copy.y end if anchor.y < 0 then posi_copy.y = size.y - posi_copy.y end
if direction.z < 0 then posi_copy.z = size.z - posi_copy.z end if anchor.z < 0 then posi_copy.z = size.z - posi_copy.z end
local posi_copy = vector.add(pos1, posi_copy) local posi_copy = vector.add(pos1, posi_copy)

View file

@ -8,27 +8,27 @@
-- @param pos1 Vector Position 1 of the defined region, -- @param pos1 Vector Position 1 of the defined region,
-- @param pos2 Vector Position 2 of the defined region. -- @param pos2 Vector Position 2 of the defined region.
-- @param scale Vector The scale factor - as a vector - by which to scale down. -- @param scale Vector The scale factor - as a vector - by which to scale down.
-- @param direction Vector The direction to scale in - as a vector. e.g. { x = -1, y = 1, z = -1 } would mean scale in the negative x, positive y, and nevative z directions. -- @param anchor Vector The direction to scale in - as a vector. e.g. { x = -1, y = 1, z = -1 } would mean scale in the negative x, positive y, and nevative z directions.
-- @return boolean, string|table Whether the operation was successful or not. If not, then an error messagea as a string is also passed. If it was, then a statistics object is returned instead. -- @return boolean, string|table Whether the operation was successful or not. If not, then an error messagea as a string is also passed. If it was, then a statistics object is returned instead.
function worldeditadditions.scale_up(pos1, pos2, scale, direction) function worldeditadditions.scale_up(pos1, pos2, scale, anchor)
pos1, pos2 = worldedit.sort_pos(pos1, pos2) pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if (scale.x < 1 and scale.x > -1) and (scale.y < 1 and scale.y > -1) and (scale.z < 1 and scale.z > -1) then if (scale.x < 1 and scale.x > -1) and (scale.y < 1 and scale.y > -1) and (scale.z < 1 and scale.z > -1) then
return false, "Error: Scale factor vectors may not mix values -1 < factor < 1 and (1 < factor or factor < -1) - in other words, you can't scale both up and down at the same time (try worldeditadditions.scale, which automatically applies such scale factor vectors as 2 successive operations)" return false, "Error: Scale factor vectors may not mix values -1 < factor < 1 and (1 < factor or factor < -1) - in other words, you can't scale both up and down at the same time (try worldeditadditions.scale, which automatically applies such scale factor vectors as 2 successive operations)"
end end
if direction.x == 0 or direction.y == 0 or direction.z == 0 then if anchor.x == 0 or anchor.y == 0 or anchor.z == 0 then
return false, "Error: One of the components of the direction vector was 0 (direction components should either be greater than or less than 0 to indicate the direction to scale in.)" return false, "Error: One of the components of the anchor vector was 0 (anchor components should either be greater than or less than 0 to indicate the anchor to scale in.)"
end end
local size = vector.subtract(pos2, pos1) local size = vector.subtract(pos2, pos1)
local pos1_big = vector.new(pos1) local pos1_big = vector.new(pos1)
local pos2_big = vector(pos2) local pos2_big = vector.new(pos2)
if direction.x < 1 then pos1_big.x = pos1_big.x - size.x if anchor.x < 1 then pos1_big.x = pos1_big.x - size.x
else pos2_big.x = pos2_big.x + size.x end else pos2_big.x = pos2_big.x + size.x end
if direction.y < 1 then pos1_big.y = pos1_big.y - size.y if anchor.y < 1 then pos1_big.y = pos1_big.y - size.y
else pos2_big.y = pos2_big.y + size.y end else pos2_big.y = pos2_big.y + size.y end
if direction.z < 1 then pos1_big.z = pos1_big.z - size.z if anchor.z < 1 then pos1_big.z = pos1_big.z - size.z
else pos2_big.z = pos2_big.z + size.z end else pos2_big.z = pos2_big.z + size.z end

View file

@ -14,7 +14,7 @@ worldedit.register_command("scale", {
local parts = worldeditadditions.split(params_text, "%s+", false) local parts = worldeditadditions.split(params_text, "%s+", false)
local scale = vector.new(1, 1, 1) local scale = vector.new(1, 1, 1)
local direction = vector.new(1, 1, 1) local anchor = vector.new(1, 1, 1)
if #parts == 2 then if #parts == 2 then
if parts[1] ~= "x" or parts[1] ~= "y" or parts[1] ~= "z" if parts[1] ~= "x" or parts[1] ~= "y" or parts[1] ~= "z"
@ -27,7 +27,7 @@ worldedit.register_command("scale", {
if axis:sub(1, 1) == "-" then if axis:sub(1, 1) == "-" then
axis = axis:sub(2, 2) axis = axis:sub(2, 2)
direction[axis] = -1 anchor[axis] = -1
end end
scale[axis] = factor scale[axis] = factor
@ -54,41 +54,45 @@ worldedit.register_command("scale", {
if #parts == 6 then if #parts == 6 then
local val = tonumber(parts[4]) local val = tonumber(parts[4])
if not val then return false, "Error: x axis anchor wasn't a number." end if not val then return false, "Error: x axis anchor wasn't a number." end
direction.x = val anchor.x = val
val = tonumber(parts[5]) val = tonumber(parts[5])
if not val then return false, "Error: y axis anchor wasn't a number." end if not val then return false, "Error: y axis anchor wasn't a number." end
direction.y = val anchor.y = val
val = tonumber(parts[6]) val = tonumber(parts[6])
if not val then return false, "Error: z axis anchor wasn't a number." end if not val then return false, "Error: z axis anchor wasn't a number." end
direction.z = val anchor.z = val
end end
if scale.x == 0 then return false, "Error: x scale factor was 0" 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.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 if scale.z == 0 then return false, "Error: z scale factor was 0" end
if direction.x == 0 then return false, "Error: x axis anchor was 0" end if anchor.x == 0 then return false, "Error: x axis anchor was 0" end
if direction.y == 0 then return false, "Error: y axis anchor was 0" end if anchor.y == 0 then return false, "Error: y axis anchor was 0" end
if direction.z == 0 then return false, "Error: z axis anchor was 0" end if anchor.z == 0 then return false, "Error: z axis anchor was 0" end
return true, scale, direction return true, scale, anchor
end, end,
nodes_needed = function(name, scale, direction) nodes_needed = function(name, scale, anchor)
local volume = worldedit.volume(worldedit.pos1[name], worldedit.pos2[name]) local volume = worldedit.volume(worldedit.pos1[name], worldedit.pos2[name])
local factor = math.ceil(math.abs(scale.x)) local factor = math.ceil(math.abs(scale.x))
* math.ceil(math.abs(scale.y)) * math.ceil(math.abs(scale.y))
* math.ceil(math.abs(scale.z)) * math.ceil(math.abs(scale.z))
return volume * factor return volume * factor
end, end,
func = function(name, scale, direction) func = function(name, scale, anchor)
print("initial scale: "..worldeditadditions.vector.tostring(scale)..", anchor: "..worldeditadditions.vector.tostring(anchor))
local start_time = worldeditadditions.get_ms_time() local start_time = worldeditadditions.get_ms_time()
local success, stats = worldeditadditions.scale(pos1, pos2, scale, direction) local success, stats = worldeditadditions.scale(
worldedit.pos1[name],
worldedit.pos2[name],
scale, anchor
)
if not success then return success, stats end if not success then return success, stats end
-- TODO read stats here
local time_taken = worldeditadditions.get_ms_time() - start_time local time_taken = worldeditadditions.get_ms_time() - start_time