]> git.lizzy.rs Git - worldedit.git/commitdiff
Super duper VoxelManipulator speedups to nearly every API function, and plus support...
authorAnthony Zhang <azhang9@gmail.com>
Sun, 21 Jul 2013 20:54:25 +0000 (16:54 -0400)
committerAnthony Zhang <azhang9@gmail.com>
Sun, 21 Jul 2013 20:54:25 +0000 (16:54 -0400)
WorldEdit API.md
worldedit/code.lua
worldedit/init.lua
worldedit/manipulations.lua
worldedit/primitives.lua
worldedit/queue.lua
worldedit/serialization.lua
worldedit/visualization.lua

index 12320f6c7488df7db4d98ae3eed1880eeb0fc4cd..1006932ab7a6c5f2132a843048ec9dd9abd41d3f 100644 (file)
@@ -208,6 +208,6 @@ Returns an error if the code fails or nil otherwise.
 \r
 ### error = worldedit.luatransform(pos1, pos2, code)\r
 \r
-Executes `code` as a Lua chunk in the global namespace with the variable pos available, for each node in a region defined by positions `pos1` and `pos2`.\r
+Executes `code` as a Lua chunk in the global namespace with the variable `pos` available, for each node in a region defined by positions `pos1` and `pos2`.\r
 \r
 Returns an error if the code fails or nil otherwise.
\ No newline at end of file
index 366688e95e0e33e69dcda85a5315bd35a0415d16..8e2d3621089a735ca25c356ac2e1b1e9c33efb7f 100644 (file)
@@ -24,6 +24,10 @@ worldedit.luatransform = function(pos1, pos2, code)
        end\r
        local operation = factory()\r
 \r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local pos = {x=pos1.x, y=0, z=0}\r
        while pos.x <= pos2.x do\r
                pos.y = pos1.y\r
index 11b90e957844d4be14b3da9f772b3dc76b1e74aa..f41d740ce22d8aaf1e5024c9f69b652766a19acd 100644 (file)
@@ -2,7 +2,7 @@ local path = minetest.get_modpath(minetest.get_current_modname())
 \r
 local loadmodule = function(path)\r
        return pcall(function()\r
-               dofile(path)\r
+               return dofile(path)\r
        end)\r
 end\r
 \r
index b92296a515d0f9ed26517c4a9ae1fd077cc2cff3..2180cb6f0dc56501fd91e78a9f86c3cbfc488934 100644 (file)
@@ -3,6 +3,7 @@ local minetest = minetest --local copy of global
 \r
 --wip: test the entire API again to make sure it works\r
 --wip: remove env parameter where no longer needed in chat commands module\r
+--wip: fix the queue\r
 \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
@@ -32,12 +33,19 @@ worldedit.set = function(pos1, pos2, nodename)
 \r
        --set up voxel manipulator\r
        local manip = minetest.get_voxel_manip()\r
-       manip:read_from_map(pos1, pos2)\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
 \r
-       --fill nodes table with node to be set\r
+       --fill emerged area with ignore\r
        local nodes = {}\r
+       local ignore = minetest.get_content_id("ignore")\r
+       for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
+               nodes[i] = ignore\r
+       end\r
+\r
+       --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\r
-       for i = 1, (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos2.z - pos1.z) do\r
+       for i in area:iterp(pos1, pos2) do\r
                nodes[i] = node_id\r
        end\r
 \r
@@ -53,39 +61,55 @@ end
 worldedit.replace = function(pos1, pos2, searchnode, replacenode)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
 \r
-       local node = {name=replacenode}\r
-       local add_node = minetest.add_node\r
-       local nodes = minetest.find_nodes_in_area(pos1, pos2, searchnode)\r
-       for _, pos in ipairs(nodes) do\r
-               add_node(pos, node)\r
+       --set up voxel manipulator\r
+       local manip = minetest.get_voxel_manip()\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
+\r
+       local nodes = manip:get_data()\r
+       local searchnode_id = minetest.get_content_id(searchnode)\r
+       local replacenode_id = minetest.get_content_id(replacenode)\r
+       local count = 0\r
+       for i in area:iterp(pos1, pos2) do --replace searchnode with replacenode\r
+               if nodes[i] == searchnode_id then\r
+                       nodes[i] = replacenode_id\r
+                       count = count + 1\r
+               end\r
        end\r
