]> git.lizzy.rs Git - worldedit.git/blob - worldedit/common.lua
worldedit: Document inner working of worldedit.keep_loaded
[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         -- 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)
54 end
55
56
57 local mh = {}
58 worldedit.manip_helpers = mh
59
60
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.
66         local data = {}
67         local c_ignore = minetest.get_content_id("ignore")
68         for i = 1, worldedit.volume(area.MinEdge, area.MaxEdge) do
69                 data[i] = c_ignore
70         end
71         return data
72 end
73
74
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})
79         return manip, area
80 end
81
82
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)
87 end
88
89
90 function mh.init_axis_radius(base_pos, axis, radius)
91         return mh.init_axis_radius_length(base_pos, axis, radius, radius)
92 end
93
94
95 function mh.init_axis_radius_length(base_pos, axis, radius, length)
96         local other1, other2 = worldedit.get_axis_others(axis)
97         local pos1 = {
98                 [axis]   = base_pos[axis],
99                 [other1] = base_pos[other1] - radius,
100                 [other2] = base_pos[other2] - radius
101         }
102         local pos2 = {
103                 [axis]   = base_pos[axis] + length,
104                 [other1] = base_pos[other1] + radius,
105                 [other2] = base_pos[other2] + radius
106         }
107         return mh.init(pos1, pos2)
108 end
109
110
111 function mh.finish(manip, data)
112         -- Update map
113         if data ~= nil then
114                 manip:set_data(data)
115         end
116         manip:write_to_map()
117         manip:update_map()
118 end
119