Add //spiral2, write glue for square spirals

Next up: circular spirals!
This commit is contained in:
Starbeamrainbowlabs 2021-10-31 15:11:01 +00:00
parent a3f26200a0
commit d3a8efb9b8
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 100 additions and 7 deletions

View file

@ -28,7 +28,7 @@ function worldeditadditions.spiral_square(pos1, pos2, target_node, interval, acc
local volume = pos2:subtract(pos1)
local volume_half = volume:divide(2)
print("DEBUG:spiral_square | pos1: "..pos1..", pos2: "..pos2..", target_node: "..target_node)
print("DEBUG:spiral_square | pos1: "..pos1..", pos2: "..pos2..", target_node: "..target_node, "interval:"..interval..", acceleration: "..acceleration)
-- Fetch the nodes in the specified area
@ -39,13 +39,14 @@ function worldeditadditions.spiral_square(pos1, pos2, target_node, interval, acc
local count = 0 -- The number of nodes replaced
local centre = pos2:subtract(pos1):floor()
local centre = pos2:subtract(pos1):floor():divide(2):add(pos1)
local pos_current = centre:clone()
local pos_corner_last = pos_current:clone()
local pos_current = centre:clone():floor()
local side_length = 0
local direction = Vector3.new(1, 0, 0)
local side_length_max = interval
local side_length_max = interval + 1
local sides_complete = 0
-- local sides_acc = 0
while pos_current:is_contained(pos1, pos2) do
@ -57,8 +58,16 @@ function worldeditadditions.spiral_square(pos1, pos2, target_node, interval, acc
pos_current = pos_current:add(direction)
side_length = side_length + 1
if side_length > side_length_max then
side_length_max = side_length_max + interval + acceleration
print("DEBUG cpos", pos_current, "side_length", side_length, "side_length_max", side_length_max, "direction", direction)
if side_length >= side_length_max then
sides_complete = sides_complete + 1
-- sides_acc = sides_acc + 1
if sides_complete % 2 == 0 then
-- sides_acc = 0
side_length_max = side_length_max + interval + acceleration + 1
end
side_length = 0
if direction.x == 0 and direction.z == 1 then
direction.x = 1

View file

@ -0,0 +1,83 @@
local wea = worldeditadditions
local Vector3 = wea.Vector3
worldedit.register_command("spiral2", {
params = "[<circle|square>] [<replace_node=dirt> [<interval=3> [<acceleration=0>] ] ]",
description = "Generates a spiral that fills the defined region using the specified replace node. The spiral is either circular (default) or square in shape. The interval specifies the distance between the walls of the spiral, and the acceleration specifies how quickly this value should increase.",
privs = { worldedit = true },
require_pos = 2,
parse = function(params_text)
if not params_text then return true, "circle", "dirt" end
params_text = wea.trim(params_text)
if params_text == "" then return true, "circle", "dirt" end
local parts = wea.split_shell(params_text)
local mode = "circle"
local target_node = "dirt"
local target_node_found = false
local interval = 3
local acceleration = 0
if parts[1] ~= "circle" and parts[1] ~= "square" then
target_node = parts[1]
target_node_found = true
else
mode = parts[1]
table.remove(parts, 1)
end
if #parts >= 1 and not target_node_found then
target_node = parts[1]
table.remove(parts, 1)
end
if #parts >= 1 then
interval = tonumber(parts[1])
table.remove(parts, 1)
end
if #parts >= 1 then
acceleration = tonumber(parts[3])
table.remove(parts, 1)
end
local target_node_norm = worldedit.normalize_nodename(target_node)
if not target_node_norm then
return false, "Error: Unknown node '"..target_node_norm.."'."
end
if not interval then
return false, "Error: Invalid interval value. Interval values must be integers."
end
if not acceleration then
return false, "Error: Invalid acceleration value. Acceleration values must be integers."
end
return true, mode, target_node_norm, interval, acceleration
end,
nodes_needed = function(name)
return worldedit.volume(worldedit.pos1[name], worldedit.pos2[name])
end,
func = function(name, mode, target_node, interval, acceleration)
local start_time = wea.get_ms_time()
local pos1, pos2 = Vector3.sort(worldedit.pos1[name], worldedit.pos2[name])
local success, count
if mode == "circle" then
return false, "Error: Not implemented yet. Coming soon!"
else
success, count = wea.spiral_square(
pos1, pos2,
target_node,
interval, acceleration
)
if not success then return success, count end
end
local time_taken = wea.get_ms_time() - start_time
minetest.log("action", name .. " used //spiral at "..pos1.." - "..pos2..", adding " .. count .. " nodes in " .. time_taken .. "s")
return true, count .. " nodes replaced in " .. wea.format.human_time(time_taken)
end
})

View file

@ -35,6 +35,7 @@ dofile(we_c.modpath.."/commands/replacemix.lua")
dofile(we_c.modpath.."/commands/scale.lua")
dofile(we_c.modpath.."/commands/torus.lua")
dofile(we_c.modpath.."/commands/walls.lua")
dofile(we_c.modpath.."/commands/spiral2.lua")
dofile(we_c.modpath.."/commands/count.lua")