make generation presets use string ids

This commit is contained in:
Zoey 2024-04-19 17:54:05 -07:00
parent 21217a3597
commit 55bbb3a59c
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
4 changed files with 100 additions and 11 deletions

71
Cargo.lock generated
View file

@ -17,6 +17,24 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "allocator-api2"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.2.0" version = "1.2.0"
@ -68,6 +86,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"flate2", "flate2",
"half", "half",
"internment",
"parking_lot", "parking_lot",
"rand", "rand",
"serde", "serde",
@ -127,12 +146,32 @@ dependencies = [
"crunchy", "crunchy",
] ]
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "internment"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c615b58b58eccfd62519fa08c68512256ce31089f9ec21bb9d1d7ac237bdfaa2"
dependencies = [
"hashbrown",
"serde",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.11" version = "1.0.11"
@ -200,6 +239,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.1" version = "0.12.1"
@ -413,6 +458,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -557,3 +608,23 @@ name = "windows_x86_64_msvc"
version = "0.52.5" version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
[[package]]
name = "zerocopy"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View file

@ -6,8 +6,9 @@ version = "0.1.0"
[dependencies] [dependencies]
flate2 = "1" flate2 = "1"
half = "2" half = "2"
internment = { version = "0.8", features = ["serde"] }
parking_lot = "0.12.1" parking_lot = "0.12.1"
rand = "0.8" rand = "0.8"
serde = { version = "1", features = ["derive"] } serde = {version = "1", features = ["derive"]}
serde_json = "1" serde_json = "1"
tokio = {version = "1", features = ["full"]} tokio = {version = "1", features = ["full"]}

View file

@ -1,5 +1,7 @@
use std::{collections::BTreeMap, sync::LazyLock}; use std::{collections::BTreeMap, sync::LazyLock};
use internment::Intern;
use crate::player::PlayerType; use crate::player::PlayerType;
/// information about all blocks implemented /// information about all blocks implemented
@ -95,7 +97,7 @@ pub static BLOCK_INFO: LazyLock<BTreeMap<u8, BlockInfo>> = LazyLock::new(|| {
}); });
/// map of block string ids to their byte ids /// map of block string ids to their byte ids
pub static BLOCK_STRING_ID_MAP: LazyLock<BTreeMap<&'static str, u8>> = LazyLock::new(|| { pub static BLOCK_STRING_ID_MAP: LazyLock<BTreeMap<Intern<String>, u8>> = LazyLock::new(|| {
BLOCK_INFO BLOCK_INFO
.iter() .iter()
.map(|(id, info)| (info.str_id, *id)) .map(|(id, info)| (info.str_id, *id))
@ -106,7 +108,7 @@ pub static BLOCK_STRING_ID_MAP: LazyLock<BTreeMap<&'static str, u8>> = LazyLock:
#[derive(Debug)] #[derive(Debug)]
pub struct BlockInfo { pub struct BlockInfo {
/// the block's string id /// the block's string id
pub str_id: &'static str, pub str_id: Intern<String>,
/// the type of block /// the type of block
pub block_type: BlockType, pub block_type: BlockType,
/// permissions needed to place this block /// permissions needed to place this block
@ -117,9 +119,9 @@ pub struct BlockInfo {
impl BlockInfo { impl BlockInfo {
/// creates a new block info /// creates a new block info
pub const fn new(str_id: &'static str) -> Self { pub fn new(str_id: &'static str) -> Self {
Self { Self {
str_id, str_id: Intern::new(str_id.to_owned()),
block_type: BlockType::Solid, block_type: BlockType::Solid,
place_permissions: PlayerType::Normal, place_permissions: PlayerType::Normal,
break_permissions: PlayerType::Normal, break_permissions: PlayerType::Normal,

View file

@ -1,7 +1,8 @@
use internment::Intern;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::Level; use super::{block::BLOCK_STRING_ID_MAP, Level};
/// enum for different kinds of level generation /// enum for different kinds of level generation
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -29,7 +30,7 @@ pub enum FlatPreset {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct FlatLayer { pub struct FlatLayer {
/// the block for the layer /// the block for the layer
pub block: u8, pub block: String,
/// the depth of the layer /// the depth of the layer
pub depth: usize, pub depth: usize,
} }
@ -60,11 +61,17 @@ impl LevelGeneration {
FlatPreset::StoneAndGrass => { FlatPreset::StoneAndGrass => {
custom_layers = vec![ custom_layers = vec![
FlatLayer { FlatLayer {
block: 1, block: "stone".to_owned(),
depth: level.y_size / 2 - 4, depth: level.y_size / 2 - 4,
}, },
FlatLayer { block: 3, depth: 3 }, FlatLayer {
FlatLayer { block: 2, depth: 1 }, block: "dirt".to_owned(),
depth: 3,
},
FlatLayer {
block: "grass".to_owned(),
depth: 1,
},
]; ];
layers_ref = &custom_layers; layers_ref = &custom_layers;
} }
@ -75,10 +82,18 @@ impl LevelGeneration {
let mut y = 0; let mut y = 0;
for layer in layers_ref { for layer in layers_ref {
let block = Intern::new(layer.block.to_string());
for _ in 0..layer.depth { for _ in 0..layer.depth {
for x in 0..level.x_size { for x in 0..level.x_size {
for z in 0..level.z_size { for z in 0..level.z_size {
level.set_block(x, y, z, layer.block); level.set_block(
x,
y,
z,
*BLOCK_STRING_ID_MAP
.get(&block)
.expect("missing block type!"),
);
} }
} }
y += 1; y += 1;