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
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
use super::{get_num, Flow, LInstruction};
use crate::{
    executor::{Display, DisplayState, ExecutorContext},
    memory::{LAddress, LRegistry, LVar},
};
use enum_dispatch::enum_dispatch;
use fimg::Image;

pub const INSTRS: &[&str] = &[
    "clear", "color", "col", "stroke", "line", "rect", "lineRect", "triangle",
];

#[enum_dispatch]
pub trait DrawInstruction<'v> {
    fn draw(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    );
}

#[derive(Debug)]
#[enum_dispatch(DrawInstruction)]
pub enum DrawInstr<'v> {
    DrawLine(Line<'v>),
    DrawRectBordered(RectBordered<'v>),
    DrawRectFilled(RectFilled<'v>),
    DrawTriangle(Triangle<'v>),
    Clear(Clear<'v>),
    SetColorDyn(SetColorDyn<'v>),
    SetColorConst(SetColorConst),
    SetStroke(SetStroke<'v>),
}

#[derive(Debug)]
pub struct Clear<'v> {
    pub r: LAddress<'v>,
    pub g: LAddress<'v>,
    pub b: LAddress<'v>,
    pub a: LAddress<'v>,
}

impl<'v> DrawInstruction<'v> for Clear<'v> {
    fn draw(&self, mem: &mut LRegistry<'v>, image: &mut Image<&mut [u8], 4>, _: &mut DisplayState) {
        macro_rules! u8 {
            ($v:ident) => {
                match mem.get(&self.$v) {
                    LVar::Num(n) => n.round() as u8,
                    _ => return,
                }
            };
        }
        let (r, g, b, a) = (u8!(r), u8!(g), u8!(b), u8!(a));
        for [r2, g2, b2, a2] in image.chunked_mut() {
            (*r2, *b2, *g2, *a2) = (r, g, b, a);
        }
    }
}

#[derive(Debug)]
pub struct SetColorDyn<'v> {
    pub r: LAddress<'v>,
    pub g: LAddress<'v>,
    pub b: LAddress<'v>,
    pub a: LAddress<'v>,
}
impl<'v> DrawInstruction<'v> for SetColorDyn<'v> {
    fn draw(&self, mem: &mut LRegistry<'v>, _: &mut Image<&mut [u8], 4>, state: &mut DisplayState) {
        macro_rules! u8 {
            ($v:ident) => {
                match mem.get(&self.$v) {
                    LVar::Num(n) => n.round() as u8,
                    _ => return,
                }
            };
        }
        state.color = (u8!(r), u8!(g), u8!(b), u8!(a));
    }
}

#[derive(Debug)]
pub struct SetColorConst {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}
impl DrawInstruction<'_> for SetColorConst {
    fn draw(&self, _: &mut LRegistry<'_>, _: &mut Image<&mut [u8], 4>, state: &mut DisplayState) {
        state.color = (self.r, self.g, self.b, self.a);
    }
}

#[derive(Debug)]
pub struct SetStroke<'v> {
    pub size: LAddress<'v>,
}
impl<'v> DrawInstruction<'v> for SetStroke<'v> {
    fn draw(&self, mem: &mut LRegistry<'v>, _: &mut Image<&mut [u8], 4>, state: &mut DisplayState) {
        if let &LVar::Num(n) = mem.get(&self.size) {
            state.stroke = n;
        }
    }
}

pub type Point<'v> = (LAddress<'v>, LAddress<'v>);
#[rustfmt::skip]
macro_rules! point {
    ($mem:ident@$point:expr) => {{
        let &LVar::Num(a) = $mem.get(&$point.0) else { return; };
        let &LVar::Num(b) = $mem.get(&$point.1) else { return; };
        (a,b)
    }}
}

macro_rules! map {
    ($tup:expr, $fn:expr) => {{
        let (a, b) = $tup;
        ($fn(a), $fn(b))
    }};
}
#[derive(Debug)]
pub struct Line<'v> {
    pub point_a: Point<'v>,
    pub point_b: Point<'v>,
}
impl<'v> DrawInstruction<'v> for Line<'v> {
    #[allow(unused_variables)]
    fn draw(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    ) {
        // i will happily ignore that stroke specifys the stroke of lines
        let a = map!(point!([email protected]_a), |n| n as i32);
        let b = map!(point!([email protected]_b), |n| n as i32);
        image.line(a, b, state.col());
    }
}

macro_rules! unbounded {
    ($img:ident @ $x:expr => $y:expr) => {
        $img.width() <= $x || $img.height() <= $y
    };
}

#[derive(Debug)]
pub struct RectFilled<'v> {
    pub position: Point<'v>,
    pub width: LAddress<'v>,
    pub height: LAddress<'v>,
}
impl<'v> DrawInstruction<'v> for RectFilled<'v> {
    fn draw(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    ) {
        let pos = map!(point!([email protected]), |n| n as u32);
        let width = get_num!(mem.get(&self.width)) as u32;
        let height = get_num!(mem.get(&self.height)) as u32;
        if unbounded!(image @ pos.0 + width => pos.1 + height) {
            return;
        }
        // SAFETY: bounds checked above
        unsafe { image.filled_box(pos, width, height, state.col()) };
    }
}

#[derive(Debug)]
pub struct RectBordered<'v> {
    pub position: Point<'v>,
    pub width: LAddress<'v>,
    pub height: LAddress<'v>,
}

impl<'v> DrawInstruction<'v> for RectBordered<'v> {
    fn draw(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    ) {
        // happily ignoring that state specifies box stroke width
        let pos = map!(point!([email protected]), |n| n as u32);
        let width = get_num!(mem.get(&self.width)) as u32;
        let height = get_num!(mem.get(&self.height)) as u32;
        if unbounded!(image @ pos.0 + width => pos.1 + height) {
            return;
        }
        // SAFETY: bounds checked above
        unsafe { image.r#box(pos, width, height, state.col()) };
    }
}

#[derive(Debug)]
pub struct Triangle<'v> {
    pub points: (Point<'v>, Point<'v>, Point<'v>),
}
impl<'v> DrawInstruction<'v> for Triangle<'v> {
    fn draw(&self, mem: &mut LRegistry<'v>, i: &mut Image<&mut [u8], 4>, state: &mut DisplayState) {
        let to32 = |n| n as f32;
        let (a, b, c) = (
            map!(point!([email protected]), to32),
            map!(point!([email protected]), to32),
            map!(point!([email protected]), to32),
        );
        if unbounded!(i @ a.0 as u32 => a.1 as u32)
            || unbounded!(i @ b.0 as u32 => b.1 as u32)
            || unbounded!(i @ c.0 as u32 => c.1 as u32)
        {
            return;
        }
        // SAFETY: bounds are checked
        unsafe { i.tri(a, b, c, state.col()) };
    }
}

#[derive(Debug, Default)]
pub struct Flush {
    pub(crate) display: Display,
}
impl LInstruction<'_> for Flush {
    fn run<W: std::io::Write>(&self, exec: &mut ExecutorContext<'_, W>) -> Flow {
        exec.flush(self.display);
        Flow::Continue
    }
}