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 -- Create a vmanip and read the area from map, this
50 -- causes all MapBlocks to be loaded into memory.
51 -- This doesn't actually *keep* them loaded, unlike the name implies.
52 local manip = minetest.get_voxel_manip()
53 manip:read_from_map(pos1, pos2)
58 worldedit.manip_helpers = mh
61 --- Generates an empty VoxelManip data table for an area.
62 -- @return The empty data table.
63 function mh.get_empty_data(area)
64 -- Fill emerged area with ignore so that blocks in the area that are
65 -- only partially modified aren't overwriten.
67 local c_ignore = minetest.get_content_id("ignore")
68 for i = 1, worldedit.volume(area.MinEdge, area.MaxEdge) do
75 function mh.init(pos1, pos2)
76 local manip = minetest.get_voxel_manip()
77 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
78 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
83 function mh.init_radius(pos, radius)
84 local pos1 = vector.subtract(pos, radius)
85 local pos2 = vector.add(pos, radius)
86 return mh.init(pos1, pos2)
90 function mh.init_axis_radius(base_pos, axis, radius)
91 return mh.init_axis_radius_length(base_pos, axis, radius, radius)
95 function mh.init_axis_radius_length(base_pos, axis, radius, length)
96 local other1, other2 = worldedit.get_axis_others(axis)
98 [axis] = base_pos[axis],
99 [other1] = base_pos[other1] - radius,
100 [other2] = base_pos[other2] - radius
103 [axis] = base_pos[axis] + length,
104 [other1] = base_pos[other1] + radius,
105 [other2] = base_pos[other2] + radius
107 return mh.init(pos1, pos2)
111 function mh.finish(manip, data)