azalea_core/
cursor3d.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
use crate::position::BlockPos;

pub struct Cursor3d {
    index: usize,

    origin: BlockPos,

    width: usize,
    height: usize,
    depth: usize,

    end: usize,
}

impl Cursor3d {
    pub fn origin(&self) -> BlockPos {
        self.origin
    }
}

impl Iterator for Cursor3d {
    type Item = CursorIteration;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index == self.end {
            return None;
        }
        let x = self.index % self.width;
        let r = self.index / self.width;
        let y = r % self.height;
        let z = r / self.height;
        self.index += 1;

        let mut iteration_type = 0;
        if x == 0 || x == self.width - 1 {
            iteration_type += 1;
        }
        if y == 0 || y == self.height - 1 {
            iteration_type += 1;
        }
        if z == 0 || z == self.depth - 1 {
            iteration_type += 1;
        }

        Some(CursorIteration {
            pos: BlockPos {
                x: self.origin.x + x as i32,
                y: self.origin.y + y as i32,
                z: self.origin.z + z as i32,
            },
            iteration_type: iteration_type.into(),
        })
    }
}

#[repr(u8)]
#[derive(Eq, PartialEq, Debug)]
pub enum CursorIterationType {
    Inside = 0,
    Face = 1,
    Edge = 2,
    Corner = 3,
}

pub struct CursorIteration {
    pub pos: BlockPos,
    pub iteration_type: CursorIterationType,
}

impl Cursor3d {
    pub fn new(origin: BlockPos, end: BlockPos) -> Self {
        let width = (end.x - origin.x + 1)
            .try_into()
            .expect("Impossible width.");
        let height = (end.y - origin.y + 1)
            .try_into()
            .expect("Impossible height.");
        let depth = (end.z - origin.z + 1)
            .try_into()
            .expect("Impossible depth.");

        Self {
            index: 0,

            origin,

            width,
            height,
            depth,

            end: width * height * depth,
        }
    }
}

impl From<u8> for CursorIterationType {
    fn from(value: u8) -> Self {
        match value {
            0 => CursorIterationType::Inside,
            1 => CursorIterationType::Face,
            2 => CursorIterationType::Edge,
            3 => CursorIterationType::Corner,
            _ => panic!("Invalid iteration type"),
        }
    }
}