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
use super::*;
use crate::{
    site2::gen::place_circular,
    util::{RandomField, Sampler, CARDINALS},
    Land,
};
use common::{
    comp::Content,
    generation::SpecialEntity,
    terrain::{BlockKind, SpriteCfg, SpriteKind},
};
use rand::prelude::*;
use std::sync::Arc;
use vek::*;

/// Represents house data generated by the `generate()` method
pub struct CoastalAirshipDock {
    /// Tile position of the door tile
    pub door_tile: Vec2<i32>,
    /// Approximate altitude of the door tile
    pub(crate) alt: i32,
    base: i32,
    center: Vec2<i32>,
    size: i32,
    bldg_height: i32,
    diameter: i32,
    pub docking_positions: Vec<Vec3<i32>>,
}

impl CoastalAirshipDock {
    pub fn generate(
        land: &Land,
        _rng: &mut impl Rng,
        site: &Site,
        door_tile: Vec2<i32>,
        door_dir: Vec2<i32>,
        tile_aabr: Aabr<i32>,
    ) -> Self {
        let door_tile_pos = site.tile_center_wpos(door_tile);
        let bounds = Aabr {
            min: site.tile_wpos(tile_aabr.min),
            max: site.tile_wpos(tile_aabr.max),
        };
        let diameter = (bounds.max.x - bounds.min.x).min(bounds.max.y - bounds.min.y);
        let alt = land.get_alt_approx(site.tile_center_wpos(door_tile + door_dir)) as i32 + 2;
        let size = (diameter / 3) + 2;
        let bldg_height = 12;
        let base = alt + 1;
        let center = bounds.center();
        let mut docking_positions = vec![];
        let top_floor = base + (bldg_height * 6) - 3;
        for dir in CARDINALS {
            let docking_pos = center + dir * size;
            docking_positions.push(docking_pos.with_z(top_floor));
        }
        Self {
            door_tile: door_tile_pos,
            alt,
            base,
            center,
            size,
            bldg_height,
            diameter,
            docking_positions,
        }
    }
}

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

    #[cfg_attr(feature = "be-dyn-lib", export_name = "render_coastal_airship_dock")]
    fn render_inner(&self, _site: &Site, _land: &Land, painter: &Painter) {
        let base = self.base;
        let center = self.center;
        let white = Fill::Sampling(Arc::new(|center| {
            Some(match (RandomField::new(0).get(center)) % 37 {
                0..=8 => Block::new(BlockKind::Rock, Rgb::new(251, 251, 227)),
                9..=17 => Block::new(BlockKind::Rock, Rgb::new(245, 245, 229)),
                18..=26 => Block::new(BlockKind::Rock, Rgb::new(250, 243, 221)),
                27..=35 => Block::new(BlockKind::Rock, Rgb::new(240, 240, 230)),
                _ => Block::new(BlockKind::Rock, Rgb::new(255, 244, 193)),
            })
        }));
        let blue_broken = Fill::Sampling(Arc::new(|center| {
            Some(match (RandomField::new(0).get(center)) % 20 {
                0 => Block::new(BlockKind::Rock, Rgb::new(30, 187, 235)),
                _ => Block::new(BlockKind::Rock, Rgb::new(11, 146, 187)),
            })
        }));

        let length = self.diameter / 2;
        let width = (self.diameter / 2) - 1;
        let height = 15;
        // fence, blue gates
        painter
            .aabb(Aabb {
                min: Vec2::new(center.x - length - 6, center.y - width - 6).with_z(base - 2),
                max: Vec2::new(center.x + length + 7, center.y + width + 7).with_z(base - 1),
            })
            .fill(blue_broken.clone());

        for dir in CARDINALS {
            let frame_pos = Vec2::new(
                center.x + dir.x * (length + 5),
                center.y + dir.y * (width + 5),
            );
            painter
                .line(center.with_z(base - 1), frame_pos.with_z(base - 1), 3.0)
                .fill(blue_broken.clone());
        }
        // foundation
        painter
            .aabb(Aabb {
                min: Vec2::new(center.x - length - 6, center.y - width - 6).with_z(base - height),
                max: Vec2::new(center.x + length + 7, center.y + width + 7).with_z(base - 2),
            })
            .fill(white.clone());
        for f in 0..8 {
            painter
                .aabb(Aabb {
                    min: Vec2::new(center.x - length - 7 - f, center.y - width - 7 - f)
                        .with_z(base - 3 - f),
                    max: Vec2::new(center.x + length + 8 + f, center.y + width + 8 + f)
                        .with_z(base - 2 - f),
                })
                .fill(white.clone());
        }
        // clear yard
        painter
            .aabb(Aabb {
                min: Vec2::new(center.x - length - 5, center.y - width - 5).with_z(base - 2),
                max: Vec2::new(center.x + length + 6, center.y + width + 6).with_z(base + height),
            })
            .clear();
        // clear entries
        for dir in CARDINALS {
            let clear_pos = Vec2::new(
                center.x + dir.x * (length + 7),
                center.y + dir.y * (width + 7),
            );
            painter
                .line(center.with_z(base - 1), clear_pos.with_z(base - 1), 2.0)
                .clear();
        }

        // rooms
        let size = self.size;
        let room_offset = size / 6;
        let bldg_height = self.bldg_height;
        for r in 0..=4 {
            let bldg_size = size - (room_offset * r);
            let bldg_base = base + ((bldg_height + 2) * r);
            if r == 4 {
                painter
                    .cylinder(Aabb {
                        min: (center - bldg_size - 2).with_z(bldg_base + bldg_height - 1),
                        max: (center + bldg_size + 2).with_z(bldg_base + bldg_height),
                    })
                    .fill(white.clone());
                painter
                    .cylinder(Aabb {
                        min: (center - bldg_size - 2).with_z(bldg_base + bldg_height),
                        max: (center + bldg_size + 2).with_z(bldg_base + bldg_height + 1),
                    })
                    .fill(blue_broken.clone());
                painter
                    .cylinder(Aabb {
                        min: (center - bldg_size - 1).with_z(bldg_base + bldg_height),
                        max: (center + bldg_size + 1).with_z(bldg_base + bldg_height + 1),
                    })
                    .clear();

                let cargo_pos = Vec2::new(center.x, center.y + 5);
                for dir in CARDINALS {
                    let sprite_pos = cargo_pos + dir;
                    let rows = 1 + (RandomField::new(0).get(sprite_pos.with_z(base)) % 3) as i32;
                    for r in 0..rows {
                        painter
                            .aabb(Aabb {
                                min: (sprite_pos).with_z(bldg_base + bldg_height + r),
                                max: (sprite_pos + 1).with_z(bldg_base + bldg_height + 1 + r),
                            })
                            .fill(Fill::Block(Block::air(
                                match (RandomField::new(0).get(sprite_pos.with_z(base + r)) % 2)
                                    as i32
                                {
                                    0 => SpriteKind::Barrel,
                                    _ => SpriteKind::CrateBlock,
                                },
                            )));
                        if r > 1 {
                            painter.owned_resource_sprite(
                                sprite_pos.with_z(bldg_base + bldg_height + 1 + r),
                                SpriteKind::Crate,
                                0,
                            );
                        }
                    }

                    // docks
                    let gangway_pos = center + dir * (size / 2);
                    let dock_pos = center + dir * (size - 3);
                    painter
                        .aabb(Aabb {
                            min: (gangway_pos - 3).with_z(bldg_base + bldg_height - 1),
                            max: (gangway_pos + 3).with_z(bldg_base + bldg_height),
                        })
                        .fill(white.clone());
                    painter
                        .cylinder(Aabb {
                            min: (dock_pos - 4).with_z(bldg_base + bldg_height),
                            max: (dock_pos + 4).with_z(bldg_base + bldg_height + 1),
                        })
                        .fill(blue_broken.clone());
                    painter
                        .cylinder(Aabb {
                            min: (dock_pos - 3).with_z(bldg_base + bldg_height - 1),
                            max: (dock_pos + 3).with_z(bldg_base + bldg_height + 1),
                        })
                        .fill(white.clone());
                }
                // campfire
                let campfire_pos = center.with_z(bldg_base + bldg_height);
                painter.spawn(
                    EntityInfo::at(campfire_pos.map(|e| e as f32 + 0.5))
                        .into_special(SpecialEntity::Waypoint),
                );
            }
            painter
                .cylinder(Aabb {
                    min: (center - bldg_size).with_z(bldg_base - 2),
                    max: (center + bldg_size).with_z(bldg_base + bldg_height),
                })
                .fill(white.clone());
        }
        for r in 0..=4 {
            let bldg_size = size - (room_offset * r);
            let bldg_base = base + ((bldg_height + 2) * r);

            let step_positions = place_circular(center, (bldg_size - 1) as f32, 14);
            for (s, step_pos) in step_positions.enumerate() {
                let step_size = (size / 3) - r;

                painter
                    .cylinder(Aabb {
                        min: (step_pos - step_size).with_z(bldg_base - 2 + s as i32),
                        max: (step_pos + step_size).with_z(bldg_base + 4 + s as i32),
                    })
                    .clear();
                painter
                    .cylinder(Aabb {
                        min: (step_pos - step_size).with_z(bldg_base - 3 + s as i32),
                        max: (step_pos + step_size).with_z(bldg_base - 2 + s as i32),
                    })
                    .fill(blue_broken.clone());
                painter
                    .cylinder(Aabb {
                        min: (step_pos - step_size + 1).with_z(bldg_base - 4 + s as i32),
                        max: (step_pos + step_size - 1).with_z(bldg_base - 2 + s as i32),
                    })
                    .fill(white.clone());
            }
            let lamp_positions = place_circular(center, (bldg_size + 1) as f32, 14);
            for (l, lamp_pos) in lamp_positions.enumerate() {
                if (RandomField::new(0).get(lamp_pos.with_z(base)) % 4) < 1 {
                    painter
                        .aabb(Aabb {
                            min: (lamp_pos - 1).with_z(bldg_base - 3 + l as i32),
                            max: (lamp_pos + 1).with_z(bldg_base - 2 + l as i32),
                        })
                        .fill(blue_broken.clone());

                    painter.sprite(
                        lamp_pos.with_z(bldg_base - 2 + l as i32),
                        SpriteKind::FireBowlGround,
                    );
                }
            }
        }
        for dock_pos in &self.docking_positions {
            painter.rotated_sprite_with_cfg(
                *dock_pos,
                SpriteKind::Sign,
                Dir::from_vec2(dock_pos.xy() - self.center).sprite_ori(),
                SpriteCfg {
                    unlock: None,
                    content: Some(Content::localized("common-signs-airship_dock")),
                },
            );
        }
    }
}