-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from DaniD3v/master
Superflat Worldgeneration
- Loading branch information
Showing
20 changed files
with
812 additions
and
277 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// TODO make this work with the protocol | ||
#[derive(Serialize, Deserialize, Clone, Copy)] | ||
#[non_exhaustive] | ||
pub enum Biome { | ||
Plains, | ||
// TODO list all Biomes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::collections::HashMap; | ||
|
||
use serde::Deserialize; | ||
|
||
use super::block_registry::BLOCKS; | ||
use crate::level::WorldError; | ||
|
||
// 0 is air -> reasonable default | ||
#[derive(Default, Deserialize, Debug, Hash, Clone, Copy, PartialEq, Eq)] | ||
#[serde(transparent)] | ||
pub struct BlockId { | ||
data: u16, | ||
} | ||
|
||
impl BlockId { | ||
pub const AIR: Self = Self::from_id(0); | ||
|
||
pub fn new( | ||
text_id: &str, | ||
properties: Option<&HashMap<String, String>>, | ||
) -> Result<Self, WorldError> { | ||
let mut block_states = BLOCKS | ||
.get(text_id) | ||
.ok_or(WorldError::BlockIdentifierNotFound)? | ||
.states | ||
.iter(); | ||
|
||
let block_state = match properties { | ||
Some(properties) => match block_states.find(|state| &state.properties == properties) { | ||
Some(state) => state, | ||
None => return Err(WorldError::BlockStateIdNotFound), | ||
}, | ||
None => block_states | ||
.find(|state| state.is_default) | ||
.expect("Every Block should have at least 1 default state"), | ||
}; | ||
|
||
Ok(block_state.id) | ||
} | ||
|
||
pub const fn from_id(id: u16) -> Self { | ||
// TODO: add check if the id is actually valid | ||
Self { data: id } | ||
} | ||
|
||
pub fn is_air(&self) -> bool { | ||
self.data == 0 || self.data == 12959 || self.data == 12958 | ||
} | ||
|
||
pub fn get_id(&self) -> u16 { | ||
self.data | ||
} | ||
|
||
/// An i32 is the way mojang internally represents their Blocks | ||
pub fn get_id_mojang_repr(&self) -> i32 { | ||
self.data as i32 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,52 @@ | ||
use std::collections::HashMap; | ||
|
||
use lazy_static::lazy_static; | ||
use serde::Deserialize; | ||
|
||
use crate::level::WorldError; | ||
use super::block_id::BlockId; | ||
|
||
const BLOCKS_JSON: &str = include_str!("../../assets/blocks.json"); | ||
lazy_static! { | ||
pub static ref BLOCKS: HashMap<String, RegistryBlockType> = | ||
serde_json::from_str(include_str!("../../assets/blocks.json")) | ||
.expect("Could not parse block.json registry."); | ||
} | ||
|
||
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct BlockDefinition { | ||
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct RegistryBlockDefinition { | ||
/// e.g. minecraft:door or minecraft:button | ||
#[serde(rename = "type")] | ||
kind: String, | ||
block_set_type: Option<String>, | ||
} | ||
pub category: String, | ||
|
||
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct BlockState { | ||
default: Option<bool>, | ||
id: i64, | ||
properties: Option<HashMap<String, String>>, | ||
/// Specifies the variant of the blocks category. | ||
/// e.g. minecraft:iron_door has the variant iron | ||
#[serde(rename = "block_set_type")] | ||
pub variant: Option<String>, | ||
} | ||
|
||
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct BlocksElement { | ||
definition: BlockDefinition, | ||
properties: Option<HashMap<String, Vec<String>>>, | ||
states: Vec<BlockState>, | ||
} | ||
/// One possible state of a Block. | ||
/// This could e.g. be an extended piston facing left. | ||
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct RegistryBlockState { | ||
pub id: BlockId, | ||
|
||
lazy_static! { | ||
pub static ref BLOCKS: HashMap<String, BlocksElement> = | ||
serde_json::from_str(BLOCKS_JSON).expect("Could not parse block.json registry."); | ||
/// Whether this is the default state of the Block | ||
#[serde(default, rename = "default")] | ||
pub is_default: bool, | ||
|
||
/// The propertise active for this `BlockState`. | ||
#[serde(default)] | ||
pub properties: HashMap<String, String>, | ||
} | ||
|
||
pub fn block_id_and_properties_to_block_state_id( | ||
block_id: &str, | ||
properties: Option<&HashMap<String, String>>, | ||
) -> Result<i64, WorldError> { | ||
let block = match BLOCKS.get(block_id) { | ||
Some(block) => block, | ||
None => return Err(WorldError::BlockStateIdNotFound), | ||
}; | ||
let block_state_id = match properties { | ||
None => Ok(block | ||
.states | ||
.iter() | ||
.find(|state| state.default.unwrap_or(false)) | ||
.expect("Each block should have at least one default state") | ||
.id), | ||
Some(properties) => block | ||
.states | ||
.iter() | ||
.find(|state| state.properties.as_ref() == Some(properties)) | ||
.map(|state| state.id) | ||
.ok_or(WorldError::BlockStateIdNotFound), | ||
}; | ||
block_state_id | ||
/// A fully-fledged block definition. | ||
/// Stores the category, variant, all of the possible states and all of the possible properties. | ||
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct RegistryBlockType { | ||
pub definition: RegistryBlockDefinition, | ||
pub states: Vec<RegistryBlockState>, | ||
|
||
// TODO is this safe to remove? It's currently not used in the Project. @lukas0008 @Snowiiii | ||
/// A list of valid property keys/values for a block. | ||
#[serde(default, rename = "properties")] | ||
valid_properties: HashMap<String, Vec<String>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.