mindustry logic execution, map- and schematic- parsing and rendering
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
use std::{
    fmt::{Debug, Write},
    ops::Deref,
};
#[derive(Clone, PartialEq, Eq)]
pub struct Array2D<T: Clone> {
    width: usize,
    height: usize,
    /// column
    data: Box<[T]>,
}

impl<T: Debug + Clone> Debug for Array2D<Option<T>> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Arr[\n")?;
        for y in (0..self.height).rev() {
            for x in 0..self.width {
                let t = &self[x][y];
                if let Some(t) = t {
                    t.fmt(f)?;
                } else {
                    f.write_char('_')?;
                }
                f.write_str(", ")?;
            }
            f.write_char('\n')?;
        }
        f.write_char(']')?;
        Ok(())
    }
}

impl<T: Clone> Array2D<T> {
    pub fn new(fill: T, width: usize, height: usize) -> Array2D<T> {
        Array2D {
            width,
            height,
            data: vec![fill; width * height].into_boxed_slice(),
        }
    }
}

impl<T: Clone> Deref for Array2D<T> {
    type Target = Box<[T]>;
    /// a sin it is
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<T: Clone> std::ops::Index<usize> for Array2D<T> {
    type Output = [T];

    fn index(&self, x: usize) -> &Self::Output {
        &self.data[self.height * x..self.height * (x + 1)]
    }
}

impl<T: Clone> std::ops::IndexMut<usize> for Array2D<T> {
    fn index_mut(&mut self, x: usize) -> &mut Self::Output {
        &mut self.data[self.height * x..self.height * (x + 1)]
    }
}