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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#![allow(dead_code)]

use super::{super::skeleton::*, Archetype};
use crate::{
    site::BlockMask,
    util::{RandomField, Sampler},
    IndexRef,
};
use common::{
    calendar::{Calendar, CalendarEvent},
    make_case_elim,
    terrain::{Block, BlockKind, SpriteKind},
};
use rand::prelude::*;
use serde::Deserialize;
use vek::*;

#[derive(Deserialize)]
pub struct Colors {
    pub foundation: (u8, u8, u8),
    pub floor: (u8, u8, u8),
    pub roof: roof_color::PureCases<(u8, u8, u8)>,
    pub wall: wall_color::PureCases<(u8, u8, u8)>,
    pub support: support_color::PureCases<(u8, u8, u8)>,
}

pub struct ColorTheme {
    roof: RoofColor,
    wall: WallColor,
    support: SupportColor,
}

make_case_elim!(
    roof_color,
    #[repr(u32)]
    #[derive(Clone, Copy)]
    pub enum RoofColor {
        Roof1 = 0,
        Roof2 = 1,
        Roof3 = 2,
        Roof4 = 3,
        Roof5 = 4,
        Roof6 = 5,
        Roof7 = 6,
    }
);

make_case_elim!(
    wall_color,
    #[repr(u32)]
    #[derive(Clone, Copy)]
    pub enum WallColor {
        Wall1 = 0,
        Wall2 = 1,
        Wall3 = 2,
        Wall4 = 3,
        Wall5 = 4,
        Wall6 = 5,
        Wall7 = 6,
        Wall8 = 7,
        Wall9 = 8,
    }
);

make_case_elim!(
    support_color,
    #[repr(u32)]
    #[derive(Clone, Copy)]
    pub enum SupportColor {
        Support1 = 0,
        Support2 = 1,
        Support3 = 2,
        Support4 = 3,
    }
);

const ROOF_COLORS: [RoofColor; roof_color::NUM_VARIANTS] = [
    RoofColor::Roof1,
    RoofColor::Roof2,
    RoofColor::Roof3,
    RoofColor::Roof4,
    RoofColor::Roof4,
    RoofColor::Roof6,
    RoofColor::Roof7,
];

const WALL_COLORS: [WallColor; wall_color::NUM_VARIANTS] = [
    WallColor::Wall1,
    WallColor::Wall2,
    WallColor::Wall3,
    WallColor::Wall4,
    WallColor::Wall5,
    WallColor::Wall6,
    WallColor::Wall7,
    WallColor::Wall8,
    WallColor::Wall9,
];

const SUPPORT_COLORS: [SupportColor; support_color::NUM_VARIANTS] = [
    SupportColor::Support1,
    SupportColor::Support2,
    SupportColor::Support3,
    SupportColor::Support4,
];

pub struct House {
    pub colors: ColorTheme,
    pub noise: RandomField,
    pub roof_ribbing: bool,
    pub roof_ribbing_diagonal: bool,
    pub christmas_decorations: bool,
}

#[derive(Copy, Clone)]
pub enum Pillar {
    None,
    Chimney(i32),
    Tower(i32),
}

#[derive(Copy, Clone)]
pub enum RoofStyle {
    Hip,
    Gable,
    Rounded,
}

#[derive(Copy, Clone)]
pub enum StoreyFill {
    None,
    Upper,
    All,
}

impl StoreyFill {
    fn has_lower(&self) -> bool { matches!(self, StoreyFill::All) }

    fn has_upper(&self) -> bool { !matches!(self, StoreyFill::None) }
}

#[derive(Copy, Clone)]
pub struct Attr {
    pub central_supports: bool,
    pub storey_fill: StoreyFill,
    pub roof_style: RoofStyle,
    pub mansard: i32,
    pub pillar: Pillar,
    pub levels: i32,
    pub window: SpriteKind,
}

