diff --git a/README.md b/README.md index d726438..e513605 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,22 @@ Replace all instances of with in the current WorldEdi //replace dirt flowers:flower_waterlily //replace flowers:flower_rose flowers:flower_tulip +### //hollowsphere + +Add hollow sphere at WorldEdit position 1 with radius , composed of . + + //hollowsphere 5 dirt + //hollowsphere 12 default:glass + //hollowsphere 17 mesecons:mesecon + +### //sphere + +Add sphere at WorldEdit position 1 with radius , composed of . + + //sphere 5 dirt + //sphere 12 default:glass + //sphere 17 mesecons:mesecon + ### //hollowcylinder x/y/z/? Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length and radius , composed of . @@ -228,15 +244,27 @@ Replaces all instances of `searchnode` with `replacenode` in a region defined by Returns the number of nodes replaced. +### worldedit.hollow_sphere = function(pos, radius, nodename) + +Adds a hollow sphere at `pos` with radius `radius`, composed of `nodename`. + +Returns the number of nodes added. + +### worldedit.sphere = function(pos, radius, nodename) + +Adds a sphere at `pos` with radius `radius`, composed of `nodename`. + +Returns the number of nodes added. + ### worldedit.hollow_cylinder(pos, axis, length, radius, nodename) -Adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`. +Adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`. Returns the number of nodes added. ### worldedit.cylinder(pos, axis, length, radius, nodename) -Adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`. +Adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`. Returns the number of nodes added. diff --git a/functions.lua b/functions.lua index 2d16daf..4b98a84 100644 --- a/functions.lua +++ b/functions.lua @@ -75,7 +75,50 @@ worldedit.replace = function(pos1, pos2, searchnode, replacenode) return count end ---adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, returning the number of nodes added +--adds a hollow sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added +worldedit.hollow_sphere = function(pos, radius, nodename) + local node = {name=nodename} + local pos1 = {x=0, y=0, z=0} + local full_radius = radius * radius + radius + local env = minetest.env + for x = -radius, radius do + pos1.x = pos.x + x + for y = -radius, radius do + pos1.y = pos.y + y + for z = -radius, radius do + if x*x+y*y+z*z >= (radius-1) * (radius-1) + (radius-1) and x*x+y*y+z*z <= full_radius then + pos1.z = pos.z + z + env:add_node({x=pos.x+x,y=pos.y+y,z=pos.z+z}, node) + end + end + end + end +end + +--adds a sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added +worldedit.sphere = function(pos, radius, nodename) + local node = {name=nodename} + local pos1 = {x=0, y=0, z=0} + local full_radius = radius * radius + radius + local count = 0 + local env = minetest.env + for x = -radius, radius do + pos1.x = pos.x + x + for y = -radius, radius do + pos1.y = pos.y + y + for z = -radius, radius do + if x*x+y*y+z*z <= full_radius then + pos1.z = pos.z + z + env:add_node(pos1, node) + count = count + 1 + end + end + end + end + return count +end + +--adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) local other1, other2 if axis == "x" then @@ -90,6 +133,11 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) local currentpos = {x=pos.x, y=pos.y, z=pos.z} local node = {name=nodename} local count = 0 + local step = 1 + if length < 0 + length = -length + step = -1 + end for i = 1, length do local offset1, offset2 = 0, radius local delta = -radius @@ -126,12 +174,12 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) end offset1 = offset1 + 1 end - currentpos[axis] = currentpos[axis] + 1 + currentpos[axis] = currentpos[axis] + step end return count end ---adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, returning the number of nodes added +--adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added worldedit.cylinder = function(pos, axis, length, radius, nodename) local other1, other2 if axis == "x" then @@ -146,6 +194,11 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename) local currentpos = {x=pos.x, y=pos.y, z=pos.z} local node = {name=nodename} local count = 0 + local step = 1 + if length < 0 + length = -length + step = -1 + end for i = 1, length do local offset1, offset2 = 0, radius local delta = -radius @@ -180,12 +233,12 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename) delta = delta - (offset2 * 2) end end - currentpos[axis] = currentpos[axis] + 1 + currentpos[axis] = currentpos[axis] + step end return count end ---adds a spiral at `pos` with size `size`, returning the number of nodes changed +--adds a spiral at `pos` with size `size`, composed of `nodename`, returning the number of nodes changed worldedit.spiral = function(pos, size, nodename) local shift_x, shift_y sa = spiralt(size) diff --git a/init.lua b/init.lua index 1e9b0ef..a7e1947 100644 --- a/init.lua +++ b/init.lua @@ -189,6 +189,58 @@ minetest.register_chatcommand("/replace", { end, }) +minetest.register_chatcommand("/hollowsphere", { + params = " ", + description = "Add hollow sphere at WorldEdit position 1 with radius , composed of ", + 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, _, radius, nodename = param:find("^(%d+)%s+([^%s]+)$") + if found == nil then + minetest.chat_send_player(name, "Invalid usage: " .. param) + return + end + if not worldedit.node_is_valid(pos, nodename) then + minetest.chat_send_player(name, "Invalid node name: " .. param) + return + end + + local count = worldedit.hollow_sphere(pos, tonumber(radius), nodename) + minetest.chat_send_player(name, count .. " nodes added") + end, +}) + +minetest.register_chatcommand("/sphere", { + params = " ", + description = "Add sphere at WorldEdit position 1 with radius , composed of ", + 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, _, radius, nodename = param:find("^(%d+)%s+([^%s]+)$") + if found == nil then + minetest.chat_send_player(name, "Invalid usage: " .. param) + return + end + if not worldedit.node_is_valid(pos, nodename) then + minetest.chat_send_player(name, "Invalid node name: " .. param) + return + end + + local count = worldedit.sphere(pos, tonumber(radius), nodename) + minetest.chat_send_player(name, count .. " nodes added") + end, +}) + minetest.register_chatcommand("/hollowcylinder", { params = "x/y/z/? ", description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length and radius , composed of ",