1#![doc = env!("CARGO_PKG_DESCRIPTION")]
2#![warn(missing_docs)]
3#![warn(clippy::missing_docs_in_private_items)]
4
5mod biomes;
6mod block_color;
7mod block_types;
8mod legacy_biomes;
9mod legacy_block_types;
10
11use std::collections::HashMap;
12
13use bincode::{BorrowDecode, Decode, Encode};
14use enumflags2::{BitFlags, bitflags};
15
16#[bitflags]
18#[repr(u8)]
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum BlockFlag {
21 Opaque,
23 Grass,
25 Foliage,
27 Birch,
29 Spruce,
31 Water,
33 WallSign,
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
42pub struct Color(pub [u8; 3]);
43
44pub type Colorf = glam::Vec3;
46
47#[derive(Debug, Clone, Copy)]
49pub struct BlockColor {
50 pub flags: BitFlags<BlockFlag>,
52 pub color: Color,
54}
55
56impl BlockColor {
57 #[inline]
59 pub fn is(&self, flag: BlockFlag) -> bool {
60 self.flags.contains(flag)
61 }
62}
63
64impl Encode for BlockColor {
65 fn encode<E: bincode::enc::Encoder>(
66 &self,
67 encoder: &mut E,
68 ) -> Result<(), bincode::error::EncodeError> {
69 bincode::Encode::encode(&self.flags.bits(), encoder)?;
70 bincode::Encode::encode(&self.color, encoder)?;
71 Ok(())
72 }
73}
74
75impl<Context> Decode<Context> for BlockColor {
76 fn decode<D: bincode::de::Decoder<Context = Context>>(
77 decoder: &mut D,
78 ) -> Result<Self, bincode::error::DecodeError> {
79 Ok(BlockColor {
80 flags: BitFlags::from_bits(bincode::Decode::decode(decoder)?).or(Err(
81 bincode::error::DecodeError::Other("invalid block flags"),
82 ))?,
83 color: bincode::Decode::decode(decoder)?,
84 })
85 }
86}
87
88impl<'de, Context> BorrowDecode<'de, Context> for BlockColor {
89 fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = Context>>(
90 decoder: &mut D,
91 ) -> Result<Self, bincode::error::DecodeError> {
92 Ok(BlockColor {
93 flags: BitFlags::from_bits(bincode::BorrowDecode::borrow_decode(decoder)?).or(Err(
94 bincode::error::DecodeError::Other("invalid block flags"),
95 ))?,
96 color: bincode::BorrowDecode::borrow_decode(decoder)?,
97 })
98 }
99}
100
101#[derive(Debug, Clone)]
103struct ConstBlockType {
104 pub block_color: BlockColor,
106 pub sign_material: Option<&'static str>,
108}
109
110#[derive(Debug, Clone)]
112pub struct BlockType {
113 pub block_color: BlockColor,
115 pub sign_material: Option<String>,
117}
118
119impl From<&ConstBlockType> for BlockType {
120 fn from(value: &ConstBlockType) -> Self {
121 BlockType {
122 block_color: value.block_color,
123 sign_material: value.sign_material.map(String::from),
124 }
125 }
126}
127
128#[derive(Debug)]
130pub struct BlockTypes {
131 block_type_map: HashMap<String, BlockType>,
133 legacy_block_types: Box<[[BlockType; 16]; 256]>,
135}
136
137impl Default for BlockTypes {
138 fn default() -> Self {
139 let block_type_map: HashMap<_, _> = block_types::BLOCK_TYPES
140 .iter()
141 .map(|(k, v)| (String::from(*k), BlockType::from(v)))
142 .collect();
143 let legacy_block_types = Box::new(legacy_block_types::LEGACY_BLOCK_TYPES.map(|inner| {
144 inner.map(|id| {
145 block_type_map
146 .get(id)
147 .expect("Unknown legacy block type")
148 .clone()
149 })
150 }));
151
152 BlockTypes {
153 block_type_map,
154 legacy_block_types,
155 }
156 }
157}
158
159impl BlockTypes {
160 #[inline]
162 pub fn get(&self, id: &str) -> Option<&BlockType> {
163 let suffix = id.strip_prefix("minecraft:")?;
164 self.block_type_map.get(suffix)
165 }
166
167 #[inline]
169 pub fn get_legacy(&self, id: u8, data: u8) -> Option<&BlockType> {
170 Some(&self.legacy_block_types[id as usize][data as usize])
171 }
172}
173
174pub use block_color::{block_color, needs_biome};
175
176#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
178pub enum BiomeGrassColorModifier {
179 DarkForest,
181 Swamp,
183}
184
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
190pub struct Biome {
191 pub temp: i8,
196 pub downfall: i8,
201 pub water_color: Option<Color>,
203 pub foliage_color: Option<Color>,
205 pub grass_color: Option<Color>,
207 pub grass_color_modifier: Option<BiomeGrassColorModifier>,
209}
210
211impl Biome {
212 const fn new(temp: i16, downfall: i16) -> Biome {
214 const fn encode(v: i16) -> i8 {
219 (v / 5) as i8
220 }
221 Biome {
222 temp: encode(temp),
223 downfall: encode(downfall),
224 grass_color_modifier: None,
225 water_color: None,
226 foliage_color: None,
227 grass_color: None,
228 }
229 }
230
231 const fn water(self, water_color: [u8; 3]) -> Biome {
233 Biome {
234 water_color: Some(Color(water_color)),
235 ..self
236 }
237 }
238
239 const fn foliage(self, foliage_color: [u8; 3]) -> Biome {
241 Biome {
242 foliage_color: Some(Color(foliage_color)),
243 ..self
244 }
245 }
246
247 const fn grass(self, grass_color: [u8; 3]) -> Biome {
249 Biome {
250 grass_color: Some(Color(grass_color)),
251 ..self
252 }
253 }
254
255 const fn modify(self, grass_color_modifier: BiomeGrassColorModifier) -> Biome {
257 Biome {
258 grass_color_modifier: Some(grass_color_modifier),
259 ..self
260 }
261 }
262
263 fn decode(val: i8) -> f32 {
266 f32::from(val) / 20.0
267 }
268
269 pub fn temp(&self) -> f32 {
271 Self::decode(self.temp)
272 }
273
274 pub fn downfall(&self) -> f32 {
276 Self::decode(self.downfall)
277 }
278}
279
280#[derive(Debug)]
282pub struct BiomeTypes {
283 biome_map: HashMap<String, &'static Biome>,
285 legacy_biomes: Box<[&'static Biome; 256]>,
287 fallback_biome: &'static Biome,
289}
290
291impl Default for BiomeTypes {
292 fn default() -> Self {
293 let mut biome_map: HashMap<_, _> = biomes::BIOMES
294 .iter()
295 .map(|(k, v)| (String::from(*k), v))
296 .collect();
297
298 for &(old, new) in legacy_biomes::BIOME_ALIASES.iter().rev() {
299 let biome = biome_map
300 .get(new)
301 .copied()
302 .expect("Biome alias for unknown biome");
303 assert!(biome_map.insert(String::from(old), biome).is_none());
304 }
305
306 let legacy_biomes = (0..=255)
307 .map(|index| {
308 let id = legacy_biomes::legacy_biome(index);
309 *biome_map.get(id).expect("Unknown legacy biome")
310 })
311 .collect::<Box<[_]>>()
312 .try_into()
313 .unwrap();
314
315 let fallback_biome = *biome_map.get("plains").expect("Plains biome undefined");
316
317 Self {
318 biome_map,
319 legacy_biomes,
320 fallback_biome,
321 }
322 }
323}
324
325impl BiomeTypes {
326 #[inline]
328 pub fn get(&self, id: &str) -> Option<&Biome> {
329 let suffix = id.strip_prefix("minecraft:")?;
330 self.biome_map.get(suffix).copied()
331 }
332
333 #[inline]
335 pub fn get_legacy(&self, id: u8) -> Option<&Biome> {
336 Some(self.legacy_biomes[id as usize])
337 }
338
339 #[inline]
341 pub fn get_fallback(&self) -> &Biome {
342 self.fallback_biome
343 }
344}