Skip to content

Commit

Permalink
Let seed be specified in config
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshiew committed Dec 31, 2024
1 parent ddf5392 commit c760aab
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 32 deletions.
1 change: 1 addition & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ rotation_x: -0.08
rotation_y: -0.9
rotation_z: -0.4
rotation_w: 0.18
seed: 0
2 changes: 1 addition & 1 deletion crates/extras/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn bench_mountain_islands(c: &mut Criterion) {
})
.collect();
let palette: Palette = mapping.into();
let wgen = MountainIslands::from(palette);
let wgen = MountainIslands::new(0, palette);
const SIZE: i32 = 4;
c.bench_function("mountain islands", |b| {
b.iter(|| {
Expand Down
4 changes: 2 additions & 2 deletions crates/extras/src/worldgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub enum WorldGenTypes {
}

impl WorldGenTypes {
pub fn as_world_gen(&self, palette: Palette) -> Arc<dyn WorldGen + Send + Sync> {
pub fn as_world_gen(&self, seed: u32, palette: Palette) -> Arc<dyn WorldGen + Send + Sync> {
match self {
WorldGenTypes::Flat => Arc::new(flat::Flat::from(palette)),
WorldGenTypes::MountainIslands => {
Arc::new(mountain_islands::MountainIslands::from(palette))
Arc::new(mountain_islands::MountainIslands::new(seed, palette))
}
WorldGenTypes::SingleBlock => Arc::new(single_block::SingleBlock::from(palette)),
}
Expand Down
42 changes: 14 additions & 28 deletions crates/extras/src/worldgen/mountain_islands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,8 @@ pub struct MountainIslands {
stone: MappedBlockID,
}

impl From<Palette> for MountainIslands {
fn from(palette: Palette) -> Self {
let mut wgen = Self::default();
tracing::debug!(?wgen.heightmap.octaves, wgen.heightmap.frequency, wgen.heightmap.lacunarity, wgen.heightmap.persistence, "MountainIslands initialized");

wgen.water = *palette.inner.get(WATER_BLOCK_ID).unwrap();
wgen.snow = *palette.inner.get(SNOW_BLOCK_ID).unwrap();
wgen.gravel = *palette.inner.get(GRAVEL_BLOCK_ID).unwrap();
wgen.sand = *palette.inner.get(SAND_BLOCK_ID).unwrap();
wgen.dirt = *palette.inner.get(DIRT_BLOCK_ID).unwrap();
wgen.grass = *palette.inner.get(GRASS_BLOCK_ID).unwrap();
wgen.stone = *palette.inner.get(STONE_BLOCK_ID).unwrap();
wgen
}
}

impl Default for MountainIslands {
fn default() -> Self {
let seed = 0;
impl MountainIslands {
pub fn new(seed: u32, palette: Palette) -> Self {
let vspline = Spline::from_vec(vec![
Key::new(-1., 0.6, Interpolation::Cosine),
Key::new(-0.9, 0.7, Interpolation::Cosine),
Expand All @@ -60,22 +43,25 @@ impl Default for MountainIslands {
Key::new(0.9, 1., Interpolation::Cosine),
Key::new(1.1, 1.5, Interpolation::default()), // this last one must be strictly greater than 1 because sometime we may sample with exactly the value 1.
]);
Self {

let wgen = Self {
heightmap: default_heightmap(seed),
verticality: Perlin::new(seed),
terrain_variance: default_terrain_variance(seed),
vspline,
vertical_scale: CHUNK_SIZE_F64 * 4.,
horizontal_smoothness: CHUNK_SIZE_F64 * 0.1,

water: 1.try_into().unwrap(),
snow: 2.try_into().unwrap(),
gravel: 3.try_into().unwrap(),
sand: 4.try_into().unwrap(),
dirt: 5.try_into().unwrap(),
grass: 6.try_into().unwrap(),
stone: 7.try_into().unwrap(),
}
water: *palette.inner.get(WATER_BLOCK_ID).unwrap(),
snow: *palette.inner.get(SNOW_BLOCK_ID).unwrap(),
gravel: *palette.inner.get(GRAVEL_BLOCK_ID).unwrap(),
sand: *palette.inner.get(SAND_BLOCK_ID).unwrap(),
dirt: *palette.inner.get(DIRT_BLOCK_ID).unwrap(),
grass: *palette.inner.get(GRASS_BLOCK_ID).unwrap(),
stone: *palette.inner.get(STONE_BLOCK_ID).unwrap(),
};
tracing::debug!(?wgen.heightmap.octaves, wgen.heightmap.frequency, wgen.heightmap.lacunarity, wgen.heightmap.persistence, "MountainIslands initialized");
wgen
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/infinigen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ impl Plugin for AppPlugin {
fn build(&self, app: &mut App) {
tracing::info!("Initializing app plugin with config: {:#?}", self.settings);
let world_setting = self.settings.world.clone();
let seed = self.settings.seed.to_owned() as u32;
let world = Box::new(move |palette| {
let world_gen_type = WorldGenTypes::from_str(&world_setting).unwrap_or_else(|_| {
panic!("couldn't parse world gen type from {}", &world_setting)
});
world_gen_type.as_world_gen(palette)
world_gen_type.as_world_gen(seed, palette)
});
app.init_state::<AppState>()
.insert_resource(CameraSettings {
Expand Down
4 changes: 4 additions & 0 deletions crates/infinigen/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct AppSettings {
pub rotation_z: f64,
#[serde(default)]
pub rotation_w: f64,

#[serde(default)]
pub seed: u64,
}

impl Default for AppSettings {
Expand All @@ -43,6 +46,7 @@ impl Default for AppSettings {
rotation_y: -0.9,
rotation_z: -0.4,
rotation_w: 0.18,
seed: 0,
}
}
}

0 comments on commit c760aab

Please sign in to comment.