Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compile time BlockState #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions pumpkin-macros/src/block_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::{collections::HashMap, sync::LazyLock};

use quote::quote;
use serde::Deserialize;

static BLOCKS: LazyLock<TopLevel> = LazyLock::new(|| {
serde_json::from_str(include_str!("../../assets/blocks.json"))
.expect("Could not parse blocks.json registry.")
});

static STATE_BY_REGISTRY_ID: LazyLock<HashMap<String, Block>> = LazyLock::new(|| {
let mut map = HashMap::new();
for block in &BLOCKS.blocks {
map.insert(block.name.clone(), block.clone());
}
map
});

pub(crate) fn block_state_impl(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input_string = item.to_string();
let registry_id = input_string.trim_matches('"');

let default_state_id = STATE_BY_REGISTRY_ID
.get(registry_id)
.expect("Invalid registry id")
.default_state_id;

if std::env::var("CARGO_PKG_NAME").unwrap() == "pumpkin-world" {
quote! {
crate::block::BlockState {
state_id: #default_state_id
}
}
.into()
} else {
quote! {
pumpkin_world::block::BlockState {
state_id: #default_state_id
}
}
.into()
}
}

#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct TopLevel {
block_entity_types: Vec<String>,
shapes: Vec<Shape>,
pub blocks: Vec<Block>,
}

#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct Block {
pub id: u16,
pub item_id: u16,
pub hardness: f32,
pub wall_variant_id: Option<u16>,
pub translation_key: String,
pub name: String,
pub properties: Vec<Property>,
pub default_state_id: u16,
pub states: Vec<State>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
pub struct Property {
name: String,
values: Vec<String>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
pub struct State {
pub id: u16,
pub air: bool,
pub luminance: u8,
pub burnable: bool,
pub opacity: Option<u32>,
pub replaceable: bool,
pub collision_shapes: Vec<u16>,
pub block_entity_type: Option<u32>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct Shape {
min: [f32; 3],
max: [f32; 3],
}
6 changes: 6 additions & 0 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ mod particle;
pub fn particle(item: TokenStream) -> TokenStream {
particle::particle_impl(item)
}

mod block_state;
#[proc_macro]
pub fn block_state(item: TokenStream) -> TokenStream {
block_state::block_state_impl(item)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::ops::Add;

use pumpkin_macros::block_state;

use crate::{block::BlockState, coordinates::ChunkRelativeBlockCoordinates};

pub mod plains;
Expand All @@ -25,7 +27,7 @@ pub fn generate_tree(
y: (chunk_relative_coordinates.y.add(y as i16)).into(),
z: z.add(dz as u8).into(),
};
tree_blocks.push((block_coordinates, BlockState::new("oak_log").unwrap()));
tree_blocks.push((block_coordinates, block_state!("oak_log")));
}
}
}
Expand All @@ -40,7 +42,7 @@ pub fn generate_tree(
y: (chunk_relative_coordinates.y.add(y as i16)).into(),
z: z.add(dz as u8).into(),
};
tree_blocks.push((block_coordinates, BlockState::new("oak_leaves").unwrap()));
tree_blocks.push((block_coordinates, block_state!("oak_leaves")));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use noise::Perlin;
use pumpkin_core::math::vector2::Vector2;
use pumpkin_macros::block_state;
use rand::Rng;

use crate::{
biome::Biome,
block::BlockState,
chunk::ChunkBlocks,
coordinates::{BlockCoordinates, ChunkRelativeBlockCoordinates, XZBlockCoordinates},
world_gen::{
Expand Down Expand Up @@ -55,16 +55,13 @@ impl PerlinTerrainGenerator for PlainsTerrainGenerator {

let y = *at.y;
if y == -64 {
blocks.set_block(coordinates, BlockState::new("bedrock").unwrap().state_id);
blocks.set_block(coordinates, block_state!("bedrock").state_id);
} else if y >= -63 && y <= begin_stone_height {
blocks.set_block(coordinates, BlockState::new("stone").unwrap().state_id);
blocks.set_block(coordinates, block_state!("stone").state_id);
} else if y >= begin_stone_height && y < begin_dirt_height {
blocks.set_block(coordinates, BlockState::new("dirt").unwrap().state_id);
blocks.set_block(coordinates, block_state!("dirt").state_id);
} else if y == chunk_height - 2 {
blocks.set_block(
coordinates,
BlockState::new("grass_block").unwrap().state_id,
);
blocks.set_block(coordinates, block_state!("grass_block").state_id);
} else if y == chunk_height - 1 {
// TODO: generate flowers and grass
let grass: u8 = rand::thread_rng().gen_range(0..7);
Expand All @@ -73,40 +70,24 @@ impl PerlinTerrainGenerator for PlainsTerrainGenerator {
if flower == 6 {
match rand::thread_rng().gen_range(0..4) {
0 => {
blocks.set_block(
coordinates,
BlockState::new("dandelion").unwrap().state_id,
);
blocks.set_block(coordinates, block_state!("dandelion").state_id);
}
1 => {
blocks.set_block(
coordinates,
BlockState::new("oxeye_daisy").unwrap().state_id,
);
blocks.set_block(coordinates, block_state!("oxeye_daisy").state_id);
}
2 => {
blocks.set_block(
coordinates,
BlockState::new("cornflower").unwrap().state_id,
);
blocks.set_block(coordinates, block_state!("cornflower").state_id);
}
3 => {
blocks
.set_block(coordinates, BlockState::new("poppy").unwrap().state_id);
blocks.set_block(coordinates, block_state!("poppy").state_id);
}
_ => {
blocks.set_block(
coordinates,
BlockState::new("azure_bluet").unwrap().state_id,
);
blocks.set_block(coordinates, block_state!("azure_bluet").state_id);
}
}
} else {
// TODO: Tall grass, Tall grass data called `half`, There is `upper` and `lower`
blocks.set_block(
coordinates,
BlockState::new("short_grass").unwrap().state_id,
);
blocks.set_block(coordinates, block_state!("short_grass").state_id);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions pumpkin-world/src/world_gen/implementation/superflat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pumpkin_core::math::vector2::Vector2;
use pumpkin_macros::block_state;

use crate::{
biome::Biome,
Expand Down Expand Up @@ -42,9 +43,9 @@ impl TerrainGenerator for SuperflatTerrainGenerator {
// TODO allow specifying which blocks should be at which height in the config.
fn generate_block(&self, at: BlockCoordinates, _: Biome) -> BlockState {
match *at.y {
-64 => BlockState::new("bedrock").unwrap(),
-63..=-62 => BlockState::new("dirt").unwrap(),
-61 => BlockState::new("grass_block").unwrap(),
-64 => block_state!("bedrock"),
-63..=-62 => block_state!("dirt"),
-61 => block_state!("grass_block"),
_ => BlockState::AIR,
}
}
Expand Down