1 --- Common functions [INTERNAL]. All of these functions are internal!
2 -- @module worldedit.common
4 --- Copies and modifies positions `pos1` and `pos2` so that each component of
5 -- `pos1` is less than or equal to the corresponding component of `pos2`.
6 -- Returns the new positions.
7 function worldedit.sort_pos(pos1, pos2)
8 pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
9 pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
10 if pos1.x > pos2.x then
11 pos2.x, pos1.x = pos1.x, pos2.x
13 if pos1.y > pos2.y then
14 pos2.y, pos1.y = pos1.y, pos2.y
16 if pos1.z > pos2.z then
17 pos2.z, pos1.z = pos1.z, pos2.z
23 --- Determines the volume of the region defined by positions `pos1` and `pos2`.
24 -- @return The volume.
25 function worldedit.volume(pos1, pos2)
26 local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
27 return (pos2.x - pos1.x + 1) *
28 (pos2.y - pos1.y + 1) *
33 --- Gets other axes given an axis.
34 -- @raise Axis must be x, y, or z!
35 function worldedit.get_axis_others(axis)
38 elseif axis == "y" then
40 elseif axis == "z" then
43 error("Axis must be x, y, or z!")
48 function worldedit.keep_loaded(pos1, pos2)
49 local manip = minetest.get_voxel_manip()
50 manip:read_from_map(pos1, pos2)
55 worldedit.manip_helpers = mh
58 --- Generates an empty VoxelManip data table for an area.
59 -- @return The empty data table.
60 function mh.get_empty_data(area)
61 -- Fill emerged area with ignore so that blocks in the area that are
62 -- only partially modified aren't overwriten.
64 local c_ignore = minetest.get_content_id("ignore")
65 for i = 1, worldedit.volume(area.MinEdge, area.MaxEdge) do
72 function mh.init(pos1, pos2)
73 local manip = minetest.get_voxel_manip()
74 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
75 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
80 function mh.init_radius(pos, radius)
81 local pos1 = vector.subtract(pos, radius)
82 local pos2 = vector.add(pos, radius)
83 return mh.init(pos1, pos2)
87 function mh.init_axis_radius(base_pos, axis, radius)
88 return mh.init_axis_radius_length(base_pos, axis, radius, radius)
92 function mh.init_axis_radius_length(base_pos, axis, radius, length)
93 local other1, other2 = worldedit.get_axis_others(axis)
95 [axis] = base_pos[axis],
96 [other1] = base_pos[other1] - radius,
97 [other2] = base_pos[other2] - radius
100 [axis] = base_pos[axis] + length,
101 [other1] = base_pos[other1] + radius,
102 [other2] = base_pos[other2] + radius
104 return mh.init(pos1, pos2)
108 function mh.finish(manip, data)