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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#![allow(incomplete_features)]
#![allow(
    clippy::option_map_unit_fn,
    clippy::blocks_in_conditions,
    clippy::identity_op,
    clippy::needless_pass_by_ref_mut //until we find a better way for specs
)]
#![allow(clippy::branches_sharing_code)] // TODO: evaluate
#![deny(clippy::clone_on_ref_ptr)]
#![feature(option_zip, let_chains)]

mod all;
mod block;
pub mod canvas;
pub mod civ;
mod column;
pub mod config;
pub mod index;
pub mod land;
pub mod layer;
pub mod pathfinding;
pub mod sim;
pub mod sim2;
pub mod site;
pub mod site2;
pub mod util;

// Reexports
pub use crate::{
    canvas::{Canvas, CanvasInfo},
    config::{Features, CONFIG},
    land::Land,
    layer::PathLocals,
};
pub use block::BlockGen;
use civ::WorldCivStage;
pub use column::ColumnSample;
pub use common::terrain::site::{DungeonKindMeta, SettlementKindMeta};
use common::{spiral::Spiral2d, terrain::CoordinateConversions};
pub use index::{IndexOwned, IndexRef};
use sim::WorldSimStage;

use crate::{
    column::ColumnGen,
    index::Index,
    layer::spot::Spot,
    site::{SiteKind, SpawnRules},
    util::{Grid, Sampler},
};
use common::{
    assets,
    calendar::Calendar,
    generation::{ChunkSupplement, EntityInfo, SpecialEntity},
    lod,
    resources::TimeOfDay,
    rtsim::ChunkResource,
    terrain::{
        Block, BlockKind, SpriteKind, TerrainChunk, TerrainChunkMeta, TerrainChunkSize, TerrainGrid,
    },
    vol::{ReadVol, RectVolSize, WriteVol},
};
use common_base::prof_span;
use common_net::msg::{world_msg, WorldMapMsg};
use enum_map::EnumMap;
use rand::{prelude::*, Rng};
use rand_chacha::ChaCha8Rng;
use serde::Deserialize;
use std::time::Duration;
use vek::*;

#[cfg(all(feature = "be-dyn-lib", feature = "use-dyn-lib"))]
compile_error!("Can't use both \"be-dyn-lib\" and \"use-dyn-lib\" features at once");

#[cfg(feature = "use-dyn-lib")]
use {common_dynlib::LoadedLib, lazy_static::lazy_static, std::sync::Arc, std::sync::Mutex};

#[cfg(feature = "use-dyn-lib")]
lazy_static! {
    pub static ref LIB: Arc<Mutex<Option<LoadedLib>>> =
        common_dynlib::init("veloren-world", "world");
}

#[cfg(feature = "use-dyn-lib")]
pub fn init() { lazy_static::initialize(&LIB); }

#[derive(Debug)]
pub enum Error {
    Other(String),
}

#[derive(Debug)]
pub enum WorldGenerateStage {
    WorldSimGenerate(WorldSimStage),
    WorldCivGenerate(WorldCivStage),
    EconomySimulation,
    SpotGeneration,
}

pub struct World {
    sim: sim::WorldSim,
    civs: civ::Civs,
}

#[derive(Deserialize)]
pub struct Colors {
    pub deep_stone_color: (u8, u8, u8),
    pub block: block::Colors,
    pub column: column::Colors,
    pub layer: layer::Colors,
    pub site: site::Colors,
}

impl assets::Asset for Colors {
    type Loader = assets::RonLoader;

    const EXTENSION: &'static str = "ron";
}

impl World {
    pub fn generate(
        seed: u32,
        opts: sim::WorldOpts,
        threadpool: &rayon::ThreadPool,
        report_stage: &(dyn Fn(WorldGenerateStage) + Send + Sync),
    ) -> (Self, IndexOwned) {
        prof_span!("World::generate");
        // NOTE: Generating index first in order to quickly fail if the color manifest
        // is broken.
        threadpool.install(|| {
            let mut index = Index::new(seed);
            let calendar = opts.calendar.clone();

            let mut sim = sim::WorldSim::generate(seed, opts, threadpool, &|stage| {
                report_stage(WorldGenerateStage::WorldSimGenerate(stage))
            });

            let civs =
                civ::Civs::generate(seed, &mut sim, &mut index, calendar.as_ref(), &|stage| {
                    report_stage(WorldGenerateStage::WorldCivGenerate(stage))
                });

            report_stage(WorldGenerateStage::EconomySimulation);
            sim2::simulate(&mut index, &mut sim);

            report_stage(WorldGenerateStage::SpotGeneration);
            Spot::generate(&mut sim);

            (Self { sim, civs }, IndexOwned::new(index))
        })
    }

