Add //spiral and worldedit.spiral API function

This commit is contained in:
sfan5 2012-08-19 09:36:11 +02:00
parent 5b66b5ec25
commit e39493e789
4 changed files with 86 additions and 3 deletions

View File

@ -109,6 +109,15 @@ Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>
//cylinder z -12 3 mesecons:mesecon //cylinder z -12 3 mesecons:mesecon
//cylinder ? 2 4 stone //cylinder ? 2 4 stone
### //spiral <size> <node>
Add Spiral at WorldEdit position 1 with size <size>, composed of <node>.
//spiral 8 dirt
//spiral 5 default:glass
//spiral 2 stone
### //copy x/y/z/? <amount> ### //copy x/y/z/? <amount>
Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes. Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes.
@ -216,6 +225,12 @@ Adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `
Returns the number of nodes added. Returns the number of nodes added.
### worldedit.spiral(pos, size, nodename)
Adds a Spiral at `pos` with size `size`.
Returns the number of nodes changed.
### worldedit.copy(pos1, pos2, axis, amount) ### worldedit.copy(pos1, pos2, axis, amount)
Copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes. Copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes.

View File

@ -1,3 +1,4 @@
dofile(minetest.get_modpath("worldedit") .. "/spirals.lua")
--modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions --modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions
worldedit.sort_pos = function(pos1, pos2) worldedit.sort_pos = function(pos1, pos2)
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z} pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
@ -182,6 +183,26 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename)
return count return count
end end
--adds a spiral at `pos` with size `size`, returning the number of nodes changed
worldedit.spiral = function(pos, size, nodename)
local shift_x,shift_y
sa = spiralt(size)
shift_y = #sa -- "Height" of the Array
local fe = sa[1]
shift_x = #fe -- "Width" of the Array
fe = nil
local count = 0
for x,v in ipairs(sa) do
for y, z in ipairs(v) do
minetest.env:add_node({x=pos.x-shift_x+x,y=pos.y-shift_y+y,z=pos.z+z}, {name=nodename})
count = count + 1
end
end
return count
end
--copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes copied --copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes copied
worldedit.copy = function(pos1, pos2, axis, amount) worldedit.copy = function(pos1, pos2, axis, amount)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)

View File

@ -216,6 +216,36 @@ minetest.register_chatcommand("/hollowcylinder", {
end, end,
}) })
minetest.register_chatcommand("/spiral", {
params = "<size> <node>",
description = "Add Spiral at WorldEdit position 1 with size <size>, composed of <node>",
privs = {worldedit=true},
func = function(name, param)
local pos = worldedit.pos1[name]
if pos == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local found, _, size, nodename = param:find("(%d+)%s+([^%s]+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
if axis == "?" then
axis = worldedit.player_axis(name)
end
if not worldedit.node_is_valid(pos, nodename) then
minetest.chat_send_player(name, "Invalid node name: " .. param)
return
end
local count = worldedit.spiral(pos, tonumber(size), nodename)
minetest.chat_send_player(name, count .. " nodes changed")
end,
})
minetest.register_chatcommand("/cylinder", { minetest.register_chatcommand("/cylinder", {
params = "x/y/z/? <length> <radius> <node>", params = "x/y/z/? <length> <radius> <node>",
description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>", description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>",

17
spirals.lua Normal file
View File

@ -0,0 +1,17 @@
av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
function sindex(y, x) -- returns the value at (x, y) in a spiral that starts at 1 and goes outwards
if y == -x and y >= x then return (2*y+1)^2 end
local l = math.max(av(y), av(x))
return (2*l-1)^2+4*l+2*l*sn(x+y)+sn(y^2-x^2)*(l-(av(y)==l and sn(y)*x or sn(x)*y)) -- OH GOD WHAT
end
function spiralt(side)
local ret, start, stop = {}, math.floor((-side+1)/2), math.floor((side-1)/2)
for i = 1, side do
ret[i] = {}
for j = 1, side do
ret[i][j] = side^2 - sindex(stop - i + 1,start + j - 1) --moves the coordinates so (0,0) is at the center of the spiral
end
end
return ret
end