-       return #nodes\r
+\r
+       --update map nodes\r
+       manip:set_data(nodes)\r
+       manip:write_to_map()\r
+       manip:update_map()\r
+\r
+       return count\r
 end\r
 \r
 --replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced\r
-worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode) --wip: use voxelmanip get_data for this\r
+worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
 \r
-       local pos = {x=pos1.x, y=0, z=0}\r
-       local node = {name=replacenode}\r
-       local get_node, add_node = minetest.get_node, minetest.add_node\r
+       --set up voxel manipulator\r
+       local manip = minetest.get_voxel_manip()\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
+\r
+       local nodes = manip:get_data()\r
+       local searchnode_id = minetest.get_content_id(searchnode)\r
+       local replacenode_id = minetest.get_content_id(replacenode)\r
        local count = 0\r
-       while pos.x <= pos2.x do\r
-               pos.y = pos1.y\r
-               while pos.y <= pos2.y do\r
-                       pos.z = pos1.z\r
-                       while pos.z <= pos2.z do\r
-                               local name = get_node(pos).name\r
-                               if name ~= "ignore" and name ~= searchnode then\r
-                                       add_node(pos, node)\r
-                                       count = count + 1\r
-                               end\r
-                               pos.z = pos.z + 1\r
-                       end\r
-                       pos.y = pos.y + 1\r
+       for i in area:iterp(pos1, pos2) do --replace anything that is not searchnode with replacenode\r
+               if nodes[i] ~= searchnode_id then\r
+                       nodes[i] = replacenode_id\r
+                       count = count + 1\r
                end\r
-               pos.x = pos.x + 1\r
        end\r
+\r
+       --update map nodes\r
+       manip:set_data(nodes)\r
+       manip:write_to_map()\r
+       manip:update_map()\r
+\r
        return count\r
 end\r
 \r
@@ -94,7 +118,7 @@ worldedit.copy = function(pos1, pos2, axis, amount, env)
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        if env == nil then env = minetest.env end\r
 \r
-       --wip: copy slice by slice using schematic method in the copy axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time)\r
+       --wip: copy slice by slice using schematic method in the copy axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time), use voxelmanip to keep area loaded\r
        if amount < 0 then\r
                local pos = {x=pos1.x, y=0, z=0}\r
                while pos.x <= pos2.x do\r
@@ -144,7 +168,7 @@ worldedit.move = function(pos1, pos2, axis, amount, env)
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        if env == nil then env = minetest.env end\r
 \r
-       --wip: move slice by slice using schematic method in the move axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time and erase original after, using schematic method)\r
+       --wip: move slice by slice using schematic method in the move axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time and erase original after, using schematic method), use voxelmanip to keep area loaded\r
        if amount < 0 then\r
                local pos = {x=pos1.x, y=0, z=0}\r
                while pos.x <= pos2.x do\r
@@ -205,7 +229,7 @@ worldedit.stack = function(pos1, pos2, axis, count, env)
                amount = amount + length\r
                copy(pos1, pos2, axis, amount, env)\r
        end\r
-       return worldedit.volume(pos1, pos2)\r
+       return worldedit.volume(pos1, pos2) * count\r
 end\r
 \r
 --scales the region defined by positions `pos1` and `pos2` by an factor of positive integer `factor` with `pos1` as the origin, returning the number of nodes scaled, the new scaled position 1, and the new scaled position 2\r
@@ -216,14 +240,20 @@ worldedit.scale = function(pos1, pos2, factor)
        local get_node, get_meta, place_schematic = minetest.get_node, minetest.get_meta, minetest.place_schematic\r
        local placeholder_node = {name="", param1=0, param2=0}\r
        local nodes = {}\r
-       for i = 1, size ^ 3 do\r
+       for i = 1, factor ^ 3 do\r
                nodes[i] = placeholder_node\r
        end\r
-       local schematic = {size={x=size, y=size, z=size}, data=nodes}\r
+       local schematic = {size={x=factor, y=factor, z=factor}, data=nodes}\r
+\r
+       local size = factor - 1\r
+\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       local new_pos2 = {x=pos1.x + (pos2.x - pos1.x) * factor + size, y=pos1.y + (pos2.y - pos1.y) * factor + size, z=pos1.z + (pos2.z - pos1.z) * factor + size}\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, new_pos2)\r
 \r
        local pos = {x=pos2.x, y=0, z=0}\r
        local bigpos = {x=0, y=0, z=0}\r
