Skip to content

Commit

Permalink
Simplify chunk types
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshiew committed Jan 8, 2025
1 parent ec312be commit dd2168f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 45 deletions.
22 changes: 0 additions & 22 deletions crates/common/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@ pub const CHUNK_SIZE_I32: i32 = CHUNK_SIZE as i32;
pub const CHUNK_SIZE_F32: f32 = CHUNK_SIZE as f32;
pub const CHUNK_SIZE_F64: f64 = CHUNK_SIZE as f64;

#[derive(Default, Clone)]
pub enum Chunk {
#[default]
Empty,
Array3(Box<Array3Chunk>),
}

impl fmt::Debug for Chunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Chunk::Empty => write!(f, "Chunk::Empty"),
Chunk::Array3(_) => write!(f, "Chunk::Array3"),
}
}
}

impl From<Array3Chunk> for Chunk {
fn from(chunk: Array3Chunk) -> Self {
Chunk::Array3(Box::new(chunk))
}
}

/// Chunk represented as a 3D array of [`MappedBlockID`].
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Array3Chunk {
Expand Down
5 changes: 3 additions & 2 deletions crates/common/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::num::NonZeroU8;
use linearize::Linearize;
use strum::EnumIter;

use crate::chunks::{Chunk, CHUNK_SIZE, CHUNK_SIZE_F32, CHUNK_SIZE_I32};
use crate::chunks::{Array3Chunk, CHUNK_SIZE, CHUNK_SIZE_F32, CHUNK_SIZE_I32};
use crate::zoom::ZoomLevel;

/// Chunks work with [`MappedBlockID`]s (u8s), which correspond to [`crate::blocks::BlockID`]s (strings).
Expand Down Expand Up @@ -38,7 +38,8 @@ impl MappedBlockID {
}

pub trait WorldGen {
fn get(&self, pos: &ChunkPosition, zoom_level: ZoomLevel) -> Chunk;
/// Should return `None` for empty chunks.
fn get(&self, pos: &ChunkPosition, zoom_level: ZoomLevel) -> Option<Array3Chunk>;
}

/// Position of a block within a chunk.
Expand Down
8 changes: 4 additions & 4 deletions crates/extras/src/worldgen/flat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use infinigen_common::blocks::Palette;
use infinigen_common::chunks::Chunk;
use infinigen_common::chunks::Array3Chunk;
use infinigen_common::world::{ChunkPosition, MappedBlockID, WorldGen};
use infinigen_common::zoom::ZoomLevel;

Expand All @@ -20,12 +20,12 @@ impl From<Palette> for Flat {
}

impl WorldGen for Flat {
fn get(&self, pos: &ChunkPosition, _zoom_level: ZoomLevel) -> Chunk {
fn get(&self, pos: &ChunkPosition, _zoom_level: ZoomLevel) -> Option<Array3Chunk> {
// zoom level does not change anything
if pos.y == -1 {
infinigen_common::chunks::top_chunk(self.dirt).into()
Some(infinigen_common::chunks::top_chunk(self.dirt))
} else {
Chunk::Empty
None
}
}
}
12 changes: 6 additions & 6 deletions crates/extras/src/worldgen/mountain_islands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use infinigen_common::blocks::Palette;
use infinigen_common::chunks::{Array3Chunk, Chunk, CHUNK_SIZE, CHUNK_SIZE_F64, CHUNK_USIZE};
use infinigen_common::chunks::{Array3Chunk, CHUNK_SIZE, CHUNK_SIZE_F64, CHUNK_USIZE};
use infinigen_common::world::{
BlockPosition, ChunkPosition, MappedBlockID, WorldGen, WorldPosition,
};
Expand Down Expand Up @@ -81,9 +81,9 @@ const MIN_Y_HEIGHT: i32 = -6;

/// Based on <https://www.youtube.com/watch?v=CSa5O6knuwI>
impl WorldGen for MountainIslands {
fn get(&self, pos: &ChunkPosition, zoom_level: ZoomLevel) -> Chunk {
fn get(&self, pos: &ChunkPosition, zoom_level: ZoomLevel) -> Option<Array3Chunk> {
if pos.y < MIN_Y_HEIGHT {
return Chunk::Empty;
return None;
}
let zoom = zoom_level.as_f64();
let sand_level = (SEA_LEVEL + (1. / zoom)).floor();
Expand Down Expand Up @@ -134,7 +134,7 @@ impl WorldGen for MountainIslands {
}
}
if is_empty {
return Chunk::Empty;
return None;
}

let mut terrain_variances = [[0.; CHUNK_USIZE]; CHUNK_USIZE];
Expand Down Expand Up @@ -193,10 +193,10 @@ impl WorldGen for MountainIslands {
}

if is_empty {
return Chunk::Empty;
return None;
}
}

chunk.into()
Some(chunk)
}
}
6 changes: 3 additions & 3 deletions crates/extras/src/worldgen/single_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use infinigen_common::blocks::Palette;
use infinigen_common::chunks::{Array3Chunk, Chunk, CHUNK_SIZE};
use infinigen_common::chunks::{Array3Chunk, CHUNK_SIZE};
use infinigen_common::world::{BlockPosition, ChunkPosition, MappedBlockID, WorldGen};
use infinigen_common::zoom::ZoomLevel;

Expand All @@ -20,7 +20,7 @@ impl From<Palette> for SingleBlock {
}

impl WorldGen for SingleBlock {
fn get(&self, _pos: &ChunkPosition, _zoom_level: ZoomLevel) -> Chunk {
fn get(&self, _pos: &ChunkPosition, _zoom_level: ZoomLevel) -> Option<Array3Chunk> {
// TODO: implement zoom?
let mut chunk = Array3Chunk::default();
chunk.insert(
Expand All @@ -31,6 +31,6 @@ impl WorldGen for SingleBlock {
},
self.grass,
);
chunk.into()
Some(chunk)
}
}
8 changes: 4 additions & 4 deletions crates/plugins/src/world/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ahash::AHashMap;
use bevy::prelude::*;
use bevy::tasks::{block_on, poll_once, AsyncComputeTaskPool, Task};
use infinigen_common::blocks::BlockVisibility;
use infinigen_common::chunks::{Array3Chunk, Chunk, CHUNK_SIZE};
use infinigen_common::chunks::{Array3Chunk, CHUNK_SIZE};
use infinigen_common::mesh::faces::{extract_faces, BlockVisibilityChecker};
use infinigen_common::world::{BlockPosition, ChunkPosition, MappedBlockID, WorldGen};
use infinigen_common::zoom::ZoomLevel;
Expand Down Expand Up @@ -72,16 +72,16 @@ pub fn generate_chunk_async(
let position = position.to_owned();
task_pool.spawn(async move {
let chunk = worldgen.get(&position, zoom_level);
let Chunk::Array3(mut chunk) = chunk else {
let Some(mut chunk) = chunk else {
return (zoom_level, position, None);
};
let faces = extract_faces(chunk.as_ref(), *visibility_checker.clone());
let faces = extract_faces(&chunk, *visibility_checker.clone());
let translucents = split_out_translucent(&mut chunk, *visibility_checker);
(
zoom_level,
position,
Some(ChunkInfo {
opaque: chunk,
opaque: Box::new(chunk),
faces,
translucents: translucents.into_values().collect(),
}),
Expand Down
8 changes: 4 additions & 4 deletions crates/plugins/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ahash::AHashMap;
use bevy::prelude::*;
use events::{GenerateChunkRequest, GenerateChunkTask};
use infinigen_common::blocks::Palette;
use infinigen_common::chunks::{Array3Chunk, Chunk};
use infinigen_common::chunks::Array3Chunk;
use infinigen_common::mesh::shapes::ChunkFace;
use infinigen_common::world::{ChunkPosition, Direction, WorldGen};
use infinigen_common::zoom::ZoomLevel;
Expand Down Expand Up @@ -38,8 +38,8 @@ impl Default for World {
fn default() -> Self {
struct Empty;
impl WorldGen for Empty {
fn get(&self, _pos: &ChunkPosition, _zoom_level: ZoomLevel) -> Chunk {
Chunk::Empty
fn get(&self, _pos: &ChunkPosition, _zoom_level: ZoomLevel) -> Option<Array3Chunk> {
None
}
}
Self {
Expand All @@ -50,7 +50,7 @@ impl Default for World {
}

impl World {
pub fn generate(&self, zoom_level: ZoomLevel, pos: &ChunkPosition) -> Chunk {
pub fn generate(&self, zoom_level: ZoomLevel, pos: &ChunkPosition) -> Option<Array3Chunk> {
self.generator.get(pos, zoom_level)
}
}
Expand Down

0 comments on commit dd2168f

Please sign in to comment.