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
use super::SpawnRules;
use crate::{
    column::ColumnSample,
    sim::WorldSim,
    site::{
        namegen::NameGen,
        settlement::building::{
            archetype::keep::{Attr, FlagColor, Keep as KeepArchetype, StoneColor},
            Archetype, Ori,
        },
    },
    IndexRef,
};
use common::{
    generation::ChunkSupplement,
    terrain::{Block, BlockKind, SpriteKind},
    vol::{ReadVol, RectSizedVol, WriteVol},
};
use core::f32;
use rand::prelude::*;
use serde::Deserialize;
use vek::*;

struct Keep {
    offset: Vec2<i32>,
    locus: i32,
    storeys: i32,
    is_tower: bool,
    alt: i32,
}

struct Tower {
    offset: Vec2<i32>,
    alt: i32,
}

pub struct Castle {
    name: String,
    origin: Vec2<i32>,
    //seed: u32,
    radius: i32,
    towers: Vec<Tower>,
    keeps: Vec<Keep>,
    rounded_towers: bool,
    ridged: bool,
    flags: bool,

    evil: bool,
}

pub struct GenCtx<'a, R: Rng> {
    sim: Option<&'a mut WorldSim>,
    rng: &'a mut R,
}

#[derive(Deserialize)]
pub struct Colors;

impl Castle {
    pub fn generate(wpos: Vec2<i32>, sim: Option<&mut WorldSim>, rng: &mut impl Rng) -> Self {
        let ctx = GenCtx { sim, rng };

        let boundary_towers = ctx.rng.gen_range(5..10);
        let keep_count = ctx.rng.gen_range(1..4);
        let boundary_noise = ctx.rng.gen_range(-2i32..8).max(1) as f32;

        let radius = 150;

        let this = Self {
            name: {
                let name = NameGen::location(ctx.rng).generate();
                match ctx.rng.gen_range(0..6) {
                    0 => format!("Fort {}", name),
                    1 => format!("{} Citadel", name),
                    2 => format!("{} Castle", name),
                    3 => format!("{} Stronghold", name),
                    4 => format!("{} Fortress", name),
                    _ => format!("{} Keep", name),
                }
            },
            origin: wpos,
            // alt: ctx
            //     .sim
            //     .as_ref()
            //     .and_then(|sim| sim.get_alt_approx(wpos))
            //     .unwrap_or(0.0) as i32
            //     + 6,
            //seed: ctx.rng.gen(),
            radius,

            towers: (0..boundary_towers)
                .map(|i| {
                    let angle = (i as f32 / boundary_towers as f32) * f32::consts::PI * 2.0;
                    let dir = Vec2::new(angle.cos(), angle.sin());
                    let dist = radius as f32 + ((angle * boundary_noise).sin() - 1.0) * 40.0;

                    let mut offset = (dir * dist).map(|e| e as i32);
                    // Try to move the tower around until it's not intersecting a path
                    for i in (1..80).step_by(5) {
                        if ctx
                            .sim
                            .as_ref()
                            .and_then(|sim| sim.get_nearest_path(wpos + offset))
                            .map(|(dist, _, _, _)| dist > 24.0)
                            .unwrap_or(true)
                        {
                            break;
                        }
                        offset = (dir * dist)
                            .map(|e| (e + ctx.rng.gen_range(-1.0..1.0) * i as f32) as i32);
                    }

                    Tower {
                        offset,
                        alt: ctx
                            .sim
                            .as_ref()
                            .and_then(|sim| sim.get_alt_approx(wpos + offset))
                            .unwrap_or(0.0) as i32
                            + 2,
                    }
                })
                .collect(),
            rounded_towers: ctx.rng.gen(),
            ridged: ctx.rng.gen(),
            flags: ctx.rng.gen(),
            evil: ctx.rng.gen(),
            keeps: (0..keep_count)
                .map(|i| {
                    let angle = (i as f32 / keep_count as f32) * f32::consts::PI * 2.0;
                    let dir = Vec2::new(angle.cos(), angle.sin());
                    let dist =
                        (radius as f32 + ((angle * boundary_noise).sin() - 1.0) * 40.0) * 0.3;

                    let locus = ctx.rng.gen_range(20..26);
                    let offset = (dir * dist).map(|e| e as i32);
                    let storeys = ctx.rng.gen_range(1..8).clamped(3, 5);

                    Keep {
                        offset,
                        locus,
                        storeys,
                        is_tower: true,
                        alt: ctx
                            .sim
                            .as_ref()
                            .and_then(|sim| sim.get_alt_approx(wpos + offset))
                            .unwrap_or(0.0) as i32
                            + 2,
                    }
                })
                .collect(),
        };

        this
    }

