2012-10-14 03:45:50 +02:00
worldedit = worldedit or { }
2013-07-12 20:22:30 +02:00
local minetest = minetest --local copy of global
2012-10-14 03:45:50 +02:00
--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 )
pos1 = { x = pos1.x , y = pos1.y , z = pos1.z }
pos2 = { x = pos2.x , y = pos2.y , z = pos2.z }
if pos1.x > pos2.x then
pos2.x , pos1.x = pos1.x , pos2.x
end
if pos1.y > pos2.y then
pos2.y , pos1.y = pos1.y , pos2.y
end
if pos1.z > pos2.z then
pos2.z , pos1.z = pos1.z , pos2.z
end
return pos1 , pos2
end
--determines the volume of the region defined by positions `pos1` and `pos2`, returning the volume
worldedit.volume = function ( pos1 , pos2 )
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
return ( pos2.x - pos1.x + 1 ) * ( pos2.y - pos1.y + 1 ) * ( pos2.z - pos1.z + 1 )
end
minetest.register_node ( " worldedit:placeholder " , {
drawtype = " airlike " ,
paramtype = " light " ,
sunlight_propagates = true ,
diggable = false ,
groups = { not_in_creative_inventory = 1 } ,
} )
--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
2013-06-23 18:46:59 +02:00
worldedit.hide = function ( pos1 , pos2 )
2013-07-21 22:54:25 +02:00
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
manip : read_from_map ( pos1 , pos2 )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
local pos = { x = pos1.x , y = 0 , z = 0 }
2013-06-23 18:46:59 +02:00
local get_node , get_meta , add_node = minetest.get_node , minetest.get_meta , minetest.add_node
2012-10-14 03:45:50 +02:00
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
2013-06-23 18:46:59 +02:00
local node = get_node ( pos )
2013-07-29 00:15:46 +02:00
if node.name ~= " worldedit:placeholder " then
local data = get_meta ( pos ) : to_table ( ) --obtain metadata of original node
data.fields . worldedit_placeholder = node.name --add the node's name
node.name = " worldedit:placeholder " --set node name
add_node ( pos , node ) --add placeholder node
get_meta ( pos ) : from_table ( data ) --set placeholder metadata to the original node's metadata
end
2012-10-14 03:45:50 +02:00
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
return worldedit.volume ( pos1 , pos2 )
end
--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
2013-06-23 18:46:59 +02:00
worldedit.suppress = function ( pos1 , pos2 , nodename )
2013-07-29 00:15:46 +02:00
--ignore placeholder supression
if nodename == " worldedit:placeholder " then
return 0
end
2013-07-21 22:54:25 +02:00
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
manip : read_from_map ( pos1 , pos2 )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-06-23 05:08:51 +02:00
local nodes = minetest.find_nodes_in_area ( pos1 , pos2 , nodename )
2013-06-23 18:46:59 +02:00
local get_node , get_meta , add_node = minetest.get_node , minetest.get_meta , minetest.add_node
2013-06-23 05:08:51 +02:00
for _ , pos in ipairs ( nodes ) do
2013-06-23 18:46:59 +02:00
local node = get_node ( pos )
local data = get_meta ( pos ) : to_table ( ) --obtain metadata of original node
2013-07-29 00:15:46 +02:00
data.fields . worldedit_placeholder = node.name --add the node's name
node.name = " worldedit:placeholder " --set node name
add_node ( pos , node ) --add placeholder node
get_meta ( pos ) : from_table ( data ) --set placeholder metadata to the original node's metadata
2012-10-14 03:45:50 +02:00
end
2013-06-23 05:08:51 +02:00
return # nodes
2012-10-14 03:45:50 +02:00
end
2012-10-21 23:49:43 +02:00
--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
2013-07-21 22:54:25 +02:00
worldedit.highlight = function ( pos1 , pos2 , nodename ) --wip: speed this up with voxmanip get_data to speed up searching
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
manip : read_from_map ( pos1 , pos2 )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
local pos = { x = pos1.x , y = 0 , z = 0 }
2013-06-23 18:46:59 +02:00
local get_node , get_meta , add_node = minetest.get_node , minetest.get_meta , minetest.add_node
2012-10-14 03:45:50 +02:00
local count = 0
2013-06-23 18:46:59 +02:00
while pos.x <= pos2.x do
2012-10-14 03:45:50 +02:00
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
2013-06-23 18:46:59 +02:00
local node = get_node ( pos )
2012-10-14 03:45:50 +02:00
if node.name == nodename then --node found
count = count + 1
2013-07-29 00:15:46 +02:00
elseif node.name ~= " worldedit:placeholder " then --hide other nodes
2013-06-23 18:46:59 +02:00
local data = get_meta ( pos ) : to_table ( ) --obtain metadata of original node
2013-07-29 00:15:46 +02:00
data.fields . worldedit_placeholder = node.name --add the node's name
node.name = " worldedit:placeholder " --set node name
add_node ( pos , node ) --add placeholder node
get_meta ( pos ) : from_table ( data ) --set placeholder metadata to the original node's metadata
2012-10-14 03:45:50 +02:00
end
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
return count
end
--restores all nodes hidden with WorldEdit functions in a region defined by positions `pos1` and `pos2`, returning the number of nodes restored
2013-06-23 18:46:59 +02:00
worldedit.restore = function ( pos1 , pos2 )
2013-07-21 22:54:25 +02:00
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
manip : read_from_map ( pos1 , pos2 )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-06-23 18:46:59 +02:00
local nodes = minetest.find_nodes_in_area ( pos1 , pos2 , " worldedit:placeholder " )
local get_node , get_meta , add_node = minetest.get_node , minetest.get_meta , minetest.add_node
2013-06-23 05:08:51 +02:00
for _ , pos in ipairs ( nodes ) do
2013-07-29 00:15:46 +02:00
local node = get_node ( pos )
2013-06-23 18:46:59 +02:00
local data = get_meta ( pos ) : to_table ( ) --obtain node metadata
2013-06-23 05:08:51 +02:00
node.name = data.fields . worldedit_placeholder --set node name
data.fields . worldedit_placeholder = nil --delete old nodename
2013-07-29 00:15:46 +02:00
add_node ( pos , node ) --add original node
2013-06-23 18:46:59 +02:00
get_meta ( pos ) : from_table ( data ) --set original node metadata
2012-10-14 03:45:50 +02:00
end
2013-06-23 05:08:51 +02:00
return # nodes
2013-04-28 19:08:09 +02:00
end