minedmap_resource/
lib.rs

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/// Flags describing special properties of [BlockType]s
17#[bitflags]
18#[repr(u8)]
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum BlockFlag {
21	/// The block type is opaque
22	Opaque,
23	/// The block type is colored using biome grass colors
24	Grass,
25	/// The block type is colored using biome foliage colors
26	Foliage,
27	/// The block type is birch foliage
28	Birch,
29	/// The block type is spruce foliage
30	Spruce,
31	/// The block type is colored using biome water colors
32	Water,
33	/// The block type is a wall sign
34	///
35	/// The WallSign flag is used to distinguish wall signs from
36	/// freestanding or -hanging signs.
37	WallSign,
38}
39
40/// An RGB color with u8 components
41#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
42pub struct Color(pub [u8; 3]);
43
44/// An RGB color with f32 components
45pub type Colorf = glam::Vec3;
46
47/// A block type specification
48#[derive(Debug, Clone, Copy)]
49pub struct BlockColor {
50	/// Bit set of [BlockFlag]s describing special properties of the block type
51	pub flags: BitFlags<BlockFlag>,
52	/// Base color of the block type
53	pub color: Color,
54}
55
56impl BlockColor {
57	/// Checks whether a block color has a given [BlockFlag] set
58	#[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/// A block type specification (for use in constants)
102#[derive(Debug, Clone)]
103struct ConstBlockType {
104	/// Determines the rendered color of the block type
105	pub block_color: BlockColor,
106	/// Material of a sign block
107	pub sign_material: Option<&'static str>,
108}
109
110/// A block type specification
111#[derive(Debug, Clone)]
112pub struct BlockType {
113	/// Determines the rendered color of the block type
114	pub block_color: BlockColor,
115	/// Material of a sign block
116	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/// Used to look up standard Minecraft block types
129#[derive(Debug)]
130pub struct BlockTypes {
131	/// Map of string IDs to block types
132	block_type_map: HashMap<String, BlockType>,
133	/// Array used to look up old numeric block type and subtype values
134	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	/// Resolves a Minecraft 1.13+ string block type ID
161	#[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	/// Resolves a Minecraft pre-1.13 numeric block type ID
168	#[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/// Grass color modifier used by a biome
177#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
178pub enum BiomeGrassColorModifier {
179	/// Grass color modifier used by the dark forest biome
180	DarkForest,
181	/// Grass color modifier used by swamp biomes
182	Swamp,
183}
184
185/// A biome specification
186///
187/// A Biome contains all information about a biome necessary to compute a block
188/// color given a block type and depth
189#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
190pub struct Biome {
191	/// Temperature value
192	///
193	/// For more efficient storage, the temperature is stored as an integer
194	/// after mutiplying the raw value by 20
195	pub temp: i8,
196	/// Downfall value
197	///
198	/// For more efficient storage, the downfall is stored as an integer
199	/// after mutiplying the raw value by 20
200	pub downfall: i8,
201	/// Water color override
202	pub water_color: Option<Color>,
203	/// Foliage color override
204	pub foliage_color: Option<Color>,
205	/// Grass color override
206	pub grass_color: Option<Color>,
207	/// Grass color modifier
208	pub grass_color_modifier: Option<BiomeGrassColorModifier>,
209}
210
211impl Biome {
212	/// Constructs a new Biome
213	const fn new(temp: i16, downfall: i16) -> Biome {
214		/// Helper to encode temperature and downfall values
215		///
216		/// Converts temperatue and downfall from the input format
217		/// (mutiplied by 100) to i8 range for more efficient storage.
218		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	/// Builder function to override the biome water color
232	const fn water(self, water_color: [u8; 3]) -> Biome {
233		Biome {
234			water_color: Some(Color(water_color)),
235			..self
236		}
237	}
238
239	/// Builder function to override the biome foliage color
240	const fn foliage(self, foliage_color: [u8; 3]) -> Biome {
241		Biome {
242			foliage_color: Some(Color(foliage_color)),
243			..self
244		}
245	}
246
247	/// Builder function to override the biome grass color
248	const fn grass(self, grass_color: [u8; 3]) -> Biome {
249		Biome {
250			grass_color: Some(Color(grass_color)),
251			..self
252		}
253	}
254
255	/// Builder function to set a grass color modifier
256	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	/// Decodes a temperature or downfall value from the storage format to
264	/// f32 for further calculation
265	fn decode(val: i8) -> f32 {
266		f32::from(val) / 20.0
267	}
268
269	/// Returns the biome's temperature decoded to its original float value
270	pub fn temp(&self) -> f32 {
271		Self::decode(self.temp)
272	}
273
274	/// Returns the biome's downfall decoded to its original float value
275	pub fn downfall(&self) -> f32 {
276		Self::decode(self.downfall)
277	}
278}
279
280/// Used to look up standard Minecraft biome types
281#[derive(Debug)]
282pub struct BiomeTypes {
283	/// Map of string IDs to biome types
284	biome_map: HashMap<String, &'static Biome>,
285	/// Array used to look up old numeric biome IDs
286	legacy_biomes: Box<[&'static Biome; 256]>,
287	/// Fallback for unknown (new/modded) biomes
288	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	/// Resolves a Minecraft 1.18+ string biome type ID
327	#[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	/// Resolves a Minecraft pre-1.18 numeric biome type ID
334	#[inline]
335	pub fn get_legacy(&self, id: u8) -> Option<&Biome> {
336		Some(self.legacy_biomes[id as usize])
337	}
338
339	/// Returns the fallback for unknown (new/modded) biomes
340	#[inline]
341	pub fn get_fallback(&self) -> &Biome {
342		self.fallback_biome
343	}
344}