fdm_toolkit/
world.rs

1use crate::collectable::{CollectableType, Collectable};
2use crate::chunk::Chunk;
3
4use serde_derive::{Deserialize, Serialize};
5
6use core::fmt::{Formatter, Display, Result as FmtResult};
7use std::collections::HashMap;
8use core::default::Default;
9use core::cmp::PartialEq;
10use core::convert::Into;
11
12
13
14
15
16#[non_exhaustive]
17#[derive(Deserialize, PartialEq, Serialize, Clone, Debug, Copy, Hash, Eq)]
18#[serde(rename_all="snake_case")]
19#[repr(u8)]
20pub enum Block {
21	/// Air.
22	Air,
23	/// Grass – a generic block of soil with green grass on top, used as the floor for most of the world.
24	Grass,
25	/// Dirt – a generic block of soil which can be found under Grass.
26	Dirt,
27	/// Stone – a generic block of (cobbled) stone which can be found under Dirt.
28	Stone,
29	/// Wood – a generic block to be used as a tree-trunk in non-Midnight biomes.
30	Wood,
31	///
32	Leaf,
33	/// Lava – a (purely decorative) placeholder block found at the bottom of the world.
34	Lava,
35	///
36	IronOre,
37	/// Deadly Ore – a glowing ore which produces the most valuable resource, Deadly Bars.
38	DeadlyOre,
39	/// Chest – a block that can store items.
40	Chest,
41	/// Midnight Grass – the Midnight biome's variant of [`Block::Grass`].
42	MidnightGrass,
43	///
44	MidnightSoil,
45	///
46	MidnightStone,
47	///
48	MidnightWood,
49	///
50	MidnightLeaf,
51	///
52	Bush,
53	///
54	MidnightBush,
55	/// A generic red flower.
56	RedFlower,
57	/// A generic white flower.
58	WhiteFlower,
59	/// A generic blue flower.
60	BlueFlower,
61	///
62	TallGrass,
63	/// Sand – a generic block of sand, used as the floor for the Desert biome.
64	Sand,
65	/// Sandstone – a generic block of (cobbled) sandstone which can be found under Sand.
66	Sandstone,
67	///
68	Cactus,
69	///
70	Snow,
71	///
72	Ice,
73	/// Snowy Bush – the Snow biome's variant of [`Block::Bush`].
74	SnowyBush,
75	/// Glass – a generic, see-through, block of glass crafted from Sand and Wood.
76	Glass,
77	///
78	SolenoidOre,
79	///
80	SnowyLeaf,
81	/// Pumpkin – a naturally, but infrequently, occurring block in grasslands.
82	Pumpkin,
83	///
84	JackOLantern,
85	/// Barrier – a special block which, presumably, represents an impassible block
86	Barrier,
87	/// A special block which represents the border of a chunk.
88	ChunkBorder
89}
90
91impl Block {
92	#[inline(always)] pub const fn as_str(&self) -> &'static str {
93		match self {
94			Self::MidnightGrass => "Midnight Grass",
95			Self::MidnightStone => "Midnight Stone",
96			Self::JackOLantern  => "Jack o'Lantern",
97			Self::MidnightBush  => "Midnight Bush",
98			Self::MidnightLeaf  => "Midnight Leaf",
99			Self::MidnightSoil  => "Midnight Soil",
100			Self::MidnightWood  => "Midnight Wood",
101			Self::SolenoidOre   => "Solenoid Ore",
102			Self::WhiteFlower   => "White Flower",
103			Self::BlueFlower    => "Blue Flower",
104			Self::DeadlyOre     => "Deadly Ore",
105			Self::RedFlower     => "Red Flower",
106			Self::Sandstone     => "Sandstone",
107			Self::SnowyBush     => "Snowy Bush",
108			Self::SnowyLeaf     => "Snowy Leaf",
109			Self::TallGrass     => "Tall Grass",
110			Self::Barrier       => "Barrier",
111			Self::IronOre       => "Iron Ore",
112			Self::Pumpkin       => "Pumpkin",
113			Self::Cactus        => "Cactus",
114			Self::Chest         => "Chest",
115			Self::Glass         => "Glass",
116			Self::Grass         => "Grass",
117			Self::Stone         => "Stone",
118			Self::Bush          => "Bush",
119			Self::Dirt          => "Dirt",
120			Self::Lava          => "Lava",
121			Self::Leaf          => "Leaf",
122			Self::Sand          => "Sand",
123			Self::Snow          => "Snow",
124			Self::Wood          => "Wood",
125			Self::Air           => "Air",
126			Self::Ice           => "Ice",
127			
128			_ => ""
129		}
130	}
131}
132
133impl Collectable for Block {
134	fn name(&self) -> &str { self.as_str() }
135	#[inline(always)] fn typ(&self)  -> CollectableType { CollectableType::Block }
136}
137
138impl PartialEq<u8> for Block {
139	fn eq(&self, other:&u8) -> bool { *self as u8 == *other }
140}
141
142impl Default for Block {
143	#[inline(always)] fn default() -> Self { Self::Air }
144}
145
146impl Display for Block {
147	fn fmt(&self, f:&mut Formatter) -> FmtResult { f.write_str(self.as_str()) }
148}
149
150impl Into<u8> for Block {
151	fn into(self) -> u8 { self as u8 }
152}
153
154
155
156/// A world.
157#[derive(PartialEq, Clone, Debug, Eq)]
158#[repr(transparent)]
159pub struct World(HashMap<(i64, i64, i64), Chunk>);
160
161impl World {
162	/// The size of the (whole) world along the Y axis.
163	pub const HEIGHT:usize = 128;
164}
165
166
167
168
169
170impl PartialEq<Block> for u8 {
171	fn eq(&self, other:&Block) -> bool { *self == *other as u8 }
172}