impl Attr {
    pub fn generate<R: Rng>(rng: &mut R, _locus: i32) -> Self {
        Self {
            central_supports: rng.gen(),
            storey_fill: match rng.gen_range(0..3) {
                0 => StoreyFill::None,
                1 => StoreyFill::Upper,
                _ => StoreyFill::All,
            },
            roof_style: match rng.gen_range(0..3) {
                0 => RoofStyle::Hip,
                1 => RoofStyle::Gable,
                _ => RoofStyle::Rounded,
            },
            mansard: rng.gen_range(-7..4).max(0),
            pillar: match rng.gen_range(0..4) {
                0 => Pillar::Chimney(rng.gen_range(2..6)),
                _ => Pillar::None,
            },
            levels: rng.gen_range(1..3),
            window: match rng.gen_range(0..4) {
                0 => SpriteKind::Window1,
                1 => SpriteKind::Window2,
                2 => SpriteKind::Window3,
                _ => SpriteKind::Window4,
            },
        }
    }
}

impl Archetype for House {
    type Attr = Attr;

    fn generate<R: Rng>(rng: &mut R, calendar: Option<&Calendar>) -> (Self, Skeleton<Self::Attr>) {
        let len = rng.gen_range(-8..24).clamped(0, 20);
        let locus = 6 + rng.gen_range(0..5);
        let branches_per_side = 1 + len as usize / 20;
        let levels = rng.gen_range(1..3);
        let skel = Skeleton {
            offset: -rng.gen_range(0..len + 7).clamped(0, len),
            ori: if rng.gen() { Ori::East } else { Ori::North },
            root: Branch {
                len,
                attr: Attr {
                    storey_fill: StoreyFill::All,
                    mansard: 0,
                    pillar: match rng.gen_range(0..3) {
                        0 => Pillar::Chimney(rng.gen_range(2..6)),
                        1 => Pillar::Tower(5 + rng.gen_range(1..5)),
                        _ => Pillar::None,
                    },
                    levels,
                    ..Attr::generate(rng, locus)
                },
                locus,
                border: 4,
                children: [1, -1]
                    .iter()
                    .flat_map(|flip| (0..branches_per_side).map(move |i| (i, *flip)))
                    .filter_map(|(i, flip)| {
                        if rng.gen() {
                            Some((
                                i as i32 * len / (branches_per_side - 1).max(1) as i32,
                                Branch {
                                    len: rng.gen_range(8..16) * flip,
                                    attr: Attr {
                                        levels: rng.gen_range(1..4).min(levels),
                                        ..Attr::generate(rng, locus)
                                    },
                                    locus: (6 + rng.gen_range(0..3)).min(locus),
                                    border: 4,
                                    children: Vec::new(),
                                },
                            ))
                        } else {
                            None
                        }
                    })
                    .collect(),
            },
        };

        let this = Self {
            colors: ColorTheme {
                roof: *ROOF_COLORS.choose(rng).unwrap(),
                wall: *WALL_COLORS.choose(rng).unwrap(),
                support: *SUPPORT_COLORS.choose(rng).unwrap(),
            },
            noise: RandomField::new(rng.gen()),
            roof_ribbing: rng.gen(),
            roof_ribbing_diagonal: rng.gen(),
            christmas_decorations: calendar
                .map(|c| c.is_event(CalendarEvent::Christmas))
                .unwrap_or_default(),
        };

        (this, skel)
    }

