2014-10-30 03:47:08 +01:00
|
|
|
--- Generic node manipulations.
|
|
|
|
-- @module worldedit.manipulations
|
2012-10-14 03:45:50 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local mh = worldedit.manip_helpers
|
2012-10-14 03:45:50 +02:00
|
|
|
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Sets a region to `node_names`.
|
|
|
|
-- @param pos1
|
|
|
|
-- @param pos2
|
|
|
|
-- @param node_names Node name or list of node names.
|
|
|
|
-- @return The number of nodes set.
|
|
|
|
function worldedit.set(pos1, pos2, node_names)
|
|
|
|
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
2013-06-23 02:59:23 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local manip, area = mh.init(pos1, pos2)
|
|
|
|
local data = mh.get_empty_data(area)
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
if type(node_names) == "string" then -- Only one type of node
|
|
|
|
local id = minetest.get_content_id(node_names)
|
|
|
|
-- Fill area with node
|
|
|
|
for i in area:iterp(pos1, pos2) do
|
|
|
|
data[i] = id
|
|
|
|
end
|
|
|
|
else -- Several types of nodes specified
|
|
|
|
local node_ids = {}
|
|
|
|
for i, v in ipairs(node_names) do
|
|
|
|
node_ids[i] = minetest.get_content_id(v)
|
|
|
|
end
|
|
|
|
-- Fill area randomly with nodes
|
2014-07-07 01:42:02 +02:00
|
|
|
local id_count, rand = #node_ids, math.random
|
2014-10-30 03:47:08 +01:00
|
|
|
for i in area:iterp(pos1, pos2) do
|
|
|
|
data[i] = node_ids[rand(id_count)]
|
2013-07-21 22:54:25 +02:00
|
|
|
end
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
mh.finish(manip, data)
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
return worldedit.volume(pos1, pos2)
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
|
2017-09-05 14:40:46 +02:00
|
|
|
--- Sets param2 of a region.
|
|
|
|
-- @param pos1
|
|
|
|
-- @param pos2
|
|
|
|
-- @param param2 Value of param2 to set
|
|
|
|
-- @return The number of nodes set.
|
|
|
|
function worldedit.set_param2(pos1, pos2, param2)
|
|
|
|
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
|
|
|
local manip, area = mh.init(pos1, pos2)
|
|
|
|
local param2_data = manip:get_param2_data()
|
|
|
|
|
|
|
|
-- Set param2 for every node
|
|
|
|
for i in area:iterp(pos1, pos2) do
|
|
|
|
param2_data[i] = param2
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Update map
|
|
|
|
manip:set_param2_data(param2_data)
|
|
|
|
manip:write_to_map()
|
|
|
|
manip:update_map()
|
|
|
|
|
|
|
|
return worldedit.volume(pos1, pos2)
|
|
|
|
end
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Replaces all instances of `search_node` with `replace_node` in a region.
|
|
|
|
-- When `inverse` is `true`, replaces all instances that are NOT `search_node`.
|
|
|
|
-- @return The number of nodes replaced.
|
|
|
|
function worldedit.replace(pos1, pos2, search_node, replace_node, inverse)
|
2013-08-01 04:15:08 +02:00
|
|
|
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local manip, area = mh.init(pos1, pos2)
|
|
|
|
local data = manip:get_data()
|
2013-08-01 04:15:08 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local search_id = minetest.get_content_id(search_node)
|
|
|
|
local replace_id = minetest.get_content_id(replace_node)
|
2013-08-01 04:15:08 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
local count = 0
|
2013-08-01 04:15:08 +02:00
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
if not inverse then
|
|
|
|
for i in area:iterp(pos1, pos2) do
|
|
|
|
if data[i] == search_id then
|
|
|
|
data[i] = replace_id
|
|
|
|
count = count + 1
|
2013-08-01 04:15:08 +02:00
|
|
|
end
|
|
|
|
end
|
2014-07-07 01:42:02 +02:00
|
|
|
else
|
2014-10-30 03:47:08 +01:00
|
|
|
for i in area:iterp(pos1, pos2) do
|
|
|
|
if data[i] ~= search_id then
|
|
|
|
data[i] = replace_id
|
|
|
|
count = count + 1
|
2014-07-07 01:42:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-30 03:47:08 +01:00
|
|
|
|
|
|
|
mh.finish(manip, data)
|
|
|
|
|
|
|
|
return count
|
2014-07-07 01:42:02 +02:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
|
|
|
|
--- Copies a region along `axis` by `amount` nodes.
|
|
|
|
-- @param pos1
|
|
|
|
-- @param pos2
|
|
|
|
-- @param axis Axis ("x", "y", or "z")
|
|
|
|
-- @param amount
|
|
|
|
-- @return The number of nodes copied.
|
|
|
|
function worldedit.copy(pos1, pos2, axis, amount)
|
2012-10-14 03:45:50 +02:00
|
|
|
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
2019-09-17 01:20:10 +02:00
|
|
|
-- Decide if we need to copy stuff backwards (only applies to metadata)
|
|
|
|
local backwards = amount > 0 and amount < (pos2[axis] - pos1[axis] + 1)
|
2019-09-08 20:12:38 +02:00
|
|
|
|
2023-06-09 14:28:29 +02:00
|
|
|
local off = vector.new()
|
2019-09-08 20:12:38 +02:00
|
|
|
off[axis] = amount
|
2019-09-17 01:20:10 +02:00
|
|
|
return worldedit.copy2(pos1, pos2, off, backwards)
|
2016-01-05 13:57:48 +01:00
|
|
|
end
|
2014-10-30 03:47:08 +01:00
|
|
|
|
2019-09-08 20:12:38 +02:00
|
|
|
--- Copies a region by offset vector `off`.
|
|
|
|
-- @param pos1
|
|
|
|
-- @param pos2
|
|
|
|
-- @param off
|
2019-09-17 01:20:10 +02:00
|
|
|
-- @param meta_backwards (not officially part of API)
|
2019-09-08 20:12:38 +02:00
|
|
|
-- @return The number of nodes copied.
|
2019-09-17 01:20:10 +02:00
|
|
|
function worldedit.copy2(pos1, pos2, off, meta_backwards)
|
2019-09-08 20:12:38 +02:00
|
|
|
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
|
|
|
local src_manip, src_area = mh.init(pos1, pos2)
|
2023-06-09 14:28:29 +02:00
|
|
|
local src_stride = vector.new(1, src_area.ystride, src_area.zstride)
|
2019-09-08 20:12:38 +02:00
|
|
|
local src_offset = vector.subtract(pos1, src_area.MinEdge)
|
|
|
|
|
|
|
|
local dpos1 = vector.add(pos1, off)
|
|
|
|
local dpos2 = vector.add(pos2, off)
|
|
|
|
local dim = vector.add(vector.subtract(pos2, pos1), 1)
|
|
|
|
|
|
|
|
local dst_manip, dst_area = mh.init(dpos1, dpos2)
|
2023-06-09 14:28:29 +02:00
|
|
|
local dst_stride = vector.new(1, dst_area.ystride, dst_area.zstride)
|
2019-09-08 20:12:38 +02:00
|
|
|
local dst_offset = vector.subtract(dpos1, dst_area.MinEdge)
|
|
|
|
|
|
|
|
local function do_copy(src_data, dst_data)
|
|
|
|
for z = 0, dim.z-1 do
|
|
|
|
local src_index_z = (src_offset.z + z) * src_stride.z + 1 -- +1 for 1-based indexing
|
|
|
|
local dst_index_z = (dst_offset.z + z) * dst_stride.z + 1
|
|
|
|
for y = 0, dim.y-1 do
|
|
|
|
local src_index_y = src_index_z + (src_offset.y + y) * src_stride.y
|
|
|
|
local dst_index_y = dst_index_z + (dst_offset.y + y) * dst_stride.y
|
|
|
|
-- Copy entire row at once
|
|
|
|
local src_index_x = src_index_y + src_offset.x
|
|
|
|
local dst_index_x = dst_index_y + dst_offset.x
|
|
|
|
for x = 0, dim.x-1 do
|
|
|
|
dst_data[dst_index_x + x] = src_data[src_index_x + x]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Copy node data
|
|
|
|
local src_data = src_manip:get_data()
|
|
|
|
local dst_data = dst_manip:get_data()
|
|
|
|
do_copy(src_data, dst_data)
|
|
|
|
dst_manip:set_data(dst_data)
|
|
|
|
|
|
|
|
-- Copy param1
|
|
|
|
src_manip:get_light_data(src_data)
|
|
|
|
dst_manip:get_light_data(dst_data)
|
|
|
|
do_copy(src_data, dst_data)
|
|
|
|
dst_manip:set_light_data(dst_data)
|
|
|
|
|
|
|
|
-- Copy param2
|
|
|
|
src_manip:get_param2_data(src_data)
|
|
|
|
dst_manip:get_param2_data(dst_data)
|
|
|
|
do_copy(src_data, dst_data)
|
|
|
|
dst_manip:set_param2_data(dst_data)
|
|
|
|
|
|
|
|
mh.finish(dst_manip)
|
|
|
|
|
|
|
|
-- Copy metadata
|
|
|
|
local get_meta = minetest.get_meta
|
2019-09-17 01:20:10 +02:00
|
|
|
if meta_backwards then
|
|
|
|
for z = dim.z-1, 0, -1 do
|
|
|
|
for y = dim.y-1, 0, -1 do
|
|
|
|
for x = dim.x-1, 0, -1 do
|
2023-06-09 14:28:29 +02:00
|
|
|
local pos = vector.new(pos1.x+x, pos1.y+y, pos1.z+z)
|
2019-09-17 01:20:10 +02:00
|
|
|
local meta = get_meta(pos):to_table()
|
|
|
|
pos = vector.add(pos, off)
|
|
|
|
get_meta(pos):from_table(meta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2019-09-08 20:12:38 +02:00
|
|
|
for z = 0, dim.z-1 do
|
|
|
|
for y = 0, dim.y-1 do
|
|
|
|
for x = 0, dim.x-1 do
|
2023-06-09 14:28:29 +02:00
|
|
|
local pos = vector.new(pos1.x+x, pos1.y+y, pos1.z+z)
|
2019-09-08 20:12:38 +02:00
|
|
|
local meta = get_meta(pos):to_table()
|
|
|
|
pos = vector.add(pos, off)
|
|
|
|
get_meta(pos):from_table(meta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-17 01:20:10 +02:00
|
|
|
end
|
2019-09-08 20:12:38 +02:00
|
|
|
|
|
|
|
return worldedit.volume(pos1, pos2)
|
|
|
|
end
|
|
|
|
|
2019-09-17 18:03:19 +02:00
|
|
|
--- Deletes all node metadata in the region
|
|
|
|
-- @param pos1
|
|
|
|
-- @param pos2
|
|
|
|
-- @return The number of nodes that had their meta deleted.
|
|
|
|
function worldedit.delete_meta(pos1, pos2)
|
|
|
|
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
|
|
|
local meta_positions = minetest.find_nodes_with_meta(pos1, pos2)
|
|
|
|
local get_meta = minetest.get_meta
|
|
|
|
for _, pos in ipairs(meta_positions) do
|
|
|
|
get_meta(pos):from_table(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
return #meta_positions
|
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Moves a region along `axis` by `amount` nodes.
|
|
|
|
-- @return The number of nodes moved.
|
|
|
|
function worldedit.move(pos1, pos2, axis, amount)
|
2012-10-14 03:45:50 +02:00
|
|
|
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
2019-09-08 21:05:30 +02:00
|
|
|
local dim = vector.add(vector.subtract(pos2, pos1), 1)
|
2019-09-17 18:37:33 +02:00
|
|
|
local overlap = math.abs(amount) < dim[axis]
|
|
|
|
-- Decide if we need to copy metadata backwards
|
|
|
|
local backwards = overlap and amount > 0
|
|
|
|
|
|
|
|
local function nuke_area(my_off, my_dim)
|
|
|
|
if my_dim.x == 0 or my_dim.y == 0 or my_dim.z == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local my_pos1 = vector.add(pos1, my_off)
|
|
|
|
local my_pos2 = vector.subtract(vector.add(my_pos1, my_dim), 1)
|
|
|
|
worldedit.set(my_pos1, my_pos2, "air")
|
|
|
|
worldedit.delete_meta(my_pos1, my_pos2)
|
2019-09-08 21:05:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Copy stuff to new location
|
2023-06-09 14:28:29 +02:00
|
|
|
local off = vector.new()
|
2019-09-08 21:05:30 +02:00
|
|
|
off[axis] = amount
|
2019-09-17 18:37:33 +02:00
|
|
|
worldedit.copy2(pos1, pos2, off, backwards)
|
2019-09-08 21:05:30 +02:00
|
|
|
-- Nuke old area
|
2019-09-17 18:37:33 +02:00
|
|
|
if not overlap then
|
2023-06-09 14:28:29 +02:00
|
|
|
nuke_area(vector.new(), dim)
|
2012-10-14 03:45:50 +02:00
|
|
|
else
|
2019-09-17 18:37:33 +02:00
|
|
|
-- Source and destination region are overlapping, which means we can't
|
|
|
|
-- blindly delete the [pos1, pos2] area
|
|
|
|
local leftover = vector.new(dim) -- size of the leftover slice
|
|
|
|
leftover[axis] = math.abs(amount)
|
|
|
|
if amount > 0 then
|
2023-06-09 14:28:29 +02:00
|
|
|
nuke_area(vector.new(), leftover)
|
2019-09-17 18:37:33 +02:00
|
|
|
else
|
2023-06-09 14:28:29 +02:00
|
|
|
local top = vector.new() -- offset of the leftover slice from pos1
|
2019-11-13 20:49:25 +01:00
|
|
|
top[axis] = dim[axis] - math.abs(amount)
|
2019-09-17 18:37:33 +02:00
|
|
|
nuke_area(top, leftover)
|
2012-10-14 03:45:50 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-17 18:37:33 +02:00
|
|
|
|
2012-10-14 03:45:50 +02:00
|
|
|
return worldedit.volume(pos1, pos2)
|
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
|
|
|
|
--- Attempts to fix the lighting in a region.
|
|
|
|
-- @return The number of nodes updated.
|
|
|
|
function worldedit.fixlight(pos1, pos2)
|
2012-10-14 03:45:50 +02:00
|
|
|
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2017-01-02 19:03:21 +01:00
|
|
|
local vmanip = minetest.get_voxel_manip(pos1, pos2)
|
|
|
|
vmanip:write_to_map()
|
|
|
|
vmanip:update_map() -- this updates the lighting
|
2013-07-21 22:54:25 +02:00
|
|
|
|
2017-01-02 19:03:21 +01:00
|
|
|
return worldedit.volume(pos1, pos2)
|
2013-01-13 00:29:57 +01:00
|
|
|
end
|
2013-07-29 18:43:24 +02:00
|
|
|
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
--- Clears all objects in a region.
|
|
|
|
-- @return The number of objects cleared.
|
|
|
|
function worldedit.clear_objects(pos1, pos2)
|
|
|
|
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
|
|
|
worldedit.keep_loaded(pos1, pos2)
|
|
|
|
|
2021-04-30 19:33:27 +02:00
|
|
|
local function should_delete(obj)
|
|
|
|
-- Avoid players and WorldEdit entities
|
|
|
|
if obj:is_player() then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local entity = obj:get_luaentity()
|
2024-03-30 00:17:51 +01:00
|
|
|
return not (entity and entity.name:find("^worldedit:"))
|
2021-04-30 19:33:27 +02:00
|
|
|
end
|
|
|
|
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Offset positions to include full nodes (positions are in the center of nodes)
|
2023-06-09 14:28:29 +02:00
|
|
|
pos1 = vector.add(pos1, -0.5)
|
2023-10-08 18:21:15 +02:00
|
|
|
pos2 = vector.add(pos2, 0.5)
|
2013-07-29 18:43:24 +02:00
|
|
|
|
2021-04-30 19:33:27 +02:00
|
|
|
local count = 0
|
|
|
|
if minetest.get_objects_in_area then
|
2023-06-09 14:28:29 +02:00
|
|
|
local objects = minetest.get_objects_in_area(pos1, pos2)
|
2021-04-30 19:33:27 +02:00
|
|
|
|
|
|
|
for _, obj in pairs(objects) do
|
|
|
|
if should_delete(obj) then
|
|
|
|
obj:remove()
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Fallback implementation via get_objects_inside_radius
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Center of region
|
|
|
|
local center = {
|
2023-06-09 14:28:29 +02:00
|
|
|
x = pos1.x + ((pos2.x - pos1.x) / 2),
|
|
|
|
y = pos1.y + ((pos2.y - pos1.y) / 2),
|
|
|
|
z = pos1.z + ((pos2.z - pos1.z) / 2)
|
2014-10-30 03:47:08 +01:00
|
|
|
}
|
|
|
|
-- Bounding sphere radius
|
|
|
|
local radius = math.sqrt(
|
2023-06-09 14:28:29 +02:00
|
|
|
(center.x - pos1.x) ^ 2 +
|
|
|
|
(center.y - pos1.y) ^ 2 +
|
|
|
|
(center.z - pos1.z) ^ 2)
|
2023-10-08 18:21:15 +02:00
|
|
|
local objects = minetest.get_objects_inside_radius(center, radius)
|
|
|
|
for _, obj in pairs(objects) do
|
2021-04-30 19:33:27 +02:00
|
|
|
if should_delete(obj) then
|
2019-06-16 20:30:56 +02:00
|
|
|
local pos = obj:get_pos()
|
2023-06-09 14:28:29 +02:00
|
|
|
if pos.x >= pos1.x and pos.x <= pos2.x and
|
|
|
|
pos.y >= pos1.y and pos.y <= pos2.y and
|
|
|
|
pos.z >= pos1.z and pos.z <= pos2.z then
|
2014-10-30 03:47:08 +01:00
|
|
|
-- Inside region
|
2013-07-31 06:02:37 +02:00
|
|
|
obj:remove()
|
|
|
|
count = count + 1
|
|
|
|
end
|
2013-07-29 18:43:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|