summaryrefslogtreecommitdiff
path: root/worldedit/worldedit
diff options
context:
space:
mode:
Diffstat (limited to 'worldedit/worldedit')
-rw-r--r--worldedit/worldedit/code.lua52
-rw-r--r--worldedit/worldedit/common.lua114
-rw-r--r--worldedit/worldedit/compatibility.lua74
-rw-r--r--worldedit/worldedit/init.lua44
-rw-r--r--worldedit/worldedit/manipulations.lua629
-rw-r--r--worldedit/worldedit/primitives.lua273
-rw-r--r--worldedit/worldedit/serialization.lua239
-rw-r--r--worldedit/worldedit/visualization.lua131
8 files changed, 1556 insertions, 0 deletions
diff --git a/worldedit/worldedit/code.lua b/worldedit/worldedit/code.lua
new file mode 100644
index 0000000..a939deb
--- /dev/null
+++ b/worldedit/worldedit/code.lua
@@ -0,0 +1,52 @@
+--- Lua code execution functions.
+-- @module worldedit.code
+
+--- Executes `code` as a Lua chunk in the global namespace.
+-- @return An error message if the code fails, or nil on success.
+function worldedit.lua(code)
+ local func, err = loadstring(code)
+ if not func then -- Syntax error
+ return err
+ end
+ local good, err = pcall(func)
+ if not good then -- Runtime error
+ return err
+ end
+ return nil
+end
+
+
+--- Executes `code` as a Lua chunk in the global namespace with the variable
+-- pos available, for each node in a region defined by positions `pos1` and
+-- `pos2`.
+-- @return An error message if the code fails, or nil on success.
+function worldedit.luatransform(pos1, pos2, code)
+ pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ local factory, err = loadstring("return function(pos) " .. code .. " end")
+ if not factory then -- Syntax error
+ return err
+ end
+ local func = factory()
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ 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 good, err = pcall(func, pos)
+ if not good then -- Runtime error
+ return err
+ end
+ pos.z = pos.z + 1
+ end
+ pos.y = pos.y + 1
+ end
+ pos.x = pos.x + 1
+ end
+ return nil
+end
+
diff --git a/worldedit/worldedit/common.lua b/worldedit/worldedit/common.lua
new file mode 100644
index 0000000..be9a2c9
--- /dev/null
+++ b/worldedit/worldedit/common.lua
@@ -0,0 +1,114 @@
+--- Common functions [INTERNAL]. All of these functions are internal!
+-- @module worldedit.common
+
+--- Copies and modifies positions `pos1` and `pos2` so that each component of
+-- `pos1` is less than or equal to the corresponding component of `pos2`.
+-- Returns the new positions.
+function worldedit.sort_pos(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`.
+-- @return The volume.
+function worldedit.volume(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
+
+
+--- Gets other axes given an axis.
+-- @raise Axis must be x, y, or z!
+function worldedit.get_axis_others(axis)
+ if axis == "x" then
+ return "y", "z"
+ elseif axis == "y" then
+ return "x", "z"
+ elseif axis == "z" then
+ return "x", "y"
+ else
+ error("Axis must be x, y, or z!")
+ end
+end
+
+
+function worldedit.keep_loaded(pos1, pos2)
+ local manip = minetest.get_voxel_manip()
+ manip:read_from_map(pos1, pos2)
+end
+
+
+local mh = {}
+worldedit.manip_helpers = mh
+
+
+--- Generates an empty VoxelManip data table for an area.
+-- @return The empty data table.
+function mh.get_empty_data(area)
+ -- Fill emerged area with ignore so that blocks in the area that are
+ -- only partially modified aren't overwriten.
+ local data = {}
+ local c_ignore = minetest.get_content_id("ignore")
+ for i = 1, worldedit.volume(area.MinEdge, area.MaxEdge) do
+ data[i] = c_ignore
+ end
+ return data
+end
+
+
+function mh.init(pos1, pos2)
+ 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})
+ return manip, area
+end
+
+
+function mh.init_radius(pos, radius)
+ local pos1 = vector.subtract(pos, radius)
+ local pos2 = vector.add(pos, radius)
+ return mh.init(pos1, pos2)
+end
+
+
+function mh.init_axis_radius(base_pos, axis, radius)
+ return mh.init_axis_radius_length(base_pos, axis, radius, radius)
+end
+
+
+function mh.init_axis_radius_length(base_pos, axis, radius, length)
+ local other1, other2 = worldedit.get_axis_others(axis)
+ local pos1 = {
+ [axis] = base_pos[axis],
+ [other1] = base_pos[other1] - radius,
+ [other2] = base_pos[other2] - radius
+ }
+ local pos2 = {
+ [axis] = base_pos[axis] + length,
+ [other1] = base_pos[other1] + radius,
+ [other2] = base_pos[other2] + radius
+ }
+ return mh.init(pos1, pos2)
+end
+
+
+function mh.finish(manip, data)
+ -- Update map
+ manip:set_data(data)
+ manip:write_to_map()
+ manip:update_map()
+end
+
diff --git a/worldedit/worldedit/compatibility.lua b/worldedit/worldedit/compatibility.lua
new file mode 100644
index 0000000..c989a05
--- /dev/null
+++ b/worldedit/worldedit/compatibility.lua
@@ -0,0 +1,74 @@
+--- Compatibility functions.
+-- @module worldedit.compatibility
+
+local function deprecated(new_func)
+ local info = debug.getinfo(1, "n")
+ local msg = "worldedit." .. info.name .. "() is deprecated."
+ if new_func then
+ msg = msg .. " Use worldedit." .. new_func .. "() instead."
+ end
+ minetest.log("deprecated", msg)
+end
+
+worldedit.allocate_old = worldedit.allocate
+
+worldedit.deserialize_old = worldedit.deserialize
+
+function worldedit.metasave(pos1, pos2, filename)
+ deprecated("save")
+ local file, err = io.open(filename, "wb")
+ if err then return 0 end
+ local data, count = worldedit.serialize(pos1, pos2)
+ file:write(data)
+ file:close()
+ return count
+end
+
+function worldedit.metaload(originpos, filename)
+ deprecated("load")
+ filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem"
+ local file, err = io.open(filename, "wb")
+ if err then return 0 end
+ local data = file:read("*a")
+ return worldedit.deserialize(originpos, data)
+end
+
+function worldedit.scale(pos1, pos2, factor)
+ deprecated("stretch")
+ return worldedit.stretch(pos1, pos2, factor, factor, factor)
+end
+
+function worldedit.valueversion(value)
+ deprecated("read_header")
+ local version = worldedit.read_header(value)
+ if not version or version > worldedit.LATEST_SERIALIZATION_VERSION then
+ return 0
+ end
+ return version
+end
+
+function worldedit.replaceinverse(pos1, pos2, search_node, replace_node)
+ deprecated("replace")
+ return worldedit.replace(pos1, pos2, search_node, replace_node, true)
+end
+
+function worldedit.clearobjects(...)
+ deprecated("clear_objects")
+ return worldedit.clear_objects(...)
+end
+
+function worldedit.hollow_sphere(pos, radius, node_name)
+ deprecated("sphere")
+ return worldedit.sphere(pos, radius, node_name, true)
+end
+
+function worldedit.hollow_dome(pos, radius, node_name)
+ deprecated("dome")
+ return worldedit.dome(pos, radius, node_name, true)
+end
+
+function worldedit.hollow_cylinder(pos, axis, length, radius, node_name)
+ deprecated("cylinder")
+ return worldedit.cylinder(pos, axis, length, radius, node_name, true)
+end
+
diff --git a/worldedit/worldedit/init.lua b/worldedit/worldedit/init.lua
new file mode 100644
index 0000000..e193454
--- /dev/null
+++ b/worldedit/worldedit/init.lua
@@ -0,0 +1,44 @@
+--- Worldedit.
+-- @module worldedit
+-- @release 1.1
+-- @copyright 2013 sfan5, Anthony Zhang (Uberi/Temperest), and Brett O'Donnell (cornernote).
+-- @license GNU Affero General Public License version 3 (AGPLv3)
+-- @author sfan5
+-- @author Anthony Zang (Uberi/Temperest)
+-- @author Bret O'Donnel (cornernote)
+-- @author ShadowNinja
+
+worldedit = {}
+worldedit.version = {1, 1, major=1, minor=1}
+worldedit.version_string = table.concat(worldedit.version, ".")
+
+if not minetest.get_voxel_manip then
+ local err_msg = "This version of WorldEdit requires Minetest 0.4.8 or later! You have an old version."
+ minetest.log("error", string.rep("#", 128))
+ minetest.log("error", err_msg)
+ minetest.log("error", string.rep("#", 128))
+ error(err_msg)
+end
+
+local path = minetest.get_modpath(minetest.get_current_modname())
+
+local function load_module(path)
+ local file = io.open(path, "r")
+ if not file then return end
+ file:close()
+ return dofile(path)
+end
+
+dofile(path .. "/common.lua")
+load_module(path .. "/manipulations.lua")
+load_module(path .. "/primitives.lua")
+load_module(path .. "/visualization.lua")
+load_module(path .. "/serialization.lua")
+load_module(path .. "/code.lua")
+load_module(path .. "/compatibility.lua")
+
+
+if minetest.setting_getbool("log_mods") then
+ print("[WorldEdit] Loaded!")
+end
+
diff --git a/worldedit/worldedit/manipulations.lua b/worldedit/worldedit/manipulations.lua
new file mode 100644
index 0000000..cf95517
--- /dev/null
+++ b/worldedit/worldedit/manipulations.lua
@@ -0,0 +1,629 @@
+--- Generic node manipulations.
+-- @module worldedit.manipulations
+
+local mh = worldedit.manip_helpers
+
+
+--- 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)
+
+ local manip, area = mh.init(pos1, pos2)
+ local data = mh.get_empty_data(area)
+
+ 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
+ local id_count, rand = #node_ids, math.random
+ for i in area:iterp(pos1, pos2) do
+ data[i] = node_ids[rand(id_count)]
+ end
+ end
+
+ mh.finish(manip, data)
+
+ return worldedit.volume(pos1, pos2)
+end
+
+
+--- 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)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ local manip, area = mh.init(pos1, pos2)
+ local data = manip:get_data()
+
+ local search_id = minetest.get_content_id(search_node)
+ local replace_id = minetest.get_content_id(replace_node)
+
+ local count = 0
+
+ --- TODO: This could be shortened by checking `inverse` in the loop,
+ -- but that would have a speed penalty. Is the penalty big enough
+ -- to matter?
+ 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
+ end
+ end
+ else
+ for i in area:iterp(pos1, pos2) do
+ if data[i] ~= search_id then
+ data[i] = replace_id
+ count = count + 1
+ end
+ end
+ end
+
+ mh.finish(manip, data)
+
+ return count
+end
+
+
+--- Duplicates a region `amount` times with offset vector `direction`.
+-- Stacking is spread across server steps, one copy per step.
+-- @return The number of nodes stacked.
+function worldedit.stack2(pos1, pos2, direction, amount, finished)
+ local i = 0
+ local translated = {x=0, y=0, z=0}
+ local function next_one()
+ if i < amount then
+ i = i + 1
+ translated.x = translated.x + direction.x
+ translated.y = translated.y + direction.y
+ translated.z = translated.z + direction.z
+ worldedit.copy2(pos1, pos2, translated)
+ minetest.after(0, next_one)
+ else
+ if finished then
+ finished()
+ end
+ end
+ end
+ next_one()
+ return worldedit.volume(pos1, pos2) * amount
+end
+
+
+--- 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)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local get_node, get_meta, set_node = minetest.get_node,
+ minetest.get_meta, minetest.set_node
+ -- Copy things backwards when negative to avoid corruption.
+ -- FIXME: Lots of code duplication here.
+ if amount < 0 then
+ local pos = {}
+ pos.x = pos1.x
+ 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 = get_node(pos) -- Obtain current node
+ local meta = get_meta(pos):to_table() -- Get meta of current node
+ local value = pos[axis] -- Store current position
+ pos[axis] = value + amount -- Move along axis
+ set_node(pos, node) -- Copy node to new position
+ 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 = {}
+ pos.x = pos2.x
+ 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 = get_node(pos) -- Obtain current node
+ local meta = get_meta(pos):to_table() -- Get meta of current node
+ local value = pos[axis] -- Store current position
+ pos[axis] = value + amount -- Move along axis
+ set_node(pos, node) -- Copy node to new position
+ 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
+
+--- Copies a region by offset vector `off`.
+-- @param pos1
+-- @param pos2
+-- @param off
+-- @return The number of nodes copied.
+function worldedit.copy2(pos1, pos2, off)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local get_node, get_meta, set_node = minetest.get_node,
+ minetest.get_meta, minetest.set_node
+ local pos = {}
+ pos.x = pos2.x
+ 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 = get_node(pos) -- Obtain current node
+ local meta = get_meta(pos):to_table() -- Get meta of current node
+ local newpos = vector.add(pos, off) -- Calculate new position
+ set_node(newpos, node) -- Copy node to new position
+ get_meta(newpos):from_table(meta) -- Set metadata of new node
+ pos.z = pos.z - 1
+ end
+ pos.y = pos.y - 1
+ end
+ pos.x = pos.x - 1
+ end
+ return worldedit.volume(pos1, pos2)
+end
+
+--- Moves a region along `axis` by `amount` nodes.
+-- @return The number of nodes moved.
+function worldedit.move(pos1, pos2, axis, amount)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ --- TODO: 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).
+ local get_node, get_meta, set_node, remove_node = minetest.get_node,
+ minetest.get_meta, minetest.set_node, minetest.remove_node
+ -- Copy things backwards when negative to avoid corruption.
+ --- FIXME: Lots of code duplication here.
+ if amount < 0 then
+ local pos = {}
+ pos.x = pos1.x
+ 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 = get_node(pos) -- Obtain current node
+ local meta = get_meta(pos):to_table() -- Get metadata of current node
+ remove_node(pos) -- Remove current node
+ local value = pos[axis] -- Store current position
+ pos[axis] = value + amount -- Move along axis
+ set_node(pos, node) -- Move node to new position
+ 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 = {}
+ pos.x = pos2.x
+ 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 = get_node(pos) -- Obtain current node
+ local meta = get_meta(pos):to_table() -- Get metadata of current node
+ remove_node(pos) -- Remove current node
+ local value = pos[axis] -- Store current position
+ pos[axis] = value + amount -- Move along axis
+ set_node(pos, node) -- Move node to new position
+ 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 a region along `axis` `amount` times.
+-- Stacking is spread across server steps, one copy per step.
+-- @param pos1
+-- @param pos2
+-- @param axis Axis direction, "x", "y", or "z".
+-- @param count
+-- @return The number of nodes stacked.
+function worldedit.stack(pos1, pos2, axis, count)
+ 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
+ local i = 1
+ function next_one()
+ if i <= count then
+ i = i + 1
+ amount = amount + length
+ copy(pos1, pos2, axis, amount)
+ minetest.after(0, next_one)
+ end
+ end
+ next_one()
+ return worldedit.volume(pos1, pos2) * count
+end
+
+
+--- Stretches a region by a factor of positive integers along the X, Y, and Z
+-- axes, respectively, with `pos1` as the origin.
+-- @param pos1
+-- @param pos2
+-- @param stretch_x Amount to stretch along X axis.
+-- @param stretch_y Amount to stretch along Y axis.
+-- @param stretch_z Amount to stretch along Z axis.
+-- @return The number of nodes scaled.
+-- @return The new scaled position 1.
+-- @return The new scaled position 2.
+function worldedit.stretch(pos1, pos2, stretch_x, stretch_y, stretch_z)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ -- Prepare schematic of large node
+ local get_node, get_meta, place_schematic = minetest.get_node,
+ minetest.get_meta, minetest.place_schematic
+ local placeholder_node = {name="", param1=255, param2=0}
+ local nodes = {}
+ for i = 1, stretch_x * stretch_y * stretch_z do
+ nodes[i] = placeholder_node
+ end
+ local schematic = {size={x=stretch_x, y=stretch_y, z=stretch_z}, data=nodes}
+
+ local size_x, size_y, size_z = stretch_x - 1, stretch_y - 1, stretch_z - 1
+
+ local new_pos2 = {
+ x = pos1.x + (pos2.x - pos1.x) * stretch_x + size_x,
+ y = pos1.y + (pos2.y - pos1.y) * stretch_y + size_y,
+ z = pos1.z + (pos2.z - pos1.z) * stretch_z + size_z,
+ }
+ worldedit.keep_loaded(pos1, new_pos2)
+
+ local pos = {x=pos2.x, y=0, z=0}
+ local big_pos = {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
+ local node = get_node(pos) -- Get current node
+ local meta = get_meta(pos):to_table() -- Get meta of current node
+
+ -- Calculate far corner of the big node
+ local pos_x = pos1.x + (pos.x - pos1.x) * stretch_x
+ local pos_y = pos1.y + (pos.y - pos1.y) * stretch_y
+ local pos_z = pos1.z + (pos.z - pos1.z) * stretch_z
+
+ -- Create large node
+ placeholder_node.name = node.name
+ placeholder_node.param2 = node.param2
+ big_pos.x, big_pos.y, big_pos.z = pos_x, pos_y, pos_z
+ place_schematic(big_pos, schematic)
+
+ -- Fill in large node meta
+ if next(meta.fields) ~= nil or next(meta.inventory) ~= nil then
+ -- Node has meta fields
+ for x = 0, size_x do
+ for y = 0, size_y do
+ for z = 0, size_z do
+ big_pos.x = pos_x + x
+ big_pos.y = pos_y + y
+ big_pos.z = pos_z + z
+ -- Set metadata of new node
+ get_meta(big_pos):from_table(meta)
+ end
+ end
+ end
+ end
+ pos.z = pos.z - 1
+ end
+ pos.y = pos.y - 1
+ end
+ pos.x = pos.x - 1
+ end
+ return worldedit.volume(pos1, pos2) * stretch_x * stretch_y * stretch_z, pos1, new_pos2
+end
+
+
+--- Transposes a region between two axes.
+-- @return The number of nodes transposed.
+-- @return The new transposed position 1.
+-- @return The new transposed position 2.
+function worldedit.transpose(pos1, pos2, axis1, axis2)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ 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
+ local new_pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
+ new_pos2[axis1] = pos1[axis1] + extent2
+ new_pos2[axis2] = pos1[axis2] + extent1
+
+ local upper_bound = {x=pos2.x, y=pos2.y, z=pos2.z}
+ if upper_bound[axis1] < new_pos2[axis1] then upper_bound[axis1] = new_pos2[axis1] end
+ if upper_bound[axis2] < new_pos2[axis2] then upper_bound[axis2] = new_pos2[axis2] end
+ worldedit.keep_loaded(pos1, upper_bound)
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local get_node, get_meta, set_node = minetest.get_node,
+ minetest.get_meta, minetest.set_node
+ 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]
+ if compare(extent1, extent2) then -- Transpose only if below the diagonal
+ local node1 = get_node(pos)
+ local meta1 = get_meta(pos):to_table()
+ local value1, value2 = pos[axis1], pos[axis2] -- Save position values
+ pos[axis1], pos[axis2] = pos1[axis1] + extent2, pos1[axis2] + extent1 -- Swap axis extents
+ local node2 = get_node(pos)
+ local meta2 = get_meta(pos):to_table()
+ set_node(pos, node1)
+ get_meta(pos):from_table(meta1)
+ pos[axis1], pos[axis2] = value1, value2 -- Restore position values
+ set_node(pos, node2)
+ get_meta(pos):from_table(meta2)
+ end
+ pos.z = pos.z + 1
+ end
+ pos.y = pos.y + 1
+ end
+ pos.x = pos.x + 1
+ end
+ return worldedit.volume(pos1, pos2), pos1, new_pos2
+end
+
+
+--- Flips a region along `axis`.
+-- @return The number of nodes flipped.
+function worldedit.flip(pos1, pos2, axis)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ --- TODO: Flip the region slice by slice along the flip axis using schematic method.
+ 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)
+ local get_node, get_meta, set_node = minetest.get_node,
+ minetest.get_meta, minetest.set_node
+ 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 node1 = get_node(pos)
+ local meta1 = get_meta(pos):to_table()
+ local value = pos[axis] -- Save position
+ pos[axis] = start - value -- Shift position
+ local node2 = get_node(pos)
+ local meta2 = get_meta(pos):to_table()
+ set_node(pos, node1)
+ get_meta(pos):from_table(meta1)
+ pos[axis] = value -- Restore position
+ set_node(pos, node2)
+ get_meta(pos):from_table(meta2)
+ 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 clockwise around an axis.
+-- @param pos1
+-- @param pos2
+-- @param axis Axis ("x", "y", or "z").
+-- @param angle Angle in degrees (90 degree increments only).
+-- @return The number of nodes rotated.
+-- @return The new first position.
+-- @return The new second position.
+function worldedit.rotate(pos1, pos2, axis, angle)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ local other1, other2 = worldedit.get_axis_others(axis)
+ angle = angle % 360
+
+ local count
+ if angle == 90 then
+ worldedit.flip(pos1, pos2, other1)
+ count, pos1, pos2 = worldedit.transpose(pos1, pos2, other1, other2)
+ elseif angle == 180 then
+ worldedit.flip(pos1, pos2, other1)
+ count = worldedit.flip(pos1, pos2, other2)
+ elseif angle == 270 then
+ worldedit.flip(pos1, pos2, other2)
+ count, pos1, pos2 = worldedit.transpose(pos1, pos2, other1, other2)
+ else
+ error("Only 90 degree increments are supported!")
+ end
+ return count, pos1, pos2
+end
+
+
+--- Rotates all oriented nodes in a region clockwise around the Y axis.
+-- @param pos1
+-- @param pos2
+-- @param angle Angle in degrees (90 degree increments only).
+-- @return The number of nodes oriented.
+-- TODO: Support 6D facedir rotation along arbitrary axis.
+function worldedit.orient(pos1, pos2, angle)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+ local registered_nodes = minetest.registered_nodes
+
+ local wallmounted = {
+ [90] = {[0]=0, 1, 5, 4, 2, 3},
+ [180] = {[0]=0, 1, 3, 2, 5, 4},
+ [270] = {[0]=0, 1, 4, 5, 3, 2}
+ }
+ local facedir = {
+ [90] = {[0]=1, 2, 3, 0},
+ [180] = {[0]=2, 3, 0, 1},
+ [270] = {[0]=3, 0, 1, 2}
+ }
+
+ angle = angle % 360
+ if angle == 0 then
+ return 0
+ end
+ if angle % 90 ~= 0 then
+ error("Only 90 degree increments are supported!")
+ end
+ local wallmounted_substitution = wallmounted[angle]
+ local facedir_substitution = facedir[angle]
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local count = 0
+ local set_node, get_node, get_meta, swap_node = minetest.set_node,
+ minetest.get_node, minetest.get_meta, minetest.swap_node
+ 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 = get_node(pos)
+ local def = registered_nodes[node.name]
+ if def then
+ if def.paramtype2 == "wallmounted" then
+ node.param2 = wallmounted_substitution[node.param2]
+ local meta = get_meta(pos):to_table()
+ set_node(pos, node)
+ get_meta(pos):from_table(meta)
+ count = count + 1
+ elseif def.paramtype2 == "facedir" then
+ node.param2 = facedir_substitution[node.param2]
+ local meta = get_meta(pos):to_table()
+ set_node(pos, node)
+ get_meta(pos):from_table(meta)
+ 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
+
+
+--- Attempts to fix the lighting in a region.
+-- @return The number of nodes updated.
+function worldedit.fixlight(pos1, pos2)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ 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)
+ end
+ return #nodes
+end
+
+
+--- 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)
+
+ -- Offset positions to include full nodes (positions are in the center of nodes)
+ local pos1x, pos1y, pos1z = pos1.x - 0.5, pos1.y - 0.5, pos1.z - 0.5
+ local pos2x, pos2y, pos2z = pos2.x + 0.5, pos2.y + 0.5, pos2.z + 0.5
+
+ -- Center of region
+ local center = {
+ x = pos1x + ((pos2x - pos1x) / 2),
+ y = pos1y + ((pos2y - pos1y) / 2),
+ z = pos1z + ((pos2z - pos1z) / 2)
+ }
+ -- Bounding sphere radius
+ local radius = math.sqrt(
+ (center.x - pos1x) ^ 2 +
+ (center.y - pos1y) ^ 2 +
+ (center.z - pos1z) ^ 2)
+ local count = 0
+ for _, obj in pairs(minetest.get_objects_inside_radius(center, radius)) do
+ local entity = obj:get_luaentity()
+ -- Avoid players and WorldEdit entities
+ if not obj:is_player() and (not entity or
+ not entity.name:find("^worldedit:")) then
+ 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
+ -- Inside region
+ obj:remove()
+ count = count + 1
+ end
+ end
+ end
+ return count
+end
+
diff --git a/worldedit/worldedit/primitives.lua b/worldedit/worldedit/primitives.lua
new file mode 100644
index 0000000..962a02f
--- /dev/null
+++ b/worldedit/worldedit/primitives.lua
@@ -0,0 +1,273 @@
+--- Functions for creating primitive shapes.
+-- @module worldedit.primitives
+
+local mh = worldedit.manip_helpers
+
+
+--- Adds a sphere of `node_name` centered at `pos`.
+-- @param pos Position to center sphere at.
+-- @param radius Sphere radius.
+-- @param node_name Name of node to make shere of.
+-- @param hollow Whether the sphere should be hollow.
+-- @return The number of nodes added.
+function worldedit.sphere(pos, radius, node_name, hollow)
+ local manip, area = mh.init_radius(pos, radius)
+
+ local data = mh.get_empty_data(area)
+
+ -- Fill selected area with node
+ local node_id = minetest.get_content_id(node_name)
+ local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
+ local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
+ local stride_z, stride_y = area.zstride, area.ystride
+ local count = 0
+ for z = -radius, radius do
+ -- Offset contributed by z plus 1 to make it 1-indexed
+ local new_z = (z + offset_z) * stride_z + 1
+ for y = -radius, radius do
+ local new_y = new_z + (y + offset_y) * stride_y
+ for x = -radius, radius do
+ local squared = x * x + y * y + z * z
+ if squared <= max_radius and (not hollow or squared >= min_radius) then
+ -- Position is on surface of sphere
+ local i = new_y + (x + offset_x)
+ data[i] = node_id
+ count = count + 1
+ end
+ end
+ end
+ end
+
+ mh.finish(manip, data)
+
+ return count
+end
+
+
+--- Adds a dome.
+-- @param pos Position to center dome at.
+-- @param radius Dome radius. Negative for concave domes.
+-- @param node_name Name of node to make dome of.
+-- @param hollow Whether the dome should be hollow.
+-- @return The number of nodes added.
+-- TODO: Add axis option.
+function worldedit.dome(pos, radius, node_name, hollow)
+ local min_y, max_y = 0, radius
+ if radius < 0 then
+ radius = -radius
+ min_y, max_y = -radius, 0
+ end
+
+ local manip, area = mh.init_axis_radius(pos, "y", radius)
+ local data = mh.get_empty_data(area)
+
+ -- Add dome
+ local node_id = minetest.get_content_id(node_name)
+ local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
+ local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
+ local stride_z, stride_y = area.zstride, area.ystride
+ local count = 0
+ for z = -radius, radius do
+ local new_z = (z + offset_z) * stride_z + 1 --offset contributed by z plus 1 to make it 1-indexed
+ for y = min_y, max_y do
+ local new_y = new_z + (y + offset_y) * stride_y
+ for x = -radius, radius do
+ local squared = x * x + y * y + z * z
+ if squared <= max_radius and (not hollow or squared >= min_radius) then
+ -- Position is in dome
+ local i = new_y + (x + offset_x)
+ data[i] = node_id
+ count = count + 1
+ end
+ end
+ end
+ end
+
+ mh.finish(manip, data)
+
+ return count
+end
+
+--- Adds a cylinder.
+-- @param pos Position to center base of cylinder at.
+-- @param axis Axis ("x", "y", or "z")
+-- @param length Cylinder length.
+-- @param radius Cylinder radius.
+-- @param node_name Name of node to make cylinder of.
+-- @param hollow Whether the cylinder should be hollow.
+-- @return The number of nodes added.
+function worldedit.cylinder(pos, axis, length, radius, node_name, hollow)
+ local other1, other2 = worldedit.get_axis_others(axis)
+
+ -- Handle negative lengths
+ local current_pos = {x=pos.x, y=pos.y, z=pos.z}
+ if length < 0 then
+ length = -length
+ current_pos[axis] = current_pos[axis] - length
+ end
+
+ -- Set up voxel manipulator
+ local manip, area = mh.init_axis_radius_length(current_pos, axis, radius, length)
+ local data = mh.get_empty_data(area)
+
+ -- Add cylinder
+ local node_id = minetest.get_content_id(node_name)
+ local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
+ local stride = {x=1, y=area.ystride, z=area.zstride}
+ local offset = {
+ x = current_pos.x - area.MinEdge.x,
+ y = current_pos.y - area.MinEdge.y,
+ z = current_pos.z - area.MinEdge.z,
+ }
+ local min_slice, max_slice = offset[axis], offset[axis] + length - 1
+ local count = 0
+ for index2 = -radius, radius do
+ -- Offset contributed by other axis 1 plus 1 to make it 1-indexed
+ local new_index2 = (index2 + offset[other1]) * stride[other1] + 1
+ for index3 = -radius, radius do
+ local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]
+ local squared = index2 * index2 + index3 * index3
+ if squared <= max_radius and (not hollow or squared >= min_radius) then
+ -- Position is in cylinder
+ -- Add column along axis
+ for index1 = min_slice, max_slice do
+ local vi = new_index3 + index1 * stride[axis]
+ data[vi] = node_id
+ end
+ count = count + length
+ end
+ end
+ end
+
+ mh.finish(manip, data)
+
+ return count
+end
+
+
+--- Adds a pyramid.
+-- @param pos Position to center base of pyramid at.
+-- @param axis Axis ("x", "y", or "z")
+-- @param height Pyramid height.
+-- @param node_name Name of node to make pyramid of.
+-- @return The number of nodes added.
+function worldedit.pyramid(pos, axis, height, node_name)
+ local other1, other2 = worldedit.get_axis_others(axis)
+
+ -- Set up voxel manipulator
+ local manip, area = mh.init_axis_radius(pos, axis,
+ height >= 0 and height or -height)
+ local data = mh.get_empty_data(area)
+
+ -- Handle inverted pyramids
+ local start_axis, end_axis, step
+ if height > 0 then
+ height = height - 1
+ step = 1
+ else
+ height = height + 1
+ step = -1
+ end
+
+ -- Add pyramid
+ local node_id = minetest.get_content_id(node_name)
+ local stride = {x=1, y=area.ystride, z=area.zstride}
+ local offset = {
+ x = pos.x - area.MinEdge.x,
+ y = pos.y - area.MinEdge.y,
+ z = pos.z - area.MinEdge.z,
+ }
+ local size = math.abs(height * step)
+ local count = 0
+ -- For each level of the pyramid
+ for index1 = 0, height, step do
+ -- Offset contributed by axis plus 1 to make it 1-indexed
+ local new_index1 = (index1 + offset[axis]) * stride[axis] + 1
+ for index2 = -size, size do
+ local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]
+ for index3 = -size, size do
+ local i = new_index2 + (index3 + offset[other2]) * stride[other2]
+ data[i] = node_id
+ end
+ end
+ count = count + (size * 2 + 1) ^ 2
+ size = size - 1
+ end
+
+ mh.finish(manip, data)
+
+ return count
+end
+
+--- Adds a spiral.
+-- @param pos Position to center spiral at.
+-- @param length Spral length.
+-- @param height Spiral height.
+-- @param spacer Space between walls.
+-- @param node_name Name of node to make spiral of.
+-- @return Number of nodes added.
+-- TODO: Add axis option.
+function worldedit.spiral(pos, length, height, spacer, node_name)
+ local extent = math.ceil(length / 2)
+
+ local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)
+ local data = mh.get_empty_data(area)
+
+ -- Set up variables
+ local node_id = minetest.get_content_id(node_name)
+ local stride = {x=1, y=area.ystride, z=area.zstride}
+ local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
+ local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1
+
+ -- Add first column
+ local count = height
+ local column = i
+ for y = 1, height do
+ data[column] = node_id
+ column = column + stride.y
+ end
+
+ -- Add spiral segments
+ local stride_axis, stride_other = stride.x, stride.z
+ local sign = -1
+ local segment_length = 0
+ spacer = spacer + 1
+ -- Go through each segment except the last
+ for segment = 1, math.floor(length / spacer) * 2 do
+ -- Change sign and length every other turn starting with the first
+ if segment % 2 == 1 then
+ sign = -sign
+ segment_length = segment_length + spacer
+ end
+ -- Fill segment
+ for index = 1, segment_length do
+ -- Move along the direction of the segment
+ i = i + stride_axis * sign
+ local column = i
+ -- Add column
+ for y = 1, height do
+ data[column] = node_id
+ column = column + stride.y
+ end
+ end
+ count = count + segment_length * height
+ stride_axis, stride_other = stride_other, stride_axis -- Swap axes
+ end
+
+ -- Add shorter final segment
+ sign = -sign
+ for index = 1, segment_length do
+ i = i + stride_axis * sign
+ local column = i
+ -- Add column
+ for y = 1, height do
+ data[column] = node_id
+ column = column + stride.y
+ end
+ end
+ count = count + segment_length * height
+
+ mh.finish(manip, data)
+
+ return count
+end
diff --git a/worldedit/worldedit/serialization.lua b/worldedit/worldedit/serialization.lua
new file mode 100644
index 0000000..00d984d
--- /dev/null
+++ b/worldedit/worldedit/serialization.lua
@@ -0,0 +1,239 @@
+--- Schematic serialization and deserialiation.
+-- @module worldedit.serialization
+
+worldedit.LATEST_SERIALIZATION_VERSION = 5
+local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"
+
+
+--[[
+Serialization version history:
+ 1: Original format. Serialized Lua table with a weird linked format...
+ 2: Position and node seperated into sub-tables in fields `1` and `2`.
+ 3: List of nodes, one per line, with fields seperated by spaces.
+ Format: <X> <Y> <Z> <Name> <Param1> <Param2>
+ 4: Serialized Lua table containing a list of nodes with `x`, `y`, `z`,
+ `name`, `param1`, `param2`, and `meta` fields.
+ 5: Added header and made `param1`, `param2`, and `meta` fields optional.
+ Header format: <Version>,<ExtraHeaderField1>,...:<Content>
+--]]
+
+
+--- Reads the header of serialized data.
+-- @param value Serialized WorldEdit data.
+-- @return The version as a positive natural number, or 0 for unknown versions.
+-- @return Extra header fields as a list of strings, or nil if not supported.
+-- @return Content (data after header).
+function worldedit.read_header(value)
+ if value:find("^[0-9]+[%-:]") then
+ local header_end = value:find(":", 1, true)
+ local header = value:sub(1, header_end - 1):split(",")
+ local version = tonumber(header[1])
+ table.remove(header, 1)
+ local content = value:sub(header_end + 1)
+ return version, header, content
+ end
+ -- Old versions that didn't include a header with a version number
+ if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then -- List format
+ return 3, nil, value
+ elseif value:find("^[^\"']+%{%d+%}") then
+ if value:find("%[\"meta\"%]") then -- Meta flat table format
+ return 2, nil, value
+ end
+ return 1, nil, value -- Flat table format
+ elseif value:find("%{") then -- Raw nested table format
+ return 4, nil, value
+ end
+ return nil
+end
+
+
+--- Converts the region defined by positions `pos1` and `pos2`
+-- into a single string.
+-- @return The serialized data.
+-- @return The number of nodes serialized.
+function worldedit.serialize(pos1, pos2)
+ pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local count = 0
+ local result = {}
+ 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 = get_node(pos)
+ if node.name ~= "air" and node.name ~= "ignore" then
+ count = count + 1
+ local meta = get_meta(pos):to_table()
+
+ local meta_empty = true
+ -- Convert metadata item stacks to item strings
+ for name, inventory in pairs(meta.inventory) do
+ for index, stack in ipairs(inventory) do
+ meta_empty = false
+ inventory[index] = stack.to_string and stack:to_string() or stack
+ end
+ end
+ for k in pairs(meta) do
+ if k ~= "inventory" then
+ meta_empty = false
+ break
+ end
+ end
+
+ result[count] = {
+ x = pos.x - pos1.x,
+ y = pos.y - pos1.y,
+ z = pos.z - pos1.z,
+ name = node.name,
+ param1 = node.param1 ~= 0 and node.param1 or nil,
+ param2 = node.param2 ~= 0 and node.param2 or nil,
+ meta = not meta_empty and meta or nil,
+ }
+ end
+ pos.z = pos.z + 1
+ end
+ pos.y = pos.y + 1
+ end
+ pos.x = pos.x + 1
+ end
+ -- Serialize entries
+ result = minetest.serialize(result)
+ return LATEST_SERIALIZATION_HEADER .. result, count
+end
+
+
+--- Loads the schematic in `value` into a node list in the latest format.
+-- Contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile)
+-- by ChillCode, available under the MIT license.
+-- @return A node list in the latest format, or nil on failure.
+local function load_schematic(value)
+ local version, header, content = worldedit.read_header(value)
+ local nodes = {}
+ if version == 1 or version == 2 then -- Original flat table format
+ local tables = minetest.deserialize(content)
+ if not tables then return nil end
+
+ -- Transform the node table into an array of nodes
+ for i = 1, #tables do
+ for j, v in pairs(tables[i]) do
+ if type(v) == "table" then
+ tables[i][j] = tables[v[1]]
+ end
+ end
+ end
+ nodes = tables[1]
+
+ if version == 1 then --original flat table format
+ for i, entry in ipairs(nodes) do
+ local pos = entry[1]
+ entry.x, entry.y, entry.z = pos.x, pos.y, pos.z
+ entry[1] = nil
+ local node = entry[2]
+ entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2
+ entry[2] = nil
+ end
+ end
+ elseif version == 3 then -- List format
+ for x, y, z, name, param1, param2 in content:gmatch(
+ "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..
+ "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do
+ param1, param2 = tonumber(param1), tonumber(param2)
+ table.insert(nodes, {
+ x = originx + tonumber(x),
+ y = originy + tonumber(y),
+ z = originz + tonumber(z),
+ name = name,
+ param1 = param1 ~= 0 and param1 or nil,
+ param2 = param2 ~= 0 and param2 or nil,
+ })
+ end
+ elseif version == 4 or version == 5 then -- Nested table format
+ if not jit then
+ -- This is broken for larger tables in the current version of LuaJIT
+ nodes = minetest.deserialize(content)
+ else
+ -- XXX: This is a filthy hack that works surprisingly well - in LuaJIT, `minetest.deserialize` will fail due to the register limit
+ nodes = {}
+ content = content:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data
+ local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
+ local startpos, startpos1, endpos = 1, 1
+ while true do -- go through each individual node entry (except the last)
+ startpos, endpos = escaped:find("},%s*{", startpos)
+ if not startpos then
+ break
+ end
+ local current = content:sub(startpos1, startpos)
+ local entry = minetest.deserialize("return " .. current)
+ table.insert(nodes, entry)
+ startpos, startpos1 = endpos, endpos
+ end
+ local entry = minetest.deserialize("return " .. content:sub(startpos1)) -- process the last entry
+ table.insert(nodes, entry)
+ end
+ else
+ return nil
+ end
+ return nodes
+end
+
+--- Determines the volume the nodes represented by string `value` would occupy
+-- if deserialized at `origin_pos`.
+-- @return Low corner position.
+-- @return High corner position.
+-- @return The number of nodes.
+function worldedit.allocate(origin_pos, value)
+ local nodes = load_schematic(value)
+ if not nodes then return nil end
+ return worldedit.allocate_with_nodes(origin_pos, nodes)
+end
+
+
+-- Internal
+function worldedit.allocate_with_nodes(origin_pos, nodes)
+ local huge = math.huge
+ local pos1x, pos1y, pos1z = huge, huge, huge
+ local pos2x, pos2y, pos2z = -huge, -huge, -huge
+ local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
+ for i, entry in ipairs(nodes) do
+ local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
+ if x < pos1x then pos1x = x end
+ if y < pos1y then pos1y = y end
+ if z < pos1z then pos1z = z end
+ if x > pos2x then pos2x = x end
+ if y > pos2y then pos2y = y end
+ if z > pos2z then pos2z = z end
+ end
+ local pos1 = {x=pos1x, y=pos1y, z=pos1z}
+ local pos2 = {x=pos2x, y=pos2y, z=pos2z}
+ return pos1, pos2, #nodes
+end
+
+
+--- Loads the nodes represented by string `value` at position `origin_pos`.
+-- @return The number of nodes deserialized.
+function worldedit.deserialize(origin_pos, value)
+ local nodes = load_schematic(value)
+ if not nodes then return nil end
+
+ local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)
+ worldedit.keep_loaded(pos1, pos2)
+
+ local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
+ local count = 0
+ local add_node, get_meta = minetest.add_node, minetest.get_meta
+ for i, entry in ipairs(nodes) do
+ entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
+ -- Entry acts as both position and node
+ add_node(entry, entry)
+ if entry.meta then
+ get_meta(entry):from_table(entry.meta)
+ end
+ end
+ return #nodes
+end
+
diff --git a/worldedit/worldedit/visualization.lua b/worldedit/worldedit/visualization.lua
new file mode 100644
index 0000000..5ac49f3
--- /dev/null
+++ b/worldedit/worldedit/visualization.lua
@@ -0,0 +1,131 @@
+--- Functions for visibly hiding nodes
+-- @module worldedit.visualization
+
+minetest.register_node("worldedit:placeholder", {
+ drawtype = "airlike",
+ paramtype = "light",
+ sunlight_propagates = true,
+ diggable = false,
+ walkable = 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.
+-- @return The number of nodes hidden.
+function worldedit.hide(pos1, pos2)
+ pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local get_node, get_meta, swap_node = minetest.get_node,
+ minetest.get_meta, minetest.swap_node
+ 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 = get_node(pos)
+ if node.name ~= "air" and node.name ~= "worldedit:placeholder" then
+ -- Save the node's original name
+ get_meta(pos):set_string("worldedit_placeholder", node.name)
+ -- Swap in placeholder node
+ node.name = "worldedit:placeholder"
+ swap_node(pos, node)
+ end
+ 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 `node_name` in a region defined by positions
+-- `pos1` and `pos2` by non-destructively replacing them with invisible nodes.
+-- @return The number of nodes suppressed.
+function worldedit.suppress(pos1, pos2, node_name)
+ -- Ignore placeholder supression
+ if node_name == "worldedit:placeholder" then
+ return 0
+ end
+
+ pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local nodes = minetest.find_nodes_in_area(pos1, pos2, node_name)
+ local get_node, get_meta, swap_node = minetest.get_node,
+ minetest.get_meta, minetest.swap_node
+ for _, pos in ipairs(nodes) do
+ local node = get_node(pos)
+ -- Save the node's original name
+ get_meta(pos):set_string("worldedit_placeholder", node.name)
+ -- Swap in placeholder node
+ node.name = "worldedit:placeholder"
+ swap_node(pos, node)
+ end
+ return #nodes
+end
+
+--- Highlights all instances of `node_name` in a region defined by positions
+-- `pos1` and `pos2` by non-destructively hiding all other nodes.
+-- @return The number of nodes found.
+function worldedit.highlight(pos1, pos2, node_name)
+ pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local pos = {x=pos1.x, y=0, z=0}
+ local get_node, get_meta, swap_node = minetest.get_node,
+ minetest.get_meta, minetest.swap_node
+ local count = 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 = get_node(pos)
+ if node.name == node_name then -- Node found
+ count = count + 1
+ elseif node.name ~= "worldedit:placeholder" then -- Hide other nodes
+ -- Save the node's original name
+ get_meta(pos):set_string("worldedit_placeholder", node.name)
+ -- Swap in placeholder node
+ node.name = "worldedit:placeholder"
+ swap_node(pos, node)
+ 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`.
+-- @return The number of nodes restored.
+function worldedit.restore(pos1, pos2)
+ local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
+
+ worldedit.keep_loaded(pos1, pos2)
+
+ local nodes = minetest.find_nodes_in_area(pos1, pos2, "worldedit:placeholder")
+ local get_node, get_meta, swap_node = minetest.get_node,
+ minetest.get_meta, minetest.swap_node
+ for _, pos in ipairs(nodes) do
+ local node = get_node(pos)
+ local meta = get_meta(pos)
+ local data = meta:to_table()
+ node.name = data.fields.worldedit_placeholder
+ data.fields.worldedit_placeholder = nil
+ meta:from_table(data)
+ swap_node(pos, node)
+ end
+ return #nodes
+end
+