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
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
use beef::lean::Cow;

use crate::debug::{info::VarData, printable::Printable};
#[derive(Clone, Debug)]
pub enum LVar<'string> {
    Num(f64),
    String(Cow<'string, str>),
}

impl PartialEq for LVar<'_> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Num(a), Self::Num(b)) => (a - b).abs() < 0.000_001,
            (Self::String(l0), Self::String(r0)) => l0 == r0,
            _ => false,
        }
    }
}

impl Default for LVar<'static> {
    fn default() -> Self {
        Self::Num(0.0)
    }
}

impl LVar<'_> {
    // get null
    pub const fn null() -> LVar<'static> {
        LVar::Num(0.0)
    }

    pub fn num(&self) -> Option<f64> {
        match *self {
            Self::Num(n) => Some(n),
            Self::String(_) => None,
        }
    }
}

#[derive(Clone, Copy)]
pub struct LAddress {
    pub address: u8,
}

impl LAddress {
    pub(crate) const fn addr(address: u8) -> Self {
        LAddress { address }
    }
}

#[derive(Copy, Clone)]
pub struct Priv {
    _priv: (),
}

impl std::fmt::Debug for LAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:x}", self.address)
    }
}

impl std::fmt::Display for LVar<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Num(n) => write!(f, "{}", crate::debug::ff(*n)), // yeeeeahhhh
            Self::String(s) => write!(f, r#""{s}""#),
        }
    }
}

impl From<f64> for LVar<'_> {
    fn from(value: f64) -> Self {
        Self::Num(value)
    }
}

impl From<bool> for LVar<'_> {
    fn from(value: bool) -> Self {
        Self::Num(value.into())
    }
}

impl From<usize> for LVar<'_> {
    fn from(value: usize) -> Self {
        Self::Num(value as f64)
    }
}

impl<'s> From<&'s str> for LVar<'s> {
    fn from(value: &'s str) -> Self {
        Self::String(value.into())
    }
}

impl<'s> From<Cow<'s, str>> for LVar<'s> {
    fn from(value: Cow<'s, str>) -> Self {
        Self::String(value)
    }
}

/// cleared every loop
#[derive(Debug)]
pub struct LRegistry<'str>(pub [LVar<'str>; 255]);

impl<'s> std::ops::Index<LAddress> for LRegistry<'s> {
    type Output = LVar<'s>;

    fn index(&self, index: LAddress) -> &Self::Output {
        &self.0[index.address as usize]
    }
}

impl<'s> std::ops::IndexMut<LAddress> for LRegistry<'s> {
    fn index_mut(&mut self, index: LAddress) -> &mut Self::Output {
        &mut self.0[index.address as usize]
    }
}

impl<'s> Default for LRegistry<'s> {
    fn default() -> Self {
        Self([const { LVar::null() }; 255])
    }
}

impl std::fmt::Display for LRegistry<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "R[")?;
        let mut iter = self
            .0
            .iter()
            .enumerate()
            .filter(|&(_, v)| v != &LVar::null());
        if let Some((i, v)) = iter.next() {
            write!(f, "{i}: {v}")?;
        }
        for (i, v) in iter {
            write!(f, ", {i}: {v}")?;
        }
        write!(f, "]")
    }
}

impl LRegistry<'_> {
    pub fn get(&self, a: LAddress) -> &LVar {
        &self[a]
    }
}

impl Printable for LRegistry<'_> {
    fn print(
        &self,
        info: &crate::debug::info::DebugInfo<'_>,
        f: &mut impl std::fmt::Write,
    ) -> std::fmt::Result {
        write!(f, "R[")?;
        let mut iter = self
            .0
            .iter()
            .zip(0..u8::MAX)
            .filter(|&(v, _)| v != &LVar::null())
            .map(|(v, i)| (&info[LAddress::addr(i)], v))
            .filter_map(|(d, v)| match d {
                VarData::Variable(d) => Some((*d, v)),
                VarData::Constant(_) => None,
            });
        if let Some((i, v)) = iter.next() {
            write!(f, "{i}: {v}")?;
        }
        for (i, v) in iter {
            write!(f, ", {i}: {v}")?;
        }
        write!(f, "]")
    }
}