]> git.lizzy.rs Git - worldedit.git/commitdiff
Add //spiral and worldedit.spiral API function
authorsfan5 <sfan5@live.de>
Sun, 19 Aug 2012 07:36:11 +0000 (09:36 +0200)
committersfan5 <sfan5@live.de>
Sun, 19 Aug 2012 07:36:11 +0000 (09:36 +0200)
README.md
functions.lua
init.lua
spirals.lua [new file with mode: 0644]

index a52d96ae638d3153e35ba3be7467432d61dcc436..a83f2f44317cbbb96d4aba9497a3711811da08f4 100644 (file)
--- a/README.md
+++ b/README.md
@@ -108,6 +108,15 @@ Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>
     //cylinder y 28 10 default:glass
     //cylinder z -12 3 mesecons:mesecon
     //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>
 
@@ -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.
 
+### 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)
 
 Copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes.
@@ -286,4 +301,4 @@ This mod is licensed under the [GNU Affero General Public License](http://www.gn
 
 Basically, this means everyone is free to use, modify, and distribute the files, as long as these modifications are also licensed the same way.
 
-Most importantly, the Affero variant of the GPL requires you to publish your modifications in source form, even if the mod is run only on the server, and not distributed.
\ No newline at end of file
+Most importantly, the Affero variant of the GPL requires you to publish your modifications in source form, even if the mod is run only on the server, and not distributed.
index 270b50e130a4bedadd0babafbc05b122c27a0e5f..ee549deb4c1126002b78e7f135925b0ea59c3133 100644 (file)
@@ -1,3 +1,4 @@
+dofile(minetest.get_modpath("worldedit") .. "/spirals.lua")\r
 --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\r
 worldedit.sort_pos = function(pos1, pos2)\r
        pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}\r
@@ -182,6 +183,26 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename)
        return count\r
 end\r
 \r
+--adds a spiral at `pos` with size `size`, returning the number of nodes changed\r
+worldedit.spiral = function(pos, size, nodename)\r
+    local shift_x,shift_y\r
+       sa = spiralt(size)\r
+    shift_y = #sa -- "Height" of the Array\r
+    local fe = sa[1]\r
+    shift_x = #fe -- "Width" of the Array\r
+    fe = nil\r
+    \r
+    local count = 0\r
+    for x,v in ipairs(sa) do\r
+        for y, z in ipairs(v) do\r
+            minetest.env:add_node({x=pos.x-shift_x+x,y=pos.y-shift_y+y,z=pos.z+z}, {name=nodename})\r
+            count = count + 1\r
+        end\r
+    end\r
+       \r
+       return count\r
+end\r
+\r
 --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\r
 worldedit.copy = function(pos1, pos2, axis, amount)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
@@ -476,4 +497,4 @@ worldedit.deserialize_old = function(originpos, value)
                count = count + 1\r
        end\r
        return count\r
-end
\ No newline at end of file
+end\r
index 10d03b8fe12ab9fbb4b793c6358eb1852b3e8731..cef7e53642bb254329cd9e66446c1fccd61bbf3a 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -216,6 +216,36 @@ minetest.register_chatcommand("/hollowcylinder", {
        end,\r
 })\r
 \r
+\r
+minetest.register_chatcommand("/spiral", {\r
+       params = "<size> <node>",\r
+       description = "Add Spiral at WorldEdit position 1 with size <size>, composed of <node>",\r
+       privs = {worldedit=true},\r
+       func = function(name, param)\r
+               local pos = worldedit.pos1[name]\r
+               if pos == nil then\r
+                       minetest.chat_send_player(name, "No WorldEdit region selected")\r
+                       return\r
+               end\r
+\r
+               local found, _, size, nodename = param:find("(%d+)%s+([^%s]+)$")\r
+               if found == nil then\r
+                       minetest.chat_send_player(name, "Invalid usage: " .. param)\r
+                       return\r
+               end\r
+               if axis == "?" then\r
+                       axis = worldedit.player_axis(name)\r
+               end\r
+               if not worldedit.node_is_valid(pos, nodename) then\r
+                       minetest.chat_send_player(name, "Invalid node name: " .. param)\r
+                       return\r
+               end\r
+\r
+               local count = worldedit.spiral(pos, tonumber(size), nodename)\r
+               minetest.chat_send_player(name, count .. " nodes changed")\r
+       end,\r
+})\r
+\r
 minetest.register_chatcommand("/cylinder", {\r
        params = "x/y/z/? <length> <radius> <node>",\r
        description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>",\r
@@ -486,4 +516,4 @@ minetest.register_chatcommand("/load", {
 \r
                minetest.chat_send_player(name, count .. " nodes loaded")\r
        end,\r
-})
\ No newline at end of file
+})\r
diff --git a/spirals.lua b/spirals.lua
new file mode 100644 (file)
index 0000000..0a8dea1
--- /dev/null
@@ -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