-       size = factor - 1\r
        while pos.x >= pos1.x do\r
                pos.y = pos2.y\r
                while pos.y >= pos1.y do\r
@@ -236,14 +266,19 @@ worldedit.scale = function(pos1, pos2, factor)
                                local posx, posy, posz = pos1.x + (pos.x - pos1.x) * factor, pos1.y + (pos.y - pos1.y) * factor, pos1.z + (pos.z - pos1.z) * factor\r
 \r
                                --create large node\r
-                               placeholder_node[1], placeholder_node[3] = node.name, node.param2\r
+                               placeholder_node.name = node.name\r
+                               placeholder_node.param1, placeholder_node.param2 = node.param1, node.param2\r
                                bigpos.x, bigpos.y, bigpos.z = posx, posy, posz\r
                                place_schematic(bigpos, schematic)\r
-                               for x = 0, size do --fill in large node meta\r
-                                       for y = 0, size do\r
-                                               for z = 0, size do\r
-                                                       bigpos.x, bigpos.y, bigpos.z = posx + x, posy + y, posz + z\r
-                                                       get_meta(bigpos):from_table(meta) --set metadata of new node\r
+\r
+                               --fill in large node meta\r
+                               if next(meta.fields) ~= nil and next(meta.inventory) ~= nil then --node has meta fields\r
+                                       for x = 0, size do\r
+                                               for y = 0, size do\r
+                                                       for z = 0, size do\r
+                                                               bigpos.x, bigpos.y, bigpos.z = posx + x, posy + y, posz + z\r
+                                                               get_meta(bigpos):from_table(meta) --set metadata of new node\r
+                                                       end\r
                                                end\r
                                        end\r
                                end\r
@@ -253,8 +288,7 @@ worldedit.scale = function(pos1, pos2, factor)
                end\r
                pos.x = pos.x - 1\r
        end\r
-       local newpos2 = {x=pos1.x + (pos2.x - pos1.x) * factor + size, y=pos1.y + (pos2.y - pos1.y) * factor + size, z=pos1.z + (pos2.z - pos1.z) * factor + size}\r
-       return worldedit.volume(pos1, pos2), pos1, newpos2\r
+       return worldedit.volume(pos1, pos2) * (factor ^ 3), pos1, new_pos2\r
 end\r
 \r
 --transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes, returning the number of nodes transposed, the new transposed position 1, and the new transposed position 2\r
@@ -275,9 +309,16 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2, env)
        end\r
 \r
        --calculate the new position 2 after transposition\r
-       local newpos2 = {x=pos2.x, y=pos2.y, z=pos2.z}\r
-       newpos2[axis1] = pos1[axis1] + extent2\r
-       newpos2[axis2] = pos1[axis2] + extent1\r
+       local new_pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}\r
+       new_pos2[axis1] = pos1[axis1] + extent2\r
+       new_pos2[axis2] = pos1[axis2] + extent1\r
+\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       local upperbound = {x=pos2.x, y=pos2.y, z=pos2.z}\r
+       if upperbound[axis1] < new_pos2[axis1] then upperbound[axis1] = new_pos2[axis1] end\r
+       if upperbound[axis2] < new_pos2[axis2] then upperbound[axis2] = new_pos2[axis2] end\r
+       manip:read_from_map(pos1, upperbound)\r
 \r
        local pos = {x=pos1.x, y=0, z=0}\r
        local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node\r
@@ -306,34 +347,38 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2, env)
                end\r
                pos.x = pos.x + 1\r
        end\r
-       return worldedit.volume(pos1, pos2), pos1, newpos2\r
+       return worldedit.volume(pos1, pos2), pos1, new_pos2\r
 end\r
 \r
 --flips a region defined by the positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z"), returning the number of nodes flipped\r
 worldedit.flip = function(pos1, pos2, axis, env)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
 \r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        --wip: flip the region slice by slice along the flip axis using schematic method\r
        local pos = {x=pos1.x, y=0, z=0}\r
        local start = pos1[axis] + pos2[axis]\r
        pos2[axis] = pos1[axis] + math.floor((pos2[axis] - pos1[axis]) / 2)\r
-       if env == nil then env = minetest.env end\r
+       local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node\r
        while pos.x <= pos2.x do\r
                pos.y = pos1.y\r
                while pos.y <= pos2.y do\r
                        pos.z = pos1.z\r
                        while pos.z <= pos2.z do\r