    pub fn sim(&self) -> &sim::WorldSim { &self.sim }

    pub fn civs(&self) -> &civ::Civs { &self.civs }

    pub fn tick(&self, _dt: Duration) {
        // TODO
    }

    pub fn get_map_data(&self, index: IndexRef, threadpool: &rayon::ThreadPool) -> WorldMapMsg {
        prof_span!("World::get_map_data");
        threadpool.install(|| {
            // we need these numbers to create unique ids for cave ends
            let num_sites = self.civs().sites().count() as u64;
            let num_caves = self.civs().caves.values().count() as u64;
            WorldMapMsg {
                pois: self.civs().pois.iter().map(|(_, poi)| {
                    world_msg::PoiInfo {
                        name: poi.name.clone(),
                        kind: match &poi.kind {
                            civ::PoiKind::Peak(alt) => world_msg::PoiKind::Peak(*alt),
                            civ::PoiKind::Biome(size) => world_msg::PoiKind::Lake(*size),
                        },
                        wpos: poi.loc * TerrainChunkSize::RECT_SIZE.map(|e| e as i32),
                    }
                }).collect(),
                sites: self
                    .civs()
                    .sites
                    .iter()
                    .filter(|(_, site)| !matches!(&site.kind,
                        civ::SiteKind::PirateHideout
                        | civ::SiteKind::JungleRuin
                        | civ::SiteKind::RockCircle
                        | civ::SiteKind::TrollCave
                        | civ::SiteKind::Camp))
                    .map(|(_, site)| {
                        world_msg::SiteInfo {
                            id: site.site_tmp.map(|i| i.id()).unwrap_or_default(),
                            name: site.site_tmp.map(|id| index.sites[id].name().to_string()),
                            // TODO: Probably unify these, at some point
                            kind: match &site.kind {
                                civ::SiteKind::Settlement
                                | civ::SiteKind::Refactor
                                | civ::SiteKind::CliffTown
                                | civ::SiteKind::SavannahPit
                                | civ::SiteKind::CoastalTown
                                | civ::SiteKind::DesertCity
                                | civ::SiteKind::PirateHideout
                                | civ::SiteKind::JungleRuin
                                | civ::SiteKind::RockCircle
                                | civ::SiteKind::TrollCave
                                | civ::SiteKind::Camp => world_msg::SiteKind::Town,
                                civ::SiteKind::Dungeon => world_msg::SiteKind::Dungeon {
                                    difficulty: match site.site_tmp.map(|id| &index.sites[id].kind) {
                                        Some(SiteKind::Dungeon(d)) => d.dungeon_difficulty().unwrap_or(0),
                                        _ => 0,
                                    },
                                },
                                civ::SiteKind::Castle => world_msg::SiteKind::Castle,
                                civ::SiteKind::Tree | civ::SiteKind::GiantTree => world_msg::SiteKind::Tree,
                                // TODO: Maybe change?
                                civ::SiteKind::Gnarling => world_msg::SiteKind::Gnarling,
                                //civ::SiteKind::DwarvenMine => world_msg::SiteKind::DwarvenMine,
                                civ::SiteKind::ChapelSite => world_msg::SiteKind::ChapelSite,
                                civ::SiteKind::Terracotta => world_msg::SiteKind::Terracotta,
                                civ::SiteKind::Citadel => world_msg::SiteKind::Castle,
                                civ::SiteKind::Bridge(_, _) => world_msg::SiteKind::Bridge,
                                civ::SiteKind::Adlet => world_msg::SiteKind::Adlet,
                                civ::SiteKind::Haniwa => world_msg::SiteKind::Haniwa,
                            },
                            wpos: site.center * TerrainChunkSize::RECT_SIZE.map(|e| e as i32),
                        }
                    })
                    .chain(
                        self.civs()
                            .caves
                            .iter()
                            .flat_map(|(id, info)| {
                                // separate the two locations, combine with name
                                std::iter::once((id.id() + num_sites, info.name.clone(), info.location.0))
                                    // unfortunately we have to introduce a fake id (as it gets stored in a map in the client)
                                    .chain(std::iter::once((id.id() + num_sites + num_caves, info.name.clone(), info.location.1)))
                            }) // unwrap inner iteration
                            .map(|(id, name, pos)| world_msg::SiteInfo {
                                id,
                                name: Some(name),
                                kind: world_msg::SiteKind::Cave,
                                wpos: pos,
                            }),
                    )
                    .chain(layer::cave::surface_entrances(&Land::from_sim(self.sim()))
                        .enumerate()
                        .map(|(i, wpos)| world_msg::SiteInfo {
                            id: 65536 + i as u64, // Generate a fake ID, TODO: don't do this
                            name: None,
                            kind: world_msg::SiteKind::Cave,
                            wpos,
                        }))
                    .collect(),
                possible_starting_sites: {
                    const STARTING_SITE_COUNT: usize = 5;

                    let mut candidates = self
                        .civs()
                        .sites
                        .iter()
                        .filter_map(|(_, civ_site)| Some((civ_site, civ_site.site_tmp?)))
                        .map(|(civ_site, site_id)| {
                            // Score the site according to how suitable it is to be a starting site

                            let (site2, mut score) = match &index.sites[site_id].kind {
                                SiteKind::Refactor(site2) => (site2, 2.0),
                                // Non-town sites should not be chosen as starting sites and get a score of 0
                                _ => return (site_id.id(), 0.0)
                            };

                            /// Optimal number of plots in a starter town
                            const OPTIMAL_STARTER_TOWN_SIZE: f32 = 30.0;

                            // Prefer sites of a medium size
                            let plots = site2.plots().len() as f32;
                            let size_score = if plots > OPTIMAL_STARTER_TOWN_SIZE {
                                1.0 + (1.0 / (1.0 + ((plots - OPTIMAL_STARTER_TOWN_SIZE) / 15.0).powi(3)))
                            } else {
                               (2.05 / (1.0 + ((OPTIMAL_STARTER_TOWN_SIZE - plots) / 15.0).powi(5))) - 0.05
                            }.max(0.01);

                            score *= size_score;

                            // Prefer sites that are close to the centre of the world
                            let pos_score = (
                                10.0 / (
                                    1.0 + (
                                        civ_site.center.map2(self.sim().get_size(),
                                            |e, sz|
                                            (e as f32 / sz as f32 - 0.5).abs() * 2.0).reduce_partial_max()
                                    ).powi(6) * 25.0
                                )
                            ).max(0.02);
                            score *= pos_score;

                            // Check if neighboring biomes are beginner friendly
                            let mut chunk_scores = 2.0;
                            for (chunk, distance) in Spiral2d::with_radius(10)
                                .filter_map(|rel_pos| {
                                    let chunk_pos = civ_site.center + rel_pos * 2;
                                    self.sim().get(chunk_pos).zip(Some(rel_pos.as_::<f32>().magnitude()))
                                })
                            {
                                let weight = 1.0 / (distance * std::f32::consts::TAU + 1.0);
                                let chunk_difficulty = 20.0 / (20.0 + chunk.get_biome().difficulty().pow(4) as f32 / 5.0);
                                // let chunk_difficulty = 1.0 / chunk.get_biome().difficulty() as f32;

                                chunk_scores *= 1.0 - weight + chunk_difficulty * weight;
                            }

                            score *= chunk_scores;

                            (site_id.id(), score)
                        })
                        .collect::<Vec<_>>();
                    candidates.sort_by_key(|(_, score)| -(*score * 1000.0) as i32);
                    candidates.into_iter().map(|(site_id, _)| site_id).take(STARTING_SITE_COUNT).collect()
                },
                ..self.sim.get_map(index, self.sim().calendar.as_ref())
            }
        })
    }

