azalea_world/
iterators.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Iterators for iterating over Minecraft blocks and chunks, based on
//! [prismarine-world's iterators](https://github.com/PrismarineJS/prismarine-world/blob/master/src/iterators.js).

use azalea_core::position::{BlockPos, ChunkPos};

/// An octahedron iterator, useful for iterating over blocks in a world.
///
/// ```
/// # use azalea_core::position::BlockPos;
/// # use azalea_world::iterators::BlockIterator;
///
/// let mut iter = BlockIterator::new(BlockPos::default(), 4);
/// for block_pos in iter {
///    println!("{:?}", block_pos);
/// }
/// ```
pub struct BlockIterator {
    start: BlockPos,
    max_distance: u32,

    pos: BlockPos,
    apothem: u32,
    left: i32,
    right: i32,
}
impl BlockIterator {
    pub fn new(start: BlockPos, max_distance: u32) -> Self {
        Self {
            start,
            max_distance,

            pos: BlockPos {
                x: -1,
                y: -1,
                z: -1,
            },
            apothem: 1,
            left: 1,
            right: 2,
        }
    }
}

impl Iterator for BlockIterator {
    type Item = BlockPos;

    fn next(&mut self) -> Option<Self::Item> {
        if self.apothem > self.max_distance {
            return None;
        }

        self.right -= 1;
        if self.right < 0 {
            self.left -= 1;
            if self.left < 0 {
                self.pos.z += 2;
                if self.pos.z > 1 {
                    self.pos.y += 2;
                    if self.pos.y > 1 {
                        self.pos.x += 2;
                        if self.pos.x > 1 {
                            self.apothem += 1;
                            self.pos.x = -1;
                        }
                        self.pos.y = -1;
                    }
                    self.pos.z = -1;
                }
                self.left = self.apothem as i32;
            }
            self.right = self.left;
        }
        let x = self.pos.x * self.right;
        let y = self.pos.y * ((self.apothem as i32) - self.left);
        let z = self.pos.z * ((self.apothem as i32) - (i32::abs(x) + i32::abs(y)));
        Some(BlockPos { x, y, z } + self.start)
    }
}

/// A spiral iterator, useful for iterating over chunks in a world. Use
/// `ChunkIterator` to sort by x+y+z (Manhattan) distance.
///
/// ```
/// # use azalea_core::position::ChunkPos;
/// # use azalea_world::iterators::SquareChunkIterator;
///
/// let mut iter = SquareChunkIterator::new(ChunkPos::default(), 4);
/// for chunk_pos in iter {
///   println!("{:?}", chunk_pos);
/// }
/// ```
pub struct SquareChunkIterator {
    start: ChunkPos,
    number_of_points: u32,

    dir: ChunkPos,

    segment_len: u32,
    pos: ChunkPos,
    segment_passed: u32,
    current_iter: u32,
}
impl SquareChunkIterator {
    pub fn new(start: ChunkPos, max_distance: u32) -> Self {
        Self {
            start,
            number_of_points: u32::pow(max_distance * 2 - 1, 2),

            dir: ChunkPos { x: 1, z: 0 },

            segment_len: 1,
            pos: ChunkPos::default(),
            segment_passed: 0,
            current_iter: 0,
        }
    }

    /// Change the distance that this iterator won't go past.
    ///
    /// ```
    /// # use azalea_core::position::ChunkPos;
    /// # use azalea_world::iterators::SquareChunkIterator;
    ///
    /// let mut iter = SquareChunkIterator::new(ChunkPos::default(), 2);
    /// while let Some(chunk_pos) = iter.next() {
    ///   println!("{:?}", chunk_pos);
    /// }
    /// iter.set_max_distance(4);
    /// while let Some(chunk_pos) = iter.next() {
    ///   println!("{:?}", chunk_pos);
    /// }
    /// ```
    pub fn set_max_distance(&mut self, max_distance: u32) {
        self.number_of_points = u32::pow(max_distance * 2 - 1, 2);
    }
}
impl Iterator for SquareChunkIterator {
    type Item = ChunkPos;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_iter > self.number_of_points {
            return None;
        }

        let output = self.start + self.dir;

        // make a step, add the direction to the current position
        self.pos.x += self.dir.x;
        self.pos.z += self.dir.z;
        self.segment_passed += 1;

        if self.segment_passed == self.segment_len {
            // done with current segment
            self.segment_passed = 0;

            // rotate directions
            (self.dir.x, self.dir.z) = (-self.dir.z, self.dir.x);

            // increase segment length if necessary
            if self.dir.z == 0 {
                self.segment_len += 1;
            }
        }
        self.current_iter += 1;
        Some(output)
    }
}

/// A diagonal spiral iterator, useful for iterating over chunks in a world.
///
/// ```
/// # use azalea_core::position::ChunkPos;
/// # use azalea_world::iterators::ChunkIterator;
///
/// let mut iter = ChunkIterator::new(ChunkPos::default(), 4);
/// for chunk_pos in iter {
///   println!("{:?}", chunk_pos);
/// }
/// ```
pub struct ChunkIterator {
    pub max_distance: u32,
    pub start: ChunkPos,
    pub pos: ChunkPos,
    pub layer: u32,
    pub leg: i32,
}
impl ChunkIterator {
    pub fn new(start: ChunkPos, max_distance: u32) -> Self {
        Self {
            max_distance,
            start,
            pos: ChunkPos { x: 2, z: -1 },
            layer: 1,
            leg: -1,
        }
    }
}
impl Iterator for ChunkIterator {
    type Item = ChunkPos;

    fn next(&mut self) -> Option<Self::Item> {
        match self.leg {
            -1 => {
                self.leg = 0;
                return Some(self.start);
            }
            0 => {
                if self.max_distance == 1 {
                    return None;
                }
                self.pos.x -= 1;
                self.pos.z += 1;
                if self.pos.x == 0 {
                    self.leg = 1;
                }
            }
            1 => {
                self.pos.x -= 1;
                self.pos.z -= 1;
                if self.pos.z == 0 {
                    self.leg = 2;
                }
            }
            2 => {
                self.pos.x += 1;
                self.pos.z -= 1;
                if self.pos.x == 0 {
                    self.leg = 3;
                }
            }
            3 => {
                self.pos.x += 1;
                self.pos.z += 1;
                if self.pos.z == 0 {
                    self.pos.x += 1;
                    self.leg = 0;
                    self.layer += 1;
                    if self.layer == self.max_distance {
                        return None;
                    }
                }
            }
            _ => unreachable!(),
        }
        Some(self.start + self.pos)
    }
}