-                               local node1 = env:get_node(pos)\r
-                               local meta1 = env:get_meta(pos):to_table()\r
+                               local node1 = get_node(pos)\r
+                               local meta1 = get_meta(pos):to_table()\r
                                local value = pos[axis]\r
                                pos[axis] = start - value\r
-                               local node2 = env:get_node(pos)\r
-                               local meta2 = env:get_meta(pos):to_table()\r
-                               env:add_node(pos, node1)\r
-                               env:get_meta(pos):from_table(meta1)\r
+                               local node2 = get_node(pos)\r
+                               local meta2 = get_meta(pos):to_table()\r
+                               add_node(pos, node1)\r
+                               get_meta(pos):from_table(meta1)\r
                                pos[axis] = value\r
-                               env:add_node(pos, node2)\r
-                               env:get_meta(pos):from_table(meta2)\r
+                               add_node(pos, node2)\r
+                               get_meta(pos):from_table(meta2)\r
                                pos.z = pos.z + 1\r
                        end\r
                        pos.y = pos.y + 1\r
@@ -372,7 +417,7 @@ worldedit.rotate = function(pos1, pos2, axis, angle, env)
 end\r
 \r
 --rotates all oriented nodes in a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise (90 degree increment) around the Y axis, returning the number of nodes oriented\r
-worldedit.orient = function(pos1, pos2, angle, env)\r
+worldedit.orient = function(pos1, pos2, angle, env) --wip: support 6D facedir rotation along arbitrary axis\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local registered_nodes = minetest.registered_nodes\r
 \r
@@ -394,6 +439,10 @@ worldedit.orient = function(pos1, pos2, angle, env)
        local wallmounted_substitution = wallmounted[angle]\r
        local facedir_substitution = facedir[angle]\r
 \r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local count = 0\r
        local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node\r
        local pos = {x=pos1.x, y=0, z=0}\r
@@ -431,6 +480,11 @@ end
 --fixes the lighting in a region defined by positions `pos1` and `pos2`, returning the number of nodes updated\r
 worldedit.fixlight = function(pos1, pos2, env)\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local nodes = minetest.find_nodes_in_area(pos1, pos2, "air")\r
        local dig_node = minetest.dig_node\r
        for _, pos in ipairs(nodes) do\r
index fecb6d8701a5f9a76b8e6385268fe16a34e7d833..85bc3b3959a43ad5e78861dee6dcd4822ed52a90 100644 (file)
@@ -5,27 +5,39 @@ local minetest = minetest --local copy of global
 worldedit.hollow_sphere = function(pos, radius, nodename)\r
        --set up voxel manipulator\r
        local manip = minetest.get_voxel_manip()\r
-       manip:read_from_map(\r
-               {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius},\r
-               {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius},\r
-       )\r
+       local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}\r
+       local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
 \r
-       local insert = table.insert\r
+       --fill emerged area with ignore\r
+       local nodes = {}\r
+       local ignore = minetest.get_content_id("ignore")\r
+       for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
+               nodes[i] = ignore\r
+       end\r
+\r
+       --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\r
-       local ignore_id = minetest.get_content_id("ignore")\r
        local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
-       local nodes = {}\r
+       local ystride, zstride = area.ystride, area.zstride\r
+       local x, y, z = -radius, -radius, -radius\r
        local count = 0\r
-       for x = -radius, radius do\r
-               for y = -radius, radius do\r
-                       for z = -radius, radius do\r
-                               local squared = x * x + y * y + z * z\r
-                               if squared >= min_radius and squared <= max_radius then --surface of sphere\r
-                                       insert(nodes, node_id)\r
-                                       count = count + 1\r
-                               else\r
-                                       insert(nodes, ignore_id)\r
-                               end\r
+       for i in area:iterp(pos1, pos2) do\r
+               local squared = x * x + y * y + z * z\r
+               if squared >= min_radius and squared <= max_radius then --position is on surface of sphere\r
+                       nodes[i] = node_id\r
+                       count = count + 1\r
+               end\r
+\r
+               --move to the next position\r
+               x = x + 1\r
+               if x > radius then\r
+                       x = -radius\r
+                       y = y + 1\r
+                       if y > radius then\r
+                               y = -radius\r
+                               z = z + 1\r
                        end\r
                end\r
        end\r
@@ -42,26 +54,38 @@ end
 worldedit.sphere = function(pos, radius, nodename)\r
        --set up voxel manipulator\r
        local manip = minetest.get_voxel_manip()\r
