From 882e30aa5e859cf8cd951feac8a95cb7f8d09bf6 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 13 Oct 2024 19:57:07 -0400 Subject: [PATCH] Begin refactoring `aneri-pathfind`; update `Cargo.lock` --- Cargo.lock | 20 ++++++++----- crates/pathfind/Cargo.toml | 1 + crates/pathfind/src/direction.rs | 16 ++++++++++ crates/pathfind/src/lib.rs | 7 +++++ crates/pathfind/src/map.rs | 50 ++++++++++++++++++++++++++++++++ crates/pathfind/src/position.rs | 39 +++++++++++++++++++++++++ crates/pathfind/src/tile.rs | 9 ++++++ 7 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 crates/pathfind/src/direction.rs create mode 100644 crates/pathfind/src/map.rs create mode 100644 crates/pathfind/src/position.rs create mode 100644 crates/pathfind/src/tile.rs diff --git a/Cargo.lock b/Cargo.lock index 1e7377a..bec3e22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,7 @@ version = "0.1.0" dependencies = [ "ahash 0.8.11", "bitflags 2.6.0", + "grid", "meowtonin", "ordered-float", "pathfinding", @@ -528,9 +529,9 @@ dependencies = [ [[package]] name = "bytemuck" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" +checksum = "8334215b81e418a0a7bdb8ef0849474f40bb10c8b71f1c4ed315cff49f32494d" [[package]] name = "byteorder" @@ -552,9 +553,9 @@ checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "cc" -version = "1.1.29" +version = "1.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58e804ac3194a48bb129643eb1d62fcc20d18c6b8c181704489353d13120bcd1" +checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" dependencies = [ "jobserver", "libc", @@ -1655,6 +1656,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "grid" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36119f3a540b086b4e436bb2b588cf98a68863470e0e880f4d0842f112a3183a" + [[package]] name = "hash32" version = "0.3.1" @@ -1736,14 +1743,13 @@ dependencies = [ [[package]] name = "hoot" -version = "0.2.3" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "936723a03900105673cf6efb8821399ebdc287a494949c375f2ce50db83bf6c9" +checksum = "6a018c1f2075066355e95ac5ab7909d35a84c9d14d1fae84c4bacdf6dec188d8" dependencies = [ "http", "httparse", "log", - "tinyvec", "url", ] diff --git a/crates/pathfind/Cargo.toml b/crates/pathfind/Cargo.toml index fd8b351..a583ad6 100644 --- a/crates/pathfind/Cargo.toml +++ b/crates/pathfind/Cargo.toml @@ -14,3 +14,4 @@ meowtonin = { workspace = true } pathfinding = "4" bitflags = "2" ordered-float = "4" +grid = "0.15" diff --git a/crates/pathfind/src/direction.rs b/crates/pathfind/src/direction.rs new file mode 100644 index 0000000..26d004c --- /dev/null +++ b/crates/pathfind/src/direction.rs @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: MPL-2.0 +use bitflags::bitflags; + +bitflags! { + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] + pub struct Direction: u8 { + const NORTH = 1; + const SOUTH = 2; + const EAST = 4; + const WEST = 8; + const NORTHEAST = Self::NORTH.bits() | Self::EAST.bits(); + const NORTHWEST = Self::NORTH.bits() | Self::WEST.bits(); + const SOUTHEAST = Self::SOUTH.bits() | Self::EAST.bits(); + const SOUTHWEST = Self::SOUTH.bits() | Self::WEST.bits(); + } +} diff --git a/crates/pathfind/src/lib.rs b/crates/pathfind/src/lib.rs index 55ff1df..535066a 100644 --- a/crates/pathfind/src/lib.rs +++ b/crates/pathfind/src/lib.rs @@ -6,6 +6,12 @@ clippy::perf, clippy::style )] +pub mod direction; +pub mod map; +pub mod position; +pub mod tile; + +/* use ahash::AHashMap; use bitflags::bitflags; use ordered_float::OrderedFloat; @@ -184,3 +190,4 @@ pub fn main() { } } */ +*/ diff --git a/crates/pathfind/src/map.rs b/crates/pathfind/src/map.rs new file mode 100644 index 0000000..99966d5 --- /dev/null +++ b/crates/pathfind/src/map.rs @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: MPL-2.0 +use crate::{position::Position, tile::Tile}; +use grid::Grid; + +pub struct GameMap { + grid: Grid>, +} + +impl GameMap { + pub fn new(width: usize, height: usize) -> Self { + Self { + grid: Grid::new(width, height), + } + } + + pub fn get_tile(&self, pos: Position) -> Option<&Tile> { + self.grid + .get(pos.x(), pos.y()) + .and_then(|tile| tile.as_ref()) + } + + pub fn get_tiles(&self, pos_list: T) -> Vec> + where + T: AsRef<[Position]>, + { + pos_list + .as_ref() + .iter() + .map(|&pos| self.get_tile(pos)) + .collect() + } + + pub fn set_tile(&mut self, pos: Position, tile: T) -> Option + where + T: Into>, + { + let tile_ref = self.grid.get_mut(pos.x(), pos.y())?; + let mut tile = tile.into(); + std::mem::swap(tile_ref, &mut tile); + tile + } + + pub fn width(&self) -> usize { + self.grid.rows() + } + + pub fn height(&self) -> usize { + self.grid.cols() + } +} diff --git a/crates/pathfind/src/position.rs b/crates/pathfind/src/position.rs new file mode 100644 index 0000000..3dbb94e --- /dev/null +++ b/crates/pathfind/src/position.rs @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MPL-2.0 + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Position { + x: u16, + y: u16, +} + +impl Position { + /// Creates a new [`Position`], from 0-indexed x and y coordinates. + pub const fn new(x: u16, y: u16) -> Self { + Self { x, y } + } + + /// Creates a new [`Position`], from 1-indexed x and y coordinates. + pub const fn new_1i(x: u16, y: u16) -> Self { + Self::new(x.saturating_sub(1), y.saturating_sub(1)) + } + + /// Gets the x coordinate of this [`Position`], as a 0-indexed [`u16`]. + pub const fn x(&self) -> u16 { + self.x + } + + /// Gets the y coordinate of this [`Position`], as a 0-indexed [`u16`]. + pub const fn y(&self) -> u16 { + self.y + } + + /// Gets the x coordinate of this [`Position`], as a 1-indexed [`u16`]. + pub const fn x_1(&self) -> u16 { + self.x().saturating_add(1) + } + + /// Gets the y coordinate of this [`Position`], as a 1-indexed [`u16`]. + pub const fn y_1(&self) -> u16 { + self.y().saturating_add(1) + } +} diff --git a/crates/pathfind/src/tile.rs b/crates/pathfind/src/tile.rs new file mode 100644 index 0000000..10c24e4 --- /dev/null +++ b/crates/pathfind/src/tile.rs @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: MPL-2.0 +use crate::direction::Direction; +use ordered_float::OrderedFloat; + +pub struct Tile { + pub weight: OrderedFloat, + pub passable_directions: Direction, + pub condition: Option bool>>, +}