diff --git a/Cargo.lock b/Cargo.lock index 173ebe04..2a69b044 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -403,6 +403,27 @@ dependencies = [ "syn 2.0.106", ] +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.106", +] + [[package]] name = "digest" version = "0.10.7" @@ -468,11 +489,13 @@ version = "0.1.0" dependencies = [ "bumpalo", "bytemuck", + "derive_more", "dreammaker", "either", "foldhash", "gfx_core", "gif", + "glam", "indexmap", "inflate", "lodepng", @@ -530,6 +553,7 @@ dependencies = [ "foldhash", "get-size", "get-size-derive", + "glam", "indexmap", "interval-tree", "lodepng", @@ -777,6 +801,12 @@ dependencies = [ "url", ] +[[package]] +name = "glam" +version = "0.30.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19fc433e8437a212d1b6f1e68c7824af3aed907da60afa994e7f542d18d12aa9" + [[package]] name = "hashbrown" version = "0.15.5" @@ -1507,6 +1537,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rustls" version = "0.23.31" @@ -1563,6 +1602,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + [[package]] name = "serde" version = "1.0.219" diff --git a/crates/dmm-tools/Cargo.toml b/crates/dmm-tools/Cargo.toml index 31fdc979..1890e48c 100644 --- a/crates/dmm-tools/Cargo.toml +++ b/crates/dmm-tools/Cargo.toml @@ -13,6 +13,8 @@ lodepng = "3.10.7" indexmap = "2.6.0" foldhash = "0.2.0" either = "1.13.0" +glam = "0.30" +derive_more = {version = "2.1.1", features = ["deref"]} [dependencies.bytemuck] version = "1.19.0" diff --git a/crates/dmm-tools/src/dmm.rs b/crates/dmm-tools/src/dmm.rs index 39a42c4a..492dfe63 100644 --- a/crates/dmm-tools/src/dmm.rs +++ b/crates/dmm-tools/src/dmm.rs @@ -4,7 +4,9 @@ use std::fs::File; use std::io; use std::path::Path; +use derive_more::Deref; use foldhash::fast::RandomState; +use glam::{IVec2, IVec3}; use indexmap::IndexMap; use ndarray::{self, Array3, Axis}; @@ -28,36 +30,25 @@ pub struct Key(KeyType); /// An XY coordinate pair in the BYOND coordinate system. /// /// The lower-left corner is `{ x: 1, y: 1 }`. -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct Coord2 { - pub x: i32, - pub y: i32, -} +#[derive(Deref)] +pub struct Coord2(IVec2); impl Coord2 { - #[inline] - pub fn new(x: i32, y: i32) -> Coord2 { - Coord2 { x, y } - } - #[inline] pub fn z(self, z: i32) -> Coord3 { - Coord3 { - x: self.x, - y: self.y, - z, - } + self.extend(z).into() } + #[inline] fn to_raw(self, (dim_y, dim_x): (usize, usize)) -> (usize, usize) { assert!( - self.x >= 1 && self.x <= dim_x as i32, + (1..=(dim_x as i32)).contains(&self.x), "x={} not in [1, {}]", self.x, dim_x ); assert!( - self.y >= 1 && self.y <= dim_y as i32, + (1..=(dim_y as i32)).contains(&self.y), "y={} not in [1, {}]", self.y, dim_y @@ -65,11 +56,9 @@ impl Coord2 { (dim_y - self.y as usize, self.x as usize - 1) } + #[inline] fn from_raw((y, x): (usize, usize), (dim_y, _dim_x): (usize, usize)) -> Coord2 { - Coord2 { - x: x as i32 + 1, - y: (dim_y - y) as i32, - } + Coord2(IVec2::new(x as i32 + 1, (dim_y - y) as i32)) } } @@ -77,11 +66,13 @@ impl std::ops::Add for Coord2 { type Output = Coord2; fn add(self, rhs: Dir) -> Coord2 { - let (x, y) = rhs.offset(); - Coord2 { - x: self.x + x, - y: self.y + y, - } + Coord2(self.0 + rhs.offset()) + } +} + +impl From for Coord2 { + fn from(value: IVec2) -> Self { + Self(value) } } @@ -91,42 +82,26 @@ impl std::ops::Add for Coord2 { /// /// Note that BYOND by default considers "UP" to be Z+1, but this does not /// necessarily apply to a given game's logic. -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct Coord3 { - pub x: i32, - pub y: i32, - pub z: i32, -} +#[derive(Deref)] +pub struct Coord3(IVec3); impl Coord3 { #[inline] - pub fn new(x: i32, y: i32, z: i32) -> Coord3 { - Coord3 { x, y, z } - } - - #[inline] - pub fn xy(self) -> Coord2 { - Coord2 { - x: self.x, - y: self.y, - } - } - fn to_raw(self, (dim_z, dim_y, dim_x): (usize, usize, usize)) -> (usize, usize, usize) { assert!( - self.x >= 1 && self.x <= dim_x as i32, + (1..=(dim_x as i32)).contains(&self.x), "x={} not in [1, {}]", self.x, dim_x ); assert!( - self.y >= 1 && self.y <= dim_y as i32, + (1..=(dim_y as i32)).contains(&self.y), "y={} not in [1, {}]", self.y, dim_y ); assert!( - self.z >= 1 && self.z <= dim_z as i32, + (1..=(dim_z as i32)).contains(&self.z), "y={} not in [1, {}]", self.z, dim_z @@ -139,15 +114,18 @@ impl Coord3 { } #[allow(dead_code)] + #[inline] fn from_raw( (z, y, x): (usize, usize, usize), (_dim_z, dim_y, _dim_x): (usize, usize, usize), ) -> Coord3 { - Coord3 { - x: x as i32 + 1, - y: (dim_y - y) as i32, - z: z as i32 + 1, - } + Coord3(IVec3::new(x as i32 + 1, (dim_y - y) as i32, z as i32 + 1)) + } +} + +impl From for Coord3 { + fn from(value: IVec3) -> Self { + Self(value) } } diff --git a/crates/dreammaker/Cargo.toml b/crates/dreammaker/Cargo.toml index 01b096a5..04f247f8 100644 --- a/crates/dreammaker/Cargo.toml +++ b/crates/dreammaker/Cargo.toml @@ -22,6 +22,7 @@ derivative = "2.2.0" get-size = "0.1.4" get-size-derive = "0.1.3" beef = "0.5.2" +glam = "0.30" [dev-dependencies] walkdir = "2.5.0" diff --git a/crates/dreammaker/src/dmi.rs b/crates/dreammaker/src/dmi.rs index e4b9d6ea..21c4dd74 100644 --- a/crates/dreammaker/src/dmi.rs +++ b/crates/dreammaker/src/dmi.rs @@ -1,6 +1,7 @@ //! DMI metadata parsing and representation. use foldhash::{HashMap, HashMapExt}; +use glam::IVec2; use std::collections::BTreeMap; use std::fmt::Display; use std::io; @@ -190,16 +191,16 @@ impl Dir { } /// Get this direction's offset in BYOND's coordinate system. - pub fn offset(self) -> (i32, i32) { + pub fn offset(self) -> IVec2 { match self { - Dir::North => (0, 1), - Dir::South => (0, -1), - Dir::East => (1, 0), - Dir::West => (-1, 0), - Dir::Northeast => (1, 1), - Dir::Northwest => (-1, 1), - Dir::Southeast => (1, -1), - Dir::Southwest => (-1, -1), + Dir::North => IVec2::new(0, 1), + Dir::South => IVec2::new(0, -1), + Dir::East => IVec2::new(1, 0), + Dir::West => IVec2::new(-1, 0), + Dir::Northeast => IVec2::new(1, 1), + Dir::Northwest => IVec2::new(-1, 1), + Dir::Southeast => IVec2::new(1, -1), + Dir::Southwest => IVec2::new(-1, -1), } } }