From ac5e801834245bcf93f720d290a36b09a2527576 Mon Sep 17 00:00:00 2001 From: Anthony Zhang Date: Fri, 12 Jul 2013 14:22:30 -0400 Subject: [PATCH] Even bigger speed gains by using LuaVoxelManipulator in a few choice places! Faster //set, //cylinder, etc., but plenty of room for improvements still. --- Chat Commands.md | 4 +- worldedit/code.lua | 1 + worldedit/compatibility.lua | 1 + worldedit/manipulations.lua | 21 ++-- worldedit/primitives.lua | 196 +++++++++++++++++++++++------------- worldedit/queue.lua | 1 + worldedit/serialization.lua | 31 +++--- worldedit/visualization.lua | 3 +- 8 files changed, 161 insertions(+), 97 deletions(-) diff --git a/Chat Commands.md b/Chat Commands.md index 9951ca3..6bd1772 100644 --- a/Chat Commands.md +++ b/Chat Commands.md @@ -276,8 +276,8 @@ Executes as a Lua chunk in the global namespace. Executes as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region. - //luatransform minetest.env:add_node(pos, {name="default:stone"}) - //luatransform if minetest.env:get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"}) + //luatransform minetest.add_node(pos, {name="default:stone"}) + //luatransform if minetest.get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"}) ### //mtschemcreate diff --git a/worldedit/code.lua b/worldedit/code.lua index ee2d340..366688e 100644 --- a/worldedit/code.lua +++ b/worldedit/code.lua @@ -1,4 +1,5 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global --executes `code` as a Lua chunk in the global namespace, returning an error if the code fails or nil otherwise worldedit.lua = function(code) diff --git a/worldedit/compatibility.lua b/worldedit/compatibility.lua index 0ea2333..f6971cc 100644 --- a/worldedit/compatibility.lua +++ b/worldedit/compatibility.lua @@ -1,4 +1,5 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global worldedit.allocate_old = worldedit.allocate worldedit.deserialize_old = worldedit.deserialize diff --git a/worldedit/manipulations.lua b/worldedit/manipulations.lua index 141059f..b92296a 100644 --- a/worldedit/manipulations.lua +++ b/worldedit/manipulations.lua @@ -1,4 +1,5 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global --wip: test the entire API again to make sure it works --wip: remove env parameter where no longer needed in chat commands module @@ -29,16 +30,22 @@ end worldedit.set = function(pos1, pos2, nodename) local pos1, pos2 = worldedit.sort_pos(pos1, pos2) - local size = {x=pos2.x - pos1.x, y=pos2.y - pos1.y, z=pos2.z - pos1.z} - local nodes = {} + --set up voxel manipulator + local manip = minetest.get_voxel_manip() + manip:read_from_map(pos1, pos2) --fill nodes table with node to be set - local node = {name=nodename, param1=0, param2=0} - for i = 1, (size.x * size.y * size.z) do - nodes[i] = node + local nodes = {} + local node_id = minetest.get_content_id(nodename) + for i = 1, (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos2.z - pos1.z) do + nodes[i] = node_id end - minetest.place_schematic(pos1, {size=size, data=nodes}) + --update map nodes + manip:set_data(nodes) + manip:write_to_map() + manip:update_map() + return worldedit.volume(pos1, pos2) end @@ -56,7 +63,7 @@ worldedit.replace = function(pos1, pos2, searchnode, replacenode) end --replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced -worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode) +worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode) --wip: use voxelmanip get_data for this local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos = {x=pos1.x, y=0, z=0} diff --git a/worldedit/primitives.lua b/worldedit/primitives.lua index 7582fa0..fecb6d8 100644 --- a/worldedit/primitives.lua +++ b/worldedit/primitives.lua @@ -1,100 +1,149 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global --adds a hollow sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added worldedit.hollow_sphere = function(pos, radius, nodename) + --set up voxel manipulator + local manip = minetest.get_voxel_manip() + manip:read_from_map( + {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}, + {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}, + ) + local insert = table.insert - local node = {name=nodename, param1=0, param2=0} - local ignore = {name="ignore", param1=0, param2=0} + local node_id = minetest.get_content_id(nodename) + local ignore_id = minetest.get_content_id("ignore") + local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) local nodes = {} local count = 0 - local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) for x = -radius, radius do for y = -radius, radius do for z = -radius, radius do local squared = x * x + y * y + z * z - if squared >= min_radius and squared <= max_radius then - insert(nodes, node) + if squared >= min_radius and squared <= max_radius then --surface of sphere + insert(nodes, node_id) count = count + 1 else - insert(nodes, ignore) + insert(nodes, ignore_id) end end end end - minetest.place_schematic({x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes}) + + --update map nodes + manip:set_data(nodes) + manip:write_to_map() + manip:update_map() + return count end --adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added worldedit.sphere = function(pos, radius, nodename) + --set up voxel manipulator + local manip = minetest.get_voxel_manip() + manip:read_from_map( + {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}, + {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}, + ) + local insert = table.insert - local node = {name=nodename, param1=0, param2=0} - local ignore = {name="ignore", param1=0, param2=0} + local node_id = minetest.get_content_id(nodename) + local ignore_id = minetest.get_content_id("ignore") + local max_radius = radius * (radius + 1) local nodes = {} local count = 0 - local max_radius = radius * (radius + 1) for x = -radius, radius do for y = -radius, radius do for z = -radius, radius do - if x * x + y * y + z * z <= max_radius then - insert(nodes, node) + if x * x + y * y + z * z <= max_radius then --inside sphere + insert(nodes, node_id) count = count + 1 else - insert(nodes, ignore) + insert(nodes, ignore_id) end end end end - minetest.place_schematic({x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes}) + + --update map nodes + manip:set_data(nodes) + manip:write_to_map() + manip:update_map() + return count end --adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added worldedit.hollow_dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed + --set up voxel manipulator + local manip = minetest.get_voxel_manip() + manip:read_from_map( + {x=pos.x - radius, y=pos.y, z=pos.z - radius}, + {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}, + ) + local insert = table.insert - local node = {name=nodename, param1=0, param2=0} - local ignore = {name="ignore", param1=0, param2=0} + local node_id = minetest.get_content_id(nodename) + local ignore_id = minetest.get_content_id("ignore") + local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) local nodes = {} local count = 0 - local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) for x = -radius, radius do for y = 0, radius do for z = -radius, radius do local squared = x * x + y * y + z * z - if squared >= min_radius and squared <= max_radius then - insert(nodes, node) + if squared >= min_radius and squared <= max_radius then --surface of dome + insert(nodes, node_id) count = count + 1 else - insert(nodes, ignore) + insert(nodes, ignore_id) end end end end - minetest.place_schematic({x=pos.x - radius, y=pos.y, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes}) + + --update map nodes + manip:set_data(nodes) + manip:write_to_map() + manip:update_map() + return count end --adds a dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added worldedit.dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed + --set up voxel manipulator + local manip = minetest.get_voxel_manip() + manip:read_from_map( + {x=pos.x - radius, y=pos.y, z=pos.z - radius}, + {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}, + ) + local insert = table.insert - local node = {name=nodename, param1=0, param2=0} - local ignore = {name="ignore", param1=0, param2=0} + local node_id = minetest.get_content_id(nodename) + local ignore_id = minetest.get_content_id("ignore") + local max_radius = radius * (radius + 1) local nodes = {} local count = 0 - local max_radius = radius * (radius + 1) for x = -radius, radius do for y = 0, radius do for z = -radius, radius do - if x * x + y * y + z * z <= max_radius then - insert(nodes, node) + if x * x + y * y + z * z <= max_radius then --inside dome + insert(nodes, node_id) count = count + 1 else - insert(nodes, ignore) + insert(nodes, ignore_id) end end end end - minetest.place_schematic({x=pos.x - radius, y=pos.y, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes}) + + --update map nodes + manip:set_data(nodes) + manip:write_to_map() + manip:update_map() + return count end @@ -109,6 +158,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) other1, other2 = "x", "y" end + --handle negative lengths local currentpos = {x=pos.x, y=pos.y, z=pos.z} if length < 0 then length = -length @@ -151,7 +201,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) currentpos[other1] = first1 place_schematic(currentpos, schematic) --octant 7 - count = count + (length * 8) --wip: broken because sometimes currentpos is repeated + count = count + 8 --wip: broken because sometimes currentpos is repeated --move to next location delta = delta + (offset1 * 2) + 1 @@ -161,6 +211,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) end offset1 = offset1 + 1 end + count = count * length --apply the length to the number of nodes return count end @@ -175,52 +226,57 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename, env) other1, other2 = "x", "y" end - --wip: make this faster using the schematic method by adding columns in a circle pattern like in hollow_cylinder, or by adding whole 2D slices using custom sized schematics - if env == nil then env = minetest.env end + --handle negative lengths local currentpos = {x=pos.x, y=pos.y, z=pos.z} - local node = {name=nodename} - local count = 0 - local step = 1 if length < 0 then length = -length - step = -1 + currentpos[axis] = currentpos[axis] - length end + + --create schematic for single node column along the axis + local node = {name=nodename, param1=0, param2=0} + local nodes = {} for i = 1, length do - local offset1, offset2 = 0, radius - local delta = -radius - while offset1 <= offset2 do - --connect each pair of octants - currentpos[other1] = pos[other1] - offset1 - local second1, second2 = pos[other2] + offset2, pos[other2] - offset2 - for i = 0, offset1 * 2 do - currentpos[other2] = second1 - env:add_node(currentpos, node) --octant 1 to 4 - currentpos[other2] = second2 - env:add_node(currentpos, node) --octant 5 to 8 - currentpos[other1] = currentpos[other1] + 1 - end - currentpos[other1] = pos[other1] - offset2 - local second1, second2 = pos[other2] + offset1, pos[other2] - offset1 - for i = 0, offset2 * 2 do - currentpos[other2] = second1 - env:add_node(currentpos, node) --octant 2 to 3 - currentpos[other2] = second2 - env:add_node(currentpos, node) --octant 6 to 7 - currentpos[other1] = currentpos[other1] + 1 - end - - count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken - - --move to next location - delta = delta + (offset1 * 2) + 1 - offset1 = offset1 + 1 - if delta >= 0 then - offset2 = offset2 - 1 - delta = delta - (offset2 * 2) - end - end - currentpos[axis] = currentpos[axis] + step + nodes[i] = node end + local schematic = {size={[axis]=length, [other1]=1, [other2]=1}, data=nodes} + + local place_schematic = minetest.place_schematic + local count = 0 + local offset1, offset2 = 0, radius + local delta = -radius + while offset1 <= offset2 do + --connect each pair of octants taking advantage of symmetry along two axes + currentpos[other1] = pos[other1] - offset1 + local second1, second2 = pos[other2] + offset2, pos[other2] - offset2 + for i = 0, offset1 * 2 do + currentpos[other2] = second1 + place_schematic(currentpos, schematic) --octant 1 to 4 + currentpos[other2] = second2 + place_schematic(currentpos, schematic) --octant 5 to 8 + currentpos[other1] = currentpos[other1] + 1 + end + currentpos[other1] = pos[other1] - offset2 + local second1, second2 = pos[other2] + offset1, pos[other2] - offset1 + for i = 0, offset2 * 2 do + currentpos[other2] = second1 + place_schematic(currentpos, schematic) --octant 2 to 3 + currentpos[other2] = second2 + place_schematic(currentpos, schematic) --octant 6 to 7 + currentpos[other1] = currentpos[other1] + 1 + end + + count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken since node positions may coincide + + --move to next location + delta = delta + (offset1 * 2) + 1 + offset1 = offset1 + 1 + if delta >= 0 then + offset2 = offset2 - 1 + delta = delta - (offset2 * 2) + end + end + count = count * length --apply the length to the number of nodes return count end @@ -274,7 +330,7 @@ worldedit.spiral = function(pos, width, height, spacer, nodename, env) --wip: cl end return ret end - if env == nil then env = minetest.env end + if env == nil then env = minetest.env end -- connect the joined parts local spiral = spiralt(width) height = tonumber(height) diff --git a/worldedit/queue.lua b/worldedit/queue.lua index cf80cf7..9246b9d 100644 --- a/worldedit/queue.lua +++ b/worldedit/queue.lua @@ -1,4 +1,5 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global worldedit.queue = {} worldedit.lower = 1 diff --git a/worldedit/serialization.lua b/worldedit/serialization.lua index 6481fae..74cb218 100644 --- a/worldedit/serialization.lua +++ b/worldedit/serialization.lua @@ -1,4 +1,5 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global --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) @@ -37,16 +38,16 @@ worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and wheth local pos = {x=pos1.x, y=0, z=0} local count = 0 local result = {} - local env = minetest.env + local get_node, get_meta = minetest.get_node, minetest.get_meta while pos.x <= pos2.x do pos.y = pos1.y while pos.y <= pos2.y do pos.z = pos1.z while pos.z <= pos2.z do - local node = env:get_node(pos) + local node = get_node(pos) if node.name ~= "air" and node.name ~= "ignore" then count = count + 1 - local meta = env:get_meta(pos):to_table() + local meta = get_meta(pos):to_table() --convert metadata itemstacks to itemstrings for name, inventory in pairs(meta.inventory) do @@ -160,10 +161,10 @@ end --loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized --contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible) -worldedit.deserialize = function(originpos, value, env) +worldedit.deserialize = function(originpos, value) local originx, originy, originz = originpos.x, originpos.y, originpos.z local count = 0 - if env == nil then env = minetest.env end + local add_node, get_meta = minetest.add_node, minetest.get_meta local version = worldedit.valueversion(value) if version == 1 or version == 2 then --original flat table format --obtain the node table @@ -190,27 +191,23 @@ worldedit.deserialize = function(originpos, value, env) local entry = nodes[index] local pos = entry[1] pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z - env:add_node(pos, entry[2]) + add_node(pos, entry[2]) end else --previous meta flat table format for index = 1, #nodes do local entry = nodes[index] entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z - env:add_node(entry, entry) --entry acts both as position and as node - env:get_meta(entry):from_table(entry.meta) + add_node(entry, entry) --entry acts both as position and as node + get_meta(entry):from_table(entry.meta) end end elseif version == 3 then --previous list format local pos = {x=0, y=0, z=0} local node = {name="", param1=0, param2=0} for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries - pos.x = originx + tonumber(x) - pos.y = originy + tonumber(y) - pos.z = originz + tonumber(z) - node.name = name - node.param1 = param1 - node.param2 = param2 - env:add_node(pos, node) + pos.x, pos.y, pos.z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z) + node.name, node.param1, node.param2 = name, param1, param2 + add_node(pos, node) count = count + 1 end elseif version == 4 then --current nested table format @@ -237,13 +234,13 @@ worldedit.deserialize = function(originpos, value, env) for index = 1, count do local entry = nodes[index] entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z - env:add_node(entry, entry) --entry acts both as position and as node + add_node(entry, entry) --entry acts both as position and as node end --load the metadata for index = 1, count do local entry = nodes[index] - env:get_meta(entry):from_table(entry.meta) + get_meta(entry):from_table(entry.meta) end end return count diff --git a/worldedit/visualization.lua b/worldedit/visualization.lua index 3d697a2..c07a139 100644 --- a/worldedit/visualization.lua +++ b/worldedit/visualization.lua @@ -1,4 +1,5 @@ worldedit = worldedit or {} +local minetest = minetest --local copy of global --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) @@ -76,7 +77,7 @@ worldedit.suppress = function(pos1, pos2, nodename) end --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 -worldedit.highlight = function(pos1, pos2, nodename) +worldedit.highlight = function(pos1, pos2, nodename) --wip: speed this up with voxmanip get_data local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos = {x=pos1.x, y=0, z=0} local placeholder = {name="worldedit:placeholder", param1=0, param2=0}