    pub fn sample_columns(
        &self,
    ) -> impl Sampler<
        Index = (Vec2<i32>, IndexRef, Option<&'_ Calendar>),
        Sample = Option<ColumnSample>,
    > + '_ {
        ColumnGen::new(&self.sim)
    }

    pub fn sample_blocks(&self) -> BlockGen { BlockGen::new(ColumnGen::new(&self.sim)) }

    /// Find a position that's accessible to a player at the given world
    /// position by searching blocks vertically.
    ///
    /// If `ascending` is `true`, we try to find the highest accessible position
    /// instead of the lowest.
    pub fn find_accessible_pos(
        &self,
        index: IndexRef,
        spawn_wpos: Vec2<i32>,
        ascending: bool,
    ) -> Vec3<f32> {
        let chunk_pos = TerrainGrid::chunk_key(spawn_wpos);

        // Unwrapping because generate_chunk only returns err when should_continue evals
        // to true
        let (tc, _cs) = self
            .generate_chunk(index, chunk_pos, None, || false, None)
            .unwrap();

        tc.find_accessible_pos(spawn_wpos, ascending)
    }

    #[allow(clippy::result_unit_err)]
    pub fn generate_chunk(
        &self,
        index: IndexRef,
        chunk_pos: Vec2<i32>,
        rtsim_resources: Option<EnumMap<ChunkResource, f32>>,
        // TODO: misleading name
        mut should_continue: impl FnMut() -> bool,
        time: Option<(TimeOfDay, Calendar)>,
    ) -> Result<(TerrainChunk, ChunkSupplement), ()> {
        let calendar = time.as_ref().map(|(_, cal)| cal);

        let mut sampler = self.sample_blocks();

        let chunk_wpos2d = chunk_pos * TerrainChunkSize::RECT_SIZE.map(|e| e as i32);
        let chunk_center_wpos2d = chunk_wpos2d + TerrainChunkSize::RECT_SIZE.map(|e| e as i32 / 2);
        let grid_border = 4;
        let zcache_grid = Grid::populate_from(
            TerrainChunkSize::RECT_SIZE.map(|e| e as i32) + grid_border * 2,
            |offs| sampler.get_z_cache(chunk_wpos2d - grid_border + offs, index, calendar),
        );

        let air = Block::air(SpriteKind::Empty);
        let stone = Block::new(
            BlockKind::Rock,
            zcache_grid
                .get(grid_border + TerrainChunkSize::RECT_SIZE.map(|e| e as i32) / 2)
                .and_then(|zcache| zcache.as_ref())
                .map(|zcache| zcache.sample.stone_col)
                .unwrap_or_else(|| index.colors.deep_stone_color.into()),
        );

        let (base_z, sim_chunk) = match self
            .sim
            /*.get_interpolated(
                chunk_pos.map2(chunk_size2d, |e, sz: u32| e * sz as i32 + sz as i32 / 2),
                |chunk| chunk.get_base_z(),
            )
            .and_then(|base_z| self.sim.get(chunk_pos).map(|sim_chunk| (base_z, sim_chunk))) */
            .get_base_z(chunk_pos)
        {
            Some(base_z) => (base_z as i32, self.sim.get(chunk_pos).unwrap()),
            // Some((base_z, sim_chunk)) => (base_z as i32, sim_chunk),
            None => {
                // NOTE: This is necessary in order to generate a handful of chunks at the edges
                // of the map.
                return Ok((self.sim().generate_oob_chunk(), ChunkSupplement::default()));
            },
        };

        let meta = TerrainChunkMeta::new(
            sim_chunk.get_location_name(&index.sites, &self.civs.pois, chunk_center_wpos2d),
            sim_chunk.get_biome(),
            sim_chunk.alt,
            sim_chunk.tree_density,
            sim_chunk.cave.1.alt != 0.0,
            sim_chunk.river.is_river(),
            sim_chunk.river.near_water(),
            sim_chunk.river.velocity,
            sim_chunk.temp,
            sim_chunk.humidity,
            sim_chunk
                .sites
                .iter()
                .filter(|id| {
                    index.sites[**id]
                        .get_origin()
                        .distance_squared(chunk_center_wpos2d) as f32
                        <= index.sites[**id].radius().powi(2)
                })
                .min_by_key(|id| {
                    index.sites[**id]
                        .get_origin()
                        .distance_squared(chunk_center_wpos2d)
                })
                .map(|id| index.sites[*id].kind.convert_to_meta().unwrap_or_default()),
            self.sim.approx_chunk_terrain_normal(chunk_pos),
            sim_chunk.rockiness,
            sim_chunk.cliff_height,
        );

        let mut chunk = TerrainChunk::new(base_z, stone, air, meta);

        for y in 0..TerrainChunkSize::RECT_SIZE.y as i32 {
            for x in 0..TerrainChunkSize::RECT_SIZE.x as i32 {
                if should_continue() {
                    return Err(());
                };

                let offs = Vec2::new(x, y);

                let z_cache = match zcache_grid.get(grid_border + offs) {
                    Some(Some(z_cache)) => z_cache,
                    _ => continue,
                };

                let (min_z, max_z) = z_cache.get_z_limits();

                (base_z..min_z as i32).for_each(|z| {
                    let _ = chunk.set(Vec3::new(x, y, z), stone);
                });

                (min_z as i32..max_z as i32).for_each(|z| {
                    let lpos = Vec3::new(x, y, z);
                    let wpos = Vec3::from(chunk_wpos2d) + lpos;

                    if let Some(block) = sampler.get_with_z_cache(wpos, Some(z_cache)) {
                        let _ = chunk.set(lpos, block);
                    }
                });
            }
        }

        let sample_get = |offs| {
            zcache_grid
                .get(grid_border + offs)
                .and_then(Option::as_ref)
                .map(|zc| &zc.sample)
        };

        // Only use for rng affecting dynamic elements like chests and entities!
        let mut dynamic_rng = ChaCha8Rng::from_seed(thread_rng().gen());

        // Apply layers (paths, caves, etc.)
        let mut canvas = Canvas {
            info: CanvasInfo {
                chunk_pos,
                wpos: chunk_pos * TerrainChunkSize::RECT_SIZE.map(|e| e as i32),
                column_grid: &zcache_grid,
                column_grid_border: grid_border,
                chunks: &self.sim,
                index,
                chunk: sim_chunk,
                calendar,
            },
            chunk: &mut chunk,
            entities: Vec::new(),
            rtsim_resource_blocks: Vec::new(),
        };

        if index.features.train_tracks {
            layer::apply_trains_to(&mut canvas, &self.sim, sim_chunk, chunk_center_wpos2d);
        }

        if index.features.caverns {
            layer::apply_caverns_to(&mut canvas, &mut dynamic_rng);
        }
        if index.features.caves {
            layer::apply_caves2_to(&mut canvas, &mut dynamic_rng);
            // layer::apply_caves_to(&mut canvas, &mut dynamic_rng);
        }
        if index.features.rocks {
            layer::apply_rocks_to(&mut canvas, &mut dynamic_rng);
        }
        if index.features.shrubs {
            layer::apply_shrubs_to(&mut canvas, &mut dynamic_rng);
        }
        if index.features.trees {
            layer::apply_trees_to(&mut canvas, &mut dynamic_rng, calendar);
        }
        if index.features.scatter {
            layer::apply_scatter_to(&mut canvas, &mut dynamic_rng, calendar);
        }
        if index.features.paths {
            layer::apply_paths_to(&mut canvas);
        }
        if index.features.spots {
            layer::apply_spots_to(&mut canvas, &mut dynamic_rng);
        }
        // layer::apply_coral_to(&mut canvas);

        // Apply site generation
        sim_chunk
            .sites
            .iter()
            .for_each(|site| index.sites[*site].apply_to(&mut canvas, &mut dynamic_rng));

        let mut rtsim_resource_blocks = std::mem::take(&mut canvas.rtsim_resource_blocks);
        let mut supplement = ChunkSupplement {
            entities: std::mem::take(&mut canvas.entities),
            rtsim_max_resources: Default::default(),
        };
        drop(canvas);

        let gen_entity_pos = |dynamic_rng: &mut ChaCha8Rng| {
            let lpos2d = TerrainChunkSize::RECT_SIZE
                .map(|sz| dynamic_rng.gen::<u32>().rem_euclid(sz) as i32);
            let mut lpos = Vec3::new(
                lpos2d.x,
                lpos2d.y,
                sample_get(lpos2d).map(|s| s.alt as i32 - 32).unwrap_or(0),
            );

            while let Some(block) = chunk.get(lpos).ok().copied().filter(Block::is_solid) {
                lpos.z += block.solid_height().ceil() as i32;
            }

            (Vec3::from(chunk_wpos2d) + lpos).map(|e: i32| e as f32) + 0.5
        };

        if sim_chunk.contains_waypoint {
            let waypoint_pos = gen_entity_pos(&mut dynamic_rng);
            if sim_chunk
                .sites
                .iter()
                .map(|site| index.sites[*site].spawn_rules(waypoint_pos.xy().as_()))
                .fold(SpawnRules::default(), |a, b| a.combine(b))
                .waypoints
            {
                supplement
                    .add_entity(EntityInfo::at(waypoint_pos).into_special(SpecialEntity::Waypoint));
            }
        }

        // Apply layer supplement
        layer::apply_caves_supplement(
            &mut dynamic_rng,
            chunk_wpos2d,
            sample_get,
            &chunk,
            index,
            &mut supplement,
        );

        // Apply layer supplement
        layer::wildlife::apply_wildlife_supplement(
            &mut dynamic_rng,
            chunk_wpos2d,
            sample_get,
            &chunk,
            index,
            sim_chunk,
            &mut supplement,
            time.as_ref(),
        );

        // Apply site supplementary information
        sim_chunk.sites.iter().for_each(|site| {
            index.sites[*site].apply_supplement(
                &mut dynamic_rng,
                chunk_wpos2d,
                sample_get,
                &mut supplement,
                site.id(),
                time.as_ref(),
            )
        });

        // Finally, defragment to minimize space consumption.
        chunk.defragment();

        // Before we finish, we check candidate rtsim resource blocks, deduplicating
        // positions and only keeping those that actually do have resources.
        // Although this looks potentially very expensive, only blocks that are rtsim
        // resources (i.e: a relatively small number of sprites) are processed here.
        if let Some(rtsim_resources) = rtsim_resources {
            rtsim_resource_blocks.sort_unstable_by_key(|pos| pos.into_array());
            rtsim_resource_blocks.dedup();
            for wpos in rtsim_resource_blocks {
                let _ = chunk.map(wpos - chunk_wpos2d.with_z(0), |block| {
                    if let Some(res) = block.get_rtsim_resource() {
                        // Note: this represents the upper limit, not the actual number spanwed, so
                        // we increment this before deciding whether we're going to spawn the
                        // resource.
                        supplement.rtsim_max_resources[res] += 1;
                        // Throw a dice to determine whether this resource should actually spawn
                        // TODO: Don't throw a dice, try to generate the *exact* correct number
                        if dynamic_rng.gen_bool(rtsim_resources[res] as f64) {
                            block
                        } else {
                            block.into_vacant()
                        }
                    } else {
                        block
                    }
                });
            }
        }

        Ok((chunk, supplement))
    }

