• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

BuckarooBanzay / mapblock_lib / 8748698230

19 Apr 2024 05:08AM UTC coverage: 19.22% (-35.6%) from 54.827%
8748698230

Pull #3

github

BuckarooBanzay
mtt action
Pull Request #3: mtt action

202 of 1051 relevant lines covered (19.22%)

16.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.36
/util.lua
1
---------
2
-- utilities and helpers
3

4
------
5
-- Mapblock position
6
-- @number x mapblock x-position
7
-- @number y mapblock y-position
8
-- @number z mapblock z-position
9
-- @table mapblock_pos
10

11
------
12
-- Node position
13
-- @number x node x-position
14
-- @number y node y-position
15
-- @number z node z-position
16
-- @table node_pos
17

18
--- returns the mapblock-center of the given coordinates
19
-- @param pos @{node_pos} the node-position
20
-- @return @{node_pos} the node-position of the current mapblock-center with fractions
21
function mapblock_lib.get_mapblock_center(pos)
2✔
22
        local mapblock = vector.floor( vector.divide(pos, 16))
×
23
        return vector.add(vector.multiply(mapblock, 16), 7.5)
×
24
end
25

26
--- returns the mapblock position for the node-position
27
-- @param pos @{node_pos} the node-position
28
-- @return @{mapblock_pos} the mapblock-position
29
function mapblock_lib.get_mapblock(pos)
2✔
30
        return vector.floor( vector.divide(pos, 16))
×
31
end
32

33
--- returns true if the two positions align to a single mapblock's edges
34
-- @param pos1 @{node_pos} the first node-position
35
-- @param pos2 @{node_pos} the second node-position
36
function mapblock_lib.is_mapblock_aligned(pos1, pos2)
2✔
37
        pos1, pos2 = mapblock_lib.sort_pos(pos1, pos2)
×
38
        local mapblock_pos1 = mapblock_lib.get_mapblock(pos1)
×
39
        local mapblock_pos2 = mapblock_lib.get_mapblock(pos2)
×
40
        if not vector.equals(mapblock_pos1, mapblock_pos2) then
×
41
                -- not even in the same mapblock
42
                return false
×
43
        end
44
        local min, max = mapblock_lib.get_mapblock_bounds_from_mapblock(mapblock_pos1)
×
45
        return vector.equals(min, pos1) and vector.equals(max, pos2)
×
46
end
47

48
--- returns the max/min bounds for the mapblock-position
49
-- @param block_pos @{mapblock_pos} the mapblock-position
50
-- @return @{node_pos} the min-node-position
51
-- @return @{node_pos} the max-node-position
52
function mapblock_lib.get_mapblock_bounds_from_mapblock(block_pos)
2✔
53
        local min = vector.multiply(block_pos, 16)
×
54
        local max = vector.add(min, 15)
×
55
        return min, max
×
56
end
57

58
--- returns the max/min bounds for the node-position
59
-- @param pos @{node_pos} the node-position
60
-- @return @{node_pos} the min-node-position
61
-- @return @{node_pos} the max-node-position
62
function mapblock_lib.get_mapblock_bounds(pos)
2✔
63
        local mapblock = vector.floor( vector.divide(pos, 16))
×
64
        return mapblock_lib.get_mapblock_bounds_from_mapblock(mapblock)
×
65
end
66

67
--- sorts the position by ascending order
68
-- @param pos1 @{node_pos} the first position
69
-- @param pos2 @{node_pos} the second position
70
-- @return @{node_pos} the lower position
71
-- @return @{node_pos} the upper position
72
function mapblock_lib.sort_pos(pos1, pos2)
2✔
73
        pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
×
74
        pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
×
75
        if pos1.x > pos2.x then
×
76
                pos2.x, pos1.x = pos1.x, pos2.x
×
77
        end
78
        if pos1.y > pos2.y then
×
79
                pos2.y, pos1.y = pos1.y, pos2.y
×
80
        end
81
        if pos1.z > pos2.z then
×
82
                pos2.z, pos1.z = pos1.z, pos2.z
×
83
        end
84
        return pos1, pos2
×
85
end
86

87
--- returns an iterator function for the mapblock coordinate range
88
-- @param pos1 @{mapblock_pos} the lower position
89
-- @param pos2 @{mapblock_pos} the upper position
90
-- @return a position iterator
91
-- @return the total node/mapblock count
92
function mapblock_lib.pos_iterator(pos1, pos2)
2✔
93
        local total_count = ((pos2.x - pos1.x) + 1) * ((pos2.y - pos1.y) + 1) * ((pos2.z - pos1.z) + 1)
×
94
        local pos
95
        return function()
96
                if not pos then
×
97
                        -- init, copy values
98
                        pos = { x=pos1.x, y=pos1.y, z=pos1.z }
×
99
                else
100
                        -- shift x
101
                        pos.x = pos.x + 1
×
102
                        if pos.x > pos2.x then
×
103
                                -- shift z
104
                                pos.x = pos1.x
×
105
                                pos.z = pos.z + 1
×
106
                                if pos.z > pos2.z then
×
107
                                        --shift y
108
                                        pos.z = pos1.z
×
109
                                        pos.y = pos.y + 1
×
110
                                        if pos.y > pos2.y then
×
111
                                                -- done
112
                                                pos = nil
