veloren_world/site/plot/
pirate_hideout.rs

1use super::*;
2use crate::{
3    Land,
4    assets::AssetHandle,
5    site::gen::PrimitiveTransform,
6    util::{RandomField, sampler::Sampler},
7};
8use common::{
9    generation::EntityInfo,
10    terrain::{Structure as PrefabStructure, StructuresGroup},
11};
12use lazy_static::lazy_static;
13use rand::prelude::*;
14use std::f32::consts::TAU;
15use vek::*;
16
17pub struct PirateHideout {
18    bounds: Aabr<i32>,
19    pub(crate) alt: i32,
20}
21impl PirateHideout {
22    pub fn generate(land: &Land, _rng: &mut impl Rng, site: &Site, tile_aabr: Aabr<i32>) -> Self {
23        let bounds = Aabr {
24            min: site.tile_wpos(tile_aabr.min),
25            max: site.tile_wpos(tile_aabr.max),
26        };
27        Self {
28            bounds,
29            alt: land.get_alt_approx(site.tile_center_wpos((tile_aabr.max - tile_aabr.min) / 2))
30                as i32
31                + 2,
32        }
33    }
34}
35
36impl Structure for PirateHideout {
37    #[cfg(feature = "use-dyn-lib")]
38    const UPDATE_FN: &'static [u8] = b"render_pirate_hideout\0";
39
40    #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "render_pirate_hideout"))]
41    fn render_inner(&self, _site: &Site, land: &Land, painter: &Painter) {
42        let center = self.bounds.center();
43        let base = land.get_alt_approx(center) as i32;
44        let mut thread_rng = thread_rng();
45        let model_pos = center.with_z(base - 2);
46        // model
47        lazy_static! {
48            pub static ref MODEL: AssetHandle<StructuresGroup> =
49                PrefabStructure::load_group("site_structures.pirate_hideout.pirate_hideout");
50        }
51        let rng = RandomField::new(0).get(model_pos) % 10;
52        let model = MODEL.read();
53        let model = model[rng as usize % model.len()].clone();
54        painter
55            .prim(Primitive::Prefab(Box::new(model.clone())))
56            .translate(model_pos)
57            .fill(Fill::Prefab(Box::new(model), model_pos, rng));
58
59        if thread_rng.gen_range(0..=3) < 1 {
60            // buccaneer
61            painter.spawn(
62                EntityInfo::at(center.with_z(base + 2).as_()).with_asset_expect(
63                    "common.entity.spot.buccaneer",
64                    &mut thread_rng,
65                    None,
66                ),
67            )
68        }
69        // npcs
70        let npc_radius = 15;
71        let phi = TAU / 5.0;
72        for n in 1..=5 {
73            let npc_pos = Vec2::new(
74                center.x + (npc_radius as f32 * ((n as f32 * phi).cos())) as i32,
75                center.y + (npc_radius as f32 * ((n as f32 * phi).sin())) as i32,
76            );
77
78            match RandomField::new(0).get(npc_pos.with_z(base + 2)) % 10 {
79                // rat
80                0 => painter.spawn(
81                    EntityInfo::at(npc_pos.with_z(base).as_()).with_asset_expect(
82                        "common.entity.wild.peaceful.rat",
83                        &mut thread_rng,
84                        None,
85                    ),
86                ),
87                // parrot
88                1 => painter.spawn(
89                    EntityInfo::at(npc_pos.with_z(base).as_()).with_asset_expect(
90                        "common.entity.wild.peaceful.parrot",
91                        &mut thread_rng,
92                        None,
93                    ),
94                ),
95                // pirates
96                _ => painter.spawn(
97                    EntityInfo::at(npc_pos.with_z(base).as_()).with_asset_expect(
98                        "common.entity.spot.pirate",
99                        &mut thread_rng,
100                        None,
101                    ),
102                ),
103            }
104        }
105    }
106}