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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use super::{get_num, Flow, LInstruction};
use crate::{
    executor::{Display, DisplayState, ExecutorContext},
    memory::{LAddress, LRegistry, LVar},
};
use enum_dispatch::enum_dispatch;
use fimg::Image;
use std::fmt::{self, Display as Disp, Formatter};

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

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

#[derive(Debug, Copy, Clone)]
#[enum_dispatch(DrawInstruction)]
pub enum DrawInstr {
    Line(Line),
    RectBordered(RectBordered),
    RectFilled(RectFilled),
    Triangle(Triangle),
    Clear(Clear),
    SetColor(SetColor),
    SetStroke(SetStroke),
    Poly(Poly),
    LinePoly(LinePoly),
}

impl Disp for DrawInstr {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Line(i) => write!(f, "{i}"),
            Self::RectBordered(i) => write!(f, "{i}"),
            Self::RectFilled(i) => write!(f, "{i}"),
            Self::Triangle(i) => write!(f, "{i}"),
            Self::Clear(i) => write!(f, "{i}"),
            Self::SetColor(i) => write!(f, "{i}"),
            Self::SetStroke(i) => write!(f, "{i}"),
            Self::Poly(i) => write!(f, "{i}"),
            Self::LinePoly(i) => write!(f, "{i}"),
        }
    }
}

#[derive(Debug, Copy, Clone)]

pub struct Clear {
    pub r: LAddress,
    pub g: LAddress,
    pub b: LAddress,
}

impl DrawInstruction for Clear {
    fn draw<'v>(
        &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) = (u8!(r), u8!(g), u8!(b));
        for [r2, g2, b2, a2] in image.chunked_mut() {
            (*r2, *b2, *g2, *a2) = (r, g, b, 255);
        }
    }
}

impl Disp for Clear {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "draw clear {} {} {}", self.r, self.g, self.b)
    }
}

#[derive(Debug, Copy, Clone)]

pub struct SetColor {
    pub r: LAddress,
    pub g: LAddress,
    pub b: LAddress,
    pub a: LAddress,
}
impl DrawInstruction for SetColor {
    fn draw<'v>(
        &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));
    }
}

impl Disp for SetColor {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "draw color {} {} {} {}", self.r, self.g, self.b, self.a)
    }
}

#[derive(Debug, Copy, Clone)]

pub struct SetStroke {
    pub size: LAddress,
}
impl DrawInstruction for SetStroke {
    fn draw<'v>(
        &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;
        }
    }
}

impl Disp for SetStroke {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "draw stroke {}", self.size)
    }
}

pub type Point = (LAddress, LAddress);
#[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, Copy, Clone)]

pub struct Line {
    pub point_a: Point,
    pub point_b: Point,
}

impl DrawInstruction for Line {
    fn draw<'v>(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    ) {
        let a = map!(point!([email protected]_a), |n| n as f32);
        let b = map!(point!([email protected]_b), |n| n as f32);
        image.thick_line(a, b, state.stroke as f32, state.col());
    }
}

impl Disp for Line {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "draw line {} {} {} {}",
            self.point_a.0, self.point_a.1, self.point_b.0, self.point_b.1
        )
    }
}

#[derive(Debug, Copy, Clone)]

pub struct RectFilled {
    pub position: Point,
    pub width: LAddress,
    pub height: LAddress,
}

impl DrawInstruction for RectFilled {
    fn draw<'v>(
        &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;
        image.filled_box(pos, width, height, state.col());
    }
}

impl Disp for RectFilled {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "draw rect {} {} {} {}",
            self.position.0, self.position.1, self.width, self.height
        )
    }
}

#[derive(Debug, Copy, Clone)]

pub struct RectBordered {
    pub position: Point,
    pub width: LAddress,
    pub height: LAddress,
}

impl Disp for RectBordered {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "draw lineRect {} {} {} {}",
            self.position.0, self.position.1, self.width, self.height
        )
    }
}

impl DrawInstruction for RectBordered {
    fn draw<'v>(
        &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;
        image.stroked_box(pos, width, height, state.stroke.round() as u32, state.col());
    }
}

#[derive(Debug, Copy, Clone)]

pub struct Triangle {
    pub points: (Point, Point, Point),
}
impl DrawInstruction for Triangle {
    fn draw<'v>(
        &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),
        );
        i.tri::<f32>(a, b, c, state.col());
    }
}
impl Disp for Triangle {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "draw triangle {} {} {} {} {} {}",
            self.points.0.0,
            self.points.0.1,
            self.points.1.0,
            self.points.1.1,
            self.points.2.0,
            self.points.2.1
        )
    }
}

#[derive(Debug, Copy, Clone)]

pub struct Poly {
    pub(crate) pos: Point,
    pub(crate) sides: LAddress,
    pub(crate) radius: LAddress,
    pub(crate) rot: LAddress,
}

impl DrawInstruction for Poly {
    fn draw<'v>(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    ) {
        let sides = get_num!(mem.get(self.sides)).round() as usize;
        if sides < 90 {
            image.poly(
                map!(point!([email protected]), |n| n as f32),
                sides,
                get_num!(mem.get(self.radius)) as f32,
                get_num!(mem.get(self.rot)) as f32,
                state.col(),
            );
        } else {
            image.circle(
                map!(point!([email protected]), |n: f64| n.round() as i32),
                get_num!(mem.get(self.radius)).round() as i32,
                state.col(),
            );
        }
    }
}

impl Disp for Poly {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "draw poly {} {} {} {} {}",
            self.pos.0, self.pos.1, self.sides, self.radius, self.rot
        )
    }
}

#[derive(Debug, Copy, Clone)]

pub struct LinePoly {
    pub(crate) pos: Point,
    pub(crate) sides: LAddress,
    pub(crate) radius: LAddress,
    pub(crate) rot: LAddress,
}

impl DrawInstruction for LinePoly {
    fn draw<'v>(
        &self,
        mem: &mut LRegistry<'v>,
        image: &mut Image<&mut [u8], 4>,
        state: &mut DisplayState,
    ) {
        let sides = get_num!(mem.get(self.sides)).round() as usize;
        if sides < 90 {
            image.border_poly(
                map!(point!([email protected]), |n| n as f32),
                sides,
                get_num!(mem.get(self.radius)) as f32,
                get_num!(mem.get(self.rot)) as f32,
                state.stroke as f32,
                state.col(),
            );
        } else {
            image.border_circle(
                map!(point!([email protected]), |n: f64| n.round() as i32),
                get_num!(mem.get(self.radius)).round() as i32,
                state.col(),
            );
        }
    }
}

impl Disp for LinePoly {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "draw linePoly {} {} {} {} {}",
            self.pos.0, self.pos.1, self.sides, self.radius, self.rot
        )
    }
}

#[derive(Debug, Copy, Clone, 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
    }
}

impl Disp for Flush {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let d = self.display;
        write!(f, "drawflush {d}")
    }
}