×
113
                                        end
114
                                end
115
                        end
116
                end
117

118
                return pos
×
119
        end, total_count
×
120
end
121

122
--- iterate through the specified region
123
-- @param pos1 @{mapblock_pos} the lower position
124
-- @param pos2 @{mapblock_pos} the upper position
125
-- @param fn the position callback
126
function mapblock_lib.for_each(pos1, pos2, fn)
2✔
127
        local it = mapblock_lib.pos_iterator(pos1, pos2)
×
128
        while true do
129
                local pos = it()
×
130
                if not pos then
×
131
                        break
132
                end
133
                fn(pos)
×
134
        end
135
end
136

137
-- pre-generate air-only mapblock
138
local air_content_id = minetest.get_content_id("air")
1✔
139
local air_mapblock_nodeids = {}
1✔
140
local air_mapblock_param1 = {}
1✔
141
local air_mapblock_param2 = {}
1✔
142
for i=1,4096 do
4,097✔
143
        air_mapblock_nodeids[i] = air_content_id
4,096✔
144
        air_mapblock_param1[i] = minetest.LIGHT_MAX
4,096✔
145
        air_mapblock_param2[i] = 0
4,096✔
146
end
147

148
--- clears a mapblock (fills it with air)
149
-- @param mapblock_pos the mapblock position
150
function mapblock_lib.clear_mapblock(mapblock_pos)
2✔
151
        local min, max = mapblock_lib.get_mapblock_bounds_from_mapblock(mapblock_pos)
×
152
        local manip = minetest.get_voxel_manip()
×
153
        manip:read_from_map(min, max)
×
154

155
        manip:set_data(air_mapblock_nodeids)
×
156
        manip:set_light_data(air_mapblock_param1)
×
157
        manip:set_param2_data(air_mapblock_param2)
×
158
        manip:write_to_map()
×
159

160
        if minetest.get_objects_in_area then
×
161
                -- remove entities
162
                local objects = minetest.get_objects_in_area(min, max)
×
163
                for _, obj in pairs(objects) do
×
164
                        if not obj:is_player() then
×
165
                                obj:remove()
×
166
                        end
167
                end
168
        end
169
end
170

171
function mapblock_lib.flip_pos(rel_pos, max, axis)
2✔
172
        rel_pos[axis] = max[axis] - rel_pos[axis]
×
173
end
174

175
function mapblock_lib.transpose_pos(rel_pos, axis1, axis2)
2✔
176
        rel_pos[axis1], rel_pos[axis2] = rel_pos[axis2], rel_pos[axis1]
×
177
end
178

179
--- rotate a position around the y axis
180
-- @param rel_pos @{node_pos} the relative position to rotate
181
-- @param max @{node_pos} the maximum position to rotate in
182
-- @param rotation_y the clock-wise rotation, either 0,90,180 or 270
183
-- @return @{node_pos} the rotated position
184
function mapblock_lib.rotate_pos(rel_pos, max_pos, rotation_y)
2✔
185
        local new_pos = {x=rel_pos.x, y=rel_pos.y, z=rel_pos.z}
×
186
        if rotation_y == 90 then
×
187
                mapblock_lib.flip_pos(new_pos, max_pos, "x")
×
188
                mapblock_lib.transpose_pos(new_pos, "x", "z")
×
189
        elseif rotation_y == 180 then
×
190
                mapblock_lib.flip_pos(new_pos, max_pos, "x")
×
191
                mapblock_lib.flip_pos(new_pos, max_pos, "z")
×
192
        elseif rotation_y == 270 then
×
193
                mapblock_lib.flip_pos(new_pos, max_pos, "z")
×
194
                mapblock_lib.transpose_pos(new_pos, "x", "z")
×
195
        end
196
        return new_pos
×
197
end
198

199
--- rotate a size vector
200
-- @param size @{node_pos} a size vector
201
-- @param rotation_y the clock-wise rotation, either 0,90,180 or 270
202
-- @return @{node_pos} the rotated size
203
function mapblock_lib.rotate_size(size, rotation_y)
2✔
204
        local new_size = {x=size.x, y=size.y, z=size.z}
×
205
        if rotation_y == 90 or rotation_y == 270 then
×
206
                -- swap x and z axes
207
                mapblock_lib.transpose_pos(new_size, "x", "z")
×
208
        end
209
        return new_size
×
210
end
211

212
function mapblock_lib.compare_mapblock(mb1, mb2, strict)
2✔
213
        for i=1,4096 do
×
214
                if mb1.node_ids[i] ~= mb2.node_ids[i] then
×
215
                        return false, "node-id mismatch at index " .. i ..
×
216
                                " mb1=" .. mb1.node_ids[i] .. "(" .. minetest.get_name_from_content_id(mb1.node_ids[i]) .. ")" ..
×
217
                                " mb2=" .. mb2.node_ids[i] .. "(" .. minetest.get_name_from_content_id(mb2.node_ids[i]) .. ")"
×
218
                elseif strict and mb1.param1[i] ~= mb2.param1[i] then
×
219
                        return false, "param1 mismatch at index " .. i
×
220
                elseif mb1.param2[i] ~= mb2.param2[i] then
×
221
                        return false, "param2 mismatch at index " .. i
×
222
                end
223
        end
224
        return true
×
225
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc