veloren_world/site/plot/
giant_tree.rs1use crate::{
2 Land, Sampler, SpriteKind,
3 layer::tree::{ProceduralTree, TreeConfig},
4 site::{Fill, Painter, Site, Structure, namegen::NameGen},
5 util::FastNoise,
6};
7use common::{
8 generation::EntityInfo,
9 terrain::{Block, BlockKind},
10};
11use rand::Rng;
12use vek::*;
13
14pub struct GiantTree {
15 name: String,
16 wpos: Vec3<i32>,
17 tree: ProceduralTree,
18 seed: u32,
19}
20
21impl GiantTree {
22 pub fn generate(site: &Site, center_tile: Vec2<i32>, land: &Land, rng: &mut impl Rng) -> Self {
23 let wpos = site.tile_center_wpos(center_tile);
24 Self {
25 name: format!("Tree of {}", NameGen::location(rng).generate()),
26 wpos: wpos.with_z(land.get_alt_approx(wpos) as i32),
28 tree: {
29 let config = TreeConfig::giant(rng, 4.0, true);
30 ProceduralTree::generate(config, rng)
31 },
32 seed: rng.gen(),
33 }
34 }
35
36 pub fn name(&self) -> &str { &self.name }
37
38 pub fn radius(&self) -> f32 { 100.0 }
39
40 pub fn tree(&self) -> &ProceduralTree { &self.tree }
41
42 pub fn entity_at(
43 &self,
44 pos: Vec3<i32>,
45 above_block: &Block,
46 dynamic_rng: &mut impl Rng,
47 ) -> Option<EntityInfo> {
48 if above_block.kind() == BlockKind::Leaves && dynamic_rng.gen_bool(0.000055) {
49 let entity = EntityInfo::at(pos.as_());
50 match dynamic_rng.gen_range(0..=7) {
51 0 => Some(entity.with_asset_expect(
52 "common.entity.wild.aggressive.horn_beetle",
53 dynamic_rng,
54 None,
55 )),
56 1 => Some(entity.with_asset_expect(
57 "common.entity.wild.aggressive.stag_beetle",
58 dynamic_rng,
59 None,
60 )),
61 2..=3 => Some(entity.with_asset_expect(
62 "common.entity.wild.aggressive.deadwood",
63 dynamic_rng,
64 None,
65 )),
66 4 => Some(entity.with_asset_expect(
67 "common.entity.wild.aggressive.maneater",
68 dynamic_rng,
69 None,
70 )),
71 5..=7 => Some(entity.with_asset_expect(
72 "common.entity.wild.peaceful.parrot",
73 dynamic_rng,
74 None,
75 )),
76 _ => None,
77 }
78 } else {
79 None
80 }
81 }
82
83 pub fn leaf_color(&self) -> Rgb<u8> {
84 let fast_noise = FastNoise::new(self.seed);
85 let dark = Rgb::new(10, 70, 50).map(|e| e as f32);
86 let light = Rgb::new(80, 140, 10).map(|e| e as f32);
87 Lerp::lerp(
88 dark,
89 light,
90 fast_noise.get((self.wpos.map(|e| e as f64) * 0.05) * 0.5 + 0.5),
91 )
92 .map(|e| e as u8)
93 }
94}
95
96impl Structure for GiantTree {
97 #[cfg(feature = "use-dyn-lib")]
98 const UPDATE_FN: &'static [u8] = b"render_gianttree\0";
99
100 #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "render_gianttree"))]
101 fn render_inner(&self, _site: &Site, _land: &Land, painter: &Painter) {
102 let leaf_col = self.leaf_color();
103 let mut rng = rand::thread_rng();
104 self.tree.walk(|branch, parent| {
105 let aabr = Aabr {
106 min: self.wpos.xy() + branch.get_aabb().min.xy().as_(),
107 max: self.wpos.xy() + branch.get_aabb().max.xy().as_(),
108 };
109 if aabr.collides_with_aabr(painter.render_aabr().as_()) {
110 painter
111 .line_two_radius(
112 self.wpos + branch.get_line().start.as_(),
113 self.wpos + branch.get_line().end.as_(),
114 parent.get_wood_radius(),
115 branch.get_wood_radius(),
116 )
117 .fill(Fill::Block(Block::new(
118 BlockKind::Wood,
119 Rgb::new(80, 32, 0),
120 )));
121 if branch.get_leaf_radius() > branch.get_wood_radius() {
122 painter
123 .line_two_radius(
124 self.wpos + branch.get_line().start.as_(),
125 self.wpos + branch.get_line().end.as_(),
126 parent.get_leaf_radius(),
127 branch.get_leaf_radius(),
128 )
129 .fill(Fill::Block(Block::new(BlockKind::Leaves, leaf_col)));
130 let branch_start = branch.get_line().start;
132 let branch_end = branch.get_line().end;
133 let branch_direction = (branch_end - branch_start).normalized();
134
135 let displacement =
137 (branch_direction * branch.get_leaf_radius()).map(|e| e.round() as i32);
138 let pos = self.wpos + branch_end.as_() + displacement;
139 if rng.gen_bool(0.07) {
140 let ori = rng.gen_range(0..=3) * 2;
141 painter.resource_sprite(pos, SpriteKind::Ironwood, ori);
142 }
143 }
144 true
145 } else {
146 false
147 }
148 });
149 }
150}