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
use super::*;
use crate::Land;
use common::terrain::BlockKind;
use rand::prelude::*;
use vek::*;

/// Represents data generated by the `generate()` method
pub struct GliderFinish {
    /// Location of center
    center: Vec2<i32>,
    /// Approximate altitude
    pub(crate) alt: i32,
    /// Tile side length for checkerboard surface
    tile_width: i32,
    /// Distance from the center of the checkerboard platform to the edge.
    radius: i32,
}

impl GliderFinish {
    pub fn generate(land: &Land, _rng: &mut impl Rng, _site: &Site, wpos: Vec2<i32>) -> Self {
        Self {
            center: wpos,
            alt: land.get_alt_approx(wpos) as i32,
            tile_width: 2,
            radius: 10,
        }
    }
}

impl Structure for GliderFinish {
    #[cfg(feature = "use-dyn-lib")]
    const UPDATE_FN: &'static [u8] = b"render_glider_finish\0";

    #[cfg_attr(feature = "be-dyn-lib", export_name = "render_glider_finish")]
    fn render_inner(&self, _site: &Site, _land: &Land, painter: &Painter) {
        let red = Fill::Brick(BlockKind::Wood, Rgb::new(200, 0, 0), 24);
        let green = Fill::Brick(BlockKind::Wood, Rgb::new(0, 200, 0), 24);
        let white = Fill::Brick(BlockKind::GlowingRock, Rgb::new(200, 200, 200), 24);
        let black = Fill::Brick(BlockKind::Wood, Rgb::new(0, 0, 0), 24);
        let wood = Fill::Brick(BlockKind::Wood, Rgb::new(40, 30, 20), 24);

        let base = self.alt + 6;

        // Base
        let foundation_0 = painter.aabb(Aabb {
            min: Vec2::new(
                self.center.x - self.radius - 4,
                self.center.y - self.radius - 4,
            )
            .with_z(base - 50),
            max: Vec2::new(
                self.center.x + self.radius + 4,
                self.center.y + self.radius + 4,
            )
            .with_z(base - 2),
        });
        let foundation_1 = painter.aabb(Aabb {
            min: Vec2::new(
                self.center.x - self.radius - 3,
                self.center.y - self.radius - 3,
            )
            .with_z(base - 2),
            max: Vec2::new(
                self.center.x + self.radius + 3,
                self.center.y + self.radius + 3,
            )
            .with_z(base - 1),
        });
        let foundation_2 = painter.aabb(Aabb {
            min: Vec2::new(
                self.center.x - self.radius - 2,
                self.center.y - self.radius - 2,
            )
            .with_z(base - 1),
            max: Vec2::new(
                self.center.x + self.radius + 2,
                self.center.y + self.radius + 2,
            )
            .with_z(base),
        });
        let platform = painter.aabb(Aabb {
            min: Vec2::new(
                self.center.x - self.radius - 1,
                self.center.y - self.radius - 1,
            )
            .with_z(base),
            max: Vec2::new(
                self.center.x + self.radius + 1,
                self.center.y + self.radius + 1,
            )
            .with_z(base + 1),
        });
        let above_platform = painter.aabb(Aabb {
            min: Vec2::new(
                self.center.x - self.radius - 4,
                self.center.y - self.radius - 4,
            )
            .with_z(base - 2),
            max: Vec2::new(
                self.center.x + self.radius + 4,
                self.center.y + self.radius + 4,
            )
            .with_z(base + 30),
        });

        // Checkerboard pattern
        let mut white_fills = Vec::new();
        let mut black_fills = Vec::new();
        for i in 0..self.radius * 2 {
            for j in 0..self.radius * 2 {
                let white_tile_start = (i % 4 == 0 && j % 4 == 0) || (i % 4 == 2 && j % 4 == 2);
                let black_tile_start = (i % 4 == 0 && j % 4 == 2) || (i % 4 == 2 && j % 4 == 0);
                if white_tile_start {
                    let tile_start = Vec2::new(
                        self.center.x - self.radius + i,
                        self.center.y - self.radius + j,
                    );
                    let tile_end = tile_start + self.tile_width;
                    let white_tile = painter.aabb(Aabb {
                        min: tile_start.with_z(base),
                        max: tile_end.with_z(base + 1),
                    });
                    white_fills.push(white_tile);
                } else if black_tile_start {
                    let tile_start = Vec2::new(
                        self.center.x - self.radius + i,
                        self.center.y - self.radius + j,
                    );
                    let tile_end = tile_start + self.tile_width;
                    let black_tile = painter.aabb(Aabb {
                        min: tile_start.with_z(base),
                        max: tile_end.with_z(base + 1),
                    });
                    black_fills.push(black_tile);
                }
            }
        }

        let green_fills = [platform];
        let red_fills = [foundation_2];
        let wood_fills = [foundation_0, foundation_1];
        let clears = [above_platform];

        let fills = [
            (&clears as &[_], None),
            (&wood_fills, Some(wood)),
            (&red_fills, Some(red)),
            (&green_fills, Some(green)),
            (&black_fills, Some(black)),
            (&white_fills, Some(white)),
        ];

        for (primitives, fill_color_maybe) in fills {
            for prim in primitives {
                if let Some(fill_color) = &fill_color_maybe {
                    prim.fill(fill_color.clone());
                } else {
                    prim.clear();
                }
            }
        }
    }
}