]> git.lizzy.rs Git - worldedit.git/blob - worldedit/common.lua
Faster copying using vmanips
[worldedit.git] / worldedit / common.lua
1 --- Common functions [INTERNAL].  All of these functions are internal!
2 -- @module worldedit.common
3
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
12         end
13         if pos1.y > pos2.y then
14                 pos2.y, pos1.y = pos1.y, pos2.y
15         end
16         if pos1.z > pos2.z then
17                 pos2.z, pos1.z = pos1.z, pos2.z
18         end
19         return pos1, pos2
20 end
21
22
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) *
29                 (pos2.z - pos1.z + 1)
30 end
31
32
33 --- Gets other axes given an axis.
34 -- @raise Axis must be x, y, or z!
35 function worldedit.get_axis_others(axis)
36         if axis == "x" then
37                 return "y", "z"
38         elseif axis == "y" then
39                 return "x", "z"
40         elseif axis == "z" then
41                 return "x", "y"
42         else
43                 error("Axis must be x, y, or z!")
44         end
45 end
46
47
48 function worldedit.keep_loaded(pos1, pos2)
49         local manip = minetest.get_voxel_manip()
50         manip:read_from_map(pos1, pos2)
51 end
52
53
54 local mh = {}
55 worldedit.manip_helpers = mh
56
57
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.
63         local data = {}
64         local c_ignore = minetest.get_content_id("ignore")
65         for i = 1, worldedit.volume(area.MinEdge, area.MaxEdge) do
66                 data[i] = c_ignore
67         end
68         return data
69 end
70
71
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})
76         return manip, area
77 end
78
79
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)
84 end
85
86
87 function mh.init_axis_radius(base_pos, axis, radius)
88         return mh.init_axis_radius_length(base_pos, axis, radius, radius)
89 end
90
91
92 function mh.init_axis_radius_length(base_pos, axis, radius, length)
93         local other1, other2 = worldedit.get_axis_others(axis)
94         local pos1 = {
95                 [axis]   = base_pos[axis],
96                 [other1] = base_pos[other1] - radius,
97                 [other2] = base_pos[other2] - radius
98         }
99         local pos2 = {
100                 [axis]   = base_pos[axis] + length,
101                 [other1] = base_pos[other1] + radius,
102                 [other2] = base_pos[other2] + radius
103         }
104         return mh.init(pos1, pos2)
105 end
106
107
108 function mh.finish(manip, data)
109         -- Update map
110         if data ~= nil then
111                 manip:set_data(data)
112         end
113         manip:write_to_map()
114         manip:update_map()
115 end
116