forked from mtcontrib/Minetest-WorldEdit
Even bigger speed gains by using LuaVoxelManipulator in a few choice places! Faster //set, //cylinder, etc., but plenty of room for improvements still.
This commit is contained in:
parent
b6bc841c39
commit
ac5e801834
@ -276,8 +276,8 @@ Executes <code> as a Lua chunk in the global namespace.
|
|||||||
|
|
||||||
Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region.
|
Executes <code> 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 minetest.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 if minetest.get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"})
|
||||||
|
|
||||||
### //mtschemcreate <file>
|
### //mtschemcreate <file>
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
worldedit = worldedit or {}
|
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
|
--executes `code` as a Lua chunk in the global namespace, returning an error if the code fails or nil otherwise
|
||||||
worldedit.lua = function(code)
|
worldedit.lua = function(code)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
worldedit = worldedit or {}
|
worldedit = worldedit or {}
|
||||||
|
local minetest = minetest --local copy of global
|
||||||
|
|
||||||
worldedit.allocate_old = worldedit.allocate
|
worldedit.allocate_old = worldedit.allocate
|
||||||
worldedit.deserialize_old = worldedit.deserialize
|
worldedit.deserialize_old = worldedit.deserialize
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
worldedit = worldedit or {}
|
worldedit = worldedit or {}
|
||||||
|
local minetest = minetest --local copy of global
|
||||||
|
|
||||||
--wip: test the entire API again to make sure it works
|
--wip: test the entire API again to make sure it works
|
||||||
--wip: remove env parameter where no longer needed in chat commands module
|
--wip: remove env parameter where no longer needed in chat commands module
|
||||||
@ -29,16 +30,22 @@ end
|
|||||||
worldedit.set = function(pos1, pos2, nodename)
|
worldedit.set = function(pos1, pos2, nodename)
|
||||||
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
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}
|
--set up voxel manipulator
|
||||||
local nodes = {}
|
local manip = minetest.get_voxel_manip()
|
||||||
|
manip:read_from_map(pos1, pos2)
|
||||||
|
|
||||||
--fill nodes table with node to be set
|
--fill nodes table with node to be set
|
||||||
local node = {name=nodename, param1=0, param2=0}
|
local nodes = {}
|
||||||
for i = 1, (size.x * size.y * size.z) do
|
local node_id = minetest.get_content_id(nodename)
|
||||||
nodes[i] = node
|
for i = 1, (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos2.z - pos1.z) do
|
||||||
|
nodes[i] = node_id
|
||||||
end
|
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)
|
return worldedit.volume(pos1, pos2)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -56,7 +63,7 @@ worldedit.replace = function(pos1, pos2, searchnode, replacenode)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced
|
--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 pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
||||||
|
|
||||||
local pos = {x=pos1.x, y=0, z=0}
|
local pos = {x=pos1.x, y=0, z=0}
|
||||||
|
@ -1,100 +1,149 @@
|
|||||||
worldedit = worldedit or {}
|
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
|
--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)
|
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 insert = table.insert
|
||||||
local node = {name=nodename, param1=0, param2=0}
|
local node_id = minetest.get_content_id(nodename)
|
||||||
local ignore = {name="ignore", param1=0, param2=0}
|
local ignore_id = minetest.get_content_id("ignore")
|
||||||
|
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
||||||
local nodes = {}
|
local nodes = {}
|
||||||
local count = 0
|
local count = 0
|
||||||
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
|
||||||
for x = -radius, radius do
|
for x = -radius, radius do
|
||||||
for y = -radius, radius do
|
for y = -radius, radius do
|
||||||
for z = -radius, radius do
|
for z = -radius, radius do
|
||||||
local squared = x * x + y * y + z * z
|
local squared = x * x + y * y + z * z
|
||||||
if squared >= min_radius and squared <= max_radius then
|
if squared >= min_radius and squared <= max_radius then --surface of sphere
|
||||||
insert(nodes, node)
|
insert(nodes, node_id)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
else
|
else
|
||||||
insert(nodes, ignore)
|
insert(nodes, ignore_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
--adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
|
--adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
|
||||||
worldedit.sphere = function(pos, radius, nodename)
|
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 insert = table.insert
|
||||||
local node = {name=nodename, param1=0, param2=0}
|
local node_id = minetest.get_content_id(nodename)
|
||||||
local ignore = {name="ignore", param1=0, param2=0}
|
local ignore_id = minetest.get_content_id("ignore")
|
||||||
|
local max_radius = radius * (radius + 1)
|
||||||
local nodes = {}
|
local nodes = {}
|
||||||
local count = 0
|
local count = 0
|
||||||
local max_radius = radius * (radius + 1)
|
|
||||||
for x = -radius, radius do
|
for x = -radius, radius do
|
||||||
for y = -radius, radius do
|
for y = -radius, radius do
|
||||||
for z = -radius, radius do
|
for z = -radius, radius do
|
||||||
if x * x + y * y + z * z <= max_radius then
|
if x * x + y * y + z * z <= max_radius then --inside sphere
|
||||||
insert(nodes, node)
|
insert(nodes, node_id)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
else
|
else
|
||||||
insert(nodes, ignore)
|
insert(nodes, ignore_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
--adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
|
--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
|
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 insert = table.insert
|
||||||
local node = {name=nodename, param1=0, param2=0}
|
local node_id = minetest.get_content_id(nodename)
|
||||||
local ignore = {name="ignore", param1=0, param2=0}
|
local ignore_id = minetest.get_content_id("ignore")
|
||||||
|
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
||||||
local nodes = {}
|
local nodes = {}
|
||||||
local count = 0
|
local count = 0
|
||||||
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
|
||||||
for x = -radius, radius do
|
for x = -radius, radius do
|
||||||
for y = 0, radius do
|
for y = 0, radius do
|
||||||
for z = -radius, radius do
|
for z = -radius, radius do
|
||||||
local squared = x * x + y * y + z * z
|
local squared = x * x + y * y + z * z
|
||||||
if squared >= min_radius and squared <= max_radius then
|
if squared >= min_radius and squared <= max_radius then --surface of dome
|
||||||
insert(nodes, node)
|
insert(nodes, node_id)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
else
|
else
|
||||||
insert(nodes, ignore)
|
insert(nodes, ignore_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
--adds a dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
|
--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
|
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 insert = table.insert
|
||||||
local node = {name=nodename, param1=0, param2=0}
|
local node_id = minetest.get_content_id(nodename)
|
||||||
local ignore = {name="ignore", param1=0, param2=0}
|
local ignore_id = minetest.get_content_id("ignore")
|
||||||
|
local max_radius = radius * (radius + 1)
|
||||||
local nodes = {}
|
local nodes = {}
|
||||||
local count = 0
|
local count = 0
|
||||||
local max_radius = radius * (radius + 1)
|
|
||||||
for x = -radius, radius do
|
for x = -radius, radius do
|
||||||
for y = 0, radius do
|
for y = 0, radius do
|
||||||
for z = -radius, radius do
|
for z = -radius, radius do
|
||||||
if x * x + y * y + z * z <= max_radius then
|
if x * x + y * y + z * z <= max_radius then --inside dome
|
||||||
insert(nodes, node)
|
insert(nodes, node_id)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
else
|
else
|
||||||
insert(nodes, ignore)
|
insert(nodes, ignore_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -109,6 +158,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
|
|||||||
other1, other2 = "x", "y"
|
other1, other2 = "x", "y"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--handle negative lengths
|
||||||
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
|
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
|
||||||
if length < 0 then
|
if length < 0 then
|
||||||
length = -length
|
length = -length
|
||||||
@ -151,7 +201,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
|
|||||||
currentpos[other1] = first1
|
currentpos[other1] = first1
|
||||||
place_schematic(currentpos, schematic) --octant 7
|
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
|
--move to next location
|
||||||
delta = delta + (offset1 * 2) + 1
|
delta = delta + (offset1 * 2) + 1
|
||||||
@ -161,6 +211,7 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
|
|||||||
end
|
end
|
||||||
offset1 = offset1 + 1
|
offset1 = offset1 + 1
|
||||||
end
|
end
|
||||||
|
count = count * length --apply the length to the number of nodes
|
||||||
return count
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -175,41 +226,47 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename, env)
|
|||||||
other1, other2 = "x", "y"
|
other1, other2 = "x", "y"
|
||||||
end
|
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
|
--handle negative lengths
|
||||||
if env == nil then env = minetest.env end
|
|
||||||
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
|
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
|
if length < 0 then
|
||||||
length = -length
|
length = -length
|
||||||
step = -1
|
currentpos[axis] = currentpos[axis] - length
|
||||||
end
|
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
|
for i = 1, length do
|
||||||
|
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 offset1, offset2 = 0, radius
|
||||||
local delta = -radius
|
local delta = -radius
|
||||||
while offset1 <= offset2 do
|
while offset1 <= offset2 do
|
||||||
--connect each pair of octants
|
--connect each pair of octants taking advantage of symmetry along two axes
|
||||||
currentpos[other1] = pos[other1] - offset1
|
currentpos[other1] = pos[other1] - offset1
|
||||||
local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
|
local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
|
||||||
for i = 0, offset1 * 2 do
|
for i = 0, offset1 * 2 do
|
||||||
currentpos[other2] = second1
|
currentpos[other2] = second1
|
||||||
env:add_node(currentpos, node) --octant 1 to 4
|
place_schematic(currentpos, schematic) --octant 1 to 4
|
||||||
currentpos[other2] = second2
|
currentpos[other2] = second2
|
||||||
env:add_node(currentpos, node) --octant 5 to 8
|
place_schematic(currentpos, schematic) --octant 5 to 8
|
||||||
currentpos[other1] = currentpos[other1] + 1
|
currentpos[other1] = currentpos[other1] + 1
|
||||||
end
|
end
|
||||||
currentpos[other1] = pos[other1] - offset2
|
currentpos[other1] = pos[other1] - offset2
|
||||||
local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
|
local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
|
||||||
for i = 0, offset2 * 2 do
|
for i = 0, offset2 * 2 do
|
||||||
currentpos[other2] = second1
|
currentpos[other2] = second1
|
||||||
env:add_node(currentpos, node) --octant 2 to 3
|
place_schematic(currentpos, schematic) --octant 2 to 3
|
||||||
currentpos[other2] = second2
|
currentpos[other2] = second2
|
||||||
env:add_node(currentpos, node) --octant 6 to 7
|
place_schematic(currentpos, schematic) --octant 6 to 7
|
||||||
currentpos[other1] = currentpos[other1] + 1
|
currentpos[other1] = currentpos[other1] + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken
|
count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken since node positions may coincide
|
||||||
|
|
||||||
--move to next location
|
--move to next location
|
||||||
delta = delta + (offset1 * 2) + 1
|
delta = delta + (offset1 * 2) + 1
|
||||||
@ -219,8 +276,7 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename, env)
|
|||||||
delta = delta - (offset2 * 2)
|
delta = delta - (offset2 * 2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
currentpos[axis] = currentpos[axis] + step
|
count = count * length --apply the length to the number of nodes
|
||||||
end
|
|
||||||
return count
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
worldedit = worldedit or {}
|
worldedit = worldedit or {}
|
||||||
|
local minetest = minetest --local copy of global
|
||||||
|
|
||||||
worldedit.queue = {}
|
worldedit.queue = {}
|
||||||
worldedit.lower = 1
|
worldedit.lower = 1
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
worldedit = worldedit or {}
|
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
|
--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)
|
||||||
@ -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 pos = {x=pos1.x, y=0, z=0}
|
||||||
local count = 0
|
local count = 0
|
||||||
local result = {}
|
local result = {}
|
||||||
local env = minetest.env
|
local get_node, get_meta = minetest.get_node, minetest.get_meta
|
||||||
while pos.x <= pos2.x do
|
while pos.x <= pos2.x do
|
||||||
pos.y = pos1.y
|
pos.y = pos1.y
|
||||||
while pos.y <= pos2.y do
|
while pos.y <= pos2.y do
|
||||||
pos.z = pos1.z
|
pos.z = pos1.z
|
||||||
while pos.z <= pos2.z do
|
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
|
if node.name ~= "air" and node.name ~= "ignore" then
|
||||||
count = count + 1
|
count = count + 1
|
||||||
local meta = env:get_meta(pos):to_table()
|
local meta = get_meta(pos):to_table()
|
||||||
|
|
||||||
--convert metadata itemstacks to itemstrings
|
--convert metadata itemstacks to itemstrings
|
||||||
for name, inventory in pairs(meta.inventory) do
|
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
|
--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)
|
--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 originx, originy, originz = originpos.x, originpos.y, originpos.z
|
||||||
local count = 0
|
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)
|
local version = worldedit.valueversion(value)
|
||||||
if version == 1 or version == 2 then --original flat table format
|
if version == 1 or version == 2 then --original flat table format
|
||||||
--obtain the node table
|
--obtain the node table
|
||||||
@ -190,27 +191,23 @@ worldedit.deserialize = function(originpos, value, env)
|
|||||||
local entry = nodes[index]
|
local entry = nodes[index]
|
||||||
local pos = entry[1]
|
local pos = entry[1]
|
||||||
pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z
|
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
|
end
|
||||||
else --previous meta flat table format
|
else --previous meta flat table format
|
||||||
for index = 1, #nodes do
|
for index = 1, #nodes do
|
||||||
local entry = nodes[index]
|
local entry = nodes[index]
|
||||||
entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z
|
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
|
||||||
env:get_meta(entry):from_table(entry.meta)
|
get_meta(entry):from_table(entry.meta)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif version == 3 then --previous list format
|
elseif version == 3 then --previous list format
|
||||||
local pos = {x=0, y=0, z=0}
|
local pos = {x=0, y=0, z=0}
|
||||||
local node = {name="", param1=0, param2=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
|
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.x, pos.y, pos.z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)
|
||||||
pos.y = originy + tonumber(y)
|
node.name, node.param1, node.param2 = name, param1, param2
|
||||||
pos.z = originz + tonumber(z)
|
add_node(pos, node)
|
||||||
node.name = name
|
|
||||||
node.param1 = param1
|
|
||||||
node.param2 = param2
|
|
||||||
env:add_node(pos, node)
|
|
||||||
count = count + 1
|
count = count + 1
|
||||||
end
|
end
|
||||||
elseif version == 4 then --current nested table format
|
elseif version == 4 then --current nested table format
|
||||||
@ -237,13 +234,13 @@ worldedit.deserialize = function(originpos, value, env)
|
|||||||
for index = 1, count do
|
for index = 1, count do
|
||||||
local entry = nodes[index]
|
local entry = nodes[index]
|
||||||
entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z
|
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
|
end
|
||||||
|
|
||||||
--load the metadata
|
--load the metadata
|
||||||
for index = 1, count do
|
for index = 1, count do
|
||||||
local entry = nodes[index]
|
local entry = nodes[index]
|
||||||
env:get_meta(entry):from_table(entry.meta)
|
get_meta(entry):from_table(entry.meta)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return count
|
return count
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
worldedit = worldedit or {}
|
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
|
--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)
|
||||||
@ -76,7 +77,7 @@ worldedit.suppress = function(pos1, pos2, nodename)
|
|||||||
end
|
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
|
--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 pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
||||||
local pos = {x=pos1.x, y=0, z=0}
|
local pos = {x=pos1.x, y=0, z=0}
|
||||||
local placeholder = {name="worldedit:placeholder", param1=0, param2=0}
|
local placeholder = {name="worldedit:placeholder", param1=0, param2=0}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user