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


/// This is a standard structure used to exchange block positions. Coordinates
/// are signed 32-bits integers.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct BlockPos {
    pub x: i32,
    pub y: i32,
    pub z: i32
}

impl BlockPos {

    #[inline]
    pub const fn new(x: i32, y: i32, z: i32) -> Self {
        Self { x, y, z }
    }

    #[inline]
    pub const fn nil() -> Self {
        Self::new(0, 0, 0)
    }

    #[inline]
    pub fn into_array(&self) -> [i32; 3] {
        [self.x, self.y, self.z]
    }

    #[inline]
    pub fn add(&self, dx: i32, dy: i32, dz: i32) -> Self {
        Self {
            x: self.x + dx,
            y: self.y + dy,
            z: self.z + dz,
        }
    }

    #[inline]
    pub fn relative(&self, dir: Direction, mul: i32) -> Self {
        let (dx, dy, dz) = dir.normal(mul);
        self.add(dx, dy, dz)
    }

}

impl Default for BlockPos {
    fn default() -> Self {
        Self::nil()
    }
}

macro_rules! impl_block_pos_relative_shortcut {
    ($func_name:ident, $direction:ident) => {
        impl BlockPos {
            #[inline]
            pub fn $func_name(&self, mul: i32) -> Self {
                self.relative(Direction::$direction, mul)
            }
        }
    };
}

impl_block_pos_relative_shortcut!(east, East);
impl_block_pos_relative_shortcut!(west, West);
impl_block_pos_relative_shortcut!(south, South);
impl_block_pos_relative_shortcut!(north, North);
impl_block_pos_relative_shortcut!(above, Up);
impl_block_pos_relative_shortcut!(below, Down);


/// This is a standard structure used to exchange block positions. Coordinates
/// are 64-bits floating point numbers.
#[derive(Debug, PartialEq, Clone)]
pub struct EntityPos {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl EntityPos {

    #[inline]
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    #[inline]
    pub fn nil() -> Self {
        Self::new(0.0, 0.0, 0.0)
    }

    #[inline]
    pub fn into_array(&self) -> [f64; 3] {
        [self.x, self.y, self.z]
    }

}

impl Default for EntityPos {
    fn default() -> Self {
        Self::nil()
    }
}

// Conversions //

impl From<&'_ BlockPos> for EntityPos {
    fn from(pos: &'_ BlockPos) -> Self {
        Self::new(pos.x as f64, pos.y as f64, pos.z as f64)
    }
}

impl From<&'_ EntityPos> for BlockPos {
    fn from(pos: &'_ EntityPos) -> Self {
        Self::new(pos.x.floor() as i32, pos.y.floor() as i32, pos.z.floor() as i32)
    }
}


/// Cardinal direction used in-game.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Direction {
    East,  // +X
    West,  // -X
    South, // +Z
    North, // -Z
    Up,    // +Y
    Down,  // -Y
}

impl Direction {

    pub fn opposite(self) -> Self {
        match self {
            Self::East => Self::West,
            Self::West => Self::East,
            Self::South => Self::North,
            Self::North => Self::South,
            Self::Up => Self::Down,
            Self::Down => Self::Up
        }
    }

    pub fn axis(self) -> Axis {
        match self {
            Self::East | Self::West => Axis::X,
            Self::South | Self::North => Axis::Z,
            Self::Up | Self::Down => Axis::Y,
        }
    }

    pub fn normal(self, mul: i32) -> (i32, i32, i32) {
        match self {
            Self::East => (mul, 0, 0),
            Self::West => (-mul, 0, 0),
            Self::South => (0, 0, mul),
            Self::North => (0, 0, -mul),
            Self::Up => (0, mul, 0),
            Self::Down => (0, -mul, 0)
        }
    }

}


#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Axis {
    X,
    Y,
    Z
}

impl Axis {

    pub fn directions(self) -> (Direction, Direction) {
        match self {
            Self::X => (Direction::East, Direction::West),
            Self::Y => (Direction::Up, Direction::Down),
            Self::Z => (Direction::South, Direction::North)
        }
    }

}