-       manip:read_from_map(\r
-               {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius},\r
-               {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius},\r
-       )\r
+       local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}\r
+       local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
 \r
-       local insert = table.insert\r
+       --fill emerged area with ignore\r
+       local nodes = {}\r
+       local ignore = minetest.get_content_id("ignore")\r
+       for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
+               nodes[i] = ignore\r
+       end\r
+\r
+       --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\r
-       local ignore_id = minetest.get_content_id("ignore")\r
        local max_radius = radius * (radius + 1)\r
-       local nodes = {}\r
+       local ystride, zstride = area.ystride, area.zstride\r
+       local x, y, z = -radius, -radius, -radius\r
        local count = 0\r
-       for x = -radius, radius do\r
-               for y = -radius, radius do\r
-                       for z = -radius, radius do\r
-                               if x * x + y * y + z * z <= max_radius then --inside sphere\r
-                                       insert(nodes, node_id)\r
-                                       count = count + 1\r
-                               else\r
-                                       insert(nodes, ignore_id)\r
-                               end\r
+       for i in area:iterp(pos1, pos2) do\r
+               if x * x + y * y + z * z <= max_radius then --position is inside sphere\r
+                       nodes[i] = node_id\r
+                       count = count + 1\r
+               end\r
+\r
+               --move to the next position\r
+               x = x + 1\r
+               if x > radius then\r
+                       x = -radius\r
+                       y = y + 1\r
+                       if y > radius then\r
+                               y = -radius\r
+                               z = z + 1\r
                        end\r
                end\r
        end\r
@@ -75,30 +99,42 @@ worldedit.sphere = function(pos, radius, nodename)
 end\r
 \r
 --adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added\r
-worldedit.hollow_dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed\r
+worldedit.hollow_dome = function(pos, radius, nodename)\r
        --set up voxel manipulator\r
        local manip = minetest.get_voxel_manip()\r
-       manip:read_from_map(\r
-               {x=pos.x - radius, y=pos.y, z=pos.z - radius},\r
-               {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius},\r
-       )\r
+       local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}\r
+       local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
 \r
-       local insert = table.insert\r
+       --fill emerged area with ignore\r
+       local nodes = {}\r
+       local ignore = minetest.get_content_id("ignore")\r
+       for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
+               nodes[i] = ignore\r
+       end\r
+\r
+       --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\r
-       local ignore_id = minetest.get_content_id("ignore")\r
        local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
-       local nodes = {}\r
+       local ystride, zstride = area.ystride, area.zstride\r
+       local x, y, z = -radius, 0, -radius\r
        local count = 0\r
-       for x = -radius, radius do\r
-               for y = 0, radius do\r
-                       for z = -radius, radius do\r
-                               local squared = x * x + y * y + z * z\r
-                               if squared >= min_radius and squared <= max_radius then --surface of dome\r
-                                       insert(nodes, node_id)\r
-                                       count = count + 1\r
-                               else\r
-                                       insert(nodes, ignore_id)\r
-                               end\r
+       for i in area:iterp(pos1, pos2) do\r
+               local squared = x * x + y * y + z * z\r
+               if squared >= min_radius and squared <= max_radius then --position is on surface of sphere\r
+                       nodes[i] = node_id\r
+                       count = count + 1\r
+               end\r
+\r
+               --move to the next position\r
+               x = x + 1\r
+               if x > radius then\r
+                       x = -radius\r
+                       y = y + 1\r
+                       if y > radius then\r
+                               y = 0\r
+                               z = z + 1\r
                        end\r
                end\r
        end\r
@@ -115,26 +151,38 @@ end
 worldedit.dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed\r
        --set up voxel manipulator\r
        local manip = minetest.get_voxel_manip()\r
-       manip:read_from_map(\r
-               {x=pos.x - radius, y=pos.y, z=pos.z - radius},\r
-               {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius},\r
-       )\r
+       local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}\r
+       local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}\r
+       local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)\r
+       local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})\r
 \r
-       local insert = table.insert\r
+       --fill emerged area with ignore\r
+       local nodes = {}\r
+       local ignore = minetest.get_content_id("ignore")\r
+       for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do\r
+               nodes[i] = ignore\r
+       end\r
+\r
+       --fill selected area with node\r
        local node_id = minetest.get_content_id(nodename)\r
