mirror of
https://github.com/kristoferssolo/hexlab.git
synced 2025-10-21 19:40:34 +00:00
chore(lint): fix clippy warnings
This commit is contained in:
parent
e59271cf01
commit
dcc68ffc00
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -2063,7 +2063,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
|
||||
|
||||
[[package]]
|
||||
name = "hexlab"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"hexx",
|
||||
@ -2251,9 +2251,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.161"
|
||||
version = "0.2.167"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
|
||||
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
@ -3548,9 +3548,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.25.0"
|
||||
version = "0.25.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5902c5d130972a0000f60860bfbf46f7ca3db5391eddfedd1b8728bd9dc96c0e"
|
||||
checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31"
|
||||
|
||||
[[package]]
|
||||
name = "twox-hash"
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "hexlab"
|
||||
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
edition = "2021"
|
||||
description = "A hexagonal maze generation and manipulation library"
|
||||
repository = "https://github.com/kristoferssolo/hexlab"
|
||||
@ -46,3 +46,9 @@ panic = "abort" # Smaller binary size
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[lints.clippy]
|
||||
pedantic = "warn"
|
||||
nursery = "warn"
|
||||
unwrap_used = "warn"
|
||||
expect_used = "warn"
|
||||
|
||||
@ -76,6 +76,7 @@ pub enum MazeBuilderError {
|
||||
/// .build()
|
||||
/// .expect("Failed to create maze");
|
||||
/// ```
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
#[derive(Default)]
|
||||
pub struct MazeBuilder {
|
||||
radius: Option<u32>,
|
||||
@ -87,6 +88,7 @@ pub struct MazeBuilder {
|
||||
impl MazeBuilder {
|
||||
/// Creates a new [`MazeBuilder`] instance.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@ -97,7 +99,8 @@ impl MazeBuilder {
|
||||
///
|
||||
/// * `radius` - The size of the maze (number of tiles along one edge).
|
||||
#[inline]
|
||||
pub fn with_radius(mut self, radius: u32) -> Self {
|
||||
#[must_use]
|
||||
pub const fn with_radius(mut self, radius: u32) -> Self {
|
||||
self.radius = Some(radius);
|
||||
self
|
||||
}
|
||||
@ -108,7 +111,8 @@ impl MazeBuilder {
|
||||
///
|
||||
/// * `seed` - The random seed value.
|
||||
#[inline]
|
||||
pub fn with_seed(mut self, seed: u64) -> Self {
|
||||
#[must_use]
|
||||
pub const fn with_seed(mut self, seed: u64) -> Self {
|
||||
self.seed = Some(seed);
|
||||
self
|
||||
}
|
||||
@ -121,13 +125,15 @@ impl MazeBuilder {
|
||||
///
|
||||
/// * `generator_type` - The maze generation algorithm to use.
|
||||
#[inline]
|
||||
pub fn with_generator(mut self, generator_type: GeneratorType) -> Self {
|
||||
#[must_use]
|
||||
pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self {
|
||||
self.generator_type = generator_type;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_start_position(mut self, pos: Hex) -> Self {
|
||||
#[must_use]
|
||||
pub const fn with_start_position(mut self, pos: Hex) -> Self {
|
||||
self.start_position = Some(pos);
|
||||
self
|
||||
}
|
||||
@ -159,7 +165,7 @@ impl MazeBuilder {
|
||||
/// ```
|
||||
pub fn build(self) -> Result<HexMaze, MazeBuilderError> {
|
||||
let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?;
|
||||
let mut maze = self.create_hex_maze(radius);
|
||||
let mut maze = create_hex_maze(radius);
|
||||
|
||||
if let Some(start_pos) = self.start_position {
|
||||
if maze.get_tile(&start_pos).is_none() {
|
||||
@ -174,9 +180,18 @@ impl MazeBuilder {
|
||||
Ok(maze)
|
||||
}
|
||||
|
||||
fn create_hex_maze(&self, radius: u32) -> HexMaze {
|
||||
fn generate_maze(&self, maze: &mut HexMaze) {
|
||||
match self.generator_type {
|
||||
GeneratorType::RecursiveBacktracking => {
|
||||
generate_backtracking(maze, self.start_position, self.seed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn create_hex_maze(radius: u32) -> HexMaze {
|
||||
let mut maze = HexMaze::new();
|
||||
let radius = radius as i32;
|
||||
let radius = i32::try_from(radius).unwrap_or(5);
|
||||
|
||||
for q in -radius..=radius {
|
||||
let r1 = (-radius).max(-q - radius);
|
||||
let r2 = radius.min(-q + radius);
|
||||
@ -189,15 +204,6 @@ impl MazeBuilder {
|
||||
maze
|
||||
}
|
||||
|
||||
fn generate_maze(&self, maze: &mut HexMaze) {
|
||||
match self.generator_type {
|
||||
GeneratorType::RecursiveBacktracking => {
|
||||
generate_backtracking(maze, self.start_position, self.seed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use hexx::EdgeDirection;
|
||||
|
||||
@ -6,13 +6,14 @@ use rand_chacha::ChaCha8Rng;
|
||||
|
||||
use crate::HexMaze;
|
||||
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub enum GeneratorType {
|
||||
#[default]
|
||||
RecursiveBacktracking,
|
||||
}
|
||||
|
||||
pub(crate) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>, seed: Option<u64>) {
|
||||
pub fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>, seed: Option<u64>) {
|
||||
if maze.is_empty() {
|
||||
return;
|
||||
}
|
||||
@ -21,10 +22,10 @@ pub(crate) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>,
|
||||
|
||||
let mut visited = HashSet::new();
|
||||
|
||||
let mut rng: Box<dyn RngCore> = match seed {
|
||||
Some(seed) => Box::new(ChaCha8Rng::seed_from_u64(seed)),
|
||||
None => Box::new(thread_rng()),
|
||||
};
|
||||
let mut rng: Box<dyn RngCore> = seed.map_or_else(
|
||||
|| Box::new(thread_rng()) as Box<dyn RngCore>,
|
||||
|seed| Box::new(ChaCha8Rng::seed_from_u64(seed)) as Box<dyn RngCore>,
|
||||
);
|
||||
recursive_backtrack(maze, start, &mut visited, &mut rng);
|
||||
}
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ pub struct HexMaze(HashMap<Hex, HexTile>);
|
||||
impl HexMaze {
|
||||
/// Creates a new empty maze
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@ -28,29 +29,32 @@ impl HexMaze {
|
||||
/// Adds a wall in the specified direction at the given coordinates
|
||||
pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) {
|
||||
if let Some(tile) = self.0.get_mut(&coord) {
|
||||
tile.walls.add(direction)
|
||||
tile.walls.add(direction);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the tile at the specified coordinates
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> {
|
||||
self.0.get(coord)
|
||||
}
|
||||
|
||||
/// Returns a reference to the walls at the specified coordinates
|
||||
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
|
||||
self.0.get(coord).map(|tile| tile.walls())
|
||||
self.0.get(coord).map(HexTile::walls)
|
||||
}
|
||||
|
||||
/// Returns the number of tiles in the maze
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
/// Returns true if the maze is empty
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
@ -21,6 +21,7 @@ pub struct HexTile {
|
||||
|
||||
impl HexTile {
|
||||
/// Creates a new tile with pos and default walls
|
||||
#[must_use]
|
||||
pub fn new(pos: Hex) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
@ -30,22 +31,28 @@ impl HexTile {
|
||||
|
||||
/// Returns a reference to the tile's walls
|
||||
#[inline]
|
||||
pub fn walls(&self) -> &Walls {
|
||||
#[must_use]
|
||||
pub const fn walls(&self) -> &Walls {
|
||||
&self.walls
|
||||
}
|
||||
|
||||
/// Returns position of the tile
|
||||
#[inline]
|
||||
pub fn pos(&self) -> Hex {
|
||||
#[must_use]
|
||||
pub const fn pos(&self) -> Hex {
|
||||
self.pos
|
||||
}
|
||||
|
||||
#[cfg(feature = "bevy")]
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn to_vec2(&self, layout: &HexLayout) -> Vec2 {
|
||||
layout.hex_to_world_pos(self.pos)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bevy")]
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn to_vec3(&self, layout: &HexLayout) -> Vec3 {
|
||||
let pos = self.to_vec2(layout);
|
||||
Vec3::new(pos.x, 0., pos.y)
|
||||
@ -1,13 +1,13 @@
|
||||
mod builder;
|
||||
mod generator;
|
||||
mod maze;
|
||||
mod tile;
|
||||
mod hex_maze;
|
||||
mod hex_tile;
|
||||
mod walls;
|
||||
|
||||
pub use builder::{MazeBuilder, MazeBuilderError};
|
||||
pub use generator::GeneratorType;
|
||||
pub use maze::HexMaze;
|
||||
pub use tile::HexTile;
|
||||
pub use hex_maze::HexMaze;
|
||||
pub use hex_tile::HexTile;
|
||||
pub use walls::Walls;
|
||||
|
||||
pub mod prelude {
|
||||
|
||||
24
src/walls.rs
24
src/walls.rs
@ -62,6 +62,8 @@ impl Walls {
|
||||
/// assert!(walls.is_closed());
|
||||
/// assert_eq!(walls.count(), 6);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@ -77,7 +79,9 @@ impl Walls {
|
||||
/// assert!(walls.is_empty());
|
||||
/// assert_eq!(walls.count(), 0);
|
||||
/// ```
|
||||
pub fn empty() -> Self {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn empty() -> Self {
|
||||
Self(0)
|
||||
}
|
||||
|
||||
@ -95,7 +99,9 @@ impl Walls {
|
||||
/// let walls = Walls::new();
|
||||
/// assert!(!walls.is_empty());
|
||||
/// ```
|
||||
pub fn is_empty(&self) -> bool {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn is_empty(&self) -> bool {
|
||||
self.0 == 0
|
||||
}
|
||||
|
||||
@ -193,7 +199,8 @@ impl Walls {
|
||||
/// assert_eq!(walls.as_bits(), 0b111111);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn as_bits(&self) -> u8 {
|
||||
#[must_use]
|
||||
pub const fn as_bits(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
|
||||
@ -215,8 +222,9 @@ impl Walls {
|
||||
/// assert_eq!(walls.count(), 2);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn count(&self) -> u8 {
|
||||
self.0.count_ones() as u8
|
||||
u8::try_from(self.0.count_ones()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Returns all possible directions as a `Walls` value
|
||||
@ -234,8 +242,9 @@ impl Walls {
|
||||
/// assert!(all_walls.is_closed());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn all_directions() -> Self {
|
||||
Self(0b111111)
|
||||
#[must_use]
|
||||
pub const fn all_directions() -> Self {
|
||||
Self(0b11_1111)
|
||||
}
|
||||
|
||||
/// Toggles a wall in the specified direction.
|
||||
@ -293,6 +302,7 @@ impl Walls {
|
||||
/// assert!(walls.is_closed());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn is_closed(&self) -> bool {
|
||||
self.count() == 6
|
||||
}
|
||||
@ -354,7 +364,7 @@ impl<const N: usize> From<[EdgeDirection; N]> for Walls {
|
||||
|
||||
impl Default for Walls {
|
||||
fn default() -> Self {
|
||||
Self(0b111111)
|
||||
Self(0b11_1111)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user