Skip to content

Commit

Permalink
content: Add random rotations to tree leaves blocks.
Browse files Browse the repository at this point in the history
This lets us get a lot more variation out of the same original voxels.
  • Loading branch information
kpreid committed Sep 25, 2024
1 parent 8f4d0e6 commit 885e902
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions all-is-cubes-content/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use core::fmt;

use itertools::Itertools as _;
use petgraph::visit::EdgeRef as _;
use rand::seq::SliceRandom as _;
use rand::Rng as _;

use all_is_cubes::block::{self, Block, AIR};
use all_is_cubes::linking::BlockProvider;
Expand Down Expand Up @@ -65,6 +67,7 @@ pub(crate) fn make_log(
blocks: &BlockProvider<LandscapeBlocks>,
directions: FaceMap<Option<TreeGrowth>>,
leaves: Option<TreeGrowth>,
rng: &mut dyn rand::RngCore,
) -> Block {
// TODO: this needs to canonicalize rotations so that we don't end up with
// identical-looking but differently defined blocks.
Expand All @@ -87,9 +90,13 @@ pub(crate) fn make_log(
);

if let Some(leaves_growth) = leaves {
// Rotating (and mirroring) leaves gives less obvious repetition
let leaves_rotation = *GridRotation::ALL.choose(rng).unwrap();
wood.with_modifier(
block::Composite::new(
blocks[Leaves(leaves_growth)].clone(),
blocks[Leaves(leaves_growth)]
.clone()
.rotate(leaves_rotation),
block::CompositeOperator::Over,
)
.reversed() // wood always wins
Expand All @@ -106,7 +113,7 @@ pub(crate) fn make_log(
/// Panics if `root` is not within `bounds`.
pub(crate) fn make_tree(
blocks: &BlockProvider<LandscapeBlocks>,
rng: &mut impl rand::Rng,
rng: &mut dyn rand::RngCore,
root: Cube,
bounds: GridAab,
) -> SpaceTransaction {
Expand Down Expand Up @@ -202,7 +209,7 @@ pub(crate) fn make_tree(

// Convert graph into blocks. Skip the bottom layer that existed just to help the root.
let mut txn = SpaceTransaction::default();
for (cube, log) in graph.logs(blocks) {
for (cube, log) in graph.logs(blocks, rng) {
if log != AIR && bounds.contains_cube(cube) {
txn.at(cube).overwrite(log);
}
Expand Down Expand Up @@ -289,6 +296,7 @@ mod graph {
pub fn logs<'a>(
&'a self,
blocks: &'a BlockProvider<LandscapeBlocks>,
rng: &'a mut dyn rand::RngCore,
) -> impl Iterator<Item = (Cube, Block)> + 'a {
self.bounds().interior_iter().map(|cube| {
(
Expand All @@ -297,6 +305,7 @@ mod graph {
blocks,
self.neighbor_edges(cube),
self.leaves(cube).flatten(),
rng,
),
)
})
Expand Down

0 comments on commit 885e902

Please sign in to comment.