-       local ignore_id = minetest.get_content_id("ignore")\r
        local max_radius = radius * (radius + 1)\r
-       local nodes = {}\r
+       local ystride, zstride = area.ystride, area.zstride\r
+       local x, y, z = -radius, 0, -radius\r
        local count = 0\r
-       for x = -radius, radius do\r
-               for y = 0, radius do\r
-                       for z = -radius, radius do\r
-                               if x * x + y * y + z * z <= max_radius then --inside dome\r
-                                       insert(nodes, node_id)\r
-                                       count = count + 1\r
-                               else\r
-                                       insert(nodes, ignore_id)\r
-                               end\r
+       for i in area:iterp(pos1, pos2) do\r
+               if x * x + y * y + z * z <= max_radius then --position is inside sphere\r
+                       nodes[i] = node_id\r
+                       count = count + 1\r
+               end\r
+\r
+               --move to the next position\r
+               x = x + 1\r
+               if x > radius then\r
+                       x = -radius\r
+                       y = y + 1\r
+                       if y > radius then\r
+                               y = 0\r
+                               z = z + 1\r
                        end\r
                end\r
        end\r
@@ -148,7 +196,7 @@ worldedit.dome = function(pos, radius, nodename) --wip: use bresenham sphere for
 end\r
 \r
 --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\r
-worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)\r
+worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) --wip: rewrite this using voxelmanip\r
        local other1, other2\r
        if axis == "x" then\r
                other1, other2 = "y", "z"\r
@@ -216,7 +264,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
 end\r
 \r
 --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\r
-worldedit.cylinder = function(pos, axis, length, radius, nodename, env)\r
+worldedit.cylinder = function(pos, axis, length, radius, nodename, env) --wip: rewrite this using voxelmanip\r
        local other1, other2\r
        if axis == "x" then\r
                other1, other2 = "y", "z"\r
@@ -281,7 +329,7 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename, env)
 end\r
 \r
 --adds a pyramid centered at `pos` with height `height`, composed of `nodename`, returning the number of nodes added\r
-worldedit.pyramid = function(pos, height, nodename, env)\r
+worldedit.pyramid = function(pos, height, nodename, env) --wip: rewrite this using voxelmanip\r
        local pos1x, pos1y, pos1z = pos.x - height, pos.y, pos.z - height\r
        local pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height\r
        local pos = {x=0, y=pos1y, z=0}\r
index 9246b9daa945f5571c6f5fec9754bc0c99ece1a5..e9f3ac33a499af5dcef373360665b5d709f4344e 100644 (file)
@@ -12,41 +12,39 @@ minetest.register_globalstep(function(dtime)
     local elapsed = 0
     local env = minetest.env
     while worldedit.lower <= worldedit.higher and elapsed <= worldedit.MAXIMUM_TIME do
-       local entry = worldedit.queue[worldedit.lower]
+        local entry = worldedit.queue[worldedit.lower]
         if entry.t == "set_node" then
             env:set_node(entry.pos, entry.node)
-           elapsed = elapsed + 0.0002
+            elapsed = elapsed + 0.0002
         elseif entry.t == "remove_node" then
             env:remove_node(entry.pos)
-           elapsed = elapsed + 0.0002
+            elapsed = elapsed + 0.0002
         elseif entry.t == "place_node" then
             env:place_node(entry.pos, entry.node)
-           elapsed = elapsed + 0.001
+            elapsed = elapsed + 0.001
         elseif entry.t == "dig_node" then
             env:dig_node(entry.pos)
-           elapsed = elapsed + 0.001
+            elapsed = elapsed + 0.001
         elseif entry.t == "add_entity" then
             env:add_entity(entry.pos, entry.name)
-           elapsed = elapsed + 0.005
+            elapsed = elapsed + 0.005
         elseif entry.t == "add_item" then
             env:add_item(entry.pos, entry.item)
-           elapsed = elapsed + 0.005
+            elapsed = elapsed + 0.005
         elseif entry.t == "meta_from_table" then
             env:get_meta(entry.pos):from_table(entry.table)
-           elapsed = elapsed + 0.0002
+            elapsed = elapsed + 0.0002
         else
             print("Unknown queue event type: " .. entry.t)
         end
         worldedit.queue[worldedit.lower] = nil
-       worldedit.lower = worldedit.lower + 1
+        worldedit.lower = worldedit.lower + 1
     end
 end)
 