    fn draw(
        &self,
        index: IndexRef,
        _pos: Vec3<i32>,
        dist: i32,
        bound_offset: Vec2<i32>,
        center_offset: Vec2<i32>,
        z: i32,
        ori: Ori,
        locus: i32,
        _len: i32,
        attr: &Self::Attr,
    ) -> BlockMask {
        let colors = &index.colors.site.settlement.building.archetype.house;
        let roof_color = *self.colors.roof.elim_case_pure(&colors.roof);
        let wall_color = *self.colors.wall.elim_case_pure(&colors.wall);
        let support_color = *self.colors.support.elim_case_pure(&colors.support);
        let christmas_theme = self.christmas_decorations;

        let profile = Vec2::new(bound_offset.x, z);

        let make_block = |(r, g, b)| {
            let nz = self
                .noise
                .get(Vec3::new(center_offset.x, center_offset.y, z * 8));
            BlockMask::new(
                Block::new(
                    BlockKind::Misc,
                    // TODO: Clarify exactly how this affects the color.
                    Rgb::new(r, g, b)
                        .map(|e: u8| e.saturating_add((nz & 0x0F) as u8).saturating_sub(8)),
                ),
                2,
            )
        };

        let facade_layer = 3;
        let structural_layer = facade_layer + 1;
        let internal_layer = structural_layer + 1;
        let foundation_layer = internal_layer + 1;
        let floor_layer = foundation_layer + 1;

        let foundation = make_block(colors.foundation).with_priority(foundation_layer);
        let log = make_block(support_color);
        let floor = make_block(colors.floor);
        let wall = make_block(wall_color).with_priority(facade_layer);
        let roof = make_block(roof_color).with_priority(facade_layer - 1);
        const EMPTY: BlockMask = BlockMask::nothing();
        // TODO: Take environment into account.
        let internal = BlockMask::new(Block::air(SpriteKind::Empty), internal_layer);
        let end_ori = match ori {
            Ori::East => 2,
            Ori::North => 4,
        };
        let end_window = BlockMask::new(
            Block::air(attr.window).with_ori(end_ori).unwrap(),
            structural_layer,
        );
        let fire = BlockMask::new(Block::air(SpriteKind::Ember), foundation_layer);

        let storey_height = 6;
        let storey = ((z - 1) / storey_height).min(attr.levels - 1);
        let floor_height = storey_height * storey;
        let ceil_height = storey_height * (storey + 1);
        let lower_width = locus - 1;
        let upper_width = locus;
        let width = if profile.y >= ceil_height {
            upper_width
        } else {
            lower_width
        };
        let foundation_height = 0 - (dist - width - 1).max(0);
        let roof_top = storey_height * attr.levels + 2 + width;

        let edge_ori = if bound_offset.x.abs() > bound_offset.y.abs() {
            if center_offset.x > 0 { 6 } else { 2 }
        } else if (center_offset.y > 0) ^ (ori == Ori::East) {
            0
        } else {
            4
        };
        let edge_ori = if ori == Ori::East {
            (edge_ori + 2) % 8
        } else {
            edge_ori
        };

        if let Pillar::Chimney(chimney_height) = attr.pillar {
            let chimney_top = roof_top + chimney_height;
            // Chimney shaft
            if center_offset.map(|e| e.abs()).reduce_max() == 0 && profile.y > foundation_height {
                return if profile.y == foundation_height + 1 {
                    fire
                } else {
                    internal.with_priority(foundation_layer)
                };
            }

            // Chimney
            if center_offset.map(|e| e.abs()).reduce_max() <= 1 && profile.y < chimney_top {
                // Fireplace
                return if center_offset.product() == 0
                    && profile.y > foundation_height + 1
                    && profile.y <= foundation_height + 3
                {
                    internal
                } else {
                    foundation
                };
            }
        }

        if profile.y <= foundation_height && dist < width + 3 {
            // Foundations
            if attr.storey_fill.has_lower() {
                if dist == width - 1 {
                    // Floor lining
                    return log.with_priority(floor_layer);
                } else if dist < width - 1 && profile.y == foundation_height {
                    // Floor
                    return floor.with_priority(floor_layer);
                }
            }

            return if dist < width
                && profile.y < foundation_height
                && profile.y >= foundation_height - 3
            {
                // Basement
                internal
            } else {
                foundation.with_priority(1)
            };
        }

        // Roofs and walls
        let do_roof_wall =
            |profile: Vec2<i32>, width, dist, bound_offset: Vec2<i32>, roof_top, mansard| {
                // Roof

                let (roof_profile, roof_dist) = match &attr.roof_style {
                    RoofStyle::Hip => (Vec2::new(dist, profile.y), dist),
                    RoofStyle::Gable => (profile, dist),
                    RoofStyle::Rounded => {
                        let circular_dist = (bound_offset.map(|e| e.pow(4) as f32).sum().powf(0.25)
                            - 0.5)
                            .ceil() as i32;
                        (Vec2::new(circular_dist, profile.y), circular_dist)
                    },
                };

                let roof_level = roof_top - roof_profile.x.max(mansard);

                if profile.y > roof_level {
                    return EMPTY;
                }

                // Roof
                if profile.y == roof_level && roof_dist <= width + 2 {
                    let is_ribbing = ((profile.y - ceil_height) % 3 == 0 && self.roof_ribbing)
                        || (bound_offset.x == bound_offset.y && self.roof_ribbing_diagonal);
                    return if (roof_profile.x == 0 && mansard == 0)
                        || roof_dist == width + 2
                        || is_ribbing
                    {
                        // Eaves
                        log
                    } else {
                        roof
                    };
                }

                // Wall

                if dist == width && profile.y < roof_level {
                    // Doors
                    if center_offset.x > 0
                        && center_offset.y > 0
                        && bound_offset.x > 0
                        && bound_offset.x < width
                        && profile.y < ceil_height
                        && attr.storey_fill.has_lower()
                        && storey == 0
                    {
                        return if (bound_offset.x == (width - 1) / 2
                            || bound_offset.x == (width - 1) / 2 + 1)
                            && profile.y <= foundation_height + 3
                        {
                            // Doors on first floor only
                            if profile.y == foundation_height + 1 {
                                BlockMask::new(
                                    Block::air(SpriteKind::Door)
                                        .with_ori(
                                            match ori {
                                                Ori::East => 2,
                                                Ori::North => 0,
                                            } + if bound_offset.x == (width - 1) / 2 {
                                                0
                                            } else {
                                                4
                                            },
                                        )
                                        .unwrap(),
                                    structural_layer,
                                )
                            } else {
                                EMPTY.with_priority(structural_layer)
                            }
                        } else {
                            wall
                        };
                    }

                    return if bound_offset.x == bound_offset.y || profile.y == ceil_height {
                        // Support beams
                        log
                    } else if !attr.storey_fill.has_lower() && profile.y < ceil_height
                        || !attr.storey_fill.has_upper()
                    {
                        EMPTY
                    } else {
                        let (frame_bounds, frame_borders) = if profile.y >= ceil_height {
                            (
                                Aabr {
                                    min: Vec2::new(-1, ceil_height + 2),
                                    max: Vec2::new(1, ceil_height + 5),
                                },
                                Vec2::new(1, 1),
                            )
                        } else {
                            (
                                Aabr {
                                    min: Vec2::new(2, floor_height + 2),
                                    max: Vec2::new(width - 2, ceil_height - 2),
                                },
                                Vec2::new(1, 0),
                            )
                        };
                        let window_bounds = Aabr {
                            min: (frame_bounds.min + frame_borders)
                                .map2(frame_bounds.center(), |a, b| a.min(b)),
                            max: (frame_bounds.max - frame_borders)
                                .map2(frame_bounds.center(), |a, b| a.max(b)),
                        };

                        // Window
                        if (frame_bounds.size() + 1).reduce_min() > 2 {
                            // Window frame is large enough for a window
                            let surface_pos = Vec2::new(bound_offset.x, profile.y);
                            if window_bounds.contains_point(surface_pos) {
                                return end_window;
                            } else if frame_bounds.contains_point(surface_pos) {
                                return log.with_priority(structural_layer);
                            };
                        }

                        // Wall
                        if attr.central_supports && profile.x == 0 {
                            // Support beams
                            log.with_priority(structural_layer)
                        } else {
                            wall
                        }
                    };
                }

                if dist < width {
                    // Internals
                    if profile.y == ceil_height {
                        if profile.x == 0 {
                            // Rafters
                            return log;
                        } else if attr.storey_fill.has_upper() {
                            // Ceiling
                            return floor;
                        }
                    } else if !attr.storey_fill.has_lower()
                        && center_offset.sum() % 2 == 0
                        && profile.y == 1
                        && center_offset.map(|e| e % 3 == 0).reduce_and()
                        && self
                            .noise
                            .chance(Vec3::new(center_offset.x, center_offset.y, z), 0.8)
                    {
                        let furniture = match self.noise.get(Vec3::new(
                            center_offset.x,
                            center_offset.y,
                            z + 100,
                        )) % 14
                        {
                            0..=1 => SpriteKind::Crate,
                            2 => SpriteKind::Bench,
                            3 => SpriteKind::Anvil,
                            4 => SpriteKind::CookingPot,
                            5 => SpriteKind::CraftingBench,
                            6 => SpriteKind::FireBowlGround,
                            7 => SpriteKind::Cauldron,
                            8 => SpriteKind::Forge,
                            9 => SpriteKind::Loom,
                            10 => SpriteKind::SpinningWheel,
                            11 => SpriteKind::TanningRack,
                            12 => SpriteKind::DismantlingBench,
                            13 => SpriteKind::RepairBench,
                            _ => unreachable!(),
                        };

                        return BlockMask::new(Block::air(furniture).with_ori(end_ori).unwrap(), 1);
                    } else if (!attr.storey_fill.has_lower() && profile.y < ceil_height)
                        || (!attr.storey_fill.has_upper() && profile.y >= ceil_height)
                    {
                        return EMPTY;
                    // Furniture
                    } else if dist == width - 1
                        && center_offset.sum() % 2 == 0
                        && profile.y == floor_height + 1
                        && self
                            .noise
                            .chance(Vec3::new(center_offset.x, center_offset.y, z), 0.2)
                    {
                        // NOTE: Used only for dynamic elements like chests and entities!
                        let mut dynamic_rng = thread_rng();
                        let furniture = match self.noise.get(Vec3::new(
                            center_offset.x,
                            center_offset.y,
                            z + 100,
                        )) % 12
                        {
                            0 => SpriteKind::Planter,
                            1 => SpriteKind::ChairSingle,
                            2 => SpriteKind::ChairDouble,
                            3 => SpriteKind::CoatRack,
                            4 => {
                                if dynamic_rng.gen_range(0..8) == 0 {
                                    SpriteKind::Chest
                                } else {
                                    SpriteKind::Crate
                                }
                            },
                            6 => SpriteKind::DrawerMedium,
                            7 => SpriteKind::DrawerSmall,
                            8 => SpriteKind::TableSide,
                            9 => SpriteKind::WardrobeSingle,
                            10 => {
                                if dynamic_rng.gen_range(0..10) == 0 {
                                    SpriteKind::PotionMinor
                                } else {
                                    SpriteKind::VialEmpty
                                }
                            },
                            _ => {
                                if dynamic_rng.gen_range(0..2) == 0 {
                                    SpriteKind::Bowl
                                } else {
                                    SpriteKind::Pot
                                }
                            },
                        };

                        return BlockMask::new(
                            Block::air(furniture).with_ori(edge_ori).unwrap(),
                            internal_layer,
                        );
                    } else {
                        return internal;
                    }
                }

                // Wall ornaments
                if dist == width + 1
                    && center_offset.map(|e| e.abs()).reduce_min() == 0
                    && profile.y == floor_height + 3
                    && self.noise.chance(
                        Vec3::new(center_offset.x, center_offset.y, z),
                        if christmas_theme { 0.70 } else { 0.35 },
                    )
                    && attr.storey_fill.has_lower()
                {
                    let ornament = if christmas_theme {
                        match self
                            .noise
                            .get(Vec3::new(center_offset.x, center_offset.y, z + 100))
                            % 4
                        {
                            0 => SpriteKind::ChristmasWreath,
                            _ => SpriteKind::ChristmasOrnament,
                        }
                    } else {
                        match self
                            .noise
                            .get(Vec3::new(center_offset.x, center_offset.y, z + 100))
                            % 6
                        {
                            0 => SpriteKind::HangingSign,
                            1..=3 => SpriteKind::HangingBasket,
                            4 => SpriteKind::WallSconce,
                            5 => SpriteKind::WallLampSmall,
                            _ => SpriteKind::DungeonWallDecor,
                        }
                    };

                    BlockMask::new(
                        Block::air(ornament).with_ori((edge_ori + 4) % 8).unwrap(),
                        internal_layer,
                    )
                } else {
                    EMPTY
                }
            };

        let mut cblock = do_roof_wall(profile, width, dist, bound_offset, roof_top, attr.mansard);

        if let Pillar::Tower(tower_height) = attr.pillar {
            let tower_top = roof_top + tower_height;
            let profile = Vec2::new(center_offset.x.abs(), profile.y);
            let dist = center_offset.map(|e| e.abs()).reduce_max();

            cblock = cblock.resolve_with(do_roof_wall(
                profile,
                4,
                dist,
                center_offset.map(|e| e.abs()),
                tower_top,
                attr.mansard,
            ));
        }

        cblock
    }
}