Skip to content

Commit

Permalink
Terrain submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
checkraisefold committed Jan 6, 2025
1 parent 6b49ae0 commit ca8bf6b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 55 deletions.
6 changes: 2 additions & 4 deletions rbx_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ mod error;
mod faces;
mod font;
mod lister;
mod material_colors;
mod physical_properties;
mod referent;
mod security_capabilities;
mod shared_string;
mod smooth_grid;
mod tags;
mod terrain;
mod unique_id;
mod variant;

Expand All @@ -31,12 +30,11 @@ pub use content::*;
pub use error::*;
pub use faces::*;
pub use font::*;
pub use material_colors::*;
pub use physical_properties::*;
pub use referent::*;
pub use security_capabilities::*;
pub use shared_string::*;
pub use smooth_grid::*;
pub use tags::*;
pub use terrain::*;
pub use unique_id::*;
pub use variant::*;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::Error as CrateError;
pub struct MaterialColors {
/// The underlying map used by this struct. A `BTreeMap` is used
/// over a `HashMap` to ensure serialization with serde is ordered.
inner: BTreeMap<TerrainMaterials, Color3uint8>,
inner: BTreeMap<TerrainColorMaterial, Color3uint8>,
}

impl MaterialColors {
Expand All @@ -32,7 +32,7 @@ impl MaterialColors {
/// Retrieves the set color for the given material, or the default if
/// none is set.
#[inline]
pub fn get_color(&self, material: TerrainMaterials) -> Color3uint8 {
pub fn get_color(&self, material: TerrainColorMaterial) -> Color3uint8 {
if let Some(color) = self.inner.get(&material) {
*color
} else {
Expand All @@ -42,7 +42,7 @@ impl MaterialColors {

/// Sets the color for the given material.
#[inline]
pub fn set_color(&mut self, material: TerrainMaterials, color: Color3uint8) {
pub fn set_color(&mut self, material: TerrainColorMaterial, color: Color3uint8) {
self.inner.insert(material, color);
}

Expand Down Expand Up @@ -79,7 +79,7 @@ impl MaterialColors {

impl<T> From<T> for MaterialColors
where
T: Into<BTreeMap<TerrainMaterials, Color3uint8>>,
T: Into<BTreeMap<TerrainColorMaterial, Color3uint8>>,
{
fn from(value: T) -> Self {
Self {
Expand All @@ -88,7 +88,7 @@ where
}
}

/// An error that can occur when deserializing or working with MaterialColors and TerrainMaterials.
/// An error that can occur when deserializing or working with MaterialColors and TerrainColorMaterial.
#[derive(Debug, Error)]
pub(crate) enum MaterialColorsError {
/// The `MaterialColors` blob was the wrong number of bytes.
Expand All @@ -102,7 +102,7 @@ pub(crate) enum MaterialColorsError {
UnknownMaterial(String),
}

/// Constructs an enum named `TerrainMaterials` for all values contained in
/// Constructs an enum named `TerrainColorMaterial` for all values contained in
/// `MaterialColors` alongside a mapping for a default color for that material.
///
/// Additionally, makes a constant named `MATERIAL_ORDER` that indicates what
Expand All @@ -114,23 +114,23 @@ macro_rules! material_colors {
// all have tangible downsides.
// See: https://danielkeep.github.io/tlborm/book/blk-counting.html

/// A list of all `TerrainMaterials` in the order they must be read
/// A list of all `TerrainColorMaterial` in the order they must be read
/// and written.
const MATERIAL_ORDER: [TerrainMaterials; 21] = [$(TerrainMaterials::$name,)*];
const MATERIAL_ORDER: [TerrainColorMaterial; 21] = [$(TerrainColorMaterial::$name,)*];

/// All materials that are represented by `MaterialColors`.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
)]
pub enum TerrainMaterials {
enum TerrainColorMaterial {
$(
$name,
)*
}

impl TerrainMaterials {
impl TerrainColorMaterial {
/// Returns the default color for the given `TerrainMaterial`.
pub fn default_color(&self) -> Color3uint8 {
match self {
Expand All @@ -141,7 +141,7 @@ macro_rules! material_colors {
}
}

impl FromStr for TerrainMaterials {
impl FromStr for TerrainColorMaterial {
type Err = CrateError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -193,26 +193,26 @@ mod test {
let expected: MaterialColors = serde_json::from_str(serialized).unwrap();

assert_eq!(
expected.get_color(TerrainMaterials::Grass),
expected.get_color(TerrainColorMaterial::Grass),
Color3uint8::new(10, 20, 30),
);
assert_eq!(
expected.get_color(TerrainMaterials::Mud),
expected.get_color(TerrainColorMaterial::Mud),
Color3uint8::new(255, 0, 127),
);

assert_eq!(
expected.get_color(TerrainMaterials::Brick),
TerrainMaterials::Brick.default_color()
expected.get_color(TerrainColorMaterial::Brick),
TerrainColorMaterial::Brick.default_color()
);
}

#[test]
#[cfg(feature = "serde")]
fn serialize() {
let mut colors = MaterialColors::new();
colors.set_color(TerrainMaterials::Grass, Color3uint8::new(10, 20, 30));
colors.set_color(TerrainMaterials::Mud, Color3uint8::new(255, 0, 127));
colors.set_color(TerrainColorMaterial::Grass, Color3uint8::new(10, 20, 30));
colors.set_color(TerrainColorMaterial::Mud, Color3uint8::new(255, 0, 127));

assert_eq!(
serde_json::to_string(&colors).unwrap(),
Expand Down Expand Up @@ -284,15 +284,17 @@ mod test {

#[test]
fn from_str_materials() {
assert!(TerrainMaterials::from_str("Grass").is_ok());
assert!(TerrainMaterials::from_str("Concrete").is_ok());
assert!(TerrainMaterials::from_str("Rock").is_ok());
assert!(TerrainMaterials::from_str("Asphalt").is_ok());
assert!(TerrainMaterials::from_str("Salt").is_ok());
assert!(TerrainMaterials::from_str("Pavement").is_ok());

assert!(TerrainMaterials::from_str("A name I am certain Roblox will never add").is_err());
assert!(TerrainColorMaterial::from_str("Grass").is_ok());
assert!(TerrainColorMaterial::from_str("Concrete").is_ok());
assert!(TerrainColorMaterial::from_str("Rock").is_ok());
assert!(TerrainColorMaterial::from_str("Asphalt").is_ok());
assert!(TerrainColorMaterial::from_str("Salt").is_ok());
assert!(TerrainColorMaterial::from_str("Pavement").is_ok());

assert!(
TerrainColorMaterial::from_str("A name I am certain Roblox will never add").is_err()
);
// `from_str` is case-sensitive
assert!(TerrainMaterials::from_str("gRaSs").is_err());
assert!(TerrainColorMaterial::from_str("gRaSs").is_err());
}
}
36 changes: 36 additions & 0 deletions rbx_types/src/terrain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Terrain has two binary formats, being the material colors and smooth grid blobs.
// The smooth grid spec can be found here: https://github.com/rojo-rbx/rbx-dom/blob/terrain/docs/smooth-grid.md
// The material colors spec can be found here: https://github.com/rojo-rbx/rbx-dom/blob/master/docs/binary-strings.md#materialcolors

mod material_colors;
mod smooth_grid;

pub use self::material_colors::*;
pub use self::smooth_grid::*;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum TerrainMaterials {
Air,
Water,
Grass,
Slate,
Concrete,
Brick,
Sand,
WoodPlanks,
Rock,
Glacier,
Snow,
Sandstone,
Mud,
Basalt,
Ground,
CrackedLava,
Asphalt,
Cobblestone,
Ice,
LeafyGrass,
Salt,
Limestone,
Pavement,
}
Loading

0 comments on commit ca8bf6b

Please sign in to comment.