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
2013-06-23 02:59:23 +02:00
--wip: remove env parameter where no longer needed in chat commands module
2013-07-21 22:54:25 +02:00
--wip: fix the queue
2013-06-23 02:59:23 +02:00
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
--sets a region defined by positions `pos1` and `pos2` to `nodename`, returning the number of nodes filled
2013-06-23 02:59:23 +02:00
worldedit.set = function ( pos1 , pos2 , nodename )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-07-12 20:22:30 +02:00
--set up voxel manipulator
local manip = minetest.get_voxel_manip ( )
2013-07-21 22:54:25 +02:00
local emerged_pos1 , emerged_pos2 = manip : read_from_map ( pos1 , pos2 )
local area = VoxelArea : new ( { MinEdge = emerged_pos1 , MaxEdge = emerged_pos2 } )
2013-06-23 02:59:23 +02:00
2013-07-21 22:54:25 +02:00
--fill emerged area with ignore
2013-07-12 20:22:30 +02:00
local nodes = { }
2013-07-21 22:54:25 +02:00
local ignore = minetest.get_content_id ( " ignore " )
for i = 1 , worldedit.volume ( emerged_pos1 , emerged_pos2 ) do
nodes [ i ] = ignore
end
--fill selected area with node
2013-07-12 20:22:30 +02:00
local node_id = minetest.get_content_id ( nodename )
2013-07-21 22:54:25 +02:00
for i in area : iterp ( pos1 , pos2 ) do
2013-07-12 20:22:30 +02:00
nodes [ i ] = node_id
2012-10-14 03:45:50 +02:00
end
2013-06-23 02:59:23 +02:00
2013-07-12 20:22:30 +02:00
--update map nodes
manip : set_data ( nodes )
manip : write_to_map ( )
manip : update_map ( )
2012-10-14 03:45:50 +02:00
return worldedit.volume ( pos1 , pos2 )
end
--replaces all instances of `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced
2013-06-23 18:46:59 +02:00
worldedit.replace = function ( pos1 , pos2 , searchnode , replacenode )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-07-21 22:54:25 +02:00
--set up voxel manipulator
local manip = minetest.get_voxel_manip ( )
local emerged_pos1 , emerged_pos2 = manip : read_from_map ( pos1 , pos2 )
local area = VoxelArea : new ( { MinEdge = emerged_pos1 , MaxEdge = emerged_pos2 } )
local nodes = manip : get_data ( )
local searchnode_id = minetest.get_content_id ( searchnode )
local replacenode_id = minetest.get_content_id ( replacenode )
local count = 0
for i in area : iterp ( pos1 , pos2 ) do --replace searchnode with replacenode
if nodes [ i ] == searchnode_id then
nodes [ i ] = replacenode_id
count = count + 1
end
2012-10-14 03:45:50 +02:00
end
2013-07-21 22:54:25 +02:00
--update map nodes
manip : set_data ( nodes )
manip : write_to_map ( )
manip : update_map ( )
return count
2012-10-14 03:45:50 +02:00
end
2013-01-13 00:29:57 +01:00
--replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced
2013-07-21 22:54:25 +02:00
worldedit.replaceinverse = function ( pos1 , pos2 , searchnode , replacenode )
2013-01-13 00:29:57 +01:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-07-21 22:54:25 +02:00
--set up voxel manipulator
local manip = minetest.get_voxel_manip ( )
local emerged_pos1 , emerged_pos2 = manip : read_from_map ( pos1 , pos2 )
local area = VoxelArea : new ( { MinEdge = emerged_pos1 , MaxEdge = emerged_pos2 } )
local nodes = manip : get_data ( )
local searchnode_id = minetest.get_content_id ( searchnode )
local replacenode_id = minetest.get_content_id ( replacenode )
2013-01-13 00:29:57 +01:00
local count = 0
2013-07-21 22:54:25 +02:00
for i in area : iterp ( pos1 , pos2 ) do --replace anything that is not searchnode with replacenode
if nodes [ i ] ~= searchnode_id then
nodes [ i ] = replacenode_id
count = count + 1
2013-01-13 00:29:57 +01:00
end
end
2013-07-21 22:54:25 +02:00
--update map nodes
manip : set_data ( nodes )
manip : write_to_map ( )
manip : update_map ( )
2013-01-13 00:29:57 +01:00
return count
end
2012-10-14 03:45:50 +02:00
--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
2013-04-28 19:08:09 +02:00
worldedit.copy = function ( pos1 , pos2 , axis , amount , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-04-28 19:08:09 +02:00
if env == nil then env = minetest.env end
2012-10-14 03:45:50 +02:00
2013-07-21 22:54:25 +02:00
--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
2012-10-14 03:45:50 +02:00
if amount < 0 then
local pos = { x = pos1.x , y = 0 , z = 0 }
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 ) --obtain current node
local meta = env : get_meta ( pos ) : to_table ( ) --get meta of current node
local value = pos [ axis ] --store current position
pos [ axis ] = value + amount --move along axis
env : add_node ( pos , node ) --copy node to new position
env : get_meta ( pos ) : from_table ( meta ) --set metadata of new node
pos [ axis ] = value --restore old position
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
else
local pos = { x = pos2.x , y = 0 , z = 0 }
while pos.x >= pos1.x do
pos.y = pos2.y
while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
local node = minetest.env : get_node ( pos ) --obtain current node
local meta = env : get_meta ( pos ) : to_table ( ) --get meta of current node
local value = pos [ axis ] --store current position
pos [ axis ] = value + amount --move along axis
minetest.env : add_node ( pos , node ) --copy node to new position
env : get_meta ( pos ) : from_table ( meta ) --set metadata of new node
pos [ axis ] = value --restore old position
pos.z = pos.z - 1
end
pos.y = pos.y - 1
end
pos.x = pos.x - 1
end
end
return worldedit.volume ( pos1 , pos2 )
end
--moves 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 moved
2013-04-28 19:08:09 +02:00
worldedit.move = function ( pos1 , pos2 , axis , amount , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-04-28 19:08:09 +02:00
if env == nil then env = minetest.env end
2012-10-14 03:45:50 +02:00
2013-07-21 22:54:25 +02:00
--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
2012-10-14 03:45:50 +02:00
if amount < 0 then
local pos = { x = pos1.x , y = 0 , z = 0 }
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 ) --obtain current node
local meta = env : get_meta ( pos ) : to_table ( ) --get metadata of current node
env : remove_node ( pos )
local value = pos [ axis ] --store current position
pos [ axis ] = value + amount --move along axis
env : add_node ( pos , node ) --move node to new position
env : get_meta ( pos ) : from_table ( meta ) --set metadata of new node
pos [ axis ] = value --restore old position
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
else
local pos = { x = pos2.x , y = 0 , z = 0 }
while pos.x >= pos1.x do
pos.y = pos2.y
while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
local node = env : get_node ( pos ) --obtain current node
local meta = env : get_meta ( pos ) : to_table ( ) --get metadata of current node
env : remove_node ( pos )
local value = pos [ axis ] --store current position
pos [ axis ] = value + amount --move along axis
env : add_node ( pos , node ) --move node to new position
env : get_meta ( pos ) : from_table ( meta ) --set metadata of new node
pos [ axis ] = value --restore old position
pos.z = pos.z - 1
end
pos.y = pos.y - 1
end
pos.x = pos.x - 1
end
end
return worldedit.volume ( pos1 , pos2 )
end
--duplicates the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") `count` times, returning the number of nodes stacked
2013-04-28 19:08:09 +02:00
worldedit.stack = function ( pos1 , pos2 , axis , count , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
local length = pos2 [ axis ] - pos1 [ axis ] + 1
if count < 0 then
count = - count
length = - length
end
local amount = 0
local copy = worldedit.copy
for i = 1 , count do
amount = amount + length
2013-04-28 19:08:09 +02:00
copy ( pos1 , pos2 , axis , amount , env )
2012-10-14 03:45:50 +02:00
end
2013-07-21 22:54:25 +02:00
return worldedit.volume ( pos1 , pos2 ) * count
2012-10-14 03:45:50 +02:00
end
2013-06-18 21:05:49 +02:00
--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
2013-06-23 18:46:59 +02:00
worldedit.scale = function ( pos1 , pos2 , factor )
2013-06-18 21:05:49 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-06-23 02:59:23 +02:00
--prepare schematic of large node
2013-06-23 18:46:59 +02:00
local get_node , get_meta , place_schematic = minetest.get_node , minetest.get_meta , minetest.place_schematic
local placeholder_node = { name = " " , param1 = 0 , param2 = 0 }
2013-06-23 02:59:23 +02:00
local nodes = { }
2013-07-21 22:54:25 +02:00
for i = 1 , factor ^ 3 do
2013-06-23 02:59:23 +02:00
nodes [ i ] = placeholder_node
end
2013-07-21 22:54:25 +02:00
local schematic = { size = { x = factor , y = factor , z = factor } , data = nodes }
local size = factor - 1
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
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 }
local emerged_pos1 , emerged_pos2 = manip : read_from_map ( pos1 , new_pos2 )
2013-06-23 02:59:23 +02:00
2013-06-18 21:05:49 +02:00
local pos = { x = pos2.x , y = 0 , z = 0 }
local bigpos = { x = 0 , y = 0 , z = 0 }
while pos.x >= pos1.x do
pos.y = pos2.y
while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
2013-06-23 18:46:59 +02:00
local node = get_node ( pos ) --obtain current node
local meta = get_meta ( pos ) : to_table ( ) --get meta of current node
2013-06-23 02:59:23 +02:00
2013-06-18 21:05:49 +02:00
local value = pos [ axis ] --store current position
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
2013-06-23 02:59:23 +02:00
--create large node
2013-07-21 22:54:25 +02:00
placeholder_node.name = node.name
placeholder_node.param1 , placeholder_node.param2 = node.param1 , node.param2
2013-06-23 02:59:23 +02:00
bigpos.x , bigpos.y , bigpos.z = posx , posy , posz
place_schematic ( bigpos , schematic )
2013-07-21 22:54:25 +02:00
--fill in large node meta
if next ( meta.fields ) ~= nil and next ( meta.inventory ) ~= nil then --node has meta fields
for x = 0 , size do
for y = 0 , size do
for z = 0 , size do
bigpos.x , bigpos.y , bigpos.z = posx + x , posy + y , posz + z
get_meta ( bigpos ) : from_table ( meta ) --set metadata of new node
end
2013-06-18 21:05:49 +02:00
end
end
end
pos.z = pos.z - 1
end
pos.y = pos.y - 1
end
pos.x = pos.x - 1
end
2013-07-21 22:54:25 +02:00
return worldedit.volume ( pos1 , pos2 ) * ( factor ^ 3 ) , pos1 , new_pos2
2013-06-18 21:05:49 +02:00
end
--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
2013-04-28 19:08:09 +02:00
worldedit.transpose = function ( pos1 , pos2 , axis1 , axis2 , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2012-12-12 23:17:56 +01:00
local compare
local extent1 , extent2 = pos2 [ axis1 ] - pos1 [ axis1 ] , pos2 [ axis2 ] - pos1 [ axis2 ]
if extent1 > extent2 then
compare = function ( extent1 , extent2 )
return extent1 > extent2
end
else
compare = function ( extent1 , extent2 )
return extent1 < extent2
end
end
--calculate the new position 2 after transposition
2013-07-21 22:54:25 +02:00
local new_pos2 = { x = pos2.x , y = pos2.y , z = pos2.z }
new_pos2 [ axis1 ] = pos1 [ axis1 ] + extent2
new_pos2 [ axis2 ] = pos1 [ axis2 ] + extent1
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
local upperbound = { x = pos2.x , y = pos2.y , z = pos2.z }
if upperbound [ axis1 ] < new_pos2 [ axis1 ] then upperbound [ axis1 ] = new_pos2 [ axis1 ] end
if upperbound [ axis2 ] < new_pos2 [ axis2 ] then upperbound [ axis2 ] = new_pos2 [ axis2 ] end
manip : read_from_map ( pos1 , upperbound )
2012-12-12 23:17:56 +01:00
2012-10-14 03:45:50 +02:00
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
local extent1 , extent2 = pos [ axis1 ] - pos1 [ axis1 ] , pos [ axis2 ] - pos1 [ axis2 ]
2012-12-12 23:17:56 +01:00
if compare ( extent1 , extent2 ) then --transpose only if below the diagonal
2013-06-23 18:46:59 +02:00
local node1 = get_node ( pos )
local meta1 = get_meta ( pos ) : to_table ( )
2012-12-12 23:17:56 +01:00
local value1 , value2 = pos [ axis1 ] , pos [ axis2 ] --save position values
pos [ axis1 ] , pos [ axis2 ] = pos1 [ axis1 ] + extent2 , pos1 [ axis2 ] + extent1 --swap axis extents
2013-06-23 18:46:59 +02:00
local node2 = get_node ( pos )
local meta2 = get_meta ( pos ) : to_table ( )
add_node ( pos , node1 )
get_meta ( pos ) : from_table ( meta1 )
2012-12-12 23:17:56 +01:00
pos [ axis1 ] , pos [ axis2 ] = value1 , value2 --restore position values
2013-06-23 18:46:59 +02:00
add_node ( pos , node2 )
get_meta ( pos ) : from_table ( meta2 )
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
2013-07-21 22:54:25 +02:00
return worldedit.volume ( pos1 , pos2 ) , pos1 , new_pos2
2012-10-14 03:45:50 +02:00
end
--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
2013-04-28 19:08:09 +02:00
worldedit.flip = function ( pos1 , pos2 , axis , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( 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 )
2013-06-23 02:59:23 +02:00
--wip: flip the region slice by slice along the flip axis using schematic method
2012-10-14 03:45:50 +02:00
local pos = { x = pos1.x , y = 0 , z = 0 }
local start = pos1 [ axis ] + pos2 [ axis ]
pos2 [ axis ] = pos1 [ axis ] + math.floor ( ( pos2 [ axis ] - pos1 [ axis ] ) / 2 )
2013-07-21 22:54:25 +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-07-21 22:54:25 +02:00
local node1 = get_node ( pos )
local meta1 = get_meta ( pos ) : to_table ( )
2012-10-14 03:45:50 +02:00
local value = pos [ axis ]
pos [ axis ] = start - value
2013-07-21 22:54:25 +02:00
local node2 = get_node ( pos )
local meta2 = get_meta ( pos ) : to_table ( )
add_node ( pos , node1 )
get_meta ( pos ) : from_table ( meta1 )
2012-10-14 03:45:50 +02:00
pos [ axis ] = value
2013-07-21 22:54:25 +02:00
add_node ( pos , node2 )
get_meta ( pos ) : from_table ( meta2 )
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
--rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around axis `axis` (90 degree increment), returning the number of nodes rotated
2013-04-28 19:08:09 +02:00
worldedit.rotate = function ( pos1 , pos2 , axis , angle , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
local axis1 , axis2
if axis == " x " then
axis1 , axis2 = " z " , " y "
elseif axis == " y " then
axis1 , axis2 = " x " , " z "
else --axis == "z"
axis1 , axis2 = " y " , " x "
end
angle = angle % 360
2012-12-12 23:17:56 +01:00
local count
2012-10-14 03:45:50 +02:00
if angle == 90 then
2013-04-28 19:08:09 +02:00
worldedit.flip ( pos1 , pos2 , axis1 , env )
count , pos1 , pos2 = worldedit.transpose ( pos1 , pos2 , axis1 , axis2 , env )
2012-10-14 03:45:50 +02:00
elseif angle == 180 then
2013-04-28 19:08:09 +02:00
worldedit.flip ( pos1 , pos2 , axis1 , env )
count = worldedit.flip ( pos1 , pos2 , axis2 , env )
2012-10-14 03:45:50 +02:00
elseif angle == 270 then
2013-04-28 19:08:09 +02:00
worldedit.flip ( pos1 , pos2 , axis2 , env )
count , pos1 , pos2 = worldedit.transpose ( pos1 , pos2 , axis1 , axis2 , env )
2012-10-14 03:45:50 +02:00
end
2012-12-12 23:17:56 +01:00
return count , pos1 , pos2
2012-10-14 03:45:50 +02:00
end
2013-01-12 22:46:40 +01:00
--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
2013-07-21 22:54:25 +02:00
worldedit.orient = function ( pos1 , pos2 , angle , env ) --wip: support 6D facedir rotation along arbitrary axis
2013-01-12 22:46:40 +01:00
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
2013-06-23 18:46:59 +02:00
local registered_nodes = minetest.registered_nodes
2013-01-12 22:46:40 +01:00
local wallmounted = {
[ 90 ] = { [ 0 ] = 0 , [ 1 ] = 1 , [ 2 ] = 5 , [ 3 ] = 4 , [ 4 ] = 2 , [ 5 ] = 3 } ,
[ 180 ] = { [ 0 ] = 0 , [ 1 ] = 1 , [ 2 ] = 3 , [ 3 ] = 2 , [ 4 ] = 5 , [ 5 ] = 4 } ,
[ 270 ] = { [ 0 ] = 0 , [ 1 ] = 1 , [ 2 ] = 4 , [ 3 ] = 5 , [ 4 ] = 3 , [ 5 ] = 2 }
}
local facedir = {
[ 90 ] = { [ 0 ] = 1 , [ 1 ] = 2 , [ 2 ] = 3 , [ 3 ] = 0 } ,
[ 180 ] = { [ 0 ] = 2 , [ 1 ] = 3 , [ 2 ] = 0 , [ 3 ] = 1 } ,
[ 270 ] = { [ 0 ] = 3 , [ 1 ] = 0 , [ 2 ] = 1 , [ 3 ] = 2 }
}
angle = angle % 360
if angle == 0 then
return 0
end
local wallmounted_substitution = wallmounted [ angle ]
local facedir_substitution = facedir [ angle ]
2013-07-21 22:54:25 +02:00
--make area stay loaded
local manip = minetest.get_voxel_manip ( )
manip : read_from_map ( pos1 , pos2 )
2013-01-12 22:46:40 +01:00
local count = 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
2013-01-12 22:46:40 +01:00
local pos = { x = pos1.x , y = 0 , z = 0 }
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 )
local def = registered_nodes [ node.name ]
2013-01-12 22:46:40 +01:00
if def then
if def.paramtype2 == " wallmounted " then
node.param2 = wallmounted_substitution [ node.param2 ]
2013-06-23 18:46:59 +02:00
local meta = get_meta ( pos ) : to_table ( )
add_node ( pos , node )
get_meta ( pos ) : from_table ( meta )
2013-01-12 22:46:40 +01:00
count = count + 1
elseif def.paramtype2 == " facedir " then
node.param2 = facedir_substitution [ node.param2 ]
2013-06-23 18:46:59 +02:00
local meta = get_meta ( pos ) : to_table ( )
add_node ( pos , node )
get_meta ( pos ) : from_table ( meta )
2013-01-12 22:46:40 +01:00
count = count + 1
end
end
pos.z = pos.z + 1
end
pos.y = pos.y + 1
end
pos.x = pos.x + 1
end
return count
end
2013-01-12 22:02:23 +01:00
--fixes the lighting in a region defined by positions `pos1` and `pos2`, returning the number of nodes updated
2013-04-28 19:08:09 +02:00
worldedit.fixlight = function ( pos1 , pos2 , env )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.sort_pos ( 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 )
2013-06-23 18:46:59 +02:00
local nodes = minetest.find_nodes_in_area ( pos1 , pos2 , " air " )
local dig_node = minetest.dig_node
for _ , pos in ipairs ( nodes ) do
dig_node ( pos )
2012-10-14 03:45:50 +02:00
end
2013-06-23 18:46:59 +02:00
return # nodes
2013-01-13 00:29:57 +01:00
end
2013-07-29 18:43:24 +02:00
--clears all objects in a region defined by the positions `pos1` and `pos2`, returning the number of objects cleared
worldedit.clearobjects = function ( pos1 , pos2 )
local pos1 , pos2 = worldedit.sort_pos ( pos1 , pos2 )
--set up voxel manipulator
local manip = minetest.get_voxel_manip ( )
manip : read_from_map ( pos1 , pos2 )
local pos1x , pos1y , pos1z = pos1.x , pos1.y , pos1.z
local pos2x , pos2y , pos2z = pos2.x , pos2.y , pos2.z
local center = { x = ( pos1x + pos2x + 1 ) / 2 , y = ( pos1y + pos2y + 1 ) / 2 , z = ( pos1z + pos2z + 1 ) / 2 }
local radius = ( ( center.x - pos1x + 0.5 ) + ( center.y - pos1y + 0.5 ) + ( center.z - pos1z + 0.5 ) ) ^ 0.5
local count = 0
for _ , obj in pairs ( minetest.get_objects_inside_radius ( center , radius ) ) do
local pos = obj : getpos ( )
if pos.x >= pos1x and pos.x <= pos2x
and pos.y >= pos1y and pos.y <= pos2y
and pos.z >= pos1z and pos.z <= pos2z then
obj : remove ( )
count = count + 1
end
end
return count
end