    // Zone coordinates
    pub fn get_lod_zone(&self, pos: Vec2<i32>, index: IndexRef) -> lod::Zone {
        let min_wpos = pos.map(lod::to_wpos);
        let max_wpos = (pos + 1).map(lod::to_wpos);

        let mut objects = Vec::new();

        // Add trees
        prof_span!(guard, "add trees");
        objects.extend(
            &mut self
                .sim()
                .get_area_trees(min_wpos, max_wpos)
                .filter_map(|attr| {
                    ColumnGen::new(self.sim())
                        .get((attr.pos, index, self.sim().calendar.as_ref()))
                        .filter(|col| layer::tree::tree_valid_at(attr.pos, col, None, attr.seed))
                        .zip(Some(attr))
                })
                .filter_map(|(col, tree)| {
                    Some(lod::Object {
                        kind: match tree.forest_kind {
                            all::ForestKind::Dead => lod::ObjectKind::Dead,
                            all::ForestKind::Pine => lod::ObjectKind::Pine,
                            all::ForestKind::Mangrove => lod::ObjectKind::Mangrove,
                            all::ForestKind::Acacia => lod::ObjectKind::Acacia,
                            all::ForestKind::Birch => lod::ObjectKind::Birch,
                            all::ForestKind::Redwood => lod::ObjectKind::Redwood,
                            all::ForestKind::Baobab => lod::ObjectKind::Baobab,
                            all::ForestKind::Frostpine => lod::ObjectKind::Frostpine,
                            all::ForestKind::Palm => lod::ObjectKind::Palm,
                            _ => lod::ObjectKind::GenericTree,
                        },
                        pos: {
                            let rpos = tree.pos - min_wpos;
                            if rpos.is_any_negative() {
                                return None;
                            } else {
                                rpos.map(|e| e as i16).with_z(col.alt as i16)
                            }
                        },
                        flags: lod::InstFlags::empty()
                            | if col.snow_cover {
                                lod::InstFlags::SNOW_COVERED
                            } else {
                                lod::InstFlags::empty()
                            },
                        color: {
                            let field = crate::util::RandomField::new(tree.seed);
                            let lerp = field.get_f32(Vec3::from(tree.pos)) * 0.8 + 0.1;
                            let sblock = tree.forest_kind.leaf_block();

                            crate::all::leaf_color(index, tree.seed, lerp, &sblock)
                                .unwrap_or(Rgb::black())
                        },
                    })
                }),
        );
        drop(guard);

        // Add structures
        objects.extend(
            index
                .sites
                .iter()
                .filter(|(_, site)| {
                    site.get_origin()
                        .map2(min_wpos.zip(max_wpos), |e, (min, max)| e >= min && e < max)
                        .reduce_and()
                })
                .filter_map(|(_, site)| {
                    site.site2().map(|site| {
                        site.plots().filter_map(|plot| match &plot.kind {
                            site2::plot::PlotKind::House(h) => Some((
                                site.tile_wpos(plot.root_tile),
                                h.roof_color(),
                                lod::ObjectKind::House,
                            )),
                            site2::plot::PlotKind::GiantTree(t) => Some((
                                site.tile_wpos(plot.root_tile),
                                t.leaf_color(),
                                lod::ObjectKind::GiantTree,
                            )),
                            site2::plot::PlotKind::Haniwa(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::Haniwa,
                            )),
                            site2::plot::PlotKind::DesertCityMultiPlot(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::Desert,
                            )),
                            site2::plot::PlotKind::DesertCityArena(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::Arena,
                            )),
                            site2::plot::PlotKind::SavannahHut(_)
                            | site2::plot::PlotKind::SavannahWorkshop(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::SavannahHut,
                            )),
                            site2::plot::PlotKind::SavannahPit(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::SavannahPit,
                            )),
                            site2::plot::PlotKind::TerracottaPalace(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::TerracottaPalace,
                            )),
                            site2::plot::PlotKind::TerracottaHouse(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::TerracottaHouse,
                            )),
                            site2::plot::PlotKind::TerracottaYard(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::TerracottaYard,
                            )),
                            site2::plot::PlotKind::AirshipDock(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::AirshipDock,
                            )),
                            site2::plot::PlotKind::CoastalHouse(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::CoastalHouse,
                            )),
                            site2::plot::PlotKind::CoastalWorkshop(_) => Some((
                                site.tile_wpos(plot.root_tile),
                                Rgb::black(),
                                lod::ObjectKind::CoastalWorkshop,
                            )),
                            _ => None,
                        })
                    })
                })
                .flatten()
                .filter_map(|(wpos2d, color, model)| {
                    ColumnGen::new(self.sim())
                        .get((wpos2d, index, self.sim().calendar.as_ref()))
                        .zip(Some((wpos2d, color, model)))
                })
                .map(|(column, (wpos2d, color, model))| lod::Object {
                    kind: model,
                    pos: (wpos2d - min_wpos)
                        .map(|e| e as i16)
                        .with_z(self.sim().get_alt_approx(wpos2d).unwrap_or(0.0) as i16),
                    flags: if column.snow_cover {
                        lod::InstFlags::SNOW_COVERED
                    } else {
                        lod::InstFlags::empty()
                    },
                    color,
                }),
        );

        lod::Zone { objects }
    }

    // determine waypoint name
    pub fn get_location_name(&self, index: IndexRef, wpos2d: Vec2<i32>) -> Option<String> {
        let chunk_pos = wpos2d.wpos_to_cpos();
        let sim_chunk = self.sim.get(chunk_pos)?;
        sim_chunk.get_location_name(&index.sites, &self.civs.pois, wpos2d)
    }
}