Minetest-WorldEditAdditions/worldeditadditions/lib/selection/selection.lua

84 lines
3.3 KiB
Lua
Raw Normal View History

2022-09-18 21:20:04 +00:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
2021-08-02 23:01:02 +00:00
-- ███████ ███████ ██ ███████ ██████ ████████ ██ ██████ ███ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
-- ███████ █████ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ███████ ███████ ███████ ███████ ██████ ██ ██ ██████ ██ ████
---Selection helpers and modifiers
local selection = {}
--- Additively adds a point to the current selection or
2021-08-03 01:16:50 +00:00
-- makes a selection from the provided point.
-- @param name string Player name.
-- @param pos vector The position to include.
2021-08-02 23:01:02 +00:00
function selection.add_point(name, pos)
if pos ~= nil then
local created_new = not worldedit.pos1[name] or not worldedit.pos2[name]
2021-08-02 23:01:02 +00:00
-- print("[set_pos1]", name, "("..pos.x..", "..pos.y..", "..pos.z..")")
2022-09-18 21:20:04 +00:00
if not worldedit.pos1[name] then worldedit.pos1[name] = Vector3.new(pos) end
if not worldedit.pos2[name] then worldedit.pos2[name] = Vector3.new(pos) end
2021-08-02 23:01:02 +00:00
worldedit.marker_update(name)
local volume_before = worldedit.volume(worldedit.pos1[name], worldedit.pos2[name])
2022-09-18 21:20:04 +00:00
worldedit.pos1[name], worldedit.pos2[name] = Vector3.expand_region(
Vector3.new(worldedit.pos1[name]),
Vector3.new(worldedit.pos2[name]),
pos
)
2021-08-02 23:01:02 +00:00
local volume_after = worldedit.volume(worldedit.pos1[name], worldedit.pos2[name])
local volume_difference = volume_after - volume_before
if volume_difference == 0 and created_new then
volume_difference = 1
end
local msg = "Expanded region by "
if created_new then msg = "Created new region with " end
msg = msg..volume_difference.." node"
if volume_difference ~= 1 then msg = msg.."s" end
2021-08-02 23:01:02 +00:00
worldedit.marker_update(name)
worldedit.player_notify(name, msg)
2021-08-02 23:01:02 +00:00
else
worldedit.player_notify(name, "Error: Too far away (try raising your maxdist with //farwand maxdist <number>)")
-- print("[set_pos1]", name, "nil")
end
end
--- Clears current selection.
2021-08-03 01:16:50 +00:00
-- @param name string Player name.
2021-08-02 23:01:02 +00:00
function selection.clear_points(name)
worldedit.pos1[name] = nil
worldedit.pos2[name] = nil
worldedit.marker_update(name)
worldedit.set_pos[name] = nil
worldedit.player_notify(name, "Region cleared")
end
2021-08-03 01:16:50 +00:00
--- Checks if a string is a valid axis.
-- @param str string String to check (be sure to remove any + or -).
-- @param hv bool Include "h" (general horizontal) and "v" (general vertical).
-- @return bool If string is a valid axis then true.
function selection.check_axis(str,hv)
if hv then
return (str == "x" or str == "y" or str == "z" or str == "h" or str == "v")
else
return (str == "x" or str == "y" or str == "z")
end
end
--- Checks if a string is a valid dir.
-- @param str string String to check (be sure to remove any + or -).
-- @return bool If string is a valid dir then true.
function selection.check_dir(str)
2022-05-21 02:29:01 +00:00
return (str == "facing" or str == "front" or str == "back" or str == "left" or str == "right" or str == "up" or str == "down")
2021-08-03 01:16:50 +00:00
end
2021-08-02 23:01:02 +00:00
return selection