Skip to main content

steel_core/worldgen/feature/features/
random_neighbor_spread.rs

1use super::super::prelude::*;
2use super::super::runner::FeatureDecorationRunner;
3
4impl FeatureDecorationRunner {
5    pub(in crate::worldgen::feature) fn place_random_neighbor_spread_feature(
6        region: &mut WorldGenRegion<'_>,
7        registry: &Registry,
8        random: &mut WorldgenRandom,
9        config: &RandomNeighborSpreadConfiguration,
10        origin: BlockPos,
11    ) -> bool {
12        let origin_state = BlockStateProviderEvaluator::sample_block_state_provider(
13            region,
14            registry,
15            random,
16            &config.block,
17            origin,
18        );
19        let _ = region.set_block_state(origin, origin_state, UpdateFlags::UPDATE_CLIENTS);
20
21        let attempts = config.attempts.sample(random);
22        for _ in 0..attempts {
23            let place_pos = origin.offset(
24                config.xz_offset.sample(random),
25                config.y_offset.sample(random),
26                config.xz_offset.sample(random),
27            );
28            if !BlockStateProviderEvaluator::test_block_predicate(
29                region,
30                registry,
31                &config.can_replace,
32                place_pos,
33            ) {
34                continue;
35            }
36
37            let mut neighbors = 0;
38            for direction in Self::VANILLA_DIRECTION_VALUES {
39                let block = region
40                    .block_state(place_pos.relative(direction))
41                    .get_block();
42                if Self::block_matches_holder_set(block, &config.accepted_neighbors) {
43                    neighbors += 1;
44                }
45                if neighbors > 1 {
46                    break;
47                }
48            }
49
50            if neighbors == 1 {
51                let state = BlockStateProviderEvaluator::sample_block_state_provider(
52                    region,
53                    registry,
54                    random,
55                    &config.block,
56                    place_pos,
57                );
58                let _ = region.set_block_state(place_pos, state, UpdateFlags::UPDATE_CLIENTS);
59            }
60        }
61
62        true
63    }
64}