-do
-       worldedit.enqueue = function(value)
-               worldedit.higher = worldedit.higher + 1
-               worldedit.queue[worldedit.higher] = value
-       end
+worldedit.enqueue = function(value)
+        worldedit.higher = worldedit.higher + 1
+        worldedit.queue[worldedit.higher] = value
 end
 
 function table.copy(t, seen)
@@ -123,4 +121,3 @@ worldedit.queue_aliasenv = {
     add_entity      = queue_addentity,
     add_item        = queue_additem,
 }
-
index 74cb2186e2b6024ab28835a5faa9766554e6b3bf..e0d960d3ee7b2845aec3a5d6c7c4979211ef3bd9 100644 (file)
@@ -34,6 +34,10 @@ end
 \r
 --converts the region defined by positions `pos1` and `pos2` into a single string, returning the serialized data and the number of nodes serialized\r
 worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and whether they can be serialized\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local pos = {x=pos1.x, y=0, z=0}\r
        local count = 0\r
@@ -141,7 +145,24 @@ worldedit.allocate = function(originpos, value)
                        count = count + 1\r
                end\r
        elseif version == 4 then --current nested table format\r
-               local nodes = minetest.deserialize(value)\r
+               --wip: this is a filthy hack that works surprisingly well\r
+               value = value:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1)\r
+               local escaped = value:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)\r
+               local startpos, startpos1, endpos = 1, 1\r
+               local nodes = {}\r
+               while true do\r
+                       startpos, endpos = escaped:find("},%s*{", startpos)\r
+                       if not startpos then\r
+                               break\r
+                       end\r
+                       local current = value:sub(startpos1, startpos)\r
+                       table.insert(nodes, minetest.deserialize("return " .. current))\r
+                       startpos, startpos1 = endpos, endpos\r
+               end\r
+               table.insert(nodes, minetest.deserialize("return " .. value:sub(startpos1)))\r
+\r
+               --local nodes = minetest.deserialize(value) --wip: this is broken for larger tables in the current version of LuaJIT\r
+\r
                count = #nodes\r
                for index = 1, count do\r
                        local entry = nodes[index]\r
@@ -161,7 +182,7 @@ end
 \r
 --loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized\r
 --contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.deserialize = function(originpos, value)\r
+worldedit.deserialize = function(originpos, value) --wip: use voxelmanip to make sure the blocks are loaded\r
        local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
        local count = 0\r
        local add_node, get_meta = minetest.add_node, minetest.get_meta\r
index c07a13907c5caa8e8b5b98d12307749449260888..d65414653d00f299db8245226707913ae1836f01 100644 (file)
@@ -33,6 +33,10 @@ minetest.register_node("worldedit:placeholder", {
 \r
 --hides all nodes in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes, returning the number of nodes hidden\r
 worldedit.hide = function(pos1, pos2)\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local pos = {x=pos1.x, y=0, z=0}\r
        local placeholder = {name="worldedit:placeholder", param1=0, param2=0}\r
@@ -60,6 +64,10 @@ end
 \r
 --suppresses all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes, returning the number of nodes suppressed\r
 worldedit.suppress = function(pos1, pos2, nodename)\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local placeholder = {name="worldedit:placeholder", param1=0, param2=0}\r
        local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename)\r
@@ -77,7 +85,11 @@ worldedit.suppress = function(pos1, pos2, nodename)
 end\r
 \r
 --highlights all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively hiding all other nodes, returning the number of nodes found\r
-worldedit.highlight = function(pos1, pos2, nodename) --wip: speed this up with voxmanip get_data\r
+worldedit.highlight = function(pos1, pos2, nodename) --wip: speed this up with voxmanip get_data to speed up searching\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local pos = {x=pos1.x, y=0, z=0}\r
        local placeholder = {name="worldedit:placeholder", param1=0, param2=0}\r
@@ -110,6 +122,10 @@ end
 \r
 --restores all nodes hidden with WorldEdit functions in a region defined by positions `pos1` and `pos2`, returning the number of nodes restored\r
 worldedit.restore = function(pos1, pos2)\r
+       --make area stay loaded\r
+       local manip = minetest.get_voxel_manip()\r
+       manip:read_from_map(pos1, pos2)\r
+\r
        local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
        local node = {name="", param1=0, param2=0}\r
        local nodes = minetest.find_nodes_in_area(pos1, pos2, "worldedit:placeholder")\r