    pub fn name(&self) -> &str { &self.name }

    pub fn contains_point(&self, wpos: Vec2<i32>) -> bool {
        let lpos = wpos - self.origin;
        for i in 0..self.towers.len() {
            let tower0 = &self.towers[i];
            let tower1 = &self.towers[(i + 1) % self.towers.len()];

            if lpos.determine_side(Vec2::zero(), tower0.offset) > 0
                && lpos.determine_side(Vec2::zero(), tower1.offset) <= 0
                && lpos.determine_side(tower0.offset, tower1.offset) > 0
            {
                return true;
            }
        }

        false
    }

    pub fn get_origin(&self) -> Vec2<i32> { self.origin }

    pub fn radius(&self) -> f32 { 200.0 }

    pub fn spawn_rules(&self, wpos: Vec2<i32>) -> SpawnRules {
        SpawnRules {
            trees: wpos.distance_squared(self.origin) > self.radius.pow(2),
            ..SpawnRules::default()
        }
    }

    pub fn apply_to<'a>(
        &'a self,
        index: IndexRef,
        wpos2d: Vec2<i32>,
        mut get_column: impl FnMut(Vec2<i32>) -> Option<&'a ColumnSample<'a>>,
        vol: &mut (impl RectSizedVol<Vox = Block> + ReadVol + WriteVol),
    ) {
        for y in 0..vol.size_xy().y as i32 {
            for x in 0..vol.size_xy().x as i32 {
                let offs = Vec2::new(x, y);

                let wpos2d = wpos2d + offs;
                let rpos = wpos2d - self.origin;

                if rpos.magnitude_squared() > (self.radius + 64).pow(2) {
                    continue;
                }

                let col_sample = if let Some(col) = get_column(offs) {
                    col
                } else {
                    continue;
                };

                // Inner ground
                if self.contains_point(wpos2d) {
                    let surface_z = col_sample.alt as i32;
                    for z in -5..3 {
                        let pos = Vec3::new(offs.x, offs.y, surface_z + z);

                        if z > 0 {
                            if vol.get(pos).unwrap().kind() != BlockKind::Water {
                                // TODO: Take environment into account.
                                let _ = vol.set(pos, Block::air(SpriteKind::Empty));
                            }
                        } else {
                            let _ = vol.set(
                                pos,
                                Block::new(
                                    BlockKind::Earth,
                                    col_sample.sub_surface_color.map(|e| (e * 255.0) as u8),
                                ),
                            );
                        }
                    }
                }

                let (wall_dist, wall_pos, wall_alt, wall_ori, _towers) = (0..self.towers.len())
                    .map(|i| {
                        let tower0 = &self.towers[i];
                        let tower1 = &self.towers[(i + 1) % self.towers.len()];

                        let wall = LineSegment2 {
                            start: tower0.offset.map(|e| e as f32),
                            end: tower1.offset.map(|e| e as f32),
                        };

                        let projected = wall
                            .projected_point(rpos.map(|e| e as f32))
                            .map(|e| e.floor() as i32);

                        let tower0_dist = tower0
                            .offset
                            .map(|e| e as f32)
                            .distance(projected.map(|e| e as f32));
                        let tower1_dist = tower1
                            .offset
                            .map(|e| e as f32)
                            .distance(projected.map(|e| e as f32));
                        let tower_lerp = tower0_dist / (tower0_dist + tower1_dist);
                        let wall_ori = if (tower0.offset.x - tower1.offset.x).abs()
                            < (tower0.offset.y - tower1.offset.y).abs()
                        {
                            Ori::North
                        } else {
                            Ori::East
                        };

                        (
                            wall.distance_to_point(rpos.map(|e| e as f32)) as i32,
                            projected,
                            Lerp::lerp(tower0.alt as f32, tower1.alt as f32, tower_lerp) as i32,
                            wall_ori,
                            [tower0, tower1],
                        )
                    })
                    .min_by_key(|x| x.0)
                    .unwrap();
                let border_pos = (wall_pos - rpos).map(|e| e.abs());
                let wall_rpos = if wall_ori == Ori::East {
                    rpos
                } else {
                    rpos.yx()
                };
                let head_space = col_sample
                    .path
                    .map(|(dist, _, path, _)| path.head_space(dist))
                    .unwrap_or(0);

                let wall_sample = if let Some(col) = get_column(offs + wall_pos - rpos) {
                    col
                } else {
                    col_sample
                };

                // Make sure particularly weird terrain doesn't give us underground walls
                let wall_alt = wall_alt + (wall_sample.alt as i32 - wall_alt - 10).max(0);

                let keep_archetype = KeepArchetype {
                    flag_color: if self.evil {
                        FlagColor::Evil
                    } else {
                        FlagColor::Good
                    },
                    stone_color: if self.evil {
                        StoneColor::Evil
                    } else {
                        StoneColor::Good
                    },
                };

                for z in -10..64 {
                    let wpos = Vec3::new(wpos2d.x, wpos2d.y, col_sample.alt as i32 + z);

                    // Boundary wall
                    let wall_z = wpos.z - wall_alt;
                    if z < head_space {
                        continue;
                    }

                    let mut mask = keep_archetype.draw(
                        index,
                        Vec3::from(wall_rpos) + Vec3::unit_z() * wpos.z - wall_alt,
                        wall_dist,
                        border_pos,
                        rpos - wall_pos,
                        wall_z,
                        wall_ori,
                        4,
                        0,
                        &Attr {
                            storeys: 2,
                            is_tower: false,
                            flag: self.flags,
                            ridged: false,
                            rounded: true,
                            has_doors: false,
                        },
                    );

                    // Boundary towers
                    for tower in &self.towers {
                        let tower_wpos = Vec3::new(
                            self.origin.x + tower.offset.x,
                            self.origin.y + tower.offset.y,
                            tower.alt,
                        );
                        let tower_locus = 10;

                        let border_pos = (tower_wpos - wpos).xy().map(|e| e.abs());
                        mask = mask.resolve_with(keep_archetype.draw(
                            index,
                            if (tower_wpos.x - wpos.x).abs() < (tower_wpos.y - wpos.y).abs() {
                                wpos - tower_wpos
                            } else {
                                Vec3::new(
                                    wpos.y - tower_wpos.y,
                                    wpos.x - tower_wpos.x,
                                    wpos.z - tower_wpos.z,
                                )
                            },
                            border_pos.reduce_max() - tower_locus,
                            Vec2::new(border_pos.reduce_min(), border_pos.reduce_max()),
                            (wpos - tower_wpos).xy(),
                            wpos.z - tower.alt,
                            if border_pos.x > border_pos.y {
                                Ori::East
                            } else {
                                Ori::North
                            },
                            tower_locus,
                            0,
                            &Attr {
                                storeys: 3,
                                is_tower: true,
                                flag: self.flags,
                                ridged: self.ridged,
                                rounded: self.rounded_towers,
                                has_doors: false,
                            },
                        ));
                    }

                    // Keeps
                    for keep in &self.keeps {
                        let keep_wpos = Vec3::new(
                            self.origin.x + keep.offset.x,
                            self.origin.y + keep.offset.y,
                            keep.alt,
                        );

                        let border_pos = (keep_wpos - wpos).xy().map(|e| e.abs());
                        mask = mask.resolve_with(keep_archetype.draw(
                            index,
                            if (keep_wpos.x - wpos.x).abs() < (keep_wpos.y - wpos.y).abs() {
                                wpos - keep_wpos
                            } else {
                                Vec3::new(
                                    wpos.y - keep_wpos.y,
                                    wpos.x - keep_wpos.x,
                                    wpos.z - keep_wpos.z,
                                )
                            },
                            border_pos.reduce_max() - keep.locus,
                            Vec2::new(border_pos.reduce_min(), border_pos.reduce_max()),
                            (wpos - keep_wpos).xy(),
                            wpos.z - keep.alt,
                            if border_pos.x > border_pos.y {
                                Ori::East
                            } else {
                                Ori::North
                            },
                            keep.locus,
                            0,
                            &Attr {
                                storeys: keep.storeys,
                                is_tower: keep.is_tower,
                                flag: self.flags,
                                ridged: self.ridged,
                                rounded: self.rounded_towers,
                                has_doors: true,
                            },
                        ));
                    }

                    if let Some(block) = mask.finish() {
                        let _ = vol.set(Vec3::new(offs.x, offs.y, wpos.z), block);
                    }
                }
            }
        }
    }

    pub fn apply_supplement<'a>(
        &'a self,
        // NOTE: Used only for dynamic elements like chests and entities!
        _dynamic_rng: &mut impl Rng,
        _wpos2d: Vec2<i32>,
        _get_column: impl FnMut(Vec2<i32>) -> Option<&'a ColumnSample<'a>>,
        _supplement: &mut ChunkSupplement,
    ) {
        // TODO
    }
}